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