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