(no commit message)
[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 javax.xml.transform.sax.SAXResult;
18
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;
25
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;
39
40 /**
41 * @author a.babadshanjan
42 * @created 25.09.2008
43 * @version 1.0
44 */
45 @Component
46 public class JaxbExport extends CdmExportBase<JaxbExportConfigurator, JaxbExportState> implements ICdmExport<JaxbExportConfigurator, JaxbExportState> {
47
48 private static final Logger logger = Logger.getLogger(JaxbExport.class);
49 private CdmDocumentBuilder cdmDocumentBuilder = null;
50
51 // /**
52 // *
53 // */
54 // public JaxbExport() {
55 // super();
56 // this.ioName = this.getClass().getSimpleName();
57 // }
58
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.
62 *
63 * @param exImpConfig
64 * @param dbname
65 * @param filename
66 */
67 // @Override
68 // protected boolean doInvoke(IExportConfigurator config,
69 // Map<String, MapWrapper<? extends CdmBase>> stores) {
70 @Override
71 protected boolean doInvoke(JaxbExportState state) {
72
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());
78
79 TransactionStatus txStatus = startTransaction(true);
80 DataSet dataSet = new DataSet();
81
82 // get data from DB
83
84 try {
85 logger.info("Retrieving data from DB");
86
87 retrieveData(jaxbExpConfig, dataSet);
88
89 } catch (Exception e) {
90 logger.error("Error retrieving data");
91 e.printStackTrace();
92 }
93
94 logger.info("All data retrieved");
95
96 try {
97 cdmDocumentBuilder = new CdmDocumentBuilder();
98 PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(fileName), "UTF8"), true);
99
100 /*SAXResult result = new SAXResult();
101 ContentHandler handler = new DefaultHandler();
102
103 result.setHandler(handler);*/
104
105 //cdmDocumentBuilder.marshal(dataSet, result);
106 cdmDocumentBuilder.marshal(dataSet, writer);
107
108 // TODO: Split into one file per data set member to see whether performance improves?
109
110 logger.info("XML file written");
111 logger.info("Filename is: " + fileName);
112
113 } catch (Exception e) {
114 logger.error("Marshalling error");
115 e.printStackTrace();
116 }
117 commitTransaction(txStatus);
118
119 return true;
120
121 }
122
123
124 private void retrieveData (IExportConfigurator config, DataSet dataSet) {
125
126 JaxbExportConfigurator jaxbExpConfig = (JaxbExportConfigurator)config;
127 final int MAX_ROWS = 50000;
128 int numberOfRows = jaxbExpConfig.getMaxRows();
129
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;
143
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));
148 }
149
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));
155 // }
156
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));
161 }
162
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));
168 }
169
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));
174 }
175
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));
181 }
182
183 if (jaxbExpConfig.isDoHomotypicalGroups() == true) {
184 if (homotypicalGroupRows == 0) { homotypicalGroupRows = MAX_ROWS; }
185 logger.info("# Homotypical Groups");
186 dataSet.setHomotypicalGroups(getNameService().getAllHomotypicalGroups(homotypicalGroupRows, 0));
187 }
188
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);
197 }
198 }
199
200 // TODO:
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));
207
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);
214 // }
215
216 if (jaxbExpConfig.isDoTypeDesignations() == true) {
217 logger.info("# TypeDesignations");
218 dataSet.addTypeDesignations(getNameService().getAllTypeDesignations(MAX_ROWS, 0));
219 }
220
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));
225 }
226
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));
233 }
234
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));
239 }
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));
244 }
245 }
246
247
248 @Override
249 protected boolean doCheck(JaxbExportState state) {
250 boolean result = true;
251 logger.warn("No check implemented for Jaxb export");
252 return result;
253 }
254
255
256 @Override
257 protected boolean isIgnore(JaxbExportState state) {
258 return false;
259 }
260
261 }