ref #8248 Split import wizard page
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / io / e4 / in / excel / taxa / ExcelNormalExplicitTaxaImportWizardE4.java
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 import java.util.ArrayList;
18 import java.util.List;
19
20 import javax.inject.Inject;
21 import javax.inject.Named;
22
23 import org.apache.commons.io.IOUtils;
24 import org.apache.log4j.Logger;
25 import org.eclipse.core.runtime.jobs.Job;
26 import org.eclipse.e4.core.contexts.ContextInjectionFactory;
27 import org.eclipse.e4.core.contexts.IEclipseContext;
28 import org.eclipse.e4.core.di.annotations.Optional;
29 import org.eclipse.e4.ui.services.IServiceConstants;
30 import org.eclipse.e4.ui.workbench.modeling.EPartService;
31 import org.eclipse.jface.viewers.IStructuredSelection;
32 import org.eclipse.jface.viewers.TreeSelection;
33
34 import eu.etaxonomy.cdm.database.DbSchemaValidation;
35 import eu.etaxonomy.cdm.io.common.IImportConfigurator.SOURCE_TYPE;
36 import eu.etaxonomy.cdm.io.excel.taxa.NormalExplicitImportConfigurator;
37 import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
38 import eu.etaxonomy.taxeditor.io.e4.in.AbstractImportWizardE4;
39 import eu.etaxonomy.taxeditor.io.e4.in.GenericConfiguratorWizardPageE4;
40 import eu.etaxonomy.taxeditor.io.e4.in.ImportFromFileDataSourceWithReferenceWizardPage;
41 import eu.etaxonomy.taxeditor.l10n.Messages;
42 import eu.etaxonomy.taxeditor.store.CdmStore;
43
44
45 /**
46 *
47 * @author pplitzner
48 * @since Oct 5, 2017
49 *
50 */
51 public class ExcelNormalExplicitTaxaImportWizardE4 extends AbstractImportWizardE4<NormalExplicitImportConfigurator>{
52
53 private NormalExplicitImportConfigurator configurator;
54
55 private ImportFromFileDataSourceWithReferenceWizardPage dataSourcePage;
56 private static final Logger logger = Logger.getLogger(ExcelNormalExplicitTaxaImportWizardE4.class);
57
58 @Inject
59 public ExcelNormalExplicitTaxaImportWizardE4(IEclipseContext context, EPartService partService,
60 @Optional@Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection) {
61 super(context, partService, selection);
62 }
63
64 /** {@inheritDoc} */
65 @Override
66 public NormalExplicitImportConfigurator getConfigurator() {
67 return configurator;
68 }
69
70 /** {@inheritDoc} */
71 @Override
72 public boolean performFinish() {
73 URI source = dataSourcePage.getUri();
74 configurator.setSource(source);
75 configurator.setDbSchemaValidation(DbSchemaValidation.CREATE);
76 configurator.setSourceReferenceTitle(dataSourcePage.getTextReferenceString().getText());
77 File file = new File(source);
78 FileInputStream fis = null;
79
80 try {
81 fis = new FileInputStream(file);
82 } catch (FileNotFoundException e) {
83 logger.error("Error while reading file" + source.toString());
84 }
85 try {
86 configurator.setStream(IOUtils.toByteArray(fis));
87 } catch (IOException e) {
88 // TODO Auto-generated catch block
89 e.printStackTrace();
90 }
91 Job job = CdmStore.getImportManager().createIOServiceJob(configurator,file , SOURCE_TYPE.INPUTSTREAM);
92 CdmStore.getImportManager().run(job);
93
94 return true;
95 }
96
97 /** {@inheritDoc} */
98 @Override
99 public void init() {
100 configurator = CdmStore.getImportManager().NormalExplicitConfigurator();
101 if (selection instanceof TreeSelection && !selection.isEmpty()){
102 TaxonNodeDto node = (TaxonNodeDto)selection.getFirstElement();
103 configurator.setParentUUID(node.getTaxonUuid());
104 configurator.setClassificationUuid(node.getClassificationUUID());
105 }
106 }
107
108 /** {@inheritDoc} */
109 @Override
110 public void addPages() {
111 dataSourcePage = ContextInjectionFactory.make(ImportFromFileDataSourceWithReferenceWizardPage.class, context);
112 dataSourcePage.setTitle("Choose NormalExplicit");
113 dataSourcePage.setDescription("Please choose an xls file in the NormalExplicit format.");
114 dataSourcePage.setExtensions(new String[]{"*.xlsx", "*.xls", "*.*"});
115 addPage(dataSourcePage);
116 super.addPages();
117
118 }
119 @Override
120 protected void addConfiguratorPage(){
121 List<String> ignoreMethods = new ArrayList<>();
122 ignoreMethods.add("setIgnoreNull");
123 ignoreMethods.add("setInteractWithUser");
124 ignoreMethods.add("setUseClassification");
125 ignoreMethods.add("setRegisterAuditing");
126 ignoreMethods.add("setDeduplicateAuthors");
127 ignoreMethods.add("setDeduplicateReferences");
128 pageConfiguration = ContextInjectionFactory.make(GenericConfiguratorWizardPageE4.class, context);
129 pageConfiguration.initImport(getConfigurator(), ignoreMethods, Messages.ExcelTaxonUpdateWizard_ConfiguratorWizard_label);
130 addPage(pageConfiguration);
131 }
132 }