Crear la clase Sesion.java
Donde administraremos las conexiones a las bases de datos
Donde administraremos las conexiones a las bases de datos
Donde administraremos las conexiones a las bases de datos
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.avbravo.couchdb;
/**
*
* @author avbravo
*/
import com.fourspaces.couchdb.Database;
import com.fourspaces.couchdb.Document;
import com.fourspaces.couchdb.Session;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.swing.JOptionPane;
import org.openide.awt.StatusDisplayer;
/**
*
* @author avbravo
*/
public class Sesion {
private
private
private
private
static
static
static
static
String ipCouchdb = "localhost";
int puertoCouchdb = 5984;
String userCouchdb;
String passwordCouchdb;
private static String prefijo;
private static String formatoFecha;
public static Session dbSession;
public static Database db;
public static Document doc;
static Connection conn;
static Statement stmt;
static ResultSet rs;
static ResultSetMetaData mdata;
static DatabaseMetaData dmd;
int num_columnas;
static List<String> ListaTablas = new ArrayList<String>();
static List<String> ListaMensajes = new ArrayList<String>();
public static List<String> getListaMensajes() {
return ListaMensajes;
}
public static void setListaMensajes(List<String> ListaMensajes) {
Sesion.ListaMensajes = ListaMensajes;
}
public static String getFormatoFecha() {
return formatoFecha;
}
public static void setFormatoFecha(String formatoFecha) {
Sesion.formatoFecha = formatoFecha;
}
public static String getPrefijo() {
return prefijo;
}
public static void setPrefijo(String prefijo) {
Sesion.prefijo = prefijo;
}
public static List<String> getListaTablas() {
return ListaTablas;
}
public static void setListaTablas(List<String> ListaTablas) {
Sesion.ListaTablas = ListaTablas;
}
public String getIpCouchdb() {
return ipCouchdb;
}
public void setIpCouchdb(String ipCouchdb) {
this.ipCouchdb = ipCouchdb;
}
public String getPasswordCouchdb() {
return passwordCouchdb;
}
public void setPasswordCouchdb(String passwordCouchdb) {
this.passwordCouchdb = passwordCouchdb;
}
public int getPuertoCouchdb() {
return puertoCouchdb;
}
public void setPuertoCouchdb(int puertoCouchdb) {
this.puertoCouchdb = puertoCouchdb;
}
public String getUserCouchdb() {
return userCouchdb;
}
public void setUserCouchdb(String userCouchdb) {
this.userCouchdb = userCouchdb;
}
public static DatabaseMetaData getDmd() {
return dmd;
}
public static void setDmd(DatabaseMetaData dmd) {
Sesion.dmd = dmd;
}
/*
* Agrega una tabla a la lista
*/
public void AgregarTablaLista(String tabla){
try{
this.ListaTablas.add(tabla);
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "AgregarTablaLista()\nMensaje: " +
ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
/*
*inicio
*/
public boolean ConectarCouchDB() {
try {
dbSession = new Session(ipCouchdb, puertoCouchdb);
String host = dbSession.getHost();
//
// dbSession
List<String> databasesCouchdb = dbSession.getDatabaseNames();
StatusDisplayer.getDefault().setStatusText("host..." + dbSession.getHost());
if (dbSession == null) {
return false;
}
return true;
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "ConectarCouchDB()\nMensaje: " +
ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
this.ListaMensajes.add("ConectarCouchDB) " +
ex.getLocalizedMessage().toString());
StatusDisplayer.getDefault().setStatusText("ConectarCouchDB. Error " +
ex.getMessage().toString());
}
return false;
}
public boolean CrearBaseDatos(String dbname) {
try {
db = dbSession.createDatabase(dbname);
if (db == null) {
return false;
}
return true;
} catch (Exception ex) {
// JOptionPane.showMessageDialog(null, "CrearBaseDatos()\nMensaje: " +
ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
this.ListaMensajes.add("CrearBaseDatos(String dbname) " +
ex.getLocalizedMessage().toString());
StatusDisplayer.getDefault().setStatusText("CrearBaseDatos(String dbname).
Error " + ex.getMessage().toString());
}
return false;
}
public boolean AbrirBaseDatos(String dbname) {
try {
db = dbSession.getDatabase(dbname);
if (db == null) {
return false;
}
return true;
} catch (Exception ex) {
// JOptionPane.showMessageDialog(null, "AbrirBaseDatos()\nMensaje: " +
ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
this.ListaMensajes.add("AbrirBaseDatos(String dbname) " +
ex.getLocalizedMessage().toString());
StatusDisplayer.getDefault().setStatusText("AbrirBaseDatos(String dbname).
Error " + ex.getMessage().toString());
}
return false;
}
public void AgregarDocumentos() {
try {
doc = new Document();
doc.setId("myid");
doc.put("nombre", "valor");
db.saveDocument(doc);
} catch (Exception ex) {
this.ListaMensajes.add("AgregarDocumentos " + ex.getLocalizedMessage().toString());
StatusDisplayer.getDefault().setStatusText("AgregarDocumentos(). Error " +
ex.getMessage().toString());
}
}
public String FechaToString(Date date) {
try {
if (date == null) {
return "";
}
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int day = cal.get(Calendar.DATE);
int month = cal.get(Calendar.MONTH) + 1;
int year = cal.get(Calendar.YEAR);
String sfecha;
String sdia, smonth, syear;
if (day < 10) {
sdia = "0" + String.valueOf(day);
} else {
sdia = String.valueOf(day);
}
if (month < 10) {
smonth = String.valueOf(month);
} else {
smonth = String.valueOf(month);
}
syear = String.valueOf(year);
formatoFecha = formatoFecha.trim();
if (formatoFecha.equals("dd/MM/aaaa")) {
return sdia + "/" + month + "/" + syear;
}
if (formatoFecha.equals("MM/dd/aaaa")) {
return smonth + "/" + sdia + "/" + syear;
}
if (formatoFecha.equals("aaaa/MM/dd")) {
return syear + "/" + smonth + "/" + sdia;
}
if (formatoFecha.equals("dd/aaaa/MM")) {
return sdia + "/" + syear + "/" + month;
}
if (formatoFecha.equals("MM/aaaa/dd")) {
return smonth + "/" + syear + "/" + sdia;
}
if (formatoFecha.equals("aaaa/dd/MM")) {
return syear + "/" + sdia + "/" + smonth;
}
} catch (Exception ex) {
// JOptionPane.showMessageDialog(null, "FechaToString()\nMensaje: " +
ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
StatusDisplayer.getDefault().setStatusText("FechaToString(). Error " +
ex.getMessage().toString());
this.ListaMensajes.add("FechaToString() "
+ex.getLocalizedMessage().toString());
}
return "";
}
public void procesarTabla(String tabla) {
try {
String name_couchdb = prefijo + tabla;
if (ConectarCouchDB() == true) {
if (CrearBaseDatos(name_couchdb) == true) {
if (AbrirBaseDatos(name_couchdb) == true) {
this.ListaMensajes.add("Migrando
: " + tabla + " a " + name_couchdb);
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from " + tabla);
mdata = rs.getMetaData(); //metadata
num_columnas = mdata.getColumnCount(); //numero columnas
migrar();
}
stmt.close();
rs.close();
// conn.close();
}
}
} catch (Exception ex) {
StatusDisplayer.getDefault().setStatusText("procesarTabla(). Error " +
ex.getMessage().toString());
this.ListaMensajes.add("procesarTabla() "
+ex.getLocalizedMessage().toString());
}
}
private void migrar() {
try {
while (rs.next()) {
Document doc = new Document();
for (int i = 1; i <= num_columnas; i++) {
// Recorro las columnas
if (mdata.getColumnTypeName(i).equals("date")) {
String fecha = FechaToString(rs.getDate(i));
doc.put(mdata.getColumnLabel(i), fecha);
} else {
doc.put(mdata.getColumnLabel(i), rs.getString(i));
}
}
db.saveDocument(doc);
}
} catch (Exception ex) {
StatusDisplayer.getDefault().setStatusText("migrar(). Error " +
ex.getMessage().toString());
this.ListaMensajes.add("migrar() " +ex.getLocalizedMessage().toString());
}
}
}
CREAR LA CLASE DBGraphScene
package org.avbravo.couchdb;
import java.awt.Image;
import java.awt.Point;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import org.netbeans.api.visual.vmd.VMDGraphScene;
import org.netbeans.api.visual.vmd.VMDNodeWidget;
import org.netbeans.api.visual.vmd.VMDPinWidget;
import org.openide.util.ImageUtilities;
/**
*
* @author avbravo
*/
public class DBGraphScene extends VMDGraphScene {
private static final Image IMAGE_LIST =
ImageUtilities.loadImage("org/avbravo/couchdb/resources/list_16.png"); // NOI18N
private static final Image IMAGE_ITEM =
ImageUtilities.loadImage("org/avbravo/couchdb/resources/item_16.png"); // NOI18N
private static int edgeID = 1;
Sesion sesion = new Sesion();
public DBGraphScene(Connection connection) {
try {
createSceneFromConnection(connection);
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "DBGraphScene()" + e.getLocalizedMessage().toString());
}
}
private void createSceneFromConnection(Connection jdbcConnection) throws SQLException {
try {
ArrayList<String> tables = new ArrayList<String>();
DatabaseMetaData databaseMetaData = jdbcConnection.getMetaData();
String[] names = {"TABLE"};
ResultSet resultSet = databaseMetaData.getTables(null, "%", "%", names);
while (resultSet.next()) {
String table = resultSet.getString("TABLE_NAME");
sesion.AgregarTablaLista(table);
tables.add(table);
createNode(this, (int) (Math.random() * 800), (int) (Math.random() * 800), IMAGE_LIST, table,
"Table", null);
// ResultSet columns = jdbcConnection.getMetaData().getColumns(null, null, table.toUpperCase(),
"%");
//Quitar el metodo toUpper() para evitar que no reconozca tablas con
//nombres en minuscula
ResultSet columns = jdbcConnection.getMetaData().getColumns(null, null, table, "%");
while (columns.next()) {
String columnName = columns.getString("COLUMN_NAME");
createPin(this, table, table + ":" + columnName, IMAGE_ITEM, columnName, columnName);
}
}
for (String string : tables) {
ResultSet resultSet1 = databaseMetaData.getExportedKeys(null, null, string);
while (resultSet1.next()) {
String pkTable = resultSet1.getString("PKTABLE_NAME");
String pkColumn = resultSet1.getString("PKCOLUMN_NAME");
String fkTable = resultSet1.getString("FKTABLE_NAME");
String fkColumn = resultSet1.getString("FKCOLUMN_NAME");
/*
*Para mostrar las relaciones de las tablas
* quitar el comentario de esta linea
* pendiente verificar el comportamiento con postgresql
*/
createEdge(this, fkTable + ":" + fkColumn, pkTable + ":" + pkColumn);
}
}
this.moveTo(null);
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "createSceneFromConnection<()" +
ex.getLocalizedMessage().toString());
}
}
private static String createNode(VMDGraphScene scene, int x, int y, Image image, String name, String type,
java.util.List<Image> glyphs) {
String node = name;
try {
VMDNodeWidget widget = (VMDNodeWidget) scene.addNode(node);
widget.setPreferredLocation(new Point(x, y));
widget.setNodeProperties(image, name, type, glyphs);
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "createNode()" + ex.getLocalizedMessage().toString());
}
return node;
}
private static void createPin(VMDGraphScene scene, String nodeID, String pinID, Image image, String
name, String type) {
((VMDPinWidget) scene.addPin(nodeID, pinID)).setProperties(name, null);
}
private static void createEdge(VMDGraphScene scene, String sourcePinID, String targetPinID) {
try {
String edge = "edge" + DBGraphScene.edgeID++;
scene.addEdge(edge);
scene.setEdgeSource(edge, sourcePinID);
scene.setEdgeTarget(edge, targetPinID);
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "createEdge()" + ex.getLocalizedMessage().toString());
}
}
private void moveTo(Point point) {
try {
int index = 0;
for (String node : getNodes()) {
getSceneAnimator().animatePreferredLocation(findWidget(node), point != null ? point : new Point(+
+index * 100, index * 100));
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "moveTo()" + ex.getLocalizedMessage().toString());
}
}
}
CREAR EL PAQUETE org.avbravo.couchdb.resources
Crear el paquete y guardar las imĆ”genes allĆ.
Agregar imƔgenes
Comments