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