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 javax
.xml
.transform
.sax
.SAXResult
;
19 import org
.apache
.log4j
.Logger
;
20 import org
.springframework
.stereotype
.Component
;
21 import org
.springframework
.transaction
.TransactionStatus
;
22 import org
.xml
.sax
.ContentHandler
;
23 import org
.xml
.sax
.Locator
;
24 import org
.xml
.sax
.helpers
.DefaultHandler
;
26 import eu
.etaxonomy
.cdm
.io
.common
.CdmExportBase
;
27 import eu
.etaxonomy
.cdm
.io
.common
.CdmIoBase
;
28 import eu
.etaxonomy
.cdm
.io
.common
.ICdmExport
;
29 import eu
.etaxonomy
.cdm
.io
.common
.ICdmIO
;
30 import eu
.etaxonomy
.cdm
.io
.common
.IExportConfigurator
;
31 import eu
.etaxonomy
.cdm
.io
.common
.IImportConfigurator
;
32 import eu
.etaxonomy
.cdm
.model
.agent
.AgentBase
;
33 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTermBase
;
34 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
35 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
36 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceBase
;
37 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
38 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonomicTree
;
41 * @author a.babadshanjan
46 public class JaxbExport
extends CdmExportBase
<JaxbExportConfigurator
, JaxbExportState
> implements ICdmExport
<JaxbExportConfigurator
, JaxbExportState
> {
48 private static final Logger logger
= Logger
.getLogger(JaxbExport
.class);
49 private CdmDocumentBuilder cdmDocumentBuilder
= null;
54 // public JaxbExport() {
56 // this.ioName = this.getClass().getSimpleName();
59 /** Retrieves data from a CDM DB and serializes them CDM to XML.
60 * Starts with root taxa and traverses the taxonomic tree to retrieve children taxa, synonyms and relationships.
61 * Taxa that are not part of the taxonomic tree are not found.
68 // protected boolean doInvoke(IExportConfigurator config,
69 // Map<String, MapWrapper<? extends CdmBase>> stores) {
71 protected boolean doInvoke(JaxbExportState state
) {
73 JaxbExportConfigurator jaxbExpConfig
= (JaxbExportConfigurator
)state
.getConfig();
74 String dbname
= jaxbExpConfig
.getSource().getName();
75 String fileName
= jaxbExpConfig
.getDestination();
76 logger
.info("Serializing DB " + dbname
+ " to file " + fileName
);
77 logger
.debug("DbSchemaValidation = " + jaxbExpConfig
.getDbSchemaValidation());
79 TransactionStatus txStatus
= startTransaction(true);
80 DataSet dataSet
= new DataSet();
85 logger
.info("Retrieving data from DB");
87 retrieveData(jaxbExpConfig
, dataSet
);
89 } catch (Exception e
) {
90 logger
.error("Error retrieving data");
94 logger
.info("All data retrieved");
97 cdmDocumentBuilder
= new CdmDocumentBuilder();
98 PrintWriter writer
= new PrintWriter(new OutputStreamWriter(new FileOutputStream(fileName
), "UTF8"), true);
100 /*SAXResult result = new SAXResult();
101 ContentHandler handler = new DefaultHandler();
103 result.setHandler(handler);*/
105 //cdmDocumentBuilder.marshal(dataSet, result);
106 cdmDocumentBuilder
.marshal(dataSet
, writer
);
108 // TODO: Split into one file per data set member to see whether performance improves?
110 logger
.info("XML file written");
111 logger
.info("Filename is: " + fileName
);
113 } catch (Exception e
) {
114 logger
.error("Marshalling error");
117 commitTransaction(txStatus
);
124 private void retrieveData (IExportConfigurator config
, DataSet dataSet
) {
126 JaxbExportConfigurator jaxbExpConfig
= (JaxbExportConfigurator
)config
;
127 final int MAX_ROWS
= 50000;
128 int numberOfRows
= jaxbExpConfig
.getMaxRows();
130 int agentRows
= numberOfRows
;
131 int definedTermBaseRows
= numberOfRows
;
132 int referenceBaseRows
= numberOfRows
;
133 int taxonNameBaseRows
= numberOfRows
;
134 int taxonBaseRows
= numberOfRows
;
135 int relationshipRows
= numberOfRows
;
136 int occurrencesRows
= numberOfRows
;
137 int mediaRows
= numberOfRows
;
138 int featureDataRows
= numberOfRows
;
139 int taxonomicTreeDataRows
= numberOfRows
;
140 int languageDataRows
= numberOfRows
;
141 int termVocabularyRows
= numberOfRows
;
142 int homotypicalGroupRows
= numberOfRows
;
144 if (jaxbExpConfig
.isDoTermVocabularies() == true) {
145 if (termVocabularyRows
== 0) { termVocabularyRows
= MAX_ROWS
; }
146 logger
.info("# TermVocabulary");
147 dataSet
.setTermVocabularies(getTermService().listTermVocabularies(termVocabularyRows
, 0, null, null));
150 // if (jaxbExpConfig.isDoLanguageData() == true) {
151 // if (languageDataRows == 0) { languageDataRows = MAX_ROWS; }
152 // logger.info("# Representation, Language String");
153 // dataSet.setLanguageData(getTermService().getAllRepresentations(MAX_ROWS, 0));
154 // dataSet.addLanguageData(getTermService().getAllLanguageStrings(MAX_ROWS, 0));
157 if (jaxbExpConfig
.isDoTerms() == true) {
158 if (definedTermBaseRows
== 0) { definedTermBaseRows
= getTermService().count(DefinedTermBase
.class); }
159 logger
.info("# DefinedTermBase: " + definedTermBaseRows
);
160 dataSet
.setTerms(getTermService().getAllDefinedTerms(definedTermBaseRows
, 0));
163 if (jaxbExpConfig
.isDoAuthors() == true) {
164 if (agentRows
== 0) { agentRows
= getAgentService().count(AgentBase
.class); }
165 logger
.info("# Agents: " + agentRows
);
166 //logger.info(" # Team: " + appCtr.getAgentService().count(Team.class));
167 dataSet
.setAgents(getAgentService().getAllAgents(agentRows
, 0));
170 if (jaxbExpConfig
.getDoReferences() != IImportConfigurator
.DO_REFERENCES
.NONE
) {
171 if (referenceBaseRows
== 0) { referenceBaseRows
= getReferenceService().count(ReferenceBase
.class); }
172 logger
.info("# ReferenceBase: " + referenceBaseRows
);
173 dataSet
.setReferences(getReferenceService().getAllReferences(referenceBaseRows
, 0));
176 if (jaxbExpConfig
.isDoTaxonNames() == true) {
177 if (taxonNameBaseRows
== 0) { taxonNameBaseRows
= getNameService().count(TaxonNameBase
.class); }
178 logger
.info("# TaxonNameBase: " + taxonNameBaseRows
);
179 //logger.info(" # Taxon: " + getNameService().count(BotanicalName.class));
180 dataSet
.setTaxonomicNames(getNameService().getAllNames(taxonNameBaseRows
, 0));
183 if (jaxbExpConfig
.isDoHomotypicalGroups() == true) {
184 if (homotypicalGroupRows
== 0) { homotypicalGroupRows
= MAX_ROWS
; }
185 logger
.info("# Homotypical Groups");
186 dataSet
.setHomotypicalGroups(getNameService().getAllHomotypicalGroups(homotypicalGroupRows
, 0));
189 if (jaxbExpConfig
.isDoTaxa() == true) {
190 if (taxonBaseRows
== 0) { taxonBaseRows
= getTaxonService().count(TaxonBase
.class); }
191 logger
.info("# TaxonBase: " + taxonBaseRows
);
192 // dataSet.setTaxa(new ArrayList<Taxon>());
193 // dataSet.setSynonyms(new ArrayList<Synonym>());
194 List
<TaxonBase
> tb
= getTaxonService().getAllTaxonBases(taxonBaseRows
, 0);
195 for (TaxonBase taxonBase
: tb
) {
196 dataSet
.addTaxonBase(taxonBase
);
201 // retrieve taxa and synonyms separately
202 // need correct count for taxa and synonyms
203 // if (taxonBaseRows == 0) { taxonBaseRows = getTaxonService().count(TaxonBase.class); }
204 // logger.info("# Synonym: " + taxonBaseRows);
205 // dataSet.setSynonyms(new ArrayList<Synonym>());
206 // dataSet.setSynonyms(getTaxonService().getAllSynonyms(taxonBaseRows, 0));
208 // if (jaxbExpConfig.isDoRelTaxa() == true) {
209 // if (relationshipRows == 0) { relationshipRows = MAX_ROWS; }
210 // logger.info("# Relationships");
211 // List<RelationshipBase> relationList = getTaxonService().getAllRelationships(relationshipRows, 0);
212 // Set<RelationshipBase> relationSet = new HashSet<RelationshipBase>(relationList);
213 // dataSet.setRelationships(relationSet);
216 if (jaxbExpConfig
.isDoTypeDesignations() == true) {
217 logger
.info("# TypeDesignations");
218 dataSet
.addTypeDesignations(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
.setFeatureTrees(getDescriptionService().getFeatureTreesAll(null));
240 if (jaxbExpConfig
.isDoTaxonomicTreeData() == true) {
241 if (taxonomicTreeDataRows
== 0) { taxonomicTreeDataRows
= MAX_ROWS
; }
242 logger
.info("# Taxonomic Tree");
243 dataSet
.setTaxonomicTrees(getTaxonService().listTaxonomicTrees(taxonomicTreeDataRows
, 0, null, null));
249 protected boolean doCheck(JaxbExportState state
) {
250 boolean result
= true;
251 logger
.warn("No check implemented for Jaxb export");
257 protected boolean isIgnore(JaxbExportState state
) {