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
.ByteArrayOutputStream
;
14 import java
.io
.FileNotFoundException
;
15 import java
.io
.FileOutputStream
;
16 import java
.io
.OutputStreamWriter
;
17 import java
.io
.PrintWriter
;
18 import java
.io
.UnsupportedEncodingException
;
20 import java
.util
.ArrayList
;
21 import java
.util
.List
;
23 import javax
.xml
.transform
.stream
.StreamResult
;
25 import org
.apache
.log4j
.Logger
;
26 import org
.springframework
.stereotype
.Component
;
27 import org
.springframework
.transaction
.TransactionStatus
;
29 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
30 import eu
.etaxonomy
.cdm
.io
.common
.CdmExportBase
;
31 import eu
.etaxonomy
.cdm
.io
.common
.ICdmExport
;
32 import eu
.etaxonomy
.cdm
.io
.common
.IExportConfigurator
;
33 import eu
.etaxonomy
.cdm
.io
.common
.mapping
.out
.IExportTransformer
;
34 import eu
.etaxonomy
.cdm
.model
.agent
.AgentBase
;
35 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTermBase
;
36 import eu
.etaxonomy
.cdm
.model
.common
.User
;
37 import eu
.etaxonomy
.cdm
.model
.description
.FeatureTree
;
38 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
39 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
40 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
41 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
42 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
43 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
46 * @author a.babadshanjan
51 public class JaxbExport
extends CdmExportBase
<JaxbExportConfigurator
, JaxbExportState
, IExportTransformer
> implements ICdmExport
<JaxbExportConfigurator
, JaxbExportState
> {
53 private static final Logger logger
= Logger
.getLogger(JaxbExport
.class);
55 private DataSet dataSet
;
61 // public JaxbExport() {
63 // this.ioName = this.getClass().getSimpleName();
66 /** Retrieves data from a CDM DB and serializes them CDM to XML.
67 * Starts with root taxa and traverses the classification to retrieve children taxa, synonyms and relationships.
68 * Taxa that are not part of the classification are not found.
75 // protected boolean doInvoke(IExportConfigurator config,
76 // Map<String, MapWrapper<? extends CdmBase>> stores) {
78 protected void doInvoke(JaxbExportState state
) {
80 JaxbExportConfigurator jaxbExpConfig
= (JaxbExportConfigurator
)state
.getConfig();
81 // String dbname = jaxbExpConfig.getSource().getName();
82 URI uri
= jaxbExpConfig
.getDestination();
83 // logger.info("Serializing DB " + dbname + " to file " + fileName);
84 // logger.debug("DbSchemaValidation = " + jaxbExpConfig.getDbSchemaValidation());
86 TransactionStatus txStatus
= null;
88 txStatus
= startTransaction(true);
90 dataSet
= new DataSet();
95 logger
.info("Retrieving data from DB");
97 retrieveData(jaxbExpConfig
, dataSet
);
99 } catch (Exception e
) {
100 logger
.error("Error retrieving data");
104 logger
.info("All data retrieved");
107 switch(jaxbExpConfig
.getTarget()) {
109 writeToFile(new File(uri
), dataSet
);
112 CdmDocumentBuilder cdmDocumentBuilder
= new CdmDocumentBuilder();
113 exportStream
= new ByteArrayOutputStream();
114 cdmDocumentBuilder
.marshal(dataSet
, new StreamResult(exportStream
));
119 } catch (Exception e
) {
120 logger
.error("Marshalling error");
124 commitTransaction(txStatus
);
130 public static void writeToFile(File file
, DataSet dataSet
) throws UnsupportedEncodingException
, FileNotFoundException
{
131 CdmDocumentBuilder cdmDocumentBuilder
= new CdmDocumentBuilder();
132 PrintWriter writer
= new PrintWriter(new OutputStreamWriter(new FileOutputStream(file
), "UTF8"), true);
133 cdmDocumentBuilder
.marshal(dataSet
, writer
);
135 // TODO: Split into one file per data set member to see whether performance improves?
137 logger
.info("XML file written");
138 logger
.info("Filename is: " + file
.getAbsolutePath());
141 private void retrieveData (IExportConfigurator config
, DataSet dataSet
) {
143 JaxbExportConfigurator jaxbExpConfig
= (JaxbExportConfigurator
)config
;
144 final int MAX_ROWS
= 50000;
145 int numberOfRows
= jaxbExpConfig
.getMaxRows();
147 int agentRows
= numberOfRows
;
148 int definedTermBaseRows
= numberOfRows
;
149 int referenceBaseRows
= numberOfRows
;
150 int taxonNameBaseRows
= numberOfRows
;
151 int taxonBaseRows
= numberOfRows
;
152 int taxonNodeRows
= numberOfRows
;
153 int relationshipRows
= numberOfRows
;
154 int occurrencesRows
= numberOfRows
;
155 int mediaRows
= numberOfRows
;
156 int featureDataRows
= numberOfRows
;
157 int classificationDataRows
= numberOfRows
;
158 int languageDataRows
= numberOfRows
;
159 int termVocabularyRows
= numberOfRows
;
160 int homotypicalGroupRows
= numberOfRows
;
161 int UserRows
= numberOfRows
;
163 if (jaxbExpConfig
.isDoUsers() == true) {
165 if (UserRows
== 0) { UserRows
= MAX_ROWS
; }
166 logger
.info("# User");
167 List
<User
> users
= getUserService().list(null, UserRows
, 0, null, null);
170 for (User user
: users
){
171 dataSet
.addUser(HibernateProxyHelper
.deproxy(user
));
175 if (jaxbExpConfig
.isDoTermVocabularies() == true) {
176 if (termVocabularyRows
== 0) { termVocabularyRows
= MAX_ROWS
; }
177 logger
.info("# TermVocabulary");
178 dataSet
.setTermVocabularies((List
)getVocabularyService().list(null,termVocabularyRows
, 0, null, null));
183 // if (jaxbExpConfig.isDoLanguageData() == true) {
184 // if (languageDataRows == 0) { languageDataRows = MAX_ROWS; }
185 // logger.info("# Representation, Language String");
186 // dataSet.setLanguageData(getTermService().getAllRepresentations(MAX_ROWS, 0));
188 dataSet
.setLanguageStrings(getTermService().getAllLanguageStrings(MAX_ROWS
, 0));
191 if (jaxbExpConfig
.isDoTerms() == true) {
192 if (definedTermBaseRows
== 0) { definedTermBaseRows
= getTermService().count(DefinedTermBase
.class); }
193 logger
.info("# DefinedTermBase: " + definedTermBaseRows
);
194 dataSet
.setTerms(getTermService().list(null,definedTermBaseRows
, 0,null,null));
197 if (jaxbExpConfig
.isDoAuthors() == true) {
198 if (agentRows
== 0) { agentRows
= getAgentService().count(AgentBase
.class); }
199 logger
.info("# Agents: " + agentRows
);
200 //logger.info(" # Team: " + appCtr.getAgentService().count(Team.class));
201 dataSet
.setAgents(getAgentService().list(null,agentRows
, 0,null,null));
206 if (jaxbExpConfig
.getDoReferences() != IExportConfigurator
.DO_REFERENCES
.NONE
) {
207 if (referenceBaseRows
== 0) { referenceBaseRows
= getReferenceService().count(Reference
.class); }
208 logger
.info("# Reference: " + referenceBaseRows
);
209 dataSet
.setReferences(getReferenceService().list(null,referenceBaseRows
, 0,null,null));
212 if (jaxbExpConfig
.isDoHomotypicalGroups() == true) {
213 if (homotypicalGroupRows
== 0) { homotypicalGroupRows
= MAX_ROWS
; }
214 logger
.info("# Homotypical Groups");
215 dataSet
.setHomotypicalGroups(getNameService().getAllHomotypicalGroups(homotypicalGroupRows
, 0));
218 if (jaxbExpConfig
.isDoTaxonNames() == true) {
219 if (taxonNameBaseRows
== 0) { taxonNameBaseRows
= getNameService().count(TaxonNameBase
.class); }
220 logger
.info("# TaxonNameBase: " + taxonNameBaseRows
);
221 //logger.info(" # Taxon: " + getNameService().count(BotanicalName.class));
222 dataSet
.setTaxonomicNames(getNameService().list(null,taxonNameBaseRows
, 0,null,null));
227 if (jaxbExpConfig
.isDoTaxa() == true) {
228 if (taxonBaseRows
== 0) { taxonBaseRows
= getTaxonService().count(TaxonBase
.class); }
229 logger
.info("# TaxonBase: " + taxonBaseRows
);
230 // dataSet.setTaxa(new ArrayList<Taxon>());
231 // dataSet.setSynonyms(new ArrayList<Synonym>());
232 List
<TaxonBase
> tb
= getTaxonService().list(null,taxonBaseRows
, 0,null,null);
233 for (TaxonBase taxonBase
: tb
) {
234 dataSet
.addTaxonBase(HibernateProxyHelper
.deproxy(taxonBase
));
239 // retrieve taxa and synonyms separately
240 // need correct count for taxa and synonyms
241 // if (taxonBaseRows == 0) { taxonBaseRows = getTaxonService().count(TaxonBase.class); }
242 // logger.info("# Synonym: " + taxonBaseRows);
243 // dataSet.setSynonyms(new ArrayList<Synonym>());
244 // dataSet.setSynonyms(getTaxonService().getAllSynonyms(taxonBaseRows, 0));
246 // if (jaxbExpConfig.isDoRelTaxa() == true) {
247 // if (relationshipRows == 0) { relationshipRows = MAX_ROWS; }
248 // logger.info("# Relationships");
249 // List<RelationshipBase> relationList = getTaxonService().getAllRelationships(relationshipRows, 0);
250 // Set<RelationshipBase> relationSet = new HashSet<RelationshipBase>(relationList);
251 // dataSet.setRelationships(relationSet);
253 if (jaxbExpConfig
.isDoOccurrence() == true) {
254 if (occurrencesRows
== 0) { occurrencesRows
= getOccurrenceService().count(SpecimenOrObservationBase
.class); }
255 logger
.info("# SpecimenOrObservationBase: " + occurrencesRows
);
256 List
<SpecimenOrObservationBase
> occurrenceList
= getOccurrenceService().list(null,occurrencesRows
, 0,null,null);
257 /*List<SpecimenOrObservationBase> noProxyList = new ArrayList<SpecimenOrObservationBase>();
258 for (SpecimenOrObservationBase specimen : occurrenceList){
259 specimen = (SpecimenOrObservationBase)HibernateProxyHelper.deproxy(specimen);
260 noProxyList.add(specimen);
262 dataSet
.setOccurrences(occurrenceList
);
265 if (jaxbExpConfig
.isDoTypeDesignations() == true) {
266 logger
.info("# TypeDesignations");
267 dataSet
.addTypeDesignations(getNameService().getAllTypeDesignations(MAX_ROWS
, 0));
272 if (jaxbExpConfig
.isDoMedia() == true) {
273 if (mediaRows
== 0) { mediaRows
= MAX_ROWS
; }
274 logger
.info("# Media");
275 dataSet
.setMedia(getMediaService().list(null,mediaRows
, 0,null,null));
276 // dataSet.addMedia(getMediaService().getAllMediaRepresentations(mediaRows, 0));
277 // dataSet.addMedia(getMediaService().getAllMediaRepresentationParts(mediaRows, 0));
280 if (jaxbExpConfig
.isDoFeatureData() == true) {
281 if (featureDataRows
== 0) { featureDataRows
= MAX_ROWS
; }
282 logger
.info("# Feature Tree, Feature Node");
283 List
<FeatureTree
> featureTrees
= new ArrayList
<FeatureTree
>();
284 featureTrees
= getFeatureTreeService().list(null,featureDataRows
, 0, null, null);
285 List
<FeatureTree
> taxTreesdeproxy
= new ArrayList
<FeatureTree
>();
286 for (FeatureTree featureTree
: featureTrees
){
287 HibernateProxyHelper
.deproxy(featureTree
);
288 taxTreesdeproxy
.add(featureTree
);
291 dataSet
.setFeatureTrees(getFeatureTreeService().list(null,null,null,null,null));
293 if (jaxbExpConfig
.isDoClassificationData() == true) {
294 if (classificationDataRows
== 0) { classificationDataRows
= MAX_ROWS
; }
295 logger
.info("# Classification");
298 List
<Classification
> taxTrees
= new ArrayList
<Classification
>();
299 taxTrees
= getClassificationService().list(null,classificationDataRows
, 0, null, null);
301 List
<Classification
> taxTreesdeproxy
= new ArrayList
<Classification
>();
302 for (Classification taxTree
: taxTrees
){
303 HibernateProxyHelper
.deproxy(taxTree
);
304 taxTreesdeproxy
.add(taxTree
);
306 List
<TaxonNode
> taxNodes
= new ArrayList
<TaxonNode
>();
307 taxNodes
= getClassificationService().getAllNodes();
308 List
<TaxonNode
> taxNodesdeproxy
= new ArrayList
<TaxonNode
>();
309 for (TaxonNode taxNode
: taxNodes
){
310 HibernateProxyHelper
.deproxy(taxNode
);
311 taxNodesdeproxy
.add(taxNode
);
314 dataSet
.setTaxonNodes(taxNodesdeproxy
);
315 dataSet
.setClassifications(taxTreesdeproxy
);
318 dataSet
.setLanguageStrings(null);
323 protected boolean doCheck(JaxbExportState state
) {
324 boolean result
= true;
325 logger
.warn("No check implemented for Jaxb export");
331 protected boolean isIgnore(JaxbExportState state
) {