merge-update from trunk
[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
18 import org.apache.log4j.Logger;
19 import org.springframework.stereotype.Component;
20 import org.springframework.transaction.TransactionStatus;
21
22 import eu.etaxonomy.cdm.config.Configuration;
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
62 @Override
63 protected void doInvoke(JaxbImportState state) {
64
65 JaxbImportConfigurator jaxbImpConfig = (JaxbImportConfigurator)state.getConfig();
66
67 URI uri;
68 String urlFileName = jaxbImpConfig.getSource().toString();
69 try {
70 uri = new URI(urlFileName);
71 } catch (URISyntaxException ex) {
72 logger.error("File not found");
73 state.setUnsuccessfull();
74 return;
75 }
76
77 logger.info("Deserializing " + urlFileName + " to DB " ); //+ dbname
78
79 DataSet dataSet = new DataSet();
80
81 // unmarshalling XML file
82 try {
83 cdmDocumentBuilder = new CdmDocumentBuilder();
84 logger.info("Unmarshalling " + urlFileName);
85 File file = new File(uri);
86 logger.debug("Absolute path: " + file.getAbsolutePath());
87 dataSet = cdmDocumentBuilder.unmarshal(DataSet.class, file);
88
89 } catch (Exception e) {
90 logger.error("Unmarshalling error");
91 e.printStackTrace();
92 }
93
94 // save data in DB
95 logger.info("Saving data to DB... "); //+ dbname
96
97 saveData(state, dataSet);
98
99 return;
100 }
101
102
103 /** Saves data in DB */
104 private void saveData (JaxbImportState state, DataSet dataSet) {
105 JaxbImportConfigurator jaxbImpConfig = state.getConfig();
106
107 boolean success = true;
108 Collection<TaxonBase> taxonBases;
109 List<? extends AgentBase> agents;
110 List<DefinedTermBase> terms;
111 List<User> users;
112 List<Reference> references;
113 List<TaxonNameBase> taxonomicNames;
114 List<DescriptionBase> descriptions;
115 List<TypeDesignationBase> typeDesignations;
116 List<SpecimenOrObservationBase> occurrences;
117 List<FeatureTree> featureTrees;
118 List<FeatureNode> featureNodes;
119 List<Media> media;
120 List<LanguageStringBase> languageData;
121 List<TermVocabulary<DefinedTermBase>> termVocabularies;
122 List<HomotypicalGroup> homotypicalGroups;
123
124 TransactionStatus txStatus = startTransaction();
125 //TransactionStatus txStatus = null;
126
127 // Have single transactions per service save call. Otherwise, getting
128 // H2 HYT00 error (timeout locking table DEFINEDTERMBASE) when running from editor.
129
130 try {
131 if (jaxbImpConfig.isDoUser() == true) {
132 /*
133 * this is a crucial call, otherwise the password will not be set correctly
134 * and the whole authentication will not work
135 *
136 * a bit preliminary, should be used only if the complete database is replaced
137 */
138 authenticate(Configuration.adminLogin, Configuration.adminPassword);
139
140 logger.info("Users: " + (users = dataSet.getUsers()).size());
141 for (User user : users) {
142
143 List<User> usersList = getUserService().listByUsername(user.getUsername(),null, null, null, null, null, null);
144 if (usersList.isEmpty()){
145 getUserService().save(user);
146 }else{
147 // User existingUser = usersList.get(0);
148 // user.setId(existingUser.getId());
149 // getUserService().merge(user);
150 //merging does not yet work because of #4102
151
152 }
153 }
154 }
155 } catch (Exception ex) {
156 logger.error("Error saving users");
157 ex.printStackTrace();
158 success = false;
159 }
160
161 if ((jaxbImpConfig.isDoTerms() == true)
162 && (terms = dataSet.getTerms()).size() > 0) {
163 //txStatus = startTransaction();
164 success &= saveTerms(terms);
165
166 //commitTransaction(txStatus);
167 }
168 if ((jaxbImpConfig.isDoTermVocabularies() == true)
169 && (termVocabularies = dataSet.getTermVocabularies()).size() > 0) {
170 //txStatus = startTransaction();
171 success &= saveTermVocabularies(termVocabularies);
172
173 }
174
175 // TODO: Have separate data save methods
176
177 // txStatus = startTransaction();
178 // try {
179 // if (jaxbImpConfig.isDoLanguageData() == true) {
180 // if ((languageData = dataSet.getLanguageData()).size() > 0) {
181 // logger.info("Language data: " + languageData.size());
182 // getTermService().saveLanguageDataAll(languageData);
183 // }
184 // }
185 // } catch (Exception ex) {
186 // logger.error("Error saving language data");
187 // ret = false;
188 // }
189 // commitTransaction(txStatus);
190
191
192 //txStatus = startTransaction();
193 try {
194 if (jaxbImpConfig.isDoAuthors() == true) {
195 if ((agents = dataSet.getAgents()).size() > 0) {
196 logger.info("Agents: " + agents.size());
197 getAgentService().save((Collection)agents);
198 }
199 }
200 } catch (Exception ex) {
201 logger.error("Error saving agents");
202 success = false;
203 }
204 //commitTransaction(txStatus);
205
206
207 //txStatus = startTransaction();
208 try {
209 if (jaxbImpConfig.getDoReferences() != IImportConfigurator.DO_REFERENCES.NONE) {
210 if ((references = dataSet.getReferences()).size() > 0) {
211 logger.info("References: " + references.size());
212 getReferenceService().save(references);
213 logger.info("ready...");
214 }
215 }
216 } catch (Exception ex) {
217 ex.printStackTrace();
218 logger.error("Error saving references");
219 success = false;
220 }
221 //commitTransaction(txStatus);
222
223
224 //txStatus = startTransaction();
225 try {
226 if (jaxbImpConfig.isDoTaxonNames() == true) {
227 if ((taxonomicNames = dataSet.getTaxonomicNames()).size() > 0) {
228 logger.info("Taxonomic names: " + taxonomicNames.size());
229 getNameService().save(taxonomicNames);
230 }
231 }
232 } catch (Exception ex) {
233 logger.error("Error saving taxon names");
234 success = false;
235 }
236 //commitTransaction(txStatus);
237
238
239 //txStatus = startTransaction();
240 try {
241 if (jaxbImpConfig.isDoHomotypicalGroups() == true) {
242 if ((homotypicalGroups = dataSet.getHomotypicalGroups()).size() > 0) {
243 logger.info("Homotypical groups: " + homotypicalGroups.size());
244 getNameService().saveAllHomotypicalGroups(homotypicalGroups);
245
246 }
247 }
248 } catch (Exception ex) {
249 logger.error("Error saving homotypical groups");
250 success = false;
251 }
252 //commitTransaction(txStatus);
253
254
255 //txStatus = startTransaction();
256 // Need to get the taxa and the synonyms here.
257 try {
258 if (jaxbImpConfig.isDoTaxa() == true) {
259 if ( dataSet.getTaxonBases().size() > 0) {
260 List taxa = dataSet.getTaxonBases();
261 dataSet.getTaxonBases();
262 logger.info("Taxon bases: " + taxa.size());
263 taxa.iterator();
264 getTaxonService().save(taxa);
265 /*while (taxBases.hasNext()){
266 getTaxonService().save(taxBases.next());
267 }*/
268 //getTaxonService().saveTaxonAll(taxonBases);
269 }
270 }
271 } catch (Exception ex) {
272 logger.error("Error saving taxa");
273 ex.printStackTrace();
274 success = false;
275 }
276 //commitTransaction(txStatus);
277
278
279 //txStatus = startTransaction();
280 // NomenclaturalStatus, TypeDesignations
281 try {
282 if (jaxbImpConfig.isDoTypeDesignations() == true) {
283 if ((typeDesignations = dataSet.getTypeDesignations()).size() > 0) {
284 logger.info("Type Designations: " + typeDesignations.size());
285 getNameService().saveTypeDesignationAll(typeDesignations);
286 }
287 }
288 } catch (Exception ex) {
289 logger.error("Error saving type designations");
290 success = false;
291 }
292 //commitTransaction(txStatus);
293
294
295 // TODO: Implement dataSet.getDescriptions() and IDescriptionService.saveDescriptionAll()
296 // if ((descriptions = dataSet.getDescriptions()) != null) {
297 // logger.info("Saving " + descriptions.size() + " descriptions");
298 // getDescriptionService().saveDescriptionAll(descriptions);
299 // }
300
301 //txStatus = startTransaction();
302 try {
303 if (jaxbImpConfig.isDoOccurrence() == true) {
304 if ((occurrences = dataSet.getOccurrences()).size() > 0) {
305 logger.info("Occurrences: " + occurrences.size());
306 getOccurrenceService().save(occurrences);
307 }
308 }
309 } catch (Exception ex) {
310 logger.error("Error saving occurrences");
311 success = false;
312 }
313 //commitTransaction(txStatus);
314
315
316 //txStatus = startTransaction();
317 try {
318 if (jaxbImpConfig.isDoFeatureData() == true) {
319 if ((featureTrees = dataSet.getFeatureTrees()).size() > 0) {
320 logger.info("Feature data: " + featureTrees.size());
321 getFeatureTreeService().save(featureTrees);
322 }
323 }
324 } catch (Exception ex) {
325 logger.error("Error saving feature data");
326 success = false;
327 }
328 //commitTransaction(txStatus);
329
330
331 //txStatus = startTransaction();
332 try {
333 if (jaxbImpConfig.isDoMedia() == true) {
334 if ((media = dataSet.getMedia()).size() > 0) {
335 logger.info("Media: " + media.size());
336 getMediaService().save(media);
337 }
338 }
339 } catch (Exception ex) {
340 logger.error("Error saving media");
341 success = false;
342 }
343
344 if (jaxbImpConfig.isDoClassificationData() == true) {
345 logger.info("# Classification");
346
347 Collection<TaxonNode> nodes = dataSet.getTaxonNodes();
348 Collection<Classification> classifications = dataSet.getClassifications();
349 getClassificationService().saveTaxonNodeAll(nodes);
350 for (Classification tree: classifications){
351 getClassificationService().saveOrUpdate(tree);
352 }
353 }
354
355
356 commitTransaction(txStatus);
357 logger.info("All data saved");
358 if (!success){
359 state.setUnsuccessfull();
360 }
361
362 return;
363
364 }
365
366
367 private boolean saveTermVocabularies(
368 List<TermVocabulary<DefinedTermBase>> termVocabularies) {
369
370 boolean success = true;
371 logger.info("Term vocabularies: " + termVocabularies.size());
372 try {
373 getVocabularyService().save((List)termVocabularies);
374 } catch (Exception ex) {
375 logger.error("Error saving term vocabularies");
376 success = false;
377 }
378 return success;
379 }
380
381 private boolean saveTerms(List<DefinedTermBase> terms) {
382
383 boolean success = true;
384 logger.info("Terms: " + terms.size());
385 try {
386 getTermService().save(terms);
387 } catch (Exception ex) {
388 logger.error("Error saving terms");
389 ex.printStackTrace();
390 success = false;
391 }
392 return success;
393 }
394
395
396 @Override
397 protected boolean doCheck(JaxbImportState state) {
398 boolean result = true;
399 logger.warn("No validation implemented for Jaxb import");
400 return result;
401 }
402
403
404 @Override
405 protected boolean isIgnore(JaxbImportState state) {
406 return false;
407 }
408 }