File:  [Repository] / FM2SQL / Attic / Convert.java
Revision 1.23: download - view: text, annotated - select for diffs - revision graph
Thu Jan 29 11:25:40 2004 UTC (20 years, 5 months ago) by rogo
Branches: MAIN
CVS tags: HEAD
*** empty log message ***

    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:   }
   61:   public static void convertBatch(DBBean source,DBBean destination,Vector names,Vector layouts,Vector selects,Vector creates,int mode) throws Exception
   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();
   71:    bean = source;
   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);
  139:       if(mode==Convert.DataBase.CONVERT_MODE) 
  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)
  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);
  226:     while((row=bean.getNextRow())!=null)
  227:     {
  228: /*      //Vector row = (Vector) result[0].get(j);
  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 ( ");
  236:    */
  237:              //print rows
  238:      Object obj = null;
  239:      /*
  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;
  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: 
  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: 
  306:   public static void update(String source, String destination, Vector names, Vector layouts, Vector selects, Vector creates, Vector ids,int mode) throws Exception
  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;
  384:     
  385:         Vector row = null;
  386:         command = new StringBuffer();
  387: 
  388:         command.append("UPDATE ");
  389:         command.append(beanDest.getQC());
  390:         command.append(convertText((String) names.get(tbIndex)));
  391:         command.append(beanDest.getQC());
  392:         command.append(" SET  ");
  393: 
  394:         int size = bean.getColumnNames().size();
  395:         for (int i = 0; i < size - 1; ++i)
  396:           command.append(beanDest.getQC()+convertText((String)bean.getColumnNames().get(i))+beanDest.getQC()+" = ? ,");
  397:         command.append(convertText((String)bean.getColumnNames().get(size-1))+" = ? ");
  398:         command.append("WHERE "+convertText(ids.get(tbIndex).toString())+ " =  ?");
  399:         PreparedStatement pstm = beanDest.getConnection().prepareStatement(command.toString());
  400:         System.out.println(command+" "+tbIndex);
  401: 		    int rowCount = bean.getRowCount(query);
  402:         int idIndex = bean.getColumnNames().indexOf(ids.get(tbIndex) );
  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:           }
  444:          pstm.setString(row.size()+1,row.get(idIndex).toString());
  445: 				 //System.out.println(pstm.toString());
  446: 				// System.exit(0);
  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);
  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:       }
  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:     }
  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:     }
  479: 
  480:   }
  481: 
  482: 
  483: 
  484:   public static void  convert(String source,String destination,Vector names,Vector layouts,Vector selects,Vector creates,Vector ids,int mode) throws Exception
  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: 	 java.util.TreeSet myIds;
  493:    dialog.thread=Thread.currentThread();
  494:    // setting user and passwd 
  495:    bean.setUserAndPasswd(user,passwd);
  496:   // setting user and passwd 
  497:   beanDest.setUserAndPasswd(userDest,passwdDest);
  498:   dialog.setSize(400,250);
  499:    StringBuffer command= null;
  500:    String query = null;
  501:     try
  502:     {
  503:       //bean.setConnection("jdbc:fmpro:http://141.14.237.74:8050");    
  504: 	  //bean.setConnection("jdbc:postgresql://erebos/test","postgres","rogo");
  505:       bean.setConnection(source);
  506:       if(names==null)
  507:        names=bean.getTableNames();
  508:      // Collections.sort(names);
  509:         int tbIndex = 1;
  510:      
  511:      // System.out.println("Start at "+names.indexOf("archimedes_facsimiles"));
  512:       for(tbIndex=0;tbIndex<names.size();++tbIndex)
  513:       {
  514:         Vector[] result = null;
  515:       try
  516:       {
  517:         query = "select * from " + bean.getQC() + names.get(tbIndex).toString() + bean.getQC();
  518:         String layout = (layouts.isEmpty()) ? "" : layouts.get(tbIndex).toString();
  519:         query = (selects != null) ? selects.get(tbIndex).toString() : query;
  520:         //if  vectors[1].get(i) != null)
  521:         if (layout != "")
  522:         {
  523:           layout = " layout " + bean.getQC() + layout + bean.getQC();
  524:           String name = names.get(tbIndex).toString();
  525:           StringBuffer queryLayout = new StringBuffer(query);
  526:           queryLayout.insert(queryLayout.indexOf(name) + name.length() + 1, " " + layout);
  527:           query = queryLayout.toString();
  528:           System.out.println("added layout  " + query);
  529: 
  530:         }
  531:         //  if ( layout!= "")
  532:         //	 query += " layout " + bean.getQC() + layout + bean.getQC();
  533:         dialog.title.setText("Getting table data ...");
  534:         dialog.table.setText(names.get(tbIndex).toString());
  535:         dialog.status.setText("Table " + (tbIndex + 1) + " of " + names.size());
  536:         dialog.show();
  537:         //result = bean.getQueryData(query, dialog, 0);
  538:         bean.getConnection();
  539:         bean.makeQuery(query, 50);
  540:        // @TODO id Vector must be used in querys while loop over id ranges where id>=start and id<=end
  541:         myIds = bean.getIDVector(ids.get(tbIndex).toString(),(String) names.get(tbIndex));
  542:         System.out.println("ID LIST SIZE "+myIds.size());
  543: 
  544:       } catch (Exception e)
  545:       {
  546:         continue;
  547:       }
  548:         //beanDest.setConnection("jdbc:postgresql://erebos/test3");
  549:         beanDest.setConnection(destination);
  550: 
  551:         Statement stm = beanDest.getConnection().createStatement();
  552: 
  553:         Vector tables = beanDest.getTableNames();
  554:        // Collections.sort(tables);
  555:         System.out.println(names.get(tbIndex) + " " + tables.indexOf(convertText((String) names.get(tbIndex)))); // "//beanDest.getTypeNames()); 
  556:         tables = beanDest.getTableNames();
  557:         // System.out.println(beanDest.getTableNames(beanDest.getCatalogs().get(2).toString()));
  558:         stm = beanDest.getConnection().createStatement();
  559:         // System.exit(0);
  560:       if(mode==Convert.DataBase.CONVERT_MODE) 
  561:       {
  562:         if(tables.indexOf(names.get(tbIndex))>=0) 
  563:         {
  564:          stm.executeUpdate("drop table "+beanDest.getQC()+names.get(tbIndex)+beanDest.getQC());
  565:         tables.remove((String)names.get(tbIndex));
  566:         System.out.println("dropped table"+ names.get(tbIndex));
  567:         }
  568:         else
  569:         if(tables.indexOf(convertText(names.get(tbIndex).toString()))>=0) 
  570:         {
  571:          stm.executeUpdate("drop table "+beanDest.getQC()+convertText((String)names.get(tbIndex))+beanDest.getQC());
  572:         tables.remove(convertText((String)names.get(tbIndex)));
  573:         System.out.println("dropped table"+ names.get(tbIndex));
  574:         }
  575:  
  576: 				if(tables.indexOf(names.get(tbIndex))<0&&tables.indexOf(convertText(names.get(tbIndex).toString()))<0) 
  577:         {
  578:           if(creates.get(tbIndex).equals("")||creates.get(tbIndex).toString().toLowerCase().indexOf("create")<0)
  579:           {
  580:             System.out.println("Warning empty or invalid create statement - creating one for you\n");
  581:         
  582:           command = new StringBuffer(50);
  583:           command.append("CREATE TABLE ");
  584:           command.append(beanDest.getQC());
  585:           command.append(convertText((String)names.get(tbIndex)));
  586:           command.append(beanDest.getQC());
  587:           command.append("(");
  588:            String type = null;
  589:           Vector columnNames = bean.getColumnNames();
  590:           for(int i=0;i<columnNames.size()-1;++i)
  591:           {
  592:            type = bean.metaData.getColumnTypeName(i+1);
  593:          //   System.out.println(i+" "+result[1].get(i)+" "+type);
  594:           type = (type.equals("NUMBER")) ?"INT4":type;
  595:           type = (type.equals("CONTAINER")) ?"TEXT":type;
  596: 
  597:           command.append(beanDest.getQC() + convertText((String) columnNames.get(i)) + beanDest.getQC() + " " + type + ", ");
  598:         }
  599:           type = bean.metaData.getColumnTypeName(columnNames.size());
  600:           type = (type.equals("NUMBER")) ? "INT4" : type;
  601:           type = (type.equals("CONTAINER")) ? "TEXT" : type;
  602:           command.append(beanDest.getQC() + convertText((String) columnNames.get(columnNames.size() - 1)) + beanDest.getQC() + " " + type);
  603:           command.append(" )");
  604: 
  605:           // System.out.println(command);
  606:           //  System.exit(0);
  607:           //command.append(DBBean.getQC());   
  608:           } else
  609:           command=new StringBuffer().append(creates.get(tbIndex).toString());
  610:           stm.executeUpdate(command.toString());
  611:         
  612:       }
  613:     }
  614:     // System.out.println(names);
  615:     /*    
  616:     if (idVal == "")
  617:           stm.executeUpdate("INSERT  INTO " + DBBean.getQC() + box.getSelectedItem() + DBBean.getQC() + " (" + DBBean.getQC() + columnName + DBBean.getQC() + ")  VALUES ('" + value + "') ");
  618:         else
  619:           stm.executeUpdate("UPDATE   " + DBBean.getQC() + box.getSelectedItem() + DBBean.getQC() + " SET  " + DBBean.getQC() + columnName + DBBean.getQC() + "='" + value + "' WHERE ID='" + idVal + "' ");
  620:       */
  621: 
  622:     // Vector[] result2=beanDest.getQueryData(names.get(tbIndex).toString());
  623:     // print results to screen
  624:    /* for (int i = 0; i < result[1].size(); ++i)
  625:     {
  626:       //print Header
  627:       // System.out.println(" "+result[1].get(i)+" "+result2[1].get(i)+" "+i);
  628:     }*/
  629:     // System.out.println();
  630:     dialog.title.setText("Writing table data ...");
  631:     
  632:     //for (int j = 0; j < result[0].size(); ++j)
  633:     int j=-1;
  634:     Vector row = null;
  635:     command = new StringBuffer();
  636: 
  637:     command.append("INSERT  INTO ");
  638:     command.append(beanDest.getQC());
  639:     command.append(convertText((String) names.get(tbIndex)));
  640:     command.append(beanDest.getQC());
  641:     command.append(" values ( ");
  642:     
  643:     for(int i=0;i<bean.getColumnNames().size()-1;++i) command.append("?,");
  644:     command.append("?)");
  645:     PreparedStatement pstm = beanDest.getConnection().prepareStatement(command.toString());
  646:     System.out.println(command);
  647:     int rowCount= myIds.size();//bean.getRowCount(query)
  648:     Vector vec = new Vector(myIds);
  649:     for(int k=0;k<vec.size()-1000;k=k+1000)
  650:     {
  651:      System.out.println(vec.get(k)+" "+vec.get(k+1000));
  652:     }
  653:     while((row=bean.getNextRow())!=null)
  654:     {
  655:       j++;
  656:       // row = (Vector) result[0].get(j);
  657:    /*   command = new StringBuffer();
  658: 
  659:         command.append("INSERT  INTO ");
  660:         command.append(beanDest.getQC());
  661:         command.append(convertText((String) names.get(tbIndex)));
  662:         command.append(beanDest.getQC());
  663:         command.append(" values ( ");
  664:      */
  665:            //print rows
  666:      Object obj = null;
  667:     /* for(int k=0;k<row.size()-1;++k)
  668:      {
  669:         obj = row.get(k);
  670:         //System.out.println("row "+obj+" "+k);
  671:        if(obj!=null&&!(obj instanceof ArrayList))
  672:        command.append("'"+convertUml(obj.toString())+"',"); 
  673:        else if(obj!=null&&   obj instanceof ArrayList)
  674:        command.append("'"+convertUml(((ArrayList)obj).get(0).toString())+"',"); 
  675:        else command.append("NULL,");
  676:      }
  677:      obj = row.get(row.size() - 1);
  678:      if (obj != null && !(obj instanceof ArrayList))
  679:      command.append("'"+convertUml(obj.toString())+"')"); 
  680:       else
  681:       if(obj!=null&&   obj instanceof ArrayList)
  682:        command.append("'"+convertUml(((ArrayList)obj).get(0).toString())+"')");         //command.append(obj.toString()+")");
  683:        else command.append("NULL)");
  684:       */
  685:       //command.append("'"+row.get(row.size()-1)+"')"); 
  686:       //command.append(" )");
  687:       //  for(int k=0;k<row.size();++k)
  688: 
  689:       // System.out.println();
  690:       //   System.out.println(command+" "+j+" "+row.size()+" "+  ((Vector)result2[0].get(j)).size());
  691:       // System.out.println(command);
  692:       for(int k=0;k<row.size();++k) 
  693:       {
  694:         obj = row.get(k);
  695:         if(obj instanceof ArrayList) obj=((List)obj).get(0);
  696:         String str =(obj==null) ? "NULL":obj.toString();
  697:         if(!str.equals("NULL"))
  698:         pstm.setString(k+1,str); 
  699:         else pstm.setNull(k+1,Types.NULL);
  700:       }
  701:       pstm.execute();
  702:       //stm.executeUpdate(command.toString());
  703:       dialog.progress.setValue((int) (((double) (j + 1) / (double) rowCount) * 100.0));
  704: 	// System.out.println( (int)(((double)(j+1)/(double)result[0].size())*100.0)+" "+result[0].size()+" "+j);
  705:    command = null;
  706: }// to for loop    
  707:    
  708:     }
  709:  } catch(Exception e) 
  710:    {
  711:      System.out.println("Error while connecting to database "+ e);
  712:      dialog.setVisible(false);
  713: 	   dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
  714: 		 FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
  715: 		 java.io.ByteArrayOutputStream b = new java.io.ByteArrayOutputStream();
  716: 		 java.io.PrintStream stream = new java.io.PrintStream(b);
  717: 		 stream.print(command+"\n\n");
  718: 		 e.printStackTrace(stream);
  719: 		 FM2SQL.showErrorDialog(b.toString(), "Error occured !");
  720: 	 
  721:    }
  722: 	 dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
  723: 	 FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
  724: 
  725: 	 dialog.setVisible(false); 
  726:   }
  727: 
  728:  public static String convertText(String newName)
  729:   {
  730:     StringBuffer alterMe = new StringBuffer(newName.trim().toLowerCase());
  731:     int length = alterMe.length();
  732:     int j = 0;
  733:     int index=alterMe.indexOf(".fp5");
  734:     if ( index >= 0 )
  735:      {
  736:        alterMe.delete(index, index + 4);
  737:        length = length - 4;
  738:      }
  739:  
  740:     while (j < length)
  741:     { 
  742:       if (alterMe.charAt(j) == ' ')
  743:       {
  744:         alterMe.setCharAt(j, '_');
  745:     //    if(j<length-1) j=j+1;
  746:       }
  747:       else
  748:       if (alterMe.charAt(j) == '_')
  749:       {
  750: 		
  751: 		if(alterMe.charAt(j+1)=='_')
  752: 		alterMe.deleteCharAt(j);
  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, 'a');
  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, 'o');
  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, 'u');
  776: 				alterMe.insert(j+1,"e");
  777: 				length=length+1;
  778: 				if(j<length-1) j=j+1;
  779: 			  }
  780: 		else
  781:     if (alterMe.charAt(j) == 'ß')
  782:     {
  783:       alterMe.setCharAt(j, 's');
  784:       alterMe.insert(j + 1, "s");
  785:       length = length + 1;
  786:       if (j < length - 1)
  787:         j = j + 1;
  788:     } else if (alterMe.charAt(j) == ':')
  789:     {
  790:       if(j<length-1)
  791:       {
  792:       if (alterMe.charAt(j + 1) == ':')
  793:       {
  794:         alterMe.setCharAt(j,'_');
  795:         alterMe.delete(j+1,j+2);
  796:       length = length - 1;
  797:    
  798:       }
  799:  
  800:       if (j < length - 1)
  801:         j = j + 1;
  802:       }
  803:     } else if (alterMe.charAt(j) == '-')
  804:     {
  805:       alterMe.setCharAt(j, '_');
  806: 
  807:     } else if (alterMe.charAt(j) == '?')
  808: 			{
  809: 				// changed ? to _ because of update statement
  810: 		    alterMe.setCharAt(j,'_');
  811:        // length = length + 1;
  812: 			 // j=j+1;
  813: 			System.out.println(alterMe);
  814: 			}
  815: 
  816:     else if (alterMe.charAt(j) == '.')
  817:     {
  818:       if(j==length-1)
  819:       {
  820:         alterMe.delete(j, j); 
  821:         length--;
  822:       }
  823: 			else
  824: 			alterMe.setCharAt(j,'_');
  825:     }
  826: 
  827:     ++j;
  828:   }
  829:     return alterMe.toString();
  830:   }
  831:   public static String convertToEntities(String newName)
  832:   {
  833:     StringBuffer alterMe = new StringBuffer(newName.trim());
  834:     int length = alterMe.length();
  835:     int j = 0;
  836: 
  837:     while (j < length)
  838:     {
  839: 
  840:       if (alterMe.charAt(j) == '>')
  841:       {
  842:         alterMe.setCharAt(j, '&');
  843:         alterMe.insert(j + 1, "gt;");
  844:         length = length + 2;
  845:         if (j < length - 1)
  846:           j = j + 1;
  847: 
  848:       } else if (alterMe.charAt(j) == '<')
  849:       {
  850:         alterMe.setCharAt(j, '&');
  851:         alterMe.insert(j + 1, "lt;");
  852:         length = length + 2;
  853:         if (j < length - 1)
  854:           j = j + 1;
  855: 
  856:       }
  857:       ++j;
  858:     }
  859:     return alterMe.toString();
  860:   }
  861:   public static String convertUml(String newName)
  862:    {
  863: 	 StringBuffer alterMe = new StringBuffer(newName.trim());
  864: 	 int length = alterMe.length();
  865: 	 int j = 0;
  866: 	
  867: 	
  868: 	 while (j < length)
  869: 	 { 
  870: 	
  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: 	else
  959: 	 if (alterMe.charAt(j) == '%')
  960: 	   {
  961: 		 alterMe.setCharAt(j, '\\');
  962: 		 alterMe.insert(j + 1, "%");
  963: 		 length = length + 1;
  964: 		 if(j<length-1) j=j+1;
  965: 	   }*/
  966: 		++j;
  967: 	 }
  968: 	 return alterMe.toString();
  969:    }
  970:    
  971:    public static void parseXMLConfig(StringBuffer sb)
  972:     {
  973:       boolean finished = false;
  974:       // parse string and build document tree
  975:       Xparse parser =new Xparse();
  976:       parser.changeEntities = true;
  977:       Node  root = parser.parse(sb.toString());
  978:        // printContents(root);
  979:       Vector databases= new Vector();
  980:       Vector tables = new Vector();
  981:       Vector layouts = new Vector();
  982:       Vector selects = new Vector();
  983:       Vector creates = new Vector();
  984: 			Vector ids     = new Vector();
  985:       int mode = -1;
  986:    
  987:       try
  988:       {
  989:         Node tempNode = root.find("convert/source", new int[] { 1, 1 });
  990:         if(tempNode==null) throw new Error("parse error source tag missing");
  991:         System.out.println(tempNode.name);
  992:         int length =countNodes(tempNode);
  993:         for (int i = 1; i <= length; i++)
  994:         {
  995:           
  996:           DBBean database = new DBBean();
  997:           tables = new Vector();
  998:           layouts = new Vector();
  999:           selects = new Vector();
 1000:           creates = new Vector();
 1001:           ids     = new Vector();
 1002:          // parse dataBase
 1003:           Node node = root.find("convert/source/database/url", new int[] { 1, 1, i, 1 });
 1004:           Node node1 = root.find("convert/source/database/user", new int[] { 1, 1, i, 1, 1 });
 1005:           Node node2 = root.find("convert/source/database/password", new int[] { 1, 1, i, 1, 1 });
 1006:           Node node3 = root.find("convert/source/database", new int[] { 1, 1, i});
 1007:     			Node nodeMode = root.find("convert/source/database/mode", new int[] {1, 1, i, 1,1});
 1008: 
 1009:           if(node3== null) throw new Error("parse error database tag missing");
 1010:           if(node==null) throw new Error("parse error url tag missing");
 1011:           if(node1==null) throw new Error("parse error user tag missing");
 1012:           if(node2==null) throw new Error("parse error password tag missing");
 1013:           String url=node.getCharacters();
 1014:           String user=node1.getCharacters();
 1015:           String password=node2.getCharacters();
 1016:           database.setURL(url.trim());
 1017:           database.setUserAndPasswd(user.trim(),password.trim());
 1018:           System.out.println(node.name + " " + node.getCharacters());
 1019:           System.out.println(node1.name + " " + node1.getCharacters());
 1020:           System.out.println(node2.name + " " + node2.getCharacters());
 1021:           String modeString = "";
 1022:           if (nodeMode == null)
 1023:             modeString = "convert";
 1024:           else
 1025:             modeString = nodeMode.getCharacters();
 1026:           if (modeString.equals("convert"))
 1027:             mode = DataBase.CONVERT_MODE;
 1028:           else
 1029:             if (modeString.equals("append"))
 1030:               mode = DataBase.APPEND_MODE;
 1031:             else
 1032:               if (modeString.equals("update"))
 1033:                 mode = DataBase.UPDATE_MODE;
 1034:        //   if(node3!=null)
 1035:          // System.out.println(node3.name);
 1036:           
 1037:           int length2= countNodes(node3);
 1038:     
 1039:          System.out.println("number of tables " +length2);
 1040:     
 1041:           for(int j=1;j<=length2;++j)
 1042:           {
 1043:             Node node4 = root.find("convert/source/database/table", new int[] { 1, 1, i, j });
 1044:             Node node5 = root.find("convert/source/database/table/select", new int[] { 1, 1, i, j, 1 });
 1045:             Node node6 = root.find("convert/source/database/table/create", new int[] { 1, 1, i, j, 1 });
 1046:             if(node4!=null)
 1047:             System.out.println(node4.name + " " + node4.attributes.get("layout").equals(""));
 1048:             if(node5!=null)
 1049:             System.out.println(node5.name + " " + node5.getCharacters());
 1050:             if(node6!=null)
 1051:             System.out.println(node6.name + " " + node6.getCharacters());
 1052:             if(node4==null) throw new Error("parse error table tag missing");
 1053:             // if(node5==null) throw new Error("parse error select tag missing");
 1054:             // if(node6==null) throw new Error("parse error create tag missing");
 1055:             String name = (String)node4.attributes.get("name");
 1056:             String layout = (String)node4.attributes.get("layout");
 1057:             String id = (String)node4.attributes.get("id");
 1058:             System.out.println("id was "+id);
 1059:             if(name==null) throw new Error("parse error required table tag attribute name missing");
 1060:             if(layout==null) layout ="";
 1061:             if(id==null)  id = "";
 1062:             if(name.equals("")) throw new Error("parse error table tag attribute must not be empty");
 1063:             tables.add(name);
 1064:             layouts.add(layout);
 1065:             ids.add(id);
 1066:             String query = (node5==null) ? "":node5.getCharacters(); 
 1067:             if(query.equals("")) System.err.println("Warning empty select tag or  select tag missing !!");
 1068:             query = (query.equals("")) ? "select * from "+database.getQC()+name+database.getQC():query;
 1069:             selects.add(query);
 1070:             if(node6!=null) creates.add(node6.getCharacters().trim());
 1071:              else
 1072:               creates.add("");
 1073:             
 1074:           }
 1075:           databases.add(new DataBase(database, tables, layouts, selects,creates,ids,mode));
 1076:         }
 1077:         DBBean database = new DBBean();
 1078:         // parse dataBase
 1079:         Node node = root.find("convert/destination/database/url", new int[] { 1, 1, 1, 1 });
 1080:         Node node1 = root.find("convert/destination/database/user", new int[] { 1, 1, 1, 1, 1 });
 1081:         Node node2 = root.find("convert/destination/database/password", new int[] { 1, 1, 1, 1, 1 });
 1082:         String url = node.getCharacters();
 1083:         String user = node1.getCharacters();
 1084:         String password = node2.getCharacters();
 1085:         System.out.println(url);
 1086:         database.setURL(url.trim());
 1087:         database.setUserAndPasswd(user.trim(), password.trim());
 1088:         //databases.add(database);
 1089:        for (Iterator iter = databases.iterator(); iter.hasNext();)
 1090:       {
 1091:         DataBase db = (DataBase) iter.next();
 1092:         if(mode!=DataBase.UPDATE_MODE)
 1093:         convertBatch(db.bean,database,db.tables,db.layouts,db.selects,db.creates,mode);
 1094:         else
 1095:         update(db.bean.url,database.url,db.tables,db.layouts,db.selects,db.creates,db.ids,mode);
 1096: 
 1097:       }
 1098:         // printContents(node3);
 1099:         //   FM2SQL.fmInstance=new FM2SQL();
 1100:       } catch (Exception e)
 1101:       {
 1102:         // TODO Auto-generated catch block
 1103:         e.printStackTrace();
 1104:       }
 1105:       /*
 1106:       Node tempNode=root.find(rootNode,new int[] {1});
 1107:       if(tempNode==null) return rtTag;
 1108:       int count=1;
 1109:       /* 
 1110:       for(int i=0;i<tempNode.contents.v.size();++i)
 1111:       {
 1112:         // System.out.println(((Node)tempNode.contents.v.elementAt(i)).attributes+" "+i);
 1113:       }*/
 1114:    /*
 1115:       if(tempNode.contents.v.size()==0) return rtTag;
 1116:       Node notNull=null;
 1117:       Node rtNode=root.find(rootNode+"/rt",new int[] {1,1});
 1118:       rtTag=(rtNode==null) ? "finished":rtNode.getCharacters();
 1119:       System.out.println("rt tag is "+ rtTag);
 1120:       int length=0;//(tempNode.contents.length()-1)/2;
 1121:       for(int i=0;i<tempNode.contents.v.size();++i)    {
 1122:         Node node=(Node)tempNode.contents.v.elementAt(i);
 1123:         if(node.type.equals("element"))
 1124:         if(node.name.equals("doc"))length++;
 1125:         // System.out.println(((Node)tempNode.contents.v.elementAt(i)).attributes+" "+i);
 1126:       }
 1127:      if(length==0) rtTag="finished";        
 1128:       while(count<=length)
 1129:       {
 1130:          //System.out.println(count+" "+length);
 1131:         tempNode=root.find(rootNode+"/doc",new int[] {1,count});
 1132:         if(tempNode==null)
 1133:         {
 1134:           rtTag="finished";
 1135:           break;
 1136:         }    
 1137:         Bundle.Document doc = new Bundle.Document();
 1138:         Hashtable attributes=tempNode.attributes;
 1139:         int docID=0;
 1140:         int ver=0;
 1141:          if(attributes != null)
 1142:         {
 1143:           docID=Integer.parseInt((String)attributes.get("id"));
 1144:           ver=Integer.parseInt((String)attributes.get("ver"));
 1145:           doc.addData("docID",(String)attributes.get("id"));
 1146:           doc.addData("ver",(String)attributes.get("ver"));
 1147:         }
 1148:         bundle.docs.put(new Integer(docID),doc);
 1149:         bundle.documents.addElement(doc);
 1150:         // System.out.println("id " +docID+" ver "+ver);
 1151:         Node fileNode=root.find(rootNode+"/doc/file",new int[] {1,count,1});
 1152:         attributes=(fileNode==null)? null:fileNode.attributes;
 1153:         int fileID=0;
 1154:         int fileVer=0;
 1155:         if(attributes != null)
 1156:         {
 1157:           fileID=Integer.parseInt((String)attributes.get("id"));
 1158:           fileVer=Integer.parseInt((String)attributes.get("ver"));
 1159:           doc.addData("fileID",(String)attributes.get("id"));
 1160:           doc.addData("fileVer",(String)attributes.get("ver"));
 1161:           // System.out.println("fileid " +fileID+" ver "+fileVer);
 1162:         }
 1163:      // add all Document tags to actual Document
 1164:         for(int i=0;i<tempNode.contents.v.size();++i)
 1165:         {   
 1166:           Node node=(Node)tempNode.contents.v.elementAt(i);
 1167:           if(node.type.equals("element"))
 1168:            {
 1169:              //System.out.println(node.name+" "+node.getCharacters()+" "+node.contents.v.size());
 1170:              if(node.contents.v.size()>1) 
 1171:              {
 1172:                for(int j=0;j<node.contents.v.size();++j)
 1173:                {  
 1174:                  Node node2=(Node)node.contents.v.elementAt(j);
 1175:                  if(node2.type.equals("element"))
 1176:                   {
 1177:                     doc.addData(node2.name,node2.getCharacters());
 1178:                     //System.out.println(node2.name+" "+node2.getCharacters()+" "+node2.contents.v.size());
 1179:                   }
 1180:                }
 1181:              } else doc.addData(node.name,node.getCharacters());
 1182:           }
 1183:         }
 1184:         bundle.docsLocator.put(doc.locator,doc); 
 1185:    
 1186:       bundle.documents.addElement(doc);
 1187:         count++;
 1188:       }      */
 1189:   
 1190:       // System.out.println(bundle.docs.size());
 1191:    // return rtTag;
 1192:     }
 1193:     public static Vector getXMLConfig(String xmlFile)
 1194:      {
 1195:        StringBuffer sb=null;
 1196:        try 
 1197:              {
 1198:                 // read XML Metadata from a file
 1199:                 FileInputStream fi= new FileInputStream(xmlFile);
 1200:                 InputStreamReader isr= new InputStreamReader(fi,"UTF-8");
 1201:                 BufferedReader buffr = new BufferedReader(isr);
 1202:                 sb=new StringBuffer();
 1203:                 int c=0;
 1204:                 while ((c = buffr.read()) != -1) 
 1205:                 {
 1206:                   char ch=(char)c;
 1207:                   sb.append(ch);
 1208:                   // System.out.print((char)c);
 1209:                 }
 1210:                
 1211:             } catch(Exception e) { e.printStackTrace();} 
 1212:   
 1213:        boolean finished = false;
 1214:        // parse string and build document tree
 1215:        Xparse parser =new Xparse();
 1216:        parser.changeEntities = true;
 1217:        Node  root = parser.parse(sb.toString());
 1218:    // printContents(root);
 1219:        Vector databases= new Vector();
 1220:        Vector tables = new Vector();
 1221:        Vector layouts = new Vector();
 1222:        Vector selects = new Vector();
 1223:        Vector creates = new Vector();
 1224:        Vector ids     = new Vector();
 1225:        
 1226:        int mode = -1;
 1227:        try
 1228:        {
 1229:          Node tempNode = root.find("convert/source", new int[] { 1, 1 });
 1230:          if(tempNode==null) throw new Error("parse error source tag missing");
 1231:          System.out.println(tempNode.name);
 1232:          int length =countNodes(tempNode);
 1233:          for (int i = 1; i <= length; i++)
 1234:          {
 1235:           
 1236:            DBBean database = new DBBean();
 1237:            tables = new Vector();
 1238:            layouts = new Vector();
 1239:            selects = new Vector();
 1240:            creates = new Vector();
 1241:            ids     = new Vector();
 1242:           // parse dataBase
 1243:            Node node = root.find("convert/source/database/url", new int[] { 1, 1, i, 1 });
 1244:            Node node1 = root.find("convert/source/database/user", new int[] { 1, 1, i, 1, 1 });
 1245:            Node node2 = root.find("convert/source/database/password", new int[] { 1, 1, i, 1, 1 });
 1246:            Node node3 = root.find("convert/source/database", new int[] { 1, 1, i});
 1247:            Node nodeMode = root.find("convert/source/database/mode", new int[] { 1, 1, i, 1, 1 });
 1248:            
 1249:            if(node3== null) throw new Error("parse error database tag missing");
 1250:            if(node==null) throw new Error("parse error url tag missing");
 1251:            if(node1==null) throw new Error("parse error user tag missing");
 1252:            if(node2==null) throw new Error("parse error password tag missing");
 1253:            String url=node.getCharacters();
 1254:            String user=node1.getCharacters();
 1255:            String password=node2.getCharacters();
 1256:            database.setURL(url.trim());
 1257:            database.setUserAndPasswd(user.trim(),password.trim());
 1258:            System.out.println(node.name + " " + node.getCharacters());
 1259:            System.out.println(node1.name + " " + node1.getCharacters());
 1260:            System.out.println(node2.name + " " + node2.getCharacters());
 1261:            String modeString = "";  
 1262:           if (nodeMode == null)
 1263:             modeString = "convert";
 1264:           else
 1265:             modeString = nodeMode.getCharacters();
 1266:           if (modeString.equals("convert"))
 1267:             mode = DataBase.CONVERT_MODE;
 1268:           else
 1269:             if (modeString.equals("append"))
 1270:               mode = DataBase.APPEND_MODE;
 1271:             else
 1272:               if (modeString.equals("update"))
 1273:                 mode = DataBase.UPDATE_MODE;
 1274: 			 
 1275:         
 1276:         //   if(node3!=null)
 1277:           // System.out.println(node3.name);
 1278:           
 1279:            int length2= countNodes(node3);
 1280:     
 1281:           System.out.println("number of tables " +length2);
 1282:     
 1283:            for(int j=1;j<=length2;++j)
 1284:            {
 1285:              Node node4 = root.find("convert/source/database/table", new int[] { 1, 1, i, j });
 1286:              Node node5 = root.find("convert/source/database/table/select", new int[] { 1, 1, i, j, 1 });
 1287:              Node node6 = root.find("convert/source/database/table/create", new int[] { 1, 1, i, j, 1 });
 1288:              if(node4!=null)
 1289:              System.out.println(node4.name + " " + node4.attributes.get("layout").equals(""));
 1290:              if(node5!=null)
 1291:              System.out.println(node5.name + " " + node5.getCharacters());
 1292:              if(node6!=null)
 1293:              System.out.println(node6.name + " " + node6.getCharacters());
 1294:              if(node4==null) throw new Error("parse error table tag missing");
 1295:              // if(node5==null) throw new Error("parse error select tag missing");
 1296:              // if(node6==null) throw new Error("parse error create tag missing");
 1297:              String name = (String)node4.attributes.get("name");
 1298:              String layout = (String)node4.attributes.get("layout");
 1299:              String id = (String)node4.attributes.get("id");
 1300:              System.out.println("id was "+id);
 1301:          
 1302:              if(name==null) throw new Error("parse error required table tag attribute name missing");
 1303:              if(layout==null) layout ="";
 1304:              if(id == null)  id="";
 1305:              if(name.equals("")) throw new Error("parse error table tag attribute must not be empty");
 1306:              tables.add(name);
 1307:              layouts.add(layout);
 1308:              ids.add(id);
 1309:              String query = (node5==null) ? "":node5.getCharacters(); 
 1310:              if(query.equals("")) System.err.println("Warning empty select tag or  select tag missing !!");
 1311:              query = (query.equals("")) ? "select * from "+database.getQC()+name+database.getQC():query;
 1312:              selects.add(query);
 1313:              if(node6!=null) creates.add(node6.getCharacters().trim());
 1314:               else
 1315:                creates.add("");
 1316:             
 1317:            }
 1318:            databases.add(new DataBase(database, tables, layouts, selects,creates,ids,mode));
 1319:          }
 1320:          DBBean database = new DBBean();
 1321:          // parse dataBase
 1322:          Node node = root.find("convert/destination/database/url", new int[] { 1, 1, 1, 1 });
 1323:          Node node1 = root.find("convert/destination/database/user", new int[] { 1, 1, 1, 1, 1 });
 1324:          Node node2 = root.find("convert/destination/database/password", new int[] { 1, 1, 1, 1, 1 });
 1325:          String url = node.getCharacters();
 1326:          String user = node1.getCharacters();
 1327:          String password = node2.getCharacters();
 1328:          System.out.println(url);
 1329:          database.setURL(url.trim());
 1330:          database.setUserAndPasswd(user.trim(), password.trim());
 1331:          databases.add(new DataBase(database,null,null,null,null,null,0));
 1332:          //databases.add(database);
 1333:     /*    for (Iterator iter = databases.iterator(); iter.hasNext();)
 1334:        {
 1335:          DataBase db = (DataBase) iter.next();
 1336:          convertBatch(db.bean,database,db.tables,db.layouts,db.selects,db.creates);
 1337:         
 1338:        }*/
 1339:          // printContents(node3);
 1340:          //   FM2SQL.fmInstance=new FM2SQL();
 1341:        } catch (Exception e)
 1342:        {
 1343:          // TODO Auto-generated catch block
 1344:          e.printStackTrace();
 1345:        }
 1346:        return databases;
 1347:       }
 1348: 
 1349: 
 1350:     private static int countNodes(Node tempNode)
 1351:     {
 1352:       int length = 0;
 1353:       for(int i=0;i<tempNode.contents.v.size();++i)    {
 1354:       Node node=(Node)tempNode.contents.v.elementAt(i);
 1355:       if(node.type.equals("element"))
 1356:       {
 1357:         if(node.name.equals("database"))length++;
 1358:         if(node.name.equals("table"))length++;
 1359:       }
 1360:       
 1361:             // System.out.println(((Node)tempNode.contents.v.elementAt(i)).attributes+" "+i);
 1362:        }
 1363:      return length;
 1364:     }
 1365:     private static void printContents(Node root)
 1366:     {
 1367:         
 1368:       
 1369:       Vector contents=(root.index==null)? root.contents.v:root.index.v;
 1370:       for (int i=0;i<contents.size();++i)
 1371:       {
 1372:         Node n=(Node)contents.elementAt(i);
 1373:         if(n.type.equals("element"))
 1374:         {
 1375:            System.out.println("tag "+n.name);
 1376:            System.out.println(n.getCharacters());
 1377:           //contents=n.contents.v i=0;
 1378:         }
 1379:         // System.out.println(n.type);
 1380:       
 1381:         
 1382:       
 1383:       }
 1384:     }
 1385:     public static void readXMLFile(String xmlFile)
 1386:     {
 1387:     try 
 1388:         {
 1389:            // read XML Metadata from a file
 1390:            FileInputStream fi= new FileInputStream(xmlFile);
 1391:            InputStreamReader isr= new InputStreamReader(fi,"UTF-8");
 1392:            BufferedReader buffr = new BufferedReader(isr);
 1393:            StringBuffer sb=new StringBuffer();
 1394:            int c=0;
 1395:            while ((c = buffr.read()) != -1) 
 1396:            {
 1397:              char ch=(char)c;
 1398:              sb.append(ch);
 1399:              // System.out.print((char)c);
 1400:            }
 1401:            parseXMLConfig(sb);
 1402:        } catch(Exception e) { e.printStackTrace();} 
 1403:     }
 1404: public static class DataBase 
 1405: {
 1406:   DBBean bean;
 1407:   Vector creates;
 1408:   Vector selects;
 1409:   Vector layouts;
 1410:   Vector tables;
 1411:   Vector ids;
 1412:   final static int CONVERT_MODE = 1;
 1413: 	final static int APPEND_MODE  = 2;
 1414: 	final static int UPDATE_MODE  = 3;
 1415:   int mode = -1;
 1416:   
 1417:   public DataBase(DBBean bean,Vector tables,Vector layouts,Vector selects,Vector creates,Vector ids,int mode)
 1418:   {
 1419:     this.bean = bean;
 1420:     this.tables = tables;
 1421:     this.layouts = layouts;
 1422:     this.selects = selects;
 1423:     this.creates = creates;
 1424:     this.ids     = ids;
 1425:     this.mode = mode;
 1426:     this.bean.setIDVector(ids);
 1427:   }
 1428:   public void exportToXML(BufferedWriter buffr) throws Exception
 1429:    {
 1430:     // ids=bean.getIDVector();
 1431:      buffr.write("    <database>\n");
 1432:      buffr.write("      <url>"+bean.url+"</url>\n");
 1433:      buffr.write("      <user>"+bean.user+"</user>\n");
 1434:      buffr.write("      <password>"+bean.passwd+"</password>\n");
 1435:      String modeString ="";
 1436:     if (mode == CONVERT_MODE)
 1437:       modeString = "convert";
 1438:     else
 1439:       if (mode == APPEND_MODE)
 1440:         modeString = "append";
 1441:       else
 1442:         if (mode == UPDATE_MODE)
 1443:           modeString = "update";
 1444:   
 1445:      buffr.write("      <mode>"+ modeString+"</mode>\n");
 1446:      int index = 0;
 1447:      while(index<tables.size())
 1448:      {
 1449:        String table=(String)tables.get(index);
 1450:        String layout=(String)layouts.get(index);
 1451:        String select=(String)selects.get(index);
 1452:        String create=(String)creates.get(index);
 1453:        String id=(String)ids.get(index);
 1454:    
 1455:        buffr.write("      <table name = \""+table+"\" layout = \""+layout+"\" id = \""+id+"\" >\n");
 1456:        buffr.write("         <select>"+ convertToEntities(select)+"</select>\n");
 1457:        if(!create.equals(""))
 1458:        buffr.write("         <create>"+create+"         </create>\n"); 
 1459:        buffr.write("      </table>\n");
 1460:        index++;
 1461:      }
 1462:      buffr.write("    </database>\n"); 
 1463:    }
 1464: public String toString() {return bean.url+" "+tables;}
 1465: 
 1466: }
 1467: public static String convertToUTF8(Object command)
 1468: {
 1469:   String str = null;
 1470:   try
 1471:   {
 1472:     str = new String(command.toString().getBytes("UTF-8"));
 1473:   } catch (UnsupportedEncodingException e)
 1474:   {
 1475:     // TODO Auto-generated catch block
 1476:     e.printStackTrace();
 1477:   }
 1478:   return str;
 1479: }
 1480:   public static void writeConfig(String file, DataBase source, DataBase destination) throws Exception
 1481:   {
 1482:     if(!file.toLowerCase().endsWith(".xml")) file+=".xml"; 
 1483:     File f = new File(file);
 1484:   
 1485:     FileOutputStream fout= new FileOutputStream(f);
 1486:     OutputStreamWriter outsw = new OutputStreamWriter(fout,"UTF-8");
 1487:     BufferedWriter buffw = new BufferedWriter(outsw);
 1488:     buffw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
 1489:     buffw.newLine();
 1490:     buffw.write("<convert>\n");
 1491:     buffw.write("  <source>\n");
 1492:     source.exportToXML(buffw);
 1493:     buffw.write("  </source>\n");
 1494:     buffw.write("\n  <destination>\n");
 1495:     destination.exportToXML(buffw);
 1496:     buffw.write("  </destination>\n");
 1497:     buffw.write("</convert>\n");
 1498:     buffw.close();
 1499:   } 
 1500: }  

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