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
;
18 import org
.apache
.log4j
.Logger
;
19 import org
.springframework
.stereotype
.Component
;
20 import org
.springframework
.transaction
.TransactionStatus
;
22 import eu
.etaxonomy
.cdm
.config
.Configuration
;
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)
63 protected void doInvoke(JaxbImportState state
) {
65 JaxbImportConfigurator jaxbImpConfig
= (JaxbImportConfigurator
)state
.getConfig();
68 String urlFileName
= jaxbImpConfig
.getSource().toString();
70 uri
= new URI(urlFileName
);
71 } catch (URISyntaxException ex
) {
72 logger
.error("File not found");
73 state
.setUnsuccessfull();
77 logger
.info("Deserializing " + urlFileName
+ " to DB " ); //+ dbname
79 DataSet dataSet
= new DataSet();
81 // unmarshalling XML file
83 cdmDocumentBuilder
= new CdmDocumentBuilder();
84 logger
.info("Unmarshalling " + urlFileName
);
85 File file
= new File(uri
);
86 logger
.debug("Absolute path: " + file
.getAbsolutePath());
87 dataSet
= cdmDocumentBuilder
.unmarshal(DataSet
.class, file
);
89 } catch (Exception e
) {
90 logger
.error("Unmarshalling error");
95 logger
.info("Saving data to DB... "); //+ dbname
97 saveData(state
, dataSet
);
103 /** Saves data in DB */
104 private void saveData (JaxbImportState state
, DataSet dataSet
) {
105 JaxbImportConfigurator jaxbImpConfig
= state
.getConfig();
107 boolean success
= true;
108 Collection
<TaxonBase
> taxonBases
;
109 List
<?
extends AgentBase
> agents
;
110 List
<DefinedTermBase
> terms
;
112 List
<Reference
> references
;
113 List
<TaxonNameBase
> taxonomicNames
;
114 List
<DescriptionBase
> descriptions
;
115 List
<TypeDesignationBase
> typeDesignations
;
116 List
<SpecimenOrObservationBase
> occurrences
;
117 List
<FeatureTree
> featureTrees
;
118 List
<FeatureNode
> featureNodes
;
120 List
<LanguageStringBase
> languageData
;
121 List
<TermVocabulary
<DefinedTermBase
>> termVocabularies
;
122 List
<HomotypicalGroup
> homotypicalGroups
;
124 TransactionStatus txStatus
= startTransaction();
125 //TransactionStatus txStatus = null;
127 // Have single transactions per service save call. Otherwise, getting
128 // H2 HYT00 error (timeout locking table DEFINEDTERMBASE) when running from editor.
131 if (jaxbImpConfig
.isDoUser() == true) {
133 * this is a crucial call, otherwise the password will not be set correctly
134 * and the whole authentication will not work
136 * a bit preliminary, should be used only if the complete database is replaced
138 authenticate(Configuration
.adminLogin
, Configuration
.adminPassword
);
140 logger
.info("Users: " + (users
= dataSet
.getUsers()).size());
141 for (User user
: users
) {
143 List
<User
> usersList
= getUserService().listByUsername(user
.getUsername(),null, null, null, null, null, null);
144 if (usersList
.isEmpty()){
145 getUserService().save(user
);
147 // User existingUser = usersList.get(0);
148 // user.setId(existingUser.getId());
149 // getUserService().merge(user);
150 //merging does not yet work because of #4102
155 } catch (Exception ex
) {
156 logger
.error("Error saving users");
157 ex
.printStackTrace();
161 if ((jaxbImpConfig
.isDoTerms() == true)
162 && (terms
= dataSet
.getTerms()).size() > 0) {
163 //txStatus = startTransaction();
164 success
&= saveTerms(terms
);
166 //commitTransaction(txStatus);
168 if ((jaxbImpConfig
.isDoTermVocabularies() == true)
169 && (termVocabularies
= dataSet
.getTermVocabularies()).size() > 0) {
170 //txStatus = startTransaction();
171 success
&= saveTermVocabularies(termVocabularies
);
175 // TODO: Have separate data save methods
177 // txStatus = startTransaction();
179 // if (jaxbImpConfig.isDoLanguageData() == true) {
180 // if ((languageData = dataSet.getLanguageData()).size() > 0) {
181 // logger.info("Language data: " + languageData.size());
182 // getTermService().saveLanguageDataAll(languageData);
185 // } catch (Exception ex) {
186 // logger.error("Error saving language data");
189 // commitTransaction(txStatus);
192 //txStatus = startTransaction();
194 if (jaxbImpConfig
.isDoAuthors() == true) {
195 if ((agents
= dataSet
.getAgents()).size() > 0) {
196 logger
.info("Agents: " + agents
.size());
197 getAgentService().save((Collection
)agents
);
200 } catch (Exception ex
) {
201 logger
.error("Error saving agents");
204 //commitTransaction(txStatus);
207 //txStatus = startTransaction();
209 if (jaxbImpConfig
.getDoReferences() != IImportConfigurator
.DO_REFERENCES
.NONE
) {
210 if ((references
= dataSet
.getReferences()).size() > 0) {
211 logger
.info("References: " + references
.size());
212 getReferenceService().save(references
);
213 logger
.info("ready...");
216 } catch (Exception ex
) {
217 ex
.printStackTrace();
218 logger
.error("Error saving references");
221 //commitTransaction(txStatus);
224 //txStatus = startTransaction();
226 if (jaxbImpConfig
.isDoTaxonNames() == true) {
227 if ((taxonomicNames
= dataSet
.getTaxonomicNames()).size() > 0) {
228 logger
.info("Taxonomic names: " + taxonomicNames
.size());
229 getNameService().save(taxonomicNames
);
232 } catch (Exception ex
) {
233 logger
.error("Error saving taxon names");
236 //commitTransaction(txStatus);
239 //txStatus = startTransaction();
241 if (jaxbImpConfig
.isDoHomotypicalGroups() == true) {
242 if ((homotypicalGroups
= dataSet
.getHomotypicalGroups()).size() > 0) {
243 logger
.info("Homotypical groups: " + homotypicalGroups
.size());
244 getNameService().saveAllHomotypicalGroups(homotypicalGroups
);
248 } catch (Exception ex
) {
249 logger
.error("Error saving homotypical groups");
252 //commitTransaction(txStatus);
255 //txStatus = startTransaction();
256 // Need to get the taxa and the synonyms here.
258 if (jaxbImpConfig
.isDoTaxa() == true) {
259 if ( dataSet
.getTaxonBases().size() > 0) {
260 List taxa
= dataSet
.getTaxonBases();
261 dataSet
.getTaxonBases();
262 logger
.info("Taxon bases: " + taxa
.size());
264 getTaxonService().save(taxa
);
265 /*while (taxBases.hasNext()){
266 getTaxonService().save(taxBases.next());
268 //getTaxonService().saveTaxonAll(taxonBases);
271 } catch (Exception ex
) {
272 logger
.error("Error saving taxa");
273 ex
.printStackTrace();
276 //commitTransaction(txStatus);
279 //txStatus = startTransaction();
280 // NomenclaturalStatus, TypeDesignations
282 if (jaxbImpConfig
.isDoTypeDesignations() == true) {
283 if ((typeDesignations
= dataSet
.getTypeDesignations()).size() > 0) {
284 logger
.info("Type Designations: " + typeDesignations
.size());
285 getNameService().saveTypeDesignationAll(typeDesignations
);
288 } catch (Exception ex
) {
289 logger
.error("Error saving type designations");
292 //commitTransaction(txStatus);
295 // TODO: Implement dataSet.getDescriptions() and IDescriptionService.saveDescriptionAll()
296 // if ((descriptions = dataSet.getDescriptions()) != null) {
297 // logger.info("Saving " + descriptions.size() + " descriptions");
298 // getDescriptionService().saveDescriptionAll(descriptions);
301 //txStatus = startTransaction();
303 if (jaxbImpConfig
.isDoOccurrence() == true) {
304 if ((occurrences
= dataSet
.getOccurrences()).size() > 0) {
305 logger
.info("Occurrences: " + occurrences
.size());
306 getOccurrenceService().save(occurrences
);
309 } catch (Exception ex
) {
310 logger
.error("Error saving occurrences");
313 //commitTransaction(txStatus);
316 //txStatus = startTransaction();
318 if (jaxbImpConfig
.isDoFeatureData() == true) {
319 if ((featureTrees
= dataSet
.getFeatureTrees()).size() > 0) {
320 logger
.info("Feature data: " + featureTrees
.size());
321 getFeatureTreeService().save(featureTrees
);
324 } catch (Exception ex
) {
325 logger
.error("Error saving feature data");
328 //commitTransaction(txStatus);
331 //txStatus = startTransaction();
333 if (jaxbImpConfig
.isDoMedia() == true) {
334 if ((media
= dataSet
.getMedia()).size() > 0) {
335 logger
.info("Media: " + media
.size());
336 getMediaService().save(media
);
339 } catch (Exception ex
) {
340 logger
.error("Error saving media");
344 if (jaxbImpConfig
.isDoClassificationData() == true) {
345 logger
.info("# Classification");
347 Collection
<TaxonNode
> nodes
= dataSet
.getTaxonNodes();
348 Collection
<Classification
> classifications
= dataSet
.getClassifications();
349 getClassificationService().saveTaxonNodeAll(nodes
);
350 for (Classification tree
: classifications
){
351 getClassificationService().saveOrUpdate(tree
);
356 commitTransaction(txStatus
);
357 logger
.info("All data saved");
359 state
.setUnsuccessfull();
367 private boolean saveTermVocabularies(
368 List
<TermVocabulary
<DefinedTermBase
>> termVocabularies
) {
370 boolean success
= true;
371 logger
.info("Term vocabularies: " + termVocabularies
.size());
373 getVocabularyService().save((List
)termVocabularies
);
374 } catch (Exception ex
) {
375 logger
.error("Error saving term vocabularies");
381 private boolean saveTerms(List
<DefinedTermBase
> terms
) {
383 boolean success
= true;
384 logger
.info("Terms: " + terms
.size());
386 getTermService().save(terms
);
387 } catch (Exception ex
) {
388 logger
.error("Error saving terms");
389 ex
.printStackTrace();
397 protected boolean doCheck(JaxbImportState state
) {
398 boolean result
= true;
399 logger
.warn("No validation implemented for Jaxb import");
405 protected boolean isIgnore(JaxbImportState state
) {