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.ArrayList;
|
17
|
import java.util.List;
|
18
|
import java.util.Set;
|
19
|
|
20
|
import org.apache.log4j.Logger;
|
21
|
|
22
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
23
|
import eu.etaxonomy.cdm.model.common.Language;
|
24
|
import eu.etaxonomy.cdm.model.common.LanguageString;
|
25
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
26
|
import eu.etaxonomy.cdm.model.description.Feature;
|
27
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
28
|
import eu.etaxonomy.cdm.model.description.TextData;
|
29
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
30
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
31
|
|
32
|
/**
|
33
|
* @author a.mueller
|
34
|
* @created 20.04.2011
|
35
|
*/
|
36
|
public class DwcaDescriptionExport extends DwcaDataExportBase {
|
37
|
|
38
|
private static final long serialVersionUID = 4756084824053120718L;
|
39
|
|
40
|
private static final Logger logger = Logger.getLogger(DwcaDescriptionExport.class);
|
41
|
|
42
|
private static final String ROW_TYPE = "http://rs.gbif.org/terms/1.0/Description";
|
43
|
protected static final String fileName = "description.txt";
|
44
|
|
45
|
private DwcaMetaDataRecord metaRecord;
|
46
|
|
47
|
/**
|
48
|
* Constructor
|
49
|
*/
|
50
|
public DwcaDescriptionExport(DwcaTaxExportState state) {
|
51
|
super();
|
52
|
this.ioName = this.getClass().getSimpleName();
|
53
|
metaRecord = new DwcaMetaDataRecord(! IS_CORE, fileName, ROW_TYPE);
|
54
|
state.addMetaRecord(metaRecord);
|
55
|
file = DwcaTaxExportFile.DESCRIPTION;
|
56
|
}
|
57
|
|
58
|
@Override
|
59
|
protected void doInvoke(DwcaTaxExportState state){}
|
60
|
|
61
|
|
62
|
/**
|
63
|
* @param state
|
64
|
* @param node
|
65
|
* @throws IOException
|
66
|
* @throws FileNotFoundException
|
67
|
* @throws UnsupportedEncodingException
|
68
|
*/
|
69
|
@Override
|
70
|
protected void handleTaxonNode(DwcaTaxExportState state, TaxonNode node)
|
71
|
throws IOException, FileNotFoundException, UnsupportedEncodingException {
|
72
|
DwcaTaxExportConfigurator config = state.getConfig();
|
73
|
|
74
|
try {
|
75
|
Taxon taxon = CdmBase.deproxy(node.getTaxon());
|
76
|
Set<TaxonDescription> descriptions = node.getTaxon().getDescriptions();
|
77
|
for (TaxonDescription description : descriptions){
|
78
|
for (DescriptionElementBase el : description.getElements()){
|
79
|
if (el.isInstanceOf(TextData.class) ){
|
80
|
Feature feature = el.getFeature();
|
81
|
if (feature != null &&
|
82
|
! feature.equals(Feature.IMAGE()) &&
|
83
|
! config.getFeatureExclusions().contains(feature.getUuid()) &&
|
84
|
! state.recordExists(file,el)){
|
85
|
DwcaDescriptionRecord record = new DwcaDescriptionRecord(metaRecord, config);
|
86
|
TextData textData = CdmBase.deproxy(el,TextData.class);
|
87
|
handleDescription(state, record, textData, taxon, config);
|
88
|
PrintWriter writer = createPrintWriter(state, DwcaTaxExportFile.DESCRIPTION);
|
89
|
record.write(state, writer);
|
90
|
state.addExistingRecord(file, textData);
|
91
|
}
|
92
|
}
|
93
|
}
|
94
|
}
|
95
|
} catch (Exception e) {
|
96
|
String message = "Unexpected exception: " + e.getMessage();
|
97
|
state.getResult().addException(e, message);
|
98
|
}finally{
|
99
|
flushWriter(state, file);
|
100
|
}
|
101
|
}
|
102
|
|
103
|
|
104
|
private void handleDescription(DwcaTaxExportState state, DwcaDescriptionRecord record, TextData textData, Taxon taxon, DwcaTaxExportConfigurator config) {
|
105
|
record.setId(taxon.getId());
|
106
|
record.setUuid(taxon.getUuid());
|
107
|
|
108
|
//TODO make this part of the Configuration
|
109
|
//TODO question: multiple entries for each language??
|
110
|
List<Language> preferredLanguages = new ArrayList<>();
|
111
|
preferredLanguages.add(Language.DEFAULT());
|
112
|
LanguageString languageText = textData.getPreferredLanguageString(preferredLanguages);
|
113
|
|
114
|
|
115
|
if (textData.getFeature() == null){
|
116
|
String message = "No feature available for text data ("+textData.getId()+"). Feature is required field. Taxon: " + this.getTaxonLogString(taxon);
|
117
|
state.getResult().addWarning(message);
|
118
|
}
|
119
|
record.setType(textData.getFeature());
|
120
|
|
121
|
if (languageText == null){
|
122
|
String message = "No text in default language available for text data ("+textData.getId()+"). Text is required field. Taxon: " + this.getTaxonLogString(taxon);
|
123
|
state.getResult().addWarning(message);
|
124
|
}else{
|
125
|
record.setDescription(languageText.getText());
|
126
|
record.setLanguage(languageText.getLanguage());
|
127
|
}
|
128
|
|
129
|
//sources
|
130
|
record.setSource(getSources(textData, config));
|
131
|
|
132
|
//TODO missing , relationship to credits?
|
133
|
record.setCreator(null);
|
134
|
//TODO missing, relationship to credits?
|
135
|
record.setContributor(null);
|
136
|
//TODO missing
|
137
|
record.setAudience(null);
|
138
|
record.setLicense(textData.getInDescription().getRights());
|
139
|
//TODO missing
|
140
|
record.setRightsHolder(null);
|
141
|
|
142
|
}
|
143
|
|
144
|
@Override
|
145
|
protected boolean doCheck(DwcaTaxExportState state) {
|
146
|
boolean result = true;
|
147
|
logger.warn("No check implemented for " + this.ioName);
|
148
|
return result;
|
149
|
}
|
150
|
|
151
|
|
152
|
@Override
|
153
|
public boolean isIgnore(DwcaTaxExportState state) {
|
154
|
return ! state.getConfig().isDoDescriptions();
|
155
|
}
|
156
|
|
157
|
}
|