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