License
[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.HashSet;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Set;
19
20 import org.apache.log4j.Logger;
21 import org.springframework.stereotype.Component;
22 import org.springframework.transaction.TransactionStatus;
23
24 import eu.etaxonomy.cdm.io.common.CdmIoBase;
25 import eu.etaxonomy.cdm.io.common.ICdmIO;
26 import eu.etaxonomy.cdm.io.common.IExportConfigurator;
27 import eu.etaxonomy.cdm.io.common.IImportConfigurator;
28 import eu.etaxonomy.cdm.io.common.MapWrapper;
29 import eu.etaxonomy.cdm.model.agent.Agent;
30 import eu.etaxonomy.cdm.model.common.CdmBase;
31 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
32 import eu.etaxonomy.cdm.model.common.RelationshipBase;
33 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
34 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
35 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
36 import eu.etaxonomy.cdm.model.taxon.Synonym;
37 import eu.etaxonomy.cdm.model.taxon.Taxon;
38 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
39
40 /**
41 * @author a.babadshanjan
42 * @created 25.09.2008
43 */
44 @Component
45 public class JaxbExport extends CdmIoBase<IExportConfigurator> implements ICdmIO<IExportConfigurator> {
46 // public class JaxbExport extends CdmIoBase implements ICdmIoExport {
47 // TODO: public class JaxbExport extends CdmIoBase implements ICdmIO {
48
49 private static final Logger logger = Logger.getLogger(JaxbExport.class);
50 private CdmDocumentBuilder cdmDocumentBuilder = null;
51
52 private String ioName = null;
53
54
55 /**
56 *
57 */
58 public JaxbExport() {
59 super();
60 this.ioName = this.getClass().getSimpleName();
61 }
62
63 /** Retrieves data from a CDM DB and serializes them CDM to XML.
64 * Starts with root taxa and traverses the taxonomic tree to retrieve children taxa, synonyms and relationships.
65 * Taxa that are not part of the taxonomic tree are not found.
66 *
67 * @param exImpConfig
68 * @param dbname
69 * @param filename
70 */
71 @Override
72 protected boolean doInvoke(IExportConfigurator config,
73 Map<String, MapWrapper<? extends CdmBase>> stores) {
74
75 JaxbExportConfigurator jaxbExpConfig = (JaxbExportConfigurator)config;
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());
80
81 TransactionStatus txStatus = startTransaction(true);
82 DataSet dataSet = new DataSet();
83 List<Taxon> taxa = null;
84 List<DefinedTermBase> terms = null;
85
86 // get data from DB
87
88 try {
89 logger.info("Retrieving data from DB");
90
91 retrieveData(config, dataSet);
92
93 } catch (Exception e) {
94 logger.error("Error retrieving data");
95 e.printStackTrace();
96 }
97
98 logger.info("All data retrieved");
99
100 try {
101 cdmDocumentBuilder = new CdmDocumentBuilder();
102 PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(fileName), "UTF8"), true);
103 cdmDocumentBuilder.marshal(dataSet, writer);
104
105 // TODO: Split into one file per data set member to see whether performance improves?
106
107 logger.info("XML file written");
108 logger.info("Filename is: " + fileName);
109
110 } catch (Exception e) {
111 logger.error("Marshalling error");
112 e.printStackTrace();
113 }
114 commitTransaction(txStatus);
115
116 return true;
117
118 }
119
120
121 private void retrieveData (IExportConfigurator config, DataSet dataSet) {
122
123 JaxbExportConfigurator jaxbExpConfig = (JaxbExportConfigurator)config;
124 final int MAX_ROWS = 50000;
125 int numberOfRows = jaxbExpConfig.getMaxRows();
126
127 int agentRows = numberOfRows;
128 int definedTermBaseRows = numberOfRows;
129 int referenceBaseRows = numberOfRows;
130 int taxonNameBaseRows = numberOfRows;
131 int taxonBaseRows = numberOfRows;
132 int relationshipRows = numberOfRows;
133 int occurrencesRows = numberOfRows;
134 int mediaRows = numberOfRows;
135 int featureDataRows = numberOfRows;
136 int languageDataRows = numberOfRows;
137 int termVocabularyRows = numberOfRows;
138 int homotypicalGroupRows = numberOfRows;
139
140 if (jaxbExpConfig.isDoTermVocabularies() == true) {
141 if (termVocabularyRows == 0) { termVocabularyRows = MAX_ROWS; }
142 logger.info("# TermVocabulary");
143 dataSet.setTermVocabularies(getTermService().getAllTermVocabularies(MAX_ROWS, 0));;
144 }
145
146 if (jaxbExpConfig.isDoLanguageData() == true) {
147 if (languageDataRows == 0) { languageDataRows = MAX_ROWS; }
148 logger.info("# Representation, Language String");
149 dataSet.setLanguageData(getTermService().getAllRepresentations(MAX_ROWS, 0));
150 dataSet.addLanguageData(getTermService().getAllLanguageStrings(MAX_ROWS, 0));
151 }
152
153 if (jaxbExpConfig.isDoTerms() == true) {
154 if (definedTermBaseRows == 0) { definedTermBaseRows = getTermService().count(DefinedTermBase.class); }
155 logger.info("# DefinedTermBase: " + definedTermBaseRows);
156 dataSet.setTerms(getTermService().getAllDefinedTerms(definedTermBaseRows, 0));
157 }
158
159 if (jaxbExpConfig.isDoAuthors() == true) {
160 if (agentRows == 0) { agentRows = getAgentService().count(Agent.class); }
161 logger.info("# Agents: " + agentRows);
162 //logger.info(" # Team: " + appCtr.getAgentService().count(Team.class));
163 dataSet.setAgents(getAgentService().getAllAgents(agentRows, 0));
164 }
165
166 if (jaxbExpConfig.getDoReferences() != IImportConfigurator.DO_REFERENCES.NONE) {
167 if (referenceBaseRows == 0) { referenceBaseRows = getReferenceService().count(ReferenceBase.class); }
168 logger.info("# ReferenceBase: " + referenceBaseRows);
169 dataSet.setReferences(getReferenceService().getAllReferences(referenceBaseRows, 0));
170 }
171
172 if (jaxbExpConfig.isDoTaxonNames() == true) {
173 if (taxonNameBaseRows == 0) { taxonNameBaseRows = getNameService().count(TaxonNameBase.class); }
174 logger.info("# TaxonNameBase: " + taxonNameBaseRows);
175 //logger.info(" # Taxon: " + getNameService().count(BotanicalName.class));
176 dataSet.setTaxonomicNames(getNameService().getAllNames(taxonNameBaseRows, 0));
177 }
178
179 if (jaxbExpConfig.isDoHomotypicalGroups() == true) {
180 if (homotypicalGroupRows == 0) { homotypicalGroupRows = MAX_ROWS; }
181 logger.info("# Homotypical Groups");
182 dataSet.setHomotypicalGroups(getNameService().getAllHomotypicalGroups(homotypicalGroupRows, 0));
183 }
184
185 if (jaxbExpConfig.isDoTaxa() == true) {
186 if (taxonBaseRows == 0) { taxonBaseRows = getTaxonService().count(TaxonBase.class); }
187 logger.info("# TaxonBase: " + taxonBaseRows);
188 // dataSet.setTaxa(new ArrayList<Taxon>());
189 // dataSet.setSynonyms(new ArrayList<Synonym>());
190 List<TaxonBase> tb = getTaxonService().getAllTaxonBases(taxonBaseRows, 0);
191 for (TaxonBase taxonBase : tb) {
192 if (taxonBase instanceof Taxon) {
193 dataSet.addTaxon((Taxon)taxonBase);
194 } else if (taxonBase instanceof Synonym) {
195 dataSet.addSynonym((Synonym)taxonBase);
196 } else {
197 logger.error("entry of wrong type: " + taxonBase.toString());
198 }
199 }
200 }
201
202 // TODO:
203 // retrieve taxa and synonyms separately
204 // need correct count for taxa and synonyms
205 // if (taxonBaseRows == 0) { taxonBaseRows = getTaxonService().count(TaxonBase.class); }
206 // logger.info("# Synonym: " + taxonBaseRows);
207 // dataSet.setSynonyms(new ArrayList<Synonym>());
208 // dataSet.setSynonyms(getTaxonService().getAllSynonyms(taxonBaseRows, 0));
209
210 if (jaxbExpConfig.isDoRelTaxa() == true) {
211 if (relationshipRows == 0) { relationshipRows = MAX_ROWS; }
212 logger.info("# Relationships");
213 List<RelationshipBase> relationList = getTaxonService().getAllRelationships(relationshipRows, 0);
214 Set<RelationshipBase> relationSet = new HashSet<RelationshipBase>(relationList);
215 dataSet.setRelationships(relationSet);
216 }
217
218 if (jaxbExpConfig.isDoReferencedEntities() == true) {
219 logger.info("# Referenced Entities");
220 dataSet.setReferencedEntities(getNameService().getAllNomenclaturalStatus(MAX_ROWS, 0));
221 dataSet.addReferencedEntities(getNameService().getAllTypeDesignations(MAX_ROWS, 0));
222 }
223
224 if (jaxbExpConfig.isDoOccurrence() == true) {
225 if (occurrencesRows == 0) { occurrencesRows = getOccurrenceService().count(SpecimenOrObservationBase.class); }
226 logger.info("# SpecimenOrObservationBase: " + occurrencesRows);
227 dataSet.setOccurrences(getOccurrenceService().getAllSpecimenOrObservationBases(occurrencesRows, 0));
228 }
229
230 if (jaxbExpConfig.isDoMedia() == true) {
231 if (mediaRows == 0) { mediaRows = MAX_ROWS; }
232 logger.info("# Media");
233 dataSet.setMedia(getMediaService().getAllMedia(mediaRows, 0));
234 // dataSet.addMedia(getMediaService().getAllMediaRepresentations(mediaRows, 0));
235 // dataSet.addMedia(getMediaService().getAllMediaRepresentationParts(mediaRows, 0));
236 }
237
238 if (jaxbExpConfig.isDoFeatureData() == true) {
239 if (featureDataRows == 0) { featureDataRows = MAX_ROWS; }
240 logger.info("# Feature Tree, Feature Node");
241 dataSet.setFeatureData(getDescriptionService().getFeatureNodesAll());
242 dataSet.addFeatureData(getDescriptionService().getFeatureTreesAll());
243 }
244 }
245
246
247 @Override
248 protected boolean doCheck(IExportConfigurator config) {
249 boolean result = true;
250 logger.warn("No check implemented for Jaxb export");
251 return result;
252 }
253
254
255 @Override
256 protected boolean isIgnore(IExportConfigurator config) {
257 return false;
258 }
259
260 }