1: /*
2: * Convert.java -- Converter class - Filemaker to SQL Converter Copyright (C)
3: * 2003 Robert Gordesch (rogo@mpiwg-berlin.mpg.de) This program is free
4: * software; you can redistribute it and/or modify it under the terms of the GNU
5: * General Public License as published by the Free Software Foundation; either
6: * version 2 of the License, or (at your option) any later version. Please read
7: * license.txt for the full details. A copy of the GPL may be found at
8: * http://www.gnu.org/copyleft/lgpl.html You should have received a copy of the
9: * GNU General Public License along with this program; if not, write to the Free
10: * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
11: * USA Created on 15.09.2003 by rogo
12: */
13:
14: import java.awt.Cursor;
15: import java.io.BufferedReader;
16: import java.io.BufferedWriter;
17: import java.io.File;
18: import java.io.FileInputStream;
19: import java.io.FileNotFoundException;
20: import java.io.FileOutputStream;
21: import java.io.IOException;
22: import java.io.InputStream;
23: import java.io.InputStreamReader;
24: import java.io.OutputStreamWriter;
25: import java.io.PrintStream;
26: import java.net.URL;
27: import java.sql.PreparedStatement;
28: import java.sql.ResultSet;
29: import java.sql.SQLException;
30: import java.sql.Statement;
31: import java.sql.Types;
32: import java.text.DateFormat;
33: import java.text.ParseException;
34: import java.util.ArrayList;
35: import java.util.Date;
36: import java.util.Hashtable;
37: import java.util.Iterator;
38: import java.util.List;
39: import java.util.StringTokenizer;
40: import java.util.TreeSet;
41: import java.util.Vector;
42:
43: import javax.swing.JDialog;
44: import javax.swing.JLabel;
45: import javax.swing.JPanel;
46:
47: import com.exploringxml.xml.Node;
48: import com.exploringxml.xml.Xparse;
49:
50: class Convert
51: {
52: private static final String _TEMP = "_temp";
53:
54: /**
55: * Helper class for index creation
56: *
57: * @author rogo
58: *
59: */
60: public static class IndexList extends Vector
61: {
62: public String toString()
63: {
64: StringBuffer buff = new StringBuffer(1000);
65: int count = 0;
66: for (Iterator iter = this.iterator(); iter.hasNext();)
67: {
68: String element = (String) iter.next();
69: if (count < elementCount - 1)
70: {
71: buff.append(element).append(", ");
72: count++;
73: } else
74: buff.append(element);
75:
76: }
77:
78: return buff.toString();
79: }
80: }
81:
82: /**
83: * Helper class for pre/or post execution of SQLCommands
84: * @author rogo
85: *
86: */
87: public static class SQLCommand
88: {
89: private DBBean bean;
90: private String command;
91:
92: public SQLCommand(DBBean bean, String command)
93: {
94: this.bean = bean;
95: this.command = command;
96: }
97:
98: public void executeCommand() throws SQLException, Exception
99: {
100: System.out.println("Executing command: \n");
101: System.out.println(command);
102:
103: java.sql.Connection con = bean.getConnection();
104: Statement stm = con.createStatement();
105: stm.execute(command);
106: stm.close();
107: }
108: }
109:
110: static DBBean bean = new DBBean();
111:
112: static DBBean beanDest = new DBBean();
113:
114: static String user = "", passwd = "e1nste1n";
115:
116: static String userDest = "postgres", passwdDest = "rogo";
117:
118: static boolean batchRun = false;
119:
120: static Vector databases = new Vector();
121:
122: final static int numHits = 5000;
123:
124: final static int numIntervalls = 4;
125:
126: static boolean debug = false;
127:
128: static boolean isGUI = true;
129:
130: /**
131: * Vector for all SQLCommands to executed before any conversion action is performed
132: */
133: static Vector preSQLCommands = new Vector();
134: /**
135: * Vector for all SQLCommands to executed after any conversion action has been performed
136: */
137:
138: static Vector postSQLCommands = new Vector();
139:
140: static final String versionID = new String("FM2SQL Version 0.9.8b\n");
141:
142: private static boolean noError = false;
143:
144: public static void main(String args[]) throws IOException
145: {
146: // DateFormat d = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT,
147: // Locale.GERMANY);
148: // System.out.println(convertText("rogoistSuper" + "_" + d.format(new Date()).replace(':', '_')));
149:
150: /*
151: * try { //byte[] b = "�".getBytes("UTF-8"); //
152: * System.out.println("QueryString " +b[0]+" "+b[1]+(new
153: * String(b).getBytes()[0])+" "+new String(b).getBytes()[1]);
154: * //System.out.println(new String(b,"UTF-8")); } catch
155: * (UnsupportedEncodingException e) { e.printStackTrace(); }
156: */
157: File tmpPath = new File(System.getProperty("java.io.tmpdir"));
158: isGUI = false;
159: FileOutputStream file = null;
160: if (args.length != 1)
161: {
162: System.out.println(versionID);
163: System.out.println("Usage: java Convert <xml config file>");
164: System.exit(-1);
165: }
166: File temp = null;
167: try
168: {
169: temp = new File(tmpPath, "fm2sql.txt");
170: int count = 1;
171: while (temp.exists())
172: {
173: temp = new File(tmpPath, "fm2sql" + generateSuffix(count++) + ".txt");
174: }
175: file = new FileOutputStream(temp);
176: } catch (FileNotFoundException e1)
177: {
178: e1.printStackTrace();
179: }
180: PrintStream stream = new PrintStream(file, true);
181: // write info for user to stdout
182: System.out.println(versionID);
183: System.out.println("Loading " + args[0] + "....");
184: System.out.println("Log will be written to " + temp.getCanonicalPath());
185:
186: if (!debug)
187: {
188: System.setOut(stream);
189: System.setErr(stream);
190: }
191: System.out.println(versionID);
192: System.out.println("Using config file : " + args[0] + "....");
193:
194: StringBuffer sb = readXMLFile(args[0]);
195: parseXMLConfig(sb);
196: if (!(new File(args[0]).exists()))
197: {
198:
199: System.exit(0);
200: }
201: System.out.println("Finished!");
202: // convert("jdbc:fmpro:http://141.14.237.74:8050","jdbc:postgresql://erebos/test",null,null);
203: }
204:
205: public static void convertBatch(DBBean source, DBBean destination, Vector names, Vector layouts,
206: Vector selects, Vector creates, Vector ids, int mode, String delimiter) throws Exception
207: {
208: bean = source;
209: beanDest = destination;
210: convert(null, null, names, layouts, selects, creates, ids, mode, delimiter);
211: }
212:
213: public static String formatFileMakerArray(List list, String delimiter)
214: {
215: StringBuffer formattedString = new StringBuffer();
216: for (int i = 0; i < list.size(); ++i)
217: {
218: formattedString.append(list.get(i).toString());
219: if (i < list.size() - 1)
220: formattedString.append(delimiter);
221: }
222: return formattedString.toString();
223: }
224:
225: /**
226: * Method for SQL UPDATE
227: *
228: * @param source
229: * @param destination
230: * @param names
231: * @param layouts
232: * @param selects
233: * @param creates
234: * @param ids
235: * @param mode
236: * @throws Exception
237: */
238: public static void update(String source, String destination, Vector names, Vector layouts,
239: Vector selects, Vector creates, Vector ids, int mode) throws Exception
240: {
241:
242: FM2SQL.ProgressDialog dialog = null;
243: if (isGUI)
244: {
245: dialog = initDialog();
246: }
247: // setting user and passwd
248: bean.setUserAndPasswd(user, passwd);
249: // setting user and passwd
250: beanDest.setUserAndPasswd(userDest, passwdDest);
251:
252: StringBuffer command = null;
253: String query = null;
254: try
255: {
256:
257: bean.setConnection(source);
258:
259: if (names == null)
260: names = bean.getTableNames();
261: // Collections.sort(names);
262: int tbIndex = 1;
263:
264: System.out.println("Start at table " + names.firstElement());
265: for (tbIndex = 0; tbIndex < names.size(); ++tbIndex)
266: {
267: Vector[] result = null;
268: String destTableName = "";
269: try
270: {
271: query = "select * from " + bean.getQC() + names.get(tbIndex).toString() + bean.getQC();
272: String layout = (layouts.isEmpty()) ? "" : layouts.get(tbIndex).toString();
273: query = (selects != null) ? selects.get(tbIndex).toString() : query;
274: // if vectors[1].get(i) != null)
275: if (!layout.equals(""))
276: {
277:
278: query = addLayoutToQuery(names, query, tbIndex, layout);
279:
280: }
281: if (dialog != null)
282: {
283: prepareDialogforUse(names, dialog, tbIndex);
284: }
285: bean.getConnection();
286: bean.makeQuery(query, 0);
287:
288: } catch (Exception e)
289: {
290: System.out.println("Warning exception occured \n " + e);
291:
292: continue;
293: }
294: // determine destTableName from createStatement or from source
295: // table name
296: if (!creates.get(tbIndex).equals(""))
297: {
298: String create = creates.get(tbIndex).toString().toLowerCase();
299: int fromIndex = create.indexOf("table") + 5;
300: int toIndex = create.indexOf("(");
301: destTableName = create.substring(fromIndex, toIndex).replaceAll(beanDest.getQC(), "")
302: .trim();
303: System.out.println("destTable " + destTableName);
304:
305: } else
306: destTableName = convertText(names.get(tbIndex).toString());
307:
308: beanDest.setConnection(destination);
309:
310: Statement stm = beanDest.getConnection().createStatement();
311:
312: Vector tables = beanDest.getTableNames();
313:
314: System.out.println(names.get(tbIndex) + " "
315: + tables.indexOf(convertText((String) names.get(tbIndex)))); // "//beanDest.getTypeNames());
316: tables = beanDest.getTableNames();
317: stm = beanDest.getConnection().createStatement();
318:
319: if (dialog != null)
320: dialog.title.setText("Updating table data ...");
321: else
322: System.out.println("Updating table data ...");
323: int j = -1;
324:
325: Vector row = null;
326: command = new StringBuffer();
327:
328: command.append("UPDATE ");
329: command.append(beanDest.getQC());
330: command.append(destTableName);
331: // command.append(convertText((String) names.get(tbIndex)));
332: command.append(beanDest.getQC());
333: command.append(" SET ");
334:
335: int size = bean.getColumnNames().size();
336: for (int i = 0; i < size - 1; ++i)
337: command.append(beanDest.getQC() + convertText((String) bean.getColumnNames().get(i))
338: + beanDest.getQC() + " = ? ,");
339: command.append(convertText((String) bean.getColumnNames().get(size - 1)) + " = ? ");
340: command.append("WHERE " + convertText(ids.get(tbIndex).toString()) + " = ?");
341: PreparedStatement pstm = beanDest.getConnection().prepareStatement(command.toString());
342: System.out.println(command + " " + tbIndex);
343: int rowCount = bean.getRowCount(query);
344: int idIndex = bean.getColumnNames().indexOf(ids.get(tbIndex));
345: while ((row = bean.getNextRow()) != null)
346: {
347: j++;
348: // print rows
349: Object obj = null;
350: for (int k = 0; k < row.size(); ++k)
351: {
352: obj = row.get(k);
353: if (obj instanceof ArrayList)
354: obj = ((List) obj).get(0);
355: String str = (obj == null) ? "NULL" : obj.toString();
356: if (!str.equals("NULL"))
357: pstm.setString(k + 1, str);
358: else
359: pstm.setNull(k + 1, Types.NULL);
360: }
361: pstm.setString(row.size() + 1, row.get(idIndex).toString());
362: pstm.execute();
363: if (dialog != null)
364: dialog.progress.setValue((int) (((double) (j + 1) / (double) rowCount) * 100.0));
365: command = null;
366: } // to for loop
367:
368: }
369: } catch (Exception e)
370: {
371: System.out.println("Error while connecting to database " + e);
372: if (isGUI)
373: {
374: showExceptionDialog(dialog, command, e);
375: } else
376: {
377: e.printStackTrace();
378:
379: }
380: } finally
381: {
382: if (isGUI)
383: {
384: resetGUI(dialog);
385: }
386: }
387: }
388:
389: /**
390: * @param dialog
391: */
392: private static void resetGUI(FM2SQL.ProgressDialog dialog)
393: {
394: dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
395: FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
396: dialog.setVisible(false);
397: }
398:
399: /**
400: * @param dialog
401: * @param command
402: * @param e
403: */
404: private static void showExceptionDialog(FM2SQL.ProgressDialog dialog, StringBuffer command,
405: Exception e)
406: {
407: dialog.setVisible(false);
408: dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
409: FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
410: java.io.ByteArrayOutputStream b = new java.io.ByteArrayOutputStream();
411: java.io.PrintStream stream = new java.io.PrintStream(b);
412: stream.print(command + "\n\n");
413: e.printStackTrace(stream);
414: FM2SQL.showErrorDialog(b.toString(), "Error occured !");
415: }
416:
417: /**
418: * @return
419: */
420: private static FM2SQL.ProgressDialog initDialog()
421: {
422: FM2SQL.ProgressDialog dialog;
423: dialog = new FM2SQL.ProgressDialog(FM2SQL.fmInstance, bean);
424: dialog.setTitle("Conversion running ...");
425: dialog.title.setText("Getting table data ...");
426: dialog.setLocation(FM2SQL.fmInstance.getLocationOnScreen().x
427: + (FM2SQL.fmInstance.getWidth() - 400) / 2, FM2SQL.fmInstance.getLocationOnScreen().y
428: + (FM2SQL.fmInstance.getHeight() - 250) / 2);
429: dialog.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
430: FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
431: dialog.thread = Thread.currentThread();
432: dialog.setSize(420, 250);
433: return dialog;
434: }
435:
436: /**
437: * transfers the specified array of tables to the destination database and
438: * creates the table if it does not exist if it exists and mode is not append
439: * the table is dropped
440: *
441: * @param source
442: * @param destination
443: * @param names
444: * @param layouts
445: * @param selects
446: * @param creates
447: * @param ids
448: * @param mode
449: * @throws Exception
450: */
451:
452: public static void convert(String source, String destination, Vector names, Vector layouts,
453: Vector selects, Vector creates, Vector ids, int mode, String delimiter) throws Exception
454: {
455: // TODO implement convert_temp mode!!!
456: FM2SQL.ProgressDialog dialog = null;
457:
458: if (isGUI)
459: {
460: dialog = initDialog();
461: }
462:
463: System.out.println("connection established " + source + " " + bean.url);
464:
465: java.util.TreeSet myIds = new TreeSet();
466: int deltaID = 1;
467: String idField = "";
468: String destTableName = "";
469: String[] fieldNames = null;
470:
471: if (source != null && destination != null)
472: {
473: // setting user and passwd
474: bean.setUserAndPasswd(user, passwd);
475: // setting user and passwd
476: beanDest.setUserAndPasswd(userDest, passwdDest);
477: }
478:
479: StringBuffer command = null;
480: String query = null;
481: try
482: {
483:
484: if (source != null)
485: bean.setConnection(source);
486: else
487: bean.setConnection(bean.url);
488: if (names == null)
489: names = bean.getTableNames();
490: int tbIndex = 1;
491:
492: for (tbIndex = 0; tbIndex < names.size(); ++tbIndex)
493: {
494: Vector[] result = null;
495: try
496: {
497: String layout = (layouts.isEmpty()) ? "" : layouts.get(tbIndex).toString();
498: query = (selects != null) ? selects.get(tbIndex).toString() : "select * from "
499: + bean.getQC() + names.get(tbIndex).toString() + bean.getQC();
500: if (layout.intern() != "")
501: {
502: query = addLayoutToQuery(names, query, tbIndex, layout);
503:
504: }
505: if (dialog != null)
506: {
507: prepareDialogforUse(names, dialog, tbIndex);
508: }
509:
510: bean.getConnection();
511: bean.makeQuery(query, 50);
512: idField = ids.get(tbIndex).toString();
513:
514: } catch (Exception e)
515: {
516: System.out.println("Warning exception occured \n " + e);
517:
518: continue;
519: }
520: if (destination != null)
521: beanDest.setConnection(destination);
522: else
523: beanDest.setConnection(beanDest.url);
524: Statement stm = beanDest.getConnection().createStatement();
525:
526: Vector tables = beanDest.getTableNames();
527: // Collections.sort(tables);
528: System.out.println(names.get(tbIndex) + " "
529: + tables.indexOf(convertText((String) names.get(tbIndex)))); // "//beanDest.getTypeNames());
530: tables = beanDest.getTableNames();
531: // System.out.println(beanDest.getTableNames(beanDest.getCatalogs().get(2).toString()));
532: stm = beanDest.getConnection().createStatement();
533: // System.exit(0);
534:
535: // determine destTableName from createStatement or from source
536: // table name
537: ConversionProperties prop = getFieldNamesAndDestTableName(creates.get(tbIndex).toString(),
538: query, names.get(tbIndex).toString());
539: destTableName = prop.destTableName;
540: if (mode == Convert.DataBase.CONVERT_TEMP_MODE)
541: {
542: destTableName += _TEMP;
543: }
544: fieldNames = prop.fieldNames;
545: if (mode == Convert.DataBase.CONVERT_MODE || mode == Convert.DataBase.CONVERT_TEMP_MODE)
546: {
547:
548: if (tables.indexOf(destTableName) >= 0)
549: {
550: stm.executeUpdate("drop table " + beanDest.getQC() + destTableName + beanDest.getQC());
551: tables.remove(destTableName);
552: System.out.println("dropped table" + destTableName);
553: }
554: if ((tables.indexOf(destTableName) < 0))
555: {
556:
557: if (creates.get(tbIndex).equals("")
558: || creates.get(tbIndex).toString().toLowerCase().indexOf("create") < 0)
559: {
560: System.out
561: .println("Warning empty or invalid create statement - creating one for you\n");
562:
563: command = new StringBuffer(50);
564: command.append("CREATE TABLE ");
565: command.append(beanDest.getQC());
566: command.append(destTableName);//convertText((String) names.get(tbIndex)));
567: command.append(beanDest.getQC());
568: command.append("(");
569: String type = null;
570: Vector columnNames = bean.getColumnNames();
571: for (int i = 0; i < columnNames.size() - 1; ++i)
572: {
573: type = bean.metaData.getColumnTypeName(i + 1);
574: // System.out.println(i+" "+result[1].get(i)+"
575: // "+type);
576: type = (type.equals("NUMBER")) ? "INT4" : type;
577: type = (type.equals("CONTAINER")) ? "TEXT" : type;
578:
579: command.append(beanDest.getQC() + convertText((String) columnNames.get(i))
580: + beanDest.getQC() + " " + type + ", ");
581: }
582: type = bean.metaData.getColumnTypeName(columnNames.size());
583: type = (type.equals("NUMBER")) ? "INT4" : type;
584: type = (type.equals("CONTAINER")) ? "TEXT" : type;
585: command.append(beanDest.getQC()
586: + convertText((String) columnNames.get(columnNames.size() - 1))
587: + beanDest.getQC() + " " + type);
588: command.append(" )");
589:
590: } else
591: command = new StringBuffer().append(creates.get(tbIndex).toString().toLowerCase());
592: stm.executeUpdate(command.toString());
593:
594: }
595: }
596: if (dialog != null)
597: dialog.title.setText("Writing table data ...");
598:
599: // prepare the insert statement
600: int j = -1;
601: Vector row = null;
602: command = new StringBuffer();
603:
604: command.append("INSERT INTO ");
605: command.append(beanDest.getQC());
606: command.append(destTableName);
607: command.append(beanDest.getQC());
608: command.append(" (");
609: for (int i = 0; i < fieldNames.length; i++)
610: {
611: command.append(fieldNames[i]);
612: if (i < fieldNames.length - 1)
613: command.append(",");
614: }
615: command.append(") ");
616:
617: command.append(" values ( ");
618: // add a question marks for every field
619: for (int i = 0; i < bean.getColumnNames().size() - 1; ++i)
620: command.append("?,");
621: command.append("?)");
622: PreparedStatement pstm = beanDest.getConnection().prepareStatement(command.toString());
623: System.out.println(command);
624: int rowCount = (idField != "") ? myIds.size() : bean.getRowCount(query);
625: Vector vec = new Vector(myIds);
626: int endIndex = -1;
627: String tempQuery = query;
628: String tempID = bean.getQC() + idField + bean.getQC();
629: // if id_field not do incremental conversion else do it all at
630: // once
631: if (!idField.equals(""))
632: {
633: long startTime = System.currentTimeMillis();
634: int counter = -1;
635: while (true)
636: {
637: ++counter;
638: if (counter == 0 && dialog != null)
639: dialog.title.setText("Check if data is available");
640: else if (dialog != null)
641: dialog.title.setText("Check if more data is available");
642: myIds = bean.getIDVector(ids.get(tbIndex).toString(), (String) names.get(tbIndex),
643: tempQuery, numHits);
644: if (myIds.isEmpty())
645: break;
646: vec = new Vector(myIds);
647: rowCount = vec.size();
648: System.out.println("ID LIST SIZE "
649: + Math.round((double) myIds.size() / (double) numIntervalls) + " " + myIds.size());
650: deltaID = (int) Math.round((double) myIds.size() / (double) numIntervalls);
651: if (vec.size() <= numIntervalls)
652: {
653: endIndex = 0;
654: deltaID = vec.size();
655: }
656: for (int k = 0; k < vec.size() - deltaID; k = k + deltaID)
657: {
658: System.out.println(vec.get(k) + " " + vec.get(k + deltaID) + " " + vec.lastElement());
659: if (query.toLowerCase().indexOf("where") > 0)
660: tempQuery = query + " and " + tempID + ">='" + vec.get(k) + "' and " + tempID
661: + "<='" + vec.get(k + deltaID) + "'";
662: else
663: tempQuery = query + " where " + tempID + ">='" + vec.get(k) + "' and " + tempID
664: + "<='" + vec.get(k + deltaID) + "'";
665: System.out.println(tempQuery);
666: if (dialog != null)
667: dialog.title.setText("Reading table data ...");
668:
669: bean.makeQuery(tempQuery, deltaID);
670: if (dialog != null)
671: dialog.title.setText("Writing table data ...");
672:
673: command = writeDatainDestTable(dialog, command, k, pstm, rowCount, delimiter);
674: endIndex = k + deltaID;
675: }
676: System.out.println(endIndex);
677: // all data written ? if not write last chunk of data
678: if (endIndex == vec.size() - 1)
679: System.out.println("fits");
680: else
681: {
682: System.out.println(" last intervall from " + vec.get(endIndex) + " "
683: + vec.lastElement());
684:
685: if (query.toLowerCase().indexOf("where") > 0)
686: tempQuery = query + " and " + tempID + ">='" + vec.get(endIndex) + "' and "
687: + tempID + "<='" + vec.lastElement() + "'";
688: else
689: tempQuery = query + " where " + tempID + ">='" + vec.get(endIndex) + "' and "
690: + tempID + "<='" + vec.lastElement() + "'";
691: System.out.println(tempQuery);
692: if (dialog != null)
693: dialog.title.setText("Reading table data ...");
694: bean.makeQuery(tempQuery, 0);
695: if (dialog != null)
696: dialog.title.setText("Writing table data ...");
697: command = writeDatainDestTable(dialog, command, endIndex, pstm, rowCount, delimiter);
698: }
699: // prepare new query for next chunk
700: if (query.toLowerCase().indexOf("where") > 0)
701: tempQuery = query + " and " + tempID + ">'" + vec.lastElement() + "'";
702: else
703: tempQuery = query + " where " + tempID + ">'" + vec.lastElement() + "'";
704:
705: }
706: long endTime = System.currentTimeMillis();
707: System.out.println("Time for incremental convert elapsed " + (endTime - startTime));
708: } else
709: {
710: // read and write all in one big chunk
711: long startTime = System.currentTimeMillis();
712:
713: bean.makeQuery(query, 0);
714: System.err.println("query for whole table done");
715: command = writeDatainDestTable(dialog, command, j, pstm, rowCount, delimiter);
716: long endTime = System.currentTimeMillis();
717: System.out.println("Time for old convert elapsed " + (endTime - startTime));
718:
719: }
720: if (isGUI)
721: resetGUI(dialog);
722: noError = true;
723: if (mode == Convert.DataBase.CONVERT_TEMP_MODE)
724: {
725: String originalName = destTableName.split(_TEMP)[0];
726: // TODO
727: if (beanDest.getTableNames().contains(originalName))
728: {
729: DateFormat d = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM);
730: d.setTimeZone(java.util.TimeZone.getTimeZone("CET"));
731:
732: String date = d.format(new Date());
733: date = date.replace(':', '_');
734: String newName = addQuotes(beanDest, convertText(originalName + "_" + date));
735: // rename table to newName
736: renameTable(beanDest, addQuotes(beanDest, originalName), newName);
737: }
738: // rename Table_temp to originalName
739: renameTable(beanDest, addQuotes(beanDest, destTableName), addQuotes(beanDest,
740: originalName));
741: }
742: }
743: } catch (Exception e)
744: {
745: System.out.println("Error while connecting to database " + e.getMessage());
746: noError = false;
747: if (isGUI)
748: {
749: showExceptionDialog(dialog, command, e);
750: resetGUI(dialog);
751: } else
752: {
753: e.printStackTrace();
754:
755: }
756: } catch (Error e)
757: {
758: noError = false;
759:
760: System.out.println(e);
761: e.printStackTrace();
762: }
763:
764: }
765:
766: /**
767: *
768: * @param beanDest2
769: * @param originalName
770: * @return
771: */
772: private static String addQuotes(DBBean bean, String originalName)
773: {
774: return bean.getQC() + originalName + bean.getQC();
775: }
776:
777: /**
778: * @param names
779: * @param dialog
780: * @param tbIndex
781: */
782: private static void prepareDialogforUse(Vector names, FM2SQL.ProgressDialog dialog, int tbIndex)
783: {
784: dialog.title.setText("Reading table data ...");
785: dialog.table.setText(names.get(tbIndex).toString());
786: dialog.status.setText("Table " + (tbIndex + 1) + " of " + names.size());
787: dialog.setVisible(true);
788: }
789:
790: /**
791: * @param names
792: * @param query
793: * @param tbIndex
794: * @param layout
795: * @return
796: */
797: private static String addLayoutToQuery(Vector names, String query, int tbIndex, String layout)
798: {
799: layout = " layout " + bean.getQC() + layout + bean.getQC();
800: String name = names.get(tbIndex).toString();
801: StringBuffer queryLayout = new StringBuffer(query);
802: queryLayout.insert(queryLayout.indexOf(name) + name.length() + 1, " " + layout);
803: query = queryLayout.toString();
804: System.out.println("added layout " + query);
805: return query;
806: }
807:
808: /**
809: * Writes data to the destination table
810: *
811: * @param dialog
812: * progress dialog
813: * @param command
814: * @param j
815: * data index for progress bar
816: * @param pstm
817: * prepared statement
818: * @param rowCount
819: * number of datasets
820: * @return command
821: * @throws Exception
822: * @throws SQLException
823: */
824: private static StringBuffer writeDatainDestTable(FM2SQL.ProgressDialog dialog,
825: StringBuffer command, int j, PreparedStatement pstm, int rowCount, String delimiter)
826: throws Exception, SQLException
827: {
828: Vector row;
829: while ((row = bean.getNextRow()) != null)
830: {
831: j++;
832: Object obj = null;
833: for (int k = 0; k < row.size(); ++k)
834: {
835: obj = row.get(k);
836:
837: if (obj instanceof ArrayList)
838: obj = formatFileMakerArray((List) obj, delimiter);
839:
840: String str = (obj == null) ? "NULL" : obj.toString();
841: if (obj instanceof Double)
842: {
843: pstm.setDouble(k + 1, ((Double) obj).doubleValue());
844: } else if (!str.equals("NULL"))
845: pstm.setString(k + 1, str);
846: else
847: pstm.setNull(k + 1, Types.NULL);
848: }
849: pstm.execute();
850: if (isGUI)
851: dialog.progress.setValue((int) (((double) (j + 1) / (double) rowCount) * 100.0));
852: command = null;
853: } // to while loop
854: return command;
855: }
856:
857: /**
858: * removes special characters from the input string as well as .fp5
859: *
860: * @param newName
861: * String to change
862: * @return
863: */
864: public static String convertText(String newName)
865: {
866: StringBuffer alterMe = new StringBuffer(newName.trim().toLowerCase());
867: int length = alterMe.length();
868: int j = 0;
869: int index = alterMe.indexOf(".fp5");
870: if (index >= 0)
871: {
872: alterMe.delete(index, index + 4);
873: length = length - 4;
874: }
875:
876: while (j < length)
877: {
878: if (alterMe.charAt(j) == ' ')
879: {
880: alterMe.setCharAt(j, '_');
881: // if(j<length-1) j=j+1;
882: } else if (alterMe.charAt(j) == '_')
883: {
884:
885: if (alterMe.charAt(j + 1) == '_')
886: alterMe.deleteCharAt(j);
887: length = length - 1;
888: // if(j<length-1) j=j+1;
889: } else if (alterMe.charAt(j) == 'ä')
890: {
891: alterMe.setCharAt(j, 'a');
892: alterMe.insert(j + 1, "e");
893: length = length + 1;
894: if (j < length - 1)
895: j = j + 1;
896: } else if (alterMe.charAt(j) == 'ö')
897: {
898: alterMe.setCharAt(j, 'o');
899: alterMe.insert(j + 1, "e");
900: length = length + 1;
901: if (j < length - 1)
902: j = j + 1;
903: } else if (alterMe.charAt(j) == 'ü')
904: {
905: alterMe.setCharAt(j, 'u');
906: alterMe.insert(j + 1, "e");
907: length = length + 1;
908: if (j < length - 1)
909: j = j + 1;
910: } else if (alterMe.charAt(j) == 'ß')
911: {
912: alterMe.setCharAt(j, 's');
913: alterMe.insert(j + 1, "s");
914: length = length + 1;
915: if (j < length - 1)
916: j = j + 1;
917: } else if (alterMe.charAt(j) == ':')
918: {
919: if (j < length - 1)
920: {
921: if (alterMe.charAt(j + 1) == ':')
922: {
923: alterMe.setCharAt(j, '_');
924: alterMe.delete(j + 1, j + 2);
925: length = length - 1;
926:
927: }
928:
929: if (j < length - 1)
930: j = j + 1;
931: }
932: } else if (alterMe.charAt(j) == '-')
933: {
934: alterMe.setCharAt(j, '_');
935:
936: } else if (alterMe.charAt(j) == '?')
937: {
938: // changed ? to _ because of update statement
939: alterMe.setCharAt(j, '_');
940: // length = length + 1;
941: // j=j+1;
942: System.out.println(alterMe);
943: } else if (alterMe.charAt(j) == '.')
944: {
945: if (j == length - 1)
946: {
947: alterMe.delete(j, j);
948: length--;
949: } else
950: alterMe.setCharAt(j, '_');
951: }
952:
953: ++j;
954: }
955: return alterMe.toString();
956: }
957:
958: /**
959: * Converts > and < in an entity (> or <)
960: *
961: * @param newName
962: * @return
963: */
964: public static String convertToEntities(String newName)
965: {
966: StringBuffer alterMe = new StringBuffer(newName.trim());
967: int length = alterMe.length();
968: int j = 0;
969:
970: while (j < length)
971: {
972:
973: if (alterMe.charAt(j) == '>')
974: {
975: alterMe.setCharAt(j, '&');
976: alterMe.insert(j + 1, "gt;");
977: length = length + 2;
978: if (j < length - 1)
979: j = j + 1;
980:
981: } else if (alterMe.charAt(j) == '<')
982: {
983: alterMe.setCharAt(j, '&');
984: alterMe.insert(j + 1, "lt;");
985: length = length + 2;
986: if (j < length - 1)
987: j = j + 1;
988:
989: }
990: ++j;
991: }
992: return alterMe.toString();
993: }
994:
995: /**
996: * Masks the single quote character '-->\'
997: *
998: * @param newName
999: * @return
1000: */
1001: public static String convertUml(String newName)
1002: {
1003: StringBuffer alterMe = new StringBuffer(newName.trim());
1004: int length = alterMe.length();
1005: int j = 0;
1006:
1007: while (j < length)
1008: {
1009:
1010: if (alterMe.charAt(j) == '\'')
1011: {
1012: alterMe.setCharAt(j, '\\');
1013: alterMe.insert(j + 1, "'");
1014: length = length + 1;
1015: if (j < length - 1)
1016: j = j + 1;
1017: }
1018: /*
1019: * else if (alterMe.charAt(j) == '"') { alterMe.setCharAt(j, '\\');
1020: * alterMe.insert(j + 1, "\""); length = length + 1; if(j <length-1)
1021: * j=j+1; } else if (alterMe.charAt(j) == '>') { alterMe.setCharAt(j,
1022: * '\\'); alterMe.insert(j + 1, ">"); length = length + 1; if(j <length-1)
1023: * j=j+1; } else if (alterMe.charAt(j) == ' <') { alterMe.setCharAt(j,
1024: * '\\'); alterMe.insert(j + 1, " <"); length = length + 1; if(j
1025: * <length-1) j=j+1; } else if (alterMe.charAt(j) == '?') {
1026: * alterMe.setCharAt(j, '\\'); alterMe.insert(j + 1, "?"); length = length +
1027: * 1; if(j <length-1) j=j+1; } else if (alterMe.charAt(j) == '&') {
1028: * alterMe.setCharAt(j, '\\'); alterMe.insert(j + 1, "&"); length = length +
1029: * 1; if(j <length-1) j=j+1; } else if (alterMe.charAt(j) == '=') {
1030: * alterMe.setCharAt(j, '\\'); alterMe.insert(j + 1, "="); length = length +
1031: * 1; if(j <length-1) j=j+1; } else if (alterMe.charAt(j) == ',') {
1032: * alterMe.setCharAt(j, '\\'); alterMe.insert(j + 1, ","); length = length +
1033: * 1; if(j <length-1) j=j+1; } else if (alterMe.charAt(j) == '.') {
1034: * alterMe.setCharAt(j, '\\'); alterMe.insert(j + 1, "."); length = length +
1035: * 1; if(j <length-1) j=j+1; } else if (alterMe.charAt(j) == '[') {
1036: * alterMe.setCharAt(j, '\\'); alterMe.insert(j + 1, "."); length = length +
1037: * 1; if(j <length-1) j=j+1; } else if (alterMe.charAt(j) == ']') {
1038: * alterMe.setCharAt(j, '\\'); alterMe.insert(j + 1, "."); length = length +
1039: * 1; if(j <length-1) j=j+1; } else if (alterMe.charAt(j) == '%') {
1040: * alterMe.setCharAt(j, '\\'); alterMe.insert(j + 1, "%"); length = length +
1041: * 1; if(j <length-1) j=j+1; }
1042: */
1043: ++j;
1044: }
1045: return alterMe.toString();
1046: }
1047:
1048: /**
1049: * parses the input xml file for batch conversion called from readXMLFile *
1050: *
1051: * @param sb
1052: */
1053: public static void parseXMLConfig(StringBuffer sb)
1054: {
1055: // boolean finished = false;
1056: Vector databases = new Vector();
1057: try
1058: {
1059: databases = getXMLConfig(sb);
1060:
1061: // destination DataBase object
1062: DataBase dbDest = ((DataBase) databases.lastElement());
1063:
1064: DBBean database = ((DataBase) databases.lastElement()).bean;
1065: databases.remove(databases.size() - 1);
1066:
1067: for (Iterator iterator = dbDest.preCommands.iterator(); iterator.hasNext();)
1068: {
1069: SQLCommand sqlCommand = (SQLCommand) iterator.next();
1070: sqlCommand.executeCommand();
1071: }
1072:
1073: // databases.add(database);
1074: for (Iterator iter = databases.iterator(); iter.hasNext();)
1075: {
1076: DataBase db = (DataBase) iter.next();
1077: for (Iterator iterator = db.preCommands.iterator(); iterator.hasNext();)
1078: {
1079: SQLCommand sqlCommand = (SQLCommand) iterator.next();
1080: sqlCommand.executeCommand();
1081: }
1082: int mode = db.mode;
1083: if (mode == DataBase.CONVERT_TEMP_MODE)
1084: {
1085: convertBatch(db.bean, database, db.tables, db.layouts, db.selects, db.creates, db.ids,
1086: mode, db.delimiter);
1087: if (noError)
1088: {
1089: System.out.println("no Error occured ");
1090: // db.bean.setURL(database.url);
1091: // db.bean.setUserAndPasswd(database.user,database.passwd);
1092: //
1093: // Convert.user = db.bean.user;
1094: // Convert.passwd = db.bean.passwd;
1095: // userDest = database.user;
1096: // passwdDest = database.passwd;
1097: // synchronize(db.bean.url, database.url, db.tables, db.layouts, db.selects, db.creates,
1098: // db.ids, mode, db.delimiter, new Vector(db.htIndex.values()));
1099: }
1100: }
1101: if (mode == DataBase.CONVERT_MODE || mode == DataBase.APPEND_MODE)
1102: convertBatch(db.bean, database, db.tables, db.layouts, db.selects, db.creates, db.ids,
1103: mode, db.delimiter);
1104: else if (mode == DataBase.UPDATE_MODE)
1105: {
1106:
1107: Convert.user = db.bean.user;
1108: Convert.passwd = db.bean.passwd;
1109: userDest = database.user;
1110: passwdDest = database.passwd;
1111:
1112: update(db.bean.url, database.url, db.tables, db.layouts, db.selects, db.creates, db.ids,
1113: mode);
1114: } else if (mode == DataBase.SYNCHRONIZE_MODE)
1115: {
1116: Convert.user = db.bean.user;
1117: Convert.passwd = db.bean.passwd;
1118: userDest = database.user;
1119: passwdDest = database.passwd;
1120:
1121: synchronize(db.bean.url, database.url, db.tables, db.layouts, db.selects, db.creates,
1122: db.ids, mode, db.delimiter, new Vector(db.htIndex.values()), db.destIDs);
1123: }
1124: for (Iterator iterator = db.postCommands.iterator(); iterator.hasNext();)
1125: {
1126: SQLCommand sqlCommand = (SQLCommand) iterator.next();
1127: sqlCommand.executeCommand();
1128: }
1129:
1130: }
1131: for (Iterator iterator = dbDest.postCommands.iterator(); iterator.hasNext();)
1132: {
1133: SQLCommand sqlCommand = (SQLCommand) iterator.next();
1134: sqlCommand.executeCommand();
1135: }
1136:
1137: } catch (Exception e)
1138: {
1139:
1140: e.printStackTrace();
1141:
1142: } finally
1143: {
1144: bean.closeAllConnections();
1145: beanDest.closeAllConnections();
1146: }
1147: }
1148:
1149: /**
1150: *
1151: * @param database
1152: * @param db
1153: * @throws Exception
1154: * @throws SQLException
1155: */
1156: private static void renameTable(DBBean database, String sourceTable, String destTable)
1157: throws SQLException, Exception
1158: {
1159: String command = "alter table " + sourceTable + " rename " + " to " + destTable;
1160: SQLCommand sqlCommand = new SQLCommand(database, command);
1161: sqlCommand.executeCommand();
1162: }
1163:
1164: public static Vector getXMLConfig(StringBuffer sb)
1165: {
1166:
1167: boolean finished = false;
1168: // parse string and build document tree
1169: Xparse parser = new Xparse();
1170: parser.changeEntities = true;
1171: Node root = parser.parse(sb.toString());
1172: // printContents(root);
1173: Vector databases = new Vector();
1174: Vector tables = new Vector();
1175: Vector layouts = new Vector();
1176: Vector selects = new Vector();
1177: Vector creates = new Vector();
1178: Vector ids = new Vector();
1179: Vector destIDs = new Vector();
1180:
1181: Vector preSQLCommands = new Vector();
1182: Vector postSQLCommands = new Vector();
1183:
1184: String delimiter = "|";
1185: int mode = -1;
1186: try
1187: {
1188: Node tempNode = root.find("convert/source", new int[]
1189: { 1, 1 });
1190: if (tempNode == null)
1191: throw new Error("parse error source tag missing");
1192: System.out.println(tempNode.name);
1193: int length = countNodes(tempNode);
1194: for (int i = 1; i <= length; i++)
1195: {
1196:
1197: DBBean database = new DBBean();
1198: tables = new Vector();
1199: layouts = new Vector();
1200: selects = new Vector();
1201: creates = new Vector();
1202: ids = new Vector();
1203: destIDs = new Vector();
1204:
1205: preSQLCommands = new Vector();
1206: postSQLCommands = new Vector();
1207:
1208: // parse dataBase
1209: Node node = root.find("convert/source/database/url", new int[]
1210: { 1, 1, i, 1 });
1211: Node node1 = root.find("convert/source/database/user", new int[]
1212: { 1, 1, i, 1, 1 });
1213: Node node2 = root.find("convert/source/database/password", new int[]
1214: { 1, 1, i, 1, 1 });
1215: Node node3 = root.find("convert/source/database", new int[]
1216: { 1, 1, i });
1217: Node nodeMode = root.find("convert/source/database/mode", new int[]
1218: { 1, 1, i, 1, 1 });
1219: Node delimiterNode = root.find("convert/source/database/delimiter", new int[]
1220: { 1, 1, i, 1, 1 });
1221:
1222: Node commandNodes = root.find("convert/source/database/sqlcommands", new int[]
1223: { 1, 1, i, 1, 1 });
1224: if (commandNodes != null)
1225: {
1226: parseCommandNode(commandNodes, database, preSQLCommands, postSQLCommands);
1227: }
1228: Node useNormanToUnicodeMapper = root.find(
1229: "convert/source/database/usenormantounicodemapper", new int[]
1230: { 1, 1, i, 1, 1 });
1231:
1232: if (delimiterNode != null)
1233: delimiter = delimiterNode.getCharacters();
1234: if (useNormanToUnicodeMapper != null)
1235: {
1236: database.setUseNormanToUnicodeMapper(Boolean.valueOf(
1237: useNormanToUnicodeMapper.getCharacters()).booleanValue());
1238: System.out.println("useMapper "
1239: + Boolean.valueOf(useNormanToUnicodeMapper.getCharacters().trim()).booleanValue());
1240: }
1241:
1242: if (node3 == null)
1243: throw new Error("parse error database tag missing");
1244: if (node == null)
1245: throw new Error("parse error url tag missing");
1246: if (node1 == null)
1247: throw new Error("parse error user tag missing");
1248: if (node2 == null)
1249: throw new Error("parse error password tag missing");
1250: String url = node.getCharacters();
1251: String user = node1.getCharacters();
1252: String password = node2.getCharacters();
1253: database.setURL(url.trim());
1254: database.setUserAndPasswd(user.trim(), password.trim());
1255: System.out.println(node.name + " " + node.getCharacters());
1256: System.out.println(node1.name + " " + node1.getCharacters());
1257: System.out.println(node2.name + " " + node2.getCharacters());
1258: String modeString = "";
1259: if (nodeMode == null)
1260: modeString = "convert";
1261: else
1262: modeString = nodeMode.getCharacters();
1263: if (modeString.equals("convert"))
1264: mode = DataBase.CONVERT_MODE;
1265: else if (modeString.equals("append"))
1266: mode = DataBase.APPEND_MODE;
1267: else if (modeString.equals("update"))
1268: mode = DataBase.UPDATE_MODE;
1269: else if (modeString.equals("delete"))
1270: mode = DataBase.DELETE_MODE;
1271:
1272: else if (modeString.equals("synchronize"))
1273: mode = DataBase.SYNCHRONIZE_MODE;
1274: else if (modeString.equals("convert_temp"))
1275: mode = DataBase.CONVERT_TEMP_MODE;
1276:
1277: // if(node3!=null)
1278: // System.out.println(node3.name);
1279:
1280: int length2 = countNodes(node3);
1281:
1282: System.out.println("number of tables " + length2);
1283:
1284: for (int j = 1; j <= length2; ++j)
1285: {
1286: Node node4 = root.find("convert/source/database/table", new int[]
1287: { 1, 1, i, j });
1288: Node node5 = root.find("convert/source/database/table/select", new int[]
1289: { 1, 1, i, j, 1 });
1290: Node node6 = root.find("convert/source/database/table/create", new int[]
1291: { 1, 1, i, j, 1 });
1292:
1293: if (node4 != null)
1294: System.out.println(node4.name + " " + node4.attributes.get("layout").equals(""));
1295: if (node5 != null)
1296: System.out.println(node5.name + " " + node5.getCharacters());
1297: if (node6 != null)
1298: System.out.println(node6.name + " " + node6.getCharacters());
1299: if (node4 == null)
1300: throw new Error("parse error table tag missing");
1301: // if(node5==null) throw new Error("parse error select tag
1302: // missing");
1303: // if(node6==null) throw new Error("parse error create tag
1304: // missing");
1305: String name = (String) node4.attributes.get("name");
1306: String layout = (String) node4.attributes.get("layout");
1307: String id = (String) node4.attributes.get("id");
1308: String destID = (String) node4.attributes.get("targetID");
1309: System.out.println("id was " + id);
1310: System.out.println("targetID was " + destID);
1311:
1312: if (name == null)
1313: throw new Error("parse error required table tag attribute name missing");
1314: if (layout == null)
1315: layout = "";
1316: if (id == null)
1317: id = "";
1318: if (destID == null)
1319: destID = id ;
1320:
1321: if (name.equals(""))
1322: throw new Error("parse error table tag attribute must not be empty");
1323: tables.add(name.intern());
1324: layouts.add(layout.intern());
1325: ids.add(id.intern());
1326: destIDs.add(destID.intern());
1327: String query = (node5 == null) ? "" : node5.getCharacters();
1328: if (query.equals(""))
1329: System.err.println("Warning empty select tag or select tag missing !!");
1330: query = (query.equals("")) ? "select * from " + database.getQC() + name
1331: + database.getQC() : query;
1332: selects.add(query);
1333: if (node6 != null)
1334: creates.add(node6.getCharacters().trim());
1335: else
1336: creates.add("");
1337:
1338: }
1339: DataBase dataBase = new DataBase(database, tables, layouts, selects, creates, ids, mode);
1340: dataBase.delimiter = delimiter;
1341: dataBase.preCommands = new Vector(preSQLCommands);
1342: dataBase.postCommands = new Vector(postSQLCommands);
1343: dataBase.destIDs = destIDs;
1344: databases.add(dataBase);
1345: }
1346: DBBean database = new DBBean();
1347:
1348: preSQLCommands.clear();
1349: postSQLCommands.clear();
1350:
1351: // parse dataBase
1352: Node node = root.find("convert/destination/database/url", new int[]
1353: { 1, 1, 1, 1 });
1354: Node node1 = root.find("convert/destination/database/user", new int[]
1355: { 1, 1, 1, 1, 1 });
1356: Node node2 = root.find("convert/destination/database/password", new int[]
1357: { 1, 1, 1, 1, 1 });
1358: Node commandNodes = root.find("convert/destination/database/sqlcommands", new int[]
1359: { 1, 1, 1, 1, 1 });
1360: if (commandNodes != null)
1361: {
1362: parseCommandNode(commandNodes, database, preSQLCommands, postSQLCommands);
1363: }
1364:
1365: String url = node.getCharacters();
1366: String user = node1.getCharacters();
1367: String password = node2.getCharacters();
1368: System.out.println(url);
1369: database.setURL(url.trim());
1370: database.setUserAndPasswd(user.trim(), password.trim());
1371: DataBase db = new DataBase(database, new Vector(), null, null, null, null, 0);
1372: databases.add(db);
1373: db.preCommands = new Vector(preSQLCommands);
1374: db.postCommands = new Vector(postSQLCommands);
1375:
1376: } catch (Exception e)
1377: {
1378: e.printStackTrace();
1379: }
1380: return databases;
1381: }
1382:
1383: /**
1384: *
1385: * @param commandNode
1386: * @param database
1387: * @param preSQLCommands
1388: * @param postSQLCommand
1389: */
1390: private static void parseCommandNode(Node commandNode, DBBean database,
1391: java.util.Vector preSQLCommands, java.util.Vector postSQLCommands)
1392: {
1393: // System.out.println(commandNode.name + " " + countNodes(commandNode));
1394: int numCommands = commandNode.contents.length();
1395: for (int j = 0; j < numCommands; ++j)
1396: {
1397: Node node = (Node) commandNode.contents.v.elementAt(j);
1398: if (node.type.equals("element"))
1399: {
1400: // System.out.println(node.name + " " + node.getCharacters() + database);
1401: String execute = node.attributes.get("execute").toString();
1402: if (execute.equals("before"))
1403: {
1404: preSQLCommands.add(new SQLCommand(database, node.getCharacters()));
1405: }
1406: if (execute.equals("after"))
1407: {
1408: postSQLCommands.add(new SQLCommand(database, node.getCharacters()));
1409: }
1410:
1411: }
1412:
1413: }
1414: }
1415:
1416: private static int countNodes(Node tempNode)
1417: {
1418: int length = 0;
1419: for (int i = 0; i < tempNode.contents.v.size(); ++i)
1420: {
1421: Node node = (Node) tempNode.contents.v.elementAt(i);
1422: if (node.type.equals("element"))
1423: {
1424: if (node.name.equals("database"))
1425: length++;
1426: if (node.name.equals("table"))
1427: length++;
1428: if (node.name.equals("sqlcommand"))
1429: length++;
1430:
1431: }
1432:
1433: // System.out.println(((Node)tempNode.contents.v.elementAt(i)).attributes+"
1434: // "+i);
1435: }
1436: return length;
1437: }
1438:
1439: private static void printContents(Node root)
1440: {
1441:
1442: Vector contents = (root.index == null) ? root.contents.v : root.index.v;
1443: for (int i = 0; i < contents.size(); ++i)
1444: {
1445: Node n = (Node) contents.elementAt(i);
1446: if (n.type.equals("element"))
1447: {
1448: System.out.println("tag " + n.name);
1449: System.out.println(n.getCharacters());
1450: // contents=n.contents.v i=0;
1451: }
1452: // System.out.println(n.type);
1453: }
1454: }
1455:
1456: /**
1457: * reads the specified xml file
1458: *
1459: * @param xmlFile
1460: */
1461: public static StringBuffer readXMLFile(String xmlFile)
1462: {
1463: InputStream stream = null;
1464: StringBuffer sb = new StringBuffer();
1465:
1466: try
1467: {
1468:
1469: if (xmlFile.indexOf("file://") >= 0 || xmlFile.indexOf("http://") >= 0)
1470: {
1471: URL url = new URL(xmlFile);
1472: stream = url.openStream();
1473: } else
1474: // read XML Metadata from a file
1475: stream = new FileInputStream(xmlFile);
1476: InputStreamReader isr = new InputStreamReader(stream, "UTF-8");
1477: BufferedReader buffr = new BufferedReader(isr);
1478: int c = 0;
1479: while ((c = buffr.read()) != -1)
1480: {
1481: char ch = (char) c;
1482: sb.append(ch);
1483: // System.out.print((char)c);
1484: }
1485: } catch (Exception e)
1486: {
1487: e.printStackTrace();
1488: } finally
1489: {
1490:
1491: try
1492: {
1493: stream.close();
1494: } catch (IOException e1)
1495: {
1496: // TODO Auto-generated catch block
1497: e1.printStackTrace();
1498: }
1499: }
1500: return sb;
1501: }
1502:
1503: /**
1504: * Helper class for Conversion etc. Contains data needed for the conversion
1505: *
1506: * @author rogo
1507: *
1508: */
1509:
1510: public static class ConversionProperties
1511: {
1512: String destTableName;
1513:
1514: String[] fieldNames;
1515:
1516: public ConversionProperties()
1517: {
1518: }
1519:
1520: public ConversionProperties(String destTableName, String[] fieldNames)
1521: {
1522: this.destTableName = destTableName;
1523: this.fieldNames = fieldNames;
1524:
1525: }
1526:
1527: }
1528:
1529: /**
1530: * Helper class for XML-File parsing Holds the parsed data
1531: *
1532: * @author rogo
1533: *
1534: */
1535: public static class DataBase
1536: {
1537:
1538: DBBean bean;
1539:
1540: Vector creates;
1541:
1542: Vector selects;
1543:
1544: Vector layouts;
1545:
1546: Vector tables = new Vector();
1547:
1548: Vector ids;
1549: Vector destIDs;
1550:
1551: String delimiter = "//";
1552:
1553: Vector preCommands;
1554: Vector postCommands;
1555:
1556: /**
1557: * maps table name to index fields
1558: */
1559: Hashtable htIndex = new Hashtable();
1560:
1561: boolean useNormanToUnicodeMapper = false;
1562:
1563: final static int CONVERT_MODE = 1;
1564:
1565: final static int APPEND_MODE = 2;
1566:
1567: final static int UPDATE_MODE = 3;
1568:
1569: final static int DELETE_MODE = 4;
1570:
1571: final static int SYNCHRONIZE_MODE = 5;
1572:
1573: final static int CONVERT_TEMP_MODE = 6;
1574:
1575: int mode = -1;
1576:
1577: public DataBase(DBBean bean, Vector tables, Vector layouts, Vector selects, Vector creates,
1578: Vector ids, int mode)
1579: {
1580: this.bean = bean;
1581: this.tables = tables;
1582: this.layouts = layouts;
1583: this.selects = selects;
1584: this.creates = creates;
1585: this.ids = ids;
1586: this.mode = mode;
1587: this.bean.setIDVector(ids);
1588: }
1589:
1590: /**
1591: * @param indexListVec
1592: */
1593: public void buildIndexTable(Vector indexListVec)
1594: {
1595: for (int i = 0; i < tables.size(); i++)
1596: {
1597: fillIndexList((String) tables.get(i), (String) indexListVec.get(i));
1598: }
1599: }
1600:
1601: /**
1602: * writes the data contained in this object to the buffered writer *
1603: *
1604: * @param buffr
1605: * @throws Exception
1606: */
1607: public void exportToXML(BufferedWriter buffr) throws Exception
1608: {
1609: // ids=bean.getIDVector();
1610: buffr.write(" <database>\n");
1611: buffr.write(" <url>" + bean.url + "</url>\n");
1612: buffr.write(" <user>" + bean.user + "</user>\n");
1613: buffr.write(" <password>" + bean.passwd + "</password>\n");
1614: buffr.write(" <delimiter>" + delimiter + "</delimiter>\n");
1615: String modeString = "";
1616: if (mode == CONVERT_TEMP_MODE)
1617: modeString = "convert_temp";
1618: else if (mode == CONVERT_MODE)
1619: modeString = "convert";
1620: else if (mode == APPEND_MODE)
1621: modeString = "append";
1622: else if (mode == UPDATE_MODE)
1623: modeString = "update";
1624: else if (mode == DELETE_MODE)
1625: modeString = "delete";
1626: else if (mode == SYNCHRONIZE_MODE)
1627: modeString = "synchronize";
1628:
1629: buffr.write(" <mode>" + modeString + "</mode>\n");
1630: buffr.write(" <usenormantounicodemapper>" + useNormanToUnicodeMapper
1631: + "</usenormantounicodemapper>\n");
1632: if (preCommands != null || postCommands != null)
1633: {
1634: int count = 0;
1635:
1636: buffr.write(" <sqlcommands> \n");
1637:
1638: if (preCommands != null)
1639: {
1640: while (count < preCommands.size())
1641: {
1642: SQLCommand sqlcommand = (SQLCommand) preCommands.get(count);
1643: buffr.write(" <sqlcommand execute=\"before\">" + sqlcommand.command
1644: + "</sqlcommand>\n");
1645: count++;
1646: }
1647: }
1648: if (postCommands != null)
1649: {
1650: count = 0;
1651: while (count < postCommands.size())
1652: {
1653: SQLCommand sqlcommand = (SQLCommand) postCommands.get(count);
1654:
1655: buffr.write(" <sqlcommand execute=\"after\">" + sqlcommand.command
1656: + "</sqlcommand>\n");
1657: count++;
1658: }
1659: }
1660: buffr.write(" </sqlcommands> \n");
1661:
1662: }
1663: int index = 0;
1664: while (index < tables.size())
1665: {
1666: String table = (String) tables.get(index);
1667: String layout = (String) layouts.get(index);
1668: String select = (String) selects.get(index);
1669: String create = (String) creates.get(index);
1670: String id = (String) ids.get(index);
1671: IndexList indexList = (IndexList) htIndex.get(table);
1672: if (indexList == null)
1673: indexList = new IndexList();
1674: buffr.write(" <table name = \"" + table + "\" layout = \"" + layout + "\" id = \""
1675: + id + "\" indexList =\"" + indexList + "\">\n");
1676: buffr.write(" <select>" + convertToEntities(select) + "</select>\n");
1677: if (!create.equals(""))
1678: buffr.write(" <create>" + create + " </create>\n");
1679: buffr.write(" </table>\n");
1680: index++;
1681: }
1682: buffr.write(" </database>\n");
1683: }
1684:
1685: public void fillIndexList(String table, String list)
1686: {
1687: IndexList indexList = new IndexList();
1688: StringTokenizer tokenizer = new StringTokenizer(list, ",");
1689: while (tokenizer.hasMoreTokens())
1690: {
1691: indexList.add(tokenizer.nextToken());
1692: }
1693: System.out.println(indexList);
1694:
1695: htIndex.put(table, indexList);
1696: }
1697:
1698: public String toString()
1699: {
1700: return bean.url + " " + tables;
1701: }
1702:
1703: }
1704:
1705: public static void writeConfig(String file, DataBase source, DataBase destination)
1706: throws Exception
1707: {
1708: if (!file.toLowerCase().endsWith(".xml"))
1709: file += ".xml";
1710: File f = new File(file);
1711:
1712: FileOutputStream fout = new FileOutputStream(f);
1713: OutputStreamWriter outsw = new OutputStreamWriter(fout, "UTF-8");
1714: BufferedWriter buffw = new BufferedWriter(outsw);
1715: buffw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
1716: buffw.newLine();
1717: buffw.write("<convert>\n");
1718: buffw.write(" <source>\n");
1719: source.exportToXML(buffw);
1720: buffw.write(" </source>\n");
1721: buffw.write("\n <destination>\n");
1722: destination.exportToXML(buffw);
1723: buffw.write(" </destination>\n");
1724: buffw.write("</convert>\n");
1725: buffw.close();
1726: }
1727:
1728: public static void delete(String source, String destination, Vector names, Vector layouts,
1729: Vector selects, Vector creates, Vector ids, int mode) throws Exception
1730: {
1731: FM2SQL.ProgressDialog dialog = null;
1732: if (isGUI)
1733: {
1734: dialog = initDialog();
1735: }
1736: // setting user and passwd
1737: bean.setUserAndPasswd(user, passwd);
1738: // setting user and passwd
1739: beanDest.setUserAndPasswd(userDest, passwdDest);
1740: StringBuffer command = null;
1741: String query = null;
1742: try
1743: {
1744: // bean.setConnection("jdbc:fmpro:http://141.14.237.74:8050");
1745: // bean.setConnection("jdbc:postgresql://erebos/test","postgres","rogo");
1746: bean.setConnection(source);
1747: if (names == null)
1748: names = bean.getTableNames();
1749: // Collections.sort(names);
1750: int tbIndex = 1;
1751:
1752: // System.out.println("Start at
1753: // "+names.indexOf("archimedes_facsimiles"));
1754: for (tbIndex = 0; tbIndex < names.size(); ++tbIndex)
1755: {
1756: Vector[] result = null;
1757: java.util.TreeSet myIds = new TreeSet();
1758: java.util.TreeSet myIdsDest = new TreeSet();
1759: int deltaID = 1;
1760: String idField = "";
1761: String destTableName = "";
1762:
1763: try
1764: {
1765: query = "select * from " + bean.getQC() + names.get(tbIndex).toString() + bean.getQC();
1766: String layout = (layouts.isEmpty()) ? "" : layouts.get(tbIndex).toString();
1767: query = (selects != null) ? selects.get(tbIndex).toString() : query;
1768: // if vectors[1].get(i) != null)
1769: if (layout != "")
1770: {
1771: layout = " layout " + bean.getQC() + layout + bean.getQC();
1772: String name = names.get(tbIndex).toString();
1773: StringBuffer queryLayout = new StringBuffer(query);
1774: queryLayout.insert(queryLayout.indexOf(name) + name.length() + 1, " " + layout);
1775: query = queryLayout.toString();
1776: System.out.println("added layout " + query);
1777:
1778: }
1779: dialog.title.setText("Getting table data ...");
1780: dialog.table.setText(names.get(tbIndex).toString());
1781: dialog.status.setText("Table " + (tbIndex + 1) + " of " + names.size());
1782: dialog.setVisible(true);
1783: bean.getConnection();
1784: bean.makeQuery(query, 50);
1785: idField = ids.get(tbIndex).toString();
1786:
1787: } catch (Exception e)
1788: {
1789: continue;
1790: }
1791: // determine destTableName from createStatement or from source
1792: // table name
1793: if (!creates.get(tbIndex).equals(""))
1794: {
1795: String create = creates.get(tbIndex).toString().toLowerCase();
1796: int fromIndex = create.indexOf("table") + 5;
1797: int toIndex = create.indexOf("(");
1798: destTableName = create.substring(fromIndex, toIndex).replaceAll(beanDest.getQC(), "")
1799: .trim();
1800: System.out.println("destTable " + destTableName);
1801:
1802: } else
1803: destTableName = convertText(names.get(tbIndex).toString());
1804:
1805: // for id kram
1806: Vector vec = null;
1807: Vector vecDest = null;
1808: // tempo
1809: beanDest.setConnection(destination);
1810: int rowCount = (idField != "") ? myIds.size() : bean.getRowCount(query);
1811: String tempID = bean.getQC() + idField + bean.getQC();
1812: String tempIDdest = beanDest.getQC() + convertText(idField) + beanDest.getQC();
1813:
1814: int endIndex = -1;
1815: String tempQuery = query;
1816: String destQuery = query.replaceAll(names.get(tbIndex).toString(), destTableName);
1817: String tempQueryDest = destQuery;
1818: // remove extra query parts
1819: // destQuery.substring(0,destQuery.lastIndexOf(destTableName)+destTableName.length()+1);
1820: System.out.println("new Query " + tempQueryDest);
1821: if (!idField.equals(""))
1822: {
1823: long startTime = System.currentTimeMillis();
1824: int counter = -1;
1825: while (true)
1826: {
1827: ++counter;
1828: if (counter == 0 && dialog != null)
1829: dialog.title.setText("Check if data is available");
1830: else if (dialog != null)
1831: dialog.title.setText("Check if more data is available");
1832: myIds = bean.getIDVector(ids.get(tbIndex).toString(), (String) names.get(tbIndex),
1833: tempQuery, numHits);
1834: myIdsDest = beanDest.getIDVector(convertText(idField), destTableName, tempQueryDest,
1835: numHits);
1836: if (myIds.isEmpty())
1837: break;
1838: vec = new Vector(myIds);
1839: vecDest = new Vector(myIdsDest);
1840: rowCount = vec.size();
1841: // Deletion will work this way
1842: Vector deleted = new Vector(vec);
1843: Vector linesToDelete = new Vector(vecDest);
1844: // remove all lines that should not be deleted
1845: linesToDelete.removeAll(deleted);
1846: // System.out.println("ID LIST SIZE " +
1847: // Math.round((double) myIds.size() / (double)
1848: // numIntervalls) + " " + myIdsDest.size());
1849: // / @TODO complete delete task remove query show lines
1850: // to be deleted let user choose if he wants that
1851: System.out.println("number of lines to be deleted " + linesToDelete.size());
1852: deltaID = (int) Math.round((double) myIds.size() / (double) numIntervalls);
1853: beanDest.setConnection(destination);
1854:
1855: Statement stm = beanDest.getConnection().createStatement();
1856:
1857: Vector tables = beanDest.getTableNames();
1858: // Collections.sort(tables);
1859: System.out.println(names.get(tbIndex) + " "
1860: + tables.indexOf(convertText((String) names.get(tbIndex)))); // "//beanDest.getTypeNames());
1861: tables = beanDest.getTableNames();
1862: // System.out.println(beanDest.getTableNames(beanDest.getCatalogs().get(2).toString()));
1863: stm = beanDest.getConnection().createStatement();
1864:
1865: if (dialog != null)
1866: dialog.title.setText(" Deleting table data ...");
1867:
1868: int j = -1;
1869:
1870: Vector row = null;
1871: command = new StringBuffer();
1872:
1873: command.append("DELETE FROM");
1874: command.append(beanDest.getQC());
1875: command.append(destTableName);
1876: command.append(beanDest.getQC());
1877: int size = bean.getColumnNames().size();
1878: command.append("WHERE " + convertText(ids.get(tbIndex).toString()) + " = ?");
1879: PreparedStatement pstm = beanDest.getConnection().prepareStatement(command.toString());
1880: System.out.println(command + " " + tbIndex);
1881: while (true)
1882: {
1883:
1884: ++j;
1885: if (j == linesToDelete.size())
1886: break;
1887: // print rows
1888: pstm.setString(1, linesToDelete.get(j).toString());
1889: System.out.println(pstm.toString());
1890: pstm.execute();
1891: if (isGUI)
1892: dialog.progress.setValue((int) (((double) (j + 1) / (double) rowCount) * 100.0));
1893: command = null;
1894: }
1895: // prepare new query for next chunk
1896: if (query.toLowerCase().indexOf("where") > 0)
1897: tempQuery = query + " and " + tempID + ">'" + vec.lastElement() + "'";
1898: else
1899: tempQuery = query + " where " + tempID + ">'" + vec.lastElement() + "'";
1900:
1901: } // to outer while
1902: } // to idfield if
1903: } // table loop
1904:
1905: } catch (Exception e)
1906: {
1907: System.out.println("Error while connecting to database " + e);
1908: if (isGUI)
1909: {
1910: showExceptionDialog(dialog, command, e);
1911: resetGUI(dialog);
1912: } else
1913: {
1914: e.printStackTrace();
1915: }
1916: }
1917: } // to method
1918:
1919: /**
1920: * synchronize method based on delete method code
1921: * @param source
1922: * @param destination
1923: * @param names
1924: * @param layouts
1925: * @param selects
1926: * @param creates
1927: * @param ids
1928: * @param mode
1929: * @param destIDs TODO
1930: *
1931: * @throws Exception
1932: */
1933: // TODO implement append,update and delete in one method
1934: // TODO using id based algorithm
1935: public static void synchronize(String source, String destination, Vector names, Vector layouts,
1936: Vector selects, Vector creates, Vector ids, int mode, String delimiter, Vector indexList, java.util.Vector destIDs)
1937: throws Exception
1938: {
1939: System.out.println(" bin in synchronize!!!");
1940: FM2SQL.ProgressDialog dialog = null;
1941: if (isGUI)
1942: {
1943: dialog = initDialog();
1944: dialog.setTitle("Synchronize running ...");
1945:
1946: }
1947: // setting user and passwd
1948: bean.setUserAndPasswd(user, passwd);
1949: // setting user and passwd
1950: beanDest.setUserAndPasswd(userDest, passwdDest);
1951: StringBuffer command = null;
1952: String query = null;
1953: try
1954: {
1955: bean.setConnection(source);
1956: if (names == null)
1957: names = bean.getTableNames();
1958: int tbIndex = 1;
1959:
1960: for (tbIndex = 0; tbIndex < names.size(); ++tbIndex)
1961: {
1962: Vector[] result = null;
1963: java.util.TreeSet myIds = new TreeSet();
1964: java.util.TreeSet myIdsDest = new TreeSet();
1965: int deltaID = 1;
1966: String idField = "";
1967: String destIDField = "";
1968: String destTableName = "";
1969:
1970: try
1971: {
1972: query = "select * from " + bean.getQC() + names.get(tbIndex).toString() + bean.getQC();
1973: String layout = (layouts.isEmpty()) ? "" : layouts.get(tbIndex).toString();
1974: query = (selects != null) ? selects.get(tbIndex).toString() : query;
1975: // if vectors[1].get(i) != null)
1976: if (!layout.equals(""))
1977: {
1978: query = addLayoutToQuery(names, query, tbIndex, layout);
1979:
1980: }
1981: if (dialog != null)
1982: {
1983: prepareDialogforUse(names, dialog, tbIndex);
1984: }
1985: bean.getConnection();
1986: bean.makeQuery(query, 50);
1987: idField = ids.get(tbIndex).toString();
1988: destIDField = (destIDs!=null) ? destIDs.get(tbIndex).toString():convertText(idField);
1989: } catch (Exception e)
1990: {
1991: System.out.println("Warning exception occured \n " + e);
1992:
1993: continue;
1994: }
1995: // determine destTableName from createStatement or from source
1996: // table name
1997: if (!creates.get(tbIndex).equals(""))
1998: {
1999: String create = creates.get(tbIndex).toString().toLowerCase();
2000: int fromIndex = create.indexOf("table") + 5;
2001: int toIndex = create.indexOf("(");
2002: destTableName = create.substring(fromIndex, toIndex).replaceAll(beanDest.getQC(), "")
2003: .trim();
2004: System.out.println("destTable " + destTableName);
2005:
2006: } else
2007: destTableName = convertText(names.get(tbIndex).toString());
2008:
2009: // for id kram
2010: Vector vec = null;
2011: Vector vecDest = null;
2012: // tempo
2013: beanDest.setConnection(destination);
2014: int rowCount = (idField != "") ? myIds.size() : bean.getRowCount(query);
2015: String tempID = bean.getQC() + idField + bean.getQC();
2016: String tempIDdest = beanDest.getQC() + convertText(idField) + beanDest.getQC();
2017:
2018: int endIndex = -1;
2019: String tempQuery = query;
2020: String destQuery = query.replaceAll(names.get(tbIndex).toString(), destTableName);
2021: destQuery = destQuery.replaceAll(bean.getQC(), beanDest.getQC());
2022: destQuery = removeLayoutPartFromQuery(destQuery, layouts.get(tbIndex).toString());
2023: // TODO remove layout part for destQuery
2024: String tempQueryDest = destQuery;
2025: // remove extra query parts
2026: // destQuery.substring(0,destQuery.lastIndexOf(destTableName)+destTableName.length()+1);
2027: System.out.println("new Query " + tempQueryDest);
2028: System.out.println("idfield " + idField + " " + ids.get(tbIndex).toString()+" targetID is "+destIDField);
2029: if (!idField.equals(""))
2030: {
2031: long startTime = System.currentTimeMillis();
2032: int counter = -1;
2033: TreeSet linesToDelete = null;
2034: PreparedStatement delPSt = null;
2035: while (true)
2036: {
2037: ++counter;
2038: if (counter == 0 && dialog != null)
2039: dialog.title.setText("Check if data is available");
2040: else if (dialog != null)
2041: dialog.title.setText("Check if more data is available");
2042:
2043: myIds = bean.getIDVector(ids.get(tbIndex).toString(), (String) names.get(tbIndex),
2044: tempQuery, 0);
2045: myIdsDest = beanDest.getIDVector(destIDField, destTableName, tempQueryDest, 0);
2046: // System.out.println("status of remove
2047: // "+myIds.remove("b015892"));
2048: System.out.println("ids found for " + idField + " " + !myIds.isEmpty());
2049: if (myIds.isEmpty())
2050: break;
2051: vec = new Vector(myIds);
2052: vecDest = new Vector(myIdsDest);
2053: rowCount = vec.size();
2054: // Deletion will work this way
2055: Vector deleted = new Vector(vec);
2056:
2057: TreeSet linesToAppend = new TreeSet(vec);
2058: // linesToAppend.addAll(vec);
2059: linesToDelete = new TreeSet(vecDest);
2060: // remove all lines that are already in dest database
2061: linesToAppend.removeAll(vecDest);
2062: // remove all lines that should not be deleted
2063: linesToDelete.removeAll(deleted);
2064: System.out.println("linesToAppend " + linesToAppend.size() + " " + destTableName);
2065: System.out.println("linesToDelete " + linesToDelete.size() + " " + destTableName);
2066: System.out.println("ID LIST SIZE "
2067: + Math.round((double) myIds.size() / (double) numIntervalls) + " " + myIds.size());
2068: deltaID = (int) Math.round((double) myIds.size() / (double) numIntervalls);
2069: ConversionProperties prop = getFieldNamesAndDestTableName(creates.get(tbIndex)
2070: .toString(), query, names.get(tbIndex).toString());
2071: StringBuffer insCommand = createInsertCommand(prop.destTableName, prop.fieldNames);
2072: StringBuffer updCommand = createUpdateCommand(prop.destTableName, prop.fieldNames,
2073: tempIDdest);
2074: StringBuffer delCommand = createDeleteCommand(destTableName, tempIDdest);
2075: PreparedStatement insPst = beanDest.getConnection().prepareStatement(
2076: insCommand.toString());
2077: PreparedStatement updPst = beanDest.getConnection().prepareStatement(
2078: updCommand.toString());
2079: delPSt = beanDest.getConnection().prepareStatement(delCommand.toString());
2080: // delPSt.setString(1,"b015892");
2081: // delPSt.execute();
2082: // if (true)
2083: // return;
2084: if (vec.size() <= numIntervalls)
2085: {
2086: endIndex = 0;
2087: deltaID = vec.size();
2088: }
2089: for (int k = 0; k < vec.size() - deltaID; k = k + deltaID)
2090: {
2091: System.out.println(vec.get(k) + " " + vec.get(k + deltaID) + " " + vec.lastElement());
2092: if (query.toLowerCase().indexOf("where") > 0)
2093: tempQuery = query + " and " + tempID + ">='" + vec.get(k) + "' and " + tempID
2094: + "<='" + vec.get(k + deltaID) + "'";
2095: else
2096: tempQuery = query + " where " + tempID + ">='" + vec.get(k) + "' and " + tempID
2097: + "<='" + vec.get(k + deltaID) + "'";
2098: System.out.println(tempQuery);
2099: if (dialog != null)
2100: dialog.title.setText("Reading table data ...");
2101:
2102: // bean.makeQuery(tempQuery, deltaID);
2103: if (dialog != null)
2104: dialog.title.setText("Writing table data ...");
2105:
2106: performSynchronize(idField, vec, tempQuery, linesToDelete, linesToAppend, insPst,
2107: updPst, delPSt, deltaID, delimiter, dialog);
2108: // System.out.println("ID LIST SIZE " +
2109: // Math.round((double) myIds.size() / (double)
2110: // numIntervalls) + " " + myIdsDest.size());
2111: endIndex = k + deltaID;
2112: }
2113: System.out.println(endIndex);
2114: // all data written ? if not write last chunk of data
2115: if (endIndex == vec.size() - 1)
2116: System.out.println("fits");
2117: else
2118: {
2119: System.out.println(" last intervall from " + vec.get(endIndex) + " "
2120: + vec.lastElement());
2121:
2122: if (query.toLowerCase().indexOf("where") > 0)
2123: tempQuery = query + " and " + tempID + ">='" + vec.get(endIndex) + "' and "
2124: + tempID + "<='" + vec.lastElement() + "'";
2125: else
2126: tempQuery = query + " where " + tempID + ">='" + vec.get(endIndex) + "' and "
2127: + tempID + "<='" + vec.lastElement() + "'";
2128: System.out.println(tempQuery);
2129: if (dialog != null)
2130: dialog.title.setText("Reading table data ...");
2131: // bean.makeQuery(tempQuery, 0);
2132: if (dialog != null)
2133: dialog.title.setText("Writing table data ...");
2134: performSynchronize(idField, vec, tempQuery, linesToDelete, linesToAppend, insPst,
2135: updPst, delPSt, deltaID, delimiter, dialog);
2136: // System.out.println("ID LIST SIZE " +
2137: // Math.round((double) myIds.size() / (double)
2138: // numIntervalls) + " " + myIdsDest.size());
2139: }
2140: // prepare new query for next chunk
2141: if (query.toLowerCase().indexOf("where") > 0)
2142: tempQuery = query + " and " + tempID + ">'" + vec.lastElement() + "'";
2143: else
2144: tempQuery = query + " where " + tempID + ">'" + vec.lastElement() + "'";
2145:
2146: }
2147: String tableName = names.get(tbIndex).toString();
2148: if (!indexList.isEmpty())
2149: {
2150: IndexList idList = (IndexList) indexList.get(0);
2151: System.out.println("found list " + idList);
2152: Statement stm = beanDest.getConnection().createStatement();
2153: Vector destTables = beanDest.getTableNames();
2154: System.out.println("tempQueryDest" + tempQueryDest);
2155: beanDest.makeQuery(tempQueryDest, 0);
2156: for (Iterator iter = idList.iterator(); iter.hasNext();)
2157: {
2158: String indexField = (String) iter.next();
2159: indexField = convertText(indexField);
2160: String indexName = destTableName + "_" + indexField;
2161: if (destTables.contains(indexName))
2162: {
2163: stm.execute("DROP INDEX " + destTableName + "_" + indexField);
2164: // continue;
2165: }
2166: // stm.execute("DROP INDEX
2167: // "+destTableName+"_"+indexField);
2168:
2169: String type = beanDest.getColumnType(indexField).toLowerCase();
2170: // System.out.println(indexField+" "+type+"
2171: // "+(type.indexOf("text") >= 0 ||
2172: // type.indexOf("varchar") >= 0 || type.indexOf("char")
2173: // >= 0));
2174: if (type.indexOf("text") >= 0 || type.indexOf("varchar") >= 0
2175: || type.indexOf("char") >= 0)
2176: {
2177: if (beanDest.url.indexOf("mysql") >= 0)
2178: {
2179: // System.out.println("CREATE INDEX " +
2180: // indexName + " ON " + destTableName + " (" +
2181: // indexField + "(10))");
2182: // TODO problem if index exist !!!
2183: stm.execute("CREATE INDEX " + indexName + " ON " + destTableName + " ("
2184: + indexField + "(10))");
2185: } else
2186: {
2187: stm.execute("CREATE INDEX " + indexName + " ON " + destTableName + " (lower( "
2188: + indexField + "))");
2189:
2190: }
2191:
2192: } else
2193: {
2194: stm.execute("CREATE INDEX " + destTableName + "_" + indexField + " ON "
2195: + destTableName + "(" + indexField + ")");
2196:
2197: }
2198:
2199: // stm.execute("DROP INDEX
2200: // "+destTableName+"_"+indexField);
2201:
2202: }
2203: }
2204: // CREATE UNIQUE INDEX title_idx ON films (title);
2205: for (Iterator iter = linesToDelete.iterator(); iter.hasNext();)
2206: {
2207: String id = (String) iter.next();
2208: delPSt.setString(1, id);
2209: delPSt.execute();
2210:
2211: }
2212:
2213: long endTime = System.currentTimeMillis();
2214: System.out.println("Time for incremental synchronize elapsed " + (endTime - startTime));
2215: } // to idfield if
2216: } // table loop
2217:
2218: } catch (Exception e)
2219: {
2220: System.out.println("Error while connecting to database " + e);
2221: e.printStackTrace();
2222: if (isGUI)
2223: showExceptionDialog(dialog, command, e);
2224: }
2225: if (isGUI)
2226: {
2227: resetGUI(dialog);
2228: }
2229: }
2230:
2231: /**
2232: * @param destQuery
2233: * @param string
2234: * @return
2235: */
2236: private static String removeLayoutPartFromQuery(String destQuery, String layoutName)
2237: {
2238: String removeString = "layout " + beanDest.getQC() + layoutName + beanDest.getQC();
2239: destQuery = destQuery.replaceFirst(removeString, "");
2240: System.out.println("destQuery change to " + destQuery);
2241: return destQuery;
2242: }
2243:
2244: private static void performSynchronize(String idField, Vector vec, String tempQuery,
2245: TreeSet linesToDelete, TreeSet linesToAppend, PreparedStatement insPst,
2246: PreparedStatement updPst, PreparedStatement delPSt, int deltaID, String delimiter,
2247: FM2SQL.ProgressDialog dialog) throws SQLException, ParseException, Exception
2248: {
2249: if (dialog != null)
2250: {
2251: dialog.progress.setValue(0);
2252: dialog.title.setText("Retrieving new data");
2253: }
2254:
2255: Vector[] vectors = bean.getQueryData(tempQuery, deltaID);
2256: int count = 0, size = vectors[0].size();
2257: int idIndex = vectors[1].indexOf(idField);
2258: // System.out.println(idIndex + " " + vectors[1] + " " + idField);
2259: // todo arraylist code has to be added
2260: if (dialog != null)
2261: dialog.title.setText("Synchronize with new data");
2262:
2263: for (Iterator iter = vectors[0].iterator(); iter.hasNext();)
2264: {
2265: Vector line = (Vector) iter.next();
2266: Object lineIDIndex = line.get(idIndex);
2267: if (linesToAppend.contains(lineIDIndex))
2268: System.out.println("line " + linesToAppend.contains(lineIDIndex) + " " + lineIDIndex);
2269: if (linesToAppend.contains(lineIDIndex))
2270: {
2271: for (int l = 0; l < line.size(); ++l)
2272: {
2273: Object obj = line.get(l);
2274: if (obj instanceof ArrayList)
2275: obj = formatFileMakerArray((List) obj, delimiter);
2276: if (obj != null)
2277: insPst.setString(l + 1, obj.toString());
2278: else
2279: insPst.setNull(l + 1, Types.NULL);
2280: }
2281: insPst.execute();
2282:
2283: } else
2284: // update
2285: {
2286: for (int l = 0; l < line.size(); ++l)
2287: {
2288: Object obj = line.get(l);
2289: if (obj instanceof ArrayList)
2290: obj = formatFileMakerArray((List) obj, delimiter);
2291: if (obj != null)
2292: updPst.setString(l + 1, obj.toString());
2293: else
2294: updPst.setNull(l + 1, Types.NULL);
2295: }
2296: updPst.setString(line.size() + 1, line.get(idIndex).toString());
2297: // updPst.addBatch();
2298: // updPst.execute();
2299: }
2300: if (dialog != null)
2301: {
2302: int value = (int) Math.round(((double) count / (double) size) * 100.0);
2303: dialog.progress.setValue(value);
2304: count++;
2305: }
2306: }
2307: // updPst.executeBatch();
2308: } // to method
2309:
2310: /**
2311: * Converts input String in norman encoding to unicode
2312: *
2313: * @param inp
2314: * @return converted String
2315: */
2316: static public String normanToUnicode(String inp)
2317: {
2318: StringBuffer buf = new StringBuffer();
2319: for (int i = 0; i < inp.length(); i++)
2320: {
2321: char c = inp.charAt(i);
2322: // System.out.println("char "+c+" "+(int)c);
2323: switch (c)
2324: {
2325: case 1:
2326: buf.append("\u00d0");
2327: break; // Eth
2328: case 2:
2329: buf.append("\u00f0");
2330: break; // eth
2331: case 3:
2332: buf.append("\u0141");
2333: break; // Lslash
2334: case 4:
2335: buf.append("\u0142");
2336: break; // lslash
2337: case 5:
2338: buf.append("\u0160");
2339: break; // S caron
2340: case 6:
2341: buf.append("\u0161");
2342: break; // s caron
2343: case 7:
2344: buf.append("\u00dd");
2345: break; // Y acute
2346: case 8:
2347: buf.append("\u00fd");
2348: break; // y acute
2349: case 11:
2350: buf.append("\u00de");
2351: break; // Thorn
2352: case 12:
2353: buf.append("\u00fe");
2354: break; // thorn
2355: case 14:
2356: buf.append("\u017d");
2357: break; // Z caron
2358: case 15:
2359: buf.append("\u017e");
2360: break; // z caron
2361: case 17:
2362: buf.append("\u0073");
2363: break; // asciitilde
2364: case 18:
2365: buf.append("j\u0305");
2366: break; // j macron [does a single char exist?]
2367: case 19:
2368: buf.append("^");
2369: break; // circumflex
2370: case 20:
2371: buf.append("\u0303");
2372: break; // tilde
2373: case 21:
2374: buf.append("\u00bd");
2375: break; // onehalf
2376: case 22:
2377: buf.append("\u00bc");
2378: break; // onequarter
2379: case 23:
2380: buf.append("\u00b9");
2381: break; // onesuperior
2382: case 24:
2383: buf.append("\u00be");
2384: break; // threequarters
2385: case 25:
2386: buf.append("\u00b3");
2387: break; // threesuperior
2388: case 26:
2389: buf.append("\u00b2");
2390: break; // twosuperior
2391: case 27:
2392: buf.append("\u00a6");
2393: break; // brokenbar
2394: case 28:
2395: buf.append("-");
2396: break; // minus
2397: case 29:
2398: buf.append("\u00d7");
2399: break; // multiply
2400: case 39:
2401: buf.append("'");
2402: break; // quotesingle
2403: case 94:
2404: buf.append("\u0302");
2405: break; // circumflex
2406: case 96:
2407: buf.append("\u0300");
2408: break; // grave
2409: case 196:
2410: buf.append("\u00c4");
2411: break; // A dieresis
2412: case 197:
2413: buf.append("\u00c5");
2414: break; // A ring
2415: case 201:
2416: buf.append("\u00c9");
2417: break; // E acute
2418: case 209:
2419: buf.append("\u00d1");
2420: break; // N tilde
2421: case 214:
2422: buf.append("\u00d6");
2423: break; // O dieresis
2424: case 220:
2425: buf.append("\u00dc");
2426: break; // U dieresis
2427: case 225:
2428: buf.append("\u00e1");
2429: break; // a acute
2430: case 224:
2431: buf.append("\u00e0");
2432: break; // a grave
2433: case 226:
2434: buf.append("\u00e2");
2435: break; // a circumflex
2436: case 228:
2437: buf.append("\u00e4");
2438: break; // a dieresis
2439: case 227:
2440: buf.append("\u00e3");
2441: break; // a tilde
2442: case 229:
2443: buf.append("\u0101");
2444: break; // a macron
2445: case 231:
2446: buf.append("\u00e7");
2447: break; // c cedilla
2448: case 233:
2449: buf.append("\u00e9");
2450: break; // e acute
2451: case 232:
2452: buf.append("\u00e8");
2453: break; // e grave
2454: case 234:
2455: buf.append("\u00ea");
2456: break; // e circumflex
2457: case 235:
2458: buf.append("\u00eb");
2459: break; // e dieresis
2460: case 237:
2461: buf.append("\u00ed");
2462: break; // i acute
2463: case 236:
2464: buf.append("\u00ec");
2465: break; // i grave
2466: case 238:
2467: buf.append("\u00ee");
2468: break; // i circumflex
2469: case 239:
2470: buf.append("\u00ef");
2471: break; // i dieresis
2472: case 241:
2473: buf.append("\u00f1");
2474: break; // n tilde
2475: case 243:
2476: buf.append("\u00f3");
2477: break; // o acute
2478: case 242:
2479: buf.append("\u00f2");
2480: break; // o grave
2481: case 244:
2482: buf.append("\u00f4");
2483: break; // o circumflex
2484: case 246:
2485: buf.append("\u00f6");
2486: break; // o dieresis
2487: case 245:
2488: buf.append("\u00f5");
2489: break; // o tilde
2490: case 250:
2491: buf.append("\u00fa");
2492: break; // u acute
2493: case 249:
2494: buf.append("\u00f9");
2495: break; // u grave
2496: case 251:
2497: buf.append("\u00fb");
2498: break; // u circumflex
2499: case 252:
2500: buf.append("\u00fc");
2501: break; // u dieresis
2502: case 8224:
2503: buf.append("\u1e6d");
2504: break; // t underdot
2505: case 176:
2506: buf.append("\u00b0");
2507: break; // degree
2508: case 162:
2509: buf.append("\u1ebd");
2510: break; // e tilde
2511: case 163:
2512: buf.append("\u00a3");
2513: break; // sterling
2514: case 167:
2515: buf.append("\u00a7");
2516: break; // section
2517: case 182:
2518: buf.append("\u00b6");
2519: break; // paragraph
2520: case 223:
2521: buf.append("\u015b");
2522: break; // s acute
2523: case 174:
2524: buf.append("\u1e5b");
2525: break; // r underdot
2526: case 169:
2527: buf.append("\u1e45");
2528: break; // n overdot
2529: case 353:
2530: buf.append("\u1e45");
2531: break; // n overdot
2532: case 180:
2533: buf.append("\u0301");
2534: break; // acute
2535: case 168:
2536: buf.append("\u0308");
2537: break; // dieresis
2538: case 8800:
2539: buf.append("\u1e6d");
2540: break; // t underdot
2541: case 198:
2542: buf.append("\u00c6");
2543: break; // AE
2544: case 216:
2545: buf.append("\u014d");
2546: break; // o macron
2547: case 8734:
2548: buf.append("\u0129");
2549: break; // i tilde
2550: case 177:
2551: buf.append("\u00b1");
2552: break; // plusminus
2553: case 165:
2554: buf.append("\u012b");
2555: break; // i macron
2556: case 181:
2557: buf.append("\u1e43");
2558: break; // m underdot
2559: case 8706:
2560: buf.append("\u1e0d");
2561: break; // d underdot
2562: case 240:
2563: buf.append("\u1e0d");
2564: break; // d underdot
2565:
2566: case 8721:
2567: buf.append("\u1e63");
2568: break; // s underdot
2569: case 960:
2570: buf.append("\u017a");
2571: break; // z acute
2572: case 8747:
2573: buf.append("\u1e45");
2574: break; // n overdot
2575: case 937:
2576: buf.append("\u0169");
2577: break; // u tilde
2578: case 230:
2579: buf.append("\u00e6");
2580: break; // ae
2581: case 248:
2582: buf.append("\u00f8");
2583: break; // oslash
2584: case 191:
2585: buf.append("\u0304\u0306");
2586: break; // macron breve
2587: case 172:
2588: buf.append("\u1e37");
2589: break; //
2590: case 8730:
2591: buf.append("j\u0305");
2592: break; // j macron [does a single char exist?]
2593: case 402:
2594: buf.append("\u0103");
2595: break; // a breve
2596: case 8776:
2597: buf.append("\u016d");
2598: break; // u breve
2599: case 187:
2600: buf.append("\u1e42");
2601: break; // M underdot
2602: case 8230:
2603: buf.append("\u2026");
2604: break; // ellipsis
2605: case 192:
2606: buf.append("\u00c0");
2607: break; // A grave
2608: case 195:
2609: buf.append("\u00c3");
2610: break; // A tilde
2611: case 213:
2612: buf.append("\u00d5");
2613: break; // O tilde
2614: case 338:
2615: buf.append("m\u0306");
2616: break; // m breve
2617: case 339:
2618: buf.append("\u0153");
2619: break; // oe
2620: case 8211:
2621: buf.append("\u2013");
2622: break; // endash
2623: case 8212:
2624: buf.append("\u2014");
2625: break; // emdash
2626: case 8220:
2627: buf.append("\u201c");
2628: break; // quotedblleft
2629: case 8221:
2630: buf.append("\u201d");
2631: break; // quotedblright
2632: case 8216:
2633: buf.append("\u2018");
2634: break; // quoteleft
2635: case 8217:
2636: buf.append("\u2019");
2637: break; // quoteright
2638: case 247:
2639: buf.append("\u1e37");
2640: break; // l underring [actually underdot]
2641: case 9674:
2642: buf.append("\u1e41");
2643: break; // m overdot
2644: case 255:
2645: buf.append("n\u0306");
2646: break; // n breve
2647: case 376:
2648: buf.append("\u00d7");
2649: break; // multiply
2650: case 8364:
2651: buf.append("\u1e5b");
2652: break; // r underring [actually underdot]
2653: case 8249:
2654: buf.append("\u1e44");
2655: break; // N overdot
2656: case 8250:
2657: buf.append("\u1e62");
2658: break; // S underdot
2659: case 64257:
2660: buf.append("\u1e24");
2661: break; // H underdot
2662: case 64258:
2663: buf.append("\u1e0c");
2664: break; // D underdot
2665: case 8225:
2666: buf.append("\u2021");
2667: break; // daggerdbl
2668: case 8218:
2669: buf.append("\u1e36");
2670: break; // L underdot
2671: case 8222:
2672: buf.append("\u0113");
2673: break; // e macron
2674: case 194:
2675: buf.append("\u1e5f");
2676: break; // r underbar
2677: case 202:
2678: buf.append("r\u0324");
2679: break; // r underdieresis
2680: case 193:
2681: buf.append("\u012a");
2682: break; // I macron
2683: case 8486:
2684: case 203:
2685: buf.append("\u016b");
2686: break; // u macron
2687: case 200:
2688: buf.append("\u1e6c");
2689: break; // T underdot
2690: case 205:
2691: buf.append("\u1e64");
2692: break; // S acute
2693: case 206:
2694: buf.append("\u2020");
2695: break; // dagger
2696: case 207:
2697: buf.append("\u0115");
2698: break; // e breve
2699: case 204:
2700: buf.append("\u014f");
2701: break; // o breve
2702: case 211:
2703: buf.append("\u0100");
2704: break; // A macron
2705: case 212:
2706: buf.append("\u1e46");
2707: break; // N underdot
2708: case 210:
2709: buf.append("\u1e3b");
2710: break; // l underbar
2711: case 218:
2712: buf.append("\u016a");
2713: break; // U macron
2714: case 219:
2715: buf.append("\u0179");
2716: break; // Z acute
2717: case 217:
2718: buf.append("\u1e5a");
2719: break; // R underdot
2720: case 305:
2721: buf.append("\u0131");
2722: break; // dotlessi
2723: case 710:
2724: buf.append("\u1e47");
2725: break; // n underdot
2726: case 732:
2727: buf.append("\u1e49");
2728: break; // n underbar
2729: case 175:
2730: buf.append("\u0304");
2731: break; // macron
2732: case 728:
2733: buf.append("\u0306");
2734: break; // breve
2735: case 729:
2736: case 215:
2737: buf.append("\u1e25");
2738: break; // h underdot
2739: case 730:
2740: buf.append("\u012d");
2741: break; // i breve
2742: case 184:
2743: buf.append("\u0327");
2744: break; // cedilla
2745: case 733:
2746: buf.append("\u030b");
2747: break; // hungarumlaut
2748: case 731:
2749: buf.append("\u0328");
2750: break; // ogonek
2751: case 711:
2752: buf.append("\u030c");
2753: break; // caron
2754: case 199:
2755: buf.append("\u012b\u0303");
2756: break; // imacron tilde
2757: case 8226:
2758: buf.append("\u1e5d");
2759: break; // runderdot macron
2760: case 8482:
2761: buf.append("\u016b\0306");
2762: break; // umacron breve
2763: case 8804:
2764: buf.append("\u0101\u0301");
2765: break; // amacron acute
2766: case 8805:
2767: buf.append("\u016b\u0301");
2768: break; // umacron acute
2769: case 8719:
2770: buf.append("\u0113\u0301");
2771: break; // emacron acute
2772: case 170:
2773: buf.append("\u0113\u0300");
2774: break; // emacron breve
2775: case 186:
2776: buf.append("\u014d\u0300");
2777: break; // omacron breve
2778: case 161:
2779: buf.append("\u0101\u0306");
2780: break; // amacron breve
2781: case 8710:
2782: buf.append("\u0101\u0303");
2783: break; // amacron tilde
2784: case 171:
2785: buf.append("\u012b\u0301");
2786: break; // imacron acute
2787: case 8260:
2788: buf.append("\u1e00");
2789: break; // runderdotmacron acute
2790: case 183:
2791: buf.append("\u1e5b\u0301");
2792: break; // runderdot acute
2793: case 8240:
2794: buf.append("\u012b\u0306");
2795: break; // imacron breve
2796: case 63743:
2797: buf.append("\u016b\u0303");
2798: break; // umacron tilde
2799: default:
2800: buf.append(c);
2801: if ((int) c > 127)
2802: System.out.println("char " + c + " " + (int) c);
2803: break;
2804: }
2805: }
2806: return buf.toString();
2807: }
2808:
2809: static public String normanToUnicodeOld(String inp)
2810: {
2811: StringBuffer buf = new StringBuffer();
2812: for (int i = 0; i < inp.length(); i++)
2813: {
2814: char c = inp.charAt(i);
2815: switch (c)
2816: {
2817: case 1:
2818: buf.append("\u00d0");
2819: break; // Eth
2820: case 2:
2821: buf.append("\u00f0");
2822: break; // eth
2823: case 3:
2824: buf.append("\u0141");
2825: break; // Lslash
2826: case 4:
2827: buf.append("\u0142");
2828: break; // lslash
2829: case 5:
2830: buf.append("\u0160");
2831: break; // S caron
2832: case 6:
2833: buf.append("\u0161");
2834: break; // s caron
2835: case 7:
2836: buf.append("\u00dd");
2837: break; // Y acute
2838: case 8:
2839: buf.append("\u00fd");
2840: break; // y acute
2841: case 11:
2842: buf.append("\u00de");
2843: break; // Thorn
2844: case 12:
2845: buf.append("\u00fe");
2846: break; // thorn
2847: case 14:
2848: buf.append("\u017d");
2849: break; // Z caron
2850: case 15:
2851: buf.append("\u017e");
2852: break; // z caron
2853: case 17:
2854: buf.append("\u0073");
2855: break; // asciitilde
2856: case 18:
2857: buf.append("j\u0305");
2858: break; // j macron [does a single char exist?]
2859: case 19:
2860: buf.append("^");
2861: break; // circumflex
2862: case 20:
2863: buf.append("\u0303");
2864: break; // tilde
2865: case 21:
2866: buf.append("\u00bd");
2867: break; // onehalf
2868: case 22:
2869: buf.append("\u00bc");
2870: break; // onequarter
2871: case 23:
2872: buf.append("\u00b9");
2873: break; // onesuperior
2874: case 24:
2875: buf.append("\u00be");
2876: break; // threequarters
2877: case 25:
2878: buf.append("\u00b3");
2879: break; // threesuperior
2880: case 26:
2881: buf.append("\u00b2");
2882: break; // twosuperior
2883: case 27:
2884: buf.append("\u00a6");
2885: break; // brokenbar
2886: case 28:
2887: buf.append("-");
2888: break; // minus
2889: case 29:
2890: buf.append("\u00d7");
2891: break; // multiply
2892: case 39:
2893: buf.append("'");
2894: break; // quotesingle
2895: case 94:
2896: buf.append("\u0302");
2897: break; // circumflex
2898: case 96:
2899: buf.append("\u0300");
2900: break; // grave
2901: case 128:
2902: buf.append("\u00c4");
2903: break; // A dieresis
2904: case 129:
2905: buf.append("\u00c5");
2906: break; // A ring
2907: case 131:
2908: buf.append("\u00c9");
2909: break; // E acute
2910: case 132:
2911: buf.append("\u00d1");
2912: break; // N tilde
2913: case 133:
2914: buf.append("\u00d6");
2915: break; // O dieresis
2916: case 134:
2917: buf.append("\u00dc");
2918: break; // U dieresis
2919: case 135:
2920: buf.append("\u00e1");
2921: break; // a acute
2922: case 136:
2923: buf.append("\u00e0");
2924: break; // a grave
2925: case 137:
2926: buf.append("\u00e2");
2927: break; // a circumflex
2928: case 138:
2929: buf.append("\u00e4");
2930: break; // a dieresis
2931: case 139:
2932: buf.append("\u00e3");
2933: break; // a tilde
2934: case 140:
2935: buf.append("\u0101");
2936: break; // a macron
2937: case 141:
2938: buf.append("\u00e7");
2939: break; // c cedilla
2940: case 142:
2941: buf.append("\u00e9");
2942: break; // e acute
2943: case 143:
2944: buf.append("\u00e8");
2945: break; // e grave
2946: case 144:
2947: buf.append("\u00ea");
2948: break; // e circumflex
2949: case 145:
2950: buf.append("\u00eb");
2951: break; // e dieresis
2952: case 146:
2953: buf.append("\u00ed");
2954: break; // i acute
2955: case 147:
2956: buf.append("\u00ec");
2957: break; // i grave
2958: case 148:
2959: buf.append("\u00ee");
2960: break; // i circumflex
2961: case 149:
2962: buf.append("\u00ef");
2963: break; // i dieresis
2964: case 150:
2965: buf.append("\u00f1");
2966: break; // n tilde
2967: case 151:
2968: buf.append("\u00f3");
2969: break; // o acute
2970: case 152:
2971: buf.append("\u00f2");
2972: break; // o grave
2973: case 153:
2974: buf.append("\u00f4");
2975: break; // o circumflex
2976: case 154:
2977: buf.append("\u00f6");
2978: break; // o dieresis
2979: case 155:
2980: buf.append("\u00f5");
2981: break; // o tilde
2982: case 156:
2983: buf.append("\u00fa");
2984: break; // u acute
2985: case 157:
2986: buf.append("\u00f9");
2987: break; // u grave
2988: case 158:
2989: buf.append("\u00fb");
2990: break; // u circumflex
2991: case 159:
2992: buf.append("\u00fc");
2993: break; // u dieresis
2994: case 160:
2995: buf.append("\u1e6d");
2996: break; // t underdot
2997: case 161:
2998: buf.append("\u00b0");
2999: break; // degree
3000: case 162:
3001: buf.append("\u1ebd");
3002: break; // e tilde
3003: case 163:
3004: buf.append("\u00a3");
3005: break; // sterling
3006: case 164:
3007: buf.append("\u00a7");
3008: break; // section
3009: case 166:
3010: buf.append("\u00b6");
3011: break; // paragraph
3012: case 167:
3013: buf.append("\u015b");
3014: break; // s acute
3015: case 168:
3016: buf.append("\u1e5b");
3017: break; // r underdot
3018: case 169:
3019: buf.append("\u1e67");
3020: break; // s caron
3021: case 171:
3022: buf.append("\u0301");
3023: break; // acute
3024: case 172:
3025: buf.append("\u0308");
3026: break; // dieresis
3027: case 173:
3028: buf.append("\u1e6d");
3029: break; // t underdot
3030: case 174:
3031: buf.append("\u00c6");
3032: break; // AE
3033: case 175:
3034: buf.append("\u014d");
3035: break; // o macron
3036: case 176:
3037: buf.append("\u0129");
3038: break; // i tilde
3039: case 177:
3040: buf.append("\u00b1");
3041: break; // plusminus
3042: case 180:
3043: buf.append("\u012b");
3044: break; // i macron
3045: case 181:
3046: buf.append("\u1e43");
3047: break; // m underdot
3048: case 182:
3049: buf.append("\u1e0d");
3050: break; // d underdot
3051: case 183:
3052: buf.append("\u1e63");
3053: break; // s underdot
3054: case 185:
3055: buf.append("\u017a");
3056: break; // z acute
3057: case 186:
3058: buf.append("\u1e45");
3059: break; // n overdot
3060: case 189:
3061: buf.append("\u0169");
3062: break; // u tilde
3063: case 190:
3064: buf.append("\u00e6");
3065: break; // ae
3066: case 191:
3067: buf.append("\u00f8");
3068: break; // oslash
3069: case 192:
3070: buf.append("\u0304\u0306");
3071: break; // macron breve
3072: case 194:
3073: buf.append("\u1e37");
3074: break; //
3075: case 195:
3076: buf.append("j\u0305");
3077: break; // j macron [does a single char exist?]
3078: case 196:
3079: buf.append("\u0103");
3080: break; // a breve
3081: case 197:
3082: buf.append("\u016d");
3083: break; // u breve
3084: case 200:
3085: buf.append("\u1e42");
3086: break; // M underdot
3087: case 201:
3088: buf.append("\u2026");
3089: break; // ellipsis
3090: case 203:
3091: buf.append("\u00c0");
3092: break; // A grave
3093: case 204:
3094: buf.append("\u00c3");
3095: break; // A tilde
3096: case 205:
3097: buf.append("\u00d5");
3098: break; // O tilde
3099: case 206:
3100: buf.append("m\u0306");
3101: break; // m breve
3102: case 207:
3103: buf.append("\u0153");
3104: break; // oe
3105: case 208:
3106: buf.append("\u2013");
3107: break; // endash
3108: case 209:
3109: buf.append("\u2014");
3110: break; // emdash
3111: case 210:
3112: buf.append("\u201c");
3113: break; // quotedblleft
3114: case 211:
3115: buf.append("\u201d");
3116: break; // quotedblright
3117: case 212:
3118: buf.append("\u2018");
3119: break; // quoteleft
3120: case 213:
3121: buf.append("\u2019");
3122: break; // quoteright
3123: case 214:
3124: buf.append("\u1e37");
3125: break; // l underring [actually underdot]
3126: case 215:
3127: buf.append("\u1e41");
3128: break; // m overdot
3129: case 216:
3130: buf.append("n\u0306");
3131: break; // n breve
3132: case 217:
3133: buf.append("\u00d7");
3134: break; // multiply
3135: case 219:
3136: buf.append("\u1e5b");
3137: break; // r underring [actually underdot]
3138: case 220:
3139: buf.append("\u1e44");
3140: break; // N overdot
3141: case 221:
3142: buf.append("\u1e62");
3143: break; // S underdot
3144: case 222:
3145: buf.append("\u1e24");
3146: break; // H underdot
3147: case 223:
3148: buf.append("\u1e0c");
3149: break; // D underdot
3150: case 224:
3151: buf.append("\u2021");
3152: break; // daggerdbl
3153: case 226:
3154: buf.append("\u1e36");
3155: break; // L underdot
3156: case 227:
3157: buf.append("\u0113");
3158: break; // e macron
3159: case 229:
3160: buf.append("\u1e5f");
3161: break; // r underbar
3162: case 230:
3163: buf.append("r\u0324");
3164: break; // r underdieresis
3165: case 231:
3166: buf.append("\u012a");
3167: break; // I macron
3168: case 232:
3169: buf.append("\u016b");
3170: break; // u macron
3171: case 233:
3172: buf.append("\u01e6c");
3173: break; // T underdot
3174: case 234:
3175: buf.append("\u1e64");
3176: break; // S acute
3177: case 235:
3178: buf.append("\u2020");
3179: break; // dagger
3180: case 236:
3181: buf.append("\u0115");
3182: break; // e breve
3183: case 237:
3184: buf.append("\u014f");
3185: break; // o breve
3186: case 238:
3187: buf.append("\u0100");
3188: break; // A macron
3189: case 239:
3190: buf.append("\u1e46");
3191: break; // N underdot
3192: case 241:
3193: buf.append("\u1e3b");
3194: break; // l underbar
3195: case 242:
3196: buf.append("\u016a");
3197: break; // U macron
3198: case 243:
3199: buf.append("\u0179");
3200: break; // Z acute
3201: case 244:
3202: buf.append("\u1e5a");
3203: break; // R underdot
3204: case 245:
3205: buf.append("\u0131");
3206: break; // dotlessi
3207: case 246:
3208: buf.append("\u1e47");
3209: break; // n underdot
3210: case 247:
3211: buf.append("\u1e49");
3212: break; // n underbar
3213: case 248:
3214: buf.append("\u0304");
3215: break; // macron
3216: case 249:
3217: buf.append("\u0306");
3218: break; // breve
3219: case 250:
3220: buf.append("\u1e25");
3221: break; // h underdot
3222: case 251:
3223: buf.append("\u012d");
3224: break; // i breve
3225: case 252:
3226: buf.append("\u0327");
3227: break; // cedilla
3228: case 253:
3229: buf.append("\u030b");
3230: break; // hungarumlaut
3231: case 254:
3232: buf.append("\u0328");
3233: break; // ogonek
3234: case 255:
3235: buf.append("\u030c");
3236: break; // caron
3237: case 130:
3238: buf.append("\u012b\u0303");
3239: break; // imacron tilde
3240: case 165:
3241: buf.append("\u1e5d");
3242: break; // runderdot macron
3243: case 170:
3244: buf.append("\u016b\0306");
3245: break; // umacron breve
3246: case 178:
3247: buf.append("\u0101\u0301");
3248: break; // amacron acute
3249: case 179:
3250: buf.append("\u016b\u0301");
3251: break; // umacron acute
3252: case 184:
3253: buf.append("\u0113\u0301");
3254: break; // emacron acute
3255: case 187:
3256: buf.append("\u0113\u0300");
3257: break; // emacron breve
3258: case 188:
3259: buf.append("\u014d\u0300");
3260: break; // omacron breve
3261: case 193:
3262: buf.append("\u0101\u0306");
3263: break; // amacron breve
3264: case 198:
3265: buf.append("\u0101\u0303");
3266: break; // amacron tilde
3267: case 199:
3268: buf.append("\u012b\u0301");
3269: break; // imacron acute
3270: case 218:
3271: buf.append("\u1e00");
3272: break; // runderdotmacron acute
3273: case 225:
3274: buf.append("\u1e5b\u0301");
3275: break; // runderdot acute
3276: case 228:
3277: buf.append("\u012b\u0306");
3278: break; // imacron breve
3279: case 240:
3280: buf.append("\u016b\u0303");
3281: break; // umacron tilde
3282: default:
3283: buf.append(c);
3284: break;
3285: }
3286: }
3287: return buf.toString();
3288: }
3289:
3290: static public String normanToUnicodeNew(String inp)
3291: {
3292: StringBuffer buf = new StringBuffer();
3293: for (int i = 0; i < inp.length(); i++)
3294: {
3295: char c = inp.charAt(i);
3296: switch (c)
3297: {
3298: case 1:
3299: buf.append("\u00d0");
3300: break; // Eth
3301: case 2:
3302: buf.append("\u00f0");
3303: break; // eth
3304: case 3:
3305: buf.append("\u0141");
3306: break; // Lslash
3307: case 4:
3308: buf.append("\u0142");
3309: break; // lslash
3310: case 5:
3311: buf.append("\u0160");
3312: break; // S caron
3313: case 6:
3314: buf.append("\u0161");
3315: break; // s caron
3316: case 7:
3317: buf.append("\u00dd");
3318: break; // Y acute
3319: case 8:
3320: buf.append("\u00fd");
3321: break; // y acute
3322: case 11:
3323: buf.append("\u00de");
3324: break; // Thorn
3325: case 12:
3326: buf.append("\u00fe");
3327: break; // thorn
3328: case 14:
3329: buf.append("\u017d");
3330: break; // Z caron
3331: case 15:
3332: buf.append("\u017e");
3333: break; // z caron
3334: case 17:
3335: buf.append("\u0073");
3336: break; // asciitilde
3337: case 18:
3338: buf.append("j\u0305");
3339: break; // j macron [does a single char exist?]
3340: case 19:
3341: buf.append("^");
3342: break; // circumflex
3343: case 20:
3344: buf.append("\u0303");
3345: break; // tilde
3346: case 21:
3347: buf.append("\u00bd");
3348: break; // onehalf
3349: case 22:
3350: buf.append("\u00bc");
3351: break; // onequarter
3352: case 23:
3353: buf.append("\u00b9");
3354: break; // onesuperior
3355: case 24:
3356: buf.append("\u00be");
3357: break; // threequarters
3358: case 25:
3359: buf.append("\u00b3");
3360: break; // threesuperior
3361: case 26:
3362: buf.append("\u00b2");
3363: break; // twosuperior
3364: case 27:
3365: buf.append("\u00a6");
3366: break; // brokenbar
3367: case 28:
3368: buf.append("-");
3369: break; // minus
3370: case 29:
3371: buf.append("\u00d7");
3372: break; // multiply
3373: case 39:
3374: buf.append("'");
3375: break; // quotesingle
3376: case 94:
3377: buf.append("\u0302");
3378: break; // circumflex
3379: case 96:
3380: buf.append("\u0300");
3381: break; // grave
3382: case 196:
3383: buf.append("\u00c4");
3384: break; // A dieresis
3385: case 197:
3386: buf.append("\u00c5");
3387: break; // A ring
3388: case 201:
3389: buf.append("\u00c9");
3390: break; // E acute
3391: case 209:
3392: buf.append("\u00d1");
3393: break; // N tilde
3394: case 214:
3395: buf.append("\u00d6");
3396: break; // O dieresis
3397: case 220:
3398: buf.append("\u00dc");
3399: break; // U dieresis
3400: case 225:
3401: buf.append("\u00e1");
3402: break; // a acute
3403: case 224:
3404: buf.append("\u00e0");
3405: break; // a grave
3406: case 226:
3407: buf.append("\u00e2");
3408: break; // a circumflex
3409: case 228:
3410: buf.append("\u00e4");
3411: break; // a dieresis
3412: case 227:
3413: buf.append("\u00e3");
3414: break; // a tilde
3415: case 229:
3416: buf.append("\u0101");
3417: break; // a macron
3418: case 231:
3419: buf.append("\u00e7");
3420: break; // c cedilla
3421: case 233:
3422: buf.append("\u00e9");
3423: break; // e acute
3424: case 232:
3425: buf.append("\u00e8");
3426: break; // e grave
3427: case 234:
3428: buf.append("\u00ea");
3429: break; // e circumflex
3430: case 235:
3431: buf.append("\u00eb");
3432: break; // e dieresis
3433: case 237:
3434: buf.append("\u00ed");
3435: break; // i acute
3436: case 236:
3437: buf.append("\u00ec");
3438: break; // i grave
3439: case 238:
3440: buf.append("\u00ee");
3441: break; // i circumflex
3442: case 239:
3443: buf.append("\u00ef");
3444: break; // i dieresis
3445: case 241:
3446: buf.append("\u00f1");
3447: break; // n tilde
3448: case 243:
3449: buf.append("\u00f3");
3450: break; // o acute
3451: case 242:
3452: buf.append("\u00f2");
3453: break; // o grave
3454: case 244:
3455: buf.append("\u00f4");
3456: break; // o circumflex
3457: case 246:
3458: buf.append("\u00f6");
3459: break; // o dieresis
3460: case 245:
3461: buf.append("\u00f5");
3462: break; // o tilde
3463: case 250:
3464: buf.append("\u00fa");
3465: break; // u acute
3466: case 249:
3467: buf.append("\u00f9");
3468: break; // u grave
3469: case 251:
3470: buf.append("\u00fb");
3471: break; // u circumflex
3472: case 252:
3473: buf.append("\u00fc");
3474: break; // u dieresis
3475: case 8224:
3476: buf.append("\u1e6d");
3477: break; // t underdot
3478: case 176:
3479: buf.append("\u00b0");
3480: break; // degree
3481: case 162:
3482: buf.append("\u1ebd");
3483: break; // e tilde
3484: case 163:
3485: buf.append("\u00a3");
3486: break; // sterling
3487: case 167:
3488: buf.append("\u00a7");
3489: break; // section
3490: case 182:
3491: buf.append("\u00b6");
3492: break; // paragraph
3493: case 223:
3494: buf.append("\u015b");
3495: break; // s acute
3496: case 174:
3497: buf.append("\u1e5b");
3498: break; // r underdot
3499: case 169:
3500: buf.append("\u1e45");
3501: break; // n overdot
3502: case 180:
3503: buf.append("\u0301");
3504: break; // acute
3505: case 168:
3506: buf.append("\u0308");
3507: break; // dieresis
3508: case 8800:
3509: buf.append("\u1e6d");
3510: break; // t underdot
3511: case 198:
3512: buf.append("\u00c6");
3513: break; // AE
3514: case 216:
3515: buf.append("\u014d");
3516: break; // o macron
3517: case 8734:
3518: buf.append("\u0129");
3519: break; // i tilde
3520: case 177:
3521: buf.append("\u00b1");
3522: break; // plusminus
3523: case 165:
3524: buf.append("\u012b");
3525: break; // i macron
3526: case 181:
3527: buf.append("\u1e43");
3528: break; // m underdot
3529: case 8706:
3530: buf.append("\u1e0d");
3531: break; // d underdot
3532: case 8721:
3533: buf.append("\u1e63");
3534: break; // s underdot
3535: case 960:
3536: buf.append("\u017a");
3537: break; // z acute
3538: case 8747:
3539: buf.append("\u1e45");
3540: break; // n overdot
3541: case 937:
3542: buf.append("\u0169");
3543: break; // u tilde
3544: case 230:
3545: buf.append("\u00e6");
3546: break; // ae
3547: case 248:
3548: buf.append("\u00f8");
3549: break; // oslash
3550: case 191:
3551: buf.append("\u0304\u0306");
3552: break; // macron breve
3553: case 172:
3554: buf.append("\u1e37");
3555: break; //
3556: case 8730:
3557: buf.append("j\u0305");
3558: break; // j macron [does a single char exist?]
3559: case 402:
3560: buf.append("\u0103");
3561: break; // a breve
3562: case 8776:
3563: buf.append("\u016d");
3564: break; // u breve
3565: case 187:
3566: buf.append("\u1e42");
3567: break; // M underdot
3568: case 8230:
3569: buf.append("\u2026");
3570: break; // ellipsis
3571: case 192:
3572: buf.append("\u00c0");
3573: break; // A grave
3574: case 195:
3575: buf.append("\u00c3");
3576: break; // A tilde
3577: case 213:
3578: buf.append("\u00d5");
3579: break; // O tilde
3580: case 338:
3581: buf.append("m\u0306");
3582: break; // m breve
3583: case 339:
3584: buf.append("\u0153");
3585: break; // oe
3586: case 8211:
3587: buf.append("\u2013");
3588: break; // endash
3589: case 8212:
3590: buf.append("\u2014");
3591: break; // emdash
3592: case 8220:
3593: buf.append("\u201c");
3594: break; // quotedblleft
3595: case 8221:
3596: buf.append("\u201d");
3597: break; // quotedblright
3598: case 8216:
3599: buf.append("\u2018");
3600: break; // quoteleft
3601: case 8217:
3602: buf.append("\u2019");
3603: break; // quoteright
3604: case 247:
3605: buf.append("\u1e37");
3606: break; // l underring [actually underdot]
3607: case 9674:
3608: buf.append("\u1e41");
3609: break; // m overdot
3610: case 255:
3611: buf.append("n\u0306");
3612: break; // n breve
3613: case 376:
3614: buf.append("\u00d7");
3615: break; // multiply
3616: case 8364:
3617: buf.append("\u1e5b");
3618: break; // r underring [actually underdot]
3619: case 8249:
3620: buf.append("\u1e44");
3621: break; // N overdot
3622: case 8250:
3623: buf.append("\u1e62");
3624: break; // S underdot
3625: case 64257:
3626: buf.append("\u1e24");
3627: break; // H underdot
3628: case 64258:
3629: buf.append("\u1e0c");
3630: break; // D underdot
3631: case 8225:
3632: buf.append("\u2021");
3633: break; // daggerdbl
3634: case 8218:
3635: buf.append("\u1e36");
3636: break; // L underdot
3637: case 8222:
3638: buf.append("\u0113");
3639: break; // e macron
3640: case 194:
3641: buf.append("\u1e5f");
3642: break; // r underbar
3643: case 202:
3644: buf.append("r\u0324");
3645: break; // r underdieresis
3646: case 193:
3647: buf.append("\u012a");
3648: break; // I macron
3649: case 203:
3650: buf.append("\u016b");
3651: break; // u macron
3652: case 200:
3653: buf.append("\u1e6c");
3654: break; // T underdot
3655: case 205:
3656: buf.append("\u1e64");
3657: break; // S acute
3658: case 206:
3659: buf.append("\u2020");
3660: break; // dagger
3661: case 207:
3662: buf.append("\u0115");
3663: break; // e breve
3664: case 204:
3665: buf.append("\u014f");
3666: break; // o breve
3667: case 211:
3668: buf.append("\u0100");
3669: break; // A macron
3670: case 212:
3671: buf.append("\u1e46");
3672: break; // N underdot
3673: case 210:
3674: buf.append("\u1e3b");
3675: break; // l underbar
3676: case 218:
3677: buf.append("\u016a");
3678: break; // U macron
3679: case 219:
3680: buf.append("\u0179");
3681: break; // Z acute
3682: case 217:
3683: buf.append("\u1e5a");
3684: break; // R underdot
3685: case 305:
3686: buf.append("\u0131");
3687: break; // dotlessi
3688: case 710:
3689: buf.append("\u1e47");
3690: break; // n underdot
3691: case 732:
3692: buf.append("\u1e49");
3693: break; // n underbar
3694: case 175:
3695: buf.append("\u0304");
3696: break; // macron
3697: case 728:
3698: buf.append("\u0306");
3699: break; // breve
3700: case 729:
3701: buf.append("\u1e25");
3702: break; // h underdot
3703: case 730:
3704: buf.append("\u012d");
3705: break; // i breve
3706: case 184:
3707: buf.append("\u0327");
3708: break; // cedilla
3709: case 733:
3710: buf.append("\u030b");
3711: break; // hungarumlaut
3712: case 731:
3713: buf.append("\u0328");
3714: break; // ogonek
3715: case 711:
3716: buf.append("\u030c");
3717: break; // caron
3718: case 199:
3719: buf.append("\u012b\u0303");
3720: break; // imacron tilde
3721: case 8226:
3722: buf.append("\u1e5d");
3723: break; // runderdot macron
3724: case 8482:
3725: buf.append("\u016b\0306");
3726: break; // umacron breve
3727: case 8804:
3728: buf.append("\u0101\u0301");
3729: break; // amacron acute
3730: case 8805:
3731: buf.append("\u016b\u0301");
3732: break; // umacron acute
3733: case 8719:
3734: buf.append("\u0113\u0301");
3735: break; // emacron acute
3736: case 170:
3737: buf.append("\u0113\u0300");
3738: break; // emacron breve
3739: case 186:
3740: buf.append("\u014d\u0300");
3741: break; // omacron breve
3742: case 161:
3743: buf.append("\u0101\u0306");
3744: break; // amacron breve
3745: case 8710:
3746: buf.append("\u0101\u0303");
3747: break; // amacron tilde
3748: case 171:
3749: buf.append("\u012b\u0301");
3750: break; // imacron acute
3751: case 8260:
3752: buf.append("\u1e00");
3753: break; // runderdotmacron acute
3754: case 183:
3755: buf.append("\u1e5b\u0301");
3756: break; // runderdot acute
3757: case 8240:
3758: buf.append("\u012b\u0306");
3759: break; // imacron breve
3760: case 63743:
3761: buf.append("\u016b\u0303");
3762: break; // umacron tilde
3763: default:
3764: buf.append(c);
3765: break;
3766: }
3767: }
3768: return buf.toString();
3769: }
3770:
3771: public static ConversionProperties getFieldNamesAndDestTableName(String create, String query,
3772: String tableName)
3773: {
3774: String[] fieldNames = null;
3775: String destTableName = null;
3776: // determine destTableName from createStatement or from source table
3777: // name
3778: if (!create.equals(""))
3779: {
3780: int fromIndex = create.toLowerCase().indexOf("table") + 5;
3781: int toIndex = create.indexOf("(");
3782: int endIndex = create.indexOf(")", toIndex);
3783:
3784: destTableName = create.substring(fromIndex, toIndex).replaceAll(beanDest.getQC(), "").trim();
3785: System.out.println("destTable " + destTableName);
3786: // retrieve field_names from select statement
3787: // TODO problem with different fieldNames in create statement will
3788: // overwrite them
3789: if (query.indexOf("*") < 0 && create.equals(""))// quick hack for hartmut
3790: {
3791: int selectEndIndex = query.indexOf("from");
3792: StringTokenizer tokenizer = new StringTokenizer(query.substring(6, selectEndIndex), ",");
3793: int numFields = tokenizer.countTokens();
3794: fieldNames = new String[numFields];
3795: int fieldIndex = 0;
3796: while (tokenizer.hasMoreTokens())
3797: {
3798: String fieldName = tokenizer.nextToken().trim();
3799: fieldNames[fieldIndex] = convertText(fieldName);
3800: System.out.println(fieldNames[fieldIndex]);
3801: fieldIndex++;
3802: }
3803:
3804: } else
3805: {
3806: // use create statement for field names
3807: StringTokenizer tokenizer = new StringTokenizer(create.substring(toIndex + 1, endIndex),
3808: ",");
3809: int numFields = tokenizer.countTokens();
3810: fieldNames = new String[numFields];
3811: int fieldIndex = 0;
3812: while (tokenizer.hasMoreTokens())
3813: {
3814: String fieldName = tokenizer.nextToken().trim();
3815: int index = fieldName.lastIndexOf(" ");
3816: fieldNames[fieldIndex] = fieldName.substring(0, index);
3817: System.out.println(fieldNames[fieldIndex]);
3818: fieldIndex++;
3819: }
3820: }
3821: } else
3822: {
3823: destTableName = convertText(tableName);
3824:
3825: // retrieve field_names from select statement
3826: if (query.indexOf("*") < 0)
3827: {
3828: int selectEndIndex = query.lastIndexOf("from");
3829: StringTokenizer tokenizer = new StringTokenizer(query.substring(6, selectEndIndex), ",");
3830: int numFields = tokenizer.countTokens();
3831: fieldNames = new String[numFields];
3832: int fieldIndex = 0;
3833: while (tokenizer.hasMoreTokens())
3834: {
3835: String fieldName = tokenizer.nextToken().trim();
3836: String text = convertText(fieldName);
3837: if (text.indexOf("\"") >= 0)
3838: fieldNames[fieldIndex] = convertText(fieldName);
3839: else
3840: fieldNames[fieldIndex] = beanDest.getQC() + convertText(fieldName) + beanDest.getQC();
3841: // System.out.println("field "+ fieldNames[fieldIndex]);
3842: fieldIndex++;
3843: }
3844:
3845: } else
3846: {
3847: Vector fieldNamesVec = bean.getColumnNames();
3848: fieldNames = new String[fieldNamesVec.size()];
3849: int fieldIndex = -1;
3850: for (Iterator iter = fieldNamesVec.iterator(); iter.hasNext();)
3851: {
3852: String element = (String) iter.next();
3853: fieldNames[++fieldIndex] = beanDest.getQC() + convertText(element) + beanDest.getQC();
3854: // System.out.println("field " + fieldNames[fieldIndex]);
3855: }
3856: }
3857: }
3858: return new ConversionProperties(destTableName, fieldNames);
3859: }
3860:
3861: /**
3862: * creates an insert into statement for the specified table and given field
3863: * names
3864: *
3865: * @param destTableName
3866: * @param fieldNames
3867: * @return
3868: */
3869: public static StringBuffer createInsertCommand(String destTableName, String[] fieldNames)
3870: {
3871: StringBuffer command = new StringBuffer();
3872: command.append("INSERT INTO ");
3873: command.append(beanDest.getQC());
3874: command.append(destTableName); // convertText((String)
3875: // names.get(tbIndex)));
3876: command.append(beanDest.getQC());
3877: command.append(" (");
3878: for (int i = 0; i < fieldNames.length; i++)
3879: {
3880: command.append(fieldNames[i]);
3881: if (i < fieldNames.length - 1)
3882: command.append(",");
3883: }
3884: command.append(") ");
3885:
3886: command.append(" values ( ");
3887: // add a question marks for every field
3888: for (int i = 0; i < fieldNames.length - 1; ++i)
3889: command.append("?,");
3890: command.append("?)");
3891: return command;
3892: }
3893:
3894: public static StringBuffer createUpdateCommand(String destTableName, String[] fieldNames,
3895: String id)
3896: {
3897: StringBuffer command = new StringBuffer();
3898:
3899: command.append("UPDATE ");
3900: command.append(beanDest.getQC());
3901: command.append(destTableName);
3902: command.append(beanDest.getQC());
3903: command.append(" SET ");
3904:
3905: int size = bean.getColumnNames().size();
3906: for (int i = 0; i < size - 1; ++i)
3907: command.append(fieldNames[i] + " = ? ,");
3908: command.append(fieldNames[size - 1] + " = ? ");
3909: command.append("WHERE " + id + " = ?");
3910: return command;
3911: }
3912:
3913: public static StringBuffer createDeleteCommand(String destTableName, String idField)
3914: {
3915: StringBuffer command = new StringBuffer();
3916:
3917: command.append("DELETE FROM");
3918: command.append(beanDest.getQC());
3919: command.append(destTableName);
3920: // command.append(convertText((String) names.get(tbIndex)));
3921: command.append(beanDest.getQC());
3922: command.append("WHERE " + idField + " = ?");
3923: return command;
3924: }
3925:
3926: public void makeTest(String table, String idField, String tempQuery) throws Exception
3927: {
3928: int counter = 0;
3929:
3930: // ****** test code *****
3931:
3932: bean.getConnection();
3933: ResultSet resultSet = null;
3934: String lastResult = "P227634.11";// "P227625.79554";//"P227625.77391";//"P116034.970998";
3935: String myQuery = "select " + bean.getQC() + idField + bean.getQC() + ",serial " + " from "
3936: + bean.getQC() + table + bean.getQC();
3937: System.out.println("Query is now " + myQuery);
3938: JDialog statusDialog = new JDialog();
3939: statusDialog.setTitle("Status Information");
3940: JLabel status = new JLabel("actual DataSet : ");
3941: JLabel status2 = new JLabel(Integer.toString(++counter));
3942: JLabel status3 = new JLabel(lastResult);
3943:
3944: JPanel statusPanel = new JPanel();
3945: JPanel statusPanel2 = new JPanel();
3946: statusPanel.add(status);
3947: statusPanel.add(status2);
3948: statusPanel2.add(status3);
3949: statusDialog.getContentPane().add(statusPanel, "North");
3950: statusDialog.getContentPane().add(statusPanel2, "Center");
3951: statusDialog.setLocation(400, 500);
3952: statusDialog.setSize(300, 150);
3953: statusDialog.setVisible(true);
3954: while (true)
3955: {
3956: if (!statusDialog.isVisible())
3957: statusDialog.setVisible(true);
3958: tempQuery = myQuery + " where " + bean.getQC() + idField + bean.getQC() + ">'" + lastResult
3959: + "'";
3960: resultSet = bean.makeQuery(tempQuery, 1);
3961: if (resultSet == null)
3962: {
3963: System.out.println("lastResult was " + lastResult + " counter was " + counter);
3964: break;
3965: } else
3966: {
3967: resultSet.next();
3968: lastResult = resultSet.getString(1);
3969: counter++;
3970: status2.setText(Integer.toString(counter));
3971: status3.setText(lastResult + " " + resultSet.getString(2));
3972: if (counter % 100 == 0)
3973: {
3974: System.out.println("actual Result was " + lastResult + " counter was " + counter);
3975: // break;
3976: }
3977: }
3978: resultSet = null;
3979: }
3980: System.exit(0);
3981:
3982: // ****** end Test ******
3983:
3984: }
3985:
3986: public final static String generateSuffix(final int step)
3987: {
3988: String fileString = null;
3989: if (step < 10)
3990: fileString = "00" + step;
3991: else if (step < 100)
3992: fileString = "0" + step;
3993: else
3994: fileString = step + "";
3995: return fileString;
3996: }
3997:
3998: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>