2 * Copyright (C) 2008 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
7 package eu
.etaxonomy
.cdm
.io
.jaxb
;
9 import java
.io
.FileOutputStream
;
10 import java
.io
.OutputStreamWriter
;
11 import java
.io
.PrintWriter
;
12 import java
.util
.HashSet
;
13 import java
.util
.List
;
17 import org
.apache
.log4j
.Logger
;
18 import org
.springframework
.stereotype
.Component
;
19 import org
.springframework
.transaction
.TransactionStatus
;
21 import eu
.etaxonomy
.cdm
.io
.common
.CdmIoBase
;
22 import eu
.etaxonomy
.cdm
.io
.common
.ICdmIO
;
23 import eu
.etaxonomy
.cdm
.io
.common
.IExportConfigurator
;
24 import eu
.etaxonomy
.cdm
.io
.common
.IImportConfigurator
;
25 import eu
.etaxonomy
.cdm
.io
.common
.MapWrapper
;
26 import eu
.etaxonomy
.cdm
.model
.agent
.Agent
;
27 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
28 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTermBase
;
29 import eu
.etaxonomy
.cdm
.model
.common
.RelationshipBase
;
30 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
31 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
32 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceBase
;
33 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
34 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
35 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
38 * @author a.babadshanjan
42 public class JaxbExport
extends CdmIoBase
<IExportConfigurator
> implements ICdmIO
<IExportConfigurator
> {
43 // public class JaxbExport extends CdmIoBase implements ICdmIoExport {
44 // TODO: public class JaxbExport extends CdmIoBase implements ICdmIO {
46 private static final Logger logger
= Logger
.getLogger(JaxbExport
.class);
47 private CdmDocumentBuilder cdmDocumentBuilder
= null;
49 private String ioName
= null;
57 this.ioName
= this.getClass().getSimpleName();
60 /** Retrieves data from a CDM DB and serializes them CDM to XML.
61 * Starts with root taxa and traverses the taxonomic tree to retrieve children taxa, synonyms and relationships.
62 * Taxa that are not part of the taxonomic tree are not found.
69 protected boolean doInvoke(IExportConfigurator config
,
70 Map
<String
, MapWrapper
<?
extends CdmBase
>> stores
) {
72 JaxbExportConfigurator jaxbExpConfig
= (JaxbExportConfigurator
)config
;
73 String dbname
= jaxbExpConfig
.getSource().getName();
74 String fileName
= jaxbExpConfig
.getDestination();
75 logger
.info("Serializing DB " + dbname
+ " to file " + fileName
);
76 logger
.debug("DbSchemaValidation = " + jaxbExpConfig
.getDbSchemaValidation());
78 TransactionStatus txStatus
= startTransaction(true);
79 DataSet dataSet
= new DataSet();
80 List
<Taxon
> taxa
= null;
81 List
<DefinedTermBase
> terms
= null;
86 logger
.info("Retrieving data from DB");
88 retrieveData(config
, dataSet
);
90 } catch (Exception e
) {
91 logger
.error("Error retrieving data");
95 logger
.info("All data retrieved");
98 cdmDocumentBuilder
= new CdmDocumentBuilder();
99 PrintWriter writer
= new PrintWriter(new OutputStreamWriter(new FileOutputStream(fileName
), "UTF8"), true);
100 cdmDocumentBuilder
.marshal(dataSet
, writer
);
102 // TODO: Split into one file per data set member to see whether performance improves?
104 logger
.info("XML file written");
105 logger
.info("Filename is: " + fileName
);
107 } catch (Exception e
) {
108 logger
.error("Marshalling error");
111 commitTransaction(txStatus
);
118 private void retrieveData (IExportConfigurator config
, DataSet dataSet
) {
120 JaxbExportConfigurator jaxbExpConfig
= (JaxbExportConfigurator
)config
;
121 final int MAX_ROWS
= 50000;
122 int numberOfRows
= jaxbExpConfig
.getMaxRows();
124 int agentRows
= numberOfRows
;
125 int definedTermBaseRows
= numberOfRows
;
126 int referenceBaseRows
= numberOfRows
;
127 int taxonNameBaseRows
= numberOfRows
;
128 int taxonBaseRows
= numberOfRows
;
129 int relationshipRows
= numberOfRows
;
130 int occurrencesRows
= numberOfRows
;
131 int mediaRows
= numberOfRows
;
132 int featureDataRows
= numberOfRows
;
133 int languageDataRows
= numberOfRows
;
134 int termVocabularyRows
= numberOfRows
;
135 int homotypicalGroupRows
= numberOfRows
;
137 if (jaxbExpConfig
.isDoTermVocabularies() == true) {
138 if (termVocabularyRows
== 0) { termVocabularyRows
= MAX_ROWS
; }
139 logger
.info("# TermVocabulary");
140 dataSet
.setTermVocabularies(getTermService().getAllTermVocabularies(MAX_ROWS
, 0));;
143 if (jaxbExpConfig
.isDoLanguageData() == true) {
144 if (languageDataRows
== 0) { languageDataRows
= MAX_ROWS
; }
145 logger
.info("# Representation, Language String");
146 dataSet
.setLanguageData(getTermService().getAllRepresentations(MAX_ROWS
, 0));
147 dataSet
.addLanguageData(getTermService().getAllLanguageStrings(MAX_ROWS
, 0));
150 if (jaxbExpConfig
.isDoTerms() == true) {
151 if (definedTermBaseRows
== 0) { definedTermBaseRows
= getTermService().count(DefinedTermBase
.class); }
152 logger
.info("# DefinedTermBase: " + definedTermBaseRows
);
153 dataSet
.setTerms(getTermService().getAllDefinedTerms(definedTermBaseRows
, 0));
156 if (jaxbExpConfig
.isDoAuthors() == true) {
157 if (agentRows
== 0) { agentRows
= getAgentService().count(Agent
.class); }
158 logger
.info("# Agents: " + agentRows
);
159 //logger.info(" # Team: " + appCtr.getAgentService().count(Team.class));
160 dataSet
.setAgents(getAgentService().getAllAgents(agentRows
, 0));
163 if (jaxbExpConfig
.getDoReferences() != IImportConfigurator
.DO_REFERENCES
.NONE
) {
164 if (referenceBaseRows
== 0) { referenceBaseRows
= getReferenceService().count(ReferenceBase
.class); }
165 logger
.info("# ReferenceBase: " + referenceBaseRows
);
166 dataSet
.setReferences(getReferenceService().getAllReferences(referenceBaseRows
, 0));
169 if (jaxbExpConfig
.isDoTaxonNames() == true) {
170 if (taxonNameBaseRows
== 0) { taxonNameBaseRows
= getNameService().count(TaxonNameBase
.class); }
171 logger
.info("# TaxonNameBase: " + taxonNameBaseRows
);
172 //logger.info(" # Taxon: " + getNameService().count(BotanicalName.class));
173 dataSet
.setTaxonomicNames(getNameService().getAllNames(taxonNameBaseRows
, 0));
176 if (jaxbExpConfig
.isDoHomotypicalGroups() == true) {
177 if (homotypicalGroupRows
== 0) { homotypicalGroupRows
= MAX_ROWS
; }
178 logger
.info("# Homotypical Groups");
179 dataSet
.setHomotypicalGroups(getNameService().getAllHomotypicalGroups(homotypicalGroupRows
, 0));
182 if (jaxbExpConfig
.isDoTaxa() == true) {
183 if (taxonBaseRows
== 0) { taxonBaseRows
= getTaxonService().count(TaxonBase
.class); }
184 logger
.info("# TaxonBase: " + taxonBaseRows
);
185 // dataSet.setTaxa(new ArrayList<Taxon>());
186 // dataSet.setSynonyms(new ArrayList<Synonym>());
187 List
<TaxonBase
> tb
= getTaxonService().getAllTaxonBases(taxonBaseRows
, 0);
188 for (TaxonBase taxonBase
: tb
) {
189 if (taxonBase
instanceof Taxon
) {
190 dataSet
.addTaxon((Taxon
)taxonBase
);
191 } else if (taxonBase
instanceof Synonym
) {
192 dataSet
.addSynonym((Synonym
)taxonBase
);
194 logger
.error("entry of wrong type: " + taxonBase
.toString());
200 // retrieve taxa and synonyms separately
201 // need correct count for taxa and synonyms
202 // if (taxonBaseRows == 0) { taxonBaseRows = getTaxonService().count(TaxonBase.class); }
203 // logger.info("# Synonym: " + taxonBaseRows);
204 // dataSet.setSynonyms(new ArrayList<Synonym>());
205 // dataSet.setSynonyms(getTaxonService().getAllSynonyms(taxonBaseRows, 0));
207 if (jaxbExpConfig
.isDoRelTaxa() == true) {
208 if (relationshipRows
== 0) { relationshipRows
= MAX_ROWS
; }
209 logger
.info("# Relationships");
210 List
<RelationshipBase
> relationList
= getTaxonService().getAllRelationships(relationshipRows
, 0);
211 Set
<RelationshipBase
> relationSet
= new HashSet
<RelationshipBase
>(relationList
);
212 dataSet
.setRelationships(relationSet
);
215 if (jaxbExpConfig
.isDoReferencedEntities() == true) {
216 logger
.info("# Referenced Entities");
217 dataSet
.setReferencedEntities(getNameService().getAllNomenclaturalStatus(MAX_ROWS
, 0));
218 dataSet
.addReferencedEntities(getNameService().getAllTypeDesignations(MAX_ROWS
, 0));
221 if (jaxbExpConfig
.isDoOccurrence() == true) {
222 if (occurrencesRows
== 0) { occurrencesRows
= getOccurrenceService().count(SpecimenOrObservationBase
.class); }
223 logger
.info("# SpecimenOrObservationBase: " + occurrencesRows
);
224 dataSet
.setOccurrences(getOccurrenceService().getAllSpecimenOrObservationBases(occurrencesRows
, 0));
227 if (jaxbExpConfig
.isDoMedia() == true) {
228 if (mediaRows
== 0) { mediaRows
= MAX_ROWS
; }
229 logger
.info("# Media");
230 dataSet
.setMedia(getMediaService().getAllMedia(mediaRows
, 0));
231 // dataSet.addMedia(getMediaService().getAllMediaRepresentations(mediaRows, 0));
232 // dataSet.addMedia(getMediaService().getAllMediaRepresentationParts(mediaRows, 0));
235 if (jaxbExpConfig
.isDoFeatureData() == true) {
236 if (featureDataRows
== 0) { featureDataRows
= MAX_ROWS
; }
237 logger
.info("# Feature Tree, Feature Node");
238 dataSet
.setFeatureData(getDescriptionService().getFeatureNodesAll());
239 dataSet
.addFeatureData(getDescriptionService().getFeatureTreesAll());
245 protected boolean doCheck(IExportConfigurator config
) {
246 boolean result
= true;
247 logger
.warn("No check implemented for Jaxb export");
253 protected boolean isIgnore(IExportConfigurator config
) {