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
.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
;
45 * @author a.babadshanjan
50 public class JaxbImport
extends CdmIoBase
<JaxbImportState
> implements ICdmIO
<JaxbImportState
> {
52 private static final Logger logger
= Logger
.getLogger(JaxbImport
.class);
53 private CdmDocumentBuilder cdmDocumentBuilder
= null;
56 /** Reads data from an XML file and stores them into a CDM DB.
59 * @param stores (not used)
62 // protected boolean doInvoke(IImportConfigurator config,
63 // Map<String, MapWrapper<? extends CdmBase>> stores) {
65 protected void doInvoke(JaxbImportState state
) {
69 JaxbImportConfigurator jaxbImpConfig
= (JaxbImportConfigurator
)state
.getConfig();
71 String urlFileName
= jaxbImpConfig
.getSource().toString();
72 logger
.debug("urlFileName: " + urlFileName
);
74 uri
= new URI(urlFileName
);
75 logger
.debug("uri: " + uri
.toString());
76 } catch (URISyntaxException ex
) {
77 logger
.error("File not found");
78 state
.setUnsuccessfull();
82 logger
.info("Deserializing " + urlFileName
+ " to DB " ); //+ dbname
84 DataSet dataSet
= new DataSet();
86 // unmarshalling XML file
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
);
94 } catch (Exception e
) {
95 logger
.error("Unmarshalling error");
100 logger
.info("Saving data to DB... "); //+ dbname
102 saveData(state
, dataSet
);
108 /** Saves data in DB */
109 private void saveData (JaxbImportState state
, DataSet dataSet
) {
110 JaxbImportConfigurator jaxbImpConfig
= state
.getConfig();
112 boolean success
= true;
113 Collection
<TaxonBase
> taxonBases
;
114 List
<?
extends AgentBase
> agents
;
115 List
<DefinedTermBase
> terms
;
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
;
125 List
<LanguageStringBase
> languageData
;
126 List
<TermVocabulary
<DefinedTermBase
>> termVocabularies
;
127 List
<HomotypicalGroup
> homotypicalGroups
;
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;
135 // Have single transactions per service save call. Otherwise, getting
136 // H2 HYT00 error (timeout locking table DEFINEDTERMBASE) when running from editor.
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.
146 if (jaxbImpConfig
.isDoUser() == true) {
147 if ((users
= dataSet
.getUsers()).size() > 0) {
148 logger
.info("Users: " + users
.size());
149 getUserService().save(users
);
153 } catch (Exception ex
) {
154 logger
.error("Error saving users");
158 if ((jaxbImpConfig
.isDoTerms() == true)
159 && (terms
= dataSet
.getTerms()).size() > 0) {
160 //txStatus = startTransaction();
161 success
&= saveTerms(terms
);
163 //commitTransaction(txStatus);
165 if ((jaxbImpConfig
.isDoTermVocabularies() == true)
166 && (termVocabularies
= dataSet
.getTermVocabularies()).size() > 0) {
167 //txStatus = startTransaction();
168 success
&= saveTermVocabularies(termVocabularies
);
172 // TODO: Have separate data save methods
174 // txStatus = startTransaction();
176 // if (jaxbImpConfig.isDoLanguageData() == true) {
177 // if ((languageData = dataSet.getLanguageData()).size() > 0) {
178 // logger.info("Language data: " + languageData.size());
179 // getTermService().saveLanguageDataAll(languageData);
182 // } catch (Exception ex) {
183 // logger.error("Error saving language data");
186 // commitTransaction(txStatus);
189 //txStatus = startTransaction();
191 if (jaxbImpConfig
.isDoAuthors() == true) {
192 if ((agents
= dataSet
.getAgents()).size() > 0) {
193 logger
.info("Agents: " + agents
.size());
194 getAgentService().save((Collection
)agents
);
197 } catch (Exception ex
) {
198 logger
.error("Error saving agents");
201 //commitTransaction(txStatus);
204 //txStatus = startTransaction();
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...");
213 } catch (Exception ex
) {
214 ex
.printStackTrace();
215 logger
.error("Error saving references");
218 //commitTransaction(txStatus);
221 //txStatus = startTransaction();
223 if (jaxbImpConfig
.isDoTaxonNames() == true) {
224 if ((taxonomicNames
= dataSet
.getTaxonomicNames()).size() > 0) {
225 logger
.info("Taxonomic names: " + taxonomicNames
.size());
226 getNameService().save(taxonomicNames
);
229 } catch (Exception ex
) {
230 logger
.error("Error saving taxon names");
233 //commitTransaction(txStatus);
236 //txStatus = startTransaction();
238 if (jaxbImpConfig
.isDoHomotypicalGroups() == true) {
239 if ((homotypicalGroups
= dataSet
.getHomotypicalGroups()).size() > 0) {
240 logger
.info("Homotypical groups: " + homotypicalGroups
.size());
241 getNameService().saveAllHomotypicalGroups(homotypicalGroups
);
245 } catch (Exception ex
) {
246 logger
.error("Error saving homotypical groups");
249 //commitTransaction(txStatus);
252 //txStatus = startTransaction();
253 // Need to get the taxa and the synonyms here.
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());
263 //getTaxonService().saveTaxonAll(taxonBases);
266 } catch (Exception ex
) {
267 logger
.error("Error saving taxa");
268 ex
.printStackTrace();
271 //commitTransaction(txStatus);
274 //txStatus = startTransaction();
275 // NomenclaturalStatus, TypeDesignations
277 if (jaxbImpConfig
.isDoTypeDesignations() == true) {
278 if ((typeDesignations
= dataSet
.getTypeDesignations()).size() > 0) {
279 logger
.info("Type Designations: " + typeDesignations
.size());
280 getNameService().saveTypeDesignationAll(typeDesignations
);
283 } catch (Exception ex
) {
284 logger
.error("Error saving type designations");
287 //commitTransaction(txStatus);
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);
296 //txStatus = startTransaction();
298 if (jaxbImpConfig
.isDoOccurrence() == true) {
299 if ((occurrences
= dataSet
.getOccurrences()).size() > 0) {
300 logger
.info("Occurrences: " + occurrences
.size());
301 getOccurrenceService().save(occurrences
);
304 } catch (Exception ex
) {
305 logger
.error("Error saving occurrences");
308 //commitTransaction(txStatus);
311 //txStatus = startTransaction();
313 if (jaxbImpConfig
.isDoFeatureData() == true) {
314 if ((featureTrees
= dataSet
.getFeatureTrees()).size() > 0) {
315 logger
.info("Feature data: " + featureTrees
.size());
316 getFeatureTreeService().save(featureTrees
);
319 } catch (Exception ex
) {
320 logger
.error("Error saving feature data");
323 //commitTransaction(txStatus);
326 //txStatus = startTransaction();
328 if (jaxbImpConfig
.isDoMedia() == true) {
329 if ((media
= dataSet
.getMedia()).size() > 0) {
330 logger
.info("Media: " + media
.size());
331 getMediaService().save(media
);
334 } catch (Exception ex
) {
335 logger
.error("Error saving media");
339 if (jaxbImpConfig
.isDoClassificationData() == true) {
340 logger
.info("# Classification");
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
);
351 commitTransaction(txStatus
);
352 logger
.info("All data saved");
354 state
.setUnsuccessfull();
362 private boolean saveTermVocabularies(
363 List
<TermVocabulary
<DefinedTermBase
>> termVocabularies
) {
365 boolean success
= true;
366 logger
.info("Term vocabularies: " + termVocabularies
.size());
368 getVocabularyService().save((List
)termVocabularies
);
369 } catch (Exception ex
) {
370 logger
.error("Error saving term vocabularies");
376 private boolean saveTerms(List
<DefinedTermBase
> terms
) {
378 boolean success
= true;
379 logger
.info("Terms: " + terms
.size());
381 getTermService().save(terms
);
382 } catch (Exception ex
) {
383 logger
.error("Error saving terms");
384 ex
.printStackTrace();
392 protected boolean doCheck(JaxbImportState state
) {
393 boolean result
= true;
394 logger
.warn("No validation implemented for Jaxb import");
400 protected boolean isIgnore(JaxbImportState state
) {