KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > util > Transfer


1 /* Copyright (c) 1995-2000, The Hypersonic SQL Group.
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the Hypersonic SQL Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE HYPERSONIC SQL GROUP,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * This software consists of voluntary contributions made by many individuals
31  * on behalf of the Hypersonic SQL Group.
32  *
33  *
34  * For work added by the HSQL Development Group:
35  *
36  * Copyright (c) 2001-2005, The HSQL Development Group
37  * All rights reserved.
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions are met:
41  *
42  * Redistributions of source code must retain the above copyright notice, this
43  * list of conditions and the following disclaimer.
44  *
45  * Redistributions in binary form must reproduce the above copyright notice,
46  * this list of conditions and the following disclaimer in the documentation
47  * and/or other materials provided with the distribution.
48  *
49  * Neither the name of the HSQL Development Group nor the names of its
50  * contributors may be used to endorse or promote products derived from this
51  * software without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
54  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
57  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
58  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
59  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
60  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
61  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
62  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
63  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64  */

65
66
67 package org.hsqldb.util;
68
69 import java.applet.Applet JavaDoc;
70 import java.util.Enumeration JavaDoc;
71 import java.util.Vector JavaDoc;
72
73 import java.awt.BorderLayout JavaDoc;
74 import java.awt.Button JavaDoc;
75 import java.awt.Checkbox JavaDoc;
76 import java.awt.Component JavaDoc;
77 import java.awt.Dimension JavaDoc;
78 import java.awt.FileDialog JavaDoc;
79 import java.awt.Font JavaDoc;
80 import java.awt.Frame JavaDoc;
81 import java.awt.GridLayout JavaDoc;
82 import java.awt.Image JavaDoc;
83 import java.awt.ItemSelectable JavaDoc;
84 import java.awt.Label JavaDoc;
85 import java.awt.Menu JavaDoc;
86 import java.awt.MenuBar JavaDoc;
87 import java.awt.MenuItem JavaDoc;
88 import java.awt.Panel JavaDoc;
89 import java.awt.SystemColor JavaDoc;
90 import java.awt.TextField JavaDoc;
91 import java.awt.Toolkit JavaDoc;
92 import java.awt.event.ActionEvent JavaDoc;
93 import java.awt.event.ActionListener JavaDoc;
94 import java.awt.event.ItemEvent JavaDoc;
95 import java.awt.event.ItemListener JavaDoc;
96 import java.awt.event.WindowEvent JavaDoc;
97 import java.awt.event.WindowListener JavaDoc;
98 import java.awt.image.MemoryImageSource JavaDoc;
99
100 // fredt@users 20011220 - patch 481239 by xponsard@users - enhancements
101
// enhancements to support saving and loading of transfer settings,
102
// transfer of blobs, and catalog and schema names in source db
103
// changes by fredt to allow saving and loading of transfer settings
104
// fredt@users 20020215 - patch 516309 by Nicolas Bazin - enhancements
105
// sqlbob@users 20020401 - patch 1.7.0 - reengineering
106
// nicolas BAZIN 20020430 - add Catalog selection, correct a bug preventing table
107
// edition, change double quotes to simple quotes for default values of CHAR type
108
// lonbinder@users 20030426 - correct bug in prefs load/save
109
// fredt@users 20040508 - patch 1.7.2 - bug fixes
110

111 /**
112  * Utility program (or applet) for transferring tables between different
113  * databases via JDBC. Understands HSQLDB database particularly well.
114  *
115  * @author Thomas Mueller (Hypersonic SQL Group)
116   * @version 1.7.2
117   * @since Hypersonic SQL
118  */

