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

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

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