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
.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
.io
.common
.ImportStateBase
;
27 import eu
.etaxonomy
.cdm
.io
.common
.MapWrapper
;
28 import eu
.etaxonomy
.cdm
.model
.agent
.AgentBase
;
29 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
30 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTermBase
;
31 import eu
.etaxonomy
.cdm
.model
.common
.LanguageStringBase
;
32 import eu
.etaxonomy
.cdm
.model
.common
.ReferencedEntityBase
;
33 import eu
.etaxonomy
.cdm
.model
.common
.TermVocabulary
;
34 import eu
.etaxonomy
.cdm
.model
.common
.VersionableEntity
;
35 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
36 import eu
.etaxonomy
.cdm
.model
.description
.FeatureTree
;
37 import eu
.etaxonomy
.cdm
.model
.media
.Media
;
38 import eu
.etaxonomy
.cdm
.model
.name
.HomotypicalGroup
;
39 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
40 import eu
.etaxonomy
.cdm
.model
.name
.TypeDesignationBase
;
41 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
42 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceBase
;
43 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
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)
63 // protected boolean doInvoke(IImportConfigurator config,
64 // Map<String, MapWrapper<? extends CdmBase>> stores) {
66 protected boolean doInvoke(JaxbImportState state
) {
69 boolean success
= true;
71 JaxbImportConfigurator jaxbImpConfig
= (JaxbImportConfigurator
)state
.getConfig();
73 String urlFileName
= (String
)jaxbImpConfig
.getSource();
74 logger
.debug("urlFileName: " + urlFileName
);
76 uri
= new URI(urlFileName
);
77 logger
.debug("uri: " + uri
.toString());
78 } catch (URISyntaxException ex
) {
79 logger
.error("File not found");
83 logger
.info("Deserializing file " + urlFileName
+ " to DB " ); //+ dbname
85 DataSet dataSet
= new DataSet();
87 // unmarshalling XML file
89 cdmDocumentBuilder
= new CdmDocumentBuilder();
90 logger
.info("Unmarshalling file: " + urlFileName
);
91 File file
= new File(uri
);
92 logger
.debug("Absolute path: " + file
.getAbsolutePath());
93 dataSet
= cdmDocumentBuilder
.unmarshal(DataSet
.class, file
);
95 } catch (Exception e
) {
96 logger
.error("Unmarshalling error");
101 logger
.info("Saving data to DB: " ); //+ dbname
102 success
= saveData(jaxbImpConfig
, dataSet
);
108 /** Saves data in DB */
109 private boolean saveData (JaxbImportConfigurator jaxbImpConfig
, DataSet dataSet
) {
112 Collection
<TaxonBase
> taxonBases
;
113 List
<?
extends AgentBase
> agents
;
114 List
<DefinedTermBase
> terms
;
115 List
<ReferenceBase
> references
;
116 List
<TaxonNameBase
> taxonomicNames
;
117 List
<DescriptionBase
> descriptions
;
118 List
<TypeDesignationBase
> typeDesignations
;
119 List
<SpecimenOrObservationBase
> occurrences
;
120 List
<FeatureTree
> featureTrees
;
122 List
<LanguageStringBase
> languageData
;
123 List
<TermVocabulary
<DefinedTermBase
>> termVocabularies
;
124 List
<HomotypicalGroup
> homotypicalGroups
;
126 // Get an app controller that omits term loading
127 // CdmApplicationController.getCdmAppController(boolean createNew, boolean omitTermLoading){
128 // CdmApplicationController appCtr = jaxbImpConfig.getCdmAppController(false, true);
129 //TransactionStatus txStatus = startTransaction();
130 //TransactionStatus txStatus = null;
132 // Have single transactions per service save call. Otherwise, getting
133 // H2 HYT00 error (timeout locking table DEFINEDTERMBASE) when running from editor.
135 // If data of a certain type, such as terms, are not saved here explicitly,
136 // then only those data of this type that are referenced by other objects are saved implicitly.
137 // For example, if taxa are saved all other data referenced by those taxa, such as synonyms,
138 // are automatically saved as well.
140 if ((jaxbImpConfig
.isDoTermVocabularies() == true)
141 && (termVocabularies
= dataSet
.getTermVocabularies()).size() > 0) {
142 //txStatus = startTransaction();
143 ret
&= saveTermVocabularies(termVocabularies
);
144 //commitTransaction(txStatus);
147 if ((jaxbImpConfig
.isDoTerms() == true)
148 && (terms
= dataSet
.getTerms()).size() > 0) {
149 //txStatus = startTransaction();
150 ret
&= saveTerms(terms
);
151 //commitTransaction(txStatus);
154 // TODO: Have separate data save methods
156 // txStatus = startTransaction();
158 // if (jaxbImpConfig.isDoLanguageData() == true) {
159 // if ((languageData = dataSet.getLanguageData()).size() > 0) {
160 // logger.info("Language data: " + languageData.size());
161 // getTermService().saveLanguageDataAll(languageData);
164 // } catch (Exception ex) {
165 // logger.error("Error saving language data");
168 // commitTransaction(txStatus);
171 //txStatus = startTransaction();
173 if (jaxbImpConfig
.isDoAuthors() == true) {
174 if ((agents
= dataSet
.getAgents()).size() > 0) {
175 logger
.info("Agents: " + agents
.size());
176 getAgentService().saveAgentAll(agents
);
179 } catch (Exception ex
) {
180 logger
.error("Error saving agents");
183 //commitTransaction(txStatus);
186 //txStatus = startTransaction();
188 if (jaxbImpConfig
.getDoReferences() != IImportConfigurator
.DO_REFERENCES
.NONE
) {
189 if ((references
= dataSet
.getReferences()).size() > 0) {
190 logger
.info("References: " + references
.size());
191 getReferenceService().saveReferenceAll(references
);
194 } catch (Exception ex
) {
195 logger
.error("Error saving references");
198 //commitTransaction(txStatus);
201 //txStatus = startTransaction();
203 if (jaxbImpConfig
.isDoTaxonNames() == true) {
204 if ((taxonomicNames
= dataSet
.getTaxonomicNames()).size() > 0) {
205 logger
.info("Taxonomic names: " + taxonomicNames
.size());
206 getNameService().saveTaxonNameAll(taxonomicNames
);
209 } catch (Exception ex
) {
210 logger
.error("Error saving taxon names");
213 //commitTransaction(txStatus);
216 //txStatus = startTransaction();
218 if (jaxbImpConfig
.isDoHomotypicalGroups() == true) {
219 if ((homotypicalGroups
= dataSet
.getHomotypicalGroups()).size() > 0) {
220 logger
.info("Homotypical groups: " + homotypicalGroups
.size());
221 getNameService().saveAllHomotypicalGroups(homotypicalGroups
);
224 } catch (Exception ex
) {
225 logger
.error("Error saving homotypical groups");
228 //commitTransaction(txStatus);
231 //txStatus = startTransaction();
232 // Need to get the taxa and the synonyms here.
234 if (jaxbImpConfig
.isDoTaxa() == true) {
235 if ((taxonBases
= dataSet
.getTaxonBases()).size() > 0) {
236 logger
.info("Taxon bases: " + taxonBases
.size());
237 getTaxonService().saveTaxonAll(taxonBases
);
240 } catch (Exception ex
) {
241 logger
.error("Error saving taxa");
244 //commitTransaction(txStatus);
247 //txStatus = startTransaction();
248 // NomenclaturalStatus, TypeDesignations
250 if (jaxbImpConfig
.isDoTypeDesignations() == true) {
251 if ((typeDesignations
= dataSet
.getTypeDesignations()).size() > 0) {
252 logger
.info("Type Designations: " + typeDesignations
.size());
253 getNameService().saveTypeDesignationAll(typeDesignations
);
256 } catch (Exception ex
) {
257 logger
.error("Error saving type designations");
260 //commitTransaction(txStatus);
263 // TODO: Implement dataSet.getDescriptions() and IDescriptionService.saveDescriptionAll()
264 // if ((descriptions = dataSet.getDescriptions()) != null) {
265 // logger.info("Saving " + descriptions.size() + " descriptions");
266 // getDescriptionService().saveDescriptionAll(descriptions);
269 //txStatus = startTransaction();
271 if (jaxbImpConfig
.isDoOccurrence() == true) {
272 if ((occurrences
= dataSet
.getOccurrences()).size() > 0) {
273 logger
.info("Occurrences: " + occurrences
.size());
274 getOccurrenceService().saveSpecimenOrObservationBaseAll(occurrences
);
277 } catch (Exception ex
) {
278 logger
.error("Error saving occurrences");
281 //commitTransaction(txStatus);
284 //txStatus = startTransaction();
286 if (jaxbImpConfig
.isDoFeatureData() == true) {
287 if ((featureTrees
= dataSet
.getFeatureTrees()).size() > 0) {
288 logger
.info("Feature data: " + featureTrees
.size());
289 getDescriptionService().saveFeatureTreeAll(featureTrees
);
292 } catch (Exception ex
) {
293 logger
.error("Error saving feature data");
296 //commitTransaction(txStatus);
299 //txStatus = startTransaction();
301 if (jaxbImpConfig
.isDoMedia() == true) {
302 if ((media
= dataSet
.getMedia()).size() > 0) {
303 logger
.info("Media: " + media
.size());
304 getMediaService().saveMediaAll(media
);
307 } catch (Exception ex
) {
308 logger
.error("Error saving media");
311 //commitTransaction(txStatus);
313 //commitTransaction(txStatus);
314 logger
.info("All data saved");
321 private boolean saveTermVocabularies(
322 List
<TermVocabulary
<DefinedTermBase
>> termVocabularies
) {
324 boolean success
= true;
325 logger
.info("Term vocabularies: " + termVocabularies
.size());
327 getTermService().saveTermVocabulariesAll(termVocabularies
);
328 } catch (Exception ex
) {
329 logger
.error("Error saving term vocabularies");
335 private boolean saveTerms(List
<DefinedTermBase
> terms
) {
337 boolean success
= true;
338 logger
.info("Terms: " + terms
.size());
340 getTermService().saveTermsAll(terms
);
341 } catch (Exception ex
) {
342 logger
.error("Error saving terms");
350 protected boolean doCheck(JaxbImportState state
) {
351 boolean result
= true;
352 logger
.warn("No check implemented for Jaxb import");
358 protected boolean isIgnore(JaxbImportState state
) {