Jaxb export bugfix
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / jaxb / JaxbExport.java
1 /**
2 * Copyright (C) 2008 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9
10 package eu.etaxonomy.cdm.io.jaxb;
11
12 import java.io.FileOutputStream;
13 import java.io.OutputStreamWriter;
14 import java.io.PrintWriter;
15 import java.util.List;
16
17 import org.apache.log4j.Logger;
18 import org.springframework.stereotype.Component;
19 import org.springframework.transaction.TransactionStatus;
20
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;
33
34 /**
35 * @author a.babadshanjan
36 * @created 25.09.2008
37 * @version 1.0
38 */
39 @Component
40 public class JaxbExport extends CdmExportBase<JaxbExportConfigurator, JaxbExportState> implements ICdmExport<JaxbExportConfigurator, JaxbExportState> {
41
42 private static final Logger logger = Logger.getLogger(JaxbExport.class);
43 private CdmDocumentBuilder cdmDocumentBuilder = null;
44
45 // /**
46 // *
47 // */
48 // public JaxbExport() {
49 // super();
50 // this.ioName = this.getClass().getSimpleName();
51 // }
52
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.
56 *
57 * @param exImpConfig
58 * @param dbname
59 * @param filename
60 */
61 // @Override
62 // protected boolean doInvoke(IExportConfigurator config,
63 // Map<String, MapWrapper<? extends CdmBase>> stores) {
64 @Override
65 protected boolean doInvoke(JaxbExportState state) {
66
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());
72
73 TransactionStatus txStatus = startTransaction(true);
74 DataSet dataSet = new DataSet();
75
76 // get data from DB
77
78 try {
79 logger.info("Retrieving data from DB");
80
81 retrieveData(jaxbExpConfig, dataSet);
82
83 } catch (Exception e) {
84 logger.error("Error retrieving data");
85 e.printStackTrace();
86 }
87
88 logger.info("All data retrieved");
89
90 try {
91 cdmDocumentBuilder = new CdmDocumentBuilder();
92 PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(fileName), "UTF8"), true);
93 cdmDocumentBuilder.marshal(dataSet, writer);
94
95 // TODO: Split into one file per data set member to see whether performance improves?
96
97 logger.info("XML file written");
98 logger.info("Filename is: " + fileName);
99
100 } catch (Exception e) {
101 logger.error("Marshalling error");
102 e.printStackTrace();
103 }
104 commitTransaction(txStatus);
105
106 return true;
107
108 }
109
110
111 private void retrieveData (IExportConfigurator config, DataSet dataSet) {
112
113 JaxbExportConfigurator jaxbExpConfig = (JaxbExportConfigurator)config;
114 final int MAX_ROWS = 50000;
115 int numberOfRows = jaxbExpConfig.getMaxRows();
116
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;
129
130 if (jaxbExpConfig.isDoTermVocabularies() == true) {
131 if (termVocabularyRows == 0) { termVocabularyRows = MAX_ROWS; }
132 logger.info("# TermVocabulary");
133 dataSet.setTermVocabularies(getTermService().getAllTermVocabularies(MAX_ROWS, 0));;
134 }
135
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));
141 // }
142
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));
147 }
148
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));
154 }
155
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));
160 }
161
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));
167 }
168
169 if (jaxbExpConfig.isDoHomotypicalGroups() == true) {
170 if (homotypicalGroupRows == 0) { homotypicalGroupRows = MAX_ROWS; }
171 logger.info("# Homotypical Groups");
172 dataSet.setHomotypicalGroups(getNameService().getAllHomotypicalGroups(homotypicalGroupRows, 0));
173 }
174
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);
183 }
184 }
185
186 // TODO:
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));
193
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);
200 // }
201
202 if (jaxbExpConfig.isDoTypeDesignations() == true) {
203 logger.info("# TypeDesignations");
204 dataSet.addTypeDesignations(getNameService().getAllTypeDesignations(MAX_ROWS, 0));
205 }
206
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));
211 }
212
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));
219 }
220
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));
225 }
226 }
227
228
229 @Override
230 protected boolean doCheck(JaxbExportState state) {
231 boolean result = true;
232 logger.warn("No check implemented for Jaxb export");
233 return result;
234 }
235
236
237 @Override
238 protected boolean isIgnore(JaxbExportState state) {
239 return false;
240 }
241
242 }