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