2 * Copyright (C) 2008 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.io
.jaxb
;
14 import java
.net
.URISyntaxException
;
15 import java
.util
.Collection
;
16 import java
.util
.Iterator
;
17 import java
.util
.List
;
19 import org
.apache
.log4j
.Logger
;
20 import org
.springframework
.stereotype
.Component
;
21 import org
.springframework
.transaction
.TransactionStatus
;
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
;
46 * @author a.babadshanjan
51 public class JaxbImport
extends CdmIoBase
<JaxbImportState
> implements ICdmIO
<JaxbImportState
> {
53 private static final Logger logger
= Logger
.getLogger(JaxbImport
.class);
54 private CdmDocumentBuilder cdmDocumentBuilder
= null;
57 /** Reads data from an XML file and stores them into a CDM DB.
60 * @param stores (not used)
64 protected void doInvoke(JaxbImportState state
) {
66 JaxbImportConfigurator jaxbImpConfig
= (JaxbImportConfigurator
)state
.getConfig();
69 String urlFileName
= jaxbImpConfig
.getSource().toString();
71 uri
= new URI(urlFileName
);
72 } catch (URISyntaxException ex
) {
73 logger
.error("File not found");
74 state
.setUnsuccessfull();
78 logger
.info("Deserializing " + urlFileName
+ " to DB " ); //+ dbname
80 DataSet dataSet
= new DataSet();
82 // unmarshalling XML file
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
);
90 } catch (Exception e
) {
91 logger
.error("Unmarshalling error");
96 logger
.info("Saving data to DB... "); //+ dbname
98 saveData(state
, dataSet
);
104 /** Saves data in DB */
105 private void saveData (JaxbImportState state
, DataSet dataSet
) {
106 JaxbImportConfigurator jaxbImpConfig
= state
.getConfig();
108 boolean success
= true;
109 Collection
<TaxonBase
> taxonBases
;
110 List
<?
extends AgentBase
> agents
;
111 List
<DefinedTermBase
> terms
;
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
;
121 List
<LanguageStringBase
> languageData
;
122 List
<TermVocabulary
<DefinedTermBase
>> termVocabularies
;
123 List
<HomotypicalGroup
> homotypicalGroups
;
125 TransactionStatus txStatus
= startTransaction();
126 //TransactionStatus txStatus = null;
128 // Have single transactions per service save call. Otherwise, getting
129 // H2 HYT00 error (timeout locking table DEFINEDTERMBASE) when running from editor.
132 if (jaxbImpConfig
.isDoUser() == true) {
134 * this is a crucial call, otherwise the password will not be set correctly
135 * and the whole authentication will not work
137 * a bit preliminary, should be used only if the complete database is replaced
139 authenticate(Configuration
.adminLogin
, Configuration
.adminPassword
);
141 logger
.info("Users: " + (users
= dataSet
.getUsers()).size());
142 for (User user
: users
) {
144 List
<User
> usersList
= getUserService().listByUsername(user
.getUsername(),null, null, null, null, null, null);
145 if (usersList
.isEmpty()){
146 getUserService().save(user
);
148 // User existingUser = usersList.get(0);
149 // user.setId(existingUser.getId());
150 // getUserService().merge(user);
151 //merging does not yet work because of #4102
156 } catch (Exception ex
) {
157 logger
.error("Error saving users");
158 ex
.printStackTrace();
162 if ((jaxbImpConfig
.isDoTerms() == true)
163 && (terms
= dataSet
.getTerms()).size() > 0) {
164 //txStatus = startTransaction();
165 success
&= saveTerms(terms
);
167 //commitTransaction(txStatus);
169 if ((jaxbImpConfig
.isDoTermVocabularies() == true)
170 && (termVocabularies
= dataSet
.getTermVocabularies()).size() > 0) {
171 //txStatus = startTransaction();
172 success
&= saveTermVocabularies(termVocabularies
);
176 // TODO: Have separate data save methods
178 // txStatus = startTransaction();
180 // if (jaxbImpConfig.isDoLanguageData() == true) {
181 // if ((languageData = dataSet.getLanguageData()).size() > 0) {
182 // logger.info("Language data: " + languageData.size());
183 // getTermService().saveLanguageDataAll(languageData);
186 // } catch (Exception ex) {
187 // logger.error("Error saving language data");
190 // commitTransaction(txStatus);
193 //txStatus = startTransaction();
195 if (jaxbImpConfig
.isDoAuthors() == true) {
196 if ((agents
= dataSet
.getAgents()).size() > 0) {
197 logger
.info("Agents: " + agents
.size());
198 getAgentService().save((Collection
)agents
);
201 } catch (Exception ex
) {
202 logger
.error("Error saving agents");
205 //commitTransaction(txStatus);
208 //txStatus = startTransaction();
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...");
217 } catch (Exception ex
) {
218 ex
.printStackTrace();
219 logger
.error("Error saving references");
222 //commitTransaction(txStatus);
225 //txStatus = startTransaction();
227 if (jaxbImpConfig
.isDoTaxonNames() == true) {
228 if ((taxonomicNames
= dataSet
.getTaxonomicNames()).size() > 0) {
229 logger
.info("Taxonomic names: " + taxonomicNames
.size());
230 getNameService().save(taxonomicNames
);
233 } catch (Exception ex
) {
234 logger
.error("Error saving taxon names");
237 //commitTransaction(txStatus);
240 //txStatus = startTransaction();
242 if (jaxbImpConfig
.isDoHomotypicalGroups() == true) {
243 if ((homotypicalGroups
= dataSet
.getHomotypicalGroups()).size() > 0) {
244 logger
.info("Homotypical groups: " + homotypicalGroups
.size());
245 getNameService().saveAllHomotypicalGroups(homotypicalGroups
);
249 } catch (Exception ex
) {
250 logger
.error("Error saving homotypical groups");
253 //commitTransaction(txStatus);
256 //txStatus = startTransaction();
257 // Need to get the taxa and the synonyms here.
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());
267 //getTaxonService().saveTaxonAll(taxonBases);
270 } catch (Exception ex
) {
271 logger
.error("Error saving taxa");
272 ex
.printStackTrace();
275 //commitTransaction(txStatus);
278 //txStatus = startTransaction();
279 // NomenclaturalStatus, TypeDesignations
281 if (jaxbImpConfig
.isDoTypeDesignations() == true) {
282 if ((typeDesignations
= dataSet
.getTypeDesignations()).size() > 0) {
283 logger
.info("Type Designations: " + typeDesignations
.size());
284 getNameService().saveTypeDesignationAll(typeDesignations
);
287 } catch (Exception ex
) {
288 logger
.error("Error saving type designations");
291 //commitTransaction(txStatus);
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);
300 //txStatus = startTransaction();
302 if (jaxbImpConfig
.isDoOccurrence() == true) {
303 if ((occurrences
= dataSet
.getOccurrences()).size() > 0) {
304 logger
.info("Occurrences: " + occurrences
.size());
305 getOccurrenceService().save(occurrences
);
308 } catch (Exception ex
) {
309 logger
.error("Error saving occurrences");
312 //commitTransaction(txStatus);
315 //txStatus = startTransaction();
317 if (jaxbImpConfig
.isDoFeatureData() == true) {
318 if ((featureTrees
= dataSet
.getFeatureTrees()).size() > 0) {
319 logger
.info("Feature data: " + featureTrees
.size());
320 getFeatureTreeService().save(featureTrees
);
323 } catch (Exception ex
) {
324 logger
.error("Error saving feature data");
327 //commitTransaction(txStatus);
330 //txStatus = startTransaction();
332 if (jaxbImpConfig
.isDoMedia() == true) {
333 if ((media
= dataSet
.getMedia()).size() > 0) {
334 logger
.info("Media: " + media
.size());
335 getMediaService().save(media
);
338 } catch (Exception ex
) {
339 logger
.error("Error saving media");
343 if (jaxbImpConfig
.isDoClassificationData() == true) {
344 logger
.info("# Classification");
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
);
355 commitTransaction(txStatus
);
356 logger
.info("All data saved");
358 state
.setUnsuccessfull();
366 private boolean saveTermVocabularies(
367 List
<TermVocabulary
<DefinedTermBase
>> termVocabularies
) {
369 boolean success
= true;
370 logger
.info("Term vocabularies: " + termVocabularies
.size());
372 getVocabularyService().save((List
)termVocabularies
);
373 } catch (Exception ex
) {
374 logger
.error("Error saving term vocabularies");
380 private boolean saveTerms(List
<DefinedTermBase
> terms
) {
382 boolean success
= true;
383 logger
.info("Terms: " + terms
.size());
385 getTermService().save(terms
);
386 } catch (Exception ex
) {
387 logger
.error("Error saving terms");
388 ex
.printStackTrace();
396 protected boolean doCheck(JaxbImportState state
) {
397 boolean result
= true;
398 logger
.warn("No validation implemented for Jaxb import");
404 protected boolean isIgnore(JaxbImportState state
) {