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.dwca.out;
|
11
|
|
12
|
import java.io.FileNotFoundException;
|
13
|
import java.io.IOException;
|
14
|
import java.io.PrintWriter;
|
15
|
import java.io.UnsupportedEncodingException;
|
16
|
import java.util.List;
|
17
|
import java.util.Set;
|
18
|
|
19
|
import org.apache.commons.lang.StringUtils;
|
20
|
import org.apache.log4j.Logger;
|
21
|
|
22
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
23
|
import eu.etaxonomy.cdm.model.common.Annotation;
|
24
|
import eu.etaxonomy.cdm.model.common.AnnotationType;
|
25
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
26
|
import eu.etaxonomy.cdm.model.common.RelationshipTermBase;
|
27
|
import eu.etaxonomy.cdm.model.name.INonViralName;
|
28
|
import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
|
29
|
import eu.etaxonomy.cdm.model.name.Rank;
|
30
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
31
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
32
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
33
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
34
|
import eu.etaxonomy.cdm.model.taxon.SynonymType;
|
35
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
36
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
37
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
38
|
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
|
39
|
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
|
40
|
|
41
|
/**
|
42
|
* @author a.mueller
|
43
|
* @since 18.04.2011
|
44
|
*/
|
45
|
public class DwcaTaxonExport extends DwcaDataExportBase {
|
46
|
private static final long serialVersionUID = -3770976064909193441L;
|
47
|
|
48
|
private static final Logger logger = Logger.getLogger(DwcaTaxonExport.class);
|
49
|
|
50
|
private static final String ROW_TYPE = "http://rs.tdwg.org/dwc/terms/Taxon";
|
51
|
protected static final String fileName = "coreTax.txt";
|
52
|
|
53
|
private DwcaMetaDataRecord metaRecord;
|
54
|
|
55
|
/**
|
56
|
* Constructor
|
57
|
*/
|
58
|
public DwcaTaxonExport(DwcaTaxExportState state) {
|
59
|
super();
|
60
|
this.ioName = this.getClass().getSimpleName();
|
61
|
metaRecord = new DwcaMetaDataRecord(IS_CORE, fileName, ROW_TYPE);
|
62
|
state.addMetaRecord(metaRecord);
|
63
|
file = DwcaTaxExportFile.TAXON;
|
64
|
}
|
65
|
|
66
|
@Override
|
67
|
protected void doInvoke(DwcaTaxExportState state){}
|
68
|
|
69
|
@Override
|
70
|
protected void handleTaxonNode(DwcaTaxExportState state, TaxonNode node)
|
71
|
throws IOException, FileNotFoundException, UnsupportedEncodingException {
|
72
|
|
73
|
try {
|
74
|
Taxon taxon = CdmBase.deproxy(node.getTaxon());
|
75
|
DwcaTaxExportConfigurator config = state.getConfig();
|
76
|
DwcaTaxonRecord record = new DwcaTaxonRecord(metaRecord, config);
|
77
|
|
78
|
TaxonName name = taxon.getName();
|
79
|
Taxon parent = node.getParent() == null ? null : node.getParent().getTaxon();
|
80
|
TaxonName basionym = name.getBasionym();
|
81
|
Classification classification = node.getClassification();
|
82
|
if (! state.recordExists(file, taxon)){
|
83
|
handleTaxonBase(state, record, taxon, name, taxon, parent, basionym, classification, null, false, false);
|
84
|
PrintWriter writer = createPrintWriter(state, file);
|
85
|
record.write(state, writer);
|
86
|
state.addExistingRecord(file, taxon);
|
87
|
}
|
88
|
|
89
|
|
90
|
//synonyms
|
91
|
if (state.getConfig().isDoSynonyms()){
|
92
|
handleSynonyms(state, taxon, file, classification, metaRecord);
|
93
|
}
|
94
|
|
95
|
//misapplied names
|
96
|
if (state.getConfig().isDoMisappliedNames()){
|
97
|
handleMisapplication(state, taxon, file, classification, metaRecord);
|
98
|
}
|
99
|
|
100
|
} catch (Exception e) {
|
101
|
String message = "Unexpected exception: " + e.getMessage();
|
102
|
state.getResult().addException(e, message);
|
103
|
}finally{
|
104
|
flushWriter(state, file);
|
105
|
|
106
|
}
|
107
|
}
|
108
|
|
109
|
|
110
|
|
111
|
private void handleSynonyms(DwcaTaxExportState state, Taxon taxon, DwcaTaxExportFile file,
|
112
|
Classification classification, DwcaMetaDataRecord metaRecord) throws FileNotFoundException, UnsupportedEncodingException, IOException {
|
113
|
for (Synonym synonym :taxon.getSynonyms() ){
|
114
|
if (isUnpublished(state.getConfig(), synonym)){
|
115
|
return;
|
116
|
}
|
117
|
|
118
|
DwcaTaxonRecord record = new DwcaTaxonRecord(metaRecord, state.getConfig());
|
119
|
SynonymType type = synonym.getType();
|
120
|
boolean isProParte = synonym.isProParte();
|
121
|
boolean isPartial = synonym.isPartial();
|
122
|
if (type == null){ // should not happen
|
123
|
type = SynonymType.SYNONYM_OF();
|
124
|
}
|
125
|
TaxonName name = synonym.getName();
|
126
|
//????
|
127
|
Taxon parent = null;
|
128
|
TaxonName basionym = name.getBasionym();
|
129
|
|
130
|
if (! state.recordExists(file, synonym)){
|
131
|
handleTaxonBase(state, record, synonym, name, taxon, parent, basionym, classification, type, isProParte, isPartial);
|
132
|
PrintWriter writer = createPrintWriter(state, file);
|
133
|
record.write(state, writer);
|
134
|
state.addExistingRecord(file, synonym);
|
135
|
}
|
136
|
}
|
137
|
}
|
138
|
|
139
|
private void handleMisapplication(DwcaTaxExportState state, Taxon taxon,
|
140
|
DwcaTaxExportFile file, Classification classification, DwcaMetaDataRecord metaRecord) throws FileNotFoundException, UnsupportedEncodingException, IOException {
|
141
|
|
142
|
Set<TaxonRelationship> misappliedNamesRels = taxon.getMisappliedNameRelations();
|
143
|
for (TaxonRelationship misappliedNameRel : misappliedNamesRels ){
|
144
|
DwcaTaxonRecord record = new DwcaTaxonRecord(metaRecord, state.getConfig());
|
145
|
Taxon misappliedName = misappliedNameRel.getFromTaxon();
|
146
|
TaxonName name = misappliedName.getName();
|
147
|
//????
|
148
|
Taxon parent = null;
|
149
|
TaxonName basionym = name.getBasionym();
|
150
|
|
151
|
if (! state.recordExists(file, misappliedName)){
|
152
|
handleTaxonBase(state, record, misappliedName, name, taxon, parent, basionym, classification,
|
153
|
misappliedNameRel.getType(), false, false);
|
154
|
PrintWriter writer = createPrintWriter(state, file);
|
155
|
record.write(state, writer);
|
156
|
state.addExistingRecord(file, misappliedName);
|
157
|
}
|
158
|
}
|
159
|
}
|
160
|
|
161
|
/**
|
162
|
* @param state
|
163
|
* @param record
|
164
|
* @param taxonBase
|
165
|
* @param name
|
166
|
* @param acceptedTaxon
|
167
|
* @param parent
|
168
|
* @param basionym
|
169
|
* @param isPartial
|
170
|
* @param isProParte
|
171
|
* @param config
|
172
|
* @param type
|
173
|
*/
|
174
|
private void handleTaxonBase(DwcaTaxExportState state, DwcaTaxonRecord record, TaxonBase<?> taxonBase, TaxonName name,
|
175
|
Taxon acceptedTaxon, Taxon parent, TaxonName basionym, Classification classification,
|
176
|
RelationshipTermBase<?> relType, boolean isProParte, boolean isPartial) {
|
177
|
record.setId(taxonBase.getId());
|
178
|
record.setUuid(taxonBase.getUuid());
|
179
|
|
180
|
//maybe wrong as according to the DwC-A documentation only resolvable ids are allowed, this differs from DwC documentation
|
181
|
record.setScientificNameId(name);
|
182
|
record.setScientificName(name.getTitleCache());
|
183
|
|
184
|
record.setAcceptedNameUsageId(acceptedTaxon.getUuid());
|
185
|
record.setAcceptedNameUsage(acceptedTaxon.getName() == null? acceptedTaxon.getTitleCache() : acceptedTaxon.getName().getTitleCache());
|
186
|
|
187
|
//parentNameUsage
|
188
|
if (parent != null){
|
189
|
record.setParentNameUsageId(parent.getUuid());
|
190
|
record.setParentNameUsage(parent.getTitleCache());
|
191
|
}
|
192
|
|
193
|
//originalNameUsage
|
194
|
// ??? - is not a name usage (concept)
|
195
|
if (basionym != null){
|
196
|
//FIXME needs to be a coreID otherwise use string only
|
197
|
// record.setOriginalNameUsageId(basionym.getUuid());
|
198
|
record.setOriginalNameUsageId(null);
|
199
|
record.setOriginalNameUsage(basionym.getTitleCache());
|
200
|
}
|
201
|
|
202
|
//nameAccordingTo
|
203
|
Reference sec = taxonBase.getSec();
|
204
|
if (sec == null){
|
205
|
String message = "There is a taxon without sec " + taxonBase.getTitleCache() + "(" + taxonBase.getId() + ")";
|
206
|
state.getResult().addWarning(message);
|
207
|
}else{
|
208
|
record.setNameAccordingToId(taxonBase.getSec().getUuid());
|
209
|
record.setNameAccordingTo(taxonBase.getSec().getTitleCache());
|
210
|
}
|
211
|
|
212
|
//namePublishedIn
|
213
|
// ??? is not a nameUsage (concept)
|
214
|
if (name.getNomenclaturalReference() != null){
|
215
|
record.setNamePublishedInId(name.getNomenclaturalReference().getUuid());
|
216
|
record.setNamePublishedIn(name.getNomenclaturalReference() == null ? null : name.getNomenclaturalReference().getTitleCache());
|
217
|
}
|
218
|
|
219
|
// what is the exact difference to id and acceptedNameUsageId
|
220
|
record.setTaxonConceptId(taxonBase.getUuid());
|
221
|
|
222
|
//Classification
|
223
|
if (state.getConfig().isWithHigherClassification()){
|
224
|
//all classification and rank specific fields are meant to represent the classification
|
225
|
handleHigherClassification(state, record, acceptedTaxon, classification);
|
226
|
|
227
|
//... higher ranks
|
228
|
// handleUninomialOrGenus(record, name);
|
229
|
// if (name.getRank() != null && name.getRank().equals(Rank.SUBGENUS())){
|
230
|
// record.setSubgenus(name.getNameCache());
|
231
|
// }
|
232
|
//record.setSubgenus(name.getInfraGenericEpithet());
|
233
|
}
|
234
|
if (name.getRank() != null && (name.getRank().isSupraGeneric() || name.getRank().isGenus())){
|
235
|
record.setUninomial(name.getGenusOrUninomial());
|
236
|
}else{
|
237
|
record.setGenusPart(name.getGenusOrUninomial());
|
238
|
}
|
239
|
record.setInfraGenericEpithet(name.getInfraGenericEpithet());
|
240
|
|
241
|
record.setSpecificEpithet(name.getSpecificEpithet());
|
242
|
record.setInfraspecificEpithet(name.getInfraSpecificEpithet());
|
243
|
|
244
|
record.setTaxonRank(name.getRank());
|
245
|
if (name.getRank() != null){
|
246
|
record.setVerbatimTaxonRank(name.getRank().getAbbreviation());
|
247
|
}else{
|
248
|
String message = "No rank available for " + name.getTitleCache() + "(" + name.getId() + ")";
|
249
|
state.getResult().addWarning(message);
|
250
|
}
|
251
|
|
252
|
record.setScientificNameAuthorship(name.getAuthorshipCache());
|
253
|
|
254
|
// ??? - use for TextData common names?
|
255
|
record.setVernacularName(null);
|
256
|
|
257
|
record.setNomenclaturalCode(name.getNameType());
|
258
|
// ??? TODO Misapplied Names, inferred synonyms
|
259
|
handleTaxonomicStatus(record, name, relType, isProParte, isPartial);
|
260
|
handleNomStatus(record, taxonBase, name);
|
261
|
|
262
|
// TODO we need to differentiate technical
|
263
|
String taxonRemarks = "";
|
264
|
for (Annotation annotation : taxonBase.getAnnotations()){
|
265
|
if (AnnotationType.EDITORIAL().equals(annotation.getAnnotationType())){
|
266
|
taxonRemarks += CdmUtils.Nz(annotation.getText());
|
267
|
}
|
268
|
}
|
269
|
for (Annotation annotation : name.getAnnotations()){
|
270
|
if (AnnotationType.EDITORIAL().equals(annotation.getAnnotationType())){
|
271
|
taxonRemarks += CdmUtils.Nz(annotation.getText());
|
272
|
}
|
273
|
}
|
274
|
if (StringUtils.isNotBlank(taxonRemarks)){
|
275
|
record.setTaxonRemarks(taxonRemarks);
|
276
|
}
|
277
|
|
278
|
// TODO which date is needed here (taxon, name, sec, ... ?)
|
279
|
record.setModified(taxonBase.getUpdated());
|
280
|
|
281
|
// ???
|
282
|
record.setLanguage(null);
|
283
|
|
284
|
record.setRights(taxonBase.getRights());
|
285
|
|
286
|
//TODO
|
287
|
record.setRightsHolder(null);
|
288
|
record.setAccessRights(null);
|
289
|
|
290
|
//TODO currently only via default value
|
291
|
record.setBibliographicCitation(null);
|
292
|
record.setInformationWithheld(null);
|
293
|
|
294
|
record.setDatasetId(classification);
|
295
|
record.setDatasetName(classification.getTitleCache());
|
296
|
|
297
|
//TODO
|
298
|
record.setSource(null);
|
299
|
|
300
|
return;
|
301
|
}
|
302
|
|
303
|
/**
|
304
|
* @param state
|
305
|
* @param record
|
306
|
* @param taxonBase
|
307
|
* @param classification
|
308
|
*/
|
309
|
private void handleHigherClassification(DwcaTaxExportState state, DwcaTaxonRecord record, Taxon taxon,
|
310
|
Classification classification) {
|
311
|
TaxonNode node = taxon.getTaxonNode(classification);
|
312
|
if (node == null){
|
313
|
return;
|
314
|
}
|
315
|
record.setKingdom(nameOf(node.getAncestorOfRank(Rank.KINGDOM())));
|
316
|
record.setPhylum(nameOf(node.getAncestorOfRank(Rank.PHYLUM())));
|
317
|
record.setClazz(nameOf(node.getAncestorOfRank(Rank.CLASS())));
|
318
|
record.setOrder(nameOf(node.getAncestorOfRank(Rank.ORDER())));
|
319
|
record.setFamily(nameOf(node.getAncestorOfRank(Rank.FAMILY())));
|
320
|
record.setGenus(nameOf(node.getAncestorOfRank(Rank.GENUS())));
|
321
|
record.setSubgenus(nameOf(node.getAncestorOfRank(Rank.SUBGENUS())));
|
322
|
List<TaxonNode> ancestors = node.getAncestorList();
|
323
|
String higherClassification = higherClassificationString(ancestors);
|
324
|
record.setHigherClassification(higherClassification);
|
325
|
}
|
326
|
|
327
|
/**
|
328
|
* @param ancestors
|
329
|
*/
|
330
|
private String higherClassificationString(List<TaxonNode> ancestors) {
|
331
|
String result = "";
|
332
|
for (TaxonNode node : ancestors){
|
333
|
String nameOfNode = nameOf(node);
|
334
|
if (StringUtils.isBlank(nameOfNode)){
|
335
|
nameOfNode = "-";
|
336
|
}
|
337
|
result = CdmUtils.concat("|", result, nameOfNode);
|
338
|
}
|
339
|
return result;
|
340
|
}
|
341
|
|
342
|
/**
|
343
|
* @param ancestorOfRank
|
344
|
* @return
|
345
|
*/
|
346
|
private String nameOf(TaxonNode node) {
|
347
|
if (node != null && node.getTaxon()!= null){
|
348
|
Taxon taxon = node.getTaxon();
|
349
|
TaxonName name = taxon.getName();
|
350
|
if (name!= null){
|
351
|
if (isNotBlank(name.getNameCache())){
|
352
|
return name.getNameCache();
|
353
|
}else{
|
354
|
return name.getTitleCache();
|
355
|
}
|
356
|
}else{
|
357
|
return taxon.getTitleCache();
|
358
|
}
|
359
|
}
|
360
|
return null;
|
361
|
}
|
362
|
|
363
|
/**
|
364
|
* @param record
|
365
|
* @param name
|
366
|
* @param type
|
367
|
* @param isPartial
|
368
|
* @param isProParte
|
369
|
*/
|
370
|
private void handleTaxonomicStatus(DwcaTaxonRecord record,
|
371
|
INonViralName name, RelationshipTermBase<?> type,
|
372
|
boolean isProParte, boolean isPartial) {
|
373
|
if (type == null){
|
374
|
record.setTaxonomicStatus(name.getNomenclaturalCode().acceptedTaxonStatusLabel());
|
375
|
}else{
|
376
|
String status = name.getNomenclaturalCode().synonymStatusLabel();
|
377
|
if (type.equals(SynonymType.HETEROTYPIC_SYNONYM_OF())){
|
378
|
status = "heterotypicSynonym";
|
379
|
}else if(type.equals(SynonymType.HOMOTYPIC_SYNONYM_OF())){
|
380
|
status = "homotypicSynonym";
|
381
|
}else if(type.equals(TaxonRelationshipType.MISAPPLIED_NAME_FOR())){
|
382
|
status = "misapplied";
|
383
|
}else if(type.equals(TaxonRelationshipType.PRO_PARTE_MISAPPLIED_NAME_FOR())){
|
384
|
status = "proParteMisapplied";
|
385
|
}
|
386
|
if (isProParte){
|
387
|
status = "proParteSynonym";
|
388
|
}else if (isPartial){
|
389
|
String message = "Partial synonym is not part of the gbif toxonomic status vocabulary";
|
390
|
logger.warn(message);
|
391
|
status = "partialSynonym";
|
392
|
}
|
393
|
|
394
|
record.setTaxonomicStatus(status);
|
395
|
}
|
396
|
}
|
397
|
|
398
|
// /**
|
399
|
// * @param record
|
400
|
// * @param name
|
401
|
// */
|
402
|
// private void handleUninomialOrGenus(DwcaTaxonRecord record, INonViralName name) {
|
403
|
// //epitheta
|
404
|
// String firstEpi = name.getGenusOrUninomial();
|
405
|
// if (StringUtils.isNotBlank(firstEpi)){
|
406
|
// Rank rank = name.getRank();
|
407
|
// if (rank != null){
|
408
|
// if (rank.isLower(Rank.GENUS())){
|
409
|
// record.setGenus(firstEpi);
|
410
|
// }else if (rank.equals(Rank.GENUS())){
|
411
|
// record.setGenus(firstEpi);
|
412
|
// }else if (rank.equals(Rank.KINGDOM())){
|
413
|
// record.setKingdom(firstEpi);
|
414
|
// }else if (rank.equals(Rank.PHYLUM())){
|
415
|
// record.setPhylum(firstEpi);
|
416
|
// }else if (rank.equals(Rank.CLASS())){
|
417
|
// record.setClazz(firstEpi);
|
418
|
// }else if (rank.equals(Rank.ORDER())){
|
419
|
// record.setOrder(firstEpi);
|
420
|
// }else if (rank.equals(Rank.FAMILY())){
|
421
|
// record.setFamily(firstEpi);
|
422
|
// }else{
|
423
|
// // !!!
|
424
|
// String message = "Rank not covered. Set uninomial as genus instead: " + rank.getLabel();
|
425
|
// logger.warn(message);
|
426
|
//// record.setGenus(firstEpi);
|
427
|
// }
|
428
|
//
|
429
|
// }
|
430
|
// }
|
431
|
// }
|
432
|
|
433
|
|
434
|
/**
|
435
|
* @param record
|
436
|
* @param taxon
|
437
|
* @param name
|
438
|
*/
|
439
|
private void handleNomStatus(DwcaTaxonRecord record, TaxonBase<?> taxon,
|
440
|
INonViralName name) {
|
441
|
int nStatus = name.getStatus().size();
|
442
|
if (nStatus > 0){
|
443
|
if (name.getStatus().size()> 1){
|
444
|
String warning = "There is more than 1 nomenclatural status ( " + name.getStatus().size()+ "): " + taxon.getTitleCache();
|
445
|
logger.warn(warning);
|
446
|
}
|
447
|
NomenclaturalStatusType status = name.getStatus().iterator().next().getType();
|
448
|
record.setNomenclaturalStatus(status);
|
449
|
}else{
|
450
|
record.setNomenclaturalStatus(null);
|
451
|
}
|
452
|
}
|
453
|
|
454
|
|
455
|
@Override
|
456
|
protected boolean doCheck(DwcaTaxExportState state) {
|
457
|
boolean result = true;
|
458
|
logger.warn("No check implemented for " + this.ioName);
|
459
|
return result;
|
460
|
}
|
461
|
|
462
|
|
463
|
@Override
|
464
|
public boolean isIgnore(DwcaTaxExportState state) {
|
465
|
return ! state.getConfig().isDoTaxa();
|
466
|
}
|
467
|
|
468
|
}
|