Annotation of FM2SQL/Convert.java, revision 1.36

1.1       rogo        1: import java.util.*;
                      2: import java.sql.*;
                      3: import java.awt.Cursor;
                      4: import java.io.BufferedReader;
                      5: import java.io.BufferedWriter;
                      6: import java.io.File;
                      7: import java.io.FileInputStream;
                      8: import java.io.FileNotFoundException;
                      9: import java.io.FileOutputStream;
                     10: import java.io.InputStreamReader;
                     11: import java.io.OutputStreamWriter;
                     12: import java.io.PrintStream;
                     13: import java.io.UnsupportedEncodingException;
                     14: 
                     15: import com.exploringxml.xml.Node;
                     16: import com.exploringxml.xml.Xparse;
                     17: 
1.34      rogo       18: class Convert
1.1       rogo       19: {
1.34      rogo       20:   static DBBean bean = new DBBean();
                     21:   static DBBean beanDest = new DBBean();
1.1       rogo       22: 
1.34      rogo       23:   static String user = "", passwd = "e1nste1n";
                     24:   static String userDest = "postgres", passwdDest = "rogo";
                     25:   static boolean batchRun = false;
1.1       rogo       26:   static Vector databases = new Vector();
1.30      rogo       27:   final static int numHits = 5000;
                     28:   final static int numIntervalls = 2;
1.1       rogo       29:   public static void main(String args[])
                     30:   {
1.34      rogo       31:     /*    try
                     32:         {
                     33:           //byte[] b = "ö".getBytes("UTF-8");
                     34:         //  System.out.println("QueryString " +b[0]+" "+b[1]+(new String(b).getBytes()[0])+" "+new String(b).getBytes()[1]);
                     35:         //System.out.println(new String(b,"UTF-8"));
                     36:         } catch (UnsupportedEncodingException e)
                     37:         {
                     38:           e.printStackTrace();
                     39:         }*/
1.1       rogo       40:     FileOutputStream file = null;
1.34      rogo       41:     if (args.length != 1)
1.1       rogo       42:     {
1.34      rogo       43:       System.out.println("Usage: java Convert <xml config file>");
                     44:       System.exit(-1);
1.1       rogo       45:     }
1.34      rogo       46:     if (!(new File(args[0]).exists()))
                     47:       System.exit(0);
1.1       rogo       48:     try
                     49:     {
                     50:       file = new FileOutputStream("./log.txt");
                     51:     } catch (FileNotFoundException e1)
                     52:     {
                     53:       e1.printStackTrace();
1.34      rogo       54:     }
                     55:     PrintStream stream = new PrintStream(file);
                     56:     System.setOut(stream);
                     57:     System.setErr(stream);
                     58:     readXMLFile(args[0]);
                     59:     System.out.println("Finished!");
1.1       rogo       60:     //convert("jdbc:fmpro:http://141.14.237.74:8050","jdbc:postgresql://erebos/test",null,null);
                     61:   }
1.34      rogo       62:   public static void convertBatch(DBBean source, DBBean destination, Vector names, Vector layouts, Vector selects, Vector creates, Vector ids,int mode) throws Exception
1.1       rogo       63:   {
1.34      rogo       64:     bean = source;
                     65:     beanDest = destination;
                     66:     convert(null,null,names,layouts,selects,creates,ids,mode);
                     67:     if(true) return;
                     68:     StringBuffer command = null;
1.1       rogo       69:     try
                     70:     {
                     71:       bean.setConnection(source.url);
1.34      rogo       72:       if (names == null)
                     73:         names = bean.getTableNames();
1.1       rogo       74:       //Collections.sort(names);
1.34      rogo       75:       int tbIndex = 1;
                     76: 
                     77:       for (tbIndex = 0; tbIndex < names.size(); ++tbIndex)
1.1       rogo       78:       {
                     79:         Vector[] result = null;
1.34      rogo       80:         try
1.1       rogo       81:         {
1.34      rogo       82:           String query = "select * from " + bean.getQC() + names.get(tbIndex).toString() + bean.getQC();
                     83:           String layout = (layouts.isEmpty()) ? "" : layouts.get(tbIndex).toString();
                     84:           query = (selects != null) ? selects.get(tbIndex).toString() : query;
                     85:           //if  vectors[1].get(i) != null)
                     86:           if (!layout.equals(""))
                     87:           {
                     88:             System.out.println("before " + query + " table" + names.get(tbIndex));
                     89:             layout = " layout " + bean.getQC() + layout + bean.getQC();
                     90:             String name = names.get(tbIndex).toString();
                     91:             StringBuffer queryLayout = new StringBuffer(query);
                     92:             queryLayout.insert(queryLayout.indexOf(name) + name.length() + 1, " " + layout);
                     93:             query = queryLayout.toString();
                     94:             System.out.println("added layout " + query);
                     95: 
                     96:           }
                     97:           System.out.println(" performing query " + query);
1.1       rogo       98:           //result = bean.getQueryData(query, null, 0);
1.34      rogo       99:           bean.getConnection();
                    100:           bean.makeQuery(query, 0);
                    101:         } catch (Exception e)
1.1       rogo      102:         {
                    103:           System.out.println(e.getMessage());
                    104:           e.printStackTrace();
                    105:           continue;
                    106:         }
                    107:         //beanDest.setConnection("jdbc:postgresql://erebos/test3");
                    108:         beanDest.setConnection(destination.url);
                    109: 
                    110:         Statement stm = beanDest.getConnection().createStatement();
                    111: 
                    112:         Vector tables = beanDest.getTableNames();
1.34      rogo      113:         //   Collections.sort(tables);
                    114:         System.out.println("converting table " + names.get(tbIndex) + " " + tables.indexOf(convertText((String) names.get(tbIndex)))); // "//beanDest.getTypeNames()); 
1.1       rogo      115:         tables = beanDest.getTableNames();
                    116:         // System.out.println(beanDest.getTableNames(beanDest.getCatalogs().get(2).toString()));
                    117:         stm = beanDest.getConnection().createStatement();
                    118:         // System.exit(0);
1.34      rogo      119:         if (mode == Convert.DataBase.CONVERT_MODE)
1.1       rogo      120:         {
1.34      rogo      121:           if (tables.indexOf(names.get(tbIndex)) >= 0)
                    122:           {
                    123:             stm.executeUpdate("drop table " + beanDest.getQC() + names.get(tbIndex) + beanDest.getQC());
                    124:             tables.remove((String) names.get(tbIndex));
                    125:             System.out.println("dropped table " + names.get(tbIndex));
                    126:           } else if (tables.indexOf(convertText(names.get(tbIndex).toString())) >= 0)
1.1       rogo      127:           {
1.34      rogo      128:             stm.executeUpdate("drop table " + beanDest.getQC() + convertText((String) names.get(tbIndex)) + beanDest.getQC());
                    129:             tables.remove(convertText((String) names.get(tbIndex)));
                    130:             System.out.println("dropped table " + names.get(tbIndex));
                    131:           }
                    132: 
                    133:           if (tables.indexOf(names.get(tbIndex)) < 0 && tables.indexOf(convertText(names.get(tbIndex).toString())) < 0)
1.1       rogo      134:           {
1.34      rogo      135:             if (creates.get(tbIndex).equals("") || creates.get(tbIndex).toString().toLowerCase().indexOf("create") < 0)
                    136:             {
                    137:               System.out.println("Warning empty or invalid create statement - creating one for you\n");
                    138: 
                    139:               command = new StringBuffer(50);
                    140:               command.append("CREATE TABLE ");
                    141:               command.append(beanDest.getQC());
                    142:               command.append(convertText((String) names.get(tbIndex)));
                    143:               command.append(beanDest.getQC());
                    144:               command.append("(");
                    145:               String type = null;
                    146:               Vector columnNames = bean.getColumnNames();
                    147:               for (int i = 0; i < columnNames.size() - 1; ++i)
                    148:               {
                    149:                 type = bean.metaData.getColumnTypeName(i + 1);
                    150:                 //   System.out.println(i+" "+result[1].get(i)+" "+type);
                    151:                 type = (type.equals("NUMBER")) ? "INT4" : type;
                    152:                 type = (type.equals("CONTAINER")) ? "TEXT" : type;
                    153: 
                    154:                 command.append(beanDest.getQC() + convertText((String) columnNames.get(i)) + beanDest.getQC() + " " + type + ", ");
                    155:               }
                    156:               type = bean.metaData.getColumnTypeName(columnNames.size());
                    157:               type = (type.equals("NUMBER")) ? "INT4" : type;
                    158:               type = (type.equals("CONTAINER")) ? "TEXT" : type;
                    159:               command.append(beanDest.getQC() + convertText((String) columnNames.get(columnNames.size() - 1)) + beanDest.getQC() + " " + type);
                    160:               command.append(" )");
                    161:             } else
                    162:               command = new StringBuffer().append(creates.get(tbIndex).toString());
                    163: 
                    164:             System.out.println(command);
                    165:             //  System.exit(0);
                    166:             //command.append(DBBean.getQC());   
                    167:             stm.executeUpdate(command.toString());
1.1       rogo      168: 
1.34      rogo      169:           }
1.1       rogo      170:         }
1.34      rogo      171:         Vector row = null;
                    172:         command = new StringBuffer();
1.1       rogo      173: 
                    174:         command.append("INSERT  INTO ");
                    175:         command.append(beanDest.getQC());
                    176:         command.append(convertText((String) names.get(tbIndex)));
                    177:         command.append(beanDest.getQC());
                    178:         command.append(" values ( ");
1.3       rogo      179: 
1.34      rogo      180:         for (int i = 0; i < bean.getColumnNames().size() - 1; ++i)
                    181:           command.append("?,");
                    182:         command.append("?)");
                    183:         PreparedStatement pstm = beanDest.getConnection().prepareStatement(command.toString());
                    184:         System.out.println(command);
                    185:         while ((row = bean.getNextRow()) != null)
                    186:         {
                    187:           //print rows
                    188:           Object obj = null;
                    189:           for (int k = 0; k < row.size(); ++k)
                    190:           {
                    191:             obj = row.get(k);
                    192:             if (obj instanceof ArrayList)
                    193:               obj = ((List) obj).get(0);
                    194:             String str = (obj == null) ? "NULL" : obj.toString();
                    195:             if (!str.equals("NULL"))
                    196:               pstm.setString(k + 1, str);
                    197:             else
                    198:               pstm.setNull(k + 1, Types.NULL);
                    199:           }
                    200:           pstm.execute();
                    201: 
                    202:         } // to for loop    
                    203: 
                    204:       }
                    205:     } catch (Exception e)
                    206:     {
                    207:       System.out.println("Error while connecting to database " + e);
                    208:       //dialog.setVisible(false);
                    209:       //dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                    210:       //FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                    211:       java.io.ByteArrayOutputStream b = new java.io.ByteArrayOutputStream();
                    212:       java.io.PrintStream stream = new java.io.PrintStream(b);
                    213:       stream.print(command + "\n\n");
                    214:       e.printStackTrace(stream);
                    215:       System.err.println(b);
                    216:       //FM2SQL.showErrorDialog(b.toString(), "Error occured !");
                    217: 
1.1       rogo      218:     }
1.34      rogo      219:     //  dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                    220:     //FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
1.1       rogo      221: 
1.34      rogo      222:     //  dialog.setVisible(false); 
1.1       rogo      223:   }
                    224: 
1.34      rogo      225:   public static void update(String source, String destination, Vector names, Vector layouts, Vector selects, Vector creates, Vector ids, int mode) throws Exception
1.12      rogo      226:   {
                    227:     FM2SQL.ProgressDialog dialog = null;
                    228:     if (FM2SQL.fmInstance != null)
                    229:     {
                    230:       dialog = new FM2SQL.ProgressDialog(FM2SQL.fmInstance);
                    231:       dialog.setTitle("Conversion running ...");
                    232:       dialog.title.setText("Getting table data ...");
                    233:       dialog.setLocation(FM2SQL.fmInstance.getLocationOnScreen().x + (FM2SQL.fmInstance.getWidth() - 400) / 2, FM2SQL.fmInstance.getLocationOnScreen().y + (FM2SQL.fmInstance.getHeight() - 250) / 2);
                    234:       dialog.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                    235:       FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                    236:       dialog.thread = Thread.currentThread();
                    237:     }
                    238:     // setting user and passwd 
                    239:     bean.setUserAndPasswd(user, passwd);
                    240:     // setting user and passwd 
                    241:     beanDest.setUserAndPasswd(userDest, passwdDest);
                    242:     if (dialog != null)
                    243:       dialog.setSize(400, 250);
                    244:     StringBuffer command = null;
                    245:     String query = null;
                    246:     try
                    247:     {
                    248:       //bean.setConnection("jdbc:fmpro:http://141.14.237.74:8050");    
                    249:       //bean.setConnection("jdbc:postgresql://erebos/test","postgres","rogo");
                    250:       bean.setConnection(source);
                    251:       if (names == null)
                    252:         names = bean.getTableNames();
                    253:       // Collections.sort(names);
                    254:       int tbIndex = 1;
                    255: 
                    256:       // System.out.println("Start at "+names.indexOf("archimedes_facsimiles"));
                    257:       for (tbIndex = 0; tbIndex < names.size(); ++tbIndex)
                    258:       {
                    259:         Vector[] result = null;
                    260:         try
                    261:         {
                    262:           query = "select * from " + bean.getQC() + names.get(tbIndex).toString() + bean.getQC();
                    263:           String layout = (layouts.isEmpty()) ? "" : layouts.get(tbIndex).toString();
                    264:           query = (selects != null) ? selects.get(tbIndex).toString() : query;
                    265:           //if  vectors[1].get(i) != null)
                    266:           if (layout != "")
                    267:           {
                    268:             layout = " layout " + bean.getQC() + layout + bean.getQC();
                    269:             String name = names.get(tbIndex).toString();
                    270:             StringBuffer queryLayout = new StringBuffer(query);
                    271:             queryLayout.insert(queryLayout.indexOf(name) + name.length() + 1, " " + layout);
                    272:             query = queryLayout.toString();
                    273:             System.out.println("added layout  " + query);
                    274: 
                    275:           }
                    276:           dialog.title.setText("Getting table data ...");
                    277:           dialog.table.setText(names.get(tbIndex).toString());
                    278:           dialog.status.setText("Table " + (tbIndex + 1) + " of " + names.size());
                    279:           dialog.show();
                    280:           bean.getConnection();
                    281:           bean.makeQuery(query, 0);
                    282:         } catch (Exception e)
                    283:         {
                    284:           continue;
                    285:         }
                    286:         //beanDest.setConnection("jdbc:postgresql://erebos/test3");
                    287:         beanDest.setConnection(destination);
                    288: 
                    289:         Statement stm = beanDest.getConnection().createStatement();
                    290: 
                    291:         Vector tables = beanDest.getTableNames();
                    292:         // Collections.sort(tables);
                    293:         System.out.println(names.get(tbIndex) + " " + tables.indexOf(convertText((String) names.get(tbIndex)))); // "//beanDest.getTypeNames()); 
                    294:         tables = beanDest.getTableNames();
                    295:         // System.out.println(beanDest.getTableNames(beanDest.getCatalogs().get(2).toString()));
                    296:         stm = beanDest.getConnection().createStatement();
                    297:         // System.exit(0);
1.34      rogo      298: 
1.12      rogo      299:         if (dialog != null)
1.36    ! rogo      300:           dialog.title.setText("Updating table data ...");
1.12      rogo      301: 
                    302:         int j = -1;
1.34      rogo      303: 
1.12      rogo      304:         Vector row = null;
                    305:         command = new StringBuffer();
                    306: 
                    307:         command.append("UPDATE ");
                    308:         command.append(beanDest.getQC());
                    309:         command.append(convertText((String) names.get(tbIndex)));
                    310:         command.append(beanDest.getQC());
                    311:         command.append(" SET  ");
                    312: 
                    313:         int size = bean.getColumnNames().size();
                    314:         for (int i = 0; i < size - 1; ++i)
1.34      rogo      315:           command.append(beanDest.getQC() + convertText((String) bean.getColumnNames().get(i)) + beanDest.getQC() + " = ? ,");
                    316:         command.append(convertText((String) bean.getColumnNames().get(size - 1)) + " = ? ");
                    317:         command.append("WHERE " + convertText(ids.get(tbIndex).toString()) + " =  ?");
1.12      rogo      318:         PreparedStatement pstm = beanDest.getConnection().prepareStatement(command.toString());
1.34      rogo      319:         System.out.println(command + " " + tbIndex);
                    320:         int rowCount = bean.getRowCount(query);
                    321:         int idIndex = bean.getColumnNames().indexOf(ids.get(tbIndex));
1.12      rogo      322:         while ((row = bean.getNextRow()) != null)
                    323:         {
                    324:           j++;
                    325:           //print rows
                    326:           Object obj = null;
                    327:           /* for(int k=0;k<row.size()-1;++k)
                    328:            {
                    329:                obj = row.get(k);
                    330:                //System.out.println("row "+obj+" "+k);
                    331:             if(obj!=null&&!(obj instanceof ArrayList))
                    332:             command.append("'"+convertUml(obj.toString())+"',"); 
                    333:             else if(obj!=null&&   obj instanceof ArrayList)
                    334:             command.append("'"+convertUml(((ArrayList)obj).get(0).toString())+"',"); 
                    335:             else command.append("NULL,");
                    336:            }
                    337:            obj = row.get(row.size() - 1);
                    338:            if (obj != null && !(obj instanceof ArrayList))
                    339:            command.append("'"+convertUml(obj.toString())+"')"); 
                    340:            else
                    341:            if(obj!=null&&   obj instanceof ArrayList)
                    342:             command.append("'"+convertUml(((ArrayList)obj).get(0).toString())+"')");         //command.append(obj.toString()+")");
                    343:             else command.append("NULL)");
                    344:            */
                    345:           //command.append("'"+row.get(row.size()-1)+"')"); 
                    346:           //command.append(" )");
                    347:           //  for(int k=0;k<row.size();++k)
                    348: 
                    349:           // System.out.println();
                    350:           //   System.out.println(command+" "+j+" "+row.size()+" "+  ((Vector)result2[0].get(j)).size());
                    351:           // System.out.println(command);
                    352:           for (int k = 0; k < row.size(); ++k)
                    353:           {
                    354:             obj = row.get(k);
                    355:             if (obj instanceof ArrayList)
                    356:               obj = ((List) obj).get(0);
                    357:             String str = (obj == null) ? "NULL" : obj.toString();
                    358:             if (!str.equals("NULL"))
                    359:               pstm.setString(k + 1, str);
                    360:             else
                    361:               pstm.setNull(k + 1, Types.NULL);
                    362:           }
1.34      rogo      363:           pstm.setString(row.size() + 1, row.get(idIndex).toString());
                    364:           //System.out.println(pstm.toString());
                    365:           // System.exit(0);
1.12      rogo      366:           pstm.execute();
                    367:           //stm.executeUpdate(command.toString());
1.34      rogo      368:           if (dialog != null)
                    369:             dialog.progress.setValue((int) (((double) (j + 1) / (double) rowCount) * 100.0));
1.12      rogo      370:           // System.out.println( (int)(((double)(j+1)/(double)result[0].size())*100.0)+" "+result[0].size()+" "+j);
                    371:           command = null;
                    372:         } // to for loop    
                    373: 
                    374:       }
                    375:     } catch (Exception e)
                    376:     {
                    377:       System.out.println("Error while connecting to database " + e);
1.18      rogo      378:       if (dialog != null)
                    379:       {
                    380:         dialog.setVisible(false);
                    381:         dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                    382:         FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                    383:       }
1.12      rogo      384:       java.io.ByteArrayOutputStream b = new java.io.ByteArrayOutputStream();
                    385:       java.io.PrintStream stream = new java.io.PrintStream(b);
                    386:       stream.print(command + "\n\n");
                    387:       e.printStackTrace(stream);
                    388:       FM2SQL.showErrorDialog(b.toString(), "Error occured !");
                    389: 
                    390:     }
1.18      rogo      391:     if (dialog != null)
                    392:     {
                    393:       dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                    394:       FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                    395: 
                    396:       dialog.setVisible(false);
                    397:     }
1.12      rogo      398: 
                    399:   }
1.34      rogo      400:   /**
                    401:    transfers the specified array of tables  to the destination database
                    402:     and creates the table if it does not exist if it exists and mode is not append the table is dropped
                    403:    
                    404:    **/
                    405:   public static void convert(String source, String destination, Vector names, Vector layouts, Vector selects, Vector creates, Vector ids, int mode) throws Exception
1.1       rogo      406:   {
1.34      rogo      407: 
                    408:     FM2SQL.ProgressDialog dialog = null;
                    409: 
                    410:     if (FM2SQL.fmInstance != null)
                    411:     {
                    412:       dialog = new FM2SQL.ProgressDialog(FM2SQL.fmInstance);
                    413:       dialog.setTitle("Conversion running ...");
                    414:       dialog.title.setText("Getting table data ...");
                    415:       dialog.setLocation(FM2SQL.fmInstance.getLocationOnScreen().x + (FM2SQL.fmInstance.getWidth() - 400) / 2, FM2SQL.fmInstance.getLocationOnScreen().y + (FM2SQL.fmInstance.getHeight() - 250) / 2);
                    416:       dialog.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                    417:       FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                    418:       dialog.thread = Thread.currentThread();
                    419:       dialog.setSize(400, 250);
                    420:     }
                    421:     java.util.TreeSet myIds = new TreeSet();
                    422:     int deltaID = 1;
                    423:     String idField = "";
                    424:     if (source != null && destination != null)
                    425:     {
                    426:       // setting user and passwd 
                    427:       bean.setUserAndPasswd(user, passwd);
                    428:       // setting user and passwd 
                    429:       beanDest.setUserAndPasswd(userDest, passwdDest);
                    430:     }
                    431:     StringBuffer command = null;
                    432:     String query = null;
1.1       rogo      433:     try
                    434:     {
1.34      rogo      435:       if(source!=null)
1.1       rogo      436:       bean.setConnection(source);
1.34      rogo      437:       else
                    438:       bean.setConnection(bean.url);
                    439:       
                    440:       if (names == null)
                    441:         names = bean.getTableNames();
                    442:       // Collections.sort(names);
                    443:       int tbIndex = 1;
                    444: 
                    445:       // System.out.println("Start at "+names.indexOf("archimedes_facsimiles"));
                    446:       for (tbIndex = 0; tbIndex < names.size(); ++tbIndex)
1.1       rogo      447:       {
                    448:         Vector[] result = null;
1.34      rogo      449:         try
1.1       rogo      450:         {
1.34      rogo      451:           query = "select * from " + bean.getQC() + names.get(tbIndex).toString() + bean.getQC();
                    452:           String layout = (layouts.isEmpty()) ? "" : layouts.get(tbIndex).toString();
                    453:           query = (selects != null) ? selects.get(tbIndex).toString() : query;
                    454:           //if  vectors[1].get(i) != null)
                    455:           if (layout != "")
                    456:           {
                    457:             layout = " layout " + bean.getQC() + layout + bean.getQC();
                    458:             String name = names.get(tbIndex).toString();
                    459:             StringBuffer queryLayout = new StringBuffer(query);
                    460:             queryLayout.insert(queryLayout.indexOf(name) + name.length() + 1, " " + layout);
                    461:             query = queryLayout.toString();
                    462:             System.out.println("added layout  " + query);
1.21      rogo      463: 
1.34      rogo      464:           }
                    465:           //  if ( layout!= "")
                    466:           //    query += " layout " + bean.getQC() + layout + bean.getQC();
                    467:           if (dialog != null)
                    468:           {
                    469:             dialog.title.setText("Reading table data ...");
                    470:             dialog.table.setText(names.get(tbIndex).toString());
                    471:             dialog.status.setText("Table " + (tbIndex + 1) + " of " + names.size());
                    472:             dialog.show();
                    473:           }
                    474:           //result = bean.getQueryData(query, dialog, 0);
                    475:           bean.getConnection();
                    476:           bean.makeQuery(query, 50);
                    477:           idField = ids.get(tbIndex).toString();
                    478: 
                    479:         } catch (Exception e)
                    480:         {
                    481:           System.out.println(e);
                    482:           continue;
1.1       rogo      483:         }
1.34      rogo      484:         if(destination!=null)
1.1       rogo      485:         beanDest.setConnection(destination);
1.34      rogo      486:         else
                    487:         beanDest.setConnection(beanDest.url);
1.1       rogo      488:         Statement stm = beanDest.getConnection().createStatement();
                    489: 
                    490:         Vector tables = beanDest.getTableNames();
1.34      rogo      491:         // Collections.sort(tables);
1.1       rogo      492:         System.out.println(names.get(tbIndex) + " " + tables.indexOf(convertText((String) names.get(tbIndex)))); // "//beanDest.getTypeNames()); 
                    493:         tables = beanDest.getTableNames();
                    494:         // System.out.println(beanDest.getTableNames(beanDest.getCatalogs().get(2).toString()));
                    495:         stm = beanDest.getConnection().createStatement();
                    496:         // System.exit(0);
1.34      rogo      497:         if (mode == Convert.DataBase.CONVERT_MODE)
1.1       rogo      498:         {
1.34      rogo      499:           if (tables.indexOf(names.get(tbIndex)) >= 0)
                    500:           {
                    501:             stm.executeUpdate("drop table " + beanDest.getQC() + names.get(tbIndex) + beanDest.getQC());
                    502:             tables.remove((String) names.get(tbIndex));
                    503:             System.out.println("dropped table" + names.get(tbIndex));
                    504:           } else if (tables.indexOf(convertText(names.get(tbIndex).toString())) >= 0)
1.1       rogo      505:           {
1.34      rogo      506:             stm.executeUpdate("drop table " + beanDest.getQC() + convertText((String) names.get(tbIndex)) + beanDest.getQC());
                    507:             tables.remove(convertText((String) names.get(tbIndex)));
                    508:             System.out.println("dropped table" + names.get(tbIndex));
                    509:           }
                    510: 
                    511:           if (tables.indexOf(names.get(tbIndex)) < 0 && tables.indexOf(convertText(names.get(tbIndex).toString())) < 0)
1.1       rogo      512:           {
1.34      rogo      513:             if (creates.get(tbIndex).equals("") || creates.get(tbIndex).toString().toLowerCase().indexOf("create") < 0)
                    514:             {
                    515:               System.out.println("Warning empty or invalid create statement - creating one for you\n");
                    516: 
                    517:               command = new StringBuffer(50);
                    518:               command.append("CREATE TABLE ");
                    519:               command.append(beanDest.getQC());
                    520:               command.append(convertText((String) names.get(tbIndex)));
                    521:               command.append(beanDest.getQC());
                    522:               command.append("(");
                    523:               String type = null;
                    524:               Vector columnNames = bean.getColumnNames();
                    525:               for (int i = 0; i < columnNames.size() - 1; ++i)
                    526:               {
                    527:                 type = bean.metaData.getColumnTypeName(i + 1);
                    528:                 //   System.out.println(i+" "+result[1].get(i)+" "+type);
                    529:                 type = (type.equals("NUMBER")) ? "INT4" : type;
                    530:                 type = (type.equals("CONTAINER")) ? "TEXT" : type;
                    531: 
                    532:                 command.append(beanDest.getQC() + convertText((String) columnNames.get(i)) + beanDest.getQC() + " " + type + ", ");
                    533:               }
                    534:               type = bean.metaData.getColumnTypeName(columnNames.size());
                    535:               type = (type.equals("NUMBER")) ? "INT4" : type;
                    536:               type = (type.equals("CONTAINER")) ? "TEXT" : type;
                    537:               command.append(beanDest.getQC() + convertText((String) columnNames.get(columnNames.size() - 1)) + beanDest.getQC() + " " + type);
                    538:               command.append(" )");
                    539: 
                    540:               // System.out.println(command);
                    541:               //  System.exit(0);
                    542:               //command.append(DBBean.getQC());   
                    543:             } else
                    544:               command = new StringBuffer().append(creates.get(tbIndex).toString());
                    545:             stm.executeUpdate(command.toString());
1.1       rogo      546: 
1.34      rogo      547:           }
1.1       rogo      548:         }
1.34      rogo      549:         if(dialog!=null)
                    550:         dialog.title.setText("Writing table data ...");
                    551: 
                    552:         // prepare the insert statement
                    553:         int j = -1;
                    554:         Vector row = null;
                    555:         command = new StringBuffer();
                    556: 
                    557:         command.append("INSERT  INTO ");
                    558:         command.append(beanDest.getQC());
                    559:         command.append(convertText((String) names.get(tbIndex)));
                    560:         command.append(beanDest.getQC());
                    561:         command.append(" values ( ");
                    562: 
                    563:         for (int i = 0; i < bean.getColumnNames().size() - 1; ++i)
                    564:           command.append("?,");
                    565:         command.append("?)");
                    566:         PreparedStatement pstm = beanDest.getConnection().prepareStatement(command.toString());
                    567:         System.out.println(command);
                    568:         int rowCount = (idField != "") ? myIds.size() : bean.getRowCount(query);
                    569:         Vector vec = new Vector(myIds);
                    570:         int endIndex = -1;
                    571:         String tempQuery = query;
                    572:         String tempID = bean.getQC() + idField + bean.getQC();
                    573:         if (!idField.equals(""))
                    574:         {
                    575:           long startTime = System.currentTimeMillis();
                    576:           int counter = -1;
                    577:           while (true)
                    578:           {
                    579:             ++counter;
                    580:             if (counter == 0&&dialog!=null)
                    581:               dialog.title.setText("Check if data  is available");
                    582:             else
                    583:               if(dialog!=null)
                    584:               dialog.title.setText("Check if more  data  is available");
                    585:             myIds = bean.getIDVector(ids.get(tbIndex).toString(), (String) names.get(tbIndex), tempQuery, numHits);
                    586:             if (myIds.isEmpty())
                    587:               break;
                    588:             vec = new Vector(myIds);
                    589:             rowCount = vec.size();
                    590:             System.out.println("ID LIST SIZE " + Math.round((double) myIds.size() / (double) numIntervalls) + " " + myIds.size());
                    591:             deltaID = (int) Math.round((double) myIds.size() / (double) numIntervalls);
                    592:             if (vec.size() <= numIntervalls)
                    593:             {
                    594:               endIndex = 0;
                    595:               deltaID = vec.size();
                    596:             }
                    597:             for (int k = 0; k < vec.size() - deltaID; k = k + deltaID)
                    598:             {
                    599:               System.out.println(vec.get(k) + " " + vec.get(k + deltaID) + " " + vec.lastElement());
                    600:               if (query.indexOf("where") > 0)
                    601:                 tempQuery = query + " and " + tempID + ">=" + vec.get(k) + " and " + tempID + "<=" + vec.get(k + deltaID);
                    602:               else
                    603:                 tempQuery = query + "where " + tempID + ">=" + vec.get(k) + " and " + tempID + "<=" + vec.get(k + deltaID);
                    604:               System.out.println(tempQuery);
                    605:               if(dialog!=null)
                    606:               dialog.title.setText("Reading table data ...");
                    607: 
                    608:               bean.makeQuery(tempQuery, deltaID);
                    609:               if(dialog!=null)
                    610:               dialog.title.setText("Writing table data ...");
                    611: 
                    612:               command = writeDatainDestTable(dialog, command, k, pstm, rowCount);
                    613:               endIndex = k + deltaID;
                    614:             }
                    615:             System.out.println(endIndex);
                    616:             if (endIndex == vec.size() - 1)
                    617:               System.out.println("fits");
                    618:             else
                    619:             {
                    620:               System.out.println(" last intervall from " + vec.get(endIndex) + " " + vec.lastElement());
1.1       rogo      621: 
1.34      rogo      622:               if (query.indexOf("where") > 0)
                    623:                 tempQuery = query + " and " + tempID + ">=" + vec.get(endIndex) + " and " + tempID + "<=" + vec.lastElement();
                    624:               else
                    625:                 tempQuery = query + "where " + tempID + ">=" + vec.get(endIndex) + " and " + tempID + "<=" + vec.lastElement();
                    626:               System.out.println(tempQuery);
                    627:               if(dialog!=null)
                    628:               dialog.title.setText("Reading table data ...");
                    629:               bean.makeQuery(tempQuery, 0);
                    630:               if(dialog!=null)
                    631:               dialog.title.setText("Writing table data ...");
                    632:               command = writeDatainDestTable(dialog, command, endIndex, pstm, rowCount);
                    633:             }
                    634:             if (query.indexOf("where") > 0)
                    635:               tempQuery = query + " and " + tempID + ">" + vec.lastElement();
                    636:             else
                    637:               tempQuery = query + " where " + tempID + ">" + vec.lastElement();
                    638: 
                    639:           }
                    640:           long endTime = System.currentTimeMillis();
                    641:           System.out.println("Time for incremental convert elapsed " + (endTime - startTime));
                    642:         } else
1.29      rogo      643:         {
1.34      rogo      644:           long startTime = System.currentTimeMillis();
1.29      rogo      645: 
1.34      rogo      646:           bean.makeQuery(query, 0);
                    647:           command = writeDatainDestTable(dialog, command, j, pstm, rowCount);
                    648:           long endTime = System.currentTimeMillis();
                    649:           System.out.println("Time for old convert elapsed " + (endTime - startTime));
1.29      rogo      650: 
                    651:         }
1.34      rogo      652:       }
                    653:     } catch (Exception e)
                    654:     {
                    655:       System.out.println("Error while connecting to database " + e);
                    656:       if (dialog != null)
                    657:       {
                    658:         dialog.setVisible(false);
                    659:         dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                    660:         FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                    661:         java.io.ByteArrayOutputStream b = new java.io.ByteArrayOutputStream();
                    662:         java.io.PrintStream stream = new java.io.PrintStream(b);
                    663:         stream.print(command + "\n\n");
                    664:         e.printStackTrace(stream);
                    665:         FM2SQL.showErrorDialog(b.toString(), "Error occured !");
                    666:       } else
                    667:       {
                    668:         e.printStackTrace();
1.33      rogo      669: 
1.3       rogo      670:       }
1.1       rogo      671:     }
1.34      rogo      672:     if (dialog != null)
                    673:     {
                    674:       dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                    675:       FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                    676:       dialog.setVisible(false);
                    677:     }
1.27      rogo      678:   }
                    679:   private static StringBuffer writeDatainDestTable(FM2SQL.ProgressDialog dialog, StringBuffer command, int j, PreparedStatement pstm, int rowCount) throws Exception, SQLException
                    680:   {
                    681:     Vector row;
1.34      rogo      682:     while ((row = bean.getNextRow()) != null)
                    683:     {
                    684:       j++;
                    685:       // row = (Vector) result[0].get(j);
                    686:       /*   command = new StringBuffer();
                    687:       
                    688:            command.append("INSERT  INTO ");
                    689:            command.append(beanDest.getQC());
                    690:            command.append(convertText((String) names.get(tbIndex)));
                    691:            command.append(beanDest.getQC());
                    692:            command.append(" values ( ");
                    693:         */
                    694:       //print rows
                    695:       Object obj = null;
                    696:       /* for(int k=0;k<row.size()-1;++k)
                    697:        {
                    698:           obj = row.get(k);
                    699:           //System.out.println("row "+obj+" "+k);
                    700:          if(obj!=null&&!(obj instanceof ArrayList))
                    701:          command.append("'"+convertUml(obj.toString())+"',"); 
                    702:          else if(obj!=null&&   obj instanceof ArrayList)
                    703:          command.append("'"+convertUml(((ArrayList)obj).get(0).toString())+"',"); 
                    704:          else command.append("NULL,");
                    705:        }
                    706:        obj = row.get(row.size() - 1);
                    707:        if (obj != null && !(obj instanceof ArrayList))
                    708:        command.append("'"+convertUml(obj.toString())+"')"); 
                    709:         else
                    710:         if(obj!=null&&   obj instanceof ArrayList)
                    711:          command.append("'"+convertUml(((ArrayList)obj).get(0).toString())+"')");         //command.append(obj.toString()+")");
                    712:          else command.append("NULL)");
                    713:         */
                    714:       //command.append("'"+row.get(row.size()-1)+"')"); 
                    715:       //command.append(" )");
                    716:       //  for(int k=0;k<row.size();++k)
                    717: 
                    718:       // System.out.println();
                    719:       //   System.out.println(command+" "+j+" "+row.size()+" "+  ((Vector)result2[0].get(j)).size());
                    720:       // System.out.println(command);
                    721:       for (int k = 0; k < row.size(); ++k)
                    722:       {
                    723:         obj = row.get(k);
                    724:         if (obj instanceof ArrayList)
                    725:           obj = ((List) obj).get(0);
                    726:         String str = (obj == null) ? "NULL" : obj.toString();
                    727:         if (!str.equals("NULL"))
                    728:           pstm.setString(k + 1, str);
                    729:         else
                    730:           pstm.setNull(k + 1, Types.NULL);
                    731:       }
                    732:       pstm.execute();
                    733:       //stm.executeUpdate(command.toString());
                    734:       if (dialog != null)
                    735:         dialog.progress.setValue((int) (((double) (j + 1) / (double) rowCount) * 100.0));
                    736:       // System.out.println( (int)(((double)(j+1)/(double)result[0].size())*100.0)+" "+result[0].size()+" "+j);
                    737:       command = null;
                    738:     } // to while loop    
1.27      rogo      739:     return command;
1.1       rogo      740:   }
                    741: 
1.34      rogo      742:   public static String convertText(String newName)
1.1       rogo      743:   {
                    744:     StringBuffer alterMe = new StringBuffer(newName.trim().toLowerCase());
                    745:     int length = alterMe.length();
                    746:     int j = 0;
1.34      rogo      747:     int index = alterMe.indexOf(".fp5");
                    748:     if (index >= 0)
                    749:     {
                    750:       alterMe.delete(index, index + 4);
                    751:       length = length - 4;
                    752:     }
                    753: 
1.1       rogo      754:     while (j < length)
1.34      rogo      755:     {
1.1       rogo      756:       if (alterMe.charAt(j) == ' ')
                    757:       {
                    758:         alterMe.setCharAt(j, '_');
1.34      rogo      759:         //    if(j<length-1) j=j+1;
                    760:       } else if (alterMe.charAt(j) == '_')
                    761:       {
                    762: 
                    763:         if (alterMe.charAt(j + 1) == '_')
                    764:           alterMe.deleteCharAt(j);
                    765:         length = length - 1;
                    766:         //  if(j<length-1) j=j+1;
                    767:       } else if (alterMe.charAt(j) == 'ä')
                    768:       {
                    769:         alterMe.setCharAt(j, 'a');
                    770:         alterMe.insert(j + 1, "e");
                    771:         length = length + 1;
                    772:         if (j < length - 1)
                    773:           j = j + 1;
                    774:       } else if (alterMe.charAt(j) == 'ö')
                    775:       {
                    776:         alterMe.setCharAt(j, 'o');
                    777:         alterMe.insert(j + 1, "e");
                    778:         length = length + 1;
                    779:         if (j < length - 1)
                    780:           j = j + 1;
                    781:       } else if (alterMe.charAt(j) == 'ü')
                    782:       {
                    783:         alterMe.setCharAt(j, 'u');
                    784:         alterMe.insert(j + 1, "e");
                    785:         length = length + 1;
                    786:         if (j < length - 1)
                    787:           j = j + 1;
                    788:       } else if (alterMe.charAt(j) == 'ß')
                    789:       {
                    790:         alterMe.setCharAt(j, 's');
                    791:         alterMe.insert(j + 1, "s");
                    792:         length = length + 1;
                    793:         if (j < length - 1)
                    794:           j = j + 1;
                    795:       } else if (alterMe.charAt(j) == ':')
1.1       rogo      796:       {
1.34      rogo      797:         if (j < length - 1)
                    798:         {
                    799:           if (alterMe.charAt(j + 1) == ':')
                    800:           {
                    801:             alterMe.setCharAt(j, '_');
                    802:             alterMe.delete(j + 1, j + 2);
                    803:             length = length - 1;
                    804: 
                    805:           }
1.1       rogo      806: 
1.34      rogo      807:           if (j < length - 1)
                    808:             j = j + 1;
                    809:         }
                    810:       } else if (alterMe.charAt(j) == '-')
                    811:       {
                    812:         alterMe.setCharAt(j, '_');
1.16      rogo      813: 
1.34      rogo      814:       } else if (alterMe.charAt(j) == '?')
1.3       rogo      815:       {
1.34      rogo      816:         // changed ? to _ because of update statement
                    817:         alterMe.setCharAt(j, '_');
                    818:         // length = length + 1;
                    819:         // j=j+1;
                    820:         System.out.println(alterMe);
                    821:       } else if (alterMe.charAt(j) == '.')
                    822:       {
                    823:         if (j == length - 1)
                    824:         {
                    825:           alterMe.delete(j, j);
                    826:           length--;
                    827:         } else
                    828:           alterMe.setCharAt(j, '_');
1.3       rogo      829:       }
1.34      rogo      830: 
                    831:       ++j;
1.1       rogo      832:     }
                    833:     return alterMe.toString();
                    834:   }
1.4       rogo      835:   public static String convertToEntities(String newName)
                    836:   {
                    837:     StringBuffer alterMe = new StringBuffer(newName.trim());
                    838:     int length = alterMe.length();
                    839:     int j = 0;
                    840: 
                    841:     while (j < length)
                    842:     {
                    843: 
                    844:       if (alterMe.charAt(j) == '>')
                    845:       {
                    846:         alterMe.setCharAt(j, '&');
                    847:         alterMe.insert(j + 1, "gt;");
                    848:         length = length + 2;
                    849:         if (j < length - 1)
                    850:           j = j + 1;
                    851: 
                    852:       } else if (alterMe.charAt(j) == '<')
                    853:       {
                    854:         alterMe.setCharAt(j, '&');
                    855:         alterMe.insert(j + 1, "lt;");
                    856:         length = length + 2;
                    857:         if (j < length - 1)
                    858:           j = j + 1;
                    859: 
                    860:       }
                    861:       ++j;
                    862:     }
                    863:     return alterMe.toString();
                    864:   }
1.1       rogo      865:   public static String convertUml(String newName)
1.34      rogo      866:   {
                    867:     StringBuffer alterMe = new StringBuffer(newName.trim());
                    868:     int length = alterMe.length();
                    869:     int j = 0;
                    870: 
                    871:     while (j < length)
                    872:     {
                    873: 
                    874:       if (alterMe.charAt(j) == '\'')
                    875:       {
                    876:         alterMe.setCharAt(j, '\\');
                    877:         alterMe.insert(j + 1, "'");
                    878:         length = length + 1;
                    879:         if (j < length - 1)
                    880:           j = j + 1;
                    881:       }
                    882:       /*   else
                    883:          if (alterMe.charAt(j) == '"')
                    884:          {
                    885:         alterMe.setCharAt(j, '\\');
                    886:         alterMe.insert(j + 1, "\"");
                    887:         length = length + 1;
                    888:         if(j<length-1) j=j+1;
                    889:          }
                    890:         else
                    891:         if (alterMe.charAt(j) == '>')
                    892:          {
                    893:         alterMe.setCharAt(j, '\\');
                    894:         alterMe.insert(j + 1, ">");
                    895:         length = length + 1;
                    896:         if(j<length-1) j=j+1;
                    897:          }
                    898:         else
                    899:         if (alterMe.charAt(j) == '<')
                    900:          {
                    901:         alterMe.setCharAt(j, '\\');
                    902:         alterMe.insert(j + 1, "<");
                    903:         length = length + 1;
                    904:         if(j<length-1) j=j+1;
                    905:          }
                    906:        else
                    907:        if (alterMe.charAt(j) == '?')
                    908:          {
                    909:         alterMe.setCharAt(j, '\\');
                    910:         alterMe.insert(j + 1, "?");
                    911:         length = length + 1;
                    912:         if(j<length-1) j=j+1;
                    913:          }
                    914:        else
                    915:        if (alterMe.charAt(j) == '&')
                    916:          {
                    917:         alterMe.setCharAt(j, '\\');
                    918:         alterMe.insert(j + 1, "&");
                    919:         length = length + 1;
                    920:         if(j<length-1) j=j+1;
                    921:          }
                    922:        else
                    923:        if (alterMe.charAt(j) == '=')
                    924:          {
                    925:         alterMe.setCharAt(j, '\\');
                    926:         alterMe.insert(j + 1, "=");
                    927:         length = length + 1;
                    928:         if(j<length-1) j=j+1;
                    929:          }
                    930:        else
                    931:        if (alterMe.charAt(j) == ',')
                    932:          {
                    933:         alterMe.setCharAt(j, '\\');
                    934:         alterMe.insert(j + 1, ",");
                    935:         length = length + 1;
                    936:         if(j<length-1) j=j+1;
                    937:          }
                    938:        else
                    939:        if (alterMe.charAt(j) == '.')
                    940:          {
                    941:         alterMe.setCharAt(j, '\\');
                    942:         alterMe.insert(j + 1, ".");
                    943:         length = length + 1;
                    944:         if(j<length-1) j=j+1;
                    945:          }
                    946:        else
                    947:        if (alterMe.charAt(j) == '[')
                    948:          {
                    949:         alterMe.setCharAt(j, '\\');
                    950:         alterMe.insert(j + 1, ".");
                    951:         length = length + 1;
                    952:         if(j<length-1) j=j+1;
                    953:          }
                    954:       else
                    955:        if (alterMe.charAt(j) == ']')
                    956:          {
                    957:         alterMe.setCharAt(j, '\\');
                    958:         alterMe.insert(j + 1, ".");
                    959:         length = length + 1;
                    960:         if(j<length-1) j=j+1;
                    961:          }
                    962:       else
                    963:        if (alterMe.charAt(j) == '%')
                    964:          {
                    965:         alterMe.setCharAt(j, '\\');
                    966:         alterMe.insert(j + 1, "%");
                    967:         length = length + 1;
                    968:         if(j<length-1) j=j+1;
                    969:          }*/
                    970:       ++j;
                    971:     }
                    972:     return alterMe.toString();
                    973:   }
                    974: 
                    975:   public static void parseXMLConfig(StringBuffer sb)
                    976:   {
                    977:     boolean finished = false;
                    978:     // parse string and build document tree
                    979:     Xparse parser = new Xparse();
                    980:     parser.changeEntities = true;
                    981:     Node root = parser.parse(sb.toString());
                    982:     // printContents(root);
                    983:     Vector databases = new Vector();
                    984:     Vector tables = new Vector();
                    985:     Vector layouts = new Vector();
                    986:     Vector selects = new Vector();
                    987:     Vector creates = new Vector();
                    988:     Vector ids = new Vector();
                    989:     int mode = -1;
                    990: 
                    991:     try
1.1       rogo      992:     {
1.34      rogo      993:       Node tempNode = root.find("convert/source", new int[] { 1, 1 });
                    994:       if (tempNode == null)
                    995:         throw new Error("parse error source tag missing");
                    996:       System.out.println(tempNode.name);
                    997:       int length = countNodes(tempNode);
                    998:       for (int i = 1; i <= length; i++)
1.1       rogo      999:       {
1.34      rogo     1000: 
1.1       rogo     1001:         DBBean database = new DBBean();
1.34      rogo     1002:         tables = new Vector();
                   1003:         layouts = new Vector();
                   1004:         selects = new Vector();
                   1005:         creates = new Vector();
                   1006:         ids = new Vector();
1.1       rogo     1007:         // parse dataBase
1.34      rogo     1008:         Node node = root.find("convert/source/database/url", new int[] { 1, 1, i, 1 });
                   1009:         Node node1 = root.find("convert/source/database/user", new int[] { 1, 1, i, 1, 1 });
                   1010:         Node node2 = root.find("convert/source/database/password", new int[] { 1, 1, i, 1, 1 });
                   1011:         Node node3 = root.find("convert/source/database", new int[] { 1, 1, i });
                   1012:         Node nodeMode = root.find("convert/source/database/mode", new int[] { 1, 1, i, 1, 1 });
                   1013: 
                   1014:         if (node3 == null)
                   1015:           throw new Error("parse error database tag missing");
                   1016:         if (node == null)
                   1017:           throw new Error("parse error url tag missing");
                   1018:         if (node1 == null)
                   1019:           throw new Error("parse error user tag missing");
                   1020:         if (node2 == null)
                   1021:           throw new Error("parse error password tag missing");
1.1       rogo     1022:         String url = node.getCharacters();
                   1023:         String user = node1.getCharacters();
                   1024:         String password = node2.getCharacters();
                   1025:         database.setURL(url.trim());
                   1026:         database.setUserAndPasswd(user.trim(), password.trim());
1.34      rogo     1027:         System.out.println(node.name + " " + node.getCharacters());
                   1028:         System.out.println(node1.name + " " + node1.getCharacters());
                   1029:         System.out.println(node2.name + " " + node2.getCharacters());
                   1030:         String modeString = "";
                   1031:         if (nodeMode == null)
                   1032:           modeString = "convert";
                   1033:         else
                   1034:           modeString = nodeMode.getCharacters();
                   1035:         if (modeString.equals("convert"))
                   1036:           mode = DataBase.CONVERT_MODE;
                   1037:         else if (modeString.equals("append"))
                   1038:           mode = DataBase.APPEND_MODE;
                   1039:         else if (modeString.equals("update"))
                   1040:           mode = DataBase.UPDATE_MODE;
                   1041:         //   if(node3!=null)
                   1042:         // System.out.println(node3.name);
                   1043: 
                   1044:         int length2 = countNodes(node3);
                   1045: 
                   1046:         System.out.println("number of tables " + length2);
                   1047: 
                   1048:         for (int j = 1; j <= length2; ++j)
                   1049:         {
                   1050:           Node node4 = root.find("convert/source/database/table", new int[] { 1, 1, i, j });
                   1051:           Node node5 = root.find("convert/source/database/table/select", new int[] { 1, 1, i, j, 1 });
                   1052:           Node node6 = root.find("convert/source/database/table/create", new int[] { 1, 1, i, j, 1 });
                   1053:           if (node4 != null)
                   1054:             System.out.println(node4.name + " " + node4.attributes.get("layout").equals(""));
                   1055:           if (node5 != null)
                   1056:             System.out.println(node5.name + " " + node5.getCharacters());
                   1057:           if (node6 != null)
                   1058:             System.out.println(node6.name + " " + node6.getCharacters());
                   1059:           if (node4 == null)
                   1060:             throw new Error("parse error table tag missing");
                   1061:           // if(node5==null) throw new Error("parse error select tag missing");
                   1062:           // if(node6==null) throw new Error("parse error create tag missing");
                   1063:           String name = (String) node4.attributes.get("name");
                   1064:           String layout = (String) node4.attributes.get("layout");
                   1065:           String id = (String) node4.attributes.get("id");
                   1066:           System.out.println("id was " + id);
                   1067:           if (name == null)
                   1068:             throw new Error("parse error required table tag attribute name missing");
                   1069:           if (layout == null)
                   1070:             layout = "";
                   1071:           if (id == null)
                   1072:             id = "";
                   1073:           if (name.equals(""))
                   1074:             throw new Error("parse error table tag attribute must not be empty");
                   1075:           tables.add(name);
                   1076:           layouts.add(layout);
                   1077:           ids.add(id);
                   1078:           String query = (node5 == null) ? "" : node5.getCharacters();
                   1079:           if (query.equals(""))
                   1080:             System.err.println("Warning empty select tag or  select tag missing !!");
                   1081:           query = (query.equals("")) ? "select * from " + database.getQC() + name + database.getQC() : query;
                   1082:           selects.add(query);
                   1083:           if (node6 != null)
                   1084:             creates.add(node6.getCharacters().trim());
                   1085:           else
                   1086:             creates.add("");
                   1087: 
                   1088:         }
                   1089:         databases.add(new DataBase(database, tables, layouts, selects, creates, ids, mode));
                   1090:       }
                   1091:       DBBean database = new DBBean();
                   1092:       // parse dataBase
                   1093:       Node node = root.find("convert/destination/database/url", new int[] { 1, 1, 1, 1 });
                   1094:       Node node1 = root.find("convert/destination/database/user", new int[] { 1, 1, 1, 1, 1 });
                   1095:       Node node2 = root.find("convert/destination/database/password", new int[] { 1, 1, 1, 1, 1 });
                   1096:       String url = node.getCharacters();
                   1097:       String user = node1.getCharacters();
                   1098:       String password = node2.getCharacters();
                   1099:       System.out.println(url);
                   1100:       database.setURL(url.trim());
                   1101:       database.setUserAndPasswd(user.trim(), password.trim());
                   1102:       //databases.add(database);
                   1103:       for (Iterator iter = databases.iterator(); iter.hasNext();)
1.1       rogo     1104:       {
                   1105:         DataBase db = (DataBase) iter.next();
1.34      rogo     1106:         if (mode != DataBase.UPDATE_MODE)
                   1107:           convertBatch(db.bean, database, db.tables, db.layouts, db.selects, db.creates, db.ids,mode);
1.17      rogo     1108:         else
1.34      rogo     1109:           update(db.bean.url, database.url, db.tables, db.layouts, db.selects, db.creates, db.ids, mode);
1.17      rogo     1110: 
1.1       rogo     1111:       }
1.34      rogo     1112:       // printContents(node3);
                   1113:       //   FM2SQL.fmInstance=new FM2SQL();
                   1114:     } catch (Exception e)
                   1115:     {
1.35      rogo     1116:     
1.34      rogo     1117:       e.printStackTrace();
                   1118:     }
                   1119:   }
                   1120:   public static Vector getXMLConfig(String xmlFile)
                   1121:   {
                   1122:     StringBuffer sb = null;
                   1123:     try
                   1124:     {
                   1125:       // read XML Metadata from a file
                   1126:       FileInputStream fi = new FileInputStream(xmlFile);
                   1127:       InputStreamReader isr = new InputStreamReader(fi, "UTF-8");
                   1128:       BufferedReader buffr = new BufferedReader(isr);
                   1129:       sb = new StringBuffer();
                   1130:       int c = 0;
                   1131:       while ((c = buffr.read()) != -1)
                   1132:       {
                   1133:         char ch = (char) c;
                   1134:         sb.append(ch);
                   1135:         // System.out.print((char)c);
1.1       rogo     1136:       }
                   1137: 
1.34      rogo     1138:     } catch (Exception e)
                   1139:     {
                   1140:       e.printStackTrace();
                   1141:     }
                   1142: 
                   1143:     boolean finished = false;
                   1144:     // parse string and build document tree
                   1145:     Xparse parser = new Xparse();
                   1146:     parser.changeEntities = true;
                   1147:     Node root = parser.parse(sb.toString());
                   1148:     // printContents(root);
                   1149:     Vector databases = new Vector();
                   1150:     Vector tables = new Vector();
                   1151:     Vector layouts = new Vector();
                   1152:     Vector selects = new Vector();
                   1153:     Vector creates = new Vector();
                   1154:     Vector ids = new Vector();
1.1       rogo     1155: 
1.34      rogo     1156:     int mode = -1;
                   1157:     try
1.1       rogo     1158:     {
1.34      rogo     1159:       Node tempNode = root.find("convert/source", new int[] { 1, 1 });
                   1160:       if (tempNode == null)
                   1161:         throw new Error("parse error source tag missing");
                   1162:       System.out.println(tempNode.name);
                   1163:       int length = countNodes(tempNode);
                   1164:       for (int i = 1; i <= length; i++)
1.1       rogo     1165:       {
1.34      rogo     1166: 
                   1167:         DBBean database = new DBBean();
                   1168:         tables = new Vector();
                   1169:         layouts = new Vector();
                   1170:         selects = new Vector();
                   1171:         creates = new Vector();
                   1172:         ids = new Vector();
                   1173:         // parse dataBase
                   1174:         Node node = root.find("convert/source/database/url", new int[] { 1, 1, i, 1 });
                   1175:         Node node1 = root.find("convert/source/database/user", new int[] { 1, 1, i, 1, 1 });
                   1176:         Node node2 = root.find("convert/source/database/password", new int[] { 1, 1, i, 1, 1 });
                   1177:         Node node3 = root.find("convert/source/database", new int[] { 1, 1, i });
                   1178:         Node nodeMode = root.find("convert/source/database/mode", new int[] { 1, 1, i, 1, 1 });
                   1179: 
                   1180:         if (node3 == null)
                   1181:           throw new Error("parse error database tag missing");
                   1182:         if (node == null)
                   1183:           throw new Error("parse error url tag missing");
                   1184:         if (node1 == null)
                   1185:           throw new Error("parse error user tag missing");
                   1186:         if (node2 == null)
                   1187:           throw new Error("parse error password tag missing");
                   1188:         String url = node.getCharacters();
                   1189:         String user = node1.getCharacters();
                   1190:         String password = node2.getCharacters();
                   1191:         database.setURL(url.trim());
                   1192:         database.setUserAndPasswd(user.trim(), password.trim());
                   1193:         System.out.println(node.name + " " + node.getCharacters());
                   1194:         System.out.println(node1.name + " " + node1.getCharacters());
                   1195:         System.out.println(node2.name + " " + node2.getCharacters());
                   1196:         String modeString = "";
                   1197:         if (nodeMode == null)
                   1198:           modeString = "convert";
                   1199:         else
                   1200:           modeString = nodeMode.getCharacters();
                   1201:         if (modeString.equals("convert"))
                   1202:           mode = DataBase.CONVERT_MODE;
                   1203:         else if (modeString.equals("append"))
                   1204:           mode = DataBase.APPEND_MODE;
                   1205:         else if (modeString.equals("update"))
                   1206:           mode = DataBase.UPDATE_MODE;
                   1207: 
                   1208:         //   if(node3!=null)
                   1209:         // System.out.println(node3.name);
                   1210: 
                   1211:         int length2 = countNodes(node3);
                   1212: 
                   1213:         System.out.println("number of tables " + length2);
                   1214: 
                   1215:         for (int j = 1; j <= length2; ++j)
                   1216:         {
                   1217:           Node node4 = root.find("convert/source/database/table", new int[] { 1, 1, i, j });
                   1218:           Node node5 = root.find("convert/source/database/table/select", new int[] { 1, 1, i, j, 1 });
                   1219:           Node node6 = root.find("convert/source/database/table/create", new int[] { 1, 1, i, j, 1 });
                   1220:           if (node4 != null)
                   1221:             System.out.println(node4.name + " " + node4.attributes.get("layout").equals(""));
                   1222:           if (node5 != null)
                   1223:             System.out.println(node5.name + " " + node5.getCharacters());
                   1224:           if (node6 != null)
                   1225:             System.out.println(node6.name + " " + node6.getCharacters());
                   1226:           if (node4 == null)
                   1227:             throw new Error("parse error table tag missing");
                   1228:           // if(node5==null) throw new Error("parse error select tag missing");
                   1229:           // if(node6==null) throw new Error("parse error create tag missing");
                   1230:           String name = (String) node4.attributes.get("name");
                   1231:           String layout = (String) node4.attributes.get("layout");
                   1232:           String id = (String) node4.attributes.get("id");
                   1233:           System.out.println("id was " + id);
                   1234: 
                   1235:           if (name == null)
                   1236:             throw new Error("parse error required table tag attribute name missing");
                   1237:           if (layout == null)
                   1238:             layout = "";
                   1239:           if (id == null)
                   1240:             id = "";
                   1241:           if (name.equals(""))
                   1242:             throw new Error("parse error table tag attribute must not be empty");
                   1243:           tables.add(name);
                   1244:           layouts.add(layout);
                   1245:           ids.add(id);
                   1246:           String query = (node5 == null) ? "" : node5.getCharacters();
                   1247:           if (query.equals(""))
                   1248:             System.err.println("Warning empty select tag or  select tag missing !!");
                   1249:           query = (query.equals("")) ? "select * from " + database.getQC() + name + database.getQC() : query;
                   1250:           selects.add(query);
                   1251:           if (node6 != null)
                   1252:             creates.add(node6.getCharacters().trim());
                   1253:           else
                   1254:             creates.add("");
                   1255: 
                   1256:         }
                   1257:         databases.add(new DataBase(database, tables, layouts, selects, creates, ids, mode));
1.1       rogo     1258:       }
1.34      rogo     1259:       DBBean database = new DBBean();
                   1260:       // parse dataBase
                   1261:       Node node = root.find("convert/destination/database/url", new int[] { 1, 1, 1, 1 });
                   1262:       Node node1 = root.find("convert/destination/database/user", new int[] { 1, 1, 1, 1, 1 });
                   1263:       Node node2 = root.find("convert/destination/database/password", new int[] { 1, 1, 1, 1, 1 });
                   1264:       String url = node.getCharacters();
                   1265:       String user = node1.getCharacters();
                   1266:       String password = node2.getCharacters();
                   1267:       System.out.println(url);
                   1268:       database.setURL(url.trim());
                   1269:       database.setUserAndPasswd(user.trim(), password.trim());
                   1270:       databases.add(new DataBase(database, null, null, null, null, null, 0));
                   1271:       //databases.add(database);
                   1272:       /*    for (Iterator iter = databases.iterator(); iter.hasNext();)
                   1273:          {
                   1274:            DataBase db = (DataBase) iter.next();
                   1275:            convertBatch(db.bean,database,db.tables,db.layouts,db.selects,db.creates);
                   1276:           
                   1277:          }*/
                   1278:       // printContents(node3);
                   1279:       //   FM2SQL.fmInstance=new FM2SQL();
                   1280:     } catch (Exception e)
                   1281:     {
                   1282:       // TODO Auto-generated catch block
                   1283:       e.printStackTrace();
1.1       rogo     1284:     }
1.34      rogo     1285:     return databases;
                   1286:   }
                   1287: 
                   1288:   private static int countNodes(Node tempNode)
                   1289:   {
                   1290:     int length = 0;
                   1291:     for (int i = 0; i < tempNode.contents.v.size(); ++i)
1.1       rogo     1292:     {
1.34      rogo     1293:       Node node = (Node) tempNode.contents.v.elementAt(i);
                   1294:       if (node.type.equals("element"))
1.1       rogo     1295:       {
1.34      rogo     1296:         if (node.name.equals("database"))
                   1297:           length++;
                   1298:         if (node.name.equals("table"))
                   1299:           length++;
1.1       rogo     1300:       }
1.34      rogo     1301: 
                   1302:       // System.out.println(((Node)tempNode.contents.v.elementAt(i)).attributes+" "+i);
1.1       rogo     1303:     }
1.34      rogo     1304:     return length;
                   1305:   }
                   1306:   private static void printContents(Node root)
                   1307:   {
                   1308: 
                   1309:     Vector contents = (root.index == null) ? root.contents.v : root.index.v;
                   1310:     for (int i = 0; i < contents.size(); ++i)
1.1       rogo     1311:     {
1.34      rogo     1312:       Node n = (Node) contents.elementAt(i);
                   1313:       if (n.type.equals("element"))
                   1314:       {
                   1315:         System.out.println("tag " + n.name);
                   1316:         System.out.println(n.getCharacters());
                   1317:         //contents=n.contents.v i=0;
                   1318:       }
                   1319:       // System.out.println(n.type);
                   1320: 
1.1       rogo     1321:     }
1.34      rogo     1322:   }
                   1323:   public static void readXMLFile(String xmlFile)
1.1       rogo     1324:   {
1.34      rogo     1325:     try
                   1326:     {
                   1327:       // read XML Metadata from a file
                   1328:       FileInputStream fi = new FileInputStream(xmlFile);
                   1329:       InputStreamReader isr = new InputStreamReader(fi, "UTF-8");
                   1330:       BufferedReader buffr = new BufferedReader(isr);
                   1331:       StringBuffer sb = new StringBuffer();
                   1332:       int c = 0;
                   1333:       while ((c = buffr.read()) != -1)
                   1334:       {
                   1335:         char ch = (char) c;
                   1336:         sb.append(ch);
                   1337:         // System.out.print((char)c);
                   1338:       }
                   1339:       parseXMLConfig(sb);
                   1340:     } catch (Exception e)
                   1341:     {
                   1342:       e.printStackTrace();
                   1343:     }
1.1       rogo     1344:   }
1.34      rogo     1345:   public static class DataBase
                   1346:   {
                   1347:     DBBean bean;
                   1348:     Vector creates;
                   1349:     Vector selects;
                   1350:     Vector layouts;
                   1351:     Vector tables;
                   1352:     Vector ids;
                   1353:     final static int CONVERT_MODE = 1;
                   1354:     final static int APPEND_MODE = 2;
                   1355:     final static int UPDATE_MODE = 3;
                   1356:     int mode = -1;
                   1357: 
                   1358:     public DataBase(DBBean bean, Vector tables, Vector layouts, Vector selects, Vector creates, Vector ids, int mode)
                   1359:     {
                   1360:       this.bean = bean;
                   1361:       this.tables = tables;
                   1362:       this.layouts = layouts;
                   1363:       this.selects = selects;
                   1364:       this.creates = creates;
                   1365:       this.ids = ids;
                   1366:       this.mode = mode;
                   1367:       this.bean.setIDVector(ids);
                   1368:     }
                   1369:     public void exportToXML(BufferedWriter buffr) throws Exception
                   1370:     {
                   1371:       // ids=bean.getIDVector();
                   1372:       buffr.write("    <database>\n");
                   1373:       buffr.write("      <url>" + bean.url + "</url>\n");
                   1374:       buffr.write("      <user>" + bean.user + "</user>\n");
                   1375:       buffr.write("      <password>" + bean.passwd + "</password>\n");
                   1376:       String modeString = "";
                   1377:       if (mode == CONVERT_MODE)
                   1378:         modeString = "convert";
                   1379:       else if (mode == APPEND_MODE)
1.5       rogo     1380:         modeString = "append";
1.34      rogo     1381:       else if (mode == UPDATE_MODE)
                   1382:         modeString = "update";
                   1383: 
                   1384:       buffr.write("      <mode>" + modeString + "</mode>\n");
                   1385:       int index = 0;
                   1386:       while (index < tables.size())
                   1387:       {
                   1388:         String table = (String) tables.get(index);
                   1389:         String layout = (String) layouts.get(index);
                   1390:         String select = (String) selects.get(index);
                   1391:         String create = (String) creates.get(index);
                   1392:         String id = (String) ids.get(index);
                   1393: 
                   1394:         buffr.write("      <table name = \"" + table + "\" layout = \"" + layout + "\" id = \"" + id + "\" >\n");
                   1395:         buffr.write("         <select>" + convertToEntities(select) + "</select>\n");
                   1396:         if (!create.equals(""))
                   1397:           buffr.write("         <create>" + create + "         </create>\n");
                   1398:         buffr.write("      </table>\n");
                   1399:         index++;
                   1400:       }
                   1401:       buffr.write("    </database>\n");
                   1402:     }
                   1403:     public String toString()
                   1404:     {
                   1405:       return bean.url + " " + tables;
                   1406:     }
1.1       rogo     1407: 
1.34      rogo     1408:   }
                   1409:   public static String convertToUTF8(Object command)
1.1       rogo     1410:   {
1.34      rogo     1411:     String str = null;
                   1412:     try
                   1413:     {
                   1414:       str = new String(command.toString().getBytes("UTF-8"));
                   1415:     } catch (UnsupportedEncodingException e)
                   1416:     {
                   1417:       // TODO Auto-generated catch block
                   1418:       e.printStackTrace();
                   1419:     }
                   1420:     return str;
1.1       rogo     1421:   }
                   1422:   public static void writeConfig(String file, DataBase source, DataBase destination) throws Exception
                   1423:   {
1.34      rogo     1424:     if (!file.toLowerCase().endsWith(".xml"))
                   1425:       file += ".xml";
1.1       rogo     1426:     File f = new File(file);
1.34      rogo     1427: 
                   1428:     FileOutputStream fout = new FileOutputStream(f);
                   1429:     OutputStreamWriter outsw = new OutputStreamWriter(fout, "UTF-8");
1.1       rogo     1430:     BufferedWriter buffw = new BufferedWriter(outsw);
                   1431:     buffw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
                   1432:     buffw.newLine();
                   1433:     buffw.write("<convert>\n");
                   1434:     buffw.write("  <source>\n");
                   1435:     source.exportToXML(buffw);
                   1436:     buffw.write("  </source>\n");
                   1437:     buffw.write("\n  <destination>\n");
                   1438:     destination.exportToXML(buffw);
                   1439:     buffw.write("  </destination>\n");
                   1440:     buffw.write("</convert>\n");
                   1441:     buffw.close();
1.34      rogo     1442:   }
                   1443: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>