64e097cb349bda65d44fca07e5c0f1d378f50cf0
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / io / ImportManager.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.io;
12
13 import org.eclipse.core.runtime.Assert;
14 import org.eclipse.core.runtime.IProgressMonitor;
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.core.runtime.Status;
17 import org.eclipse.core.runtime.jobs.Job;
18 import org.eclipse.swt.widgets.Display;
19
20 import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
21 import eu.etaxonomy.cdm.io.common.CdmDefaultImport;
22 import eu.etaxonomy.cdm.io.common.IImportConfigurator;
23 import eu.etaxonomy.cdm.io.excel.taxa.NormalExplicitImportConfigurator;
24 import eu.etaxonomy.cdm.io.jaxb.JaxbImportConfigurator;
25 import eu.etaxonomy.cdm.io.reference.endnote.in.EndnoteImportConfigurator;
26 import eu.etaxonomy.cdm.io.sdd.in.SDDImportConfigurator;
27 import eu.etaxonomy.cdm.io.specimen.abcd206.in.Abcd206ImportConfigurator;
28 import eu.etaxonomy.cdm.io.specimen.excel.in.SpecimenCdmExcelImportConfigurator;
29 import eu.etaxonomy.cdm.io.tcsxml.in.TcsXmlImportConfigurator;
30 import eu.etaxonomy.taxeditor.model.CdmProgressMonitorAdapter;
31 import eu.etaxonomy.taxeditor.store.CdmStore;
32 import eu.etaxonomy.taxeditor.store.StoreUtil;
33
34 /**
35 * <p>
36 * ImportHandler class.
37 * </p>
38 *
39 * @author n.hoffmann
40 * @created Sep 11, 2009
41 * @version 1.0
42 */
43 public class ImportManager extends AbstractIOManager<IImportConfigurator> {
44
45 /**
46 * @param applicationConfiguration
47 */
48 private ImportManager(ICdmApplicationConfiguration applicationConfiguration) {
49 super(applicationConfiguration);
50 }
51
52 /**
53 * <p>
54 * NewInstance
55 * </p>
56 *
57 * @param applicationConfiguration
58 * a
59 * {@link eu.etaxonomy.cdm.api.application.CdmApplicationController}
60 * object.
61 * @return a {@link eu.etaxonomy.taxeditor.io.ImportManager} object.
62 */
63 public static ImportManager NewInstance(
64 ICdmApplicationConfiguration applicationConfiguration) {
65 return new ImportManager(applicationConfiguration);
66 }
67
68 /**
69 * Use when importing data into the current application context
70 *
71 * @param configurator
72 * a {@link eu.etaxonomy.cdm.io.common.IImportConfigurator}
73 * object.
74 * @return a {@link org.eclipse.core.runtime.jobs.Job} object.
75 */
76 @Override
77 protected Job createIOJob(final IImportConfigurator configurator) {
78 Assert.isNotNull(configurator, "Configuration may not be null");
79
80 final Display display = Display.getDefault();
81
82 Job job = new Job("Import: " + configurator.getClass().getSimpleName()) {
83
84 @Override
85 protected IStatus run(IProgressMonitor monitor) {
86 monitor.beginTask("Importing data", 100);
87 configurator.setProgressMonitor(CdmProgressMonitorAdapter
88 .CreateSubMonitor(monitor, 70));
89
90 monitor.worked(5);
91
92 CdmDefaultImport<IImportConfigurator> importer = new CdmDefaultImport<IImportConfigurator>();
93 importer.setCdmAppController(applicationConfiguration);
94 monitor.worked(5);
95
96 try {
97 importer.invoke(configurator);
98 monitor.worked(80);
99 } catch (RuntimeException e) {
100 StoreUtil.errorDialog(
101 "Error importing data",
102 this,
103 "An error occured while"
104 + "importing from source '"
105 + configurator.getSourceNameString()
106 + "'.\n"
107 + "Please check error log for details.", e);
108 }
109
110 monitor.worked(5);
111 monitor.done();
112
113 display.asyncExec(new Runnable() {
114
115 @Override
116 public void run() {
117 CdmStore.getContextManager().notifyContextRefresh();
118 }
119 });
120
121 return Status.OK_STATUS;
122 }
123 };
124
125 return job;
126
127 }
128
129 private IImportConfigurator getConfigurator(TYPE type) {
130 Assert.isNotNull(type, "Type should not be null");
131
132 switch (type) {
133 case Jaxb:
134 return JaxbImportConfigurator.NewInstance(null, null);
135 case Tcs:
136 return TcsXmlImportConfigurator.NewInstance(null, null);
137 case Endnote:
138 return EndnoteImportConfigurator.NewInstance(null, null);
139 case Excel_Taxa:
140 return NormalExplicitImportConfigurator.NewInstance(null, null,
141 null, null);
142 case Abcd:
143 return Abcd206ImportConfigurator.NewInstance(null, null);
144 case Sdd:
145 return SDDImportConfigurator.NewInstance(null, null);
146 case SpecimenCdmExcel:
147 return SpecimenCdmExcelImportConfigurator.NewInstance(null, null);
148 default:
149 StoreUtil.notImplementedMessage(this);
150 throw new IllegalArgumentException("Import not supported yet");
151 }
152 }
153
154
155 /**
156 * <p>
157 * JaxbConfigurator
158 * </p>
159 *
160 * @return a {@link eu.etaxonomy.cdm.io.jaxb.JaxbImportConfigurator} object.
161 */
162 public final JaxbImportConfigurator JaxbConfigurator() {
163 return (JaxbImportConfigurator) getConfigurator(TYPE.Jaxb);
164 }
165
166 /**
167 * <p>
168 * TcsConfigurator
169 * </p>
170 *
171 * @return a {@link eu.etaxonomy.cdm.io.tcsxml.in.TcsXmlImportConfigurator}
172 * object.
173 */
174 public final TcsXmlImportConfigurator TcsConfigurator() {
175 return (TcsXmlImportConfigurator) getConfigurator(TYPE.Tcs);
176 }
177
178 /**
179 * <p>
180 * EndnoteConfigurator
181 * </p>
182 *
183 * @return a
184 * {@link eu.etaxonomy.cdm.io.reference.endnote.in.EndnoteImportConfigurator}
185 * object.
186 */
187 public final EndnoteImportConfigurator EndnoteConfigurator() {
188 return (EndnoteImportConfigurator) getConfigurator(TYPE.Endnote);
189 }
190
191 /**
192 * <p>
193 * NormalExplicitConfigurator
194 * </p>
195 *
196 * @return a
197 * {@link eu.etaxonomy.cdm.io.excel.taxa.NormalExplicitImportConfigurator}
198 * object.
199 */
200 public final NormalExplicitImportConfigurator NormalExplicitConfigurator() {
201 return (NormalExplicitImportConfigurator) getConfigurator(TYPE.Excel_Taxa);
202 }
203
204 /**
205 * <p>
206 * SddConfigurator
207 * </p>
208 *
209 * @return a {@link eu.etaxonomy.cdm.io.sdd.in.SDDImportConfigurator}
210 * object.
211 */
212 public final SDDImportConfigurator SddConfigurator() {
213 return (SDDImportConfigurator) getConfigurator(TYPE.Sdd);
214 }
215
216 /**
217 * <p>
218 * AbcdConfigurator
219 * </p>
220 *
221 * @return a
222 * {@link eu.etaxonomy.cdm.io.specimen.abcd206.in.Abcd206ImportConfigurator}
223 * object.
224 */
225 public final Abcd206ImportConfigurator AbcdConfigurator() {
226 return (Abcd206ImportConfigurator) getConfigurator(TYPE.Abcd);
227 }
228
229 public SpecimenCdmExcelImportConfigurator SpecimenCdmExcelImportConfigurator() {
230 return (SpecimenCdmExcelImportConfigurator) getConfigurator(TYPE.SpecimenCdmExcel);
231 }
232
233 }