1
|
/**
|
2
|
* Copyright (C) 2007 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.sdd.out;
|
11
|
|
12
|
import java.io.File;
|
13
|
import java.io.FileOutputStream;
|
14
|
import java.io.OutputStreamWriter;
|
15
|
import java.io.PrintWriter;
|
16
|
import java.util.HashSet;
|
17
|
import java.util.List;
|
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.CdmExportBase;
|
25
|
import eu.etaxonomy.cdm.io.common.ICdmExport;
|
26
|
import eu.etaxonomy.cdm.io.common.IExportConfigurator;
|
27
|
import eu.etaxonomy.cdm.model.agent.AgentBase;
|
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.Reference;
|
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 h.fradin (from a.babadshanjan)
|
39
|
* @created 10.12.2008
|
40
|
* @versoin 1.0
|
41
|
*/
|
42
|
@Component("sddCdmExporter")
|
43
|
public class SDDCdmExporter extends CdmExportBase<SDDExportConfigurator, SDDExportState> implements ICdmExport<SDDExportConfigurator, SDDExportState> {
|
44
|
// public class JaxbExport extends CdmIoBase implements ICdmIoExport {
|
45
|
// TODO: public class JaxbExport extends CdmIoBase implements ICdmIO {
|
46
|
|
47
|
private static final Logger logger = Logger.getLogger(SDDCdmExporter.class);
|
48
|
private SDDDocumentBuilder sddDocumentBuilder = null;
|
49
|
|
50
|
private String ioName = null;
|
51
|
|
52
|
|
53
|
/**
|
54
|
*
|
55
|
*/
|
56
|
public SDDCdmExporter() {
|
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 classification to retrieve children taxa, synonyms and relationships.
|
63
|
* Taxa that are not part of the classification are not found.
|
64
|
*
|
65
|
* @param exImpConfig
|
66
|
* @param dbname
|
67
|
* @param filename
|
68
|
*/
|
69
|
@Override
|
70
|
protected void doInvoke(SDDExportState state){
|
71
|
// protected boolean doInvoke(IExportConfigurator config,
|
72
|
// Map<String, MapWrapper<? extends CdmBase>> stores) {
|
73
|
|
74
|
SDDExportConfigurator sddExpConfig = state.getConfig();
|
75
|
|
76
|
String dbname = sddExpConfig.getSource() != null ? sddExpConfig.getSource().getName() : "unknown";
|
77
|
String fileName = sddExpConfig.getDestinationNameString();
|
78
|
logger.info("Serializing DB " + dbname + " to file " + fileName);
|
79
|
logger.debug("DbSchemaValidation = " + sddExpConfig.getDbSchemaValidation());
|
80
|
|
81
|
TransactionStatus txStatus = startTransaction(true);
|
82
|
SDDDataSet dataSet = new SDDDataSet();
|
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(sddExpConfig, 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
|
sddDocumentBuilder = new SDDDocumentBuilder();
|
102
|
File f = new File(fileName);
|
103
|
// File f = new File(fileName);
|
104
|
FileOutputStream fos = new FileOutputStream(f);
|
105
|
PrintWriter writer = new PrintWriter(new OutputStreamWriter(fos, "UTF8"), true);
|
106
|
sddDocumentBuilder.marshal(dataSet, f);
|
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;
|
120
|
|
121
|
}
|
122
|
|
123
|
|
124
|
private void retrieveData (IExportConfigurator config, SDDDataSet sddDataSet) {
|
125
|
|
126
|
SDDExportConfigurator sddExpConfig = (SDDExportConfigurator)config;
|
127
|
final int MAX_ROWS = 50000;
|
128
|
int numberOfRows = sddExpConfig.getMaxRows();
|
129
|
// TODO:
|
130
|
//CdmApplicationController appCtr = config.getCdmAppController(false, true);
|
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 languageDataRows = numberOfRows;
|
142
|
int termVocabularyRows = numberOfRows;
|
143
|
int homotypicalGroupRows = numberOfRows;
|
144
|
|
145
|
if (sddExpConfig.isDoTermVocabularies() == true) {
|
146
|
if (termVocabularyRows == 0) { termVocabularyRows = MAX_ROWS; }
|
147
|
logger.info("# TermVocabulary");
|
148
|
sddDataSet.setTermVocabularies((List)getVocabularyService().list(null,MAX_ROWS, 0,null,null));;
|
149
|
}
|
150
|
|
151
|
if (sddExpConfig.isDoLanguageData() == true) {
|
152
|
if (languageDataRows == 0) { languageDataRows = MAX_ROWS; }
|
153
|
logger.info("# Representation, Language String");
|
154
|
sddDataSet.setLanguageData(getTermService().getAllRepresentations(MAX_ROWS, 0));
|
155
|
sddDataSet.addLanguageData(getTermService().getAllLanguageStrings(MAX_ROWS, 0));
|
156
|
}
|
157
|
|
158
|
if (sddExpConfig.isDoTerms() == true) {
|
159
|
if (definedTermBaseRows == 0) { definedTermBaseRows = getTermService().count(DefinedTermBase.class); }
|
160
|
logger.info("# DefinedTermBase: " + definedTermBaseRows);
|
161
|
sddDataSet.setTerms(getTermService().list(null,definedTermBaseRows, 0,null,null));
|
162
|
}
|
163
|
|
164
|
if (sddExpConfig.isDoAuthors() == true) {
|
165
|
if (agentRows == 0) { agentRows = getAgentService().count(AgentBase.class); }
|
166
|
logger.info("# Agents: " + agentRows);
|
167
|
//logger.info(" # Team: " + getAgentService().count(Team.class));
|
168
|
sddDataSet.setAgents(getAgentService().list(null,agentRows, 0,null,null));
|
169
|
}
|
170
|
|
171
|
if (sddExpConfig.getDoReferences() != IExportConfigurator.DO_REFERENCES.NONE) {
|
172
|
if (referenceBaseRows == 0) { referenceBaseRows = getReferenceService().count(Reference.class); }
|
173
|
logger.info("# Reference: " + referenceBaseRows);
|
174
|
sddDataSet.setReferences(getReferenceService().list(null,referenceBaseRows, 0,null,null));
|
175
|
}
|
176
|
|
177
|
if (sddExpConfig.isDoTaxonNames() == true) {
|
178
|
if (taxonNameBaseRows == 0) { taxonNameBaseRows = getNameService().count(TaxonNameBase.class); }
|
179
|
logger.info("# TaxonNameBase: " + taxonNameBaseRows);
|
180
|
//logger.info(" # Taxon: " + getNameService().count(BotanicalName.class));
|
181
|
sddDataSet.setTaxonomicNames(getNameService().list(null,taxonNameBaseRows, 0,null,null));
|
182
|
}
|
183
|
|
184
|
if (sddExpConfig.isDoHomotypicalGroups() == true) {
|
185
|
if (homotypicalGroupRows == 0) { homotypicalGroupRows = MAX_ROWS; }
|
186
|
logger.info("# Homotypical Groups");
|
187
|
sddDataSet.setHomotypicalGroups(getNameService().getAllHomotypicalGroups(homotypicalGroupRows, 0));
|
188
|
}
|
189
|
|
190
|
if (sddExpConfig.isDoTaxa() == true) {
|
191
|
if (taxonBaseRows == 0) { taxonBaseRows = getTaxonService().count(TaxonBase.class); }
|
192
|
logger.info("# TaxonBase: " + taxonBaseRows);
|
193
|
// dataSet.setTaxa(new ArrayList<Taxon>());
|
194
|
// dataSet.setSynonyms(new ArrayList<Synonym>());
|
195
|
List<TaxonBase> tb = getTaxonService().list(null,taxonBaseRows, 0,null,null);
|
196
|
for (TaxonBase taxonBase : tb) {
|
197
|
if (taxonBase instanceof Taxon) {
|
198
|
sddDataSet.addTaxon((Taxon)taxonBase);
|
199
|
} else if (taxonBase instanceof Synonym) {
|
200
|
sddDataSet.addSynonym((Synonym)taxonBase);
|
201
|
} else {
|
202
|
logger.error("entry of wrong type: " + taxonBase.toString());
|
203
|
}
|
204
|
}
|
205
|
}
|
206
|
|
207
|
// TODO:
|
208
|
// retrieve taxa and synonyms separately
|
209
|
// need correct count for taxa and synonyms
|
210
|
// if (taxonBaseRows == 0) { taxonBaseRows = getTaxonService().count(TaxonBase.class); }
|
211
|
// logger.info("# Synonym: " + taxonBaseRows);
|
212
|
// dataSet.setSynonyms(new ArrayList<Synonym>());
|
213
|
// dataSet.setSynonyms(getTaxonService().getAllSynonyms(taxonBaseRows, 0));
|
214
|
|
215
|
if (sddExpConfig.isDoRelTaxa() == true) {
|
216
|
if (relationshipRows == 0) { relationshipRows = MAX_ROWS; }
|
217
|
logger.info("# Relationships");
|
218
|
List<RelationshipBase> relationList = getTaxonService().getAllRelationships(relationshipRows, 0);
|
219
|
Set<RelationshipBase> relationSet = new HashSet<RelationshipBase>(relationList);
|
220
|
sddDataSet.setRelationships(relationSet);
|
221
|
}
|
222
|
|
223
|
if (sddExpConfig.isDoReferencedEntities() == true) {
|
224
|
logger.info("# Referenced Entities");
|
225
|
sddDataSet.setReferencedEntities(getNameService().getAllNomenclaturalStatus(MAX_ROWS, 0));
|
226
|
sddDataSet.addReferencedEntities(getNameService().getAllTypeDesignations(MAX_ROWS, 0));
|
227
|
}
|
228
|
|
229
|
if (sddExpConfig.isDoOccurrence() == true) {
|
230
|
if (occurrencesRows == 0) { occurrencesRows = getOccurrenceService().count(SpecimenOrObservationBase.class); }
|
231
|
logger.info("# SpecimenOrObservationBase: " + occurrencesRows);
|
232
|
sddDataSet.setOccurrences(getOccurrenceService().list(null,occurrencesRows,0, null,null));
|
233
|
}
|
234
|
|
235
|
if (sddExpConfig.isDoMedia() == true) {
|
236
|
if (mediaRows == 0) { mediaRows = MAX_ROWS; }
|
237
|
logger.info("# Media");
|
238
|
sddDataSet.setMedia(getMediaService().list(null,mediaRows, 0,null,null));
|
239
|
// dataSet.addMedia(getMediaService().getAllMediaRepresentations(mediaRows, 0));
|
240
|
// dataSet.addMedia(getMediaService().getAllMediaRepresentationParts(mediaRows, 0));
|
241
|
}
|
242
|
|
243
|
if (sddExpConfig.isDoFeatureData() == true) {
|
244
|
if (featureDataRows == 0) { featureDataRows = MAX_ROWS; }
|
245
|
logger.info("# Feature Tree, Feature Node");
|
246
|
sddDataSet.setFeatureData(getFeatureTreeService().getFeatureNodesAll());
|
247
|
sddDataSet.addFeatureData(getFeatureTreeService().list(null,null,null,null,null));
|
248
|
}
|
249
|
}
|
250
|
|
251
|
|
252
|
@Override
|
253
|
protected boolean doCheck(SDDExportState state) {
|
254
|
boolean result = true;
|
255
|
logger.warn("No check implemented for Jaxb export");
|
256
|
return result;
|
257
|
}
|
258
|
|
259
|
|
260
|
@Override
|
261
|
protected boolean isIgnore(SDDExportState state) {
|
262
|
return false;
|
263
|
}
|
264
|
|
265
|
}
|