Revision 0a5679a4
Added by Andreas Müller over 6 years ago
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/dwca/in/GbifDistributionCsv2CdmConverter.java | ||
---|---|---|
13 | 13 |
import java.util.List; |
14 | 14 |
import java.util.Map; |
15 | 15 |
import java.util.Set; |
16 |
import java.util.UUID; |
|
16 | 17 |
|
17 | 18 |
import org.apache.log4j.Logger; |
18 | 19 |
|
19 | 20 |
import eu.etaxonomy.cdm.io.common.TdwgAreaProvider; |
21 |
import eu.etaxonomy.cdm.io.common.mapping.UndefinedTransformerMethodException; |
|
20 | 22 |
import eu.etaxonomy.cdm.io.dwca.TermUri; |
21 | 23 |
import eu.etaxonomy.cdm.io.stream.StreamItem; |
22 | 24 |
import eu.etaxonomy.cdm.model.common.CdmBase; |
... | ... | |
59 | 61 |
Taxon taxon = getTaxonBase(id, item, Taxon.class, state); |
60 | 62 |
if (taxon != null){ |
61 | 63 |
|
64 |
String locality = item.get(TermUri.DWC_LOCALITY); |
|
62 | 65 |
String locationId = item.get(TermUri.DWC_LOCATION_ID); |
63 |
NamedArea area = getAreaByLocationId(item, locationId); |
|
66 |
NamedArea area = getAreaByLocationId(item, locationId, locality, resultList);
|
|
64 | 67 |
if (area != null){ |
65 |
MappedCdmBase<? extends CdmBase> mcb = new MappedCdmBase<>(item.term, csv.get(TermUri.DWC_LOCATION_ID), area);
|
|
68 |
MappedCdmBase<? extends CdmBase> mcb = new MappedCdmBase<>(TermUri.DWC_LOCATION_ID, csv.get(TermUri.DWC_LOCATION_ID.toString()), area);
|
|
66 | 69 |
resultList.add(mcb); |
67 | 70 |
}else if (! config.isExcludeLocality()){ |
68 |
String locality = item.get(TermUri.DWC_LOCALITY); |
|
69 | 71 |
area = getAreaByLocality(item, locality); |
70 |
MappedCdmBase<? extends CdmBase> mcb = new MappedCdmBase<>(item.term, csv.get(TermUri.DWC_LOCALITY), area);
|
|
72 |
MappedCdmBase<? extends CdmBase> mcb = new MappedCdmBase<>(TermUri.DWC_LOCALITY, csv.get(TermUri.DWC_LOCALITY.toString()), area);
|
|
71 | 73 |
resultList.add(mcb); |
72 | 74 |
} |
73 | 75 |
|
74 | 76 |
if (area != null){ |
75 | 77 |
|
76 |
//TODO language, area, |
|
77 | 78 |
TaxonDescription desc = getTaxonDescription(taxon, false); |
78 | 79 |
|
79 |
//TODO |
|
80 | 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 |
} |
|
81 | 88 |
Distribution distribution = Distribution.NewInstance(area, status); |
82 | 89 |
desc.addElement(distribution); |
83 | 90 |
|
84 | 91 |
//save taxon |
85 |
MappedCdmBase<? extends CdmBase> mcb = new MappedCdmBase<>(item.term, csv.get(CORE_ID), taxon); |
|
92 |
MappedCdmBase<? extends CdmBase> mcb = new MappedCdmBase<>(item.term, csv.get(CORE_ID.toString()), taxon);
|
|
86 | 93 |
resultList.add(mcb); |
87 | 94 |
} |
88 | 95 |
|
... | ... | |
112 | 119 |
return result.iterator().next(); |
113 | 120 |
} |
114 | 121 |
|
115 |
private NamedArea getAreaByLocationId(StreamItem item, String locationId) { |
|
122 |
private NamedArea getAreaByLocationId(StreamItem item, String locationId, String newLabel, List<MappedCdmBase<? extends CdmBase>> resultList) {
|
|
116 | 123 |
String namespace = TermUri.DWC_LOCATION_ID.toString(); |
117 |
if (locationId == null){
|
|
124 |
if (isBlank(locationId)){
|
|
118 | 125 |
return null; |
119 | 126 |
} |
120 | 127 |
List<NamedArea> result = state.get(namespace, locationId, NamedArea.class); |
121 |
if (result.isEmpty()){ |
|
122 |
//try to find in cdm |
|
123 |
NamedArea newArea = getTdwgArea(locationId); |
|
124 |
if (newArea == null){ |
|
125 |
//TODO could be idInVocabulary (with not voc given), abbrevTitle of any representation, ... |
|
126 |
// state.getCurrentIO().getTermService().listByCode |
|
127 |
} |
|
128 |
if (newArea == null){ |
|
129 |
newArea = NamedArea.NewInstance(locationId, locationId, locationId); |
|
130 |
} |
|
131 |
|
|
132 |
state.putMapping(namespace, locationId, newArea); |
|
133 |
return newArea; |
|
134 |
} |
|
135 |
if (result.size() > 1){ |
|
136 |
String message = "There is more than 1 cdm entity matching given locationId '%s'. I take an arbitrary one."; |
|
137 |
fireWarningEvent(String.format(message, locationId), item, 4); |
|
138 |
} |
|
139 |
return result.iterator().next(); |
|
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 |
} |
|
140 | 167 |
} |
141 | 168 |
|
142 | 169 |
/** |
... | ... | |
189 | 216 |
Set<String> result = new HashSet<>(); |
190 | 217 |
result.add(TermUri.DWC_TAXON.toString()); |
191 | 218 |
result.add(TermUri.DWC_LOCATION_ID.toString()); |
219 |
result.add(TermUri.DWC_LOCALITY.toString()); |
|
192 | 220 |
return result; |
193 | 221 |
} |
194 | 222 |
|
Also available in: Unified diff
ref #6887 and ref #5173 improve area handling for CoL distribution import