File:  [Repository] / FM2SQL / Attic / Convert.java
Revision 1.32: download - view: text, annotated - select for diffs - revision graph
Thu Feb 12 13:02:56 2004 UTC (20 years, 8 months ago) by rogo
Branches: MAIN
CVS tags: HEAD
incremental convert +mysql driver loads now under mac osX

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

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