Annotation of FM2SQL/Convert.java, revision 1.31
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: }
482:
483:
1.1 rogo 484:
1.21 rogo 485: 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 486: {
487: FM2SQL.ProgressDialog dialog = new FM2SQL.ProgressDialog(FM2SQL.fmInstance);
488: dialog.setTitle("Conversion running ...");
489: dialog.title.setText("Getting table data ...");
490: dialog.setLocation(FM2SQL.fmInstance.getLocationOnScreen().x+(FM2SQL.fmInstance.getWidth()-400)/2,FM2SQL.fmInstance.getLocationOnScreen().y+(FM2SQL.fmInstance.getHeight()-250)/2);
491: dialog.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
492: FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
1.27 rogo 493: java.util.TreeSet myIds = new TreeSet();
1.25 rogo 494: int deltaID = 1;
1.27 rogo 495: String idField = "";
1.1 rogo 496: dialog.thread=Thread.currentThread();
497: // setting user and passwd
498: bean.setUserAndPasswd(user,passwd);
499: // setting user and passwd
500: beanDest.setUserAndPasswd(userDest,passwdDest);
501: dialog.setSize(400,250);
502: StringBuffer command= null;
503: String query = null;
504: try
505: {
506: //bean.setConnection("jdbc:fmpro:http://141.14.237.74:8050");
507: //bean.setConnection("jdbc:postgresql://erebos/test","postgres","rogo");
508: bean.setConnection(source);
509: if(names==null)
510: names=bean.getTableNames();
511: // Collections.sort(names);
512: int tbIndex = 1;
513:
514: // System.out.println("Start at "+names.indexOf("archimedes_facsimiles"));
515: for(tbIndex=0;tbIndex<names.size();++tbIndex)
516: {
517: Vector[] result = null;
1.21 rogo 518: try
519: {
520: query = "select * from " + bean.getQC() + names.get(tbIndex).toString() + bean.getQC();
521: String layout = (layouts.isEmpty()) ? "" : layouts.get(tbIndex).toString();
522: query = (selects != null) ? selects.get(tbIndex).toString() : query;
1.1 rogo 523: //if vectors[1].get(i) != null)
524: if (layout != "")
525: {
526: layout = " layout " + bean.getQC() + layout + bean.getQC();
527: String name = names.get(tbIndex).toString();
528: StringBuffer queryLayout = new StringBuffer(query);
529: queryLayout.insert(queryLayout.indexOf(name) + name.length() + 1, " " + layout);
530: query = queryLayout.toString();
1.21 rogo 531: System.out.println("added layout " + query);
532:
1.1 rogo 533: }
534: // if ( layout!= "")
535: // query += " layout " + bean.getQC() + layout + bean.getQC();
536: dialog.title.setText("Getting table data ...");
537: dialog.table.setText(names.get(tbIndex).toString());
538: dialog.status.setText("Table " + (tbIndex + 1) + " of " + names.size());
539: dialog.show();
540: //result = bean.getQueryData(query, dialog, 0);
541: bean.getConnection();
1.21 rogo 542: bean.makeQuery(query, 50);
1.22 rogo 543: // @TODO id Vector must be used in querys while loop over id ranges where id>=start and id<=end
1.27 rogo 544: idField = ids.get(tbIndex).toString();
1.29 rogo 545: /*if (idField != "")
1.27 rogo 546: {
1.29 rogo 547: myIds = bean.getIDVector(ids.get(tbIndex).toString(), (String) names.get(tbIndex), query,1000);
1.27 rogo 548: System.out.println("ID LIST SIZE " + Math.round((double) myIds.size() / (double) numIntervalls) + " " + myIds.size());
1.29 rogo 549: deltaID = (int) Math.round((double) myIds.size() / (double) numIntervalls);
550:
551: }*/
552:
1.1 rogo 553: } catch (Exception e)
1.21 rogo 554: {
1.27 rogo 555: System.out.println(e);
1.21 rogo 556: continue;
557: }
1.1 rogo 558: //beanDest.setConnection("jdbc:postgresql://erebos/test3");
559: beanDest.setConnection(destination);
560:
561: Statement stm = beanDest.getConnection().createStatement();
562:
563: Vector tables = beanDest.getTableNames();
564: // Collections.sort(tables);
565: System.out.println(names.get(tbIndex) + " " + tables.indexOf(convertText((String) names.get(tbIndex)))); // "//beanDest.getTypeNames());
566: tables = beanDest.getTableNames();
567: // System.out.println(beanDest.getTableNames(beanDest.getCatalogs().get(2).toString()));
568: stm = beanDest.getConnection().createStatement();
569: // System.exit(0);
1.7 rogo 570: if(mode==Convert.DataBase.CONVERT_MODE)
1.1 rogo 571: {
572: if(tables.indexOf(names.get(tbIndex))>=0)
573: {
574: stm.executeUpdate("drop table "+beanDest.getQC()+names.get(tbIndex)+beanDest.getQC());
575: tables.remove((String)names.get(tbIndex));
576: System.out.println("dropped table"+ names.get(tbIndex));
577: }
578: else
579: if(tables.indexOf(convertText(names.get(tbIndex).toString()))>=0)
580: {
581: stm.executeUpdate("drop table "+beanDest.getQC()+convertText((String)names.get(tbIndex))+beanDest.getQC());
582: tables.remove(convertText((String)names.get(tbIndex)));
583: System.out.println("dropped table"+ names.get(tbIndex));
584: }
585:
586: if(tables.indexOf(names.get(tbIndex))<0&&tables.indexOf(convertText(names.get(tbIndex).toString()))<0)
587: {
588: if(creates.get(tbIndex).equals("")||creates.get(tbIndex).toString().toLowerCase().indexOf("create")<0)
589: {
590: System.out.println("Warning empty or invalid create statement - creating one for you\n");
591:
592: command = new StringBuffer(50);
593: command.append("CREATE TABLE ");
594: command.append(beanDest.getQC());
595: command.append(convertText((String)names.get(tbIndex)));
596: command.append(beanDest.getQC());
597: command.append("(");
598: String type = null;
599: Vector columnNames = bean.getColumnNames();
600: for(int i=0;i<columnNames.size()-1;++i)
601: {
602: type = bean.metaData.getColumnTypeName(i+1);
603: // System.out.println(i+" "+result[1].get(i)+" "+type);
604: type = (type.equals("NUMBER")) ?"INT4":type;
605: type = (type.equals("CONTAINER")) ?"TEXT":type;
606:
607: command.append(beanDest.getQC() + convertText((String) columnNames.get(i)) + beanDest.getQC() + " " + type + ", ");
608: }
609: type = bean.metaData.getColumnTypeName(columnNames.size());
610: type = (type.equals("NUMBER")) ? "INT4" : type;
611: type = (type.equals("CONTAINER")) ? "TEXT" : type;
612: command.append(beanDest.getQC() + convertText((String) columnNames.get(columnNames.size() - 1)) + beanDest.getQC() + " " + type);
613: command.append(" )");
614:
615: // System.out.println(command);
616: // System.exit(0);
617: //command.append(DBBean.getQC());
618: } else
619: command=new StringBuffer().append(creates.get(tbIndex).toString());
620: stm.executeUpdate(command.toString());
621:
622: }
623: }
624: // System.out.println(names);
625: /*
626: if (idVal == "")
627: stm.executeUpdate("INSERT INTO " + DBBean.getQC() + box.getSelectedItem() + DBBean.getQC() + " (" + DBBean.getQC() + columnName + DBBean.getQC() + ") VALUES ('" + value + "') ");
628: else
629: stm.executeUpdate("UPDATE " + DBBean.getQC() + box.getSelectedItem() + DBBean.getQC() + " SET " + DBBean.getQC() + columnName + DBBean.getQC() + "='" + value + "' WHERE ID='" + idVal + "' ");
630: */
631:
632: // Vector[] result2=beanDest.getQueryData(names.get(tbIndex).toString());
633: // print results to screen
634: /* for (int i = 0; i < result[1].size(); ++i)
635: {
636: //print Header
637: // System.out.println(" "+result[1].get(i)+" "+result2[1].get(i)+" "+i);
638: }*/
639: // System.out.println();
640: dialog.title.setText("Writing table data ...");
641:
642: //for (int j = 0; j < result[0].size(); ++j)
643: int j=-1;
644: Vector row = null;
1.3 rogo 645: command = new StringBuffer();
646:
647: command.append("INSERT INTO ");
648: command.append(beanDest.getQC());
649: command.append(convertText((String) names.get(tbIndex)));
650: command.append(beanDest.getQC());
651: command.append(" values ( ");
652:
653: for(int i=0;i<bean.getColumnNames().size()-1;++i) command.append("?,");
654: command.append("?)");
655: PreparedStatement pstm = beanDest.getConnection().prepareStatement(command.toString());
656: System.out.println(command);
1.27 rogo 657: int rowCount= (idField!="") ? myIds.size():bean.getRowCount(query);
1.23 rogo 658: Vector vec = new Vector(myIds);
1.26 rogo 659: int endIndex = -1;
1.29 rogo 660: String tempQuery=query;String tempID = bean.getQC()+idField+bean.getQC();
1.27 rogo 661: if (!idField.equals(""))
1.23 rogo 662: {
1.31 ! rogo 663: long startTime = System.currentTimeMillis();
1.29 rogo 664: while (true)
1.27 rogo 665: {
1.29 rogo 666: dialog.title.setText("Getting next ID Vector");
1.30 rogo 667: myIds = bean.getIDVector(ids.get(tbIndex).toString(), (String) names.get(tbIndex), tempQuery, numHits);
1.29 rogo 668: if(myIds.isEmpty()) break;
669: vec = new Vector(myIds);
670: rowCount = vec.size();
671: System.out.println("ID LIST SIZE " + Math.round((double) myIds.size() / (double) numIntervalls) + " " + myIds.size());
672: deltaID = (int) Math.round((double) myIds.size() / (double) numIntervalls);
673:
674: for (int k = 0; k < vec.size() - deltaID; k = k + deltaID)
675: {
676: System.out.println(vec.get(k) + " " + vec.get(k + deltaID) + " " + vec.lastElement());
677: if (query.indexOf("where") > 0)
678: tempQuery = query + " and " + tempID + ">=" + vec.get(k) + " and " + tempID + "<=" + vec.get(k + deltaID);
679: else
680: tempQuery = query + "where " + tempID + ">=" + vec.get(k) + " and " + tempID + "<=" + vec.get(k + deltaID);
681: System.out.println(tempQuery);
682: dialog.title.setText("Reading table data ...");
683:
684: bean.makeQuery(tempQuery, deltaID);
685: dialog.title.setText("Writing table data ...");
686:
687: command = writeDatainDestTable(dialog, command, k, pstm, rowCount);
688: endIndex = k + deltaID;
689: }
690: if ((vec.size() - 1) % numIntervalls == 0)
691: System.out.println("fits");
1.27 rogo 692: else
1.29 rogo 693: {
694: if (query.indexOf("where") > 0)
695: tempQuery = query + " and " + tempID + ">=" + vec.get(endIndex) + " and " + tempID + "<=" + vec.lastElement();
696: else
697: tempQuery = query + "where " + tempID + ">=" + vec.get(endIndex) + " and " + tempID + "<=" + vec.lastElement();
698: System.out.println(tempQuery);
699: dialog.title.setText("Reading table data ...");
700: bean.makeQuery(tempQuery, 0);
701: dialog.title.setText("Writing table data ...");
702: command = writeDatainDestTable(dialog, command, j, pstm, rowCount);
703:
704: System.out.println(" last intervall from " + vec.get(endIndex) + " " + vec.lastElement());
705: command = writeDatainDestTable(dialog, command, endIndex, pstm, rowCount);
706: }
1.27 rogo 707: if (query.indexOf("where") > 0)
1.29 rogo 708: tempQuery = query + " and " + tempID + ">" + vec.lastElement();
1.27 rogo 709: else
1.29 rogo 710: tempQuery = query + " where " + tempID + ">" + vec.lastElement();
711:
1.3 rogo 712: }
1.31 ! rogo 713: long endTime = System.currentTimeMillis();
! 714: System.out.println("Time for incremental convert elapsed "+(endTime-startTime));
1.27 rogo 715: } else
716: {
717: bean.makeQuery(query, 0);
718: command = writeDatainDestTable(dialog, command, j, pstm, rowCount);
1.1 rogo 719: }
1.27 rogo 720: }
1.1 rogo 721: } catch(Exception e)
722: {
723: System.out.println("Error while connecting to database "+ e);
724: dialog.setVisible(false);
725: dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
726: FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
727: java.io.ByteArrayOutputStream b = new java.io.ByteArrayOutputStream();
728: java.io.PrintStream stream = new java.io.PrintStream(b);
729: stream.print(command+"\n\n");
730: e.printStackTrace(stream);
731: FM2SQL.showErrorDialog(b.toString(), "Error occured !");
732:
733: }
734: dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
735: FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
736:
737: dialog.setVisible(false);
1.27 rogo 738: }
739: private static StringBuffer writeDatainDestTable(FM2SQL.ProgressDialog dialog, StringBuffer command, int j, PreparedStatement pstm, int rowCount) throws Exception, SQLException
740: {
741: Vector row;
742: while((row=bean.getNextRow())!=null)
743: {
744: j++;
745: // row = (Vector) result[0].get(j);
746: /* command = new StringBuffer();
747:
748: command.append("INSERT INTO ");
749: command.append(beanDest.getQC());
750: command.append(convertText((String) names.get(tbIndex)));
751: command.append(beanDest.getQC());
752: command.append(" values ( ");
753: */
754: //print rows
755: Object obj = null;
756: /* for(int k=0;k<row.size()-1;++k)
757: {
758: obj = row.get(k);
759: //System.out.println("row "+obj+" "+k);
760: if(obj!=null&&!(obj instanceof ArrayList))
761: command.append("'"+convertUml(obj.toString())+"',");
762: else if(obj!=null&& obj instanceof ArrayList)
763: command.append("'"+convertUml(((ArrayList)obj).get(0).toString())+"',");
764: else command.append("NULL,");
765: }
766: obj = row.get(row.size() - 1);
767: if (obj != null && !(obj instanceof ArrayList))
768: command.append("'"+convertUml(obj.toString())+"')");
769: else
770: if(obj!=null&& obj instanceof ArrayList)
771: command.append("'"+convertUml(((ArrayList)obj).get(0).toString())+"')"); //command.append(obj.toString()+")");
772: else command.append("NULL)");
773: */
774: //command.append("'"+row.get(row.size()-1)+"')");
775: //command.append(" )");
776: // for(int k=0;k<row.size();++k)
777:
778: // System.out.println();
779: // System.out.println(command+" "+j+" "+row.size()+" "+ ((Vector)result2[0].get(j)).size());
780: // System.out.println(command);
781: for(int k=0;k<row.size();++k)
782: {
783: obj = row.get(k);
784: if(obj instanceof ArrayList) obj=((List)obj).get(0);
785: String str =(obj==null) ? "NULL":obj.toString();
786: if(!str.equals("NULL"))
787: pstm.setString(k+1,str);
788: else pstm.setNull(k+1,Types.NULL);
789: }
790: pstm.execute();
791: //stm.executeUpdate(command.toString());
792: dialog.progress.setValue((int) (((double) (j + 1) / (double) rowCount) * 100.0));
793: // System.out.println( (int)(((double)(j+1)/(double)result[0].size())*100.0)+" "+result[0].size()+" "+j);
794: command = null;
795: }// to while loop
796: return command;
1.1 rogo 797: }
798:
799: public static String convertText(String newName)
800: {
801: StringBuffer alterMe = new StringBuffer(newName.trim().toLowerCase());
802: int length = alterMe.length();
803: int j = 0;
1.3 rogo 804: int index=alterMe.indexOf(".fp5");
805: if ( index >= 0 )
806: {
807: alterMe.delete(index, index + 4);
808: length = length - 4;
809: }
810:
1.1 rogo 811: while (j < length)
812: {
813: if (alterMe.charAt(j) == ' ')
814: {
815: alterMe.setCharAt(j, '_');
816: // if(j<length-1) j=j+1;
817: }
818: else
819: if (alterMe.charAt(j) == '_')
820: {
821:
822: if(alterMe.charAt(j+1)=='_')
823: alterMe.deleteCharAt(j);
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, 'a');
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, 'o');
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, 'u');
847: alterMe.insert(j+1,"e");
848: length=length+1;
849: if(j<length-1) j=j+1;
850: }
851: else
852: if (alterMe.charAt(j) == 'ß')
853: {
854: alterMe.setCharAt(j, 's');
855: alterMe.insert(j + 1, "s");
856: length = length + 1;
857: if (j < length - 1)
858: j = j + 1;
859: } else if (alterMe.charAt(j) == ':')
860: {
861: if(j<length-1)
862: {
863: if (alterMe.charAt(j + 1) == ':')
864: {
865: alterMe.setCharAt(j,'_');
866: alterMe.delete(j+1,j+2);
867: length = length - 1;
868:
869: }
870:
871: if (j < length - 1)
872: j = j + 1;
873: }
874: } else if (alterMe.charAt(j) == '-')
875: {
876: alterMe.setCharAt(j, '_');
877:
1.16 rogo 878: } else if (alterMe.charAt(j) == '?')
879: {
880: // changed ? to _ because of update statement
881: alterMe.setCharAt(j,'_');
882: // length = length + 1;
883: // j=j+1;
884: System.out.println(alterMe);
885: }
886:
1.3 rogo 887: else if (alterMe.charAt(j) == '.')
888: {
889: if(j==length-1)
890: {
891: alterMe.delete(j, j);
892: length--;
893: }
1.8 rogo 894: else
895: alterMe.setCharAt(j,'_');
1.1 rogo 896: }
897:
898: ++j;
899: }
900: return alterMe.toString();
901: }
1.4 rogo 902: public static String convertToEntities(String newName)
903: {
904: StringBuffer alterMe = new StringBuffer(newName.trim());
905: int length = alterMe.length();
906: int j = 0;
907:
908: while (j < length)
909: {
910:
911: if (alterMe.charAt(j) == '>')
912: {
913: alterMe.setCharAt(j, '&');
914: alterMe.insert(j + 1, "gt;");
915: length = length + 2;
916: if (j < length - 1)
917: j = j + 1;
918:
919: } else if (alterMe.charAt(j) == '<')
920: {
921: alterMe.setCharAt(j, '&');
922: alterMe.insert(j + 1, "lt;");
923: length = length + 2;
924: if (j < length - 1)
925: j = j + 1;
926:
927: }
928: ++j;
929: }
930: return alterMe.toString();
931: }
1.1 rogo 932: public static String convertUml(String newName)
933: {
934: StringBuffer alterMe = new StringBuffer(newName.trim());
935: int length = alterMe.length();
936: int j = 0;
937:
938:
939: while (j < length)
940: {
941:
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: else
1030: if (alterMe.charAt(j) == '%')
1031: {
1032: alterMe.setCharAt(j, '\\');
1033: alterMe.insert(j + 1, "%");
1034: length = length + 1;
1035: if(j<length-1) j=j+1;
1036: }*/
1037: ++j;
1038: }
1039: return alterMe.toString();
1040: }
1041:
1042: public static void parseXMLConfig(StringBuffer sb)
1043: {
1044: boolean finished = false;
1045: // parse string and build document tree
1.4 rogo 1046: Xparse parser =new Xparse();
1047: parser.changeEntities = true;
1048: Node root = parser.parse(sb.toString());
1.1 rogo 1049: // printContents(root);
1050: Vector databases= new Vector();
1051: Vector tables = new Vector();
1052: Vector layouts = new Vector();
1053: Vector selects = new Vector();
1054: Vector creates = new Vector();
1.10 rogo 1055: Vector ids = new Vector();
1056: int mode = -1;
1.5 rogo 1057:
1.1 rogo 1058: try
1059: {
1060: Node tempNode = root.find("convert/source", new int[] { 1, 1 });
1061: if(tempNode==null) throw new Error("parse error source tag missing");
1062: System.out.println(tempNode.name);
1063: int length =countNodes(tempNode);
1064: for (int i = 1; i <= length; i++)
1065: {
1066:
1067: DBBean database = new DBBean();
1068: tables = new Vector();
1069: layouts = new Vector();
1070: selects = new Vector();
1071: creates = new Vector();
1.10 rogo 1072: ids = new Vector();
1.1 rogo 1073: // parse dataBase
1074: Node node = root.find("convert/source/database/url", new int[] { 1, 1, i, 1 });
1075: Node node1 = root.find("convert/source/database/user", new int[] { 1, 1, i, 1, 1 });
1076: Node node2 = root.find("convert/source/database/password", new int[] { 1, 1, i, 1, 1 });
1077: Node node3 = root.find("convert/source/database", new int[] { 1, 1, i});
1.18 rogo 1078: Node nodeMode = root.find("convert/source/database/mode", new int[] {1, 1, i, 1,1});
1.5 rogo 1079:
1.1 rogo 1080: if(node3== null) throw new Error("parse error database tag missing");
1081: if(node==null) throw new Error("parse error url tag missing");
1082: if(node1==null) throw new Error("parse error user tag missing");
1083: if(node2==null) throw new Error("parse error password tag missing");
1084: String url=node.getCharacters();
1085: String user=node1.getCharacters();
1086: String password=node2.getCharacters();
1087: database.setURL(url.trim());
1088: database.setUserAndPasswd(user.trim(),password.trim());
1089: System.out.println(node.name + " " + node.getCharacters());
1090: System.out.println(node1.name + " " + node1.getCharacters());
1091: System.out.println(node2.name + " " + node2.getCharacters());
1.5 rogo 1092: String modeString = "";
1093: if (nodeMode == null)
1094: modeString = "convert";
1095: else
1096: modeString = nodeMode.getCharacters();
1097: if (modeString.equals("convert"))
1098: mode = DataBase.CONVERT_MODE;
1099: else
1100: if (modeString.equals("append"))
1101: mode = DataBase.APPEND_MODE;
1102: else
1103: if (modeString.equals("update"))
1104: mode = DataBase.UPDATE_MODE;
1.1 rogo 1105: // if(node3!=null)
1106: // System.out.println(node3.name);
1107:
1108: int length2= countNodes(node3);
1109:
1110: System.out.println("number of tables " +length2);
1111:
1112: for(int j=1;j<=length2;++j)
1113: {
1114: Node node4 = root.find("convert/source/database/table", new int[] { 1, 1, i, j });
1115: Node node5 = root.find("convert/source/database/table/select", new int[] { 1, 1, i, j, 1 });
1116: Node node6 = root.find("convert/source/database/table/create", new int[] { 1, 1, i, j, 1 });
1117: if(node4!=null)
1118: System.out.println(node4.name + " " + node4.attributes.get("layout").equals(""));
1119: if(node5!=null)
1120: System.out.println(node5.name + " " + node5.getCharacters());
1121: if(node6!=null)
1122: System.out.println(node6.name + " " + node6.getCharacters());
1123: if(node4==null) throw new Error("parse error table tag missing");
1124: // if(node5==null) throw new Error("parse error select tag missing");
1125: // if(node6==null) throw new Error("parse error create tag missing");
1126: String name = (String)node4.attributes.get("name");
1127: String layout = (String)node4.attributes.get("layout");
1.10 rogo 1128: String id = (String)node4.attributes.get("id");
1129: System.out.println("id was "+id);
1.1 rogo 1130: if(name==null) throw new Error("parse error required table tag attribute name missing");
1131: if(layout==null) layout ="";
1.10 rogo 1132: if(id==null) id = "";
1.1 rogo 1133: if(name.equals("")) throw new Error("parse error table tag attribute must not be empty");
1134: tables.add(name);
1135: layouts.add(layout);
1.10 rogo 1136: ids.add(id);
1.1 rogo 1137: String query = (node5==null) ? "":node5.getCharacters();
1138: if(query.equals("")) System.err.println("Warning empty select tag or select tag missing !!");
1139: query = (query.equals("")) ? "select * from "+database.getQC()+name+database.getQC():query;
1140: selects.add(query);
1141: if(node6!=null) creates.add(node6.getCharacters().trim());
1142: else
1143: creates.add("");
1144:
1145: }
1.10 rogo 1146: databases.add(new DataBase(database, tables, layouts, selects,creates,ids,mode));
1.1 rogo 1147: }
1148: DBBean database = new DBBean();
1149: // parse dataBase
1150: Node node = root.find("convert/destination/database/url", new int[] { 1, 1, 1, 1 });
1151: Node node1 = root.find("convert/destination/database/user", new int[] { 1, 1, 1, 1, 1 });
1152: Node node2 = root.find("convert/destination/database/password", new int[] { 1, 1, 1, 1, 1 });
1153: String url = node.getCharacters();
1154: String user = node1.getCharacters();
1155: String password = node2.getCharacters();
1156: System.out.println(url);
1157: database.setURL(url.trim());
1158: database.setUserAndPasswd(user.trim(), password.trim());
1159: //databases.add(database);
1160: for (Iterator iter = databases.iterator(); iter.hasNext();)
1161: {
1162: DataBase db = (DataBase) iter.next();
1.17 rogo 1163: if(mode!=DataBase.UPDATE_MODE)
1.9 rogo 1164: convertBatch(db.bean,database,db.tables,db.layouts,db.selects,db.creates,mode);
1.17 rogo 1165: else
1166: update(db.bean.url,database.url,db.tables,db.layouts,db.selects,db.creates,db.ids,mode);
1167:
1.1 rogo 1168: }
1169: // printContents(node3);
1170: // FM2SQL.fmInstance=new FM2SQL();
1171: } catch (Exception e)
1172: {
1173: // TODO Auto-generated catch block
1174: e.printStackTrace();
1175: }
1176: /*
1177: Node tempNode=root.find(rootNode,new int[] {1});
1178: if(tempNode==null) return rtTag;
1179: int count=1;
1180: /*
1181: for(int i=0;i<tempNode.contents.v.size();++i)
1182: {
1183: // System.out.println(((Node)tempNode.contents.v.elementAt(i)).attributes+" "+i);
1184: }*/
1185: /*
1186: if(tempNode.contents.v.size()==0) return rtTag;
1187: Node notNull=null;
1188: Node rtNode=root.find(rootNode+"/rt",new int[] {1,1});
1189: rtTag=(rtNode==null) ? "finished":rtNode.getCharacters();
1190: System.out.println("rt tag is "+ rtTag);
1191: int length=0;//(tempNode.contents.length()-1)/2;
1192: for(int i=0;i<tempNode.contents.v.size();++i) {
1193: Node node=(Node)tempNode.contents.v.elementAt(i);
1194: if(node.type.equals("element"))
1195: if(node.name.equals("doc"))length++;
1196: // System.out.println(((Node)tempNode.contents.v.elementAt(i)).attributes+" "+i);
1197: }
1198: if(length==0) rtTag="finished";
1199: while(count<=length)
1200: {
1201: //System.out.println(count+" "+length);
1202: tempNode=root.find(rootNode+"/doc",new int[] {1,count});
1203: if(tempNode==null)
1204: {
1205: rtTag="finished";
1206: break;
1207: }
1208: Bundle.Document doc = new Bundle.Document();
1209: Hashtable attributes=tempNode.attributes;
1210: int docID=0;
1211: int ver=0;
1212: if(attributes != null)
1213: {
1214: docID=Integer.parseInt((String)attributes.get("id"));
1215: ver=Integer.parseInt((String)attributes.get("ver"));
1216: doc.addData("docID",(String)attributes.get("id"));
1217: doc.addData("ver",(String)attributes.get("ver"));
1218: }
1219: bundle.docs.put(new Integer(docID),doc);
1220: bundle.documents.addElement(doc);
1221: // System.out.println("id " +docID+" ver "+ver);
1222: Node fileNode=root.find(rootNode+"/doc/file",new int[] {1,count,1});
1223: attributes=(fileNode==null)? null:fileNode.attributes;
1224: int fileID=0;
1225: int fileVer=0;
1226: if(attributes != null)
1227: {
1228: fileID=Integer.parseInt((String)attributes.get("id"));
1229: fileVer=Integer.parseInt((String)attributes.get("ver"));
1230: doc.addData("fileID",(String)attributes.get("id"));
1231: doc.addData("fileVer",(String)attributes.get("ver"));
1232: // System.out.println("fileid " +fileID+" ver "+fileVer);
1233: }
1234: // add all Document tags to actual Document
1235: for(int i=0;i<tempNode.contents.v.size();++i)
1236: {
1237: Node node=(Node)tempNode.contents.v.elementAt(i);
1238: if(node.type.equals("element"))
1239: {
1240: //System.out.println(node.name+" "+node.getCharacters()+" "+node.contents.v.size());
1241: if(node.contents.v.size()>1)
1242: {
1243: for(int j=0;j<node.contents.v.size();++j)
1244: {
1245: Node node2=(Node)node.contents.v.elementAt(j);
1246: if(node2.type.equals("element"))
1247: {
1248: doc.addData(node2.name,node2.getCharacters());
1249: //System.out.println(node2.name+" "+node2.getCharacters()+" "+node2.contents.v.size());
1250: }
1251: }
1252: } else doc.addData(node.name,node.getCharacters());
1253: }
1254: }
1255: bundle.docsLocator.put(doc.locator,doc);
1256:
1257: bundle.documents.addElement(doc);
1258: count++;
1259: } */
1260:
1261: // System.out.println(bundle.docs.size());
1262: // return rtTag;
1263: }
1264: public static Vector getXMLConfig(String xmlFile)
1265: {
1266: StringBuffer sb=null;
1267: try
1268: {
1269: // read XML Metadata from a file
1270: FileInputStream fi= new FileInputStream(xmlFile);
1271: InputStreamReader isr= new InputStreamReader(fi,"UTF-8");
1272: BufferedReader buffr = new BufferedReader(isr);
1273: sb=new StringBuffer();
1274: int c=0;
1275: while ((c = buffr.read()) != -1)
1276: {
1277: char ch=(char)c;
1278: sb.append(ch);
1279: // System.out.print((char)c);
1280: }
1281:
1282: } catch(Exception e) { e.printStackTrace();}
1283:
1284: boolean finished = false;
1285: // parse string and build document tree
1.4 rogo 1286: Xparse parser =new Xparse();
1287: parser.changeEntities = true;
1288: Node root = parser.parse(sb.toString());
1289: // printContents(root);
1.1 rogo 1290: Vector databases= new Vector();
1291: Vector tables = new Vector();
1292: Vector layouts = new Vector();
1293: Vector selects = new Vector();
1294: Vector creates = new Vector();
1.10 rogo 1295: Vector ids = new Vector();
1296:
1.5 rogo 1297: int mode = -1;
1.1 rogo 1298: try
1299: {
1300: Node tempNode = root.find("convert/source", new int[] { 1, 1 });
1301: if(tempNode==null) throw new Error("parse error source tag missing");
1302: System.out.println(tempNode.name);
1303: int length =countNodes(tempNode);
1304: for (int i = 1; i <= length; i++)
1305: {
1306:
1307: DBBean database = new DBBean();
1308: tables = new Vector();
1309: layouts = new Vector();
1310: selects = new Vector();
1311: creates = new Vector();
1.10 rogo 1312: ids = new Vector();
1.1 rogo 1313: // parse dataBase
1314: Node node = root.find("convert/source/database/url", new int[] { 1, 1, i, 1 });
1315: Node node1 = root.find("convert/source/database/user", new int[] { 1, 1, i, 1, 1 });
1316: Node node2 = root.find("convert/source/database/password", new int[] { 1, 1, i, 1, 1 });
1317: Node node3 = root.find("convert/source/database", new int[] { 1, 1, i});
1.5 rogo 1318: Node nodeMode = root.find("convert/source/database/mode", new int[] { 1, 1, i, 1, 1 });
1319:
1.1 rogo 1320: if(node3== null) throw new Error("parse error database tag missing");
1321: if(node==null) throw new Error("parse error url tag missing");
1322: if(node1==null) throw new Error("parse error user tag missing");
1323: if(node2==null) throw new Error("parse error password tag missing");
1324: String url=node.getCharacters();
1325: String user=node1.getCharacters();
1326: String password=node2.getCharacters();
1327: database.setURL(url.trim());
1328: database.setUserAndPasswd(user.trim(),password.trim());
1329: System.out.println(node.name + " " + node.getCharacters());
1330: System.out.println(node1.name + " " + node1.getCharacters());
1331: System.out.println(node2.name + " " + node2.getCharacters());
1.5 rogo 1332: String modeString = "";
1333: if (nodeMode == null)
1334: modeString = "convert";
1335: else
1336: modeString = nodeMode.getCharacters();
1337: if (modeString.equals("convert"))
1338: mode = DataBase.CONVERT_MODE;
1339: else
1340: if (modeString.equals("append"))
1341: mode = DataBase.APPEND_MODE;
1342: else
1343: if (modeString.equals("update"))
1344: mode = DataBase.UPDATE_MODE;
1345:
1346:
1.1 rogo 1347: // if(node3!=null)
1348: // System.out.println(node3.name);
1349:
1350: int length2= countNodes(node3);
1351:
1352: System.out.println("number of tables " +length2);
1353:
1354: for(int j=1;j<=length2;++j)
1355: {
1356: Node node4 = root.find("convert/source/database/table", new int[] { 1, 1, i, j });
1357: Node node5 = root.find("convert/source/database/table/select", new int[] { 1, 1, i, j, 1 });
1358: Node node6 = root.find("convert/source/database/table/create", new int[] { 1, 1, i, j, 1 });
1359: if(node4!=null)
1360: System.out.println(node4.name + " " + node4.attributes.get("layout").equals(""));
1361: if(node5!=null)
1362: System.out.println(node5.name + " " + node5.getCharacters());
1363: if(node6!=null)
1364: System.out.println(node6.name + " " + node6.getCharacters());
1365: if(node4==null) throw new Error("parse error table tag missing");
1366: // if(node5==null) throw new Error("parse error select tag missing");
1367: // if(node6==null) throw new Error("parse error create tag missing");
1368: String name = (String)node4.attributes.get("name");
1369: String layout = (String)node4.attributes.get("layout");
1.10 rogo 1370: String id = (String)node4.attributes.get("id");
1371: System.out.println("id was "+id);
1372:
1.1 rogo 1373: if(name==null) throw new Error("parse error required table tag attribute name missing");
1374: if(layout==null) layout ="";
1.10 rogo 1375: if(id == null) id="";
1.1 rogo 1376: if(name.equals("")) throw new Error("parse error table tag attribute must not be empty");
1377: tables.add(name);
1378: layouts.add(layout);
1.10 rogo 1379: ids.add(id);
1.1 rogo 1380: String query = (node5==null) ? "":node5.getCharacters();
1381: if(query.equals("")) System.err.println("Warning empty select tag or select tag missing !!");
1382: query = (query.equals("")) ? "select * from "+database.getQC()+name+database.getQC():query;
1383: selects.add(query);
1384: if(node6!=null) creates.add(node6.getCharacters().trim());
1385: else
1386: creates.add("");
1387:
1388: }
1.10 rogo 1389: databases.add(new DataBase(database, tables, layouts, selects,creates,ids,mode));
1.1 rogo 1390: }
1391: DBBean database = new DBBean();
1392: // parse dataBase
1393: Node node = root.find("convert/destination/database/url", new int[] { 1, 1, 1, 1 });
1394: Node node1 = root.find("convert/destination/database/user", new int[] { 1, 1, 1, 1, 1 });
1395: Node node2 = root.find("convert/destination/database/password", new int[] { 1, 1, 1, 1, 1 });
1396: String url = node.getCharacters();
1397: String user = node1.getCharacters();
1398: String password = node2.getCharacters();
1399: System.out.println(url);
1400: database.setURL(url.trim());
1401: database.setUserAndPasswd(user.trim(), password.trim());
1.10 rogo 1402: databases.add(new DataBase(database,null,null,null,null,null,0));
1.1 rogo 1403: //databases.add(database);
1404: /* for (Iterator iter = databases.iterator(); iter.hasNext();)
1405: {
1406: DataBase db = (DataBase) iter.next();
1407: convertBatch(db.bean,database,db.tables,db.layouts,db.selects,db.creates);
1408:
1409: }*/
1410: // printContents(node3);
1411: // FM2SQL.fmInstance=new FM2SQL();
1412: } catch (Exception e)
1413: {
1414: // TODO Auto-generated catch block
1415: e.printStackTrace();
1416: }
1417: return databases;
1418: }
1419:
1420:
1421: private static int countNodes(Node tempNode)
1422: {
1423: int length = 0;
1424: for(int i=0;i<tempNode.contents.v.size();++i) {
1425: Node node=(Node)tempNode.contents.v.elementAt(i);
1426: if(node.type.equals("element"))
1427: {
1428: if(node.name.equals("database"))length++;
1429: if(node.name.equals("table"))length++;
1430: }
1431:
1432: // System.out.println(((Node)tempNode.contents.v.elementAt(i)).attributes+" "+i);
1433: }
1434: return length;
1435: }
1436: private static void printContents(Node root)
1437: {
1438:
1439:
1440: Vector contents=(root.index==null)? root.contents.v:root.index.v;
1441: for (int i=0;i<contents.size();++i)
1442: {
1443: Node n=(Node)contents.elementAt(i);
1444: if(n.type.equals("element"))
1445: {
1446: System.out.println("tag "+n.name);
1447: System.out.println(n.getCharacters());
1448: //contents=n.contents.v i=0;
1449: }
1450: // System.out.println(n.type);
1451:
1452:
1453:
1454: }
1455: }
1456: public static void readXMLFile(String xmlFile)
1457: {
1458: try
1459: {
1460: // read XML Metadata from a file
1461: FileInputStream fi= new FileInputStream(xmlFile);
1462: InputStreamReader isr= new InputStreamReader(fi,"UTF-8");
1463: BufferedReader buffr = new BufferedReader(isr);
1464: StringBuffer sb=new StringBuffer();
1465: int c=0;
1466: while ((c = buffr.read()) != -1)
1467: {
1468: char ch=(char)c;
1469: sb.append(ch);
1470: // System.out.print((char)c);
1471: }
1472: parseXMLConfig(sb);
1473: } catch(Exception e) { e.printStackTrace();}
1474: }
1475: public static class DataBase
1476: {
1477: DBBean bean;
1478: Vector creates;
1479: Vector selects;
1480: Vector layouts;
1481: Vector tables;
1.10 rogo 1482: Vector ids;
1.5 rogo 1483: final static int CONVERT_MODE = 1;
1484: final static int APPEND_MODE = 2;
1485: final static int UPDATE_MODE = 3;
1486: int mode = -1;
1487:
1.10 rogo 1488: public DataBase(DBBean bean,Vector tables,Vector layouts,Vector selects,Vector creates,Vector ids,int mode)
1.1 rogo 1489: {
1490: this.bean = bean;
1491: this.tables = tables;
1492: this.layouts = layouts;
1493: this.selects = selects;
1494: this.creates = creates;
1.10 rogo 1495: this.ids = ids;
1.5 rogo 1496: this.mode = mode;
1.14 rogo 1497: this.bean.setIDVector(ids);
1.1 rogo 1498: }
1499: public void exportToXML(BufferedWriter buffr) throws Exception
1500: {
1.14 rogo 1501: // ids=bean.getIDVector();
1.1 rogo 1502: buffr.write(" <database>\n");
1503: buffr.write(" <url>"+bean.url+"</url>\n");
1504: buffr.write(" <user>"+bean.user+"</user>\n");
1505: buffr.write(" <password>"+bean.passwd+"</password>\n");
1.5 rogo 1506: String modeString ="";
1507: if (mode == CONVERT_MODE)
1508: modeString = "convert";
1509: else
1510: if (mode == APPEND_MODE)
1511: modeString = "append";
1512: else
1513: if (mode == UPDATE_MODE)
1514: modeString = "update";
1515:
1516: buffr.write(" <mode>"+ modeString+"</mode>\n");
1.1 rogo 1517: int index = 0;
1518: while(index<tables.size())
1519: {
1520: String table=(String)tables.get(index);
1521: String layout=(String)layouts.get(index);
1522: String select=(String)selects.get(index);
1523: String create=(String)creates.get(index);
1.10 rogo 1524: String id=(String)ids.get(index);
1.1 rogo 1525:
1.14 rogo 1526: buffr.write(" <table name = \""+table+"\" layout = \""+layout+"\" id = \""+id+"\" >\n");
1.4 rogo 1527: buffr.write(" <select>"+ convertToEntities(select)+"</select>\n");
1.1 rogo 1528: if(!create.equals(""))
1529: buffr.write(" <create>"+create+" </create>\n");
1530: buffr.write(" </table>\n");
1531: index++;
1532: }
1533: buffr.write(" </database>\n");
1534: }
1535: public String toString() {return bean.url+" "+tables;}
1536:
1537: }
1538: public static String convertToUTF8(Object command)
1539: {
1540: String str = null;
1541: try
1542: {
1543: str = new String(command.toString().getBytes("UTF-8"));
1544: } catch (UnsupportedEncodingException e)
1545: {
1546: // TODO Auto-generated catch block
1547: e.printStackTrace();
1548: }
1549: return str;
1550: }
1551: public static void writeConfig(String file, DataBase source, DataBase destination) throws Exception
1552: {
1553: if(!file.toLowerCase().endsWith(".xml")) file+=".xml";
1554: File f = new File(file);
1555:
1556: FileOutputStream fout= new FileOutputStream(f);
1557: OutputStreamWriter outsw = new OutputStreamWriter(fout,"UTF-8");
1558: BufferedWriter buffw = new BufferedWriter(outsw);
1559: buffw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
1560: buffw.newLine();
1561: buffw.write("<convert>\n");
1562: buffw.write(" <source>\n");
1563: source.exportToXML(buffw);
1564: buffw.write(" </source>\n");
1565: buffw.write("\n <destination>\n");
1566: destination.exportToXML(buffw);
1567: buffw.write(" </destination>\n");
1568: buffw.write("</convert>\n");
1569: buffw.close();
1570: }
1571: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>