1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2009 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
package eu.etaxonomy.cdm.io.dwca.in;
|
11
|
|
12
|
import java.util.ArrayList;
|
13
|
import java.util.HashSet;
|
14
|
import java.util.List;
|
15
|
import java.util.Map;
|
16
|
import java.util.Set;
|
17
|
|
18
|
import org.apache.log4j.Logger;
|
19
|
|
20
|
import eu.etaxonomy.cdm.io.common.TdwgAreaProvider;
|
21
|
import eu.etaxonomy.cdm.io.dwca.TermUri;
|
22
|
import eu.etaxonomy.cdm.io.stream.StreamItem;
|
23
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
24
|
import eu.etaxonomy.cdm.model.description.Distribution;
|
25
|
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
|
26
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
27
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
28
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
29
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
30
|
|
31
|
/**
|
32
|
* @author a.mueller
|
33
|
* @date 22.11.2011
|
34
|
*
|
35
|
*/
|
36
|
public class GbifDistributionCsv2CdmConverter extends PartitionableConverterBase<DwcaDataImportConfiguratorBase, DwcaDataImportStateBase<DwcaDataImportConfiguratorBase>>
|
37
|
implements IPartitionableConverter<StreamItem, IReader<CdmBase>, String>{
|
38
|
|
39
|
@SuppressWarnings("unused")
|
40
|
private static final Logger logger = Logger.getLogger(GbifDistributionCsv2CdmConverter.class);
|
41
|
|
42
|
private static final String CORE_ID = "coreId";
|
43
|
|
44
|
/**
|
45
|
* @param state
|
46
|
*/
|
47
|
public GbifDistributionCsv2CdmConverter(DwcaDataImportStateBase state) {
|
48
|
super(state);
|
49
|
}
|
50
|
|
51
|
@Override
|
52
|
public IReader<MappedCdmBase> map(StreamItem item ){
|
53
|
List<MappedCdmBase> resultList = new ArrayList<MappedCdmBase>();
|
54
|
|
55
|
Map<String, String> csv = item.map;
|
56
|
Reference sourceReference = state.getTransactionalSourceReference();
|
57
|
String sourceReferecenDetail = null;
|
58
|
|
59
|
String id = getSourceId(item);
|
60
|
Taxon taxon = getTaxonBase(id, item, Taxon.class, state);
|
61
|
if (taxon != null){
|
62
|
|
63
|
String locationId = item.get(TermUri.DWC_LOCATION_ID);
|
64
|
NamedArea area = getAreaByLocationId(item, locationId);
|
65
|
if (area != null){
|
66
|
MappedCdmBase mcb = new MappedCdmBase(item.term, csv.get(TermUri.DWC_LOCATION_ID), area);
|
67
|
resultList.add(mcb);
|
68
|
}else if (! config.isExcludeLocality()){
|
69
|
String locality = item.get(TermUri.DWC_LOCALITY);
|
70
|
area = getAreaByLocality(item, locality);
|
71
|
MappedCdmBase mcb = new MappedCdmBase(item.term, csv.get(TermUri.DWC_LOCALITY), area);
|
72
|
resultList.add(mcb);
|
73
|
}
|
74
|
|
75
|
if (area != null){
|
76
|
|
77
|
//TODO language, area,
|
78
|
TaxonDescription desc = getTaxonDescription(taxon, false);
|
79
|
|
80
|
//TODO
|
81
|
PresenceAbsenceTerm status = null;
|
82
|
Distribution distribution = Distribution.NewInstance(area, status);
|
83
|
desc.addElement(distribution);
|
84
|
|
85
|
//save taxon
|
86
|
MappedCdmBase mcb = new MappedCdmBase(item.term, csv.get(CORE_ID), taxon);
|
87
|
resultList.add(mcb);
|
88
|
}
|
89
|
|
90
|
}else{
|
91
|
String message = "Can't retrieve taxon from database for id '%s'";
|
92
|
fireWarningEvent(String.format(message, id), item, 12);
|
93
|
}
|
94
|
|
95
|
//return
|
96
|
return new ListReader<MappedCdmBase>(resultList);
|
97
|
}
|
98
|
|
99
|
|
100
|
|
101
|
private NamedArea getAreaByLocality(StreamItem item, String locality) {
|
102
|
String namespace = TermUri.DWC_LOCALITY.toString();
|
103
|
List<NamedArea> result = state.get(namespace, locality, NamedArea.class);
|
104
|
if (result.isEmpty()){
|
105
|
NamedArea newArea = NamedArea.NewInstance(locality, locality, locality);
|
106
|
newArea.setTitleCache(locality, true);
|
107
|
return newArea;
|
108
|
}
|
109
|
if (result.size() > 1){
|
110
|
String message = "There is more than 1 cdm entity matching given locality '%s'. I take an arbitrary one.";
|
111
|
fireWarningEvent(String.format(message, locality), item, 4);
|
112
|
}
|
113
|
return result.iterator().next();
|
114
|
}
|
115
|
|
116
|
private NamedArea getAreaByLocationId(StreamItem item, String locationId) {
|
117
|
String namespace = TermUri.DWC_LOCATION_ID.toString();
|
118
|
List<NamedArea> result = state.get(namespace, locationId, NamedArea.class);
|
119
|
if (result.isEmpty()){
|
120
|
//try to find in cdm
|
121
|
NamedArea newArea = TdwgAreaProvider.getAreaByTdwgAbbreviation(locationId);
|
122
|
if (newArea == null){
|
123
|
// state.getCurrentIO().getTermService().findByAreaCode
|
124
|
}
|
125
|
if (newArea == null){
|
126
|
newArea = NamedArea.NewInstance(locationId, locationId, locationId);
|
127
|
}
|
128
|
|
129
|
return newArea;
|
130
|
}
|
131
|
if (result.size() > 1){
|
132
|
String message = "There is more than 1 cdm entity matching given locationId '%s'. I take an arbitrary one.";
|
133
|
fireWarningEvent(String.format(message, locationId), item, 4);
|
134
|
}
|
135
|
return result.iterator().next();
|
136
|
}
|
137
|
|
138
|
@Override
|
139
|
public String getSourceId(StreamItem item) {
|
140
|
String id = item.get(CORE_ID);
|
141
|
return id;
|
142
|
}
|
143
|
|
144
|
|
145
|
//********************** PARTITIONABLE **************************************/
|
146
|
|
147
|
@Override
|
148
|
protected void makeForeignKeysForItem(StreamItem item, Map<String, Set<String>> fkMap) {
|
149
|
String value;
|
150
|
String key;
|
151
|
//taxon
|
152
|
if ( hasValue(value = item.get(CORE_ID))){
|
153
|
key = TermUri.DWC_TAXON.toString();
|
154
|
Set<String> keySet = getKeySet(key, fkMap);
|
155
|
keySet.add(value);
|
156
|
}
|
157
|
|
158
|
//areaId
|
159
|
|
160
|
String locationId = item.get(TermUri.DWC_LOCATION_ID);
|
161
|
if ( hasValue(value = locationId)){
|
162
|
key = TermUri.DWC_LOCATION_ID.toString();
|
163
|
Set<String> keySet = getKeySet(key, fkMap);
|
164
|
keySet.add(value);
|
165
|
}
|
166
|
|
167
|
}
|
168
|
|
169
|
|
170
|
@Override
|
171
|
public Set<String> requiredSourceNamespaces() {
|
172
|
Set<String> result = new HashSet<String>();
|
173
|
result.add(TermUri.DWC_TAXON.toString());
|
174
|
result.add(TermUri.DWC_LOCATION_ID.toString());
|
175
|
return result;
|
176
|
}
|
177
|
|
178
|
//******************* TO STRING ******************************************/
|
179
|
|
180
|
@Override
|
181
|
public String toString(){
|
182
|
return this.getClass().getName();
|
183
|
}
|
184
|
|
185
|
|
186
|
}
|