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
;
12 import java
.io
.FileOutputStream
;
13 import java
.io
.OutputStreamWriter
;
14 import java
.io
.PrintWriter
;
15 import java
.util
.HashSet
;
16 import java
.util
.List
;
20 import org
.apache
.log4j
.Logger
;
21 import org
.springframework
.stereotype
.Component
;
22 import org
.springframework
.transaction
.TransactionStatus
;
24 import eu
.etaxonomy
.cdm
.io
.common
.CdmIoBase
;
25 import eu
.etaxonomy
.cdm
.io
.common
.ICdmIO
;
26 import eu
.etaxonomy
.cdm
.io
.common
.IExportConfigurator
;
27 import eu
.etaxonomy
.cdm
.io
.common
.IImportConfigurator
;
28 import eu
.etaxonomy
.cdm
.io
.common
.MapWrapper
;
29 import eu
.etaxonomy
.cdm
.model
.agent
.Agent
;
30 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
31 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTermBase
;
32 import eu
.etaxonomy
.cdm
.model
.common
.RelationshipBase
;
33 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
34 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
35 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceBase
;
36 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
37 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
38 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
41 * @author a.babadshanjan
45 public class JaxbExport
extends CdmIoBase
<IExportConfigurator
> implements ICdmIO
<IExportConfigurator
> {
46 // public class JaxbExport extends CdmIoBase implements ICdmIoExport {
47 // TODO: public class JaxbExport extends CdmIoBase implements ICdmIO {
49 private static final Logger logger
= Logger
.getLogger(JaxbExport
.class);
50 private CdmDocumentBuilder cdmDocumentBuilder
= null;
52 private String ioName
= null;
60 this.ioName
= this.getClass().getSimpleName();
63 /** Retrieves data from a CDM DB and serializes them CDM to XML.
64 * Starts with root taxa and traverses the taxonomic tree to retrieve children taxa, synonyms and relationships.
65 * Taxa that are not part of the taxonomic tree are not found.
72 protected boolean doInvoke(IExportConfigurator config
,
73 Map
<String
, MapWrapper
<?
extends CdmBase
>> stores
) {
75 JaxbExportConfigurator jaxbExpConfig
= (JaxbExportConfigurator
)config
;
76 String dbname
= jaxbExpConfig
.getSource().getName();
77 String fileName
= jaxbExpConfig
.getDestination();
78 logger
.info("Serializing DB " + dbname
+ " to file " + fileName
);
79 logger
.debug("DbSchemaValidation = " + jaxbExpConfig
.getDbSchemaValidation());
81 TransactionStatus txStatus
= startTransaction(true);
82 DataSet dataSet
= new DataSet();
83 List
<Taxon
> taxa
= null;
84 List
<DefinedTermBase
> terms
= null;
89 logger
.info("Retrieving data from DB");
91 retrieveData(config
, dataSet
);
93 } catch (Exception e
) {
94 logger
.error("Error retrieving data");
98 logger
.info("All data retrieved");
101 cdmDocumentBuilder
= new CdmDocumentBuilder();
102 PrintWriter writer
= new PrintWriter(new OutputStreamWriter(new FileOutputStream(fileName
), "UTF8"), true);
103 cdmDocumentBuilder
.marshal(dataSet
, writer
);
105 // TODO: Split into one file per data set member to see whether performance improves?
107 logger
.info("XML file written");
108 logger
.info("Filename is: " + fileName
);
110 } catch (Exception e
) {
111 logger
.error("Marshalling error");
114 commitTransaction(txStatus
);
121 private void retrieveData (IExportConfigurator config
, DataSet dataSet
) {
123 JaxbExportConfigurator jaxbExpConfig
= (JaxbExportConfigurator
)config
;
124 final int MAX_ROWS
= 50000;
125 int numberOfRows
= jaxbExpConfig
.getMaxRows();
127 int agentRows
= numberOfRows
;
128 int definedTermBaseRows
= numberOfRows
;
129 int referenceBaseRows
= numberOfRows
;
130 int taxonNameBaseRows
= numberOfRows
;
131 int taxonBaseRows
= numberOfRows
;
132 int relationshipRows
= numberOfRows
;
133 int occurrencesRows
= numberOfRows
;
134 int mediaRows
= numberOfRows
;
135 int featureDataRows
= numberOfRows
;
136 int languageDataRows
= numberOfRows
;
137 int termVocabularyRows
= numberOfRows
;
138 int homotypicalGroupRows
= numberOfRows
;
140 if (jaxbExpConfig
.isDoTermVocabularies() == true) {
141 if (termVocabularyRows
== 0) { termVocabularyRows
= MAX_ROWS
; }
142 logger
.info("# TermVocabulary");
143 dataSet
.setTermVocabularies(getTermService().getAllTermVocabularies(MAX_ROWS
, 0));;
146 if (jaxbExpConfig
.isDoLanguageData() == true) {
147 if (languageDataRows
== 0) { languageDataRows
= MAX_ROWS
; }
148 logger
.info("# Representation, Language String");
149 dataSet
.setLanguageData(getTermService().getAllRepresentations(MAX_ROWS
, 0));
150 dataSet
.addLanguageData(getTermService().getAllLanguageStrings(MAX_ROWS
, 0));
153 if (jaxbExpConfig
.isDoTerms() == true) {
154 if (definedTermBaseRows
== 0) { definedTermBaseRows
= getTermService().count(DefinedTermBase
.class); }
155 logger
.info("# DefinedTermBase: " + definedTermBaseRows
);
156 dataSet
.setTerms(getTermService().getAllDefinedTerms(definedTermBaseRows
, 0));
159 if (jaxbExpConfig
.isDoAuthors() == true) {
160 if (agentRows
== 0) { agentRows
= getAgentService().count(Agent
.class); }
161 logger
.info("# Agents: " + agentRows
);
162 //logger.info(" # Team: " + appCtr.getAgentService().count(Team.class));
163 dataSet
.setAgents(getAgentService().getAllAgents(agentRows
, 0));
166 if (jaxbExpConfig
.getDoReferences() != IImportConfigurator
.DO_REFERENCES
.NONE
) {
167 if (referenceBaseRows
== 0) { referenceBaseRows
= getReferenceService().count(ReferenceBase
.class); }
168 logger
.info("# ReferenceBase: " + referenceBaseRows
);
169 dataSet
.setReferences(getReferenceService().getAllReferences(referenceBaseRows
, 0));
172 if (jaxbExpConfig
.isDoTaxonNames() == true) {
173 if (taxonNameBaseRows
== 0) { taxonNameBaseRows
= getNameService().count(TaxonNameBase
.class); }
174 logger
.info("# TaxonNameBase: " + taxonNameBaseRows
);
175 //logger.info(" # Taxon: " + getNameService().count(BotanicalName.class));
176 dataSet
.setTaxonomicNames(getNameService().getAllNames(taxonNameBaseRows
, 0));
179 if (jaxbExpConfig
.isDoHomotypicalGroups() == true) {
180 if (homotypicalGroupRows
== 0) { homotypicalGroupRows
= MAX_ROWS
; }
181 logger
.info("# Homotypical Groups");
182 dataSet
.setHomotypicalGroups(getNameService().getAllHomotypicalGroups(homotypicalGroupRows
, 0));
185 if (jaxbExpConfig
.isDoTaxa() == true) {
186 if (taxonBaseRows
== 0) { taxonBaseRows
= getTaxonService().count(TaxonBase
.class); }
187 logger
.info("# TaxonBase: " + taxonBaseRows
);
188 // dataSet.setTaxa(new ArrayList<Taxon>());
189 // dataSet.setSynonyms(new ArrayList<Synonym>());
190 List
<TaxonBase
> tb
= getTaxonService().getAllTaxonBases(taxonBaseRows
, 0);
191 for (TaxonBase taxonBase
: tb
) {
192 if (taxonBase
instanceof Taxon
) {
193 dataSet
.addTaxon((Taxon
)taxonBase
);
194 } else if (taxonBase
instanceof Synonym
) {
195 dataSet
.addSynonym((Synonym
)taxonBase
);
197 logger
.error("entry of wrong type: " + taxonBase
.toString());
203 // retrieve taxa and synonyms separately
204 // need correct count for taxa and synonyms
205 // if (taxonBaseRows == 0) { taxonBaseRows = getTaxonService().count(TaxonBase.class); }
206 // logger.info("# Synonym: " + taxonBaseRows);
207 // dataSet.setSynonyms(new ArrayList<Synonym>());
208 // dataSet.setSynonyms(getTaxonService().getAllSynonyms(taxonBaseRows, 0));
210 if (jaxbExpConfig
.isDoRelTaxa() == true) {
211 if (relationshipRows
== 0) { relationshipRows
= MAX_ROWS
; }
212 logger
.info("# Relationships");
213 List
<RelationshipBase
> relationList
= getTaxonService().getAllRelationships(relationshipRows
, 0);
214 Set
<RelationshipBase
> relationSet
= new HashSet
<RelationshipBase
>(relationList
);
215 dataSet
.setRelationships(relationSet
);
218 if (jaxbExpConfig
.isDoReferencedEntities() == true) {
219 logger
.info("# Referenced Entities");
220 dataSet
.setReferencedEntities(getNameService().getAllNomenclaturalStatus(MAX_ROWS
, 0));
221 dataSet
.addReferencedEntities(getNameService().getAllTypeDesignations(MAX_ROWS
, 0));
224 if (jaxbExpConfig
.isDoOccurrence() == true) {
225 if (occurrencesRows
== 0) { occurrencesRows
= getOccurrenceService().count(SpecimenOrObservationBase
.class); }
226 logger
.info("# SpecimenOrObservationBase: " + occurrencesRows
);
227 dataSet
.setOccurrences(getOccurrenceService().getAllSpecimenOrObservationBases(occurrencesRows
, 0));
230 if (jaxbExpConfig
.isDoMedia() == true) {
231 if (mediaRows
== 0) { mediaRows
= MAX_ROWS
; }
232 logger
.info("# Media");
233 dataSet
.setMedia(getMediaService().getAllMedia(mediaRows
, 0));
234 // dataSet.addMedia(getMediaService().getAllMediaRepresentations(mediaRows, 0));
235 // dataSet.addMedia(getMediaService().getAllMediaRepresentationParts(mediaRows, 0));
238 if (jaxbExpConfig
.isDoFeatureData() == true) {
239 if (featureDataRows
== 0) { featureDataRows
= MAX_ROWS
; }
240 logger
.info("# Feature Tree, Feature Node");
241 dataSet
.setFeatureData(getDescriptionService().getFeatureNodesAll());
242 dataSet
.addFeatureData(getDescriptionService().getFeatureTreesAll());
248 protected boolean doCheck(IExportConfigurator config
) {
249 boolean result
= true;
250 logger
.warn("No check implemented for Jaxb export");
256 protected boolean isIgnore(IExportConfigurator config
) {