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

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

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