1
|
/**
|
2
|
* Copyright (C) 2009 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
|
package eu.etaxonomy.cdm.io.dwca.in;
|
10
|
|
11
|
import java.util.ArrayList;
|
12
|
import java.util.HashSet;
|
13
|
import java.util.List;
|
14
|
import java.util.Map;
|
15
|
import java.util.Set;
|
16
|
import java.util.UUID;
|
17
|
|
18
|
import org.apache.commons.lang.StringUtils;
|
19
|
import org.apache.log4j.Logger;
|
20
|
|
21
|
import eu.etaxonomy.cdm.io.common.mapping.UndefinedTransformerMethodException;
|
22
|
import eu.etaxonomy.cdm.io.dwca.TermUri;
|
23
|
import eu.etaxonomy.cdm.io.stream.StreamItem;
|
24
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
25
|
import eu.etaxonomy.cdm.model.common.Language;
|
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.media.Rights;
|
30
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
31
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
32
|
|
33
|
/**
|
34
|
* @author a.mueller
|
35
|
* @date 22.11.2011
|
36
|
*
|
37
|
*/
|
38
|
public class GbifDescriptionCsv2CdmConverter extends PartitionableConverterBase<DwcaDataImportConfiguratorBase, DwcaDataImportStateBase<DwcaDataImportConfiguratorBase>>
|
39
|
implements IPartitionableConverter<StreamItem, IReader<CdmBase>, String>{
|
40
|
|
41
|
@SuppressWarnings("unused")
|
42
|
private static final Logger logger = Logger.getLogger(GbifDescriptionCsv2CdmConverter.class);
|
43
|
|
44
|
private static final String CORE_ID = "coreId";
|
45
|
|
46
|
/**
|
47
|
* @param state
|
48
|
*/
|
49
|
public GbifDescriptionCsv2CdmConverter(DwcaDataImportStateBase state) {
|
50
|
super(state);
|
51
|
}
|
52
|
|
53
|
@Override
|
54
|
public IReader<MappedCdmBase> map(StreamItem item ){
|
55
|
List<MappedCdmBase> resultList = new ArrayList<MappedCdmBase>();
|
56
|
|
57
|
Map<String, String> csv = item.map;
|
58
|
Reference sourceReference = state.getTransactionalSourceReference();
|
59
|
String sourceReferecenDetail = null;
|
60
|
|
61
|
String id = getSourceId(item);
|
62
|
Taxon taxon = getTaxonBase(id, item, Taxon.class, state);
|
63
|
if (taxon != null){
|
64
|
|
65
|
String description = item.get(TermUri.DC_DESCRIPTION);
|
66
|
//String license = item.get(TermUri.DC_LICENSE);//lorna check - often empty in SP dwca
|
67
|
|
68
|
//TODO: Create the Language from the TermUri.DC_LANGUAGE in the dwca
|
69
|
Language language = getLanguage(item);
|
70
|
|
71
|
|
72
|
if (StringUtils.isNotBlank(description)){
|
73
|
Feature feature = getFeatureByDcType(item, resultList);
|
74
|
|
75
|
TaxonDescription taxonDescription = getTaxonDescription(taxon, false);
|
76
|
|
77
|
//deduplicate - if this taxonDescription already got these Rights attached
|
78
|
boolean addRights = true;
|
79
|
String rights = item.get(TermUri.DC_RIGHTS);
|
80
|
Set<Rights> allRights = taxonDescription.getRights();
|
81
|
for (Rights r : allRights) {
|
82
|
if (r.getText() == rights) {
|
83
|
addRights = false;
|
84
|
}
|
85
|
}
|
86
|
|
87
|
if (addRights && (rights != null || rights != "")) {
|
88
|
Rights copyright = Rights.NewInstance(rights, language);
|
89
|
taxonDescription.addRights(copyright);
|
90
|
}
|
91
|
|
92
|
TextData descElement = TextData.NewInstance(feature);
|
93
|
|
94
|
//Language language = getLanguage(item); //TODO
|
95
|
descElement.putText(language,description);
|
96
|
taxonDescription.addElement(descElement);
|
97
|
}else{
|
98
|
String message = "Description is empty. Description item will not be imported.";
|
99
|
fireWarningEvent(message, item, 4);
|
100
|
}
|
101
|
|
102
|
MappedCdmBase mcb = new MappedCdmBase(item.term, csv.get(CORE_ID), taxon);
|
103
|
resultList.add(mcb);
|
104
|
}else{
|
105
|
String message = "Taxon is not available for id '%s'";
|
106
|
message = String.format(message, id);
|
107
|
fireWarningEvent(message, item, 12);
|
108
|
}
|
109
|
|
110
|
return new ListReader<MappedCdmBase>(resultList);
|
111
|
}
|
112
|
|
113
|
|
114
|
private Language getLanguage(StreamItem item) {
|
115
|
//TODO
|
116
|
|
117
|
Language language = Language.DEFAULT();
|
118
|
String langString = item.get(TermUri.DC_LANGUAGE);
|
119
|
|
120
|
/*if (langString != null) {
|
121
|
if (!langString.equals("")) {
|
122
|
language = getTermService().getLanguageByIso(langString.substring(0, 2));
|
123
|
//can getTermService from StreamImportBase which calls GbifDescriptionCsv2CdmConverter.map
|
124
|
}
|
125
|
}*/
|
126
|
return language;
|
127
|
}
|
128
|
|
129
|
/**
|
130
|
* Determines the feature by the dc:type attribute. Tries to reuse existing
|
131
|
* features.
|
132
|
* @param item
|
133
|
* @param resultList
|
134
|
* @return
|
135
|
*/
|
136
|
private Feature getFeatureByDcType(StreamItem item, List<MappedCdmBase> resultList) {
|
137
|
String descriptionType = item.get(TermUri.DC_TYPE);
|
138
|
item.remove(TermUri.DC_TYPE);
|
139
|
|
140
|
try {
|
141
|
Feature feature = state.getTransformer().getFeatureByKey(descriptionType);
|
142
|
if (feature != null){
|
143
|
return feature;
|
144
|
}
|
145
|
String namespace = Feature.class.getCanonicalName();
|
146
|
List<Feature> features = state.get(namespace, descriptionType, Feature.class);
|
147
|
if (features.size() > 1){
|
148
|
String message = "There is more than 1 cdm entity matching given locationId '%s'. I take an arbitrary one.";
|
149
|
fireWarningEvent(String.format(message, item), item, 4);
|
150
|
return features.iterator().next();
|
151
|
}
|
152
|
UUID featureUuid = state.getTransformer().getFeatureUuid(descriptionType);
|
153
|
feature = state.getCurrentIO().getFeature(state, featureUuid, descriptionType, descriptionType, null, null);
|
154
|
if (feature == null){
|
155
|
feature = Feature.NewInstance(descriptionType, descriptionType, null);
|
156
|
feature.setSupportsTextData(true);
|
157
|
// state.putMapping(namespace, type, feature);
|
158
|
state.getCurrentIO().saveNewTerm(feature);
|
159
|
MappedCdmBase mcb = new MappedCdmBase(namespace, descriptionType, feature);
|
160
|
resultList.add(mcb);
|
161
|
}
|
162
|
return feature;
|
163
|
} catch (UndefinedTransformerMethodException e) {
|
164
|
String message = "GetFeature not yet supported by DwcA-Transformer. This should not have happend. Please contact your application developer.";
|
165
|
fireWarningEvent(message, item, 8);
|
166
|
return null;
|
167
|
}
|
168
|
|
169
|
}
|
170
|
|
171
|
@Override
|
172
|
public String getSourceId(StreamItem item) {
|
173
|
String id = item.get(CORE_ID);
|
174
|
return id;
|
175
|
}
|
176
|
|
177
|
|
178
|
//********************** PARTITIONABLE **************************************/
|
179
|
|
180
|
@Override
|
181
|
protected void makeForeignKeysForItem(StreamItem item, Map<String, Set<String>> fkMap) {
|
182
|
String value;
|
183
|
String key;
|
184
|
if ( hasValue(value = item.get(CORE_ID))){
|
185
|
key = TermUri.DWC_TAXON.toString();
|
186
|
Set<String> keySet = getKeySet(key, fkMap);
|
187
|
keySet.add(value);
|
188
|
}
|
189
|
}
|
190
|
|
191
|
|
192
|
@Override
|
193
|
public Set<String> requiredSourceNamespaces() {
|
194
|
Set<String> result = new HashSet<String>();
|
195
|
result.add(TermUri.DWC_TAXON.toString());
|
196
|
return result;
|
197
|
}
|
198
|
|
199
|
//******************* TO STRING ******************************************/
|
200
|
|
201
|
@Override
|
202
|
public String toString(){
|
203
|
return this.getClass().getName();
|
204
|
}
|
205
|
|
206
|
|
207
|
}
|