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.ByteArrayOutputStream;
|
13
|
import java.io.File;
|
14
|
import java.io.FileNotFoundException;
|
15
|
import java.io.FileOutputStream;
|
16
|
import java.io.IOException;
|
17
|
import java.io.OutputStreamWriter;
|
18
|
import java.io.PrintWriter;
|
19
|
import java.util.List;
|
20
|
|
21
|
import org.apache.log4j.Logger;
|
22
|
import org.springframework.stereotype.Component;
|
23
|
import org.springframework.transaction.TransactionStatus;
|
24
|
import org.springframework.util.ResourceUtils;
|
25
|
|
26
|
import eu.etaxonomy.cdm.api.service.IMediaService;
|
27
|
import eu.etaxonomy.cdm.api.service.ITermService;
|
28
|
import eu.etaxonomy.cdm.io.common.CdmExportBase;
|
29
|
import eu.etaxonomy.cdm.io.common.ICdmExport;
|
30
|
import eu.etaxonomy.cdm.io.common.IExportConfigurator;
|
31
|
import eu.etaxonomy.cdm.io.common.mapping.out.IExportTransformer;
|
32
|
import eu.etaxonomy.cdm.model.agent.AgentBase;
|
33
|
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
|
34
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
35
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
36
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
37
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
38
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
39
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
40
|
|
41
|
/**
|
42
|
* @author h.fradin (from a.babadshanjan)
|
43
|
* @since 10.12.2008
|
44
|
*/
|
45
|
@Component("sddCdmExporter")
|
46
|
public class SDDCdmExporter
|
47
|
extends CdmExportBase<SDDExportConfigurator, SDDExportState, IExportTransformer, File>
|
48
|
implements ICdmExport<SDDExportConfigurator, SDDExportState> {
|
49
|
// public class JaxbExport extends CdmIoBase implements ICdmIoExport {
|
50
|
// TODO: public class JaxbExport extends CdmIoBase implements ICdmIO {
|
51
|
|
52
|
private static final long serialVersionUID = 6294052197732385842L;
|
53
|
private static final Logger logger = Logger.getLogger(SDDCdmExporter.class);
|
54
|
private SDDDocumentBuilder sddDocumentBuilder = null;
|
55
|
|
56
|
/**
|
57
|
*
|
58
|
*/
|
59
|
public SDDCdmExporter() {
|
60
|
super();
|
61
|
this.ioName = this.getClass().getSimpleName();
|
62
|
}
|
63
|
|
64
|
/** Retrieves data from a CDM DB and serializes them CDM to XML.
|
65
|
* Starts with root taxa and traverses the classification to retrieve children taxa, synonyms and relationships.
|
66
|
* Taxa that are not part of the classification are not found.
|
67
|
*
|
68
|
* @param exImpConfig
|
69
|
* @param dbname
|
70
|
* @param filename
|
71
|
*/
|
72
|
@Override
|
73
|
public void doInvoke(SDDExportState state){
|
74
|
|
75
|
SDDExportConfigurator sddExpConfig = state.getConfig();
|
76
|
|
77
|
String dbname = sddExpConfig.getSource() != null ? sddExpConfig.getSource().getName() : "unknown";
|
78
|
String fileName = sddExpConfig.getDestinationNameString();
|
79
|
logger.warn("Serializing DB " + dbname + " to file " + fileName);
|
80
|
logger.debug("DbSchemaValidation = " + sddExpConfig.getDbSchemaValidation());
|
81
|
|
82
|
TransactionStatus txStatus = startTransaction(true);
|
83
|
SDDDataSet dataSet = new SDDDataSet();
|
84
|
List<Taxon> taxa = null;
|
85
|
List<DefinedTermBase> terms = null;
|
86
|
|
87
|
// get data from DB
|
88
|
|
89
|
try {
|
90
|
logger.info("Retrieving data from DB");
|
91
|
|
92
|
retrieveData(sddExpConfig, dataSet);
|
93
|
|
94
|
} catch (Exception e) {
|
95
|
logger.error("Error retrieving data");
|
96
|
e.printStackTrace();
|
97
|
}
|
98
|
|
99
|
File fy = new File(fileName);
|
100
|
|
101
|
|
102
|
if ( fy.exists() )
|
103
|
{
|
104
|
logger.warn("LORNA FILE EXISTS");
|
105
|
|
106
|
if(fy.canWrite()) {
|
107
|
|
108
|
try {
|
109
|
logger.warn("LORNA FILE PATH" + fy.getCanonicalPath());
|
110
|
logger.warn("LORNA ABS PATH" + fy.getAbsolutePath());
|
111
|
|
112
|
} catch (IOException e) {
|
113
|
// TODO Auto-generated catch block
|
114
|
e.printStackTrace();
|
115
|
}
|
116
|
}
|
117
|
} else {
|
118
|
|
119
|
logger.warn("FILE DOESNT EXIST " + fy.getAbsoluteFile().exists());
|
120
|
logger.warn("LORNA FILE DOESNT EXIST");
|
121
|
try {
|
122
|
logger.warn("LORNA FILE DOESNT EXIST" + ResourceUtils.getFile(fileName));
|
123
|
} catch (FileNotFoundException e) {
|
124
|
// TODO Auto-generated catch block
|
125
|
e.printStackTrace();
|
126
|
}
|
127
|
}
|
128
|
|
129
|
logger.warn("All data retrieved");
|
130
|
|
131
|
try {
|
132
|
sddDocumentBuilder = new SDDDocumentBuilder();
|
133
|
switch(sddExpConfig.getTarget()) {
|
134
|
case FILE:
|
135
|
|
136
|
logger.warn("LORNA the filename is " + fileName);
|
137
|
File f = new File(fileName);
|
138
|
// File f = new File(fileName);
|
139
|
//FileOutputStream fos = new FileOutputStream(f);
|
140
|
FileOutputStream fos = new FileOutputStream(ResourceUtils.getFile(fileName));
|
141
|
logger.warn("Created fos");
|
142
|
PrintWriter writer = new PrintWriter(new OutputStreamWriter(fos, "UTF8"), true);
|
143
|
//sddDocumentBuilder.marshal(dataSet, f);//lorna
|
144
|
sddDocumentBuilder.marshal(dataSet, fileName);
|
145
|
|
146
|
// TODO: Split into one file per data set member to see whether performance improves?
|
147
|
|
148
|
logger.info("XML file written");
|
149
|
logger.info("Filename is: " + fileName);
|
150
|
break;
|
151
|
case EXPORT_DATA:
|
152
|
exportStream = new ByteArrayOutputStream();
|
153
|
sddDocumentBuilder.marshal(dataSet, exportStream);
|
154
|
state.getResult().addExportData(getByteArray());
|
155
|
break;
|
156
|
default:
|
157
|
break;
|
158
|
}
|
159
|
|
160
|
} catch (Exception e) {
|
161
|
logger.warn("LORNA the filename is " + fileName);
|
162
|
logger.error("Marshalling error");
|
163
|
e.printStackTrace();
|
164
|
}
|
165
|
commitTransaction(txStatus);
|
166
|
|
167
|
return;
|
168
|
|
169
|
}
|
170
|
|
171
|
|
172
|
private void retrieveData (IExportConfigurator config, SDDDataSet sddDataSet) {
|
173
|
|
174
|
SDDExportConfigurator sddExpConfig = (SDDExportConfigurator)config;
|
175
|
final int MAX_ROWS = 50000;
|
176
|
int numberOfRows = sddExpConfig.getMaxRows();
|
177
|
// TODO:
|
178
|
//CdmApplicationController appCtr = config.getCdmAppController(false, true);
|
179
|
|
180
|
int agentRows = numberOfRows;
|
181
|
int definedTermBaseRows = numberOfRows;
|
182
|
int referenceBaseRows = numberOfRows;
|
183
|
int taxonNameRows = numberOfRows;
|
184
|
int taxonBaseRows = numberOfRows;
|
185
|
int relationshipRows = numberOfRows;
|
186
|
int occurrencesRows = numberOfRows;
|
187
|
int mediaRows = numberOfRows;
|
188
|
int featureDataRows = numberOfRows;
|
189
|
int languageDataRows = numberOfRows;
|
190
|
int termVocabularyRows = numberOfRows;
|
191
|
int homotypicalGroupRows = numberOfRows;
|
192
|
|
193
|
logger.warn("LORNA no of rows: " + numberOfRows);
|
194
|
|
195
|
|
196
|
if (sddExpConfig.isDoTermVocabularies() == true) {
|
197
|
if (termVocabularyRows == 0) { termVocabularyRows = MAX_ROWS; }
|
198
|
logger.info("# TermVocabulary");
|
199
|
sddDataSet.setTermVocabularies((List)getVocabularyService().list(null,MAX_ROWS, 0,null,null));;
|
200
|
}
|
201
|
|
202
|
if (sddExpConfig.isDoLanguageData() == true) {
|
203
|
if (languageDataRows == 0) { languageDataRows = MAX_ROWS; }
|
204
|
logger.info("# Representation, Language String");
|
205
|
sddDataSet.setLanguageData(getTermService().getAllRepresentations(MAX_ROWS, 0));
|
206
|
sddDataSet.addLanguageData(getTermService().getAllLanguageStrings(MAX_ROWS, 0));
|
207
|
}
|
208
|
|
209
|
if (sddExpConfig.isDoTerms() == true) {
|
210
|
if (definedTermBaseRows == 0) {
|
211
|
definedTermBaseRows = getTermService().count(DefinedTermBase.class);
|
212
|
logger.warn("LORNA defined term base rows: " + definedTermBaseRows);
|
213
|
}
|
214
|
logger.info("# DefinedTermBase: " + definedTermBaseRows);
|
215
|
ITermService termService = getTermService();
|
216
|
List<DefinedTermBase> list = termService.list(null,definedTermBaseRows, 0,null,null);
|
217
|
sddDataSet.setTerms(list);
|
218
|
//sddDataSet.setTerms(getTermService().list(null,definedTermBaseRows, 0,null,null));
|
219
|
//sddDataSet.setTerms(getTermService().list(null,definedTermBaseRows, 0,null,null));
|
220
|
}
|
221
|
|
222
|
if (sddExpConfig.isDoAuthors() == true) {
|
223
|
if (agentRows == 0) { agentRows = getAgentService().count(AgentBase.class); }
|
224
|
logger.info("# Agents: " + agentRows);
|
225
|
//logger.info(" # Team: " + getAgentService().count(Team.class));
|
226
|
sddDataSet.setAgents(getAgentService().list(null,agentRows, 0,null,null));
|
227
|
}
|
228
|
|
229
|
if (sddExpConfig.getDoReferences() != IExportConfigurator.DO_REFERENCES.NONE) {
|
230
|
if (referenceBaseRows == 0) { referenceBaseRows = getReferenceService().count(Reference.class); }
|
231
|
logger.info("# Reference: " + referenceBaseRows);
|
232
|
sddDataSet.setReferences(getReferenceService().list(null,referenceBaseRows, 0,null,null));
|
233
|
}
|
234
|
|
235
|
if (sddExpConfig.isDoTaxonNames() == true) {
|
236
|
if (taxonNameRows == 0) { taxonNameRows = getNameService().count(TaxonName.class); }
|
237
|
logger.info("# TaxonName: " + taxonNameRows);
|
238
|
//logger.info(" # Taxon: " + getNameService().count(BotanicalName.class));
|
239
|
sddDataSet.setTaxonomicNames(getNameService().list(null,taxonNameRows, 0,null,null));
|
240
|
}
|
241
|
|
242
|
if (sddExpConfig.isDoHomotypicalGroups() == true) {
|
243
|
if (homotypicalGroupRows == 0) { homotypicalGroupRows = MAX_ROWS; }
|
244
|
logger.info("# Homotypical Groups");
|
245
|
sddDataSet.setHomotypicalGroups(getNameService().getAllHomotypicalGroups(homotypicalGroupRows, 0));
|
246
|
}
|
247
|
|
248
|
if (sddExpConfig.isDoTaxa() == true) {
|
249
|
if (taxonBaseRows == 0) { taxonBaseRows = getTaxonService().count(TaxonBase.class); }
|
250
|
logger.info("# TaxonBase: " + taxonBaseRows);
|
251
|
// dataSet.setTaxa(new ArrayList<Taxon>());
|
252
|
// dataSet.setSynonyms(new ArrayList<Synonym>());
|
253
|
List<TaxonBase> tb = getTaxonService().list(null,taxonBaseRows, 0,null,null);
|
254
|
for (TaxonBase taxonBase : tb) {
|
255
|
if (taxonBase instanceof Taxon) {
|
256
|
sddDataSet.addTaxon((Taxon)taxonBase);
|
257
|
} else if (taxonBase instanceof Synonym) {
|
258
|
sddDataSet.addSynonym((Synonym)taxonBase);
|
259
|
} else {
|
260
|
logger.error("entry of wrong type: " + taxonBase.toString());
|
261
|
}
|
262
|
}
|
263
|
}
|
264
|
|
265
|
// TODO:
|
266
|
// retrieve taxa and synonyms separately
|
267
|
// need correct count for taxa and synonyms
|
268
|
// if (taxonBaseRows == 0) { taxonBaseRows = getTaxonService().count(TaxonBase.class); }
|
269
|
// logger.info("# Synonym: " + taxonBaseRows);
|
270
|
// dataSet.setSynonyms(new ArrayList<Synonym>());
|
271
|
// dataSet.setSynonyms(getTaxonService().getAllSynonyms(taxonBaseRows, 0));
|
272
|
//
|
273
|
// if (sddExpConfig.isDoRelTaxa() == true) {
|
274
|
// if (relationshipRows == 0) { relationshipRows = MAX_ROWS; }
|
275
|
// logger.info("# Relationships");
|
276
|
// List<RelationshipBase> relationList = getTaxonService()..listTaxonRelationships(relationshipRows, 0);
|
277
|
// Set<RelationshipBase> relationSet = new HashSet<>(relationList);
|
278
|
// sddDataSet.setRelationships(relationSet);
|
279
|
// }
|
280
|
|
281
|
if (sddExpConfig.isDoReferencedEntities() == true) {
|
282
|
logger.info("# Referenced Entities");
|
283
|
sddDataSet.setReferencedEntities(getNameService().getAllNomenclaturalStatus(MAX_ROWS, 0));
|
284
|
sddDataSet.addSourcedEntities(getNameService().getAllTypeDesignations(MAX_ROWS, 0));
|
285
|
}
|
286
|
|
287
|
if (sddExpConfig.isDoOccurrence() == true) {
|
288
|
if (occurrencesRows == 0) { occurrencesRows = getOccurrenceService().count(SpecimenOrObservationBase.class); }
|
289
|
logger.info("# SpecimenOrObservationBase: " + occurrencesRows);
|
290
|
sddDataSet.setOccurrences(getOccurrenceService().list(null,occurrencesRows,0, null,null));
|
291
|
}
|
292
|
|
293
|
if (sddExpConfig.isDoMedia() == true) {
|
294
|
if (mediaRows == 0) { mediaRows = MAX_ROWS; }
|
295
|
logger.info("# Media");
|
296
|
IMediaService mservice = getMediaService();
|
297
|
|
298
|
/*List<Media> l = mservice.list(null,mediaRows, 0,null,null);
|
299
|
for (Media med : l) {
|
300
|
logger.debug("The Media object " + med);// + med.getTitle().getLanguageLabel());
|
301
|
//logger.warn("LORNA " + med.getTitle().getText());
|
302
|
}*/
|
303
|
|
304
|
sddDataSet.setMedia(getMediaService().list(null,mediaRows, 0,null,null));
|
305
|
// dataSet.addMedia(getMediaService().getAllMediaRepresentations(mediaRows, 0));
|
306
|
// dataSet.addMedia(getMediaService().getAllMediaRepresentationParts(mediaRows, 0));
|
307
|
}
|
308
|
|
309
|
if (sddExpConfig.isDoFeatureData() == true) {
|
310
|
if (featureDataRows == 0) { featureDataRows = MAX_ROWS; }
|
311
|
logger.info("# Feature Tree, Feature Node");
|
312
|
sddDataSet.setFeatureData(getFeatureTreeService().getFeatureNodesAll());
|
313
|
sddDataSet.addFeatureData(getFeatureTreeService().list(null,null,null,null,null));
|
314
|
}
|
315
|
}
|
316
|
|
317
|
|
318
|
@Override
|
319
|
protected boolean doCheck(SDDExportState state) {
|
320
|
boolean result = true;
|
321
|
logger.warn("No check implemented for Jaxb export");
|
322
|
return result;
|
323
|
}
|
324
|
|
325
|
|
326
|
@Override
|
327
|
protected boolean isIgnore(SDDExportState state) {
|
328
|
return false;
|
329
|
}
|
330
|
|
331
|
|
332
|
|
333
|
|
334
|
}
|