001: package com.quantum.csv.wizard;
002:
003: import java.beans.PropertyChangeEvent;
004: import java.beans.PropertyChangeListener;
005: import java.io.BufferedReader;
006: import java.io.File;
007: import java.io.FileInputStream;
008: import java.io.FileReader;
009: import java.io.IOException;
010: import java.io.InputStream;
011: import java.io.InputStreamReader;
012: import java.sql.SQLException;
013: import java.util.Vector;
014: import java.util.zip.ZipFile;
015:
016: import org.eclipse.jface.dialogs.MessageDialog;
017: import org.eclipse.jface.viewers.IStructuredSelection;
018: import org.eclipse.jface.wizard.Wizard;
019: import org.eclipse.ui.IExportWizard;
020: import org.eclipse.ui.IWorkbench;
021:
022: import com.quantum.ImageStore;
023: import com.quantum.flatfiles.ImageStoreKeys;
024: import com.quantum.flatfiles.MessageUtil;
025: import com.quantum.flatfiles.QuantumFlatFilesPlugin;
026: import com.quantum.model.Entity;
027: import com.quantum.model.Table;
028: import com.quantum.ui.dialog.ExceptionDisplayDialog;
029: import com.quantum.ui.dialog.SQLExceptionDialog;
030: import com.quantum.util.connection.ConnectionException;
031: import com.quantum.view.bookmark.BookmarkView;
032: import com.quantum.view.bookmark.EntityNode;
033: import com.quantum.view.bookmark.GroupNode;
034:
035: /**
036: * @author BC Holmes
037: * @author Julen
038: */
039: public class ImportCSVWizard extends Wizard implements IExportWizard,
040: PropertyChangeListener {
041:
042: private ImportCSVDetailsPage page1;
043:
044: public ImportCSVWizard() {
045: setWindowTitle(MessageUtil.getString(getClass(), "windowTitle"));
046: setDefaultPageImageDescriptor(ImageStore.getImageDescriptor(
047: ImageStoreKeys.EXPORT_CSV, QuantumFlatFilesPlugin
048: .getDefault()));
049: }
050:
051: public boolean performFinish() {
052:
053: boolean successful = false;
054: File file = new File(this .page1.getFileName());
055:
056: // Check if the given file exists
057: if (file.exists()) {
058: ConverterCSV converter = new ConverterCSV();
059: try {
060: Vector entities = this .page1.getMatchedEntities();
061: if (this .page1.isZip()) {
062: ZipFile inZip = new ZipFile(this .page1
063: .getFileName());
064: for (int i = 0; i < entities.size(); i++) {
065: Entity entity = ((EntityNode) entities.get(i))
066: .getEntity();
067: deleteEntityRowsIfNeeded(entity);
068: InputStream iStream = inZip
069: .getInputStream(inZip.getEntry(entity
070: .getName()
071: + ".csv"));
072: InputStreamReader iStreamReader = new InputStreamReader(
073: iStream);
074: BufferedReader bufferedReader = new BufferedReader(
075: iStreamReader);
076: converter.convert(bufferedReader, entity,
077: this .page1.getColumnSeparator(),
078: this .page1.hasHeaderRow(), this .page1
079: .ignoreHeaderRow(), this .page1
080: .getEol());
081: bufferedReader.close();
082: iStream.close();
083: }
084: }
085: // If is not a zip file
086: else if (entities.size() > 0) {
087: File inputFile = new File(this .page1.getFileName());
088: FileReader fileReader = new FileReader(inputFile);
089: BufferedReader bufferedReader = new BufferedReader(
090: fileReader);
091: // convert() will do all the work
092: Entity entity = ((EntityNode) entities.get(0))
093: .getEntity();
094: deleteEntityRowsIfNeeded(entity);
095:
096: converter.convert(bufferedReader, entity,
097: this .page1.getColumnSeparator(), this .page1
098: .hasHeaderRow(), this .page1
099: .ignoreHeaderRow(), this .page1
100: .getEol());
101: if (converter.getNumBadInserts() > 0) {
102: MessageDialog.openInformation(getShell(),
103: "Rows not inserted",
104: "NON inserted rows : "
105: + String.valueOf(converter
106: .getNumBadInserts())
107: + "\nInserted Rows :"
108: + String.valueOf(converter
109: .getNumGoodInserts()));
110: }
111: bufferedReader.close();
112: }
113: successful = true;
114: } catch (IOException e) {
115: ExceptionDisplayDialog.openError(getShell(), null,
116: null, e);
117: }
118:
119: }
120: return successful;
121: }
122:
123: private void deleteEntityRowsIfNeeded(Entity entity) {
124: if (entity instanceof Table
125: && this .page1.deletePreviousContent())
126: try {
127: ((Table) entity).deleteAllRows();
128: } catch (SQLException e) {
129: SQLExceptionDialog.openException(getShell(), entity, e);
130: } catch (ConnectionException e) {
131: ExceptionDisplayDialog.openError(getShell(), null,
132: null, e);
133: ;
134: }
135:
136: }
137:
138: public void init(IWorkbench workbench,
139: IStructuredSelection selection) {
140: Object[] nodes = BookmarkView.getInstance().getSelection()
141: .toArray();
142: if (nodes.length == 0) {
143: MessageDialog
144: .openInformation(
145: getShell(),
146: "Invalid selection",
147: "Please select a Tables FOLDER (the one grouping all tables) or a list of tables");
148: return;
149: }
150: boolean correctSelection = true;
151: for (int i = 0; i < nodes.length; i++) {
152: Object node = nodes[i];
153: if (node instanceof EntityNode) {
154: if (!((EntityNode) node).isTable()
155: && !((EntityNode) node).isView()) {
156: correctSelection = false;
157: break;
158: }
159: } else if (node instanceof GroupNode) {
160: if (!(((GroupNode) node).getType()
161: .equals(Entity.TABLE_TYPE))) {
162: correctSelection = false;
163: break;
164: }
165: } else {
166: correctSelection = false;
167: break;
168: }
169: if (!correctSelection) {
170: MessageDialog
171: .openInformation(getShell(),
172: "Invalid selection",
173: "Please select ONLY a Tables folder OR a list of tables");
174: return;
175: }
176: this .page1 = new ImportCSVDetailsPage("page1");
177: }
178:
179: }
180:
181: public void dispose() {
182: super .dispose();
183: }
184:
185: /**
186: * @see org.eclipse.jface.wizard.IWizard#addPages()
187: */
188: public void addPages() {
189: if (this .page1 != null)
190: addPage(this .page1);
191: }
192:
193: /* (non-Javadoc)
194: * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
195: */
196: public void propertyChange(PropertyChangeEvent arg0) {
197: }
198: }
|