3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
11 package eu
.etaxonomy
.taxeditor
.io
;
14 import java
.io
.InputStream
;
16 import java
.nio
.file
.Files
;
17 import java
.nio
.file
.Path
;
18 import java
.nio
.file
.Paths
;
19 import java
.util
.List
;
21 import java
.util
.UUID
;
23 import org
.apache
.commons
.io
.IOUtils
;
24 import org
.eclipse
.core
.runtime
.Assert
;
25 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
26 import org
.eclipse
.core
.runtime
.IStatus
;
27 import org
.eclipse
.core
.runtime
.Status
;
28 import org
.eclipse
.core
.runtime
.jobs
.Job
;
29 import org
.eclipse
.swt
.widgets
.Display
;
30 import org
.eclipse
.ui
.PlatformUI
;
32 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationState
;
33 import eu
.etaxonomy
.cdm
.api
.application
.ICdmApplicationConfiguration
;
34 import eu
.etaxonomy
.cdm
.common
.monitor
.IRemotingProgressMonitor
;
35 import eu
.etaxonomy
.cdm
.ext
.occurrence
.OccurenceQuery
;
36 import eu
.etaxonomy
.cdm
.io
.common
.CdmDefaultImport
;
37 import eu
.etaxonomy
.cdm
.io
.common
.IImportConfigurator
;
38 import eu
.etaxonomy
.cdm
.io
.common
.IImportConfigurator
.SOURCE_TYPE
;
39 import eu
.etaxonomy
.cdm
.io
.common
.ImportResult
;
40 import eu
.etaxonomy
.cdm
.io
.dwca
.in
.DwcaImportConfigurator
;
41 import eu
.etaxonomy
.cdm
.io
.excel
.taxa
.NormalExplicitImportConfigurator
;
42 import eu
.etaxonomy
.cdm
.io
.jaxb
.JaxbImportConfigurator
;
43 import eu
.etaxonomy
.cdm
.io
.reference
.endnote
.in
.EndnoteImportConfigurator
;
44 import eu
.etaxonomy
.cdm
.io
.sdd
.in
.SDDImportConfigurator
;
45 import eu
.etaxonomy
.cdm
.io
.service
.IIOService
;
46 import eu
.etaxonomy
.cdm
.io
.specimen
.abcd206
.in
.Abcd206ImportConfigurator
;
47 import eu
.etaxonomy
.cdm
.io
.specimen
.excel
.in
.SpecimenCdmExcelImportConfigurator
;
48 import eu
.etaxonomy
.cdm
.io
.tcsxml
.in
.TcsXmlImportConfigurator
;
49 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
50 import eu
.etaxonomy
.taxeditor
.model
.CdmProgressMonitorAdapter
;
51 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
52 import eu
.etaxonomy
.taxeditor
.operation
.IPostMoniteredOperationEnabled
;
53 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
54 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
55 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.ReportTextDialog
;
59 * ImportHandler class.
63 * @created Sep 11, 2009
66 public class ImportManager
extends AbstractIOManager
<IImportConfigurator
> implements IPostMoniteredOperationEnabled
{
69 * @param applicationConfiguration
71 private ImportManager(ICdmApplicationConfiguration applicationConfiguration
) {
72 super(applicationConfiguration
);
80 * @param applicationConfiguration
82 * {@link eu.etaxonomy.cdm.api.application.CdmApplicationController}
84 * @return a {@link eu.etaxonomy.taxeditor.io.ImportManager} object.
86 public static ImportManager
NewInstance(
87 ICdmApplicationConfiguration applicationConfiguration
) {
88 return new ImportManager(applicationConfiguration
);
92 * Use when importing data into the current application context
95 * a {@link eu.etaxonomy.cdm.io.common.IImportConfigurator}
97 * @return a {@link org.eclipse.core.runtime.jobs.Job} object.
100 protected Job
createIOJob(final IImportConfigurator configurator
) {
101 Assert
.isNotNull(configurator
, "Configuration may not be null");
103 final Display display
= Display
.getDefault();
105 Job job
= new Job("Import: " + configurator
.getClass().getSimpleName()) {
108 protected IStatus
run(IProgressMonitor monitor
) {
109 monitor
.beginTask("Importing data", 100);
110 configurator
.setProgressMonitor(CdmProgressMonitorAdapter
111 .CreateSubMonitor(monitor
, 70));
115 CdmDefaultImport
<IImportConfigurator
> importer
= new CdmDefaultImport
<IImportConfigurator
>();
116 importer
.setCdmAppController(applicationConfiguration
);
118 ImportResult importResult
= null;
120 importResult
= importer
.invoke(configurator
);
123 } catch (RuntimeException e
) {
124 MessagingUtils
.messageDialog(
125 "Error importing data",
127 "An error occured while"
128 + "importing from source '"
129 + configurator
.getSourceNameString()
131 + "Please check error log for details.", e
);
138 final StringBuilder reportText
= new StringBuilder();
139 if(importResult
!=null){
140 List
<byte[]> reports
= importResult
.getReports();
141 for (byte[] bs
: reports
) {
142 reportText
.append(new String(bs
));
145 display
.asyncExec(new Runnable() {
149 // display reports with possibility to save
150 ReportTextDialog dialog
= new ReportTextDialog(PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell());
151 dialog
.setTitle(configurator
.getClass().getSimpleName()+" Report");
152 dialog
.setReportText(reportText
.toString());
154 CdmStore
.getContextManager().notifyContextRefresh();
158 return Status
.OK_STATUS
;
166 public Job
createIOServiceJob(IImportConfigurator configurator
, InputStream is
, SOURCE_TYPE type
) {
169 return createIOServiceJob(configurator
, IOUtils
.toByteArray(is
), type
);
170 } catch (Exception e
) {
171 MessagingUtils
.errorDialog("Error importing input stream",
174 TaxeditorStorePlugin
.PLUGIN_ID
,
181 public Job
createIOServiceJob(IImportConfigurator configurator
, File importFile
, SOURCE_TYPE type
) {
182 Path path
= Paths
.get(importFile
.toURI());
184 return createIOServiceJob(configurator
, Files
.readAllBytes(path
), type
);
185 } catch (Exception e
) {
186 MessagingUtils
.errorDialog("Error importing file",
189 TaxeditorStorePlugin
.PLUGIN_ID
,
196 public Job
createIOServiceJob(final IImportConfigurator configurator
, final byte[] data
, final SOURCE_TYPE type
) {
197 Assert
.isNotNull(configurator
, "Configuration may not be null");
198 final Display display
= Display
.getDefault();
199 Job job
= new Job("Import: " + configurator
.getClass().getSimpleName()) {
202 protected IStatus
run(IProgressMonitor monitor
) {
203 monitor
.beginTask("Importing data", IProgressMonitor
.UNKNOWN
);
204 IIOService ioService
= CdmApplicationState
.getIOService();
206 ioService
.importData(configurator
, data
, type
);
210 display
.asyncExec(new Runnable() {
214 CdmStore
.getContextManager().notifyContextRefresh();
218 return Status
.OK_STATUS
;
226 public void runMoniteredOperation(IImportConfigurator configurator
, InputStream is
, SOURCE_TYPE type
) {
229 runMoniteredOperation(configurator
, IOUtils
.toByteArray(is
), type
);
230 } catch (Exception e
) {
231 MessagingUtils
.errorDialog("Error importing input stream",
234 TaxeditorStorePlugin
.PLUGIN_ID
,
241 public void runMoniteredOperation(IImportConfigurator configurator
, SOURCE_TYPE type
) {
242 byte[] data
= new byte[1];
244 runMoniteredOperation(configurator
, data
, type
);
245 } catch (Exception e
) {
246 MessagingUtils
.errorDialog("Error importing input stream",
249 TaxeditorStorePlugin
.PLUGIN_ID
,
256 public void runMoniteredOperation(IImportConfigurator configurator
, File importFile
, SOURCE_TYPE type
) {
257 Path path
= Paths
.get(importFile
.toURI());
259 runMoniteredOperation(configurator
, Files
.readAllBytes(path
), type
);
260 } catch (Exception e
) {
261 MessagingUtils
.errorDialog("Error importing input stream",
264 TaxeditorStorePlugin
.PLUGIN_ID
,
271 public void runMoniteredOperation(final IImportConfigurator configurator
, final byte[] data
, final SOURCE_TYPE type
) {
272 IIOService ioService
= CdmApplicationState
.getIOService();
273 final UUID uuid
= ioService
.monitImportData(configurator
, data
, type
);
274 Display
.getDefault().asyncExec(new Runnable() {
277 AbstractUtility
.executeMoniteredOperation("Import: " + configurator
.getClass().getSimpleName(),
288 private IImportConfigurator
getConfigurator(TYPE type
) {
289 Assert
.isNotNull(type
, "Type should not be null");
293 return JaxbImportConfigurator
.NewInstance(null, null);
295 return TcsXmlImportConfigurator
.NewInstance(null, null);
297 return EndnoteImportConfigurator
.NewInstance(null, null);
299 return NormalExplicitImportConfigurator
.NewInstance(null, null,
302 return Abcd206ImportConfigurator
.NewInstance(null, null);
304 return SDDImportConfigurator
.NewInstance(null, null);
305 case SpecimenCdmExcel
:
306 return SpecimenCdmExcelImportConfigurator
.NewInstance(null, null);
308 return Abcd206ImportConfigurator
.NewInstance(null, null);
310 return DwcaImportConfigurator
.NewInstance(null, null);
312 MessagingUtils
.notImplementedMessage(this);
313 throw new IllegalArgumentException("Import not supported yet");
323 * @return a {@link eu.etaxonomy.cdm.io.jaxb.JaxbImportConfigurator} object.
325 public final JaxbImportConfigurator
JaxbConfigurator() {
326 return (JaxbImportConfigurator
) getConfigurator(TYPE
.Jaxb
);
334 * @return a {@link eu.etaxonomy.cdm.io.tcsxml.in.TcsXmlImportConfigurator}
337 public final TcsXmlImportConfigurator
TcsConfigurator() {
338 return (TcsXmlImportConfigurator
) getConfigurator(TYPE
.Tcs
);
343 * EndnoteConfigurator
347 * {@link eu.etaxonomy.cdm.io.reference.endnote.in.EndnoteImportConfigurator}
350 public final EndnoteImportConfigurator
EndnoteConfigurator() {
351 return (EndnoteImportConfigurator
) getConfigurator(TYPE
.Endnote
);
356 * NormalExplicitConfigurator
360 * {@link eu.etaxonomy.cdm.io.excel.taxa.NormalExplicitImportConfigurator}
363 public final NormalExplicitImportConfigurator
NormalExplicitConfigurator() {
364 return (NormalExplicitImportConfigurator
) getConfigurator(TYPE
.Excel_Taxa
);
372 * @return a {@link eu.etaxonomy.cdm.io.sdd.in.SDDImportConfigurator}
375 public final SDDImportConfigurator
SddConfigurator() {
376 return (SDDImportConfigurator
) getConfigurator(TYPE
.Sdd
);
385 * {@link eu.etaxonomy.cdm.io.specimen.abcd206.in.Abcd206ImportConfigurator}
388 public final Abcd206ImportConfigurator
AbcdConfigurator() {
389 return (Abcd206ImportConfigurator
) getConfigurator(TYPE
.Abcd
);
392 public SpecimenCdmExcelImportConfigurator
SpecimenCdmExcelImportConfigurator() {
393 return (SpecimenCdmExcelImportConfigurator
) getConfigurator(TYPE
.SpecimenCdmExcel
);
396 public DwcaImportConfigurator
DwcaImportConfigurator() {
397 return (DwcaImportConfigurator
) getConfigurator(TYPE
.Gbif
);
405 public void postOperation(IRemotingProgressMonitor monitor
) {
406 Display
.getDefault().asyncExec(new Runnable() {
410 CdmStore
.getContextManager().notifyContextRefresh();
416 public Job
createIOServiceJob(final DwcaImportConfigurator configurator
) {
417 Assert
.isNotNull(configurator
, "Configuration may not be null");
418 final Display display
= Display
.getDefault();
419 Job job
= new Job("Import: " + " Dwca") {
422 protected IStatus
run(IProgressMonitor monitor
) {
423 monitor
.beginTask("Importing data", IProgressMonitor
.UNKNOWN
);
424 IIOService ioService
= CdmApplicationState
.getIOService();
426 ImportResult result
= ioService
.importDataFromStream(configurator
);
429 display
.asyncExec(new Runnable() {
433 CdmStore
.getContextManager().notifyContextRefresh();
437 return Status
.OK_STATUS
;