Tuesday, October 05, 2010

Creando p2cNB parte 3

Conituando con nuestro plugin de migración de postgresql a couchdb, luego de realizar el diseño creamos la clase Sesion.java
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;

public class Sesion {

private static String ipCouchdb = "localhost";
private static int puertoCouchdb = 5984;
private static String userCouchdb;
private static String passwordCouchdb;
private static String ipPostgresql = "localhost";
private static int puertoPostgresql = 5432;
private static String databasesPostresql;
private static String userPostgresql;
private static String passwordPostgresql = "";
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 ListaTablas = new ArrayList();

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 getListaTablas() {
return ListaTablas;
}

public static void setListaTablas(List ListaTablas) {
Sesion.ListaTablas = ListaTablas;
}

public String getIpPostgresql() {
return ipPostgresql;
}

public void setIpPostgresql(String ipPostgresql) {
this.ipPostgresql = ipPostgresql;
}

public static String getDatabasesPostresql() {
return databasesPostresql;
}

public static void setDatabasesPostresql(String databasesPostresql) {
Sesion.databasesPostresql = databasesPostresql;
}

public String getPasswordPostgresql() {
return passwordPostgresql;
}

public void setPasswordPostgresql(String passwordPostgresql) {
this.passwordPostgresql = passwordPostgresql;
}

public int getPuertoPostgresql() {
return puertoPostgresql;
}

public void setPuertoPostgresql(int puertoPostgresql) {
this.puertoPostgresql = puertoPostgresql;
}

public String getUserPostgresql() {
return userPostgresql;
}

public void setUserPostgresql(String userPostgresql) {
this.userPostgresql = userPostgresql;
}

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;
}

public boolean ConectarPostgresql() {
try {

Class.forName("org.postgresql.Driver");
String url = "jdbc:postgresql://" + ipPostgresql + ":" + puertoPostgresql + "/" + databasesPostresql;

conn = DriverManager.getConnection(url, userPostgresql, passwordPostgresql);
if (conn != null) {
return true;
}
} catch (Exception ex) {

JOptionPane.showMessageDialog(null, "ConectarPostresql()\nMensaje: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
return false;
}

public String ObtenerListaTablas() {
try {

dmd = conn.getMetaData();
// Get driver name
String dname = dmd.getDriverName();
String dver = dmd.getDriverVersion();


ResultSet rs1 = dmd.getSchemas();

while (rs1.next()) {
String ss = rs1.getString(1);
ResultSet rs2 = dmd.getTables(null, ss, "%", null);
while (rs2.next()) {
if (rs2.getString(4).equals("TABLE")) {
ListaTablas.add(rs2.getString(3));

}
}
}

conn.close();
} catch (Exception ex) {

JOptionPane.showMessageDialog(null, "ConectarPostresql()\nMensaje: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
return "";
}

/*
*inicio
*/
public boolean ConectarCouchDB() {
try {
dbSession = new Session(ipCouchdb, puertoCouchdb);
String host = dbSession.getHost();
//
// dbSession
List 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);
}
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);
}
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);
}
return false;
}

public void AgregarDocumentos() {
try {

doc = new Document();
doc.setId("myid");
doc.put("nombre", "valor");

db.saveDocument(doc);
} catch (Exception ex) {

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) { StatusDisplayer.getDefault().setStatusText("FechaToString(). Error " + ex.getMessage().toString()); } return ""; } public void procesarTabla(String tabla) { try { if (ConectarPostgresql() == false) { JOptionPane.showMessageDialog(null, "No se pudo establecer la conexión verifique los datos"); return; } String name_couchdb = prefijo + tabla; if (ConectarCouchDB() == true) { if (CrearBaseDatos(name_couchdb) == true) { if (AbrirBaseDatos(name_couchdb) == true) { 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()); } } 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()); } } } Crear la clase NBRunnable, para ejecutar múltiples hilos.
package org.avbravo.p2cnb;

import org.netbeans.api.progress.ProgressHandle;
import org.netbeans.api.progress.ProgressHandleFactory;
import org.openide.awt.StatusDisplayer;

/**
*
* @author avbravo
*/
public class NBRunnable implements Runnable {

Sesion sesion = new Sesion();

@Override
public void run() {

try{

for (int i = 0; i < sesion.getListaTablas().size(); i++) {
ProgressHandle p = ProgressHandleFactory.createHandle(
"Procesando tabla: " + sesion.getListaTablas().get(i)
+ " espere....");
p.start();
sesion.procesarTabla(sesion.getListaTablas().get(i));
// break;
p.finish();
}
StatusDisplayer.getDefault().setStatusText("Migración terminada...");
// p.finish();
}catch(Exception ex){
StatusDisplayer.getDefault().setStatusText("Error "+ex.getMessage().toString());
}

}
}

1 comment:

Anonymous said...

Hola lectura de esta mensaje es muy palpitante , temas como éste animan quien ler esta página web.....