Annotation of FM2SQL/Convert.java, revision 1.10

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

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