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 org
.apache
.log4j
.Logger
;
19 import org
.springframework
.stereotype
.Component
;
20 import org
.springframework
.transaction
.TransactionStatus
;
22 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
23 import eu
.etaxonomy
.cdm
.io
.common
.CdmExportBase
;
24 import eu
.etaxonomy
.cdm
.io
.common
.ICdmExport
;
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
.common
.User
;
30 import eu
.etaxonomy
.cdm
.model
.description
.FeatureNode
;
31 import eu
.etaxonomy
.cdm
.model
.description
.FeatureTree
;
32 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
33 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
34 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceBase
;
35 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
36 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
37 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonomicTree
;
40 * @author a.babadshanjan
45 public class JaxbExport
extends CdmExportBase
<JaxbExportConfigurator
, JaxbExportState
> implements ICdmExport
<JaxbExportConfigurator
, JaxbExportState
> {
47 private static final Logger logger
= Logger
.getLogger(JaxbExport
.class);
48 private CdmDocumentBuilder cdmDocumentBuilder
= null;
53 // public JaxbExport() {
55 // this.ioName = this.getClass().getSimpleName();
58 /** Retrieves data from a CDM DB and serializes them CDM to XML.
59 * Starts with root taxa and traverses the taxonomic tree to retrieve children taxa, synonyms and relationships.
60 * Taxa that are not part of the taxonomic tree are not found.
67 // protected boolean doInvoke(IExportConfigurator config,
68 // Map<String, MapWrapper<? extends CdmBase>> stores) {
70 protected boolean doInvoke(JaxbExportState state
) {
72 JaxbExportConfigurator jaxbExpConfig
= (JaxbExportConfigurator
)state
.getConfig();
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();
84 logger
.info("Retrieving data from DB");
86 retrieveData(jaxbExpConfig
, dataSet
);
88 } catch (Exception e
) {
89 logger
.error("Error retrieving data");
93 logger
.info("All data retrieved");
96 cdmDocumentBuilder
= new CdmDocumentBuilder();
97 PrintWriter writer
= new PrintWriter(new OutputStreamWriter(new FileOutputStream(fileName
), "UTF8"), true);
99 /*SAXResult result = new SAXResult();
100 ContentHandler handler = new DefaultHandler();
102 result.setHandler(handler);*/
104 //cdmDocumentBuilder.marshal(dataSet, result);
105 cdmDocumentBuilder
.marshal(dataSet
, writer
);
107 // TODO: Split into one file per data set member to see whether performance improves?
109 logger
.info("XML file written");
110 logger
.info("Filename is: " + fileName
);
112 } catch (Exception e
) {
113 logger
.error("Marshalling error");
116 commitTransaction(txStatus
);
123 private void retrieveData (IExportConfigurator config
, DataSet dataSet
) {
125 JaxbExportConfigurator jaxbExpConfig
= (JaxbExportConfigurator
)config
;
126 final int MAX_ROWS
= 50000;
127 int numberOfRows
= jaxbExpConfig
.getMaxRows();
129 int agentRows
= numberOfRows
;
130 int definedTermBaseRows
= numberOfRows
;
131 int referenceBaseRows
= numberOfRows
;
132 int taxonNameBaseRows
= numberOfRows
;
133 int taxonBaseRows
= numberOfRows
;
134 int taxonNodeRows
= 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
;
143 int UserRows
= numberOfRows
;
145 if (jaxbExpConfig
.isDoUsers() == true) {
147 if (UserRows
== 0) { UserRows
= MAX_ROWS
; }
148 logger
.info("# User");
149 List
<User
> users
= getUserService().list(null, UserRows
, 0, null, null);
152 for (User user
: users
){
153 dataSet
.addUser( (User
)HibernateProxyHelper
.deproxy(user
));
157 if (jaxbExpConfig
.isDoTermVocabularies() == true) {
158 if (termVocabularyRows
== 0) { termVocabularyRows
= MAX_ROWS
; }
159 logger
.info("# TermVocabulary");
160 dataSet
.setTermVocabularies((List
)getVocabularyService().list(null,termVocabularyRows
, 0, null, null));
165 // if (jaxbExpConfig.isDoLanguageData() == true) {
166 // if (languageDataRows == 0) { languageDataRows = MAX_ROWS; }
167 // logger.info("# Representation, Language String");
168 // dataSet.setLanguageData(getTermService().getAllRepresentations(MAX_ROWS, 0));
170 dataSet
.setLanguageStrings(getTermService().getAllLanguageStrings(MAX_ROWS
, 0));
173 if (jaxbExpConfig
.isDoTerms() == true) {
174 if (definedTermBaseRows
== 0) { definedTermBaseRows
= getTermService().count(DefinedTermBase
.class); }
175 logger
.info("# DefinedTermBase: " + definedTermBaseRows
);
176 dataSet
.setTerms(getTermService().list(null,definedTermBaseRows
, 0,null,null));
179 if (jaxbExpConfig
.isDoAuthors() == true) {
180 if (agentRows
== 0) { agentRows
= getAgentService().count(AgentBase
.class); }
181 logger
.info("# Agents: " + agentRows
);
182 //logger.info(" # Team: " + appCtr.getAgentService().count(Team.class));
183 dataSet
.setAgents(getAgentService().list(null,agentRows
, 0,null,null));
188 if (jaxbExpConfig
.getDoReferences() != IImportConfigurator
.DO_REFERENCES
.NONE
) {
189 if (referenceBaseRows
== 0) { referenceBaseRows
= getReferenceService().count(ReferenceBase
.class); }
190 logger
.info("# ReferenceBase: " + referenceBaseRows
);
191 dataSet
.setReferences(getReferenceService().list(null,referenceBaseRows
, 0,null,null));
194 if (jaxbExpConfig
.isDoHomotypicalGroups() == true) {
195 if (homotypicalGroupRows
== 0) { homotypicalGroupRows
= MAX_ROWS
; }
196 logger
.info("# Homotypical Groups");
197 dataSet
.setHomotypicalGroups(getNameService().getAllHomotypicalGroups(homotypicalGroupRows
, 0));
200 if (jaxbExpConfig
.isDoTaxonNames() == true) {
201 if (taxonNameBaseRows
== 0) { taxonNameBaseRows
= getNameService().count(TaxonNameBase
.class); }
202 logger
.info("# TaxonNameBase: " + taxonNameBaseRows
);
203 //logger.info(" # Taxon: " + getNameService().count(BotanicalName.class));
204 dataSet
.setTaxonomicNames(getNameService().list(null,taxonNameBaseRows
, 0,null,null));
209 if (jaxbExpConfig
.isDoTaxa() == true) {
210 if (taxonBaseRows
== 0) { taxonBaseRows
= getTaxonService().count(TaxonBase
.class); }
211 logger
.info("# TaxonBase: " + taxonBaseRows
);
212 // dataSet.setTaxa(new ArrayList<Taxon>());
213 // dataSet.setSynonyms(new ArrayList<Synonym>());
214 List
<TaxonBase
> tb
= getTaxonService().list(null,taxonBaseRows
, 0,null,null);
215 for (TaxonBase taxonBase
: tb
) {
216 dataSet
.addTaxonBase((TaxonBase
)HibernateProxyHelper
.deproxy(taxonBase
));
221 // retrieve taxa and synonyms separately
222 // need correct count for taxa and synonyms
223 // if (taxonBaseRows == 0) { taxonBaseRows = getTaxonService().count(TaxonBase.class); }
224 // logger.info("# Synonym: " + taxonBaseRows);
225 // dataSet.setSynonyms(new ArrayList<Synonym>());
226 // dataSet.setSynonyms(getTaxonService().getAllSynonyms(taxonBaseRows, 0));
228 // if (jaxbExpConfig.isDoRelTaxa() == true) {
229 // if (relationshipRows == 0) { relationshipRows = MAX_ROWS; }
230 // logger.info("# Relationships");
231 // List<RelationshipBase> relationList = getTaxonService().getAllRelationships(relationshipRows, 0);
232 // Set<RelationshipBase> relationSet = new HashSet<RelationshipBase>(relationList);
233 // dataSet.setRelationships(relationSet);
235 if (jaxbExpConfig
.isDoOccurrence() == true) {
236 if (occurrencesRows
== 0) { occurrencesRows
= getOccurrenceService().count(SpecimenOrObservationBase
.class); }
237 logger
.info("# SpecimenOrObservationBase: " + occurrencesRows
);
238 List
<SpecimenOrObservationBase
> occurrenceList
= getOccurrenceService().list(null,occurrencesRows
, 0,null,null);
239 /*List<SpecimenOrObservationBase> noProxyList = new ArrayList<SpecimenOrObservationBase>();
240 for (SpecimenOrObservationBase specimen : occurrenceList){
241 specimen = (SpecimenOrObservationBase)HibernateProxyHelper.deproxy(specimen);
242 noProxyList.add(specimen);
244 dataSet
.setOccurrences(occurrenceList
);
247 if (jaxbExpConfig
.isDoTypeDesignations() == true) {
248 logger
.info("# TypeDesignations");
249 dataSet
.addTypeDesignations(getNameService().getAllTypeDesignations(MAX_ROWS
, 0));
254 if (jaxbExpConfig
.isDoMedia() == true) {
255 if (mediaRows
== 0) { mediaRows
= MAX_ROWS
; }
256 logger
.info("# Media");
257 dataSet
.setMedia(getMediaService().list(null,mediaRows
, 0,null,null));
258 // dataSet.addMedia(getMediaService().getAllMediaRepresentations(mediaRows, 0));
259 // dataSet.addMedia(getMediaService().getAllMediaRepresentationParts(mediaRows, 0));
262 if (jaxbExpConfig
.isDoFeatureData() == true) {
263 if (featureDataRows
== 0) { featureDataRows
= MAX_ROWS
; }
264 logger
.info("# Feature Tree, Feature Node");
265 List
<FeatureTree
> featureTrees
= new ArrayList
<FeatureTree
>();
266 featureTrees
= getFeatureTreeService().list(null,featureDataRows
, 0, null, null);
267 List
<FeatureTree
> taxTreesdeproxy
= new ArrayList
<FeatureTree
>();
268 for (FeatureTree featureTree
: featureTrees
){
269 HibernateProxyHelper
.deproxy(featureTree
);
270 taxTreesdeproxy
.add(featureTree
);
273 List
<FeatureNode
> featureNodes
= new ArrayList
<FeatureNode
>();
274 featureNodes
= getFeatureTreeService().getFeatureNodesAll();
275 List
<FeatureNode
> taxNodesdeproxy
= new ArrayList
<FeatureNode
>();
276 for (FeatureNode featureNode
: featureNodes
){
277 HibernateProxyHelper
.deproxy(featureNode
);
278 taxNodesdeproxy
.add(featureNode
);
281 dataSet
.setFeatureNodes(taxNodesdeproxy
);
283 dataSet
.setFeatureTrees(getFeatureTreeService().list(null,null,null,null,null));
285 if (jaxbExpConfig
.isDoTaxonomicTreeData() == true) {
286 if (taxonomicTreeDataRows
== 0) { taxonomicTreeDataRows
= MAX_ROWS
; }
287 logger
.info("# Taxonomic Tree");
290 List
<TaxonomicTree
> taxTrees
= new ArrayList
<TaxonomicTree
>();
291 taxTrees
= getTaxonTreeService().list(null,taxonomicTreeDataRows
, 0, null, null);
293 List
<TaxonomicTree
> taxTreesdeproxy
= new ArrayList
<TaxonomicTree
>();
294 for (TaxonomicTree taxTree
: taxTrees
){
295 HibernateProxyHelper
.deproxy(taxTree
);
296 taxTreesdeproxy
.add(taxTree
);
298 List
<TaxonNode
> taxNodes
= new ArrayList
<TaxonNode
>();
299 taxNodes
= getTaxonTreeService().getAllNodes();
300 List
<TaxonNode
> taxNodesdeproxy
= new ArrayList
<TaxonNode
>();
301 for (TaxonNode taxNode
: taxNodes
){
302 HibernateProxyHelper
.deproxy(taxNode
);
303 taxNodesdeproxy
.add(taxNode
);
306 dataSet
.setTaxonNodes(taxNodesdeproxy
);
307 dataSet
.setTaxonomicTrees(taxTreesdeproxy
);
310 dataSet
.setLanguageStrings(null);
315 protected boolean doCheck(JaxbExportState state
) {
316 boolean result
= true;
317 logger
.warn("No check implemented for Jaxb export");
323 protected boolean isIgnore(JaxbExportState state
) {