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
.CdmImportBase
;
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
.LanguageStringBase
;
28 import eu
.etaxonomy
.cdm
.model
.common
.User
;
29 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
30 import eu
.etaxonomy
.cdm
.model
.description
.FeatureNode
;
31 import eu
.etaxonomy
.cdm
.model
.description
.FeatureTree
;
32 import eu
.etaxonomy
.cdm
.model
.media
.Media
;
33 import eu
.etaxonomy
.cdm
.model
.name
.HomotypicalGroup
;
34 import eu
.etaxonomy
.cdm
.model
.name
.TaxonName
;
35 import eu
.etaxonomy
.cdm
.model
.name
.TypeDesignationBase
;
36 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
37 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
38 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
39 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
40 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
41 import eu
.etaxonomy
.cdm
.model
.term
.DefinedTermBase
;
42 import eu
.etaxonomy
.cdm
.model
.term
.TermVocabulary
;
45 * @author a.babadshanjan
49 public class JaxbImport
50 extends CdmImportBase
<JaxbImportConfigurator
, JaxbImportState
>
51 implements ICdmIO
<JaxbImportState
> {
53 private static final long serialVersionUID
= -96388140688227297L;
54 private static final Logger logger
= Logger
.getLogger(JaxbImport
.class);
56 private CdmDocumentBuilder cdmDocumentBuilder
= null;
59 /** Reads data from an XML file and stores them into a CDM DB.
62 * @param stores (not used)
66 protected void doInvoke(JaxbImportState state
) {
68 JaxbImportConfigurator jaxbImpConfig
= state
.getConfig();
71 String urlFileName
= jaxbImpConfig
.getSource().toString();
73 uri
= new URI(urlFileName
);
74 } catch (URISyntaxException ex
) {
75 logger
.error("File not found");
76 state
.setUnsuccessfull();
80 logger
.info("Deserializing " + urlFileName
+ " to DB " ); //+ dbname
82 DataSet dataSet
= new DataSet();
84 // unmarshalling XML file
86 cdmDocumentBuilder
= new CdmDocumentBuilder();
87 logger
.info("Unmarshalling " + urlFileName
);
88 File file
= new File(uri
);
89 logger
.debug("Absolute path: " + file
.getAbsolutePath());
90 dataSet
= cdmDocumentBuilder
.unmarshal(DataSet
.class, file
);
92 } catch (Exception e
) {
93 logger
.error("Unmarshalling error");
98 logger
.info("Saving data to DB... "); //+ dbname
100 saveData(state
, dataSet
);
106 /** Saves data in DB */
107 private void saveData (JaxbImportState state
, DataSet dataSet
) {
108 JaxbImportConfigurator jaxbImpConfig
= state
.getConfig();
110 boolean success
= true;
111 Collection
<TaxonBase
> taxonBases
;
112 List
<?
extends AgentBase
> agents
;
113 List
<DefinedTermBase
> terms
;
115 List
<Reference
> references
;
116 List
<TaxonName
> taxonomicNames
;
117 List
<DescriptionBase
> descriptions
;
118 List
<TypeDesignationBase
<?
>> typeDesignations
;
119 List
<SpecimenOrObservationBase
> occurrences
;
120 List
<FeatureTree
> featureTrees
;
121 List
<FeatureNode
> featureNodes
;
123 List
<LanguageStringBase
> languageData
;
124 List
<TermVocabulary
<DefinedTermBase
>> termVocabularies
;
125 List
<HomotypicalGroup
> homotypicalGroups
;
127 TransactionStatus txStatus
= startTransaction();
128 //TransactionStatus txStatus = null;
130 // Have single transactions per service save call. Otherwise, getting
131 // H2 HYT00 error (timeout locking table DEFINEDTERMBASE) when running from editor.
134 if (jaxbImpConfig
.isDoUser() == true) {
136 * this is a crucial call, otherwise the password will not be set correctly
137 * and the whole authentication will not work
139 * a bit preliminary, should be used only if the complete database is replaced
141 authenticate(Configuration
.adminLogin
, Configuration
.adminPassword
);
143 logger
.info("Users: " + (users
= dataSet
.getUsers()).size());
144 for (User user
: users
) {
146 List
<User
> usersList
= getUserService().listByUsername(user
.getUsername(),null, null, null, null, null, null);
147 if (usersList
.isEmpty()){
148 getUserService().save(user
);
150 // User existingUser = usersList.get(0);
151 // user.setId(existingUser.getId());
152 // getUserService().merge(user);
153 //merging does not yet work because of #4102
158 } catch (Exception ex
) {
159 logger
.error("Error saving users");
160 ex
.printStackTrace();
164 if ((jaxbImpConfig
.isDoTerms() == true)
165 && (terms
= dataSet
.getTerms()).size() > 0) {
166 //txStatus = startTransaction();
167 success
&= saveTerms(terms
);
169 //commitTransaction(txStatus);
171 if ((jaxbImpConfig
.isDoTermVocabularies() == true)
172 && (termVocabularies
= dataSet
.getTermVocabularies()).size() > 0) {
173 //txStatus = startTransaction();
174 success
&= saveTermVocabularies(termVocabularies
);
178 // TODO: Have separate data save methods
180 // txStatus = startTransaction();
182 // if (jaxbImpConfig.isDoLanguageData() == true) {
183 // if ((languageData = dataSet.getLanguageData()).size() > 0) {
184 // logger.info("Language data: " + languageData.size());
185 // getTermService().saveLanguageDataAll(languageData);
188 // } catch (Exception ex) {
189 // logger.error("Error saving language data");
192 // commitTransaction(txStatus);
195 //txStatus = startTransaction();
197 if (jaxbImpConfig
.isDoAuthors() == true) {
198 if ((agents
= dataSet
.getAgents()).size() > 0) {
199 logger
.info("Agents: " + agents
.size());
200 getAgentService().saveOrUpdate((Collection
)agents
);
203 } catch (Exception ex
) {
204 logger
.error("Error saving agents");
207 //commitTransaction(txStatus);
210 //txStatus = startTransaction();
212 if (jaxbImpConfig
.getDoReferences() != IImportConfigurator
.DO_REFERENCES
.NONE
) {
213 if ((references
= dataSet
.getReferences()).size() > 0) {
214 logger
.info("References: " + references
.size());
215 getReferenceService().saveOrUpdate(references
);
216 logger
.info("ready...");
219 } catch (Exception ex
) {
220 ex
.printStackTrace();
221 logger
.error("Error saving references");
224 //commitTransaction(txStatus);
227 //txStatus = startTransaction();
229 if (jaxbImpConfig
.isDoTaxonNames() == true) {
230 if ((taxonomicNames
= dataSet
.getTaxonomicNames()).size() > 0) {
231 logger
.info("Taxonomic names: " + taxonomicNames
.size());
232 getNameService().saveOrUpdate(taxonomicNames
);
235 } catch (Exception ex
) {
236 logger
.error("Error saving taxon names");
239 //commitTransaction(txStatus);
242 //txStatus = startTransaction();
244 if (jaxbImpConfig
.isDoHomotypicalGroups() == true) {
245 if ((homotypicalGroups
= dataSet
.getHomotypicalGroups()).size() > 0) {
246 logger
.info("Homotypical groups: " + homotypicalGroups
.size());
247 getNameService().saveAllHomotypicalGroups(homotypicalGroups
);
250 } catch (Exception ex
) {
251 logger
.error("Error saving homotypical groups");
254 //commitTransaction(txStatus);
257 //txStatus = startTransaction();
258 // Need to get the taxa and the synonyms here.
260 if (jaxbImpConfig
.isDoTaxa() == true) {
261 if ( dataSet
.getTaxonBases().size() > 0) {
262 List taxa
= dataSet
.getTaxonBases();
263 dataSet
.getTaxonBases();
264 logger
.info("Taxon bases: " + taxa
.size());
266 getTaxonService().saveOrUpdate(taxa
);
267 /*while (taxBases.hasNext()){
268 getTaxonService().save(taxBases.next());
270 //getTaxonService().saveTaxonAll(taxonBases);
273 } catch (Exception ex
) {
274 logger
.error("Error saving taxa");
275 ex
.printStackTrace();
278 //commitTransaction(txStatus);
281 //txStatus = startTransaction();
282 // NomenclaturalStatus, TypeDesignations
284 if (jaxbImpConfig
.isDoTypeDesignations() == true) {
285 if ((typeDesignations
= dataSet
.getTypeDesignations()).size() > 0) {
286 logger
.info("Type Designations: " + typeDesignations
.size());
287 getNameService().saveTypeDesignationAll(typeDesignations
);
290 } catch (Exception ex
) {
291 logger
.error("Error saving type designations");
294 //commitTransaction(txStatus);
297 // TODO: Implement dataSet.getDescriptions() and IDescriptionService.saveDescriptionAll()
298 // if ((descriptions = dataSet.getDescriptions()) != null) {
299 // logger.info("Saving " + descriptions.size() + " descriptions");
300 // getDescriptionService().saveDescriptionAll(descriptions);
303 //txStatus = startTransaction();
305 if (jaxbImpConfig
.isDoOccurrence() == true) {
306 if ((occurrences
= dataSet
.getOccurrences()).size() > 0) {
307 logger
.info("Occurrences: " + occurrences
.size());
308 getOccurrenceService().saveOrUpdate(occurrences
);
311 } catch (Exception ex
) {
312 logger
.error("Error saving occurrences");
315 //commitTransaction(txStatus);
318 //txStatus = startTransaction();
320 if (jaxbImpConfig
.isDoFeatureData() == true) {
321 if ((featureTrees
= dataSet
.getFeatureTrees()).size() > 0) {
322 logger
.info("Feature data: " + featureTrees
.size());
323 getFeatureTreeService().saveOrUpdate(featureTrees
);
326 } catch (Exception ex
) {
327 logger
.error("Error saving feature data");
330 //commitTransaction(txStatus);
333 //txStatus = startTransaction();
335 if (jaxbImpConfig
.isDoMedia() == true) {
336 if ((media
= dataSet
.getMedia()).size() > 0) {
337 logger
.info("Media: " + media
.size());
338 getMediaService().saveOrUpdate(media
);
341 } catch (Exception ex
) {
342 logger
.error("Error saving media");
346 if (jaxbImpConfig
.isDoClassificationData() == true) {
347 logger
.info("# Classification");
349 Collection
<TaxonNode
> nodes
= dataSet
.getTaxonNodes();
350 Collection
<Classification
> classifications
= dataSet
.getClassifications();
351 getClassificationService().saveTaxonNodeAll(nodes
);
352 for (Classification tree
: classifications
){
353 getClassificationService().saveOrUpdate(tree
);
358 commitTransaction(txStatus
);
359 logger
.info("All data saved");
361 state
.setUnsuccessfull();
369 private boolean saveTermVocabularies(
370 List
<TermVocabulary
<DefinedTermBase
>> termVocabularies
) {
372 boolean success
= true;
373 logger
.info("Term vocabularies: " + termVocabularies
.size());
375 getVocabularyService().saveOrUpdate((List
)termVocabularies
);
376 } catch (Exception ex
) {
377 logger
.error("Error saving term vocabularies");
383 private boolean saveTerms(List
<DefinedTermBase
> terms
) {
385 boolean success
= true;
386 logger
.info("Terms: " + terms
.size());
388 getTermService().save(terms
);
389 } catch (Exception ex
) {
390 logger
.error("Error saving terms");
391 ex
.printStackTrace();
399 protected boolean doCheck(JaxbImportState state
) {
400 boolean result
= true;
401 logger
.warn("No validation implemented for Jaxb import");
407 protected boolean isIgnore(JaxbImportState state
) {