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