001: /*
002: * ImportExportXMLPanel.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.executequery.gui.importexport;
023:
024: import java.awt.Dimension;
025:
026: import java.util.Vector;
027: import javax.swing.JDialog;
028: import javax.swing.JPanel;
029: import org.executequery.ActiveComponent;
030:
031: import org.executequery.GUIUtilities;
032: import org.underworldlabs.swing.wizard.DefaultWizardProcessModel;
033: import org.underworldlabs.swing.wizard.WizardProcessPanel;
034: import org.executequery.databasemediators.DatabaseConnection;
035: import org.executequery.databasemediators.MetaDataValues;
036: import org.executequery.gui.*;
037: import org.executequery.gui.browser.ColumnData;
038: import org.executequery.util.Log;
039: import org.underworldlabs.swing.actions.ActionBuilder;
040:
041: /* ----------------------------------------------------------
042: * CVS NOTE: Changes to the CVS repository prior to the
043: * release of version 3.0.0beta1 has meant a
044: * resetting of CVS revision numbers.
045: * ----------------------------------------------------------
046: */
047:
048: /**
049: *
050: * @author Takis Diakoumis
051: * @version $Revision: 1.8 $
052: * @date $Date: 2006/09/13 15:15:09 $
053: */
054: public class ImportExportXMLPanel extends WizardProcessPanel implements
055: ImportExportProcess, ActiveComponent {
056:
057: /** The dimension of each child panel */
058: private Dimension childDim;
059:
060: /** The object to retrieve table details */
061: private MetaDataValues metaData;
062:
063: /** The worker that will run the process */
064: private ImportExportWorker worker;
065:
066: /** The first panel displayed */
067: private ImportExportXMLPanel_1 firstPanel;
068:
069: /** The second panel displayed */
070: private ImportExportPanel_2 secondPanel;
071:
072: /** The third panel displayed */
073: private ImportExportPanel_3 thirdPanel;
074:
075: /** The fourth panel displayed - export */
076: private ExportXMLPanel_4 fourthPanel_ex;
077:
078: /** The fourth panel displayed - import */
079: private ImportXMLPanel_4 fourthPanel_im;
080:
081: /** The fifth panel displayed */
082: private ImportExportXMLPanel_5 fifthPanel;
083:
084: /** The sixth panel displayed */
085: private ImportExportXMLPanel_6 sixthPanel;
086:
087: /** The progress panel */
088: private ImportExportProgressPanel progressPanel;
089:
090: /** The type of transfer - import/export */
091: private int transferType;
092:
093: /** Whether the process was cancelled */
094: private boolean processCancelled;
095:
096: /** Whether a transfer is currently underway */
097: private boolean processing;
098:
099: /** the parent container */
100: private ActionContainer parent;
101:
102: /** the selection model */
103: private TransferXMLWizardModel model;
104:
105: public ImportExportXMLPanel(ActionContainer parent, int transferType) {
106: this (parent, transferType, null, null, null);
107: }
108:
109: public ImportExportXMLPanel(ActionContainer parent,
110: int transferType, DatabaseConnection databaseConnection,
111: String schemaName, String tableName) {
112: this .transferType = transferType;
113:
114: model = new TransferXMLWizardModel();
115: setModel(model);
116:
117: this .parent = parent;
118: try {
119: jbInit();
120: } catch (Exception e) {
121: e.printStackTrace();
122: }
123:
124: if (databaseConnection != null) {
125: firstPanel.setDatabaseConnection(databaseConnection);
126: next();
127:
128: if (schemaName != null) {
129: secondPanel.setSelectedSchema(schemaName);
130: if (tableName != null) {
131: secondPanel.setSelectedTable(tableName);
132: secondPanel.selectAllAvailable();
133: }
134:
135: }
136:
137: }
138:
139: }
140:
141: private void jbInit() throws Exception {
142: metaData = new MetaDataValues();
143: childDim = new Dimension(525, 350);
144:
145: // set the help action
146: setHelpAction(ActionBuilder.get("help-command"),
147: "import-export");
148:
149: firstPanel = new ImportExportXMLPanel_1(this );
150: model.addPanel(firstPanel);
151: prepare();
152:
153: }
154:
155: /**
156: * Returns the transfer format - XML, CSV etc.
157: */
158: public int getTransferFormat() {
159: return XML;
160: }
161:
162: /**
163: * Returns whether to trim whitespace on column data values.
164: *
165: * @return true | false
166: */
167: public boolean trimWhitespace() {
168: return false;
169: }
170:
171: /**
172: * Releases database resources before closing.
173: */
174: public void cleanup() {
175: metaData.closeConnection();
176: }
177:
178: public String getSchemaName() {
179: return secondPanel.getSelectedSchema();
180: }
181:
182: /** <p>Cancels the current in-process transfer */
183: public void cancelTransfer() {
184: processCancelled = true;
185: }
186:
187: /** <p>Return whether this process is an
188: * import or export process.
189: *
190: * @return <code>ImportExportProcess.IMPORT |
191: * ImportExportProcess.EXPORT</code>
192: */
193: public int getTransferType() {
194: return transferType;
195: }
196:
197: /** <p>Retrieves the selected rollback size for
198: * the transfer.
199: *
200: * @return the rollback size
201: */
202: public int getRollbackSize() {
203: return fifthPanel.getRollbackSize();
204: }
205:
206: /** <p>Retrieves the action on an error occuring
207: * during the import/export process.
208: *
209: * @return the action on error -<br>either:
210: * <code>ImportExportProcess.LOG_AND_CONTINUE</code> or
211: * <code>ImportExportProcess.STOP_TRANSFER</code>
212: */
213: public int getOnError() {
214: return fifthPanel.getOnError();
215: }
216:
217: /** <p>Retrieves the date format for date fields
218: * contained within the data file/database table.
219: *
220: * @return the date format (ie. ddMMyyy)
221: */
222: public String getDateFormat() {
223: return fifthPanel.getDateFormat();
224: }
225:
226: /**
227: * Returns whether to parse date values.
228: *
229: * @return true | false
230: */
231: public boolean parseDateValues() {
232: return fifthPanel.parseDateValues();
233: }
234:
235: /** <p>Whether the table name is an attribute
236: * within the XML document.
237: *
238: * @return whether the table name is an attribute
239: */
240: public boolean hasTableNameAsAttribute() {
241: return fourthPanel_im.hasTableNameAsAttribute();
242: }
243:
244: /**
245: * Returns whether to include column names.
246: *
247: * @return true | false
248: */
249: public boolean includeColumnNames() {
250: return true;
251: }
252:
253: public JDialog getDialog() {
254: if (parent.isDialog()) {
255: return (JDialog) parent;
256: }
257: return null;
258: }
259:
260: /**
261: * Retrieves the selected type of delimiter within
262: * the file to be used with this process.
263: *
264: * @return the selected delimiter
265: */
266: public char getDelimiter() {
267: return 0;
268: }
269:
270: public void setProcessComplete(boolean success) {
271: setButtonsEnabled(true);
272: setNextButtonEnabled(false);
273: setBackButtonEnabled(true);
274: setCancelButtonEnabled(true);
275:
276: if (success) {
277: setCancelButtonText("Finish");
278: }
279: processing = false;
280: }
281:
282: public void doImport() {
283: Log.info("Beginning data import process");
284: processCancelled = false;
285: setNextButtonEnabled(false);
286: setCancelButtonEnabled(false);
287: setBackButtonEnabled(false);
288: worker = new ImportXMLWorker(this , progressPanel);
289: }
290:
291: /** <p>Begins an export process. */
292: public void doExport() {
293: Log.info("Beginning data export process");
294: processCancelled = false;
295: setNextButtonEnabled(false);
296: setCancelButtonEnabled(false);
297: setBackButtonEnabled(false);
298: worker = new ExportXMLWorker(this , progressPanel);
299: }
300:
301: /** <p>Retrieves the selected tables for this process.
302: *
303: * @return the selected table names
304: */
305: public String[] getSelectedTables() {
306: return secondPanel.getSelectedTables();
307: }
308:
309: /** <p>Retrieves the table name for this process in
310: * the case of a single table import/export.
311: *
312: * @return the table name
313: */
314: public String getTableName() {
315: return secondPanel.getSelectedTables()[0];
316: }
317:
318: /** <p>Retrieves the column names for this process.
319: *
320: * @return the column names
321: */
322: public Vector<ColumnData> getSelectedColumns() {
323: return secondPanel.getSelectedColumns();
324: }
325:
326: /**
327: * Returns a <code>Vector</code> of <code>DataTransferObject</code>
328: * objects containing all relevant data for the process.
329: *
330: * @return a <code>Vector</code> of <code>DataTransferObject</code> objects
331: */
332: public Vector getDataFileVector() {
333: return thirdPanel.getDataFileVector();
334: }
335:
336: public String getTableIdentifier() {
337: return fourthPanel_im.getTableTagString();
338: }
339:
340: public String getRowIdentifier() {
341: return fourthPanel_im.getRowTagString();
342: }
343:
344: // returns single or multiple table export
345: public int getTableTransferType() {
346: return firstPanel.getTableTransferType();
347: }
348:
349: public String getPrimaryImportNodes() {
350: return fourthPanel_im.getTagInfoString();
351: }
352:
353: public String getXMLFormatString() {
354: if (fourthPanel_ex.getSelection() == ImportExportProcess.SCHEMA_ELEMENT) {
355: return "Schema and table elements";
356: } else {
357: return "Table element only";
358: }
359: }
360:
361: public int getXMLFormat() {
362: return fourthPanel_ex.getSelection();
363: }
364:
365: /** <p>Returns the type of multiple table
366: * transfer - single or multiple file.
367: *
368: * @return the type of multiple table transfer
369: */
370: public int getMutlipleTableTransferType() {
371: return firstPanel.getMutlipleTableTransferType();
372: }
373:
374: /** <p>Indicates whether the process (import only)
375: * should be run as a batch process.
376: *
377: * @return whether to run as a batch process
378: */
379: public boolean runAsBatchProcess() {
380: return fifthPanel.runAsBatchProcess();
381: }
382:
383: /**
384: * Returns the selected database connection properties object.
385: *
386: * @return the connection properties object
387: */
388: public DatabaseConnection getDatabaseConnection() {
389: return firstPanel.getDatabaseConnection();
390: }
391:
392: private boolean doNext() {
393: JPanel nextPanel = null;
394: int currentPanel = model.getSelectedIndex();
395:
396: switch (currentPanel) {
397:
398: case 0:
399:
400: DatabaseConnection dc = getDatabaseConnection();
401: if (dc != null) {
402: metaData.setDatabaseConnection(dc);
403: }
404:
405: if (secondPanel == null) {
406: secondPanel = new ImportExportPanel_2(this );
407: }
408: nextPanel = secondPanel;
409: secondPanel.setListData(getTableTransferType());
410: break;
411:
412: case 1:
413:
414: if (!secondPanel.hasSelections()) {
415: if (getTableTransferType() == ImportExportProcess.MULTIPLE_TABLE) {
416: GUIUtilities
417: .displayErrorMessage("You must select at least one table");
418: } else if (getTableTransferType() == ImportExportProcess.SINGLE_TABLE) {
419: GUIUtilities
420: .displayErrorMessage("You must select at least one column");
421: }
422:
423: return false;
424: }
425:
426: if (thirdPanel == null) {
427: thirdPanel = new ImportExportPanel_3(this );
428: } else {
429: thirdPanel.buildTable();
430: }
431: nextPanel = thirdPanel;
432: break;
433:
434: case 2:
435:
436: if (!thirdPanel.transferObjectsComplete()) {
437: return false;
438: }
439:
440: if (transferType == EXPORT) {
441:
442: if (fourthPanel_ex == null) {
443: fourthPanel_ex = new ExportXMLPanel_4(this );
444: }
445: nextPanel = fourthPanel_ex;
446: fourthPanel_ex.setSelectedRadios();
447:
448: } else if (transferType == IMPORT) {
449:
450: if (fourthPanel_im == null) {
451: fourthPanel_im = new ImportXMLPanel_4(this );
452: }
453: nextPanel = fourthPanel_im;
454: }
455:
456: break;
457:
458: case 3:
459:
460: if (transferType == IMPORT
461: && !fourthPanel_im.entriesComplete()) {
462: GUIUtilities
463: .displayErrorMessage("Please ensure all required fields been entered correctly.");
464: return false;
465: }
466:
467: if (fifthPanel == null) {
468: fifthPanel = new ImportExportXMLPanel_5(this );
469: }
470: nextPanel = fifthPanel;
471: break;
472:
473: case 4:
474:
475: if (sixthPanel == null) {
476: sixthPanel = new ImportExportXMLPanel_6(this );
477: }
478: nextPanel = sixthPanel;
479: sixthPanel.setValues();
480: break;
481:
482: case 5:
483:
484: if (progressPanel == null) {
485: progressPanel = new ImportExportProgressPanel(this );
486: }
487: model.addPanel(progressPanel);
488: processing = true;
489:
490: if (transferType == EXPORT) {
491: doExport();
492: } else if (transferType == IMPORT) {
493: doImport();
494: }
495: setButtonsEnabled(false);
496: return true;
497:
498: }
499:
500: model.addPanel(nextPanel);
501: return true;
502: }
503:
504: /**
505: * Defines the action for the BACK button.
506: */
507: private boolean doPrevious() {
508: // make sure the cancel button says cancel
509: setCancelButtonText("Cancel");
510: return true;
511: }
512:
513: /**
514: * Stops the current process.
515: */
516: public void stopTransfer() {
517: setButtonsEnabled(true);
518: if (processing) {
519: worker.cancelTransfer();
520: setBackButtonEnabled(true);
521: processCancelled = true;
522: }
523: }
524:
525: /**
526: * Defines the action for the CANCEL button.
527: */
528: public void cancel() {
529: setButtonsEnabled(true);
530: if (processing) {
531: worker.cancelTransfer();
532: setBackButtonEnabled(true);
533: processCancelled = true;
534: } else {
535: worker = null;
536: parent.finished();
537: }
538:
539: }
540:
541: public Dimension getChildDimension() {
542: return childDim;
543: }
544:
545: public MetaDataValues getMetaDataUtility() {
546: return metaData;
547: }
548:
549: private class TransferXMLWizardModel extends
550: DefaultWizardProcessModel {
551:
552: public TransferXMLWizardModel() {
553: int type = getTransferType();
554: String firstTitle = "Database Connection and Export Type";
555: String lastTitle = "Exporting Data...";
556: if (type == ImportExportProcess.IMPORT) {
557: firstTitle = "Database Connection and Import Type";
558: lastTitle = "Importing Data...";
559: }
560:
561: String[] titles = { firstTitle, "Table Selection",
562: "Data File Selection", "XML Element Style",
563: "Options", "Summary", lastTitle };
564:
565: setTitles(titles);
566:
567: String[] steps = {
568: "Select database connection and transfer type",
569: "Select the tables/columns",
570: type == ImportExportProcess.IMPORT ? "Select the XML file(s) to import from"
571: : "Select the XML file(s) to export to",
572: "Select the element style",
573: "Set any further transfer options",
574: "Summary",
575: type == ImportExportProcess.IMPORT ? "Import the data"
576: : "Export the data" };
577: setSteps(steps);
578: }
579:
580: public boolean previous() {
581: if (doPrevious()) {
582: return super .previous();
583: }
584: return false;
585: }
586:
587: public boolean next() {
588: if (doNext()) {
589: return super .next();
590: }
591: return false;
592: }
593:
594: }
595:
596: }
|