major refactoring in io-layer (config -> state)
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / jaxb / JaxbImport.java
1 /**
2 * Copyright (C) 2008 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.cdm.io.jaxb;
11
12 import java.io.File;
13 import java.net.URI;
14 import java.net.URISyntaxException;
15 import java.util.Collection;
16 import java.util.List;
17 import java.util.Map;
18
19 import org.apache.log4j.Logger;
20 import org.springframework.stereotype.Component;
21 import org.springframework.transaction.TransactionStatus;
22
23 import eu.etaxonomy.cdm.io.common.CdmIoBase;
24 import eu.etaxonomy.cdm.io.common.ICdmIO;
25 import eu.etaxonomy.cdm.io.common.IImportConfigurator;
26 import eu.etaxonomy.cdm.io.common.ImportStateBase;
27 import eu.etaxonomy.cdm.io.common.MapWrapper;
28 import eu.etaxonomy.cdm.model.agent.AgentBase;
29 import eu.etaxonomy.cdm.model.common.CdmBase;
30 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
31 import eu.etaxonomy.cdm.model.common.LanguageStringBase;
32 import eu.etaxonomy.cdm.model.common.ReferencedEntityBase;
33 import eu.etaxonomy.cdm.model.common.TermVocabulary;
34 import eu.etaxonomy.cdm.model.common.VersionableEntity;
35 import eu.etaxonomy.cdm.model.description.DescriptionBase;
36 import eu.etaxonomy.cdm.model.description.FeatureTree;
37 import eu.etaxonomy.cdm.model.media.Media;
38 import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
39 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
40 import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
41 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
42 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
43 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
44
45 /**
46 * @author a.babadshanjan
47 * @created 13.11.2008
48 * @version 1.0
49 */
50 @Component
51 public class JaxbImport extends CdmIoBase<JaxbImportState> implements ICdmIO<JaxbImportState> {
52
53 private static final Logger logger = Logger.getLogger(JaxbImport.class);
54 private CdmDocumentBuilder cdmDocumentBuilder = null;
55
56
57 /** Reads data from an XML file and stores them into a CDM DB.
58 *
59 * @param config
60 * @param stores (not used)
61 */
62 // @Override
63 // protected boolean doInvoke(IImportConfigurator config,
64 // Map<String, MapWrapper<? extends CdmBase>> stores) {
65 @Override
66 protected boolean doInvoke(JaxbImportState state) {
67
68 state.getConfig();
69 boolean success = true;
70 URI uri = null;
71 JaxbImportConfigurator jaxbImpConfig = (JaxbImportConfigurator)state.getConfig();
72
73 String urlFileName = (String)jaxbImpConfig.getSource();
74 logger.debug("urlFileName: " + urlFileName);
75 try {
76 uri = new URI(urlFileName);
77 logger.debug("uri: " + uri.toString());
78 } catch (URISyntaxException ex) {
79 logger.error("File not found");
80 return false;
81 }
82
83 logger.info("Deserializing file " + urlFileName + " to DB " ); //+ dbname
84
85 DataSet dataSet = new DataSet();
86
87 // unmarshalling XML file
88 try {
89 cdmDocumentBuilder = new CdmDocumentBuilder();
90 logger.info("Unmarshalling file: " + urlFileName);
91 File file = new File(uri);
92 logger.debug("Absolute path: " + file.getAbsolutePath());
93 dataSet = cdmDocumentBuilder.unmarshal(DataSet.class, file);
94
95 } catch (Exception e) {
96 logger.error("Unmarshalling error");
97 e.printStackTrace();
98 }
99
100 // save data in DB
101 logger.info("Saving data to DB: " ); //+ dbname
102 success = saveData(jaxbImpConfig, dataSet);
103
104 return success;
105 }
106
107
108 /** Saves data in DB */
109 private boolean saveData (JaxbImportConfigurator jaxbImpConfig, DataSet dataSet) {
110
111 boolean ret = true;
112 Collection<TaxonBase> taxonBases;
113 List<? extends AgentBase> agents;
114 List<DefinedTermBase> terms;
115 List<ReferenceBase> references;
116 List<TaxonNameBase> taxonomicNames;
117 List<DescriptionBase> descriptions;
118 List<TypeDesignationBase> typeDesignations;
119 List<SpecimenOrObservationBase> occurrences;
120 List<FeatureTree> featureTrees;
121 List<Media> media;
122 List<LanguageStringBase> languageData;
123 List<TermVocabulary<DefinedTermBase>> termVocabularies;
124 List<HomotypicalGroup> homotypicalGroups;
125
126 // Get an app controller that omits term loading
127 // CdmApplicationController.getCdmAppController(boolean createNew, boolean omitTermLoading){
128 // CdmApplicationController appCtr = jaxbImpConfig.getCdmAppController(false, true);
129 //TransactionStatus txStatus = startTransaction();
130 //TransactionStatus txStatus = null;
131
132 // Have single transactions per service save call. Otherwise, getting
133 // H2 HYT00 error (timeout locking table DEFINEDTERMBASE) when running from editor.
134
135 // If data of a certain type, such as terms, are not saved here explicitly,
136 // then only those data of this type that are referenced by other objects are saved implicitly.
137 // For example, if taxa are saved all other data referenced by those taxa, such as synonyms,
138 // are automatically saved as well.
139
140 if ((jaxbImpConfig.isDoTermVocabularies() == true)
141 && (termVocabularies = dataSet.getTermVocabularies()).size() > 0) {
142 //txStatus = startTransaction();
143 ret &= saveTermVocabularies(termVocabularies);
144 //commitTransaction(txStatus);
145 }
146
147 if ((jaxbImpConfig.isDoTerms() == true)
148 && (terms = dataSet.getTerms()).size() > 0) {
149 //txStatus = startTransaction();
150 ret &= saveTerms(terms);
151 //commitTransaction(txStatus);
152 }
153
154 // TODO: Have separate data save methods
155
156 // txStatus = startTransaction();
157 // try {
158 // if (jaxbImpConfig.isDoLanguageData() == true) {
159 // if ((languageData = dataSet.getLanguageData()).size() > 0) {
160 // logger.info("Language data: " + languageData.size());
161 // getTermService().saveLanguageDataAll(languageData);
162 // }
163 // }
164 // } catch (Exception ex) {
165 // logger.error("Error saving language data");
166 // ret = false;
167 // }
168 // commitTransaction(txStatus);
169
170
171 //txStatus = startTransaction();
172 try {
173 if (jaxbImpConfig.isDoAuthors() == true) {
174 if ((agents = dataSet.getAgents()).size() > 0) {
175 logger.info("Agents: " + agents.size());
176 getAgentService().saveAgentAll(agents);
177 }
178 }
179 } catch (Exception ex) {
180 logger.error("Error saving agents");
181 ret = false;
182 }
183 //commitTransaction(txStatus);
184
185
186 //txStatus = startTransaction();
187 try {
188 if (jaxbImpConfig.getDoReferences() != IImportConfigurator.DO_REFERENCES.NONE) {
189 if ((references = dataSet.getReferences()).size() > 0) {
190 logger.info("References: " + references.size());
191 getReferenceService().saveReferenceAll(references);
192 }
193 }
194 } catch (Exception ex) {
195 logger.error("Error saving references");
196 ret = false;
197 }
198 //commitTransaction(txStatus);
199
200
201 //txStatus = startTransaction();
202 try {
203 if (jaxbImpConfig.isDoTaxonNames() == true) {
204 if ((taxonomicNames = dataSet.getTaxonomicNames()).size() > 0) {
205 logger.info("Taxonomic names: " + taxonomicNames.size());
206 getNameService().saveTaxonNameAll(taxonomicNames);
207 }
208 }
209 } catch (Exception ex) {
210 logger.error("Error saving taxon names");
211 ret = false;
212 }
213 //commitTransaction(txStatus);
214
215
216 //txStatus = startTransaction();
217 try {
218 if (jaxbImpConfig.isDoHomotypicalGroups() == true) {
219 if ((homotypicalGroups = dataSet.getHomotypicalGroups()).size() > 0) {
220 logger.info("Homotypical groups: " + homotypicalGroups.size());
221 getNameService().saveAllHomotypicalGroups(homotypicalGroups);
222 }
223 }
224 } catch (Exception ex) {
225 logger.error("Error saving homotypical groups");
226 ret = false;
227 }
228 //commitTransaction(txStatus);
229
230
231 //txStatus = startTransaction();
232 // Need to get the taxa and the synonyms here.
233 try {
234 if (jaxbImpConfig.isDoTaxa() == true) {
235 if ((taxonBases = dataSet.getTaxonBases()).size() > 0) {
236 logger.info("Taxon bases: " + taxonBases.size());
237 getTaxonService().saveTaxonAll(taxonBases);
238 }
239 }
240 } catch (Exception ex) {
241 logger.error("Error saving taxa");
242 ret = false;
243 }
244 //commitTransaction(txStatus);
245
246
247 //txStatus = startTransaction();
248 // NomenclaturalStatus, TypeDesignations
249 try {
250 if (jaxbImpConfig.isDoTypeDesignations() == true) {
251 if ((typeDesignations = dataSet.getTypeDesignations()).size() > 0) {
252 logger.info("Type Designations: " + typeDesignations.size());
253 getNameService().saveTypeDesignationAll(typeDesignations);
254 }
255 }
256 } catch (Exception ex) {
257 logger.error("Error saving type designations");
258 ret = false;
259 }
260 //commitTransaction(txStatus);
261
262
263 // TODO: Implement dataSet.getDescriptions() and IDescriptionService.saveDescriptionAll()
264 // if ((descriptions = dataSet.getDescriptions()) != null) {
265 // logger.info("Saving " + descriptions.size() + " descriptions");
266 // getDescriptionService().saveDescriptionAll(descriptions);
267 // }
268
269 //txStatus = startTransaction();
270 try {
271 if (jaxbImpConfig.isDoOccurrence() == true) {
272 if ((occurrences = dataSet.getOccurrences()).size() > 0) {
273 logger.info("Occurrences: " + occurrences.size());
274 getOccurrenceService().saveSpecimenOrObservationBaseAll(occurrences);
275 }
276 }
277 } catch (Exception ex) {
278 logger.error("Error saving occurrences");
279 ret = false;
280 }
281 //commitTransaction(txStatus);
282
283
284 //txStatus = startTransaction();
285 try {
286 if (jaxbImpConfig.isDoFeatureData() == true) {
287 if ((featureTrees = dataSet.getFeatureTrees()).size() > 0) {
288 logger.info("Feature data: " + featureTrees.size());
289 getDescriptionService().saveFeatureTreeAll(featureTrees);
290 }
291 }
292 } catch (Exception ex) {
293 logger.error("Error saving feature data");
294 ret = false;
295 }
296 //commitTransaction(txStatus);
297
298
299 //txStatus = startTransaction();
300 try {
301 if (jaxbImpConfig.isDoMedia() == true) {
302 if ((media = dataSet.getMedia()).size() > 0) {
303 logger.info("Media: " + media.size());
304 getMediaService().saveMediaAll(media);
305 }
306 }
307 } catch (Exception ex) {
308 logger.error("Error saving media");
309 ret = false;
310 }
311 //commitTransaction(txStatus);
312
313 //commitTransaction(txStatus);
314 logger.info("All data saved");
315
316 return ret;
317
318 }
319
320
321 private boolean saveTermVocabularies(
322 List<TermVocabulary<DefinedTermBase>> termVocabularies) {
323
324 boolean success = true;
325 logger.info("Term vocabularies: " + termVocabularies.size());
326 try {
327 getTermService().saveTermVocabulariesAll(termVocabularies);
328 } catch (Exception ex) {
329 logger.error("Error saving term vocabularies");
330 success = false;
331 }
332 return success;
333 }
334
335 private boolean saveTerms(List<DefinedTermBase> terms) {
336
337 boolean success = true;
338 logger.info("Terms: " + terms.size());
339 try {
340 getTermService().saveTermsAll(terms);
341 } catch (Exception ex) {
342 logger.error("Error saving terms");
343 success = false;
344 }
345 return success;
346 }
347
348
349 @Override
350 protected boolean doCheck(JaxbImportState state) {
351 boolean result = true;
352 logger.warn("No check implemented for Jaxb import");
353 return result;
354 }
355
356
357 @Override
358 protected boolean isIgnore(JaxbImportState state) {
359 return false;
360 }
361 }