Annotation of FM2SQL/Convert.java, revision 1.26
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.26 ! rogo 28: final static int numIntervalls = 10;
1.1 rogo 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);
1.26 ! rogo 543: System.out.println("ID LIST SIZE "+Math.round((double)myIds.size()/(double)numIntervalls)+" "+myIds.size());
! 544: deltaID =(int) Math.round((double)myIds.size()/(double)numIntervalls);
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.26 ! rogo 650: int endIndex = -1;
1.25 rogo 651: for(int k=0;k<vec.size()-deltaID;k=k+deltaID)
1.23 rogo 652: {
1.25 rogo 653: System.out.println(vec.get(k)+" "+vec.get(k+deltaID)+" "+vec.lastElement());
1.26 ! rogo 654: endIndex = k+deltaID;
1.23 rogo 655: }
1.26 ! rogo 656: if((vec.size()-1)%numIntervalls==0) System.out.println("fits");
! 657: else
! 658: {
! 659: System.out.println(" last intervall from "+vec.get(endIndex)+" "+vec.lastElement());
! 660: }
1.1 rogo 661: while((row=bean.getNextRow())!=null)
662: {
663: j++;
664: // row = (Vector) result[0].get(j);
1.3 rogo 665: /* command = new StringBuffer();
1.1 rogo 666:
667: command.append("INSERT INTO ");
668: command.append(beanDest.getQC());
669: command.append(convertText((String) names.get(tbIndex)));
670: command.append(beanDest.getQC());
671: command.append(" values ( ");
1.3 rogo 672: */
673: //print rows
1.1 rogo 674: Object obj = null;
1.3 rogo 675: /* for(int k=0;k<row.size()-1;++k)
1.1 rogo 676: {
677: obj = row.get(k);
678: //System.out.println("row "+obj+" "+k);
679: if(obj!=null&&!(obj instanceof ArrayList))
1.3 rogo 680: command.append("'"+convertUml(obj.toString())+"',");
1.1 rogo 681: else if(obj!=null&& obj instanceof ArrayList)
682: command.append("'"+convertUml(((ArrayList)obj).get(0).toString())+"',");
683: else command.append("NULL,");
684: }
685: obj = row.get(row.size() - 1);
686: if (obj != null && !(obj instanceof ArrayList))
1.3 rogo 687: command.append("'"+convertUml(obj.toString())+"')");
1.1 rogo 688: else
689: if(obj!=null&& obj instanceof ArrayList)
690: command.append("'"+convertUml(((ArrayList)obj).get(0).toString())+"')"); //command.append(obj.toString()+")");
691: else command.append("NULL)");
1.3 rogo 692: */
1.1 rogo 693: //command.append("'"+row.get(row.size()-1)+"')");
694: //command.append(" )");
695: // for(int k=0;k<row.size();++k)
696:
697: // System.out.println();
698: // System.out.println(command+" "+j+" "+row.size()+" "+ ((Vector)result2[0].get(j)).size());
699: // System.out.println(command);
1.3 rogo 700: for(int k=0;k<row.size();++k)
701: {
702: obj = row.get(k);
703: if(obj instanceof ArrayList) obj=((List)obj).get(0);
704: String str =(obj==null) ? "NULL":obj.toString();
705: if(!str.equals("NULL"))
706: pstm.setString(k+1,str);
707: else pstm.setNull(k+1,Types.NULL);
708: }
709: pstm.execute();
710: //stm.executeUpdate(command.toString());
1.1 rogo 711: dialog.progress.setValue((int) (((double) (j + 1) / (double) rowCount) * 100.0));
712: // System.out.println( (int)(((double)(j+1)/(double)result[0].size())*100.0)+" "+result[0].size()+" "+j);
713: command = null;
714: }// to for loop
715:
716: }
717: } catch(Exception e)
718: {
719: System.out.println("Error while connecting to database "+ e);
720: dialog.setVisible(false);
721: dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
722: FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
723: java.io.ByteArrayOutputStream b = new java.io.ByteArrayOutputStream();
724: java.io.PrintStream stream = new java.io.PrintStream(b);
725: stream.print(command+"\n\n");
726: e.printStackTrace(stream);
727: FM2SQL.showErrorDialog(b.toString(), "Error occured !");
728:
729: }
730: dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
731: FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
732:
733: dialog.setVisible(false);
734: }
735:
736: public static String convertText(String newName)
737: {
738: StringBuffer alterMe = new StringBuffer(newName.trim().toLowerCase());
739: int length = alterMe.length();
740: int j = 0;
1.3 rogo 741: int index=alterMe.indexOf(".fp5");
742: if ( index >= 0 )
743: {
744: alterMe.delete(index, index + 4);
745: length = length - 4;
746: }
747:
1.1 rogo 748: while (j < length)
749: {
750: if (alterMe.charAt(j) == ' ')
751: {
752: alterMe.setCharAt(j, '_');
753: // if(j<length-1) j=j+1;
754: }
755: else
756: if (alterMe.charAt(j) == '_')
757: {
758:
759: if(alterMe.charAt(j+1)=='_')
760: alterMe.deleteCharAt(j);
761: length = length-1;
762: // if(j<length-1) j=j+1;
763: }
764: else
765: if (alterMe.charAt(j) == 'ä')
766: {
767: alterMe.setCharAt(j, 'a');
768: alterMe.insert(j+1,"e");
769: length=length+1;
770: if(j<length-1) j=j+1;
771: }
772: else
773: if (alterMe.charAt(j) == 'ö')
774: {
775: alterMe.setCharAt(j, 'o');
776: alterMe.insert(j+1,"e");
777: length=length+1;
778: if(j<length-1) j=j+1;
779: }
780: else
781: if (alterMe.charAt(j) == 'ü')
782: {
783: alterMe.setCharAt(j, 'u');
784: alterMe.insert(j+1,"e");
785: length=length+1;
786: if(j<length-1) j=j+1;
787: }
788: else
789: if (alterMe.charAt(j) == 'ß')
790: {
791: alterMe.setCharAt(j, 's');
792: alterMe.insert(j + 1, "s");
793: length = length + 1;
794: if (j < length - 1)
795: j = j + 1;
796: } else if (alterMe.charAt(j) == ':')
797: {
798: if(j<length-1)
799: {
800: if (alterMe.charAt(j + 1) == ':')
801: {
802: alterMe.setCharAt(j,'_');
803: alterMe.delete(j+1,j+2);
804: length = length - 1;
805:
806: }
807:
808: if (j < length - 1)
809: j = j + 1;
810: }
811: } else if (alterMe.charAt(j) == '-')
812: {
813: alterMe.setCharAt(j, '_');
814:
1.16 rogo 815: } else if (alterMe.charAt(j) == '?')
816: {
817: // changed ? to _ because of update statement
818: alterMe.setCharAt(j,'_');
819: // length = length + 1;
820: // j=j+1;
821: System.out.println(alterMe);
822: }
823:
1.3 rogo 824: else if (alterMe.charAt(j) == '.')
825: {
826: if(j==length-1)
827: {
828: alterMe.delete(j, j);
829: length--;
830: }
1.8 rogo 831: else
832: alterMe.setCharAt(j,'_');
1.1 rogo 833: }
834:
835: ++j;
836: }
837: return alterMe.toString();
838: }
1.4 rogo 839: public static String convertToEntities(String newName)
840: {
841: StringBuffer alterMe = new StringBuffer(newName.trim());
842: int length = alterMe.length();
843: int j = 0;
844:
845: while (j < length)
846: {
847:
848: if (alterMe.charAt(j) == '>')
849: {
850: alterMe.setCharAt(j, '&');
851: alterMe.insert(j + 1, "gt;");
852: length = length + 2;
853: if (j < length - 1)
854: j = j + 1;
855:
856: } else if (alterMe.charAt(j) == '<')
857: {
858: alterMe.setCharAt(j, '&');
859: alterMe.insert(j + 1, "lt;");
860: length = length + 2;
861: if (j < length - 1)
862: j = j + 1;
863:
864: }
865: ++j;
866: }
867: return alterMe.toString();
868: }
1.1 rogo 869: public static String convertUml(String newName)
870: {
871: StringBuffer alterMe = new StringBuffer(newName.trim());
872: int length = alterMe.length();
873: int j = 0;
874:
875:
876: while (j < length)
877: {
878:
879: if (alterMe.charAt(j) == '\'')
880: {
881: alterMe.setCharAt(j, '\\');
882: alterMe.insert(j + 1, "'");
883: length = length + 1;
884: if(j<length-1) j=j+1;
885: }
886: /* else
887: if (alterMe.charAt(j) == '"')
888: {
889: alterMe.setCharAt(j, '\\');
890: alterMe.insert(j + 1, "\"");
891: length = length + 1;
892: if(j<length-1) j=j+1;
893: }
894: else
895: if (alterMe.charAt(j) == '>')
896: {
897: alterMe.setCharAt(j, '\\');
898: alterMe.insert(j + 1, ">");
899: length = length + 1;
900: if(j<length-1) j=j+1;
901: }
902: else
903: if (alterMe.charAt(j) == '<')
904: {
905: alterMe.setCharAt(j, '\\');
906: alterMe.insert(j + 1, "<");
907: length = length + 1;
908: if(j<length-1) j=j+1;
909: }
910: else
911: if (alterMe.charAt(j) == '?')
912: {
913: alterMe.setCharAt(j, '\\');
914: alterMe.insert(j + 1, "?");
915: length = length + 1;
916: if(j<length-1) j=j+1;
917: }
918: else
919: if (alterMe.charAt(j) == '&')
920: {
921: alterMe.setCharAt(j, '\\');
922: alterMe.insert(j + 1, "&");
923: length = length + 1;
924: if(j<length-1) j=j+1;
925: }
926: else
927: if (alterMe.charAt(j) == '=')
928: {
929: alterMe.setCharAt(j, '\\');
930: alterMe.insert(j + 1, "=");
931: length = length + 1;
932: if(j<length-1) j=j+1;
933: }
934: else
935: if (alterMe.charAt(j) == ',')
936: {
937: alterMe.setCharAt(j, '\\');
938: alterMe.insert(j + 1, ",");
939: length = length + 1;
940: if(j<length-1) j=j+1;
941: }
942: else
943: if (alterMe.charAt(j) == '.')
944: {
945: alterMe.setCharAt(j, '\\');
946: alterMe.insert(j + 1, ".");
947: length = length + 1;
948: if(j<length-1) j=j+1;
949: }
950: else
951: if (alterMe.charAt(j) == '[')
952: {
953: alterMe.setCharAt(j, '\\');
954: alterMe.insert(j + 1, ".");
955: length = length + 1;
956: if(j<length-1) j=j+1;
957: }
958: else
959: if (alterMe.charAt(j) == ']')
960: {
961: alterMe.setCharAt(j, '\\');
962: alterMe.insert(j + 1, ".");
963: length = length + 1;
964: if(j<length-1) j=j+1;
965: }
966: else
967: if (alterMe.charAt(j) == '%')
968: {
969: alterMe.setCharAt(j, '\\');
970: alterMe.insert(j + 1, "%");
971: length = length + 1;
972: if(j<length-1) j=j+1;
973: }*/
974: ++j;
975: }
976: return alterMe.toString();
977: }
978:
979: public static void parseXMLConfig(StringBuffer sb)
980: {
981: boolean finished = false;
982: // parse string and build document tree
1.4 rogo 983: Xparse parser =new Xparse();
984: parser.changeEntities = true;
985: Node root = parser.parse(sb.toString());
1.1 rogo 986: // printContents(root);
987: Vector databases= new Vector();
988: Vector tables = new Vector();
989: Vector layouts = new Vector();
990: Vector selects = new Vector();
991: Vector creates = new Vector();
1.10 rogo 992: Vector ids = new Vector();
993: int mode = -1;
1.5 rogo 994:
1.1 rogo 995: try
996: {
997: Node tempNode = root.find("convert/source", new int[] { 1, 1 });
998: if(tempNode==null) throw new Error("parse error source tag missing");
999: System.out.println(tempNode.name);
1000: int length =countNodes(tempNode);
1001: for (int i = 1; i <= length; i++)
1002: {
1003:
1004: DBBean database = new DBBean();
1005: tables = new Vector();
1006: layouts = new Vector();
1007: selects = new Vector();
1008: creates = new Vector();
1.10 rogo 1009: ids = new Vector();
1.1 rogo 1010: // parse dataBase
1011: Node node = root.find("convert/source/database/url", new int[] { 1, 1, i, 1 });
1012: Node node1 = root.find("convert/source/database/user", new int[] { 1, 1, i, 1, 1 });
1013: Node node2 = root.find("convert/source/database/password", new int[] { 1, 1, i, 1, 1 });
1014: Node node3 = root.find("convert/source/database", new int[] { 1, 1, i});
1.18 rogo 1015: Node nodeMode = root.find("convert/source/database/mode", new int[] {1, 1, i, 1,1});
1.5 rogo 1016:
1.1 rogo 1017: if(node3== null) throw new Error("parse error database tag missing");
1018: if(node==null) throw new Error("parse error url tag missing");
1019: if(node1==null) throw new Error("parse error user tag missing");
1020: if(node2==null) throw new Error("parse error password tag missing");
1021: String url=node.getCharacters();
1022: String user=node1.getCharacters();
1023: String password=node2.getCharacters();
1024: database.setURL(url.trim());
1025: database.setUserAndPasswd(user.trim(),password.trim());
1026: System.out.println(node.name + " " + node.getCharacters());
1027: System.out.println(node1.name + " " + node1.getCharacters());
1028: System.out.println(node2.name + " " + node2.getCharacters());
1.5 rogo 1029: String modeString = "";
1030: if (nodeMode == null)
1031: modeString = "convert";
1032: else
1033: modeString = nodeMode.getCharacters();
1034: if (modeString.equals("convert"))
1035: mode = DataBase.CONVERT_MODE;
1036: else
1037: if (modeString.equals("append"))
1038: mode = DataBase.APPEND_MODE;
1039: else
1040: if (modeString.equals("update"))
1041: mode = DataBase.UPDATE_MODE;
1.1 rogo 1042: // if(node3!=null)
1043: // System.out.println(node3.name);
1044:
1045: int length2= countNodes(node3);
1046:
1047: System.out.println("number of tables " +length2);
1048:
1049: for(int j=1;j<=length2;++j)
1050: {
1051: Node node4 = root.find("convert/source/database/table", new int[] { 1, 1, i, j });
1052: Node node5 = root.find("convert/source/database/table/select", new int[] { 1, 1, i, j, 1 });
1053: Node node6 = root.find("convert/source/database/table/create", new int[] { 1, 1, i, j, 1 });
1054: if(node4!=null)
1055: System.out.println(node4.name + " " + node4.attributes.get("layout").equals(""));
1056: if(node5!=null)
1057: System.out.println(node5.name + " " + node5.getCharacters());
1058: if(node6!=null)
1059: System.out.println(node6.name + " " + node6.getCharacters());
1060: if(node4==null) throw new Error("parse error table tag missing");
1061: // if(node5==null) throw new Error("parse error select tag missing");
1062: // if(node6==null) throw new Error("parse error create tag missing");
1063: String name = (String)node4.attributes.get("name");
1064: String layout = (String)node4.attributes.get("layout");
1.10 rogo 1065: String id = (String)node4.attributes.get("id");
1066: System.out.println("id was "+id);
1.1 rogo 1067: if(name==null) throw new Error("parse error required table tag attribute name missing");
1068: if(layout==null) layout ="";
1.10 rogo 1069: if(id==null) id = "";
1.1 rogo 1070: if(name.equals("")) throw new Error("parse error table tag attribute must not be empty");
1071: tables.add(name);
1072: layouts.add(layout);
1.10 rogo 1073: ids.add(id);
1.1 rogo 1074: String query = (node5==null) ? "":node5.getCharacters();
1075: if(query.equals("")) System.err.println("Warning empty select tag or select tag missing !!");
1076: query = (query.equals("")) ? "select * from "+database.getQC()+name+database.getQC():query;
1077: selects.add(query);
1078: if(node6!=null) creates.add(node6.getCharacters().trim());
1079: else
1080: creates.add("");
1081:
1082: }
1.10 rogo 1083: databases.add(new DataBase(database, tables, layouts, selects,creates,ids,mode));
1.1 rogo 1084: }
1085: DBBean database = new DBBean();
1086: // parse dataBase
1087: Node node = root.find("convert/destination/database/url", new int[] { 1, 1, 1, 1 });
1088: Node node1 = root.find("convert/destination/database/user", new int[] { 1, 1, 1, 1, 1 });
1089: Node node2 = root.find("convert/destination/database/password", new int[] { 1, 1, 1, 1, 1 });
1090: String url = node.getCharacters();
1091: String user = node1.getCharacters();
1092: String password = node2.getCharacters();
1093: System.out.println(url);
1094: database.setURL(url.trim());
1095: database.setUserAndPasswd(user.trim(), password.trim());
1096: //databases.add(database);
1097: for (Iterator iter = databases.iterator(); iter.hasNext();)
1098: {
1099: DataBase db = (DataBase) iter.next();
1.17 rogo 1100: if(mode!=DataBase.UPDATE_MODE)
1.9 rogo 1101: convertBatch(db.bean,database,db.tables,db.layouts,db.selects,db.creates,mode);
1.17 rogo 1102: else
1103: update(db.bean.url,database.url,db.tables,db.layouts,db.selects,db.creates,db.ids,mode);
1104:
1.1 rogo 1105: }
1106: // printContents(node3);
1107: // FM2SQL.fmInstance=new FM2SQL();
1108: } catch (Exception e)
1109: {
1110: // TODO Auto-generated catch block
1111: e.printStackTrace();
1112: }
1113: /*
1114: Node tempNode=root.find(rootNode,new int[] {1});
1115: if(tempNode==null) return rtTag;
1116: int count=1;
1117: /*
1118: for(int i=0;i<tempNode.contents.v.size();++i)
1119: {
1120: // System.out.println(((Node)tempNode.contents.v.elementAt(i)).attributes+" "+i);
1121: }*/
1122: /*
1123: if(tempNode.contents.v.size()==0) return rtTag;
1124: Node notNull=null;
1125: Node rtNode=root.find(rootNode+"/rt",new int[] {1,1});
1126: rtTag=(rtNode==null) ? "finished":rtNode.getCharacters();
1127: System.out.println("rt tag is "+ rtTag);
1128: int length=0;//(tempNode.contents.length()-1)/2;
1129: for(int i=0;i<tempNode.contents.v.size();++i) {
1130: Node node=(Node)tempNode.contents.v.elementAt(i);
1131: if(node.type.equals("element"))
1132: if(node.name.equals("doc"))length++;
1133: // System.out.println(((Node)tempNode.contents.v.elementAt(i)).attributes+" "+i);
1134: }
1135: if(length==0) rtTag="finished";
1136: while(count<=length)
1137: {
1138: //System.out.println(count+" "+length);
1139: tempNode=root.find(rootNode+"/doc",new int[] {1,count});
1140: if(tempNode==null)
1141: {
1142: rtTag="finished";
1143: break;
1144: }
1145: Bundle.Document doc = new Bundle.Document();
1146: Hashtable attributes=tempNode.attributes;
1147: int docID=0;
1148: int ver=0;
1149: if(attributes != null)
1150: {
1151: docID=Integer.parseInt((String)attributes.get("id"));
1152: ver=Integer.parseInt((String)attributes.get("ver"));
1153: doc.addData("docID",(String)attributes.get("id"));
1154: doc.addData("ver",(String)attributes.get("ver"));
1155: }
1156: bundle.docs.put(new Integer(docID),doc);
1157: bundle.documents.addElement(doc);
1158: // System.out.println("id " +docID+" ver "+ver);
1159: Node fileNode=root.find(rootNode+"/doc/file",new int[] {1,count,1});
1160: attributes=(fileNode==null)? null:fileNode.attributes;
1161: int fileID=0;
1162: int fileVer=0;
1163: if(attributes != null)
1164: {
1165: fileID=Integer.parseInt((String)attributes.get("id"));
1166: fileVer=Integer.parseInt((String)attributes.get("ver"));
1167: doc.addData("fileID",(String)attributes.get("id"));
1168: doc.addData("fileVer",(String)attributes.get("ver"));
1169: // System.out.println("fileid " +fileID+" ver "+fileVer);
1170: }
1171: // add all Document tags to actual Document
1172: for(int i=0;i<tempNode.contents.v.size();++i)
1173: {
1174: Node node=(Node)tempNode.contents.v.elementAt(i);
1175: if(node.type.equals("element"))
1176: {
1177: //System.out.println(node.name+" "+node.getCharacters()+" "+node.contents.v.size());
1178: if(node.contents.v.size()>1)
1179: {
1180: for(int j=0;j<node.contents.v.size();++j)
1181: {
1182: Node node2=(Node)node.contents.v.elementAt(j);
1183: if(node2.type.equals("element"))
1184: {
1185: doc.addData(node2.name,node2.getCharacters());
1186: //System.out.println(node2.name+" "+node2.getCharacters()+" "+node2.contents.v.size());
1187: }
1188: }
1189: } else doc.addData(node.name,node.getCharacters());
1190: }
1191: }
1192: bundle.docsLocator.put(doc.locator,doc);
1193:
1194: bundle.documents.addElement(doc);
1195: count++;
1196: } */
1197:
1198: // System.out.println(bundle.docs.size());
1199: // return rtTag;
1200: }
1201: public static Vector getXMLConfig(String xmlFile)
1202: {
1203: StringBuffer sb=null;
1204: try
1205: {
1206: // read XML Metadata from a file
1207: FileInputStream fi= new FileInputStream(xmlFile);
1208: InputStreamReader isr= new InputStreamReader(fi,"UTF-8");
1209: BufferedReader buffr = new BufferedReader(isr);
1210: sb=new StringBuffer();
1211: int c=0;
1212: while ((c = buffr.read()) != -1)
1213: {
1214: char ch=(char)c;
1215: sb.append(ch);
1216: // System.out.print((char)c);
1217: }
1218:
1219: } catch(Exception e) { e.printStackTrace();}
1220:
1221: boolean finished = false;
1222: // parse string and build document tree
1.4 rogo 1223: Xparse parser =new Xparse();
1224: parser.changeEntities = true;
1225: Node root = parser.parse(sb.toString());
1226: // printContents(root);
1.1 rogo 1227: Vector databases= new Vector();
1228: Vector tables = new Vector();
1229: Vector layouts = new Vector();
1230: Vector selects = new Vector();
1231: Vector creates = new Vector();
1.10 rogo 1232: Vector ids = new Vector();
1233:
1.5 rogo 1234: int mode = -1;
1.1 rogo 1235: try
1236: {
1237: Node tempNode = root.find("convert/source", new int[] { 1, 1 });
1238: if(tempNode==null) throw new Error("parse error source tag missing");
1239: System.out.println(tempNode.name);
1240: int length =countNodes(tempNode);
1241: for (int i = 1; i <= length; i++)
1242: {
1243:
1244: DBBean database = new DBBean();
1245: tables = new Vector();
1246: layouts = new Vector();
1247: selects = new Vector();
1248: creates = new Vector();
1.10 rogo 1249: ids = new Vector();
1.1 rogo 1250: // parse dataBase
1251: Node node = root.find("convert/source/database/url", new int[] { 1, 1, i, 1 });
1252: Node node1 = root.find("convert/source/database/user", new int[] { 1, 1, i, 1, 1 });
1253: Node node2 = root.find("convert/source/database/password", new int[] { 1, 1, i, 1, 1 });
1254: Node node3 = root.find("convert/source/database", new int[] { 1, 1, i});
1.5 rogo 1255: Node nodeMode = root.find("convert/source/database/mode", new int[] { 1, 1, i, 1, 1 });
1256:
1.1 rogo 1257: if(node3== null) throw new Error("parse error database tag missing");
1258: if(node==null) throw new Error("parse error url tag missing");
1259: if(node1==null) throw new Error("parse error user tag missing");
1260: if(node2==null) throw new Error("parse error password tag missing");
1261: String url=node.getCharacters();
1262: String user=node1.getCharacters();
1263: String password=node2.getCharacters();
1264: database.setURL(url.trim());
1265: database.setUserAndPasswd(user.trim(),password.trim());
1266: System.out.println(node.name + " " + node.getCharacters());
1267: System.out.println(node1.name + " " + node1.getCharacters());
1268: System.out.println(node2.name + " " + node2.getCharacters());
1.5 rogo 1269: String modeString = "";
1270: if (nodeMode == null)
1271: modeString = "convert";
1272: else
1273: modeString = nodeMode.getCharacters();
1274: if (modeString.equals("convert"))
1275: mode = DataBase.CONVERT_MODE;
1276: else
1277: if (modeString.equals("append"))
1278: mode = DataBase.APPEND_MODE;
1279: else
1280: if (modeString.equals("update"))
1281: mode = DataBase.UPDATE_MODE;
1282:
1283:
1.1 rogo 1284: // if(node3!=null)
1285: // System.out.println(node3.name);
1286:
1287: int length2= countNodes(node3);
1288:
1289: System.out.println("number of tables " +length2);
1290:
1291: for(int j=1;j<=length2;++j)
1292: {
1293: Node node4 = root.find("convert/source/database/table", new int[] { 1, 1, i, j });
1294: Node node5 = root.find("convert/source/database/table/select", new int[] { 1, 1, i, j, 1 });
1295: Node node6 = root.find("convert/source/database/table/create", new int[] { 1, 1, i, j, 1 });
1296: if(node4!=null)
1297: System.out.println(node4.name + " " + node4.attributes.get("layout").equals(""));
1298: if(node5!=null)
1299: System.out.println(node5.name + " " + node5.getCharacters());
1300: if(node6!=null)
1301: System.out.println(node6.name + " " + node6.getCharacters());
1302: if(node4==null) throw new Error("parse error table tag missing");
1303: // if(node5==null) throw new Error("parse error select tag missing");
1304: // if(node6==null) throw new Error("parse error create tag missing");
1305: String name = (String)node4.attributes.get("name");
1306: String layout = (String)node4.attributes.get("layout");
1.10 rogo 1307: String id = (String)node4.attributes.get("id");
1308: System.out.println("id was "+id);
1309:
1.1 rogo 1310: if(name==null) throw new Error("parse error required table tag attribute name missing");
1311: if(layout==null) layout ="";
1.10 rogo 1312: if(id == null) id="";
1.1 rogo 1313: if(name.equals("")) throw new Error("parse error table tag attribute must not be empty");
1314: tables.add(name);
1315: layouts.add(layout);
1.10 rogo 1316: ids.add(id);
1.1 rogo 1317: String query = (node5==null) ? "":node5.getCharacters();
1318: if(query.equals("")) System.err.println("Warning empty select tag or select tag missing !!");
1319: query = (query.equals("")) ? "select * from "+database.getQC()+name+database.getQC():query;
1320: selects.add(query);
1321: if(node6!=null) creates.add(node6.getCharacters().trim());
1322: else
1323: creates.add("");
1324:
1325: }
1.10 rogo 1326: databases.add(new DataBase(database, tables, layouts, selects,creates,ids,mode));
1.1 rogo 1327: }
1328: DBBean database = new DBBean();
1329: // parse dataBase
1330: Node node = root.find("convert/destination/database/url", new int[] { 1, 1, 1, 1 });
1331: Node node1 = root.find("convert/destination/database/user", new int[] { 1, 1, 1, 1, 1 });
1332: Node node2 = root.find("convert/destination/database/password", new int[] { 1, 1, 1, 1, 1 });
1333: String url = node.getCharacters();
1334: String user = node1.getCharacters();
1335: String password = node2.getCharacters();
1336: System.out.println(url);
1337: database.setURL(url.trim());
1338: database.setUserAndPasswd(user.trim(), password.trim());
1.10 rogo 1339: databases.add(new DataBase(database,null,null,null,null,null,0));
1.1 rogo 1340: //databases.add(database);
1341: /* for (Iterator iter = databases.iterator(); iter.hasNext();)
1342: {
1343: DataBase db = (DataBase) iter.next();
1344: convertBatch(db.bean,database,db.tables,db.layouts,db.selects,db.creates);
1345:
1346: }*/
1347: // printContents(node3);
1348: // FM2SQL.fmInstance=new FM2SQL();
1349: } catch (Exception e)
1350: {
1351: // TODO Auto-generated catch block
1352: e.printStackTrace();
1353: }
1354: return databases;
1355: }
1356:
1357:
1358: private static int countNodes(Node tempNode)
1359: {
1360: int length = 0;
1361: for(int i=0;i<tempNode.contents.v.size();++i) {
1362: Node node=(Node)tempNode.contents.v.elementAt(i);
1363: if(node.type.equals("element"))
1364: {
1365: if(node.name.equals("database"))length++;
1366: if(node.name.equals("table"))length++;
1367: }
1368:
1369: // System.out.println(((Node)tempNode.contents.v.elementAt(i)).attributes+" "+i);
1370: }
1371: return length;
1372: }
1373: private static void printContents(Node root)
1374: {
1375:
1376:
1377: Vector contents=(root.index==null)? root.contents.v:root.index.v;
1378: for (int i=0;i<contents.size();++i)
1379: {
1380: Node n=(Node)contents.elementAt(i);
1381: if(n.type.equals("element"))
1382: {
1383: System.out.println("tag "+n.name);
1384: System.out.println(n.getCharacters());
1385: //contents=n.contents.v i=0;
1386: }
1387: // System.out.println(n.type);
1388:
1389:
1390:
1391: }
1392: }
1393: public static void readXMLFile(String xmlFile)
1394: {
1395: try
1396: {
1397: // read XML Metadata from a file
1398: FileInputStream fi= new FileInputStream(xmlFile);
1399: InputStreamReader isr= new InputStreamReader(fi,"UTF-8");
1400: BufferedReader buffr = new BufferedReader(isr);
1401: StringBuffer sb=new StringBuffer();
1402: int c=0;
1403: while ((c = buffr.read()) != -1)
1404: {
1405: char ch=(char)c;
1406: sb.append(ch);
1407: // System.out.print((char)c);
1408: }
1409: parseXMLConfig(sb);
1410: } catch(Exception e) { e.printStackTrace();}
1411: }
1412: public static class DataBase
1413: {
1414: DBBean bean;
1415: Vector creates;
1416: Vector selects;
1417: Vector layouts;
1418: Vector tables;
1.10 rogo 1419: Vector ids;
1.5 rogo 1420: final static int CONVERT_MODE = 1;
1421: final static int APPEND_MODE = 2;
1422: final static int UPDATE_MODE = 3;
1423: int mode = -1;
1424:
1.10 rogo 1425: public DataBase(DBBean bean,Vector tables,Vector layouts,Vector selects,Vector creates,Vector ids,int mode)
1.1 rogo 1426: {
1427: this.bean = bean;
1428: this.tables = tables;
1429: this.layouts = layouts;
1430: this.selects = selects;
1431: this.creates = creates;
1.10 rogo 1432: this.ids = ids;
1.5 rogo 1433: this.mode = mode;
1.14 rogo 1434: this.bean.setIDVector(ids);
1.1 rogo 1435: }
1436: public void exportToXML(BufferedWriter buffr) throws Exception
1437: {
1.14 rogo 1438: // ids=bean.getIDVector();
1.1 rogo 1439: buffr.write(" <database>\n");
1440: buffr.write(" <url>"+bean.url+"</url>\n");
1441: buffr.write(" <user>"+bean.user+"</user>\n");
1442: buffr.write(" <password>"+bean.passwd+"</password>\n");
1.5 rogo 1443: String modeString ="";
1444: if (mode == CONVERT_MODE)
1445: modeString = "convert";
1446: else
1447: if (mode == APPEND_MODE)
1448: modeString = "append";
1449: else
1450: if (mode == UPDATE_MODE)
1451: modeString = "update";
1452:
1453: buffr.write(" <mode>"+ modeString+"</mode>\n");
1.1 rogo 1454: int index = 0;
1455: while(index<tables.size())
1456: {
1457: String table=(String)tables.get(index);
1458: String layout=(String)layouts.get(index);
1459: String select=(String)selects.get(index);
1460: String create=(String)creates.get(index);
1.10 rogo 1461: String id=(String)ids.get(index);
1.1 rogo 1462:
1.14 rogo 1463: buffr.write(" <table name = \""+table+"\" layout = \""+layout+"\" id = \""+id+"\" >\n");
1.4 rogo 1464: buffr.write(" <select>"+ convertToEntities(select)+"</select>\n");
1.1 rogo 1465: if(!create.equals(""))
1466: buffr.write(" <create>"+create+" </create>\n");
1467: buffr.write(" </table>\n");
1468: index++;
1469: }
1470: buffr.write(" </database>\n");
1471: }
1472: public String toString() {return bean.url+" "+tables;}
1473:
1474: }
1475: public static String convertToUTF8(Object command)
1476: {
1477: String str = null;
1478: try
1479: {
1480: str = new String(command.toString().getBytes("UTF-8"));
1481: } catch (UnsupportedEncodingException e)
1482: {
1483: // TODO Auto-generated catch block
1484: e.printStackTrace();
1485: }
1486: return str;
1487: }
1488: public static void writeConfig(String file, DataBase source, DataBase destination) throws Exception
1489: {
1490: if(!file.toLowerCase().endsWith(".xml")) file+=".xml";
1491: File f = new File(file);
1492:
1493: FileOutputStream fout= new FileOutputStream(f);
1494: OutputStreamWriter outsw = new OutputStreamWriter(fout,"UTF-8");
1495: BufferedWriter buffw = new BufferedWriter(outsw);
1496: buffw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
1497: buffw.newLine();
1498: buffw.write("<convert>\n");
1499: buffw.write(" <source>\n");
1500: source.exportToXML(buffw);
1501: buffw.write(" </source>\n");
1502: buffw.write("\n <destination>\n");
1503: destination.exportToXML(buffw);
1504: buffw.write(" </destination>\n");
1505: buffw.write("</convert>\n");
1506: buffw.close();
1507: }
1508: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>