2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.taxeditor
.io
;
13 import java
.io
.InputStream
;
14 import java
.nio
.file
.Files
;
15 import java
.nio
.file
.Path
;
16 import java
.nio
.file
.Paths
;
17 import java
.util
.List
;
18 import java
.util
.UUID
;
20 import org
.apache
.commons
.io
.IOUtils
;
21 import org
.eclipse
.core
.runtime
.Assert
;
22 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
23 import org
.eclipse
.core
.runtime
.IStatus
;
24 import org
.eclipse
.core
.runtime
.Status
;
25 import org
.eclipse
.core
.runtime
.jobs
.Job
;
26 import org
.eclipse
.swt
.widgets
.Display
;
27 import org
.eclipse
.ui
.PlatformUI
;
29 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationState
;
30 import eu
.etaxonomy
.cdm
.api
.application
.ICdmRepository
;
31 import eu
.etaxonomy
.cdm
.common
.monitor
.IRemotingProgressMonitor
;
32 import eu
.etaxonomy
.cdm
.io
.common
.CdmDefaultImport
;
33 import eu
.etaxonomy
.cdm
.io
.common
.IImportConfigurator
;
34 import eu
.etaxonomy
.cdm
.io
.common
.IImportConfigurator
.SOURCE_TYPE
;
35 import eu
.etaxonomy
.cdm
.io
.common
.ImportConfiguratorBase
;
36 import eu
.etaxonomy
.cdm
.io
.common
.ImportResult
;
37 import eu
.etaxonomy
.cdm
.io
.descriptive
.owl
.in
.StructureTreeOwlImportConfigurator
;
38 import eu
.etaxonomy
.cdm
.io
.distribution
.excelupdate
.ExcelDistributionUpdateConfigurator
;
39 import eu
.etaxonomy
.cdm
.io
.dwca
.in
.DwcaImportConfigurator
;
40 import eu
.etaxonomy
.cdm
.io
.excel
.taxa
.NormalExplicitImportConfigurator
;
41 import eu
.etaxonomy
.cdm
.io
.jaxb
.JaxbImportConfigurator
;
42 import eu
.etaxonomy
.cdm
.io
.reference
.endnote
.in
.EndnoteImportConfigurator
;
43 import eu
.etaxonomy
.cdm
.io
.sdd
.in
.SDDImportConfigurator
;
44 import eu
.etaxonomy
.cdm
.io
.service
.IIOService
;
45 import eu
.etaxonomy
.cdm
.io
.specimen
.abcd206
.in
.Abcd206ImportConfigurator
;
46 import eu
.etaxonomy
.cdm
.io
.specimen
.excel
.in
.SpecimenCdmExcelImportConfigurator
;
47 import eu
.etaxonomy
.cdm
.io
.specimen
.gbif
.in
.GbifImportConfigurator
;
48 import eu
.etaxonomy
.cdm
.io
.specimen
.gbif
.in
.GbifImportState
;
49 import eu
.etaxonomy
.cdm
.io
.tcsxml
.in
.TcsXmlImportConfigurator
;
50 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
51 import eu
.etaxonomy
.taxeditor
.model
.CdmProgressMonitorAdapter
;
52 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
53 import eu
.etaxonomy
.taxeditor
.operation
.IPostMoniteredOperationEnabled
;
54 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
55 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
56 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.ReportTextDialog
;
60 * ImportHandler class.
64 * @created Sep 11, 2009
67 public class ImportManager
extends AbstractIOManager
<IImportConfigurator
> implements IPostMoniteredOperationEnabled
{
69 String importSuccessMessage
= "The import was successful.";
70 String updateSuccessMessage
= "The update was successful.";
73 * @param applicationConfiguration
75 private ImportManager(ICdmRepository applicationConfiguration
) {
76 super(applicationConfiguration
);
84 * @param applicationConfiguration
86 * {@link eu.etaxonomy.cdm.api.application.CdmApplicationController}
88 * @return a {@link eu.etaxonomy.taxeditor.io.ImportManager} object.
90 public static ImportManager
NewInstance(
91 ICdmRepository applicationConfiguration
) {
92 return new ImportManager(applicationConfiguration
);
96 * Use when importing data into the current application context
99 * a {@link eu.etaxonomy.cdm.io.common.IImportConfigurator}
101 * @return a {@link org.eclipse.core.runtime.jobs.Job} object.
104 protected Job
createIOJob(final IImportConfigurator configurator
) {
105 Assert
.isNotNull(configurator
, "Configuration may not be null");
107 final Display display
= Display
.getDefault();
109 Job job
= new Job("Import: " + configurator
.getClass().getSimpleName()) {
112 protected IStatus
run(IProgressMonitor monitor
) {
113 monitor
.beginTask("Importing data", 100);
114 configurator
.setProgressMonitor(CdmProgressMonitorAdapter
115 .CreateSubMonitor(monitor
, 70));
119 CdmDefaultImport
<IImportConfigurator
> importer
= new CdmDefaultImport
<IImportConfigurator
>();
120 importer
.setCdmAppController(applicationConfiguration
);
122 ImportResult importResult
= null;
124 importResult
= importer
.invoke(configurator
);
127 } catch (RuntimeException e
) {
128 MessagingUtils
.messageDialog(
129 "Error importing data",
131 "An error occurred while"
132 + "importing from source '"
133 + configurator
.getSourceNameString()
135 + "Please check error log for details.", e
);
142 final StringBuilder reportText
= new StringBuilder();
143 if(importResult
!=null){
144 List
<byte[]> reports
= importResult
.getReports();
145 for (byte[] bs
: reports
) {
146 reportText
.append(new String(bs
));
149 display
.asyncExec(new Runnable() {
153 // display reports with possibility to save
154 ReportTextDialog dialog
= new ReportTextDialog(PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell());
155 dialog
.setTitle(configurator
.getClass().getSimpleName()+" Report");
156 dialog
.setReportText(reportText
.toString());
158 CdmStore
.getContextManager().notifyContextRefresh();
162 return Status
.OK_STATUS
;
170 public Job
createIOServiceJob(IImportConfigurator configurator
, InputStream is
, SOURCE_TYPE type
) {
173 return createIOServiceJob(configurator
, IOUtils
.toByteArray(is
), type
);
174 } catch (Exception e
) {
175 MessagingUtils
.errorDialog("Error importing input stream",
178 TaxeditorStorePlugin
.PLUGIN_ID
,
185 public Job
createIOServiceJob(IImportConfigurator configurator
, File importFile
, SOURCE_TYPE type
) {
186 Path path
= Paths
.get(importFile
.toURI());
188 return createIOServiceJob(configurator
, Files
.readAllBytes(path
), type
);
189 } catch (Exception e
) {
190 MessagingUtils
.errorDialog("Error importing file",
193 TaxeditorStorePlugin
.PLUGIN_ID
,
200 public Job
createIOServiceJob(final IImportConfigurator configurator
, final byte[] data
, final SOURCE_TYPE type
) {
201 Assert
.isNotNull(configurator
, "Configuration may not be null");
202 final Display display
= Display
.getDefault();
203 Job job
= new Job("Import: " + configurator
.getClass().getSimpleName()) {
206 protected IStatus
run(IProgressMonitor monitor
) {
207 monitor
.beginTask("Importing data", IProgressMonitor
.UNKNOWN
);
208 IIOService ioService
= CdmApplicationState
.getIOService();
210 ImportResult result
= ioService
.importData(configurator
, data
, type
);
214 display
.asyncExec(new Runnable() {
218 CdmStore
.getContextManager().notifyContextRefresh();
221 showResult(configurator
.getClass().getSimpleName(), display
, result
, importSuccessMessage
);
222 return Status
.OK_STATUS
;
230 public void runMoniteredOperation(IImportConfigurator configurator
, InputStream is
, SOURCE_TYPE type
) {
233 runMoniteredOperation(configurator
, IOUtils
.toByteArray(is
), type
);
234 } catch (Exception e
) {
235 MessagingUtils
.errorDialog("Error importing input stream",
238 TaxeditorStorePlugin
.PLUGIN_ID
,
245 public void runMoniteredOperation(IImportConfigurator configurator
, SOURCE_TYPE type
) {
246 byte[] data
= new byte[1];
248 runMoniteredOperation(configurator
, data
, type
);
249 } catch (Exception e
) {
250 MessagingUtils
.errorDialog("Error importing input stream",
253 TaxeditorStorePlugin
.PLUGIN_ID
,
260 public void runMoniteredOperation(IImportConfigurator configurator
, File importFile
, SOURCE_TYPE type
) {
261 Path path
= Paths
.get(importFile
.toURI());
263 runMoniteredOperation(configurator
, Files
.readAllBytes(path
), type
);
265 } catch (Exception e
) {
266 MessagingUtils
.errorDialog("Error importing input stream",
269 TaxeditorStorePlugin
.PLUGIN_ID
,
276 public void runMoniteredOperation(final IImportConfigurator configurator
, final byte[] data
, final SOURCE_TYPE type
) {
277 IIOService ioService
= CdmApplicationState
.getIOService();
278 final UUID uuid
= ioService
.monitImportData(configurator
, data
, type
);
279 Display
.getDefault().asyncExec(new Runnable() {
282 AbstractUtility
.executeMoniteredOperation("Import: " + configurator
.getClass().getSimpleName(),
293 private IImportConfigurator
getConfigurator(TYPE type
) {
294 Assert
.isNotNull(type
, "Type should not be null");
298 return JaxbImportConfigurator
.NewInstance(null, null);
300 return TcsXmlImportConfigurator
.NewInstance(null, null);
302 return EndnoteImportConfigurator
.NewInstance(null, null);
304 return NormalExplicitImportConfigurator
.NewInstance(null, null,
307 return Abcd206ImportConfigurator
.NewInstance(null, null);
309 return SDDImportConfigurator
.NewInstance(null, null);
310 case SpecimenCdmExcel
:
311 return SpecimenCdmExcelImportConfigurator
.NewInstance(null, null);
313 return Abcd206ImportConfigurator
.NewInstance(null, null);
315 return DwcaImportConfigurator
.NewInstance(null, null);
316 case Excel_Distribution
:
317 return ExcelDistributionUpdateConfigurator
.NewInstance(null, null, null);
319 return StructureTreeOwlImportConfigurator
.NewInstance(null);
322 MessagingUtils
.notImplementedMessage(this);
323 throw new IllegalArgumentException("Import not supported yet");
333 * @return a {@link eu.etaxonomy.cdm.io.jaxb.JaxbImportConfigurator} object.
335 public final JaxbImportConfigurator
JaxbConfigurator() {
336 return (JaxbImportConfigurator
) getConfigurator(TYPE
.Jaxb
);
344 * @return a {@link eu.etaxonomy.cdm.io.tcsxml.in.TcsXmlImportConfigurator}
347 public final TcsXmlImportConfigurator
TcsConfigurator() {
348 return (TcsXmlImportConfigurator
) getConfigurator(TYPE
.Tcs
);
353 * EndnoteConfigurator
357 * {@link eu.etaxonomy.cdm.io.reference.endnote.in.EndnoteImportConfigurator}
360 public final EndnoteImportConfigurator
EndnoteConfigurator() {
361 return (EndnoteImportConfigurator
) getConfigurator(TYPE
.Endnote
);
366 * NormalExplicitConfigurator
370 * {@link eu.etaxonomy.cdm.io.excel.taxa.NormalExplicitImportConfigurator}
373 public final NormalExplicitImportConfigurator
NormalExplicitConfigurator() {
374 return (NormalExplicitImportConfigurator
) getConfigurator(TYPE
.Excel_Taxa
);
382 * @return a {@link eu.etaxonomy.cdm.io.sdd.in.SDDImportConfigurator}
385 public final SDDImportConfigurator
SddConfigurator() {
386 return (SDDImportConfigurator
) getConfigurator(TYPE
.Sdd
);
395 * {@link eu.etaxonomy.cdm.io.specimen.abcd206.in.Abcd206ImportConfigurator}
398 public final Abcd206ImportConfigurator
AbcdConfigurator() {
399 return (Abcd206ImportConfigurator
) getConfigurator(TYPE
.Abcd
);
402 public SpecimenCdmExcelImportConfigurator
SpecimenCdmExcelImportConfigurator() {
403 return (SpecimenCdmExcelImportConfigurator
) getConfigurator(TYPE
.SpecimenCdmExcel
);
406 public DwcaImportConfigurator
DwcaImportConfigurator() {
407 return (DwcaImportConfigurator
) getConfigurator(TYPE
.Gbif
);
410 public final StructureTreeOwlImportConfigurator
OwlImportConfigurator() {
411 return (StructureTreeOwlImportConfigurator
) getConfigurator(TYPE
.OWL
);
419 public void postOperation(IRemotingProgressMonitor monitor
) {
420 Display
.getDefault().asyncExec(new Runnable() {
424 CdmStore
.getContextManager().notifyContextRefresh();
430 public Job
createIOServiceJob(final GbifImportConfigurator
<GbifImportState
, InputStream
> configurator
) {
431 Assert
.isNotNull(configurator
, "Configuration may not be null");
432 final Display display
= Display
.getDefault();
433 Job job
= new Job("Import: " + " Dwca") {
436 protected IStatus
run(IProgressMonitor monitor
) {
437 monitor
.beginTask("Importing data", IProgressMonitor
.UNKNOWN
);
438 IIOService ioService
= CdmApplicationState
.getIOService();
440 ImportResult result
= ioService
.importDataFromStream(configurator
);
443 // display.asyncExec(new Runnable() {
446 // public void run() {
447 // CdmStore.getContextManager().notifyContextRefresh();
450 showResult("Gbif Import", display
, result
, importSuccessMessage
);
451 return Status
.OK_STATUS
;
459 public Job
createIOServiceJob(final Abcd206ImportConfigurator configurator
) {
460 Assert
.isNotNull(configurator
, "Configuration may not be null");
461 final Display display
= Display
.getDefault();
462 Job job
= new Job("Import: " + " Dwca") {
465 protected IStatus
run(IProgressMonitor monitor
) {
466 monitor
.beginTask("Importing data", IProgressMonitor
.UNKNOWN
);
467 IIOService ioService
= CdmApplicationState
.getIOService();
469 ImportResult result
= ioService
.importDataFromStream(configurator
);
472 display
.asyncExec(new Runnable() {
476 CdmStore
.getContextManager().notifyContextRefresh();
479 showResult("Abcd Import", display
, result
, importSuccessMessage
);
480 return Status
.OK_STATUS
;
488 public Job
createIOServiceJob(
489 final List
<Abcd206ImportConfigurator
> abcdConfigurators
) {
490 Assert
.isNotNull(abcdConfigurators
, "Configuration may not be null");
491 final Display display
= Display
.getDefault();
492 Job job
= new Job("Import: " + " Abcd") {
495 protected IStatus
run(IProgressMonitor monitor
) {
496 monitor
.beginTask("Importing data", IProgressMonitor
.UNKNOWN
);
497 IIOService ioService
= CdmApplicationState
.getIOService();
499 ImportResult result
= ioService
.importDataFromStream(abcdConfigurators
);
502 display
.asyncExec(new Runnable() {
506 CdmStore
.getContextManager().notifyContextRefresh();
509 showResult("Abcd Import", display
, result
, importSuccessMessage
);
513 return Status
.OK_STATUS
;
520 public Job
createIOServiceJob(final ImportConfiguratorBase configurator
) {
521 Assert
.isNotNull(configurator
, "Configuration may not be null");
522 final Display display
= Display
.getDefault();
524 Job job
= new Job("Update: " + configurator
.getClass().getSimpleName()) {
527 protected IStatus
run(IProgressMonitor monitor
) {
528 monitor
.beginTask("Updating data", IProgressMonitor
.UNKNOWN
);
529 IIOService ioService
= CdmApplicationState
.getIOService();
530 ImportResult importResult
= null;
531 if (configurator
instanceof ExcelDistributionUpdateConfigurator
){
532 ExcelDistributionUpdateConfigurator config
= (ExcelDistributionUpdateConfigurator
) configurator
;
533 importResult
=ioService
.updateDistributionData(config
);
537 // display.asyncExec(new Runnable() {
540 // public void run() {
541 // CdmStore.getContextManager().notifyContextRefresh();
544 showResult(configurator
.getClass().getSimpleName(), display
, importResult
, updateSuccessMessage
);
547 return Status
.OK_STATUS
;
560 public ExcelDistributionUpdateConfigurator
ExcelDistributionUpdateConfigurator() {
561 return (ExcelDistributionUpdateConfigurator
) getConfigurator(TYPE
.Excel_Distribution
);