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
.distribution
.excelupdate
.ExcelDistributionUpdateConfigurator
;
38 import eu
.etaxonomy
.cdm
.io
.dwca
.in
.DwcaImportConfigurator
;
39 import eu
.etaxonomy
.cdm
.io
.excel
.taxa
.NormalExplicitImportConfigurator
;
40 import eu
.etaxonomy
.cdm
.io
.jaxb
.JaxbImportConfigurator
;
41 import eu
.etaxonomy
.cdm
.io
.reference
.endnote
.in
.EndnoteImportConfigurator
;
42 import eu
.etaxonomy
.cdm
.io
.sdd
.in
.SDDImportConfigurator
;
43 import eu
.etaxonomy
.cdm
.io
.service
.IIOService
;
44 import eu
.etaxonomy
.cdm
.io
.specimen
.abcd206
.in
.Abcd206ImportConfigurator
;
45 import eu
.etaxonomy
.cdm
.io
.specimen
.excel
.in
.SpecimenCdmExcelImportConfigurator
;
46 import eu
.etaxonomy
.cdm
.io
.specimen
.gbif
.in
.GbifImportConfigurator
;
47 import eu
.etaxonomy
.cdm
.io
.specimen
.gbif
.in
.GbifImportState
;
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
{
68 String importSuccessMessage
= "The import was successful.";
69 String updateSuccessMessage
= "The update was successful.";
72 * @param applicationConfiguration
74 private ImportManager(ICdmRepository applicationConfiguration
) {
75 super(applicationConfiguration
);
83 * @param applicationConfiguration
85 * {@link eu.etaxonomy.cdm.api.application.CdmApplicationController}
87 * @return a {@link eu.etaxonomy.taxeditor.io.ImportManager} object.
89 public static ImportManager
NewInstance(
90 ICdmRepository applicationConfiguration
) {
91 return new ImportManager(applicationConfiguration
);
95 * Use when importing data into the current application context
98 * a {@link eu.etaxonomy.cdm.io.common.IImportConfigurator}
100 * @return a {@link org.eclipse.core.runtime.jobs.Job} object.
103 protected Job
createIOJob(final IImportConfigurator configurator
) {
104 Assert
.isNotNull(configurator
, "Configuration may not be null");
106 final Display display
= Display
.getDefault();
108 Job job
= new Job("Import: " + configurator
.getClass().getSimpleName()) {
111 protected IStatus
run(IProgressMonitor monitor
) {
112 monitor
.beginTask("Importing data", 100);
113 configurator
.setProgressMonitor(CdmProgressMonitorAdapter
114 .CreateSubMonitor(monitor
, 70));
118 CdmDefaultImport
<IImportConfigurator
> importer
= new CdmDefaultImport
<IImportConfigurator
>();
119 importer
.setCdmAppController(applicationConfiguration
);
121 ImportResult importResult
= null;
123 importResult
= importer
.invoke(configurator
);
126 } catch (RuntimeException e
) {
127 MessagingUtils
.messageDialog(
128 "Error importing data",
130 "An error occurred while"
131 + "importing from source '"
132 + configurator
.getSourceNameString()
134 + "Please check error log for details.", e
);
141 final StringBuilder reportText
= new StringBuilder();
142 if(importResult
!=null){
143 List
<byte[]> reports
= importResult
.getReports();
144 for (byte[] bs
: reports
) {
145 reportText
.append(new String(bs
));
148 display
.asyncExec(new Runnable() {
152 // display reports with possibility to save
153 ReportTextDialog dialog
= new ReportTextDialog(PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell());
154 dialog
.setTitle(configurator
.getClass().getSimpleName()+" Report");
155 dialog
.setReportText(reportText
.toString());
157 CdmStore
.getContextManager().notifyContextRefresh();
161 return Status
.OK_STATUS
;
169 public Job
createIOServiceJob(IImportConfigurator configurator
, InputStream is
, SOURCE_TYPE type
) {
172 return createIOServiceJob(configurator
, IOUtils
.toByteArray(is
), type
);
173 } catch (Exception e
) {
174 MessagingUtils
.errorDialog("Error importing input stream",
177 TaxeditorStorePlugin
.PLUGIN_ID
,
184 public Job
createIOServiceJob(IImportConfigurator configurator
, File importFile
, SOURCE_TYPE type
) {
185 Path path
= Paths
.get(importFile
.toURI());
187 return createIOServiceJob(configurator
, Files
.readAllBytes(path
), type
);
188 } catch (Exception e
) {
189 MessagingUtils
.errorDialog("Error importing file",
192 TaxeditorStorePlugin
.PLUGIN_ID
,
199 public Job
createIOServiceJob(final IImportConfigurator configurator
, final byte[] data
, final SOURCE_TYPE type
) {
200 Assert
.isNotNull(configurator
, "Configuration may not be null");
201 final Display display
= Display
.getDefault();
202 Job job
= new Job("Import: " + configurator
.getClass().getSimpleName()) {
205 protected IStatus
run(IProgressMonitor monitor
) {
206 monitor
.beginTask("Importing data", IProgressMonitor
.UNKNOWN
);
207 IIOService ioService
= CdmApplicationState
.getIOService();
209 ImportResult result
= ioService
.importData(configurator
, data
, type
);
213 display
.asyncExec(new Runnable() {
217 CdmStore
.getContextManager().notifyContextRefresh();
220 showResult(configurator
.getClass().getSimpleName(), display
, result
, importSuccessMessage
);
221 return Status
.OK_STATUS
;
229 public void runMoniteredOperation(IImportConfigurator configurator
, InputStream is
, SOURCE_TYPE type
) {
232 runMoniteredOperation(configurator
, IOUtils
.toByteArray(is
), type
);
233 } catch (Exception e
) {
234 MessagingUtils
.errorDialog("Error importing input stream",
237 TaxeditorStorePlugin
.PLUGIN_ID
,
244 public void runMoniteredOperation(IImportConfigurator configurator
, SOURCE_TYPE type
) {
245 byte[] data
= new byte[1];
247 runMoniteredOperation(configurator
, data
, type
);
248 } catch (Exception e
) {
249 MessagingUtils
.errorDialog("Error importing input stream",
252 TaxeditorStorePlugin
.PLUGIN_ID
,
259 public void runMoniteredOperation(IImportConfigurator configurator
, File importFile
, SOURCE_TYPE type
) {
260 Path path
= Paths
.get(importFile
.toURI());
262 runMoniteredOperation(configurator
, Files
.readAllBytes(path
), type
);
264 } catch (Exception e
) {
265 MessagingUtils
.errorDialog("Error importing input stream",
268 TaxeditorStorePlugin
.PLUGIN_ID
,
275 public void runMoniteredOperation(final IImportConfigurator configurator
, final byte[] data
, final SOURCE_TYPE type
) {
276 IIOService ioService
= CdmApplicationState
.getIOService();
277 final UUID uuid
= ioService
.monitImportData(configurator
, data
, type
);
278 Display
.getDefault().asyncExec(new Runnable() {
281 AbstractUtility
.executeMoniteredOperation("Import: " + configurator
.getClass().getSimpleName(),
292 private IImportConfigurator
getConfigurator(TYPE type
) {
293 Assert
.isNotNull(type
, "Type should not be null");
297 return JaxbImportConfigurator
.NewInstance(null, null);
299 return TcsXmlImportConfigurator
.NewInstance(null, null);
301 return EndnoteImportConfigurator
.NewInstance(null, null);
303 return NormalExplicitImportConfigurator
.NewInstance(null, null,
306 return Abcd206ImportConfigurator
.NewInstance(null, null);
308 return SDDImportConfigurator
.NewInstance(null, null);
309 case SpecimenCdmExcel
:
310 return SpecimenCdmExcelImportConfigurator
.NewInstance(null, null);
312 return Abcd206ImportConfigurator
.NewInstance(null, null);
314 return DwcaImportConfigurator
.NewInstance(null, null);
315 case Excel_Distribution
:
316 return ExcelDistributionUpdateConfigurator
.NewInstance(null, null, null);
319 MessagingUtils
.notImplementedMessage(this);
320 throw new IllegalArgumentException("Import not supported yet");
330 * @return a {@link eu.etaxonomy.cdm.io.jaxb.JaxbImportConfigurator} object.
332 public final JaxbImportConfigurator
JaxbConfigurator() {
333 return (JaxbImportConfigurator
) getConfigurator(TYPE
.Jaxb
);
341 * @return a {@link eu.etaxonomy.cdm.io.tcsxml.in.TcsXmlImportConfigurator}
344 public final TcsXmlImportConfigurator
TcsConfigurator() {
345 return (TcsXmlImportConfigurator
) getConfigurator(TYPE
.Tcs
);
350 * EndnoteConfigurator
354 * {@link eu.etaxonomy.cdm.io.reference.endnote.in.EndnoteImportConfigurator}
357 public final EndnoteImportConfigurator
EndnoteConfigurator() {
358 return (EndnoteImportConfigurator
) getConfigurator(TYPE
.Endnote
);
363 * NormalExplicitConfigurator
367 * {@link eu.etaxonomy.cdm.io.excel.taxa.NormalExplicitImportConfigurator}
370 public final NormalExplicitImportConfigurator
NormalExplicitConfigurator() {
371 return (NormalExplicitImportConfigurator
) getConfigurator(TYPE
.Excel_Taxa
);
379 * @return a {@link eu.etaxonomy.cdm.io.sdd.in.SDDImportConfigurator}
382 public final SDDImportConfigurator
SddConfigurator() {
383 return (SDDImportConfigurator
) getConfigurator(TYPE
.Sdd
);
392 * {@link eu.etaxonomy.cdm.io.specimen.abcd206.in.Abcd206ImportConfigurator}
395 public final Abcd206ImportConfigurator
AbcdConfigurator() {
396 return (Abcd206ImportConfigurator
) getConfigurator(TYPE
.Abcd
);
399 public SpecimenCdmExcelImportConfigurator
SpecimenCdmExcelImportConfigurator() {
400 return (SpecimenCdmExcelImportConfigurator
) getConfigurator(TYPE
.SpecimenCdmExcel
);
403 public DwcaImportConfigurator
DwcaImportConfigurator() {
404 return (DwcaImportConfigurator
) getConfigurator(TYPE
.Gbif
);
412 public void postOperation(IRemotingProgressMonitor monitor
) {
413 Display
.getDefault().asyncExec(new Runnable() {
417 CdmStore
.getContextManager().notifyContextRefresh();
423 public Job
createIOServiceJob(final GbifImportConfigurator
<GbifImportState
, InputStream
> configurator
) {
424 Assert
.isNotNull(configurator
, "Configuration may not be null");
425 final Display display
= Display
.getDefault();
426 Job job
= new Job("Import: " + " Dwca") {
429 protected IStatus
run(IProgressMonitor monitor
) {
430 monitor
.beginTask("Importing data", IProgressMonitor
.UNKNOWN
);
431 IIOService ioService
= CdmApplicationState
.getIOService();
433 ImportResult result
= ioService
.importDataFromStream(configurator
);
436 // display.asyncExec(new Runnable() {
439 // public void run() {
440 // CdmStore.getContextManager().notifyContextRefresh();
443 showResult("Gbif Import", display
, result
, importSuccessMessage
);
444 return Status
.OK_STATUS
;
452 public Job
createIOServiceJob(final Abcd206ImportConfigurator configurator
) {
453 Assert
.isNotNull(configurator
, "Configuration may not be null");
454 final Display display
= Display
.getDefault();
455 Job job
= new Job("Import: " + " Dwca") {
458 protected IStatus
run(IProgressMonitor monitor
) {
459 monitor
.beginTask("Importing data", IProgressMonitor
.UNKNOWN
);
460 IIOService ioService
= CdmApplicationState
.getIOService();
462 ImportResult result
= ioService
.importDataFromStream(configurator
);
465 display
.asyncExec(new Runnable() {
469 CdmStore
.getContextManager().notifyContextRefresh();
472 showResult("Abcd Import", display
, result
, importSuccessMessage
);
473 return Status
.OK_STATUS
;
481 public Job
createIOServiceJob(
482 final List
<Abcd206ImportConfigurator
> abcdConfigurators
) {
483 Assert
.isNotNull(abcdConfigurators
, "Configuration may not be null");
484 final Display display
= Display
.getDefault();
485 Job job
= new Job("Import: " + " Abcd") {
488 protected IStatus
run(IProgressMonitor monitor
) {
489 monitor
.beginTask("Importing data", IProgressMonitor
.UNKNOWN
);
490 IIOService ioService
= CdmApplicationState
.getIOService();
492 ImportResult result
= ioService
.importDataFromStream(abcdConfigurators
);
495 display
.asyncExec(new Runnable() {
499 CdmStore
.getContextManager().notifyContextRefresh();
502 showResult("Abcd Import", display
, result
, importSuccessMessage
);
506 return Status
.OK_STATUS
;
513 public Job
createIOServiceJob(final ImportConfiguratorBase configurator
) {
514 Assert
.isNotNull(configurator
, "Configuration may not be null");
515 final Display display
= Display
.getDefault();
517 Job job
= new Job("Update: " + configurator
.getClass().getSimpleName()) {
520 protected IStatus
run(IProgressMonitor monitor
) {
521 monitor
.beginTask("Updating data", IProgressMonitor
.UNKNOWN
);
522 IIOService ioService
= CdmApplicationState
.getIOService();
523 ImportResult importResult
= null;
524 if (configurator
instanceof ExcelDistributionUpdateConfigurator
){
525 ExcelDistributionUpdateConfigurator config
= (ExcelDistributionUpdateConfigurator
) configurator
;
526 importResult
=ioService
.updateDistributionData(config
);
530 // display.asyncExec(new Runnable() {
533 // public void run() {
534 // CdmStore.getContextManager().notifyContextRefresh();
537 showResult(configurator
.getClass().getSimpleName(), display
, importResult
, updateSuccessMessage
);
540 return Status
.OK_STATUS
;
553 public ExcelDistributionUpdateConfigurator
ExcelDistributionUpdateConfigurator() {
554 return (ExcelDistributionUpdateConfigurator
) getConfigurator(TYPE
.Excel_Distribution
);