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