d73ab1ae25098fa993330b37cbba0db82817336c
[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.CdmApplicationController;
21 import eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator;
22 import eu.etaxonomy.cdm.io.common.CdmDefaultImport;
23 import eu.etaxonomy.cdm.io.common.IImportConfigurator;
24 import eu.etaxonomy.cdm.io.excel.taxa.NormalExplicitImportConfigurator;
25 import eu.etaxonomy.cdm.io.jaxb.JaxbImportConfigurator;
26 import eu.etaxonomy.cdm.io.reference.endnote.in.EndnoteImportConfigurator;
27 import eu.etaxonomy.cdm.io.sdd.in.SDDImportConfigurator;
28 import eu.etaxonomy.cdm.io.specimen.abcd206.in.Abcd206ImportConfigurator;
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>ImportHandler class.</p>
36 *
37 * @author n.hoffmann
38 * @created Sep 11, 2009
39 * @version 1.0
40 */
41 public class ImportManager extends AbstractIOManager<IImportConfigurator> {
42
43 /**
44 * @param applicationController
45 */
46 private ImportManager(
47 CdmApplicationController applicationController) {
48 super(applicationController);
49 }
50
51 /**
52 * <p>NewInstance</p>
53 *
54 * @param applicationController a {@link eu.etaxonomy.cdm.api.application.CdmApplicationController} object.
55 * @return a {@link eu.etaxonomy.taxeditor.io.ImportManager} object.
56 */
57 public static ImportManager NewInstance(CdmApplicationController applicationController){
58 return new ImportManager(applicationController);
59 }
60
61 /**
62 * Use when importing data into the current application context
63 *
64 * @param configurator a {@link eu.etaxonomy.cdm.io.common.IImportConfigurator} object.
65 * @return a {@link org.eclipse.core.runtime.jobs.Job} object.
66 */
67 protected Job createIOJob(final IImportConfigurator configurator){
68 Assert.isNotNull(configurator, "Configuration may not be null");
69
70 final Display display = Display.getDefault();
71
72 Job job = new Job("Import: " + configurator.getClass().getSimpleName()){
73
74 @Override
75 protected IStatus run(IProgressMonitor monitor) {
76 monitor.beginTask("Importing data", 100);
77 configurator.setProgressMonitor(CdmProgressMonitorAdapter.CreateSubMonitor(monitor, 70));
78
79 monitor.worked(10);
80
81 CdmDefaultImport<IImportConfigurator> importer = new CdmDefaultImport<IImportConfigurator>();
82 importer.setCdmAppController(applicationController);
83 monitor.worked(10);
84
85 try{
86 importer.invoke(configurator);
87 monitor.worked(60);
88 }catch(RuntimeException e){
89 StoreUtil.errorDialog("Error importing data", this, "An error occured while" +
90 "importing from source '" + configurator.getSourceNameString() + "'.\n" +
91 "Please check error log for details.", e);
92 }
93
94 monitor.worked(10);
95 monitor.done();
96
97 display.asyncExec(new Runnable() {
98
99 @Override
100 public void run() {
101 CdmStore.getContextManager().notifyContextRefresh();
102 }
103 });
104
105 return Status.OK_STATUS;
106 }
107 };
108
109 return job;
110
111 }
112
113 private IImportConfigurator getConfigurator(TYPE type){
114 Assert.isNotNull(type, "Type should not be null");
115
116 switch(type){
117 case BerlinModel:
118 return BerlinModelImportConfigurator.NewInstance(null, null);
119 case Jaxb:
120 return JaxbImportConfigurator.NewInstance(null, null);
121 case Tcs:
122 return TcsXmlImportConfigurator.NewInstance(null, null);
123 case Endnote:
124 return EndnoteImportConfigurator.NewInstance(null, null);
125 case Excel_Taxa:
126 return NormalExplicitImportConfigurator.NewInstance(null, null, null);
127 case Abcd:
128 return Abcd206ImportConfigurator.NewInstance(null, null);
129 case Sdd:
130 return SDDImportConfigurator.NewInstance(null, null);
131 default:
132 StoreUtil.notImplementedMessage(this);
133 throw new IllegalArgumentException("Import not supported yet");
134 }
135 }
136
137 /**
138 * <p>BerlinModelConfigurator</p>
139 *
140 * @return a {@link eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator} object.
141 */
142 public final BerlinModelImportConfigurator BerlinModelConfigurator() { return (BerlinModelImportConfigurator) getConfigurator(TYPE.BerlinModel); }
143 /**
144 * <p>JaxbConfigurator</p>
145 *
146 * @return a {@link eu.etaxonomy.cdm.io.jaxb.JaxbImportConfigurator} object.
147 */
148 public final JaxbImportConfigurator JaxbConfigurator() { return (JaxbImportConfigurator) getConfigurator(TYPE.Jaxb); }
149 /**
150 * <p>TcsConfigurator</p>
151 *
152 * @return a {@link eu.etaxonomy.cdm.io.tcsxml.in.TcsXmlImportConfigurator} object.
153 */
154 public final TcsXmlImportConfigurator TcsConfigurator() { return (TcsXmlImportConfigurator) getConfigurator(TYPE.Tcs); }
155 /**
156 * <p>EndnoteConfigurator</p>
157 *
158 * @return a {@link eu.etaxonomy.cdm.io.reference.endnote.in.EndnoteImportConfigurator} object.
159 */
160 public final EndnoteImportConfigurator EndnoteConfigurator() { return (EndnoteImportConfigurator) getConfigurator(TYPE.Endnote); }
161 /**
162 * <p>NormalExplicitConfigurator</p>
163 *
164 * @return a {@link eu.etaxonomy.cdm.io.excel.taxa.NormalExplicitImportConfigurator} object.
165 */
166 public final NormalExplicitImportConfigurator NormalExplicitConfigurator() { return (NormalExplicitImportConfigurator) getConfigurator(TYPE.Excel_Taxa); }
167 /**
168 * <p>SddConfigurator</p>
169 *
170 * @return a {@link eu.etaxonomy.cdm.io.sdd.in.SDDImportConfigurator} object.
171 */
172 public final SDDImportConfigurator SddConfigurator() { return (SDDImportConfigurator) getConfigurator(TYPE.Sdd); }
173 /**
174 * <p>AbcdConfigurator</p>
175 *
176 * @return a {@link eu.etaxonomy.cdm.io.specimen.abcd206.in.Abcd206ImportConfigurator} object.
177 */
178 public final Abcd206ImportConfigurator AbcdConfigurator() { return (Abcd206ImportConfigurator) getConfigurator(TYPE.Abcd); }
179
180
181 }