1 |
ac5020e6
|
Andreas Müller
|
/**
|
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.ByteArrayOutputStream;
|
13 |
|
|
import java.io.File;
|
14 |
|
|
import java.io.FileNotFoundException;
|
15 |
|
|
import java.io.FileOutputStream;
|
16 |
|
|
import java.io.OutputStreamWriter;
|
17 |
|
|
import java.io.PrintWriter;
|
18 |
|
|
import java.io.UnsupportedEncodingException;
|
19 |
|
|
import java.util.ArrayList;
|
20 |
|
|
import java.util.List;
|
21 |
|
|
|
22 |
|
|
import javax.xml.transform.stream.StreamResult;
|
23 |
|
|
|
24 |
|
|
import org.apache.log4j.Logger;
|
25 |
|
|
import org.springframework.stereotype.Component;
|
26 |
|
|
import org.springframework.transaction.TransactionStatus;
|
27 |
|
|
|
28 |
|
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
29 |
|
|
import eu.etaxonomy.cdm.io.common.CdmExportBase;
|
30 |
|
|
import eu.etaxonomy.cdm.io.common.ICdmExport;
|
31 |
|
|
import eu.etaxonomy.cdm.io.common.IExportConfigurator;
|
32 |
|
|
import eu.etaxonomy.cdm.io.common.mapping.out.IExportTransformer;
|
33 |
|
|
import eu.etaxonomy.cdm.model.agent.AgentBase;
|
34 |
|
|
import eu.etaxonomy.cdm.model.common.User;
|
35 |
9dc896c9
|
Andreas Müller
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
36 |
ac5020e6
|
Andreas Müller
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
37 |
|
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
38 |
|
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
39 |
|
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
40 |
|
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
41 |
34059bfd
|
Andreas Müller
|
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
|
42 |
b64f90fb
|
Andreas Müller
|
import eu.etaxonomy.cdm.model.term.FeatureTree;
|
43 |
ac5020e6
|
Andreas Müller
|
|
44 |
|
|
/**
|
45 |
|
|
* @author a.babadshanjan
|
46 |
a88578ce
|
Andreas Müller
|
* @since 25.09.2008
|
47 |
ac5020e6
|
Andreas Müller
|
*/
|
48 |
|
|
@Component
|
49 |
18a0110f
|
Andreas Müller
|
public class JaxbExport
|
50 |
89102b8a
|
Andreas Müller
|
extends CdmExportBase<JaxbExportConfigurator, JaxbExportState, IExportTransformer, File>
|
51 |
18a0110f
|
Andreas Müller
|
implements ICdmExport<JaxbExportConfigurator, JaxbExportState> {
|
52 |
|
|
|
53 |
|
|
private static final long serialVersionUID = -525533131708894145L;
|
54 |
ac5020e6
|
Andreas Müller
|
|
55 |
|
|
private static final Logger logger = Logger.getLogger(JaxbExport.class);
|
56 |
|
|
|
57 |
|
|
private DataSet dataSet;
|
58 |
|
|
|
59 |
03ebde9e
|
Katja Luther
|
/**
|
60 |
|
|
*
|
61 |
|
|
*/
|
62 |
|
|
public JaxbExport() {
|
63 |
|
|
super();
|
64 |
|
|
}
|
65 |
|
|
|
66 |
18a0110f
|
Andreas Müller
|
/**
|
67 |
|
|
* {@inheritDoc}
|
68 |
|
|
*
|
69 |
|
|
* .<BR>Retrieves data from a CDM DB and serializes them CDM to XML.
|
70 |
ac5020e6
|
Andreas Müller
|
* Starts with root taxa and traverses the classification to retrieve children taxa, synonyms and relationships.
|
71 |
|
|
* Taxa that are not part of the classification are not found.
|
72 |
|
|
*/
|
73 |
|
|
@Override
|
74 |
|
|
protected void doInvoke(JaxbExportState state) {
|
75 |
|
|
|
76 |
89102b8a
|
Andreas Müller
|
JaxbExportConfigurator jaxbExpConfig = state.getConfig();
|
77 |
ac5020e6
|
Andreas Müller
|
// String dbname = jaxbExpConfig.getSource().getName();
|
78 |
89102b8a
|
Andreas Müller
|
File file = jaxbExpConfig.getDestination();
|
79 |
ac5020e6
|
Andreas Müller
|
// logger.info("Serializing DB " + dbname + " to file " + fileName);
|
80 |
|
|
// logger.debug("DbSchemaValidation = " + jaxbExpConfig.getDbSchemaValidation());
|
81 |
|
|
|
82 |
|
|
TransactionStatus txStatus = null;
|
83 |
|
|
|
84 |
|
|
txStatus = startTransaction(true);
|
85 |
|
|
|
86 |
|
|
dataSet = new DataSet();
|
87 |
|
|
|
88 |
|
|
// get data from DB
|
89 |
|
|
|
90 |
|
|
try {
|
91 |
|
|
logger.info("Retrieving data from DB");
|
92 |
|
|
|
93 |
|
|
retrieveData(jaxbExpConfig, dataSet);
|
94 |
|
|
|
95 |
|
|
} catch (Exception e) {
|
96 |
|
|
logger.error("Error retrieving data");
|
97 |
|
|
e.printStackTrace();
|
98 |
|
|
}
|
99 |
|
|
|
100 |
|
|
logger.info("All data retrieved");
|
101 |
|
|
|
102 |
|
|
try {
|
103 |
|
|
switch(jaxbExpConfig.getTarget()) {
|
104 |
|
|
case FILE:
|
105 |
89102b8a
|
Andreas Müller
|
writeToFile(file, dataSet);
|
106 |
ac5020e6
|
Andreas Müller
|
break;
|
107 |
|
|
case EXPORT_DATA:
|
108 |
|
|
CdmDocumentBuilder cdmDocumentBuilder = new CdmDocumentBuilder();
|
109 |
|
|
exportStream = new ByteArrayOutputStream();
|
110 |
|
|
cdmDocumentBuilder.marshal(dataSet, new StreamResult(exportStream));
|
111 |
89102b8a
|
Andreas Müller
|
state.getResult().addExportData((byte[])this.createExportData().getExportData());
|
112 |
2da896e1
|
Katja Luther
|
|
113 |
ac5020e6
|
Andreas Müller
|
break;
|
114 |
|
|
default:
|
115 |
|
|
break;
|
116 |
|
|
}
|
117 |
|
|
} catch (Exception e) {
|
118 |
|
|
logger.error("Marshalling error");
|
119 |
|
|
e.printStackTrace();
|
120 |
|
|
}
|
121 |
|
|
|
122 |
|
|
commitTransaction(txStatus);
|
123 |
|
|
|
124 |
|
|
return;
|
125 |
|
|
|
126 |
|
|
}
|
127 |
|
|
|
128 |
|
|
public static void writeToFile(File file, DataSet dataSet) throws UnsupportedEncodingException, FileNotFoundException {
|
129 |
|
|
CdmDocumentBuilder cdmDocumentBuilder = new CdmDocumentBuilder();
|
130 |
|
|
PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF8"), true);
|
131 |
|
|
cdmDocumentBuilder.marshal(dataSet, writer);
|
132 |
|
|
|
133 |
|
|
// TODO: Split into one file per data set member to see whether performance improves?
|
134 |
|
|
|
135 |
|
|
logger.info("XML file written");
|
136 |
|
|
logger.info("Filename is: " + file.getAbsolutePath());
|
137 |
|
|
}
|
138 |
|
|
|
139 |
|
|
private void retrieveData (IExportConfigurator config, DataSet dataSet) {
|
140 |
|
|
|
141 |
|
|
JaxbExportConfigurator jaxbExpConfig = (JaxbExportConfigurator)config;
|
142 |
|
|
final int MAX_ROWS = 50000;
|
143 |
|
|
int numberOfRows = jaxbExpConfig.getMaxRows();
|
144 |
|
|
|
145 |
|
|
int agentRows = numberOfRows;
|
146 |
|
|
int definedTermBaseRows = numberOfRows;
|
147 |
|
|
int referenceBaseRows = numberOfRows;
|
148 |
9dc896c9
|
Andreas Müller
|
int taxonNameRows = numberOfRows;
|
149 |
ac5020e6
|
Andreas Müller
|
int taxonBaseRows = numberOfRows;
|
150 |
|
|
int taxonNodeRows = numberOfRows;
|
151 |
|
|
int relationshipRows = numberOfRows;
|
152 |
|
|
int occurrencesRows = numberOfRows;
|
153 |
|
|
int mediaRows = numberOfRows;
|
154 |
|
|
int featureDataRows = numberOfRows;
|
155 |
|
|
int classificationDataRows = numberOfRows;
|
156 |
|
|
int languageDataRows = numberOfRows;
|
157 |
|
|
int termVocabularyRows = numberOfRows;
|
158 |
|
|
int homotypicalGroupRows = numberOfRows;
|
159 |
|
|
int UserRows= numberOfRows;
|
160 |
|
|
|
161 |
|
|
if (jaxbExpConfig.isDoUsers() == true) {
|
162 |
|
|
|
163 |
|
|
if (UserRows == 0) { UserRows = MAX_ROWS; }
|
164 |
|
|
logger.info("# User");
|
165 |
|
|
List<User> users = getUserService().list(null, UserRows, 0, null, null);
|
166 |
|
|
|
167 |
|
|
|
168 |
|
|
for (User user: users){
|
169 |
|
|
dataSet.addUser(HibernateProxyHelper.deproxy(user));
|
170 |
|
|
}
|
171 |
|
|
|
172 |
|
|
}
|
173 |
|
|
if (jaxbExpConfig.isDoTermVocabularies() == true) {
|
174 |
|
|
if (termVocabularyRows == 0) { termVocabularyRows = MAX_ROWS; }
|
175 |
|
|
logger.info("# TermVocabulary");
|
176 |
|
|
dataSet.setTermVocabularies((List)getVocabularyService().list(null,termVocabularyRows, 0, null, null));
|
177 |
|
|
}
|
178 |
|
|
|
179 |
|
|
|
180 |
|
|
|
181 |
|
|
// if (jaxbExpConfig.isDoLanguageData() == true) {
|
182 |
|
|
// if (languageDataRows == 0) { languageDataRows = MAX_ROWS; }
|
183 |
|
|
// logger.info("# Representation, Language String");
|
184 |
|
|
// dataSet.setLanguageData(getTermService().getAllRepresentations(MAX_ROWS, 0));
|
185 |
|
|
//TODO!!!
|
186 |
|
|
dataSet.setLanguageStrings(getTermService().getAllLanguageStrings(MAX_ROWS, 0));
|
187 |
|
|
// }
|
188 |
|
|
|
189 |
|
|
if (jaxbExpConfig.isDoTerms() == true) {
|
190 |
|
|
if (definedTermBaseRows == 0) { definedTermBaseRows = getTermService().count(DefinedTermBase.class); }
|
191 |
|
|
logger.info("# DefinedTermBase: " + definedTermBaseRows);
|
192 |
|
|
dataSet.setTerms(getTermService().list(null,definedTermBaseRows, 0,null,null));
|
193 |
|
|
}
|
194 |
|
|
|
195 |
|
|
if (jaxbExpConfig.isDoAuthors() == true) {
|
196 |
|
|
if (agentRows == 0) { agentRows = getAgentService().count(AgentBase.class); }
|
197 |
|
|
logger.info("# Agents: " + agentRows);
|
198 |
|
|
//logger.info(" # Team: " + appCtr.getAgentService().count(Team.class));
|
199 |
|
|
dataSet.setAgents(getAgentService().list(null,agentRows, 0,null,null));
|
200 |
|
|
}
|
201 |
|
|
|
202 |
|
|
|
203 |
|
|
|
204 |
|
|
if (jaxbExpConfig.getDoReferences() != IExportConfigurator.DO_REFERENCES.NONE) {
|
205 |
|
|
if (referenceBaseRows == 0) { referenceBaseRows = getReferenceService().count(Reference.class); }
|
206 |
|
|
logger.info("# Reference: " + referenceBaseRows);
|
207 |
|
|
dataSet.setReferences(getReferenceService().list(null,referenceBaseRows, 0,null,null));
|
208 |
|
|
}
|
209 |
|
|
|
210 |
|
|
if (jaxbExpConfig.isDoHomotypicalGroups() == true) {
|
211 |
|
|
if (homotypicalGroupRows == 0) { homotypicalGroupRows = MAX_ROWS; }
|
212 |
|
|
logger.info("# Homotypical Groups");
|
213 |
|
|
dataSet.setHomotypicalGroups(getNameService().getAllHomotypicalGroups(homotypicalGroupRows, 0));
|
214 |
|
|
}
|
215 |
|
|
|
216 |
|
|
if (jaxbExpConfig.isDoTaxonNames() == true) {
|
217 |
9dc896c9
|
Andreas Müller
|
if (taxonNameRows == 0) { taxonNameRows = getNameService().count(TaxonName.class); }
|
218 |
|
|
logger.info("# TaxonName: " + taxonNameRows);
|
219 |
ac5020e6
|
Andreas Müller
|
//logger.info(" # Taxon: " + getNameService().count(BotanicalName.class));
|
220 |
9dc896c9
|
Andreas Müller
|
dataSet.setTaxonomicNames(getNameService().list(null,taxonNameRows, 0,null,null));
|
221 |
ac5020e6
|
Andreas Müller
|
}
|
222 |
|
|
|
223 |
|
|
|
224 |
|
|
|
225 |
|
|
if (jaxbExpConfig.isDoTaxa() == true) {
|
226 |
|
|
if (taxonBaseRows == 0) { taxonBaseRows = getTaxonService().count(TaxonBase.class); }
|
227 |
|
|
logger.info("# TaxonBase: " + taxonBaseRows);
|
228 |
|
|
// dataSet.setTaxa(new ArrayList<Taxon>());
|
229 |
|
|
// dataSet.setSynonyms(new ArrayList<Synonym>());
|
230 |
|
|
List<TaxonBase> tb = getTaxonService().list(null,taxonBaseRows, 0,null,null);
|
231 |
|
|
for (TaxonBase taxonBase : tb) {
|
232 |
|
|
dataSet.addTaxonBase(HibernateProxyHelper.deproxy(taxonBase));
|
233 |
|
|
}
|
234 |
|
|
}
|
235 |
|
|
|
236 |
|
|
// TODO:
|
237 |
|
|
// retrieve taxa and synonyms separately
|
238 |
|
|
// need correct count for taxa and synonyms
|
239 |
|
|
// if (taxonBaseRows == 0) { taxonBaseRows = getTaxonService().count(TaxonBase.class); }
|
240 |
|
|
// logger.info("# Synonym: " + taxonBaseRows);
|
241 |
|
|
// dataSet.setSynonyms(new ArrayList<Synonym>());
|
242 |
|
|
// dataSet.setSynonyms(getTaxonService().getAllSynonyms(taxonBaseRows, 0));
|
243 |
|
|
|
244 |
|
|
// if (jaxbExpConfig.isDoRelTaxa() == true) {
|
245 |
|
|
// if (relationshipRows == 0) { relationshipRows = MAX_ROWS; }
|
246 |
|
|
// logger.info("# Relationships");
|
247 |
|
|
// List<RelationshipBase> relationList = getTaxonService().getAllRelationships(relationshipRows, 0);
|
248 |
|
|
// Set<RelationshipBase> relationSet = new HashSet<RelationshipBase>(relationList);
|
249 |
|
|
// dataSet.setRelationships(relationSet);
|
250 |
|
|
// }
|
251 |
|
|
if (jaxbExpConfig.isDoOccurrence() == true) {
|
252 |
|
|
if (occurrencesRows == 0) { occurrencesRows = getOccurrenceService().count(SpecimenOrObservationBase.class); }
|
253 |
|
|
logger.info("# SpecimenOrObservationBase: " + occurrencesRows);
|
254 |
|
|
List<SpecimenOrObservationBase> occurrenceList = getOccurrenceService().list(null,occurrencesRows, 0,null,null);
|
255 |
|
|
/*List<SpecimenOrObservationBase> noProxyList = new ArrayList<SpecimenOrObservationBase>();
|
256 |
|
|
for (SpecimenOrObservationBase specimen : occurrenceList){
|
257 |
|
|
specimen = (SpecimenOrObservationBase)HibernateProxyHelper.deproxy(specimen);
|
258 |
|
|
noProxyList.add(specimen);
|
259 |
|
|
}*/
|
260 |
|
|
dataSet.setOccurrences(occurrenceList);
|
261 |
|
|
}
|
262 |
|
|
|
263 |
|
|
if (jaxbExpConfig.isDoTypeDesignations() == true) {
|
264 |
|
|
logger.info("# TypeDesignations");
|
265 |
|
|
dataSet.addTypeDesignations(getNameService().getAllTypeDesignations(MAX_ROWS, 0));
|
266 |
|
|
}
|
267 |
|
|
|
268 |
|
|
|
269 |
|
|
|
270 |
|
|
if (jaxbExpConfig.isDoMedia() == true) {
|
271 |
|
|
if (mediaRows == 0) { mediaRows = MAX_ROWS; }
|
272 |
|
|
logger.info("# Media");
|
273 |
|
|
dataSet.setMedia(getMediaService().list(null,mediaRows, 0,null,null));
|
274 |
|
|
// dataSet.addMedia(getMediaService().getAllMediaRepresentations(mediaRows, 0));
|
275 |
|
|
// dataSet.addMedia(getMediaService().getAllMediaRepresentationParts(mediaRows, 0));
|
276 |
|
|
}
|
277 |
|
|
|
278 |
|
|
if (jaxbExpConfig.isDoFeatureData() == true) {
|
279 |
|
|
if (featureDataRows == 0) { featureDataRows = MAX_ROWS; }
|
280 |
|
|
logger.info("# Feature Tree, Feature Node");
|
281 |
|
|
List<FeatureTree> featureTrees = new ArrayList<FeatureTree>();
|
282 |
|
|
featureTrees= getFeatureTreeService().list(null,featureDataRows, 0, null, null);
|
283 |
|
|
List<FeatureTree> taxTreesdeproxy = new ArrayList<FeatureTree>();
|
284 |
|
|
for (FeatureTree featureTree : featureTrees){
|
285 |
|
|
HibernateProxyHelper.deproxy(featureTree);
|
286 |
|
|
taxTreesdeproxy.add(featureTree);
|
287 |
|
|
}
|
288 |
|
|
|
289 |
|
|
dataSet.setFeatureTrees(getFeatureTreeService().list(null,null,null,null,null));
|
290 |
|
|
}
|
291 |
|
|
if (jaxbExpConfig.isDoClassificationData() == true) {
|
292 |
|
|
if (classificationDataRows == 0) { classificationDataRows = MAX_ROWS; }
|
293 |
|
|
logger.info("# Classification");
|
294 |
|
|
|
295 |
|
|
|
296 |
|
|
List<Classification> taxTrees = new ArrayList<Classification>();
|
297 |
|
|
taxTrees= getClassificationService().list(null,classificationDataRows, 0, null, null);
|
298 |
|
|
|
299 |
|
|
List<Classification> taxTreesdeproxy = new ArrayList<Classification>();
|
300 |
|
|
for (Classification taxTree : taxTrees){
|
301 |
|
|
HibernateProxyHelper.deproxy(taxTree);
|
302 |
|
|
taxTreesdeproxy.add(taxTree);
|
303 |
|
|
}
|
304 |
|
|
List<TaxonNode> taxNodes = new ArrayList<TaxonNode>();
|
305 |
|
|
taxNodes= getClassificationService().getAllNodes();
|
306 |
|
|
List<TaxonNode> taxNodesdeproxy = new ArrayList<TaxonNode>();
|
307 |
|
|
for (TaxonNode taxNode : taxNodes){
|
308 |
|
|
HibernateProxyHelper.deproxy(taxNode);
|
309 |
|
|
taxNodesdeproxy.add(taxNode);
|
310 |
|
|
}
|
311 |
|
|
|
312 |
|
|
dataSet.setTaxonNodes(taxNodesdeproxy);
|
313 |
|
|
dataSet.setClassifications(taxTreesdeproxy );
|
314 |
|
|
}
|
315 |
|
|
//TODO: FIXME!!!!!
|
316 |
|
|
dataSet.setLanguageStrings(null);
|
317 |
|
|
}
|
318 |
|
|
|
319 |
|
|
|
320 |
|
|
@Override
|
321 |
|
|
protected boolean doCheck(JaxbExportState state) {
|
322 |
|
|
boolean result = true;
|
323 |
|
|
logger.warn("No check implemented for Jaxb export");
|
324 |
|
|
return result;
|
325 |
|
|
}
|
326 |
|
|
|
327 |
|
|
|
328 |
|
|
@Override
|
329 |
|
|
protected boolean isIgnore(JaxbExportState state) {
|
330 |
|
|
return false;
|
331 |
|
|
}
|
332 |
|
|
|
333 |
03ebde9e
|
Katja Luther
|
|
334 |
|
|
|
335 |
ac5020e6
|
Andreas Müller
|
}
|