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