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.Set;
|
17
|
|
18
|
import org.apache.commons.lang.StringUtils;
|
19
|
import org.apache.log4j.Logger;
|
20
|
|
21
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
22
|
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
|
23
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
24
|
import eu.etaxonomy.cdm.model.description.Feature;
|
25
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
26
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
27
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
28
|
|
29
|
/**
|
30
|
* @author a.mueller
|
31
|
* @created 18.04.2011
|
32
|
*/
|
33
|
public class DwcaVernacularExport extends DwcaDataExportBase {
|
34
|
private static final long serialVersionUID = 3169086545830374918L;
|
35
|
|
36
|
private static final Logger logger = Logger.getLogger(DwcaVernacularExport.class);
|
37
|
|
38
|
private static final String ROW_TYPE = "http://rs.gbif.org/terms/1.0/VernacularName";
|
39
|
protected static final String fileName = "vernacular.txt";
|
40
|
|
41
|
private DwcaMetaDataRecord metaRecord;
|
42
|
|
43
|
/**
|
44
|
* Constructor
|
45
|
*/
|
46
|
public DwcaVernacularExport(DwcaTaxExportState state) {
|
47
|
super();
|
48
|
this.ioName = this.getClass().getSimpleName();
|
49
|
metaRecord = new DwcaMetaDataRecord(! IS_CORE, fileName, ROW_TYPE);
|
50
|
state.addMetaRecord(metaRecord);
|
51
|
file = DwcaTaxExportFile.VERNACULAR;
|
52
|
}
|
53
|
|
54
|
@Override
|
55
|
protected void doInvoke(DwcaTaxExportState state){}
|
56
|
|
57
|
/**
|
58
|
* {@inheritDoc}
|
59
|
*/
|
60
|
@Override
|
61
|
protected void handleTaxonNode(DwcaTaxExportState state, TaxonNode node)
|
62
|
throws IOException, FileNotFoundException, UnsupportedEncodingException {
|
63
|
|
64
|
try {
|
65
|
DwcaTaxExportConfigurator config = state.getConfig();
|
66
|
Taxon taxon = CdmBase.deproxy(node.getTaxon());
|
67
|
Set<TaxonDescription> descriptions = taxon.getDescriptions();
|
68
|
for (TaxonDescription description : descriptions){
|
69
|
for (DescriptionElementBase el : description.getElements()){
|
70
|
if (el.isInstanceOf(CommonTaxonName.class)){
|
71
|
DwcaVernacularRecord record = new DwcaVernacularRecord(metaRecord, config);
|
72
|
CommonTaxonName commonTaxonName = CdmBase.deproxy(el, CommonTaxonName.class);
|
73
|
if (! state.recordExists(file, commonTaxonName)){
|
74
|
handleCommonTaxonName(state, record, commonTaxonName, taxon, config);
|
75
|
PrintWriter writer = createPrintWriter(state, file);
|
76
|
record.write(state, writer);
|
77
|
state.addExistingRecord(file, commonTaxonName);
|
78
|
}
|
79
|
}else if (el.getFeature().equals(Feature.COMMON_NAME())){
|
80
|
//TODO
|
81
|
String message = "Vernacular name export for TextData not yet implemented";
|
82
|
state.getResult().addError(message, this, "doInvoke()");
|
83
|
logger.warn(message);
|
84
|
}
|
85
|
}
|
86
|
}
|
87
|
|
88
|
} catch (Exception e) {
|
89
|
String message = "Unexpected exception: " + e.getMessage();
|
90
|
state.getResult().addException(e, message);
|
91
|
}finally{
|
92
|
flushWriter(state, file);
|
93
|
}
|
94
|
}
|
95
|
|
96
|
|
97
|
|
98
|
|
99
|
private void handleCommonTaxonName(DwcaTaxExportState state, DwcaVernacularRecord record, CommonTaxonName commonTaxonName, Taxon taxon, DwcaTaxExportConfigurator config) {
|
100
|
record.setId(taxon.getId());
|
101
|
record.setUuid(taxon.getUuid());
|
102
|
if (StringUtils.isBlank(commonTaxonName.getName())){
|
103
|
String message = "'Name' is required field for vernacular name but does not exist for taxon " + getTaxonLogString(taxon);
|
104
|
state.getResult().addWarning(message);
|
105
|
}else{
|
106
|
record.setVernacularName(commonTaxonName.getName());
|
107
|
}
|
108
|
//sources
|
109
|
record.setSource(getSources(commonTaxonName, config));
|
110
|
record.setLanguage(commonTaxonName.getLanguage());
|
111
|
// does not exist in CDM
|
112
|
record.setTemporal(null);
|
113
|
|
114
|
handleArea(state, record, commonTaxonName.getArea(), taxon, false);
|
115
|
}
|
116
|
|
117
|
@Override
|
118
|
protected boolean doCheck(DwcaTaxExportState state) {
|
119
|
boolean result = true;
|
120
|
logger.warn("No check implemented for " + this.ioName);
|
121
|
return result;
|
122
|
}
|
123
|
|
124
|
@Override
|
125
|
public boolean isIgnore(DwcaTaxExportState state) {
|
126
|
return ! state.getConfig().isDoVernacularNames();
|
127
|
}
|
128
|
}
|