File:  [Repository] / FM2SQL / Attic / Convert.java
Revision 1.45: download - view: text, annotated - select for diffs - revision graph
Tue Mar 9 12:34:09 2004 UTC (20 years, 3 months ago) by rogo
Branches: MAIN
CVS tags: HEAD
minor bug fixes

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

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