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

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