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.csv.redlist.out;
|
11
|
|
12
|
import java.io.ByteArrayOutputStream;
|
13
|
import java.io.PrintWriter;
|
14
|
import java.util.ArrayList;
|
15
|
import java.util.Collections;
|
16
|
import java.util.Comparator;
|
17
|
import java.util.HashSet;
|
18
|
import java.util.List;
|
19
|
import java.util.Set;
|
20
|
import java.util.UUID;
|
21
|
|
22
|
import org.apache.log4j.Logger;
|
23
|
import org.springframework.stereotype.Component;
|
24
|
import org.springframework.transaction.TransactionStatus;
|
25
|
|
26
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
27
|
import eu.etaxonomy.cdm.model.common.Language;
|
28
|
import eu.etaxonomy.cdm.model.common.RelationshipTermBase;
|
29
|
import eu.etaxonomy.cdm.model.description.CategoricalData;
|
30
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
31
|
import eu.etaxonomy.cdm.model.description.Distribution;
|
32
|
import eu.etaxonomy.cdm.model.description.Feature;
|
33
|
import eu.etaxonomy.cdm.model.description.State;
|
34
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
35
|
import eu.etaxonomy.cdm.model.description.TextData;
|
36
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
37
|
import eu.etaxonomy.cdm.model.name.INonViralName;
|
38
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
39
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
40
|
import eu.etaxonomy.cdm.model.taxon.SynonymType;
|
41
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
42
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
43
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
44
|
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
|
45
|
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
|
46
|
|
47
|
|
48
|
/**
|
49
|
* @author a.oppermann
|
50
|
* @since 18.10.2012
|
51
|
*/
|
52
|
|
53
|
@Component
|
54
|
public class CsvTaxExportRedlist extends CsvExportBaseRedlist {
|
55
|
private static final long serialVersionUID = 841703025922543361L;
|
56
|
|
57
|
private static final Logger logger = Logger.getLogger(CsvTaxExportRedlist.class);
|
58
|
|
59
|
private static final String ROW_TYPE = "http://rs.tdwg.org/dwc/terms/Taxon";
|
60
|
private static final String fileName = "RedlistCoreTax.csv";
|
61
|
|
62
|
public CsvTaxExportRedlist() {
|
63
|
super();
|
64
|
this.ioName = this.getClass().getSimpleName();
|
65
|
}
|
66
|
|
67
|
|
68
|
/** Retrieves data from a CDM DB and serializes them CDM to CSV.
|
69
|
* Starts with root taxa and traverses the classification to retrieve
|
70
|
* children taxa, synonyms, relationships, descriptive data, red list
|
71
|
* status (features).
|
72
|
* Taxa that are not part of the classification are not found.
|
73
|
*
|
74
|
* @param exImpConfig
|
75
|
* @param dbname
|
76
|
* @param filename
|
77
|
*/
|
78
|
@Override
|
79
|
protected void doInvoke(CsvTaxExportStateRedlist state){
|
80
|
CsvTaxExportConfiguratorRedlist config = state.getConfig();
|
81
|
TransactionStatus txStatus = startTransaction(true);
|
82
|
List<NamedArea> selectedAreas = config.getNamedAreas();
|
83
|
Set<TaxonNode> taxonNodes = assembleTaxonNodeSet(config);
|
84
|
|
85
|
PrintWriter writer = null;
|
86
|
ByteArrayOutputStream byteArrayOutputStream;
|
87
|
try {
|
88
|
byteArrayOutputStream = config.getByteArrayOutputStream();
|
89
|
writer = new PrintWriter(byteArrayOutputStream);
|
90
|
//geographical Filter
|
91
|
List<TaxonNode> filteredNodes = handleGeographicalFilter(selectedAreas, taxonNodes);
|
92
|
|
93
|
//sorting List
|
94
|
Collections.sort(filteredNodes, new Comparator<TaxonNode>() {
|
95
|
|
96
|
@Override
|
97
|
public int compare(TaxonNode tn1, TaxonNode tn2) {
|
98
|
Taxon taxon1 = tn1.getTaxon();
|
99
|
Taxon taxon2 = tn2.getTaxon();
|
100
|
if(taxon1 != null && taxon2 != null){
|
101
|
return taxon1.getTitleCache().compareTo(taxon2.getTitleCache());
|
102
|
}
|
103
|
else{
|
104
|
return 0;
|
105
|
}
|
106
|
}
|
107
|
});
|
108
|
for (TaxonNode node : filteredNodes){
|
109
|
Taxon taxon = CdmBase.deproxy(node.getTaxon(), Taxon.class);
|
110
|
CsvTaxRecordRedlist record = assembleRecord(state);
|
111
|
INonViralName name = taxon.getName();
|
112
|
Classification classification = node.getClassification();
|
113
|
config.setClassificationTitleCache(classification.getTitleCache());
|
114
|
if (! this.recordExists(taxon)){
|
115
|
handleTaxonBase(record, taxon, name, taxon, classification, null, false, false, config);
|
116
|
record.write(writer);
|
117
|
this.addExistingRecord(taxon);
|
118
|
}
|
119
|
//misapplied names
|
120
|
handleMisapplication(taxon, writer, classification, record, config);
|
121
|
writer.flush();
|
122
|
}
|
123
|
} catch (ClassCastException e) {
|
124
|
e.printStackTrace();
|
125
|
}
|
126
|
finally{
|
127
|
if(writer!=null){
|
128
|
writer.close();
|
129
|
}
|
130
|
this.clearExistingRecordIds();
|
131
|
}
|
132
|
commitTransaction(txStatus);
|
133
|
return;
|
134
|
|
135
|
}
|
136
|
|
137
|
|
138
|
//TODO: Exception handling
|
139
|
protected Set<TaxonNode> assembleTaxonNodeSet(CsvTaxExportConfiguratorRedlist config){
|
140
|
Set<TaxonNode> taxonNodes = new HashSet<>();
|
141
|
if(config != null){
|
142
|
List<UUID> taxonNodeUuidSet = new ArrayList<>(config.getTaxonNodeUuids());
|
143
|
List<TaxonNode> loadedNodes = getTaxonNodeService().load(taxonNodeUuidSet, null);
|
144
|
taxonNodes.addAll(loadedNodes);
|
145
|
}
|
146
|
return taxonNodes;
|
147
|
}
|
148
|
|
149
|
//TODO: Exception handling
|
150
|
private CsvTaxRecordRedlist assembleRecord(CsvTaxExportStateRedlist state) {
|
151
|
if(state!=null){
|
152
|
CsvTaxExportConfiguratorRedlist config = state.getConfig();
|
153
|
CsvMetaDataRecordRedlist metaRecord = new CsvMetaDataRecordRedlist(true, fileName, ROW_TYPE);
|
154
|
state.addMetaRecord(metaRecord);
|
155
|
CsvTaxRecordRedlist record = new CsvTaxRecordRedlist(metaRecord, config);
|
156
|
return record;
|
157
|
}
|
158
|
return null;
|
159
|
}
|
160
|
|
161
|
/**
|
162
|
* Takes positive List of areas and iterates over a given taxon node
|
163
|
* and their {@link Taxon} to return all {@link Taxon} with the desired
|
164
|
* geographical attribute.
|
165
|
*
|
166
|
* <p><p>
|
167
|
*
|
168
|
* If selectedAreas is null all child {@link TaxonNode}s of the given taxon node will be returned.
|
169
|
*
|
170
|
* @param selectedAreas
|
171
|
* @param taxonNodes
|
172
|
* @return
|
173
|
*/
|
174
|
protected List<TaxonNode> handleGeographicalFilter(List<NamedArea> selectedAreas,
|
175
|
Set<TaxonNode> taxonNodes) {
|
176
|
List<TaxonNode> filteredNodes = new ArrayList<TaxonNode>();
|
177
|
List<TaxonNode> allNodes = new ArrayList<TaxonNode>();
|
178
|
for (TaxonNode node : taxonNodes){
|
179
|
if(node.getTaxon()!=null){
|
180
|
allNodes.add(node);
|
181
|
}
|
182
|
allNodes.addAll(getTaxonNodeService().loadChildNodesOfTaxonNode(node, null, true, null));
|
183
|
}
|
184
|
//Geographical filter
|
185
|
if(selectedAreas != null && !selectedAreas.isEmpty() && selectedAreas.size() < 16){
|
186
|
for (TaxonNode node : allNodes){
|
187
|
Taxon taxon = CdmBase.deproxy(node.getTaxon(), Taxon.class);
|
188
|
Set<TaxonDescription> descriptions = taxon.getDescriptions();
|
189
|
for (TaxonDescription description : descriptions){
|
190
|
for (DescriptionElementBase el : description.getElements()){
|
191
|
if (el.isInstanceOf(Distribution.class) ){
|
192
|
Distribution distribution = CdmBase.deproxy(el, Distribution.class);
|
193
|
NamedArea area = distribution.getArea();
|
194
|
for(NamedArea selectedArea:selectedAreas){
|
195
|
if(selectedArea.getUuid().equals(area.getUuid())){
|
196
|
filteredNodes.add(node);
|
197
|
}
|
198
|
}
|
199
|
}
|
200
|
}
|
201
|
}
|
202
|
}
|
203
|
}else{
|
204
|
filteredNodes = allNodes;
|
205
|
}
|
206
|
return filteredNodes;
|
207
|
}
|
208
|
|
209
|
/**
|
210
|
* handles misapplied {@link Taxon}
|
211
|
* @param taxon
|
212
|
* @param writer
|
213
|
* @param classification
|
214
|
* @param metaRecord
|
215
|
* @param config
|
216
|
*/
|
217
|
private void handleMisapplication(Taxon taxon, PrintWriter writer, Classification classification, CsvTaxRecordRedlist record, CsvTaxExportConfiguratorRedlist config) {
|
218
|
Set<TaxonRelationship> misappliedNameRels = taxon.getMisappliedNameRelations();
|
219
|
for (TaxonRelationship misappliedNameRel : misappliedNameRels ){
|
220
|
// CsvTaxRecordRedlist record = new CsvTaxRecordRedlist(metaRecord, config);
|
221
|
Taxon misappliedName = misappliedNameRel.getFromTaxon();
|
222
|
INonViralName name = misappliedName.getName();
|
223
|
|
224
|
if (! this.recordExists(misappliedName)){
|
225
|
handleTaxonBase(record, misappliedName, name, taxon, classification,
|
226
|
misappliedNameRel.getType(), false, false, config);
|
227
|
record.write(writer);
|
228
|
this.addExistingRecord(misappliedName);
|
229
|
}
|
230
|
}
|
231
|
}
|
232
|
|
233
|
/**
|
234
|
* handles the information record for the actual {@link Taxon} including {@link Classification classification}, Taxon Name, Taxon ID,
|
235
|
* Taxon Status, Synonyms, {@link Feature features} data
|
236
|
* @param record the concrete information record
|
237
|
* @param taxonBase {@link Taxon}
|
238
|
* @param name
|
239
|
* @param acceptedTaxon
|
240
|
* @param parent
|
241
|
* @param basionym
|
242
|
* @param isPartial
|
243
|
* @param isProParte
|
244
|
* @param config
|
245
|
* @param type
|
246
|
*/
|
247
|
private void handleTaxonBase(CsvTaxRecordRedlist record,TaxonBase<?> taxonBase,
|
248
|
INonViralName name, Taxon acceptedTaxon, Classification classification,
|
249
|
RelationshipTermBase<?> relType, boolean isProParte, boolean isPartial,
|
250
|
CsvTaxExportConfiguratorRedlist config) {
|
251
|
|
252
|
List<Feature> features = config.getFeatures();
|
253
|
record.setHeadLinePrinted(config.isHasHeaderLines());
|
254
|
if(features != null) {
|
255
|
record.setPrintFeatures(features);
|
256
|
}
|
257
|
config.setHasHeaderLines(false);
|
258
|
|
259
|
record.setDatasetName(classification.getTitleCache());
|
260
|
record.setScientificName(name.getTitleCache());
|
261
|
record.setScientificNameId(name.getUuid().toString());
|
262
|
handleTaxonomicStatus(record, name, relType, isProParte, isPartial);
|
263
|
//synonyms
|
264
|
handleSynonyms(record,(Taxon) taxonBase);
|
265
|
//distribution
|
266
|
handleDiscriptionData(record, (Taxon) taxonBase);
|
267
|
if(features!= null) {
|
268
|
|
269
|
List<List<String>> featureCells = new ArrayList<List<String>>(features.size());
|
270
|
for(int i = 0; i < features.size(); i++) {
|
271
|
featureCells.add(new ArrayList<String>());
|
272
|
}
|
273
|
handleRelatedRedlistStatus(record, (Taxon)taxonBase, false, featureCells, features);
|
274
|
handleRelatedRedlistStatus(record, (Taxon)taxonBase, true, featureCells, features);
|
275
|
|
276
|
}
|
277
|
return;
|
278
|
}
|
279
|
|
280
|
private void handleTaxonomicStatus(
|
281
|
CsvTaxRecordRedlist record,
|
282
|
INonViralName name,
|
283
|
RelationshipTermBase<?> type,
|
284
|
boolean isProParte,
|
285
|
boolean isPartial) {
|
286
|
if (type == null){
|
287
|
record.setTaxonomicStatus(name.getNomenclaturalCode().acceptedTaxonStatusLabel());
|
288
|
}else{
|
289
|
String status = name.getNomenclaturalCode().synonymStatusLabel();
|
290
|
if (type.equals(SynonymType.HETEROTYPIC_SYNONYM_OF())){
|
291
|
status = "heterotypicSynonym";
|
292
|
}else if(type.equals(SynonymType.HOMOTYPIC_SYNONYM_OF())){
|
293
|
status = "homotypicSynonym";
|
294
|
}else if(type.equals(TaxonRelationshipType.MISAPPLIED_NAME_FOR())){
|
295
|
status = "misapplied";
|
296
|
}else if(type.equals(TaxonRelationshipType.PRO_PARTE_MISAPPLIED_NAME_FOR())){
|
297
|
status = "proParteMisapplied";
|
298
|
}
|
299
|
if (isProParte){
|
300
|
status = "proParteSynonym";
|
301
|
}else if (isPartial){
|
302
|
String message = "Partial synonym is not part of the gbif toxonomic status vocabulary";
|
303
|
logger.warn(message);
|
304
|
status = "partialSynonym";
|
305
|
}
|
306
|
|
307
|
record.setTaxonomicStatus(status);
|
308
|
}
|
309
|
}
|
310
|
|
311
|
private void handleSynonyms(CsvTaxRecordRedlist record, Taxon taxon) {
|
312
|
|
313
|
Set<Synonym> synonyms = taxon.getSynonyms();
|
314
|
ArrayList<String> synonymLabels = new ArrayList<>();
|
315
|
for (Synonym synonym :synonyms ){
|
316
|
SynonymType type = synonym.getType();
|
317
|
if (type == null){ // should not happen
|
318
|
type = SynonymType.SYNONYM_OF();
|
319
|
}
|
320
|
INonViralName name = synonym.getName();
|
321
|
synonymLabels.add(name.getTitleCache());
|
322
|
}
|
323
|
record.setSynonyms(synonymLabels);
|
324
|
}
|
325
|
|
326
|
private void handleDiscriptionData(CsvTaxRecordRedlist record, Taxon taxon) {
|
327
|
|
328
|
Set<TaxonDescription> descriptions = taxon.getDescriptions();
|
329
|
ArrayList<String> distributions = new ArrayList<String>();
|
330
|
for (TaxonDescription description : descriptions){
|
331
|
for (DescriptionElementBase el : description.getElements()){
|
332
|
if (el.isInstanceOf(Distribution.class) ){
|
333
|
Distribution distribution = CdmBase.deproxy(el, Distribution.class);
|
334
|
NamedArea area = distribution.getArea();
|
335
|
distributions.add(area.getTitleCache());
|
336
|
}
|
337
|
|
338
|
}
|
339
|
}
|
340
|
record.setCountryCode(distributions);
|
341
|
}
|
342
|
|
343
|
private void handleRedlistStatus(CsvTaxRecordRedlist record, Taxon taxon, List<List<String>> featureCells, List<Feature> features){
|
344
|
Set<TaxonDescription> descriptions = taxon.getDescriptions();
|
345
|
|
346
|
for (TaxonDescription description : descriptions){
|
347
|
for (DescriptionElementBase el : description.getElements()){
|
348
|
if(el.isInstanceOf(CategoricalData.class)){
|
349
|
CategoricalData categoricalData = CdmBase.deproxy(el, CategoricalData.class);
|
350
|
for(State state:categoricalData.getStatesOnly()){
|
351
|
Feature stateFeature = categoricalData.getFeature();
|
352
|
// find matching feature and put data into according cell
|
353
|
for(int i = 0; i < features.size(); i++) {
|
354
|
if(features.get(i).equals(stateFeature)){
|
355
|
List<String> cell = featureCells.get(i);
|
356
|
cell.add(state.toString());
|
357
|
}
|
358
|
}
|
359
|
}
|
360
|
}else if(el.isInstanceOf(TextData.class)){
|
361
|
TextData textData = CdmBase.deproxy(el, TextData.class);
|
362
|
Feature textFeature = textData.getFeature();
|
363
|
// find matching feature and put data into according cell
|
364
|
for(int i = 0; i < features.size(); i++) {
|
365
|
if(features.get(i).equals(textFeature)){
|
366
|
List<String> cell = featureCells.get(i);
|
367
|
String text = textData.getText(Language.GERMAN());
|
368
|
text = text.replaceAll(System.getProperty("line.separator"), "");
|
369
|
text = text.replaceAll(" ", " ");
|
370
|
cell.add(text);
|
371
|
|
372
|
}
|
373
|
}
|
374
|
}
|
375
|
}
|
376
|
}
|
377
|
record.setFeatures(featureCells);
|
378
|
}
|
379
|
|
380
|
|
381
|
private void handleRelatedRedlistStatus(CsvTaxRecordRedlist record, Taxon taxon, boolean relationFrom, List<List<String>> featureCells, List<Feature> features) {
|
382
|
|
383
|
if (relationFrom) {
|
384
|
handleRedlistStatus(record, taxon, featureCells, features);
|
385
|
}
|
386
|
|
387
|
|
388
|
Set<TaxonRelationship> taxRels;
|
389
|
if(relationFrom){
|
390
|
taxRels = taxon.getRelationsFromThisTaxon();
|
391
|
}else{
|
392
|
taxRels = taxon.getRelationsToThisTaxon();
|
393
|
}
|
394
|
for (TaxonRelationship taxRel:taxRels){
|
395
|
if(taxRel.getType().equals(TaxonRelationshipType.CONGRUENT_TO())){
|
396
|
Taxon relatedTaxon;
|
397
|
if(relationFrom){
|
398
|
relatedTaxon = taxRel.getToTaxon();
|
399
|
}else{
|
400
|
relatedTaxon = taxRel.getFromTaxon();
|
401
|
}
|
402
|
handleRedlistStatus(record, relatedTaxon, featureCells, features);
|
403
|
}
|
404
|
}
|
405
|
}
|
406
|
|
407
|
@Override
|
408
|
protected boolean doCheck(CsvTaxExportStateRedlist state) {
|
409
|
boolean result = true;
|
410
|
logger.warn("No check implemented for " + this.ioName);
|
411
|
return result;
|
412
|
}
|
413
|
|
414
|
@Override
|
415
|
protected boolean isIgnore(CsvTaxExportStateRedlist state) {
|
416
|
return ! state.getConfig().isDoTaxa();
|
417
|
}
|
418
|
|
419
|
|
420
|
|
421
|
|
422
|
}
|