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.log4j.Logger;
|
19
|
|
20
|
import eu.etaxonomy.cdm.io.common.TdwgAreaProvider;
|
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.description.Distribution;
|
26
|
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
|
27
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
28
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
29
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
30
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
31
|
|
32
|
/**
|
33
|
* @author a.mueller
|
34
|
* @date 22.11.2011
|
35
|
*
|
36
|
*/
|
37
|
public class GbifDistributionCsv2CdmConverter extends PartitionableConverterBase<DwcaDataImportConfiguratorBase, DwcaDataImportStateBase<DwcaDataImportConfiguratorBase>>
|
38
|
implements IPartitionableConverter<StreamItem, IReader<CdmBase>, String>{
|
39
|
|
40
|
@SuppressWarnings("unused")
|
41
|
private static final Logger logger = Logger.getLogger(GbifDistributionCsv2CdmConverter.class);
|
42
|
|
43
|
private static final String CORE_ID = "coreId";
|
44
|
|
45
|
/**
|
46
|
* @param state
|
47
|
*/
|
48
|
public GbifDistributionCsv2CdmConverter(DwcaDataImportStateBase state) {
|
49
|
super(state);
|
50
|
}
|
51
|
|
52
|
@Override
|
53
|
public IReader<MappedCdmBase<? extends CdmBase>> map(StreamItem item ){
|
54
|
List<MappedCdmBase<? extends CdmBase>> resultList = new ArrayList<>();
|
55
|
|
56
|
Map<String, String> csv = item.map;
|
57
|
Reference sourceReference = state.getTransactionalSourceReference();
|
58
|
String sourceReferecenDetail = null;
|
59
|
|
60
|
String id = getSourceId(item);
|
61
|
Taxon taxon = getTaxonBase(id, item, Taxon.class, state);
|
62
|
if (taxon != null){
|
63
|
|
64
|
String locality = item.get(TermUri.DWC_LOCALITY);
|
65
|
String locationId = item.get(TermUri.DWC_LOCATION_ID);
|
66
|
NamedArea area = getAreaByLocationId(item, locationId, locality, resultList);
|
67
|
if (area != null){
|
68
|
MappedCdmBase<? extends CdmBase> mcb = new MappedCdmBase<>(TermUri.DWC_LOCATION_ID, csv.get(TermUri.DWC_LOCATION_ID.toString()), area);
|
69
|
resultList.add(mcb);
|
70
|
}else if (! config.isExcludeLocality()){
|
71
|
area = getAreaByLocality(item, locality);
|
72
|
MappedCdmBase<? extends CdmBase> mcb = new MappedCdmBase<>(TermUri.DWC_LOCALITY, csv.get(TermUri.DWC_LOCALITY.toString()), area);
|
73
|
resultList.add(mcb);
|
74
|
}
|
75
|
|
76
|
if (area != null){
|
77
|
|
78
|
TaxonDescription desc = getTaxonDescription(taxon, false);
|
79
|
|
80
|
PresenceAbsenceTerm status = null;
|
81
|
String establishmentMeans = item.get(TermUri.DWC_ESTABLISHMENT_MEANS);
|
82
|
String occurrenceStatus = item.get(TermUri.DWC_OCCURRENCE_STATUS);
|
83
|
if (isBlank(establishmentMeans) && isBlank(occurrenceStatus)){
|
84
|
status = PresenceAbsenceTerm.PRESENT();
|
85
|
}else{
|
86
|
//FIXME TODO status
|
87
|
}
|
88
|
Distribution distribution = Distribution.NewInstance(area, status);
|
89
|
desc.addElement(distribution);
|
90
|
|
91
|
//save taxon
|
92
|
MappedCdmBase<? extends CdmBase> mcb = new MappedCdmBase<>(item.term, csv.get(CORE_ID.toString()), taxon);
|
93
|
resultList.add(mcb);
|
94
|
}
|
95
|
|
96
|
}else{
|
97
|
String message = "Can't retrieve taxon from database for id '%s'";
|
98
|
fireWarningEvent(String.format(message, id), item, 12);
|
99
|
}
|
100
|
|
101
|
//return
|
102
|
return new ListReader<>(resultList);
|
103
|
}
|
104
|
|
105
|
|
106
|
|
107
|
private NamedArea getAreaByLocality(StreamItem item, String locality) {
|
108
|
String namespace = TermUri.DWC_LOCALITY.toString();
|
109
|
List<NamedArea> result = state.get(namespace, locality, NamedArea.class);
|
110
|
if (result.isEmpty()){
|
111
|
NamedArea newArea = NamedArea.NewInstance(locality, locality, locality);
|
112
|
newArea.setTitleCache(locality, true);
|
113
|
return newArea;
|
114
|
}
|
115
|
if (result.size() > 1){
|
116
|
String message = "There is more than 1 cdm entity matching given locality '%s'. I take an arbitrary one.";
|
117
|
fireWarningEvent(String.format(message, locality), item, 4);
|
118
|
}
|
119
|
return result.iterator().next();
|
120
|
}
|
121
|
|
122
|
private NamedArea getAreaByLocationId(StreamItem item, String locationId, String newLabel, List<MappedCdmBase<? extends CdmBase>> resultList) {
|
123
|
String namespace = TermUri.DWC_LOCATION_ID.toString();
|
124
|
if (isBlank(locationId)){
|
125
|
return null;
|
126
|
}
|
127
|
List<NamedArea> result = state.get(namespace, locationId, NamedArea.class);
|
128
|
try{
|
129
|
if (result.isEmpty()){
|
130
|
NamedArea newArea = state.getTransformer().getNamedAreaByKey(locationId);
|
131
|
if (newArea != null){
|
132
|
return newArea;
|
133
|
}
|
134
|
//try to find in cdm
|
135
|
newArea = getTdwgArea(locationId);
|
136
|
if (newArea != null){
|
137
|
return newArea;
|
138
|
}
|
139
|
|
140
|
String label = isNotBlank(newLabel)? newLabel : locationId;
|
141
|
UUID namedAreaUuid = state.getTransformer().getNamedAreaUuid(locationId);
|
142
|
newArea = state.getCurrentIO().getNamedArea(state, namedAreaUuid, label, label, locationId, null);
|
143
|
|
144
|
//should not happen
|
145
|
if (newArea == null){
|
146
|
newArea = NamedArea.NewInstance(label, label, locationId);
|
147
|
// state.putMapping(namespace, type, newArea);
|
148
|
state.getCurrentIO().saveNewTerm(newArea);
|
149
|
MappedCdmBase<? extends CdmBase> mcb = new MappedCdmBase<>(namespace, locationId, newArea);
|
150
|
resultList.add(mcb);
|
151
|
}
|
152
|
|
153
|
|
154
|
state.putMapping(namespace, locationId, newArea);
|
155
|
return newArea;
|
156
|
}
|
157
|
if (result.size() > 1){
|
158
|
String message = "There is more than 1 cdm entity matching given locationId '%s'. I take an arbitrary one.";
|
159
|
fireWarningEvent(String.format(message, locationId), item, 4);
|
160
|
}
|
161
|
return result.iterator().next();
|
162
|
} catch (UndefinedTransformerMethodException e) {
|
163
|
String message = "GetNamedArea not yet supported by DwcA-Transformer. This should not have happend. Please contact your application developer.";
|
164
|
fireWarningEvent(message, item, 8);
|
165
|
return null;
|
166
|
}
|
167
|
}
|
168
|
|
169
|
/**
|
170
|
* @param locationId
|
171
|
* @return
|
172
|
*/
|
173
|
protected NamedArea getTdwgArea(String locationId) {
|
174
|
if (locationId == null){
|
175
|
return null;
|
176
|
}else if (locationId.startsWith("TDWG:")){
|
177
|
locationId = locationId.substring("TDWG:".length()); //CoL case
|
178
|
}
|
179
|
return TdwgAreaProvider.getAreaByTdwgAbbreviation(locationId);
|
180
|
}
|
181
|
|
182
|
@Override
|
183
|
public String getSourceId(StreamItem item) {
|
184
|
String id = item.get(CORE_ID);
|
185
|
return id;
|
186
|
}
|
187
|
|
188
|
|
189
|
//********************** PARTITIONABLE **************************************/
|
190
|
|
191
|
@Override
|
192
|
protected void makeForeignKeysForItem(StreamItem item, Map<String, Set<String>> fkMap) {
|
193
|
String value;
|
194
|
String key;
|
195
|
//taxon
|
196
|
if ( hasValue(value = item.get(CORE_ID))){
|
197
|
key = TermUri.DWC_TAXON.toString();
|
198
|
Set<String> keySet = getKeySet(key, fkMap);
|
199
|
keySet.add(value);
|
200
|
}
|
201
|
|
202
|
//areaId
|
203
|
|
204
|
String locationId = item.get(TermUri.DWC_LOCATION_ID);
|
205
|
if ( hasValue(value = locationId)){
|
206
|
key = TermUri.DWC_LOCATION_ID.toString();
|
207
|
Set<String> keySet = getKeySet(key, fkMap);
|
208
|
keySet.add(value);
|
209
|
}
|
210
|
|
211
|
}
|
212
|
|
213
|
|
214
|
@Override
|
215
|
public Set<String> requiredSourceNamespaces() {
|
216
|
Set<String> result = new HashSet<>();
|
217
|
result.add(TermUri.DWC_TAXON.toString());
|
218
|
result.add(TermUri.DWC_LOCATION_ID.toString());
|
219
|
result.add(TermUri.DWC_LOCALITY.toString());
|
220
|
return result;
|
221
|
}
|
222
|
|
223
|
//******************* TO STRING ******************************************/
|
224
|
|
225
|
@Override
|
226
|
public String toString(){
|
227
|
return this.getClass().getName();
|
228
|
}
|
229
|
|
230
|
|
231
|
}
|