1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
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.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.taxeditor.io.e4.in.excel.taxa;
|
11
|
|
12
|
import java.io.File;
|
13
|
import java.io.FileInputStream;
|
14
|
import java.io.FileNotFoundException;
|
15
|
import java.io.IOException;
|
16
|
import java.net.URI;
|
17
|
|
18
|
import javax.inject.Inject;
|
19
|
import javax.inject.Named;
|
20
|
|
21
|
import org.apache.commons.io.IOUtils;
|
22
|
import org.apache.log4j.Logger;
|
23
|
import org.eclipse.core.runtime.jobs.Job;
|
24
|
import org.eclipse.e4.core.contexts.ContextInjectionFactory;
|
25
|
import org.eclipse.e4.core.contexts.IEclipseContext;
|
26
|
import org.eclipse.e4.core.di.annotations.Optional;
|
27
|
import org.eclipse.e4.ui.services.IServiceConstants;
|
28
|
import org.eclipse.e4.ui.workbench.modeling.EPartService;
|
29
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
30
|
import org.eclipse.jface.viewers.TreeSelection;
|
31
|
|
32
|
import eu.etaxonomy.cdm.database.DbSchemaValidation;
|
33
|
import eu.etaxonomy.cdm.io.common.IImportConfigurator.SOURCE_TYPE;
|
34
|
import eu.etaxonomy.cdm.io.excel.taxa.NormalExplicitImportConfigurator;
|
35
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
36
|
import eu.etaxonomy.taxeditor.io.e4.in.AbstractImportWizardE4;
|
37
|
import eu.etaxonomy.taxeditor.io.e4.in.ImportFromFileDataSourceWizardPageE4;
|
38
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
39
|
|
40
|
|
41
|
/**
|
42
|
*
|
43
|
* @author pplitzner
|
44
|
* @since Oct 5, 2017
|
45
|
*
|
46
|
*/
|
47
|
public class ExcelNormalExplicitTaxaImportWizardE4 extends AbstractImportWizardE4<NormalExplicitImportConfigurator>{
|
48
|
|
49
|
private NormalExplicitImportConfigurator configurator;
|
50
|
|
51
|
private ImportFromFileDataSourceWizardPageE4 dataSourcePage;
|
52
|
private static final Logger logger = Logger.getLogger(ExcelNormalExplicitTaxaImportWizardE4.class);
|
53
|
|
54
|
@Inject
|
55
|
public ExcelNormalExplicitTaxaImportWizardE4(IEclipseContext context, EPartService partService,
|
56
|
@Optional@Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection) {
|
57
|
super(context, partService, selection);
|
58
|
}
|
59
|
|
60
|
/** {@inheritDoc} */
|
61
|
@Override
|
62
|
public NormalExplicitImportConfigurator getConfigurator() {
|
63
|
return configurator;
|
64
|
}
|
65
|
|
66
|
/** {@inheritDoc} */
|
67
|
@Override
|
68
|
public boolean performFinish() {
|
69
|
URI source = dataSourcePage.getUri();
|
70
|
configurator.setSource(source);
|
71
|
configurator.setDbSchemaValidation(DbSchemaValidation.CREATE);
|
72
|
configurator.setSourceReferenceTitle(dataSourcePage.getTextReferenceString().getText());
|
73
|
File file = new File(source);
|
74
|
FileInputStream fis = null;
|
75
|
|
76
|
try {
|
77
|
fis = new FileInputStream(file);
|
78
|
} catch (FileNotFoundException e) {
|
79
|
logger.error("Error while reading file" + source.toString());
|
80
|
}
|
81
|
try {
|
82
|
configurator.setStream(IOUtils.toByteArray(fis));
|
83
|
} catch (IOException e) {
|
84
|
// TODO Auto-generated catch block
|
85
|
e.printStackTrace();
|
86
|
}
|
87
|
Job job = CdmStore.getImportManager().createIOServiceJob(configurator,file , SOURCE_TYPE.INPUTSTREAM);
|
88
|
CdmStore.getImportManager().run(job);
|
89
|
|
90
|
return true;
|
91
|
}
|
92
|
|
93
|
/** {@inheritDoc} */
|
94
|
@Override
|
95
|
public void init() {
|
96
|
configurator = CdmStore.getImportManager().NormalExplicitConfigurator();
|
97
|
if (selection instanceof TreeSelection && !selection.isEmpty()){
|
98
|
TaxonNode node = (TaxonNode)selection.getFirstElement();
|
99
|
configurator.setParentUUID(node.getTaxon().getUuid());
|
100
|
configurator.setClassificationUuid(node.getClassification().getUuid());
|
101
|
}
|
102
|
}
|
103
|
|
104
|
/** {@inheritDoc} */
|
105
|
@Override
|
106
|
public void addPages() {
|
107
|
super.addPages();
|
108
|
|
109
|
dataSourcePage = ContextInjectionFactory.make(ImportFromFileDataSourceWizardPageE4.class, context);
|
110
|
dataSourcePage.setTitle("Choose NormalExplicit");
|
111
|
dataSourcePage.setDescription("Please choose an xls file in the NormalExplicit format.");
|
112
|
dataSourcePage.setExtensions(new String[]{"*.xlsx", "*.xls", "*.*"});
|
113
|
addPage(dataSourcePage);
|
114
|
}
|
115
|
}
|