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