-// $Id$
/**
* Copyright (C) 2007 EDIT
* European Distributed Institute of Taxonomy
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
+import java.util.List;
+import java.util.UUID;
import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang.StringUtils;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.swt.widgets.Display;
+import org.eclipse.ui.PlatformUI;
import eu.etaxonomy.cdm.api.application.CdmApplicationState;
-import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
+import eu.etaxonomy.cdm.api.application.ICdmRepository;
+import eu.etaxonomy.cdm.common.monitor.IRemotingProgressMonitor;
+import eu.etaxonomy.cdm.io.common.CacheUpdaterConfigurator;
import eu.etaxonomy.cdm.io.common.CdmDefaultImport;
import eu.etaxonomy.cdm.io.common.IImportConfigurator;
import eu.etaxonomy.cdm.io.common.IImportConfigurator.SOURCE_TYPE;
+import eu.etaxonomy.cdm.io.common.ImportConfiguratorBase;
+import eu.etaxonomy.cdm.io.common.ImportResult;
+import eu.etaxonomy.cdm.io.common.SortIndexUpdaterConfigurator;
+import eu.etaxonomy.cdm.io.distribution.excelupdate.ExcelDistributionUpdateConfigurator;
+import eu.etaxonomy.cdm.io.dwca.in.DwcaImportConfigurator;
import eu.etaxonomy.cdm.io.excel.taxa.NormalExplicitImportConfigurator;
import eu.etaxonomy.cdm.io.jaxb.JaxbImportConfigurator;
import eu.etaxonomy.cdm.io.reference.endnote.in.EndnoteImportConfigurator;
import eu.etaxonomy.cdm.io.service.IIOService;
import eu.etaxonomy.cdm.io.specimen.abcd206.in.Abcd206ImportConfigurator;
import eu.etaxonomy.cdm.io.specimen.excel.in.SpecimenCdmExcelImportConfigurator;
+import eu.etaxonomy.cdm.io.specimen.gbif.in.GbifImportConfigurator;
+import eu.etaxonomy.cdm.io.specimen.gbif.in.GbifImportState;
import eu.etaxonomy.cdm.io.tcsxml.in.TcsXmlImportConfigurator;
+import eu.etaxonomy.taxeditor.model.AbstractUtility;
import eu.etaxonomy.taxeditor.model.CdmProgressMonitorAdapter;
import eu.etaxonomy.taxeditor.model.MessagingUtils;
+import eu.etaxonomy.taxeditor.operation.IPostMoniteredOperationEnabled;
import eu.etaxonomy.taxeditor.store.CdmStore;
import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
+import eu.etaxonomy.taxeditor.ui.dialog.ReportTextDialog;
/**
* <p>
* @created Sep 11, 2009
* @version 1.0
*/
-public class ImportManager extends AbstractIOManager<IImportConfigurator> {
+public class ImportManager extends AbstractIOManager<IImportConfigurator> implements IPostMoniteredOperationEnabled {
/**
* @param applicationConfiguration
*/
- private ImportManager(ICdmApplicationConfiguration applicationConfiguration) {
+ private ImportManager(ICdmRepository applicationConfiguration) {
super(applicationConfiguration);
}
* @return a {@link eu.etaxonomy.taxeditor.io.ImportManager} object.
*/
public static ImportManager NewInstance(
- ICdmApplicationConfiguration applicationConfiguration) {
+ ICdmRepository applicationConfiguration) {
return new ImportManager(applicationConfiguration);
}
CdmDefaultImport<IImportConfigurator> importer = new CdmDefaultImport<IImportConfigurator>();
importer.setCdmAppController(applicationConfiguration);
monitor.worked(5);
-
+ ImportResult importResult = null;
try {
- importer.invoke(configurator);
+ importResult = importer.invoke(configurator);
+
monitor.worked(80);
} catch (RuntimeException e) {
MessagingUtils.messageDialog(
monitor.worked(5);
monitor.done();
+
+ final StringBuilder reportText = new StringBuilder();
+ if(importResult!=null){
+ List<byte[]> reports = importResult.getReports();
+ for (byte[] bs : reports) {
+ reportText.append(new String(bs));
+ }
+ }
display.asyncExec(new Runnable() {
@Override
public void run() {
+ // display reports with possibility to save
+ ReportTextDialog dialog = new ReportTextDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
+ dialog.setTitle(configurator.getClass().getSimpleName()+" Report");
+ dialog.setReportText(reportText.toString());
+ dialog.open();
CdmStore.getContextManager().notifyContextRefresh();
}
});
monitor.beginTask("Importing data", IProgressMonitor.UNKNOWN);
IIOService ioService = CdmApplicationState.getIOService();
- ioService.importData(configurator, data, type);
+ ImportResult result = ioService.importData(configurator, data, type);
monitor.done();
CdmStore.getContextManager().notifyContextRefresh();
}
});
-
+ showResult(configurator.getClass().getSimpleName(), display, result);
return Status.OK_STATUS;
}
};
return job;
+ }
+
+ public void runMoniteredOperation(IImportConfigurator configurator, InputStream is, SOURCE_TYPE type) {
+
+ try {
+ runMoniteredOperation(configurator, IOUtils.toByteArray(is), type);
+ } catch (Exception e) {
+ MessagingUtils.errorDialog("Error importing input stream",
+ this,
+ e.getMessage(),
+ TaxeditorStorePlugin.PLUGIN_ID,
+ e,
+ true);
+ }
+
+ }
+
+ public void runMoniteredOperation(IImportConfigurator configurator, SOURCE_TYPE type) {
+ byte[] data = new byte[1];
+ try {
+ runMoniteredOperation(configurator, data, type);
+ } catch (Exception e) {
+ MessagingUtils.errorDialog("Error importing input stream",
+ this,
+ e.getMessage(),
+ TaxeditorStorePlugin.PLUGIN_ID,
+ e,
+ true);
+ }
+
+ }
+
+ public void runMoniteredOperation(IImportConfigurator configurator, File importFile, SOURCE_TYPE type) {
+ Path path = Paths.get(importFile.toURI());
+ try {
+ runMoniteredOperation(configurator, Files.readAllBytes(path), type);
+
+ } catch (Exception e) {
+ MessagingUtils.errorDialog("Error importing input stream",
+ this,
+ e.getMessage(),
+ TaxeditorStorePlugin.PLUGIN_ID,
+ e,
+ true);
+ }
+
+ }
+
+ public void runMoniteredOperation(final IImportConfigurator configurator, final byte[] data, final SOURCE_TYPE type) {
+ IIOService ioService = CdmApplicationState.getIOService();
+ final UUID uuid = ioService.monitImportData(configurator, data, type);
+ Display.getDefault().asyncExec(new Runnable() {
+ @Override
+ public void run() {
+ AbstractUtility.executeMoniteredOperation("Import: " + configurator.getClass().getSimpleName(),
+ uuid,
+ 1000,
+ false,
+ ImportManager.this,
+ null);
+ }
+ });
+
}
private IImportConfigurator getConfigurator(TYPE type) {
return SDDImportConfigurator.NewInstance(null, null);
case SpecimenCdmExcel:
return SpecimenCdmExcelImportConfigurator.NewInstance(null, null);
+ case SpecimenSearch:
+ return Abcd206ImportConfigurator.NewInstance(null, null);
+ case Gbif:
+ return DwcaImportConfigurator.NewInstance(null, null);
+ case Excel_Distribution:
+ return ExcelDistributionUpdateConfigurator.NewInstance(null, null, null);
+
default:
MessagingUtils.notImplementedMessage(this);
throw new IllegalArgumentException("Import not supported yet");
return (SpecimenCdmExcelImportConfigurator) getConfigurator(TYPE.SpecimenCdmExcel);
}
+ public DwcaImportConfigurator DwcaImportConfigurator() {
+ return (DwcaImportConfigurator) getConfigurator(TYPE.Gbif);
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void postOperation(IRemotingProgressMonitor monitor) {
+ Display.getDefault().asyncExec(new Runnable() {
+
+ @Override
+ public void run() {
+ CdmStore.getContextManager().notifyContextRefresh();
+ }
+ });
+
+ }
+
+ public Job createIOServiceJob(final GbifImportConfigurator<GbifImportState, InputStream> configurator) {
+ Assert.isNotNull(configurator, "Configuration may not be null");
+ final Display display = Display.getDefault();
+ Job job = new Job("Import: " + " Dwca") {
+
+ @Override
+ protected IStatus run(IProgressMonitor monitor) {
+ monitor.beginTask("Importing data", IProgressMonitor.UNKNOWN);
+ IIOService ioService = CdmApplicationState.getIOService();
+
+ ImportResult result = ioService.importDataFromStream(configurator);
+ monitor.done();
+
+// display.asyncExec(new Runnable() {
+//
+// @Override
+// public void run() {
+// CdmStore.getContextManager().notifyContextRefresh();
+// }
+// });
+ showResult("Gbif Import", display, result);
+ return Status.OK_STATUS;
+ }
+ };
+
+ return job;
+
+ }
+
+ public Job createIOServiceJob(final Abcd206ImportConfigurator configurator) {
+ Assert.isNotNull(configurator, "Configuration may not be null");
+ final Display display = Display.getDefault();
+ Job job = new Job("Import: " + " Dwca") {
+
+ @Override
+ protected IStatus run(IProgressMonitor monitor) {
+ monitor.beginTask("Importing data", IProgressMonitor.UNKNOWN);
+ IIOService ioService = CdmApplicationState.getIOService();
+
+ ImportResult result = ioService.importDataFromStream(configurator);
+ monitor.done();
+
+ display.asyncExec(new Runnable() {
+
+ @Override
+ public void run() {
+ CdmStore.getContextManager().notifyContextRefresh();
+ }
+ });
+ showResult("Abcd Import", display, result);
+ return Status.OK_STATUS;
+ }
+ };
+
+ return job;
+
+ }
+
+ public Job createIOServiceJob(
+ final List<Abcd206ImportConfigurator> abcdConfigurators) {
+ Assert.isNotNull(abcdConfigurators, "Configuration may not be null");
+ final Display display = Display.getDefault();
+ Job job = new Job("Import: " + " Abcd") {
+
+ @Override
+ protected IStatus run(IProgressMonitor monitor) {
+ monitor.beginTask("Importing data", IProgressMonitor.UNKNOWN);
+ IIOService ioService = CdmApplicationState.getIOService();
+
+ ImportResult result = ioService.importDataFromStream(abcdConfigurators);
+ monitor.done();
+
+ display.asyncExec(new Runnable() {
+
+ @Override
+ public void run() {
+ CdmStore.getContextManager().notifyContextRefresh();
+ }
+ });
+ showResult("Abcd Import", display, result);
+
+
+
+ return Status.OK_STATUS;
+ }
+ };
+
+ return job;
+ }
+
+ public Job createIOServiceJob(final ImportConfiguratorBase configurator) {
+ Assert.isNotNull(configurator, "Configuration may not be null");
+ final Display display = Display.getDefault();
+
+ Job job = new Job("Update: " + configurator.getClass().getSimpleName()) {
+
+ @Override
+ protected IStatus run(IProgressMonitor monitor) {
+ monitor.beginTask("Updating data", IProgressMonitor.UNKNOWN);
+ IIOService ioService = CdmApplicationState.getIOService();
+ ImportResult importResult = null;
+ if (configurator instanceof SortIndexUpdaterConfigurator){
+ SortIndexUpdaterConfigurator config = (SortIndexUpdaterConfigurator)configurator;
+ importResult =ioService.updateSortIndex(config);
+ } else if (configurator instanceof CacheUpdaterConfigurator){
+ CacheUpdaterConfigurator config = (CacheUpdaterConfigurator) configurator;
+ importResult = ioService.updateCaches(config);
+ } else if (configurator instanceof ExcelDistributionUpdateConfigurator){
+ ExcelDistributionUpdateConfigurator config = (ExcelDistributionUpdateConfigurator) configurator;
+ importResult =ioService.updateDistributionData(config);
+ }
+
+ monitor.done();
+// display.asyncExec(new Runnable() {
+//
+// @Override
+// public void run() {
+// CdmStore.getContextManager().notifyContextRefresh();
+// }
+// });
+ showResult(configurator.getClass().getSimpleName(), display, importResult);
+
+
+ return Status.OK_STATUS;
+ }
+
+
+ };
+
+ return job;
+
+ }
+
+ /**
+ * @return
+ */
+ public ExcelDistributionUpdateConfigurator ExcelDistributionUpdateConfigurator() {
+ return (ExcelDistributionUpdateConfigurator) getConfigurator(TYPE.Excel_Distribution);
+ }
+
+
+
+
+
+
}