119 public class Transfer extends Applet JavaDoc
120 implements WindowListener JavaDoc, ActionListener JavaDoc, ItemListener JavaDoc, Traceable {
121
122     Frame JavaDoc fMain;
123     Image JavaDoc imgEmpty;
124     DataAccessPoint sourceDb;
125     DataAccessPoint targetDb;
126     TransferTable tCurrent;
127     int iMaxRows;
128     int iSelectionStep;
129     Vector JavaDoc tTable;
130     java.awt.List JavaDoc lTable;
131     String JavaDoc[] sSourceSchemas;
132     String JavaDoc sSourceCatalog, sDestSchema, sDestCatalog;
133     TextField JavaDoc tSourceTable, tDestTable, tDestDropIndex, tDestCreateIndex;
134     TextField JavaDoc tDestDrop, tDestCreate, tDestDelete, tDestAlter;
135     TextField JavaDoc tSourceSelect, tDestInsert;
136     Checkbox JavaDoc cTransfer, cDrop, cCreate, cDelete, cInsert, cAlter;
137     Checkbox JavaDoc cCreateIndex, cDropIndex;
138     Checkbox JavaDoc cFKForced, cIdxForced;
139     Button JavaDoc bStart, bContinue;
140     TextField JavaDoc tMessage;
141     int iTransferMode;
142     static boolean bMustExit;
143     int CurrentTransfer, CurrentAlter;
144     static final int SELECT_SOURCE_CATALOG = 1;
145     static final int SELECT_SOURCE_SCHEMA = 2;
146     static final int SELECT_DEST_CATALOG = 3;
147     static final int SELECT_DEST_SCHEMA = 4;
148     static final int SELECT_SOURCE_TABLES = 5;
149     static final int TRFM_TRANSFER = 1;
150     static final int TRFM_DUMP = 2;
151     static final int TRFM_RESTORE = 3;
152
153     /**
154      * Method declaration
155      *
156      *
157      * @param s
158      */

159     public void trace(String JavaDoc s) {
160
161         if ((s != null) &&!s.equals("")) {
162             tMessage.setText(s);
163
164             if (TRACE) {
165                 System.out.println(s);
166             }
167         }
168     }
169
170     /**
171      * Method declaration
172      *
173      */

174     public void init() {
175
176         Transfer m = new Transfer();
177
178         m._main(null);
179     }
180
181     /**
182      * Method declaration
183      *
184      */

185     public static void work(String JavaDoc[] arg) {
186
187         Transfer m = new Transfer();
188
189         m._main(arg);
190     }
191
192     /**
193      * Method declaration
194      *
195      *
196      * @param arg
197      */

198     public static void main(String JavaDoc[] arg) {
199
200         System.getProperties().put("sun.java2d.noddraw", "true");
201
202         bMustExit = true;
203
204         work(arg);
205     }
206
207     private boolean CatalogToSelect() {
208
209         Vector JavaDoc result = null;
210
211         try {
212             lTable.removeAll();
213
214             if (iSelectionStep == Transfer.SELECT_SOURCE_CATALOG) {
215                 result = sourceDb.getCatalog();
216             } else if (iSelectionStep == Transfer.SELECT_DEST_CATALOG) {
217                 result = targetDb.getCatalog();
218             } else {
219                 Exit();
220             }
221
222             if (result.size() > 1) {
223                 lTable.setMultipleMode(true);
224
225                 if (iSelectionStep == Transfer.SELECT_SOURCE_CATALOG) {
226                     bStart.setLabel("Select Catalog: Source");
227                 } else {
228                     bStart.setLabel("Select Catalog: Destination");
229                 }
230
231                 bStart.invalidate();
232                 bStart.setEnabled(true);
233
234                 for (Enumeration JavaDoc e =
235                         result.elements(); e.hasMoreElements(); ) {
236                     lTable.add(e.nextElement().toString());
237                 }
238
239                 lTable.repaint();
240                 trace("Select correct Catalog");
241             } else {
242                 if (result.size() == 1) {
243                     if (iSelectionStep == Transfer.SELECT_SOURCE_CATALOG) {
244                         sSourceCatalog = (String JavaDoc) result.firstElement();
245                         sSourceSchemas = null;
246                     } else {
247                         sDestCatalog = (String JavaDoc) result.firstElement();
248                         sDestSchema = null;
249                     }
250                 } else {
251                     if (iSelectionStep == Transfer.SELECT_SOURCE_CATALOG) {
252                         sSourceCatalog = null;
253                         sSourceSchemas = null;
254                     } else {
255                         sDestCatalog = null;
256                         sDestSchema = null;
257                     }
258                 }
259
260                 if ((iSelectionStep == Transfer.SELECT_DEST_CATALOG)
261                         && (sDestCatalog != null)) {
262                     try {
263                         targetDb.setCatalog(sDestCatalog);
264                     } catch (Exception JavaDoc ex) {
265                         trace("Catalog " + sSourceCatalog
266                               + " could not be selected in the target database");
267
268                         sSourceCatalog = null;
269                     }
270                 }
271
272                 iSelectionStep++;
273
274                 ProcessNextStep();
275
276                 return false;
277             }
278         } catch (Exception JavaDoc exp) {
279             lTable.removeAll();
280             trace("Exception reading catalog: " + exp);
281             exp.printStackTrace();
282         }
283
284         return (lTable.getItemCount() > 0);
285     }
286
287     private boolean SchemaToSelect() {
288
289         Vector JavaDoc result = null;
290
291         try {
292             lTable.removeAll();
293
294             if (iSelectionStep == Transfer.SELECT_SOURCE_SCHEMA) {
295                 result = sourceDb.getSchemas();
296             } else if (iSelectionStep == Transfer.SELECT_DEST_SCHEMA) {
297                 result = targetDb.getSchemas();
298             } else {
299                 Exit();
300             }
301
302             if (result.size() > 1) {
303                 lTable.setMultipleMode(true);
304
305                 if (iSelectionStep == Transfer.SELECT_SOURCE_SCHEMA) {
306                     bStart.setLabel("Select Schema: Source");
307                 } else {
308                     bStart.setLabel("Select Schema: Destination");
309                 }
310
311                 bStart.invalidate();
312                 bStart.setEnabled(true);
313
314                 for (Enumeration JavaDoc e =
315                         result.elements(); e.hasMoreElements(); ) {
316                     lTable.add(e.nextElement().toString());
317                 }
318
319                 lTable.repaint();
320                 trace("Select correct Schema or load Settings file");
321             } else {
322                 if (result.size() == 1) {
323                     if (iSelectionStep == Transfer.SELECT_SOURCE_SCHEMA) {
324                         sSourceSchemas = new String JavaDoc[1];
325                         sSourceSchemas[0] = (String JavaDoc) result.firstElement();
326                     } else {
327                         sDestSchema = (String JavaDoc) result.firstElement();
328                     }
329                 } else {
330                     if (iSelectionStep == Transfer.SELECT_SOURCE_SCHEMA) {
331                         sSourceSchemas = null;
332                     } else {
333                         sDestSchema = null;
334                     }
335                 }
336
337                 if (iTransferMode == TRFM_DUMP) {
338                     iSelectionStep = Transfer.SELECT_SOURCE_TABLES;
339                 } else {
340                     iSelectionStep++;
341                 }
342
343                 ProcessNextStep();
344
345                 return false;
346             }
347         } catch (Exception JavaDoc exp) {
348             lTable.removeAll();
349             trace("Exception reading schemas: " + exp);
350             exp.printStackTrace();
351         }
352
353         return (lTable.getItemCount() > 0);
354     }
355
356     /**
357      * Method declaration
358      *
359      */

360     void _main(String JavaDoc[] arg) {
361
362         /*
363          ** What function is asked from the transfer tool?
364          */

365         iTransferMode = TRFM_TRANSFER;
366
367         if ((arg != null) && (arg.length > 0)) {
368             if ((arg[0].toLowerCase().equals("-r"))
369                     || (arg[0].toLowerCase().equals("--restore"))) {
370                 iTransferMode = TRFM_RESTORE;
371             } else if ((arg[0].toLowerCase().equals("-d"))
372                        || (arg[0].toLowerCase().equals("--dump"))) {
373                 iTransferMode = TRFM_DUMP;
374             }
375         }
376
377         fMain = new Frame JavaDoc("HSQL Transfer Tool");
378         imgEmpty = createImage(new MemoryImageSource JavaDoc(2, 2, new int[4 * 4], 2,
379                 2));
380
381         fMain.setIconImage(imgEmpty);
382         fMain.addWindowListener(this);
383         fMain.setSize(640, 480);
384         fMain.add("Center", this);
385
386         MenuBar JavaDoc bar = new MenuBar JavaDoc();
387         String JavaDoc[] extras = {
388             "Insert 10 rows only", "Insert 1000 rows only", "Insert all rows",
389             "-", "Load Settings...", "Save Settings...", "-", "Exit"
390         };
391         Menu JavaDoc menu = new Menu JavaDoc("Options");
392
393         addMenuItems(menu, extras);
394         bar.add(menu);
395         fMain.setMenuBar(bar);
396         initGUI();
397
398         Dimension JavaDoc d = Toolkit.getDefaultToolkit().getScreenSize();
399         Dimension JavaDoc size = fMain.getSize();
400
401         // (ulrivo): full size on screen with less than 640 width
402
if (d.width >= 640) {
403             fMain.setLocation((d.width - size.width) / 2,
404                               (d.height - size.height) / 2);
405         } else {
406             fMain.setLocation(0, 0);
407             fMain.setSize(d);
408         }
409
410         fMain.setVisible(true);
411
412         CurrentTransfer = CurrentAlter = 0;
413
414         try {
415             if ((iTransferMode == TRFM_DUMP)
416                     || (iTransferMode == TRFM_TRANSFER)) {
417                 sourceDb = new TransferDb(
418                     ConnectionDialog.createConnection(
419                         fMain, "Source Database"), this);
420
421                 if (!sourceDb.isConnected()) {
422                     Exit();
423
424                     return;
425                 }
426             } else {
427                 FileDialog JavaDoc f = new FileDialog JavaDoc(fMain, "Restore FileName",
428                                               FileDialog.LOAD);
429
430                 f.show();
431
432                 String JavaDoc sFileName = f.getFile();
433                 String JavaDoc Path = f.getDirectory();
434
435                 if ((sFileName == null) || (sFileName.equals(""))) {
436                     Exit();
437
438                     return;
439                 } else {
440                     sourceDb = new TransferSQLText(Path + sFileName, this);
441                 }
442             }
443
444             if ((iTransferMode == TRFM_RESTORE)
445                     || (iTransferMode == TRFM_TRANSFER)) {
446                 targetDb = new TransferDb(
447                     ConnectionDialog.createConnection(
448                         fMain, "Target Database"), this);
449
450                 if (!targetDb.isConnected()) {
451                     Exit();
452
453                     return;
454                 }
455             } else {
456                 FileDialog JavaDoc f = new FileDialog JavaDoc(fMain, "Dump FileName",
457                                               FileDialog.SAVE);
458
459                 f.show();
460
461                 String JavaDoc sFileName = f.getFile();
462                 String JavaDoc Path = f.getDirectory();
463
464                 if ((sFileName == null) || (sFileName.equals(""))) {
465                     Exit();
466
467                     return;
468                 } else {
469                     targetDb = new TransferSQLText(Path + sFileName, this);
470                 }
471             }
472         } catch (Exception JavaDoc e) {
473             Exit();
474             e.printStackTrace();
475
476             return;
477         }
478
479         if ((iTransferMode == TRFM_DUMP)
480                 || (iTransferMode == TRFM_TRANSFER)) {
481             iSelectionStep = SELECT_SOURCE_CATALOG;
482             sSourceCatalog = null;
483         } else {
484             iSelectionStep = SELECT_DEST_CATALOG;
485             sDestCatalog = null;
486         }
487
488         ProcessNextStep();
489         fMain.show();
490
491         return;
492     }
493
494     private void RefreshMainDisplay() {
495
496         lTable.removeAll();
497         lTable.repaint();
498
499         try {
500             tTable = sourceDb.getTables(sSourceCatalog, sSourceSchemas);
501
502             for (int i = 0; i < tTable.size(); i++) {
503                 TransferTable t = (TransferTable) tTable.elementAt(i);
504
505                 t.setDest(sDestSchema, targetDb);
506                 t.extractTableStructure(sourceDb, targetDb);
507                 lTable.add(t.Stmts.sSourceTable);
508                 lTable.select(i);
509                 displayTable(t);
510             }
511
512             bStart.setEnabled(true);
513
514             if (iTransferMode == TRFM_TRANSFER) {
515                 trace("Edit definitions and press [Start Transfer]");
516             } else if (iTransferMode == TRFM_DUMP) {
517                 trace("Edit definitions and press [Start Dump]");
518             }
519         } catch (Exception JavaDoc e) {
520             trace("Exception reading source tables: " + e);
521             e.printStackTrace();
522         }
523
524         fMain.show();
525     }
526
527     /**
528      * Method declaration
529      *
530      *
531      * @param f
532      * @param m
533      */

534     private void addMenuItems(Menu JavaDoc f, String JavaDoc[] m) {
535
536         for (int i = 0; i < m.length; i++) {
537             if (m[i].equals("-")) {
538                 f.addSeparator();
539             } else {
540                 MenuItem JavaDoc item = new MenuItem JavaDoc(m[i]);
541
542                 item.addActionListener(this);
543                 f.add(item);
544             }
545         }
546     }
547
548     /**
549      * Method declaration
550      *
551      *
552      * @param e
553      */

554     public void itemStateChanged(ItemEvent JavaDoc e) {
555
556         ItemSelectable JavaDoc item = e.getItemSelectable();
557
558         if (item == lTable) {
559             if (iSelectionStep == SELECT_SOURCE_TABLES) {
560                 String JavaDoc table = lTable.getSelectedItem();
561                 int selected = ((Integer JavaDoc) e.getItem()).intValue();
562
563                 for (int i = 0; i < tTable.size(); i++) {
564                     TransferTable t = (TransferTable) tTable.elementAt(i);
565
566                     if (t == null) {
567                         continue;
568                     }
569
570                     if (i == selected) {
571                         saveTable();
572                         displayTable(t);
573                         updateEnabled(true);
574                     }
575                 }
576             }
577         } else {
578
579             // it must be a checkbox
580
saveTable();
581             updateEnabled(true);
582         }
583     }
584
585     /**
586      * Method declaration
587      *
588      */

589     private void saveTable() {
590
591         if (tCurrent == null) {
592             return;
593         }
594
595         TransferTable t = tCurrent;
596
597         t.Stmts.sSourceTable = tSourceTable.getText();
598         t.Stmts.sDestTable = tDestTable.getText();
599         t.Stmts.sDestDrop = tDestDrop.getText();
600         t.Stmts.sDestCreateIndex = tDestCreateIndex.getText();
601         t.Stmts.sDestDropIndex = tDestDropIndex.getText();
602         t.Stmts.sDestCreate = tDestCreate.getText();
603         t.Stmts.sDestDelete = tDestDelete.getText();
604         t.Stmts.sSourceSelect = tSourceSelect.getText();
605         t.Stmts.sDestInsert = tDestInsert.getText();
606         t.Stmts.sDestAlter = tDestAlter.getText();
607
608         //
609
t.Stmts.bTransfer = cTransfer.getState();
610         t.Stmts.bDrop = cDrop.getState();
611         t.Stmts.bCreate = cCreate.getState();
612         t.Stmts.bDelete = cDelete.getState();
613         t.Stmts.bInsert = cInsert.getState();
614         t.Stmts.bAlter = cAlter.getState();
615         t.Stmts.bCreateIndex = cCreateIndex.getState();
616         t.Stmts.bDropIndex = cDropIndex.getState();
617
618         if (!t.Stmts.bTransfer) {
619             t.Stmts.bInsert = false;
620
621             cInsert.setState(false);
622         }
623
624         boolean reparsetable = ((t.Stmts.bFKForced != cFKForced.getState())
625                                 || (t.Stmts.bIdxForced
626                                     != cIdxForced.getState()));
627
628         t.Stmts.bFKForced = cFKForced.getState();
629         t.Stmts.bIdxForced = cIdxForced.getState();
630
631         if (reparsetable) {
632             try {
633                 sourceDb.getTableStructure(t, targetDb);
634             } catch (Exception JavaDoc e) {
635                 trace("Exception reading source tables: " + e);
636                 e.printStackTrace();
637             }
638         }
639     }
640
641     /**
642      * Method declaration
643      *
644      *
645      * @param t
646      */

647     private void displayTable(TransferTable t) {
648
649         tCurrent = t;
650
651         if (t == null) {
652             return;
653         }
654
655         tSourceTable.setText(t.Stmts.sSourceTable);
656         tDestTable.setText(t.Stmts.sDestTable);
657         tDestDrop.setText(t.Stmts.sDestDrop);
658         tDestCreateIndex.setText(t.Stmts.sDestCreateIndex);
659         tDestDropIndex.setText(t.Stmts.sDestDropIndex);
660         tDestCreate.setText(t.Stmts.sDestCreate);
661         tDestDelete.setText(t.Stmts.sDestDelete);
662         tSourceSelect.setText(t.Stmts.sSourceSelect);
663         tDestInsert.setText(t.Stmts.sDestInsert);
664         tDestAlter.setText(t.Stmts.sDestAlter);
665         cTransfer.setState(t.Stmts.bTransfer);
666         cDrop.setState(t.Stmts.bDrop);
667         cCreate.setState(t.Stmts.bCreate);
668         cDropIndex.setState(t.Stmts.bDropIndex);
669         cCreateIndex.setState(t.Stmts.bCreateIndex);
670         cDelete.setState(t.Stmts.bDelete);
671         cInsert.setState(t.Stmts.bInsert);
672         cAlter.setState(t.Stmts.bAlter);
673         cFKForced.setState(t.Stmts.bFKForced);
674         cIdxForced.setState(t.Stmts.bIdxForced);
675     }
676
677     /**
678      * Method declaration
679      *
680      *
681      * @param and
682      */

683     private void updateEnabled(boolean and) {
684
685         boolean b = cTransfer.getState();
686
687         tDestTable.setEnabled(and && b);
688         tDestDrop.setEnabled(and && b && cDrop.getState());
689         tDestCreate.setEnabled(and && b && cCreate.getState());
690         tDestDelete.setEnabled(and && b && cDelete.getState());
691         tDestCreateIndex.setEnabled(and && b && cCreateIndex.getState());
692         tDestDropIndex.setEnabled(and && b && cDropIndex.getState());
693         tSourceSelect.setEnabled(and && b);
694         tDestInsert.setEnabled(and && b && cInsert.getState());
695         tDestAlter.setEnabled(and && b && cAlter.getState());
696         cDrop.setEnabled(and && b);
697         cCreate.setEnabled(and && b);
698         cDelete.setEnabled(and && b);
699         cCreateIndex.setEnabled(and && b);
700         cDropIndex.setEnabled(and && b);
701         cInsert.setEnabled(and && b);
702         cAlter.setEnabled(and && b);
703         cFKForced.setEnabled(cAlter.getState());
704         cIdxForced.setEnabled(cCreateIndex.getState());
705         bStart.setEnabled(and);
706
707         if (iTransferMode == TRFM_TRANSFER) {
708             bContinue.setEnabled(and);
709         }
710     }
711
712     /**
713      * Method ProcessNextStep
714      */

715     private void ProcessNextStep() {
716
717         switch (iSelectionStep) {
718
719             case SELECT_SOURCE_CATALOG :
720             case SELECT_DEST_CATALOG :
721                 if (CatalogToSelect()) {
722                     fMain.show();
723
724                     return;
725                 }
726                 break;
727
728             case SELECT_DEST_SCHEMA :
729             case SELECT_SOURCE_SCHEMA :
730                 if (SchemaToSelect()) {
731                     fMain.show();
732
733                     return;
734                 }
735                 break;
736
737             case SELECT_SOURCE_TABLES :
738                 if (iTransferMode == TRFM_TRANSFER) {
739                     bStart.setLabel("Start Transfer");
740                 } else if (iTransferMode == TRFM_DUMP) {
741                     bStart.setLabel("Start Dump");
742                 } else if (iTransferMode == TRFM_RESTORE) {
743                     bStart.setLabel("Start Restore");
744                 }
745
746                 bStart.invalidate();
747                 bStart.setEnabled(false);
748                 lTable.setMultipleMode(false);
749                 RefreshMainDisplay();
750                 break;
751
752             default :
753                 break;
754         }
755     }
756
757     /**
758      * Method declaration
759      *
760      *
761      * @param ev
762      */

763     public void actionPerformed(ActionEvent JavaDoc ev) {
764
765         if (ev.getSource() instanceof TextField JavaDoc) {
766             saveTable();
767
768             return;
769         }
770
771         String JavaDoc s = ev.getActionCommand();
772         MenuItem JavaDoc i = new MenuItem JavaDoc();
773
774         if (s == null) {
775             if (ev.getSource() instanceof MenuItem JavaDoc) {
776                 i = (MenuItem JavaDoc) ev.getSource();
777                 s = i.getLabel();
778             }
779         }
780
781         if (s == null) {}
782
783         if (s.equals("Start Transfer") || s.equals("ReStart Transfer")) {
784             bStart.setLabel("ReStart Transfer");
785             bStart.invalidate();
786
787             CurrentTransfer = 0;
788             CurrentAlter = 0;
789
790             transfer();
791         } else if (s.equals("Continue Transfer")) {
792             transfer();
793         } else if (s.equals("Start Dump") || s.equals("Start Restore")) {
794             CurrentTransfer = 0;
795             CurrentAlter = 0;
796
797             transfer();
798         } else if (s.equals("Quit")) {
799             Exit();
800         } else if (s.indexOf("Select Schema") >= 0) {
801             String JavaDoc[] selection = lTable.getSelectedItems();
802
803             if ((selection == null) || (selection.length == 0)) {
804                 return;
805             }
806
807             if (iSelectionStep == Transfer.SELECT_SOURCE_SCHEMA) {
808                 sSourceSchemas = selection;
809             } else {
810                 sDestSchema = selection[0];
811             }
812
813             if (iTransferMode == TRFM_DUMP) {
814                 iSelectionStep = Transfer.SELECT_SOURCE_TABLES;
815             } else {
816                 iSelectionStep++;
817             }
818
819             ProcessNextStep();
820         } else if (s.indexOf("Select Catalog") >= 0) {
821             String JavaDoc selection = lTable.getSelectedItem();
822
823             if ((selection == null) || (selection.equals(""))) {
824                 return;
825             }
826
827             if (iSelectionStep == Transfer.SELECT_SOURCE_CATALOG) {
828                 sSourceCatalog = selection;
829                 sSourceSchemas = null;
830             } else {
831                 sDestCatalog = selection;
832                 sDestSchema = null;
833
834                 try {
835                     targetDb.setCatalog(sDestCatalog);
836                 } catch (Exception JavaDoc ex) {
837                     trace("Catalog " + sDestCatalog
838                           + " could not be selected in the target database");
839
840                     sDestCatalog = null;
841                 }
842             }
843
844             iSelectionStep++;
845
846             ProcessNextStep();
847         } else if (s.equals("Insert 10 rows only")) {
848             iMaxRows = 10;
849         } else if (s.equals("Insert 1000 rows only")) {
850             iMaxRows = 1000;
851         } else if (s.equals("Insert all rows")) {
852             iMaxRows = 0;
853         } else if (s.equals("Load Settings...")) {
854             FileDialog JavaDoc f = new FileDialog JavaDoc(fMain, "Load Settings",
855                                           FileDialog.LOAD);
856
857             f.show();
858
859             String JavaDoc file = f.getDirectory() + f.getFile();
860
861             if (file != null) {
862                 LoadPrefs(file);
863                 displayTable(tCurrent);
864             }
865         } else if (s.equals("Save Settings...")) {
866             FileDialog JavaDoc f = new FileDialog JavaDoc(fMain, "Save Settings",
867                                           FileDialog.SAVE);
868
869             f.show();
870
871             String JavaDoc file = f.getDirectory() + f.getFile();
872
873             if (file != null) {
874                 SavePrefs(file);
875             }
876         } else if (s.equals("Exit")) {
877             windowClosing(null);
878         }
879     }
880
881     /**
882      * Method declaration
883      *
884      *
885      * @param e
886      */

887     public void windowActivated(WindowEvent JavaDoc e) {}
888
889     /**
890      * Method declaration
891      *
892      *
893      * @param e
894      */

895     public void windowDeactivated(WindowEvent JavaDoc e) {}
896
897     /**
898      * Method declaration
899      *
900      *
901      * @param e
902      */

903     public void windowClosed(WindowEvent JavaDoc e) {}
904
905     private void cleanup() {
906
907         try {
908             if (sourceDb != null) {
909                 sourceDb.close();
910             }
911
912             if (targetDb != null) {
913                 targetDb.close();
914             }
915         } catch (Exception JavaDoc e) {}
916     }
917
918     /**
919      * Method declaration
920      *
921      *
922      * @param ev
923      */

924     public void windowClosing(WindowEvent JavaDoc ev) {
925
926         fMain.dispose();
927
928         if (bMustExit) {
929             System.exit(0);
930         }
931     }
932
933     /**
934      * Method declaration
935      *
936      *
937      * @param e
938      */

939     public void windowDeiconified(WindowEvent JavaDoc e) {}
940
941     /**
942      * Method declaration
943      *
944      *
945      * @param e
946      */

947     public void windowIconified(WindowEvent JavaDoc e) {}
948
949     /**
950      * Method declaration
951      *
952      *
953      * @param e
954      */

955     public void windowOpened(WindowEvent JavaDoc e) {}
956
957     /**
958      * Method declaration
959      *
960      */

961     private void initGUI() {
962
963         Font JavaDoc fFont = new Font JavaDoc("Dialog", Font.PLAIN, 12);
964
965         setLayout(new BorderLayout JavaDoc());
966
967         Panel JavaDoc p = new Panel JavaDoc();
968
969         p.setBackground(SystemColor.control);
970         p.setLayout(new GridLayout JavaDoc(16, 1));
971
972         tSourceTable = new TextField JavaDoc();
973
974         tSourceTable.setEnabled(false);
975
976         tDestTable = new TextField JavaDoc();
977
978         tDestTable.addActionListener(this);
979
980         tDestDrop = new TextField JavaDoc();
981
982         tDestDrop.addActionListener(this);
983
984         tDestCreate = new TextField JavaDoc();
985
986         tDestCreate.addActionListener(this);
987
988         tDestDelete = new TextField JavaDoc();
989
990         tDestDelete.addActionListener(this);
991
992         tDestCreateIndex = new TextField JavaDoc();
993
994         tDestCreateIndex.addActionListener(this);
995
996         tDestDropIndex = new TextField JavaDoc();
997
998         tDestDropIndex.addActionListener(this);
999
1000        tSourceSelect = new TextField JavaDoc();
1001
1002        tSourceSelect.addActionListener(this);
1003
1004        tDestInsert = new TextField JavaDoc();
1005
1006        tDestInsert.addActionListener(this);
1007
1008        tDestAlter = new TextField JavaDoc();
1009
1010        tDestAlter.addActionListener(this);
1011
1012        cTransfer = new Checkbox JavaDoc("Transfer to destination table", true);
1013
1014        cTransfer.addItemListener(this);
1015
1016        cDrop = new Checkbox JavaDoc("Drop destination table (ignore error)", true);
1017
1018        cDrop.addItemListener(this);
1019
1020        cCreate = new Checkbox JavaDoc("Create destination table", true);
1021
1022        cCreate.addItemListener(this);
1023
1024        cDropIndex = new Checkbox JavaDoc("Drop destination index (ignore error)",
1025                                  true);
1026
1027        cDropIndex.addItemListener(this);
1028
1029        cIdxForced = new Checkbox JavaDoc("force Idx_ prefix for indexes names",
1030                                  false);
1031
1032        cIdxForced.addItemListener(this);
1033
1034        cCreateIndex = new Checkbox JavaDoc("Create destination index", true);
1035
1036        cCreateIndex.addItemListener(this);
1037
1038        cDelete = new Checkbox JavaDoc("Delete rows in destination table", true);
1039
1040        cDelete.addItemListener(this);
1041
1042        cInsert = new Checkbox JavaDoc("Insert into destination", true);
1043
1044        cInsert.addItemListener(this);
1045
1046        cFKForced = new Checkbox JavaDoc("force FK_ prefix for foreign key names",
1047                                 false);
1048
1049        cFKForced.addItemListener(this);
1050
1051        cAlter = new Checkbox JavaDoc("Alter destination table", true);
1052
1053        cAlter.addItemListener(this);
1054        p.add(createLabel("Source table"));
1055        p.add(tSourceTable);
1056        p.add(cTransfer);
1057        p.add(tDestTable);
1058        p.add(cDrop);
1059        p.add(tDestDrop);
1060        p.add(cCreate);
1061        p.add(tDestCreate);
1062        p.add(cDropIndex);
1063        p.add(tDestDropIndex);
1064        p.add(cCreateIndex);
1065        p.add(tDestCreateIndex);
1066        p.add(cDelete);
1067        p.add(tDestDelete);
1068        p.add(cAlter);
1069        p.add(tDestAlter);
1070        p.add(createLabel("Select source records"));
1071        p.add(tSourceSelect);
1072        p.add(cInsert);
1073        p.add(tDestInsert);
1074        p.add(createLabel(""));
1075        p.add(createLabel(""));
1076        p.add(cIdxForced);
1077        p.add(cFKForced);
1078        p.add(createLabel(""));
1079        p.add(createLabel(""));
1080
1081        if (iTransferMode == TRFM_TRANSFER) {
1082            bStart = new Button JavaDoc("Start Transfer");
1083            bContinue = new Button JavaDoc("Continue Transfer");
1084
1085            bContinue.setEnabled(false);
1086        } else if (iTransferMode == Transfer.TRFM_DUMP) {
1087            bStart = new Button JavaDoc("Start Dump");
1088        } else if (iTransferMode == Transfer.TRFM_RESTORE) {
1089            bStart = new Button JavaDoc("Start Restore");
1090        }
1091
1092        bStart.addActionListener(this);
1093        p.add(bStart);
1094
1095        if (iTransferMode == TRFM_TRANSFER) {
1096            bContinue.addActionListener(this);
1097            p.add(bContinue);
1098        }
1099
1100        bStart.setEnabled(false);
1101        fMain.add("Center", createBorderPanel(p));
1102
1103        lTable = new java.awt.List JavaDoc(10);
1104
1105        lTable.addItemListener(this);
1106        fMain.add("West", createBorderPanel(lTable));
1107
1108        tMessage = new TextField JavaDoc();
1109
1110        Panel JavaDoc pMessage = createBorderPanel(tMessage);
1111
1112        fMain.add("South", pMessage);
1113    }
1114
1115    /**
1116     * Method declaration
1117     *
1118     *
1119     * @param center
1120     *
1121     * @return
1122     */

1123    private Panel JavaDoc createBorderPanel(Component JavaDoc center) {
1124
1125        Panel JavaDoc p = new Panel JavaDoc();
1126
1127        p.setBackground(SystemColor.control);
1128        p.setLayout(new BorderLayout JavaDoc());
1129        p.add("Center", center);
1130        p.add("South", createLabel(""));
1131        p.add("East", createLabel(""));
1132        p.add("West", createLabel(""));
1133        p.setBackground(SystemColor.control);
1134
1135        return p;
1136    }
1137
1138    /**
1139     * Method declaration
1140     *
1141     *
1142     * @param s
1143     *
1144     * @return
1145     */

1146    private Label JavaDoc createLabel(String JavaDoc s) {
1147
1148        Label JavaDoc l = new Label JavaDoc(s);
1149
1150        l.setBackground(SystemColor.control);
1151
1152        return l;
1153    }
1154
1155    private void SavePrefs(String JavaDoc f) {
1156        saveTable();
1157        TransferCommon.savePrefs(f, sourceDb, targetDb, this, tTable);
1158    }
1159
1160    private void LoadPrefs(String JavaDoc f) {
1161
1162        TransferTable t;
1163
1164        trace("Parsing Settings file");
1165        bStart.setEnabled(false);
1166
1167        if (iTransferMode == TRFM_TRANSFER) {
1168            bContinue.setEnabled(false);
1169        }
1170
1171        tTable = TransferCommon.loadPrefs(f, sourceDb, targetDb, this);
1172        iSelectionStep = SELECT_SOURCE_TABLES;
1173
1174        lTable.removeAll();
1175
1176        for (int i = 0; i < tTable.size(); i++) {
1177            t = (TransferTable) tTable.elementAt(i);
1178
1179            lTable.add(t.Stmts.sSourceTable);
1180        }
1181
1182        t = (TransferTable) tTable.elementAt(0);
1183
1184        displayTable(t);
1185        lTable.select(0);
1186        updateEnabled(true);
1187        lTable.invalidate();
1188
1189        if (iTransferMode == TRFM_TRANSFER) {
1190            bStart.setLabel("Start Transfer");
1191            trace("Edit definitions and press [Start Transfer]");
1192        } else if (iTransferMode == TRFM_DUMP) {
1193            bStart.setLabel("Start Dump");
1194            trace("Edit definitions and press [Start Dump]");
1195        } else if (iTransferMode == TRFM_RESTORE) {
1196            bStart.setLabel("Start Restore");
1197            trace("Edit definitions and press [Start Restore]");
1198        }
1199
1200        bStart.invalidate();
1201
1202        if (iTransferMode == TRFM_TRANSFER) {
1203            bContinue.setEnabled(false);
1204        }
1205    }
1206
1207    /**
1208     * Method declaration
1209     *
1210     */

1211    private void transfer() {
1212
1213        saveTable();
1214        updateEnabled(false);
1215        trace("Start Transfer");
1216
1217        int TransferIndex = CurrentTransfer;
1218        int AlterIndex = CurrentAlter;
1219        TransferTable t = null;
1220        long startTime, stopTime;
1221
1222        startTime = System.currentTimeMillis();
1223
1224        try {
1225            for (int i = TransferIndex; i < tTable.size(); i++) {
1226                CurrentTransfer = i;
1227                t = (TransferTable) tTable.elementAt(i);
1228
1229                lTable.select(i);
1230                displayTable(t);
1231                t.transferStructure();
1232                t.transferData(iMaxRows);
1233            }
1234
1235            for (int i = AlterIndex; i < tTable.size(); i++) {
1236                CurrentAlter = i;
1237                t = (TransferTable) tTable.elementAt(i);
1238
1239                lTable.select(i);
1240                displayTable(t);
1241                t.transferAlter();
1242            }
1243
1244            stopTime = System.currentTimeMillis();
1245
1246            trace("Transfer finished successfully in: "
1247                  + (stopTime - startTime) / 1000.00 + " sec");
1248
1249            if (iTransferMode == TRFM_TRANSFER) {
1250                bContinue.setLabel("Quit");
1251                bContinue.setEnabled(true);
1252                bContinue.invalidate();
1253            } else {
1254                bStart.setLabel("Quit");
1255                bStart.setEnabled(true);
1256                bStart.invalidate();
1257            }
1258        } catch (Exception JavaDoc e) {
1259            String JavaDoc last = tMessage.getText();
1260
1261            trace("Transfer stopped - " + last + " / / Error: "
1262                  + e.getMessage());
1263            e.printStackTrace();
1264        }
1265
1266        if (iTransferMode == TRFM_TRANSFER) {
1267            bContinue.setEnabled((CurrentAlter < tTable.size()));
1268        }
1269
1270        updateEnabled(true);
1271        System.gc();
1272    }
1273
1274    protected void Exit() {
1275
1276        cleanup();
1277        fMain.dispose();
1278
1279        if (bMustExit) {
1280            System.exit(0);
1281        }
1282    }
1283}
1284
Popular Tags