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.PrintWriter;
|
13
|
import java.util.List;
|
14
|
import java.util.Set;
|
15
|
|
16
|
import org.apache.commons.lang.StringUtils;
|
17
|
import org.apache.log4j.Logger;
|
18
|
import org.springframework.stereotype.Component;
|
19
|
import org.springframework.transaction.TransactionStatus;
|
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
|
@Component
|
34
|
public class DwcaVernacularExport extends DwcaExportBase {
|
35
|
private static final long serialVersionUID = 3169086545830374918L;
|
36
|
|
37
|
private static final Logger logger = Logger.getLogger(DwcaVernacularExport.class);
|
38
|
|
39
|
private static final String ROW_TYPE = "http://rs.gbif.org/terms/1.0/VernacularName";
|
40
|
protected static final String fileName = "vernacular.txt";
|
41
|
|
42
|
|
43
|
/**
|
44
|
* Constructor
|
45
|
*/
|
46
|
public DwcaVernacularExport() {
|
47
|
super();
|
48
|
this.ioName = this.getClass().getSimpleName();
|
49
|
}
|
50
|
|
51
|
/** Retrieves data from a CDM DB and serializes them CDM to XML.
|
52
|
* Starts with root taxa and traverses the classification to retrieve children taxa, synonyms and relationships.
|
53
|
* Taxa that are not part of the classification are not found.
|
54
|
*
|
55
|
* @param exImpConfig
|
56
|
* @param dbname
|
57
|
* @param filename
|
58
|
*/
|
59
|
@Override
|
60
|
protected void doInvoke(DwcaTaxExportState state){
|
61
|
DwcaTaxExportConfigurator config = state.getConfig();
|
62
|
TransactionStatus txStatus = startTransaction(true);
|
63
|
|
64
|
DwcaTaxOutputFile file = DwcaTaxOutputFile.VERNACULAR;
|
65
|
try {
|
66
|
|
67
|
DwcaMetaDataRecord metaRecord = new DwcaMetaDataRecord(! IS_CORE, fileName, ROW_TYPE);
|
68
|
state.addMetaRecord(metaRecord);
|
69
|
|
70
|
List<TaxonNode> allNodes = allNodes(state);
|
71
|
for (TaxonNode node : allNodes){
|
72
|
Taxon taxon = CdmBase.deproxy(node.getTaxon());
|
73
|
Set<TaxonDescription> descriptions = taxon.getDescriptions();
|
74
|
for (TaxonDescription description : descriptions){
|
75
|
for (DescriptionElementBase el : description.getElements()){
|
76
|
if (el.isInstanceOf(CommonTaxonName.class)){
|
77
|
DwcaVernacularRecord record = new DwcaVernacularRecord(metaRecord, config);
|
78
|
CommonTaxonName commonTaxonName = CdmBase.deproxy(el, CommonTaxonName.class);
|
79
|
if (! state.recordExists(file, commonTaxonName)){
|
80
|
handleCommonTaxonName(record, commonTaxonName, taxon, config);
|
81
|
PrintWriter writer = createPrintWriter(state, file);
|
82
|
record.write(state, writer);
|
83
|
state.addExistingRecord(file, commonTaxonName);
|
84
|
}
|
85
|
}else if (el.getFeature().equals(Feature.COMMON_NAME())){
|
86
|
//TODO
|
87
|
String message = "Vernacular name export for TextData not yet implemented";
|
88
|
state.getResult().addError(message, this, "doInvoke()");
|
89
|
logger.warn(message);
|
90
|
}
|
91
|
}
|
92
|
}
|
93
|
|
94
|
flushWriter(state, file);
|
95
|
}
|
96
|
} catch (Exception e) {
|
97
|
String message = "Unexpected exception " + e.getMessage();
|
98
|
state.getResult().addException(e, message, "DwcaVernacularExport.doInvoke()");
|
99
|
} finally{
|
100
|
closeWriter(file, state);
|
101
|
}
|
102
|
commitTransaction(txStatus);
|
103
|
return;
|
104
|
}
|
105
|
|
106
|
|
107
|
|
108
|
|
109
|
private void handleCommonTaxonName(DwcaVernacularRecord record, CommonTaxonName commonTaxonName, Taxon taxon, DwcaTaxExportConfigurator config) {
|
110
|
record.setId(taxon.getId());
|
111
|
record.setUuid(taxon.getUuid());
|
112
|
if (StringUtils.isBlank(commonTaxonName.getName())){
|
113
|
String message = "'Name' is required field for vernacular name but does not exist for taxon " + getTaxonLogString(taxon);
|
114
|
logger.warn(message);
|
115
|
}else{
|
116
|
record.setVernacularName(commonTaxonName.getName());
|
117
|
}
|
118
|
//sources
|
119
|
record.setSource(getSources(commonTaxonName, config));
|
120
|
record.setLanguage(commonTaxonName.getLanguage());
|
121
|
// does not exist in CDM
|
122
|
record.setTemporal(null);
|
123
|
|
124
|
handleArea(record, commonTaxonName.getArea(), taxon, false);
|
125
|
}
|
126
|
|
127
|
@Override
|
128
|
protected boolean doCheck(DwcaTaxExportState state) {
|
129
|
boolean result = true;
|
130
|
logger.warn("No check implemented for " + this.ioName);
|
131
|
return result;
|
132
|
}
|
133
|
|
134
|
@Override
|
135
|
protected boolean isIgnore(DwcaTaxExportState state) {
|
136
|
return ! state.getConfig().isDoVernacularNames();
|
137
|
}
|
138
|
}
|