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