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
.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
.CdmExportBase
;
22 import eu
.etaxonomy
.cdm
.io
.common
.CdmIoBase
;
23 import eu
.etaxonomy
.cdm
.io
.common
.ICdmExport
;
24 import eu
.etaxonomy
.cdm
.io
.common
.ICdmIO
;
25 import eu
.etaxonomy
.cdm
.io
.common
.IExportConfigurator
;
26 import eu
.etaxonomy
.cdm
.io
.common
.IImportConfigurator
;
27 import eu
.etaxonomy
.cdm
.model
.agent
.AgentBase
;
28 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTermBase
;
29 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
30 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
31 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceBase
;
32 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
35 * @author a.babadshanjan
40 public class JaxbExport
extends CdmExportBase
<JaxbExportConfigurator
, JaxbExportState
> implements ICdmExport
<JaxbExportConfigurator
, JaxbExportState
> {
42 private static final Logger logger
= Logger
.getLogger(JaxbExport
.class);
43 private CdmDocumentBuilder cdmDocumentBuilder
= null;
48 // public JaxbExport() {
50 // this.ioName = this.getClass().getSimpleName();
53 /** Retrieves data from a CDM DB and serializes them CDM to XML.
54 * Starts with root taxa and traverses the taxonomic tree to retrieve children taxa, synonyms and relationships.
55 * Taxa that are not part of the taxonomic tree are not found.
62 // protected boolean doInvoke(IExportConfigurator config,
63 // Map<String, MapWrapper<? extends CdmBase>> stores) {
65 protected boolean doInvoke(JaxbExportState state
) {
67 JaxbExportConfigurator jaxbExpConfig
= (JaxbExportConfigurator
)state
.getConfig();
68 String dbname
= jaxbExpConfig
.getSource().getName();
69 String fileName
= jaxbExpConfig
.getDestination();
70 logger
.info("Serializing DB " + dbname
+ " to file " + fileName
);
71 logger
.debug("DbSchemaValidation = " + jaxbExpConfig
.getDbSchemaValidation());
73 TransactionStatus txStatus
= startTransaction(true);
74 DataSet dataSet
= new DataSet();
79 logger
.info("Retrieving data from DB");
81 retrieveData(jaxbExpConfig
, dataSet
);
83 } catch (Exception e
) {
84 logger
.error("Error retrieving data");
88 logger
.info("All data retrieved");
91 cdmDocumentBuilder
= new CdmDocumentBuilder();
92 PrintWriter writer
= new PrintWriter(new OutputStreamWriter(new FileOutputStream(fileName
), "UTF8"), true);
93 cdmDocumentBuilder
.marshal(dataSet
, writer
);
95 // TODO: Split into one file per data set member to see whether performance improves?
97 logger
.info("XML file written");
98 logger
.info("Filename is: " + fileName
);
100 } catch (Exception e
) {
101 logger
.error("Marshalling error");
104 commitTransaction(txStatus
);
111 private void retrieveData (IExportConfigurator config
, DataSet dataSet
) {
113 JaxbExportConfigurator jaxbExpConfig
= (JaxbExportConfigurator
)config
;
114 final int MAX_ROWS
= 50000;
115 int numberOfRows
= jaxbExpConfig
.getMaxRows();
117 int agentRows
= numberOfRows
;
118 int definedTermBaseRows
= numberOfRows
;
119 int referenceBaseRows
= numberOfRows
;
120 int taxonNameBaseRows
= numberOfRows
;
121 int taxonBaseRows
= numberOfRows
;
122 int relationshipRows
= numberOfRows
;
123 int occurrencesRows
= numberOfRows
;
124 int mediaRows
= numberOfRows
;
125 int featureDataRows
= numberOfRows
;
126 int languageDataRows
= numberOfRows
;
127 int termVocabularyRows
= numberOfRows
;
128 int homotypicalGroupRows
= numberOfRows
;
130 if (jaxbExpConfig
.isDoTermVocabularies() == true) {
131 if (termVocabularyRows
== 0) { termVocabularyRows
= MAX_ROWS
; }
132 logger
.info("# TermVocabulary");
133 dataSet
.setTermVocabularies(getTermService().getAllTermVocabularies(MAX_ROWS
, 0));;
136 // if (jaxbExpConfig.isDoLanguageData() == true) {
137 // if (languageDataRows == 0) { languageDataRows = MAX_ROWS; }
138 // logger.info("# Representation, Language String");
139 // dataSet.setLanguageData(getTermService().getAllRepresentations(MAX_ROWS, 0));
140 // dataSet.addLanguageData(getTermService().getAllLanguageStrings(MAX_ROWS, 0));
143 if (jaxbExpConfig
.isDoTerms() == true) {
144 if (definedTermBaseRows
== 0) { definedTermBaseRows
= getTermService().count(DefinedTermBase
.class); }
145 logger
.info("# DefinedTermBase: " + definedTermBaseRows
);
146 dataSet
.setTerms(getTermService().getAllDefinedTerms(definedTermBaseRows
, 0));
149 if (jaxbExpConfig
.isDoAuthors() == true) {
150 if (agentRows
== 0) { agentRows
= getAgentService().count(AgentBase
.class); }
151 logger
.info("# Agents: " + agentRows
);
152 //logger.info(" # Team: " + appCtr.getAgentService().count(Team.class));
153 dataSet
.setAgents(getAgentService().getAllAgents(agentRows
, 0));
156 if (jaxbExpConfig
.getDoReferences() != IImportConfigurator
.DO_REFERENCES
.NONE
) {
157 if (referenceBaseRows
== 0) { referenceBaseRows
= getReferenceService().count(ReferenceBase
.class); }
158 logger
.info("# ReferenceBase: " + referenceBaseRows
);
159 dataSet
.setReferences(getReferenceService().getAllReferences(referenceBaseRows
, 0));
162 if (jaxbExpConfig
.isDoTaxonNames() == true) {
163 if (taxonNameBaseRows
== 0) { taxonNameBaseRows
= getNameService().count(TaxonNameBase
.class); }
164 logger
.info("# TaxonNameBase: " + taxonNameBaseRows
);
165 //logger.info(" # Taxon: " + getNameService().count(BotanicalName.class));
166 dataSet
.setTaxonomicNames(getNameService().getAllNames(taxonNameBaseRows
, 0));
169 if (jaxbExpConfig
.isDoHomotypicalGroups() == true) {
170 if (homotypicalGroupRows
== 0) { homotypicalGroupRows
= MAX_ROWS
; }
171 logger
.info("# Homotypical Groups");
172 dataSet
.setHomotypicalGroups(getNameService().getAllHomotypicalGroups(homotypicalGroupRows
, 0));
175 if (jaxbExpConfig
.isDoTaxa() == true) {
176 if (taxonBaseRows
== 0) { taxonBaseRows
= getTaxonService().count(TaxonBase
.class); }
177 logger
.info("# TaxonBase: " + taxonBaseRows
);
178 // dataSet.setTaxa(new ArrayList<Taxon>());
179 // dataSet.setSynonyms(new ArrayList<Synonym>());
180 List
<TaxonBase
> tb
= getTaxonService().getAllTaxonBases(taxonBaseRows
, 0);
181 for (TaxonBase taxonBase
: tb
) {
182 dataSet
.addTaxonBase(taxonBase
);
187 // retrieve taxa and synonyms separately
188 // need correct count for taxa and synonyms
189 // if (taxonBaseRows == 0) { taxonBaseRows = getTaxonService().count(TaxonBase.class); }
190 // logger.info("# Synonym: " + taxonBaseRows);
191 // dataSet.setSynonyms(new ArrayList<Synonym>());
192 // dataSet.setSynonyms(getTaxonService().getAllSynonyms(taxonBaseRows, 0));
194 // if (jaxbExpConfig.isDoRelTaxa() == true) {
195 // if (relationshipRows == 0) { relationshipRows = MAX_ROWS; }
196 // logger.info("# Relationships");
197 // List<RelationshipBase> relationList = getTaxonService().getAllRelationships(relationshipRows, 0);
198 // Set<RelationshipBase> relationSet = new HashSet<RelationshipBase>(relationList);
199 // dataSet.setRelationships(relationSet);
202 if (jaxbExpConfig
.isDoTypeDesignations() == true) {
203 logger
.info("# TypeDesignations");
204 dataSet
.addTypeDesignations(getNameService().getAllTypeDesignations(MAX_ROWS
, 0));
207 if (jaxbExpConfig
.isDoOccurrence() == true) {
208 if (occurrencesRows
== 0) { occurrencesRows
= getOccurrenceService().count(SpecimenOrObservationBase
.class); }
209 logger
.info("# SpecimenOrObservationBase: " + occurrencesRows
);
210 dataSet
.setOccurrences(getOccurrenceService().getAllSpecimenOrObservationBases(occurrencesRows
, 0));
213 if (jaxbExpConfig
.isDoMedia() == true) {
214 if (mediaRows
== 0) { mediaRows
= MAX_ROWS
; }
215 logger
.info("# Media");
216 dataSet
.setMedia(getMediaService().getAllMedia(mediaRows
, 0));
217 // dataSet.addMedia(getMediaService().getAllMediaRepresentations(mediaRows, 0));
218 // dataSet.addMedia(getMediaService().getAllMediaRepresentationParts(mediaRows, 0));
221 if (jaxbExpConfig
.isDoFeatureData() == true) {
222 if (featureDataRows
== 0) { featureDataRows
= MAX_ROWS
; }
223 logger
.info("# Feature Tree, Feature Node");
224 dataSet
.setFeatureTrees(getDescriptionService().getFeatureTreesAll(null));
230 protected boolean doCheck(JaxbExportState state
) {
231 boolean result
= true;
232 logger
.warn("No check implemented for Jaxb export");
238 protected boolean isIgnore(JaxbExportState state
) {