Annotation of FM2SQL/Convert.java, revision 1.22

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

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