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