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
.ArrayList
;
16 import java
.util
.List
;
18 import javax
.xml
.transform
.sax
.SAXResult
;
20 import org
.apache
.log4j
.Logger
;
21 import org
.springframework
.stereotype
.Component
;
22 import org
.springframework
.transaction
.TransactionStatus
;
23 import org
.xml
.sax
.ContentHandler
;
24 import org
.xml
.sax
.Locator
;
25 import org
.xml
.sax
.helpers
.DefaultHandler
;
27 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
28 import eu
.etaxonomy
.cdm
.io
.common
.CdmExportBase
;
29 import eu
.etaxonomy
.cdm
.io
.common
.CdmIoBase
;
30 import eu
.etaxonomy
.cdm
.io
.common
.ICdmExport
;
31 import eu
.etaxonomy
.cdm
.io
.common
.ICdmIO
;
32 import eu
.etaxonomy
.cdm
.io
.common
.IExportConfigurator
;
33 import eu
.etaxonomy
.cdm
.io
.common
.IImportConfigurator
;
34 import eu
.etaxonomy
.cdm
.model
.agent
.AgentBase
;
35 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTermBase
;
36 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
37 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
38 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceBase
;
39 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
40 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonomicTree
;
43 * @author a.babadshanjan
48 public class JaxbExport
extends CdmExportBase
<JaxbExportConfigurator
, JaxbExportState
> implements ICdmExport
<JaxbExportConfigurator
, JaxbExportState
> {
50 private static final Logger logger
= Logger
.getLogger(JaxbExport
.class);
51 private CdmDocumentBuilder cdmDocumentBuilder
= null;
56 // public JaxbExport() {
58 // this.ioName = this.getClass().getSimpleName();
61 /** Retrieves data from a CDM DB and serializes them CDM to XML.
62 * Starts with root taxa and traverses the taxonomic tree to retrieve children taxa, synonyms and relationships.
63 * Taxa that are not part of the taxonomic tree are not found.
70 // protected boolean doInvoke(IExportConfigurator config,
71 // Map<String, MapWrapper<? extends CdmBase>> stores) {
73 protected boolean doInvoke(JaxbExportState state
) {
75 JaxbExportConfigurator jaxbExpConfig
= (JaxbExportConfigurator
)state
.getConfig();
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();
87 logger
.info("Retrieving data from DB");
89 retrieveData(jaxbExpConfig
, dataSet
);
91 } catch (Exception e
) {
92 logger
.error("Error retrieving data");
96 logger
.info("All data retrieved");
99 cdmDocumentBuilder
= new CdmDocumentBuilder();
100 PrintWriter writer
= new PrintWriter(new OutputStreamWriter(new FileOutputStream(fileName
), "UTF8"), true);
102 /*SAXResult result = new SAXResult();
103 ContentHandler handler = new DefaultHandler();
105 result.setHandler(handler);*/
107 //cdmDocumentBuilder.marshal(dataSet, result);
108 cdmDocumentBuilder
.marshal(dataSet
, writer
);
110 // TODO: Split into one file per data set member to see whether performance improves?
112 logger
.info("XML file written");
113 logger
.info("Filename is: " + fileName
);
115 } catch (Exception e
) {
116 logger
.error("Marshalling error");
119 commitTransaction(txStatus
);
126 private void retrieveData (IExportConfigurator config
, DataSet dataSet
) {
128 JaxbExportConfigurator jaxbExpConfig
= (JaxbExportConfigurator
)config
;
129 final int MAX_ROWS
= 50000;
130 int numberOfRows
= jaxbExpConfig
.getMaxRows();
132 int agentRows
= numberOfRows
;
133 int definedTermBaseRows
= numberOfRows
;
134 int referenceBaseRows
= numberOfRows
;
135 int taxonNameBaseRows
= numberOfRows
;
136 int taxonBaseRows
= numberOfRows
;
137 int relationshipRows
= numberOfRows
;
138 int occurrencesRows
= numberOfRows
;
139 int mediaRows
= numberOfRows
;
140 int featureDataRows
= numberOfRows
;
141 int taxonomicTreeDataRows
= numberOfRows
;
142 int languageDataRows
= numberOfRows
;
143 int termVocabularyRows
= numberOfRows
;
144 int homotypicalGroupRows
= numberOfRows
;
145 int UserRows
= numberOfRows
;
147 if (jaxbExpConfig
.isDoTermVocabularies() == true) {
148 if (termVocabularyRows
== 0) { termVocabularyRows
= MAX_ROWS
; }
149 logger
.info("# TermVocabulary");
150 dataSet
.setTermVocabularies(getTermService().listTermVocabularies(termVocabularyRows
, 0, null, null));
153 if (jaxbExpConfig
.isDoUsers() == true) {
155 if (UserRows
== 0) { UserRows
= MAX_ROWS
; }
156 logger
.info("# User");
157 dataSet
.setUsers(getUserService().list(UserRows
, 0));
160 // if (jaxbExpConfig.isDoLanguageData() == true) {
161 // if (languageDataRows == 0) { languageDataRows = MAX_ROWS; }
162 // logger.info("# Representation, Language String");
163 // dataSet.setLanguageData(getTermService().getAllRepresentations(MAX_ROWS, 0));
165 dataSet
.setLanguageStrings(getTermService().getAllLanguageStrings(MAX_ROWS
, 0));
168 if (jaxbExpConfig
.isDoTerms() == true) {
169 if (definedTermBaseRows
== 0) { definedTermBaseRows
= getTermService().count(DefinedTermBase
.class); }
170 logger
.info("# DefinedTermBase: " + definedTermBaseRows
);
171 dataSet
.setTerms(getTermService().getAllDefinedTerms(definedTermBaseRows
, 0));
174 if (jaxbExpConfig
.isDoAuthors() == true) {
175 if (agentRows
== 0) { agentRows
= getAgentService().count(AgentBase
.class); }
176 logger
.info("# Agents: " + agentRows
);
177 //logger.info(" # Team: " + appCtr.getAgentService().count(Team.class));
178 dataSet
.setAgents(getAgentService().list(agentRows
,0));
181 if (jaxbExpConfig
.getDoReferences() != IImportConfigurator
.DO_REFERENCES
.NONE
) {
182 if (referenceBaseRows
== 0) { referenceBaseRows
= getReferenceService().count(ReferenceBase
.class); }
183 logger
.info("# ReferenceBase: " + referenceBaseRows
);
184 dataSet
.setReferences(getReferenceService().list(referenceBaseRows
, 0));
187 if (jaxbExpConfig
.isDoHomotypicalGroups() == true) {
188 if (homotypicalGroupRows
== 0) { homotypicalGroupRows
= MAX_ROWS
; }
189 logger
.info("# Homotypical Groups");
190 dataSet
.setHomotypicalGroups(getNameService().getAllHomotypicalGroups(homotypicalGroupRows
, 0));
193 if (jaxbExpConfig
.isDoTaxonNames() == true) {
194 if (taxonNameBaseRows
== 0) { taxonNameBaseRows
= getNameService().count(TaxonNameBase
.class); }
195 logger
.info("# TaxonNameBase: " + taxonNameBaseRows
);
196 //logger.info(" # Taxon: " + getNameService().count(BotanicalName.class));
197 dataSet
.setTaxonomicNames(getNameService().getAllNames(taxonNameBaseRows
, 0));
202 if (jaxbExpConfig
.isDoTaxa() == true) {
203 if (taxonBaseRows
== 0) { taxonBaseRows
= getTaxonService().count(TaxonBase
.class); }
204 logger
.info("# TaxonBase: " + taxonBaseRows
);
205 // dataSet.setTaxa(new ArrayList<Taxon>());
206 // dataSet.setSynonyms(new ArrayList<Synonym>());
207 List
<TaxonBase
> tb
= getTaxonService().getAllTaxonBases(taxonBaseRows
, 0);
208 for (TaxonBase taxonBase
: tb
) {
209 dataSet
.addTaxonBase((TaxonBase
)HibernateProxyHelper
.deproxy(taxonBase
));
214 // retrieve taxa and synonyms separately
215 // need correct count for taxa and synonyms
216 // if (taxonBaseRows == 0) { taxonBaseRows = getTaxonService().count(TaxonBase.class); }
217 // logger.info("# Synonym: " + taxonBaseRows);
218 // dataSet.setSynonyms(new ArrayList<Synonym>());
219 // dataSet.setSynonyms(getTaxonService().getAllSynonyms(taxonBaseRows, 0));
221 // if (jaxbExpConfig.isDoRelTaxa() == true) {
222 // if (relationshipRows == 0) { relationshipRows = MAX_ROWS; }
223 // logger.info("# Relationships");
224 // List<RelationshipBase> relationList = getTaxonService().getAllRelationships(relationshipRows, 0);
225 // Set<RelationshipBase> relationSet = new HashSet<RelationshipBase>(relationList);
226 // dataSet.setRelationships(relationSet);
228 if (jaxbExpConfig
.isDoOccurrence() == true) {
229 if (occurrencesRows
== 0) { occurrencesRows
= getOccurrenceService().count(SpecimenOrObservationBase
.class); }
230 logger
.info("# SpecimenOrObservationBase: " + occurrencesRows
);
231 List
<SpecimenOrObservationBase
> occurrenceList
= getOccurrenceService().getAllSpecimenOrObservationBases(occurrencesRows
, 0);
232 /*List<SpecimenOrObservationBase> noProxyList = new ArrayList<SpecimenOrObservationBase>();
233 for (SpecimenOrObservationBase specimen : occurrenceList){
234 specimen = (SpecimenOrObservationBase)HibernateProxyHelper.deproxy(specimen);
235 noProxyList.add(specimen);
237 dataSet
.setOccurrences(occurrenceList
);
240 if (jaxbExpConfig
.isDoTypeDesignations() == true) {
241 logger
.info("# TypeDesignations");
242 dataSet
.addTypeDesignations(getNameService().getAllTypeDesignations(MAX_ROWS
, 0));
247 if (jaxbExpConfig
.isDoMedia() == true) {
248 if (mediaRows
== 0) { mediaRows
= MAX_ROWS
; }
249 logger
.info("# Media");
250 dataSet
.setMedia(getMediaService().getAllMedia(mediaRows
, 0));
251 // dataSet.addMedia(getMediaService().getAllMediaRepresentations(mediaRows, 0));
252 // dataSet.addMedia(getMediaService().getAllMediaRepresentationParts(mediaRows, 0));
255 if (jaxbExpConfig
.isDoFeatureData() == true) {
256 if (featureDataRows
== 0) { featureDataRows
= MAX_ROWS
; }
257 logger
.info("# Feature Tree, Feature Node");
258 dataSet
.setFeatureTrees(getDescriptionService().getFeatureTreesAll(null));
260 if (jaxbExpConfig
.isDoTaxonomicTreeData() == true) {
261 if (taxonomicTreeDataRows
== 0) { taxonomicTreeDataRows
= MAX_ROWS
; }
262 logger
.info("# Taxonomic Tree");
263 List
<TaxonomicTree
> taxTrees
= new ArrayList
<TaxonomicTree
>();
264 taxTrees
= getTaxonTreeService().list(taxonomicTreeDataRows
, 0, null, null).getRecords();
265 List
<TaxonomicTree
> taxTreesdeproxy
= new ArrayList
<TaxonomicTree
>();
266 for (TaxonomicTree taxTree
: taxTrees
){
267 HibernateProxyHelper
.deproxy(taxTree
);
268 taxTreesdeproxy
.add(taxTree
);
270 dataSet
.setTaxonomicTrees(taxTreesdeproxy
);
273 dataSet
.setLanguageStrings(null);
278 protected boolean doCheck(JaxbExportState state
) {
279 boolean result
= true;
280 logger
.warn("No check implemented for Jaxb export");
286 protected boolean isIgnore(JaxbExportState state
) {