Annotation of FM2SQL/Convert.java, revision 1.12

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

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