Annotation of FM2SQL/Convert.java, revision 1.21

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

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