001: /*
002: * ImportExportDelimitedPanel.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.actions.ActionBuilder;
033: import org.underworldlabs.swing.wizard.DefaultWizardProcessModel;
034: import org.underworldlabs.swing.wizard.WizardProcessPanel;
035: import org.executequery.databasemediators.DatabaseConnection;
036: import org.executequery.databasemediators.MetaDataValues;
037: import org.executequery.gui.*;
038: import org.executequery.gui.browser.ColumnData;
039: import org.executequery.util.Log;
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: * Import export to delimited file parent object.
050: *
051: * @author Takis Diakoumis
052: * @version $Revision: 1.8 $
053: * @date $Date: 2006/09/13 15:15:09 $
054: */
055: public class ImportExportDelimitedPanel extends WizardProcessPanel
056: implements ImportExportProcess, ActiveComponent {
057:
058: /** The type of transfer - import/export */
059: private int transferType;
060:
061: /** The dimension of each child panel */
062: private Dimension childDim;
063:
064: /** The object to retrieve table details */
065: private MetaDataValues metaData;
066:
067: /** The worker that will run the process */
068: private ImportExportWorker worker;
069:
070: /** The first panel displayed */
071: private ImportExportDelimitedPanel_1 firstPanel;
072:
073: /** The second panel displayed */
074: private ImportExportPanel_2 secondPanel;
075:
076: /** The third panel displayed */
077: private ImportExportPanel_3 thirdPanel;
078:
079: /** The fourth panel displayed */
080: private ImportExportDelimitedPanel_4 fourthPanel;
081:
082: /** The progress panel to track the process */
083: private ImportExportProgressPanel progressPanel;
084:
085: /** Whether the process has been cancelled */
086: private boolean processCancelled;
087:
088: /** Whether the process was a success */
089: private boolean processing;
090:
091: /** the parent container */
092: private ActionContainer parent;
093:
094: /** the selection model */
095: private TransferDelimitedWizardModel model;
096:
097: /**
098: * Creates a new instance of the process with the specified parent
099: * container and process type.
100: *
101: * @param the process type - import/export
102: */
103: public ImportExportDelimitedPanel(ActionContainer parent,
104: int transferType) {
105: this (parent, transferType, null, null, null);
106: }
107:
108: /**
109: * Creates a new instance of the process with the specified parent
110: * container and process type.
111: *
112: * @param the process type - import/export
113: */
114: public ImportExportDelimitedPanel(ActionContainer parent,
115: int transferType, DatabaseConnection databaseConnection,
116: String schemaName, String tableName) {
117: this .transferType = transferType;
118: model = new TransferDelimitedWizardModel();
119: setModel(model);
120:
121: this .parent = parent;
122:
123: try {
124: jbInit();
125: } catch (Exception e) {
126: e.printStackTrace();
127: }
128:
129: if (databaseConnection != null) {
130: firstPanel.setDatabaseConnection(databaseConnection);
131: next();
132:
133: if (schemaName != null) {
134: secondPanel.setSelectedSchema(schemaName);
135: if (tableName != null) {
136: secondPanel.setSelectedTable(tableName);
137: secondPanel.selectAllAvailable();
138: }
139:
140: }
141:
142: }
143:
144: }
145:
146: /** <p>Initialises the state of this instance and
147: * lays out components on the panel. */
148: private void jbInit() throws Exception {
149: processCancelled = false;
150: metaData = new MetaDataValues(true);
151: childDim = new Dimension(525, 340);
152:
153: // set the help action
154: setHelpAction(ActionBuilder.get("help-command"),
155: "import-export");
156:
157: firstPanel = new ImportExportDelimitedPanel_1(this );
158: model.addPanel(firstPanel);
159: prepare();
160: }
161:
162: /**
163: * Returns the transfer format - XML, CSV etc.
164: */
165: public int getTransferFormat() {
166: return DELIMITED;
167: }
168:
169: /**
170: * Releases database resources before closing.
171: */
172: public void cleanup() {
173: metaData.closeConnection();
174: }
175:
176: public String getSchemaName() {
177: return secondPanel.getSelectedSchema();
178: }
179:
180: /** <p>Retrieves the selected tables for this process.
181: *
182: * @return the selected table names
183: */
184: public String[] getSelectedTables() {
185: return secondPanel.getSelectedTables();
186: }
187:
188: /** <p>Retrieves the table name for this process in the
189: * case of a single table import/export.
190: *
191: * @return the table name
192: */
193: public String getTableName() {
194: return secondPanel.getSelectedTables()[0];
195: }
196:
197: /** <p>Retrieves the column names for this process.
198: *
199: * @return the column names
200: */
201: public Vector<ColumnData> getSelectedColumns() {
202: return secondPanel.getSelectedColumns();
203: }
204:
205: /** <p>Retrieves the selected rollback size for
206: * the transfer.
207: *
208: * @return the rollback size
209: */
210: public int getRollbackSize() {
211: return fourthPanel.getRollbackSize();
212: }
213:
214: public boolean includeColumnNames() {
215: return fourthPanel.includeColumnNames();
216: }
217:
218: /** <p>Retrieves the action on an error occuring
219: * during the import/export process.
220: *
221: * @return the action on error -<br>either:
222: * <code>ImportExportProcess.LOG_AND_CONTINUE</code> or
223: * <code>ImportExportProcess.STOP_TRANSFER</code>
224: */
225: public int getOnError() {
226: return fourthPanel.getOnError();
227: }
228:
229: /**
230: * Retrieves the selected type of delimiter within
231: * the file to be used with this process.
232: *
233: * @return the selected delimiter
234: */
235: public char getDelimiter() {
236: return fourthPanel.getDelimiter();
237: }
238:
239: /** <p>Retrieves the date format for date fields
240: * contained within the data file/database table.
241: *
242: * @return the date format (ie. ddMMyyy)
243: */
244: public String getDateFormat() {
245: String format = fourthPanel.getDateFormat();
246: if (format == null || format.length() == 0) {
247: return null;
248: } else {
249: return format;
250: }
251: }
252:
253: /**
254: * Returns whether to parse date values.
255: *
256: * @return true | false
257: */
258: public boolean parseDateValues() {
259: return fourthPanel.parseDateValues();
260: }
261:
262: /**
263: * Indicates whether the process (import only) should
264: * be run as a batch process.
265: *
266: * @return whether to run as a batch process
267: */
268: public boolean runAsBatchProcess() {
269: return fourthPanel.runAsBatchProcess();
270: }
271:
272: public boolean trimWhitespace() {
273: return fourthPanel.trimWhitespace();
274: }
275:
276: /** <p>Begins an import process. */
277: public void doImport() {
278: Log.info("Beginning data import process");
279: processCancelled = false;
280: setNextButtonEnabled(false);
281: setBackButtonEnabled(false);
282: setCancelButtonEnabled(false);
283: worker = new ImportDelimitedWorker(this , progressPanel);
284: }
285:
286: public void setProcessComplete(boolean success) {
287: setButtonsFinished(success);
288: }
289:
290: public void setButtonsFinished(boolean success) {
291: setButtonsEnabled(true);
292: setNextButtonEnabled(false);
293: setBackButtonEnabled(true);
294: setCancelButtonEnabled(true);
295:
296: if (success) {
297: setCancelButtonText("Finish");
298: }
299: processing = false;
300: }
301:
302: /** <p>Begins an export process. */
303: public void doExport() {
304: Log.info("Beginning data export process");
305: processCancelled = false;
306: setNextButtonEnabled(false);
307: setBackButtonEnabled(false);
308: setCancelButtonEnabled(false);
309: worker = new ExportDelimitedWorker(this , progressPanel);
310: }
311:
312: /** <p>Cancels the current in-process transfer */
313: public void cancelTransfer() {
314: Log.info("Process cancelled");
315: processCancelled = true;
316: }
317:
318: /** <p>Returns the type of transfer - import or export.
319: *
320: * @return the transfer type - import/export
321: */
322: public int getTransferType() {
323: return transferType;
324: }
325:
326: /** <p>Returns a <code>Vector</code> of <code>
327: * DataTransferObject</code> objects containing
328: * all relevant data for the process.
329: *
330: * @return a <code>Vector</code> of
331: * <code>DataTransferObject</code> objects
332: */
333: public Vector getDataFileVector() {
334: return thirdPanel.getDataFileVector();
335: }
336:
337: /**
338: * Returns the XML format style for an XML import/export.
339: * Value of -1 is returned in this case.
340: *
341: * @return the XML format
342: */
343: public int getXMLFormat() {
344: return -1;
345: }
346:
347: /**
348: * Defines the action for the BACK button.
349: */
350: private boolean doPrevious() {
351: // make sure the cancel button says cancel
352: setCancelButtonText("Cancel");
353: return true;
354: }
355:
356: /**
357: * Defines the action for the NEXT button.
358: */
359: private boolean doNext() {
360: JPanel nextPanel = null;
361: int index = model.getSelectedIndex();
362:
363: switch (index) {
364:
365: case 0:
366: DatabaseConnection dc = getDatabaseConnection();
367: if (dc != null) {
368: metaData.setDatabaseConnection(dc);
369: }
370:
371: if (secondPanel == null) {
372: secondPanel = new ImportExportPanel_2(this );
373: }
374: nextPanel = secondPanel;
375:
376: secondPanel.setListData(getTableTransferType());
377: break;
378:
379: case 1:
380:
381: if (!secondPanel.hasSelections()) {
382: if (getTableTransferType() == ImportExportProcess.MULTIPLE_TABLE) {
383: GUIUtilities
384: .displayErrorMessage("You must select at least one table");
385: } else if (getTableTransferType() == ImportExportProcess.SINGLE_TABLE) {
386: GUIUtilities
387: .displayErrorMessage("You must select at least one column");
388: }
389: return false;
390: }
391:
392: if (thirdPanel == null) {
393: thirdPanel = new ImportExportPanel_3(this );
394: } else {
395: thirdPanel.buildTable();
396: }
397: nextPanel = thirdPanel;
398: break;
399:
400: case 2:
401:
402: if (!thirdPanel.transferObjectsComplete()) {
403: return false;
404: }
405:
406: if (fourthPanel == null) {
407: fourthPanel = new ImportExportDelimitedPanel_4(this );
408: }
409: nextPanel = fourthPanel;
410: break;
411:
412: case 3:
413:
414: if (progressPanel == null) {
415: progressPanel = new ImportExportProgressPanel(this );
416: }
417: processing = true;
418: model.addPanel(progressPanel);
419:
420: if (transferType == ImportExportProcess.EXPORT) {
421: doExport();
422: } else if (transferType == ImportExportProcess.IMPORT) {
423: doImport();
424: }
425: setButtonsEnabled(false);
426: return true;
427:
428: }
429:
430: model.addPanel(nextPanel);
431: return true;
432: }
433:
434: /**
435: * Stops the current process.
436: */
437: public void stopTransfer() {
438: setButtonsEnabled(true);
439: if (processing) {
440: worker.cancelTransfer();
441: setBackButtonEnabled(true);
442: processCancelled = true;
443: }
444: }
445:
446: /**
447: * Defines the action for the CANCEL button.
448: */
449: public void cancel() {
450: setButtonsEnabled(true);
451: if (processing) {
452: worker.cancelTransfer();
453: setBackButtonEnabled(true);
454: processCancelled = true;
455: } else {
456: worker = null;
457: parent.finished();
458: }
459: }
460:
461: public JDialog getDialog() {
462: if (parent.isDialog()) {
463: return (JDialog) parent;
464: }
465: return null;
466: }
467:
468: /**
469: * Returns the type of transfer - single or multiple table.
470: *
471: * @return the type of transfer
472: */
473: public int getTableTransferType() {
474: return firstPanel.getSelection();
475: }
476:
477: /**
478: * Returns the selected database connection properties object.
479: *
480: * @return the connection properties object
481: */
482: public DatabaseConnection getDatabaseConnection() {
483: return firstPanel.getDatabaseConnection();
484: }
485:
486: /** <p>Retrieves the size of the child panel
487: * to be added to the main base panel.
488: *
489: * @return the size of the child panel
490: */
491: public Dimension getChildDimension() {
492: return childDim;
493: }
494:
495: /** <p>Retrieves the <code>MetaDataValues</code>
496: * object defined for this process.
497: *
498: * @return the <code>MetaDataValues</code> helper class
499: */
500: public MetaDataValues getMetaDataUtility() {
501: return metaData;
502: }
503:
504: /** <p>Returns the type of multiple table
505: * transfer - single or multiple file.
506: *
507: * @return the type of multiple table transfer
508: */
509: public int getMutlipleTableTransferType() {
510: return -1;
511: }
512:
513: private class TransferDelimitedWizardModel extends
514: DefaultWizardProcessModel {
515:
516: public TransferDelimitedWizardModel() {
517: int type = getTransferType();
518: String firstTitle = "Database Connection and Export Type";
519: String fifthTitle = "Exporting Data...";
520: if (type == ImportExportProcess.IMPORT) {
521: firstTitle = "Database Connection and Import Type";
522: fifthTitle = "Importing Data...";
523: }
524:
525: String[] titles = { firstTitle, "Table Selection",
526: "Data File Selection", "Options", fifthTitle };
527: setTitles(titles);
528:
529: String[] steps = {
530: "Select database connection and transfer type",
531: "Select the tables/columns",
532: type == ImportExportProcess.IMPORT ? "Select the data file(s) to import from"
533: : "Select the data file(s) to export to",
534: "Set any further transfer options",
535: type == ImportExportProcess.IMPORT ? "Import the data"
536: : "Export the data" };
537: setSteps(steps);
538: }
539:
540: public boolean previous() {
541: if (doPrevious()) {
542: return super .previous();
543: }
544: return false;
545: }
546:
547: public boolean next() {
548: if (doNext()) {
549: return super .next();
550: }
551: return false;
552: }
553:
554: }
555:
556: }
|