001: /*
002: * DataStoreImporter.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.db.importer;
013:
014: import java.io.File;
015: import java.sql.SQLException;
016: import java.util.ArrayList;
017: import java.util.List;
018: import workbench.db.ColumnIdentifier;
019: import workbench.db.TableIdentifier;
020: import workbench.gui.dialogs.dataimport.ImportOptions;
021: import workbench.gui.dialogs.dataimport.TextImportOptions;
022: import workbench.gui.dialogs.dataimport.XmlImportOptions;
023: import workbench.interfaces.Interruptable;
024: import workbench.interfaces.JobErrorHandler;
025: import workbench.log.LogMgr;
026: import workbench.resource.ResourceMgr;
027: import workbench.storage.DataStore;
028: import workbench.storage.ResultInfo;
029: import workbench.storage.RowActionMonitor;
030: import workbench.storage.RowData;
031: import workbench.util.ClipboardFile;
032: import workbench.util.MessageBuffer;
033:
034: /**
035: * A RowDataReceiver to import text files (either from a file or from a String)
036: * into a DataStore.
037: * @author support@sql-workbench.net
038: */
039: public class DataStoreImporter implements RowDataReceiver,
040: Interruptable {
041: private DataStore target;
042: private RowDataProducer source;
043: private RowActionMonitor rowMonitor;
044: private JobErrorHandler errorHandler;
045: private int currentRowNumber;
046:
047: public DataStoreImporter(DataStore data, RowActionMonitor monitor,
048: JobErrorHandler handler) {
049: this .target = data;
050: this .rowMonitor = monitor;
051: if (this .rowMonitor != null) {
052: this .rowMonitor
053: .setMonitorType(RowActionMonitor.MONITOR_INSERT);
054: }
055: this .errorHandler = handler;
056: }
057:
058: public void startImport() {
059: try {
060: this .currentRowNumber = 0;
061: this .source.start();
062: } catch (Exception e) {
063: LogMgr.logError("DataStoreImporter.startImport()",
064: "Error ocurred during import", e);
065: }
066: }
067:
068: public boolean shouldProcessNextRow() {
069: return true;
070: }
071:
072: public void nextRowSkipped() {
073: }
074:
075: public void importString(String contents) {
076: importString(contents, "\t", "\"");
077: }
078:
079: public void importString(String contents, String delimiter,
080: String quoteChar) {
081: ClipboardFile file = new ClipboardFile(contents);
082: setImportOptions(file, ProducerFactory.IMPORT_TEXT,
083: new DefaultImportOptions(),
084: new DefaultTextImportOptions(delimiter, quoteChar),
085: null);
086: }
087:
088: public void importString(String content, ImportOptions options,
089: TextImportOptions textOptions) {
090: ClipboardFile file = new ClipboardFile(content);
091: setImportOptions(file, ProducerFactory.IMPORT_TEXT, options,
092: textOptions, null);
093: }
094:
095: public void setImportOptions(File file, int type,
096: ImportOptions generalOptions,
097: TextImportOptions textOptions, XmlImportOptions xmlOptions) {
098: ProducerFactory factory = new ProducerFactory(file);
099: factory.setTextOptions(textOptions);
100: factory.setGeneralOptions(generalOptions);
101: factory.setXmlOptions(xmlOptions);
102: factory.setType(type);
103: ResultInfo info = this .target.getResultInfo();
104:
105: List<ColumnIdentifier> cols = new ArrayList<ColumnIdentifier>(
106: info.getColumnCount());
107: for (int i = 0; i < info.getColumnCount(); i++) {
108: cols.add(info.getColumn(i));
109: }
110:
111: try {
112: factory.setImportColumns(cols);
113: } catch (Exception e) {
114: LogMgr.logError("DataStoreImporter.setImportOptions()",
115: "Error setting import columns", e);
116: }
117: this .source = factory.getProducer();
118: this .source.setReceiver(this );
119: this .source.setAbortOnError(false);
120: this .source.setErrorHandler(this .errorHandler);
121: }
122:
123: public MessageBuffer getMessage() {
124: return this .source.getMessages();
125: }
126:
127: public void processRow(Object[] row) throws SQLException {
128: RowData data = new RowData(row.length);
129: for (int i = 0; i < row.length; i++) {
130: data.setValue(i, row[i]);
131: }
132: target.addRow(data);
133: currentRowNumber++;
134: if (this .rowMonitor != null)
135: this .rowMonitor.setCurrentRow(currentRowNumber, -1);
136: }
137:
138: public void recordRejected(String record) {
139:
140: }
141:
142: public void setTableCount(int total) {
143: }
144:
145: public void setCurrentTable(int current) {
146: }
147:
148: public void setTargetTable(TableIdentifier table,
149: ColumnIdentifier[] columns) throws SQLException {
150: if (columns.length != this .target.getColumnCount()) {
151: if (errorHandler != null)
152: errorHandler.fatalError(ResourceMgr
153: .getString("ErrImportInvalidColumnStructure"));
154: throw new SQLException("Invalid column count");
155: }
156: }
157:
158: public void importFinished() {
159: }
160:
161: public void importCancelled() {
162: }
163:
164: public void tableImportError() {
165: }
166:
167: public void cancelExecution() {
168: this .source.cancel();
169: }
170:
171: public boolean confirmCancel() {
172: return true;
173: }
174:
175: }
|