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
.apache
.commons
.lang
.StringUtils
;
22 import org
.eclipse
.core
.runtime
.Assert
;
23 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
24 import org
.eclipse
.core
.runtime
.IStatus
;
25 import org
.eclipse
.core
.runtime
.Status
;
26 import org
.eclipse
.core
.runtime
.jobs
.Job
;
27 import org
.eclipse
.swt
.widgets
.Display
;
28 import org
.eclipse
.ui
.PlatformUI
;
30 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationState
;
31 import eu
.etaxonomy
.cdm
.api
.application
.ICdmRepository
;
32 import eu
.etaxonomy
.cdm
.common
.monitor
.IRemotingProgressMonitor
;
33 import eu
.etaxonomy
.cdm
.io
.common
.CacheUpdaterConfigurator
;
34 import eu
.etaxonomy
.cdm
.io
.common
.CdmDefaultImport
;
35 import eu
.etaxonomy
.cdm
.io
.common
.IImportConfigurator
;
36 import eu
.etaxonomy
.cdm
.io
.common
.IImportConfigurator
.SOURCE_TYPE
;
37 import eu
.etaxonomy
.cdm
.io
.common
.ImportConfiguratorBase
;
38 import eu
.etaxonomy
.cdm
.io
.common
.ImportResult
;
39 import eu
.etaxonomy
.cdm
.io
.common
.SortIndexUpdaterConfigurator
;
40 import eu
.etaxonomy
.cdm
.io
.distribution
.excelupdate
.ExcelDistributionUpdateConfigurator
;
41 import eu
.etaxonomy
.cdm
.io
.dwca
.in
.DwcaImportConfigurator
;
42 import eu
.etaxonomy
.cdm
.io
.excel
.taxa
.NormalExplicitImportConfigurator
;
43 import eu
.etaxonomy
.cdm
.io
.jaxb
.JaxbImportConfigurator
;
44 import eu
.etaxonomy
.cdm
.io
.reference
.endnote
.in
.EndnoteImportConfigurator
;
45 import eu
.etaxonomy
.cdm
.io
.sdd
.in
.SDDImportConfigurator
;
46 import eu
.etaxonomy
.cdm
.io
.service
.IIOService
;
47 import eu
.etaxonomy
.cdm
.io
.specimen
.abcd206
.in
.Abcd206ImportConfigurator
;
48 import eu
.etaxonomy
.cdm
.io
.specimen
.excel
.in
.SpecimenCdmExcelImportConfigurator
;
49 import eu
.etaxonomy
.cdm
.io
.specimen
.gbif
.in
.GbifImportConfigurator
;
50 import eu
.etaxonomy
.cdm
.io
.specimen
.gbif
.in
.GbifImportState
;
51 import eu
.etaxonomy
.cdm
.io
.tcsxml
.in
.TcsXmlImportConfigurator
;
52 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
53 import eu
.etaxonomy
.taxeditor
.model
.CdmProgressMonitorAdapter
;
54 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
55 import eu
.etaxonomy
.taxeditor
.operation
.IPostMoniteredOperationEnabled
;
56 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
57 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
58 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.ReportTextDialog
;
62 * ImportHandler class.
66 * @created Sep 11, 2009
69 public class ImportManager
extends AbstractIOManager
<IImportConfigurator
> implements IPostMoniteredOperationEnabled
{
71 String importSuccessMessage
= "The import was successfull.";
72 String updateSuccessMessage
= "The update was successfull.";
75 * @param applicationConfiguration
77 private ImportManager(ICdmRepository applicationConfiguration
) {
78 super(applicationConfiguration
);
86 * @param applicationConfiguration
88 * {@link eu.etaxonomy.cdm.api.application.CdmApplicationController}
90 * @return a {@link eu.etaxonomy.taxeditor.io.ImportManager} object.
92 public static ImportManager
NewInstance(
93 ICdmRepository applicationConfiguration
) {
94 return new ImportManager(applicationConfiguration
);
98 * Use when importing data into the current application context
100 * @param configurator
101 * a {@link eu.etaxonomy.cdm.io.common.IImportConfigurator}
103 * @return a {@link org.eclipse.core.runtime.jobs.Job} object.
106 protected Job
createIOJob(final IImportConfigurator configurator
) {
107 Assert
.isNotNull(configurator
, "Configuration may not be null");
109 final Display display
= Display
.getDefault();
111 Job job
= new Job("Import: " + configurator
.getClass().getSimpleName()) {
114 protected IStatus
run(IProgressMonitor monitor
) {
115 monitor
.beginTask("Importing data", 100);
116 configurator
.setProgressMonitor(CdmProgressMonitorAdapter
117 .CreateSubMonitor(monitor
, 70));
121 CdmDefaultImport
<IImportConfigurator
> importer
= new CdmDefaultImport
<IImportConfigurator
>();
122 importer
.setCdmAppController(applicationConfiguration
);
124 ImportResult importResult
= null;
126 importResult
= importer
.invoke(configurator
);
129 } catch (RuntimeException e
) {
130 MessagingUtils
.messageDialog(
131 "Error importing data",
133 "An error occured while"
134 + "importing from source '"
135 + configurator
.getSourceNameString()
137 + "Please check error log for details.", e
);
144 final StringBuilder reportText
= new StringBuilder();
145 if(importResult
!=null){
146 List
<byte[]> reports
= importResult
.getReports();
147 for (byte[] bs
: reports
) {
148 reportText
.append(new String(bs
));
151 display
.asyncExec(new Runnable() {
155 // display reports with possibility to save
156 ReportTextDialog dialog
= new ReportTextDialog(PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell());
157 dialog
.setTitle(configurator
.getClass().getSimpleName()+" Report");
158 dialog
.setReportText(reportText
.toString());
160 CdmStore
.getContextManager().notifyContextRefresh();
164 return Status
.OK_STATUS
;
172 public Job
createIOServiceJob(IImportConfigurator configurator
, InputStream is
, SOURCE_TYPE type
) {
175 return createIOServiceJob(configurator
, IOUtils
.toByteArray(is
), type
);
176 } catch (Exception e
) {
177 MessagingUtils
.errorDialog("Error importing input stream",
180 TaxeditorStorePlugin
.PLUGIN_ID
,
187 public Job
createIOServiceJob(IImportConfigurator configurator
, File importFile
, SOURCE_TYPE type
) {
188 Path path
= Paths
.get(importFile
.toURI());
190 return createIOServiceJob(configurator
, Files
.readAllBytes(path
), type
);
191 } catch (Exception e
) {
192 MessagingUtils
.errorDialog("Error importing file",
195 TaxeditorStorePlugin
.PLUGIN_ID
,
202 public Job
createIOServiceJob(final IImportConfigurator configurator
, final byte[] data
, final SOURCE_TYPE type
) {
203 Assert
.isNotNull(configurator
, "Configuration may not be null");
204 final Display display
= Display
.getDefault();
205 Job job
= new Job("Import: " + configurator
.getClass().getSimpleName()) {
208 protected IStatus
run(IProgressMonitor monitor
) {
209 monitor
.beginTask("Importing data", IProgressMonitor
.UNKNOWN
);
210 IIOService ioService
= CdmApplicationState
.getIOService();
212 ImportResult result
= ioService
.importData(configurator
, data
, type
);
216 display
.asyncExec(new Runnable() {
220 CdmStore
.getContextManager().notifyContextRefresh();
223 showResult(configurator
.getClass().getSimpleName(), display
, result
, importSuccessMessage
);
224 return Status
.OK_STATUS
;
232 public void runMoniteredOperation(IImportConfigurator configurator
, InputStream is
, SOURCE_TYPE type
) {
235 runMoniteredOperation(configurator
, IOUtils
.toByteArray(is
), type
);
236 } catch (Exception e
) {
237 MessagingUtils
.errorDialog("Error importing input stream",
240 TaxeditorStorePlugin
.PLUGIN_ID
,
247 public void runMoniteredOperation(IImportConfigurator configurator
, SOURCE_TYPE type
) {
248 byte[] data
= new byte[1];
250 runMoniteredOperation(configurator
, data
, type
);
251 } catch (Exception e
) {
252 MessagingUtils
.errorDialog("Error importing input stream",
255 TaxeditorStorePlugin
.PLUGIN_ID
,
262 public void runMoniteredOperation(IImportConfigurator configurator
, File importFile
, SOURCE_TYPE type
) {
263 Path path
= Paths
.get(importFile
.toURI());
265 runMoniteredOperation(configurator
, Files
.readAllBytes(path
), type
);
267 } catch (Exception e
) {
268 MessagingUtils
.errorDialog("Error importing input stream",
271 TaxeditorStorePlugin
.PLUGIN_ID
,
278 public void runMoniteredOperation(final IImportConfigurator configurator
, final byte[] data
, final SOURCE_TYPE type
) {
279 IIOService ioService
= CdmApplicationState
.getIOService();
280 final UUID uuid
= ioService
.monitImportData(configurator
, data
, type
);
281 Display
.getDefault().asyncExec(new Runnable() {
284 AbstractUtility
.executeMoniteredOperation("Import: " + configurator
.getClass().getSimpleName(),
295 private IImportConfigurator
getConfigurator(TYPE type
) {
296 Assert
.isNotNull(type
, "Type should not be null");
300 return JaxbImportConfigurator
.NewInstance(null, null);
302 return TcsXmlImportConfigurator
.NewInstance(null, null);
304 return EndnoteImportConfigurator
.NewInstance(null, null);
306 return NormalExplicitImportConfigurator
.NewInstance(null, null,
309 return Abcd206ImportConfigurator
.NewInstance(null, null);
311 return SDDImportConfigurator
.NewInstance(null, null);
312 case SpecimenCdmExcel
:
313 return SpecimenCdmExcelImportConfigurator
.NewInstance(null, null);
315 return Abcd206ImportConfigurator
.NewInstance(null, null);
317 return DwcaImportConfigurator
.NewInstance(null, null);
318 case Excel_Distribution
:
319 return ExcelDistributionUpdateConfigurator
.NewInstance(null, null, 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
);
415 public void postOperation(IRemotingProgressMonitor monitor
) {
416 Display
.getDefault().asyncExec(new Runnable() {
420 CdmStore
.getContextManager().notifyContextRefresh();
426 public Job
createIOServiceJob(final GbifImportConfigurator
<GbifImportState
, InputStream
> configurator
) {
427 Assert
.isNotNull(configurator
, "Configuration may not be null");
428 final Display display
= Display
.getDefault();
429 Job job
= new Job("Import: " + " Dwca") {
432 protected IStatus
run(IProgressMonitor monitor
) {
433 monitor
.beginTask("Importing data", IProgressMonitor
.UNKNOWN
);
434 IIOService ioService
= CdmApplicationState
.getIOService();
436 ImportResult result
= ioService
.importDataFromStream(configurator
);
439 // display.asyncExec(new Runnable() {
442 // public void run() {
443 // CdmStore.getContextManager().notifyContextRefresh();
446 showResult("Gbif Import", display
, result
, importSuccessMessage
);
447 return Status
.OK_STATUS
;
455 public Job
createIOServiceJob(final Abcd206ImportConfigurator configurator
) {
456 Assert
.isNotNull(configurator
, "Configuration may not be null");
457 final Display display
= Display
.getDefault();
458 Job job
= new Job("Import: " + " Dwca") {
461 protected IStatus
run(IProgressMonitor monitor
) {
462 monitor
.beginTask("Importing data", IProgressMonitor
.UNKNOWN
);
463 IIOService ioService
= CdmApplicationState
.getIOService();
465 ImportResult result
= ioService
.importDataFromStream(configurator
);
468 display
.asyncExec(new Runnable() {
472 CdmStore
.getContextManager().notifyContextRefresh();
475 showResult("Abcd Import", display
, result
, importSuccessMessage
);
476 return Status
.OK_STATUS
;
484 public Job
createIOServiceJob(
485 final List
<Abcd206ImportConfigurator
> abcdConfigurators
) {
486 Assert
.isNotNull(abcdConfigurators
, "Configuration may not be null");
487 final Display display
= Display
.getDefault();
488 Job job
= new Job("Import: " + " Abcd") {
491 protected IStatus
run(IProgressMonitor monitor
) {
492 monitor
.beginTask("Importing data", IProgressMonitor
.UNKNOWN
);
493 IIOService ioService
= CdmApplicationState
.getIOService();
495 ImportResult result
= ioService
.importDataFromStream(abcdConfigurators
);
498 display
.asyncExec(new Runnable() {
502 CdmStore
.getContextManager().notifyContextRefresh();
505 showResult("Abcd Import", display
, result
, importSuccessMessage
);
509 return Status
.OK_STATUS
;
516 public Job
createIOServiceJob(final ImportConfiguratorBase configurator
) {
517 Assert
.isNotNull(configurator
, "Configuration may not be null");
518 final Display display
= Display
.getDefault();
520 Job job
= new Job("Update: " + configurator
.getClass().getSimpleName()) {
523 protected IStatus
run(IProgressMonitor monitor
) {
524 monitor
.beginTask("Updating data", IProgressMonitor
.UNKNOWN
);
525 IIOService ioService
= CdmApplicationState
.getIOService();
526 ImportResult importResult
= null;
527 if (configurator
instanceof SortIndexUpdaterConfigurator
){
528 SortIndexUpdaterConfigurator config
= (SortIndexUpdaterConfigurator
)configurator
;
529 importResult
=ioService
.updateSortIndex(config
);
530 } else if (configurator
instanceof CacheUpdaterConfigurator
){
531 CacheUpdaterConfigurator config
= (CacheUpdaterConfigurator
) configurator
;
532 importResult
= ioService
.updateCaches(config
);
533 } else if (configurator
instanceof ExcelDistributionUpdateConfigurator
){
534 ExcelDistributionUpdateConfigurator config
= (ExcelDistributionUpdateConfigurator
) configurator
;
535 importResult
=ioService
.updateDistributionData(config
);
539 // display.asyncExec(new Runnable() {
542 // public void run() {
543 // CdmStore.getContextManager().notifyContextRefresh();
546 showResult(configurator
.getClass().getSimpleName(), display
, importResult
, updateSuccessMessage
);
549 return Status
.OK_STATUS
;
562 public ExcelDistributionUpdateConfigurator
ExcelDistributionUpdateConfigurator() {
563 return (ExcelDistributionUpdateConfigurator
) getConfigurator(TYPE
.Excel_Distribution
);