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.berlinModel.in;
|
11
|
|
12
|
import java.sql.ResultSet;
|
13
|
import java.sql.SQLException;
|
14
|
import java.util.HashMap;
|
15
|
import java.util.HashSet;
|
16
|
import java.util.List;
|
17
|
import java.util.Map;
|
18
|
import java.util.Set;
|
19
|
|
20
|
import org.apache.log4j.Logger;
|
21
|
import org.springframework.stereotype.Component;
|
22
|
|
23
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
24
|
import eu.etaxonomy.cdm.io.berlinModel.in.validation.BerlinModelOccurrenceSourceImportValidator;
|
25
|
import eu.etaxonomy.cdm.io.common.IOValidator;
|
26
|
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
|
27
|
import eu.etaxonomy.cdm.io.common.Source;
|
28
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
29
|
import eu.etaxonomy.cdm.model.common.OriginalSourceType;
|
30
|
import eu.etaxonomy.cdm.model.common.RelationshipBase.Direction;
|
31
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
32
|
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
|
33
|
import eu.etaxonomy.cdm.model.description.Distribution;
|
34
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
35
|
import eu.etaxonomy.cdm.model.name.NameRelationshipType;
|
36
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
37
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
38
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
39
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
40
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
41
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
42
|
|
43
|
|
44
|
/**
|
45
|
* @author a.mueller
|
46
|
* @since 20.03.2008
|
47
|
*/
|
48
|
@Component
|
49
|
public class BerlinModelOccurrenceSourceImport extends BerlinModelImportBase {
|
50
|
|
51
|
/**
|
52
|
*
|
53
|
*/
|
54
|
private static final String EXACT = "(exact) ";
|
55
|
|
56
|
private static final long serialVersionUID = 1139543760239436841L;
|
57
|
|
58
|
private static final Logger logger = Logger.getLogger(BerlinModelOccurrenceSourceImport.class);
|
59
|
|
60
|
private static int modCount = 5000;
|
61
|
private static final String pluralString = "occurrence sources";
|
62
|
private static final String dbTableName = "emOccurrenceSource"; //??
|
63
|
|
64
|
|
65
|
private Map<String, Integer> sourceNumberRefIdMap;
|
66
|
private Map<String, Set<Integer>> nameCache2NameIdMap;
|
67
|
private Set<String> notFoundReferences = new HashSet<>();
|
68
|
|
69
|
|
70
|
public BerlinModelOccurrenceSourceImport(){
|
71
|
super(dbTableName, pluralString);
|
72
|
}
|
73
|
|
74
|
@Override
|
75
|
protected String getIdQuery(BerlinModelImportState state) {
|
76
|
String result = "SELECT occurrenceSourceId FROM " + getTableName();
|
77
|
if (state.getConfig().getOccurrenceSourceFilter() != null){
|
78
|
result += " WHERE " + state.getConfig().getOccurrenceSourceFilter();
|
79
|
}
|
80
|
return result;
|
81
|
}
|
82
|
|
83
|
@Override
|
84
|
protected String getRecordQuery(BerlinModelImportConfigurator config) {
|
85
|
String strQuery = //DISTINCT because otherwise emOccurrenceSource creates multiple records for a single distribution
|
86
|
" SELECT * " +
|
87
|
" FROM emOccurrenceSource " +
|
88
|
" WHERE (OccurrenceSourceId IN (" + ID_LIST_TOKEN + ") )" +
|
89
|
"";
|
90
|
return strQuery;
|
91
|
}
|
92
|
|
93
|
|
94
|
|
95
|
@Override
|
96
|
protected void doInvoke(BerlinModelImportState state) {
|
97
|
notFoundReferences = new HashSet<>();
|
98
|
|
99
|
try {
|
100
|
sourceNumberRefIdMap = makeSourceNumberReferenceIdMap(state);
|
101
|
nameCache2NameIdMap = makeNameCache2NameIdMap(state);
|
102
|
} catch (SQLException e) {
|
103
|
e.printStackTrace();
|
104
|
throw new RuntimeException(e);
|
105
|
}
|
106
|
super.doInvoke(state);
|
107
|
sourceNumberRefIdMap = null;
|
108
|
nameCache2NameIdMap = null;
|
109
|
if (notFoundReferences.size()>0){
|
110
|
String unfound = "'" + CdmUtils.concat("','", notFoundReferences.toArray(new String[]{})) + "'";
|
111
|
logger.warn("Not found references: " + unfound);
|
112
|
}
|
113
|
return;
|
114
|
}
|
115
|
|
116
|
@Override
|
117
|
public boolean doPartition(@SuppressWarnings("rawtypes") ResultSetPartitioner partitioner, BerlinModelImportState state) {
|
118
|
boolean success = true;
|
119
|
ResultSet rs = partitioner.getResultSet();
|
120
|
@SuppressWarnings("unchecked")
|
121
|
Map<String, Reference> refMap = partitioner.getObjectMap(BerlinModelReferenceImport.REFERENCE_NAMESPACE);
|
122
|
|
123
|
Set<DescriptionElementBase> objectsToSave = new HashSet<>();
|
124
|
try {
|
125
|
int i = 0;
|
126
|
//for each reference
|
127
|
while (rs.next()){
|
128
|
|
129
|
if ((i++ % modCount) == 0 && i!= 1 ){ logger.info("occurrence sources handled: " + (i-1));}
|
130
|
|
131
|
Integer occurrenceSourceId = rs.getInt("OccurrenceSourceId");
|
132
|
Integer occurrenceFk =nullSafeInt(rs, "OccurrenceFk");
|
133
|
String sourceNumber = rs.getString("SourceNumber");
|
134
|
String oldName = rs.getString("OldName");
|
135
|
Integer oldNameFk = nullSafeInt(rs, "OldNameFk");
|
136
|
|
137
|
Distribution distribution = (Distribution)state.getRelatedObject(BerlinModelOccurrenceImport.NAMESPACE, String.valueOf(occurrenceFk));
|
138
|
|
139
|
if (distribution == null){
|
140
|
//distribution = duplicateMap.get(occurrenceFk);
|
141
|
}
|
142
|
if (distribution != null){
|
143
|
Integer refId = sourceNumberRefIdMap.get(sourceNumber);
|
144
|
Reference ref = refMap.get(String.valueOf(refId));
|
145
|
|
146
|
if (ref != null){
|
147
|
DescriptionElementSource originalSource = DescriptionElementSource.NewInstance(OriginalSourceType.PrimaryTaxonomicSource);
|
148
|
originalSource.setCitation(ref);
|
149
|
TaxonName taxonName;
|
150
|
taxonName = TaxonName.castAndDeproxy(getName(state, oldName, oldNameFk, occurrenceSourceId, distribution));
|
151
|
if (taxonName != null){
|
152
|
if(isNotBlank(oldName) && !oldName.equals(taxonName.getNameCache())){
|
153
|
originalSource.setOriginalNameString(oldName);
|
154
|
}
|
155
|
originalSource.setNameUsedInSource(taxonName);
|
156
|
}else if(isNotBlank(oldName)){
|
157
|
originalSource.setOriginalNameString(oldName);
|
158
|
}
|
159
|
distribution.addSource(originalSource);
|
160
|
}else{
|
161
|
logger.warn("reference for sourceNumber "+sourceNumber+" could not be found. OccurrenceSourceId: " + occurrenceSourceId );
|
162
|
notFoundReferences.add(sourceNumber);
|
163
|
}
|
164
|
}else{
|
165
|
logger.warn("distribution ("+occurrenceFk+") for occurrence source (" + occurrenceSourceId + ") could not be found." );
|
166
|
}
|
167
|
|
168
|
}
|
169
|
logger.info("Distributions to save: " + objectsToSave.size());
|
170
|
getDescriptionService().saveDescriptionElement(objectsToSave);
|
171
|
|
172
|
return success;
|
173
|
} catch (SQLException e) {
|
174
|
logger.error("SQLException:" + e);
|
175
|
return false;
|
176
|
}
|
177
|
}
|
178
|
|
179
|
|
180
|
@Override
|
181
|
public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, BerlinModelImportState state) {
|
182
|
String nameSpace;
|
183
|
Class<?> cdmClass;
|
184
|
Set<String> idSet;
|
185
|
Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
|
186
|
|
187
|
try{
|
188
|
Set<String> occurrenceIdSet = new HashSet<>();
|
189
|
Set<String> nameIdSet = new HashSet<>();
|
190
|
Set<String> sourceNumberSet = new HashSet<>();
|
191
|
Set<String> oldNamesSet = new HashSet<>();
|
192
|
while (rs.next()){
|
193
|
handleForeignKey(rs, occurrenceIdSet, "occurrenceFk");
|
194
|
handleForeignKey(rs, nameIdSet, "oldNameFk");
|
195
|
sourceNumberSet.add(CdmUtils.NzTrim(rs.getString("SourceNumber")));
|
196
|
oldNamesSet.add(CdmUtils.NzTrim(rs.getString("oldName")));
|
197
|
}
|
198
|
|
199
|
sourceNumberSet.remove("");
|
200
|
Set<String> referenceIdSet = handleSourceNumber(sourceNumberSet);
|
201
|
oldNamesSet.remove("");
|
202
|
Set<String> oldNameIdSet = handleOldNames(oldNamesSet);
|
203
|
nameIdSet.addAll(oldNameIdSet);
|
204
|
|
205
|
//occurrence map
|
206
|
nameSpace = BerlinModelOccurrenceImport.NAMESPACE;
|
207
|
cdmClass = Distribution.class;
|
208
|
idSet = occurrenceIdSet;
|
209
|
@SuppressWarnings("unchecked")
|
210
|
Map<String, Distribution> occurrenceMap = (Map<String, Distribution>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
|
211
|
result.put(nameSpace, occurrenceMap);
|
212
|
|
213
|
//name map
|
214
|
nameSpace = BerlinModelTaxonNameImport.NAMESPACE;
|
215
|
cdmClass = TaxonName.class;
|
216
|
idSet =nameIdSet;
|
217
|
@SuppressWarnings("unchecked")
|
218
|
Map<String, TaxonName> nameMap = (Map<String, TaxonName>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
|
219
|
result.put(nameSpace, nameMap);
|
220
|
|
221
|
//reference map
|
222
|
nameSpace = BerlinModelReferenceImport.REFERENCE_NAMESPACE;
|
223
|
cdmClass = Reference.class;
|
224
|
idSet = referenceIdSet;
|
225
|
@SuppressWarnings("unchecked")
|
226
|
Map<String, Reference> referenceMap = (Map<String, Reference>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
|
227
|
result.put(nameSpace, referenceMap);
|
228
|
|
229
|
} catch (SQLException e) {
|
230
|
throw new RuntimeException(e);
|
231
|
}
|
232
|
return result;
|
233
|
}
|
234
|
|
235
|
private Set<String> handleSourceNumber(Set<String> sourceNumberSet) {
|
236
|
Map<String, Integer> sourceNumberReferenceIdMap = this.sourceNumberRefIdMap;
|
237
|
Set<String> referenceIdSet = new HashSet<>();
|
238
|
|
239
|
for(String sourceNumber : sourceNumberSet){
|
240
|
Integer refId = sourceNumberReferenceIdMap.get(sourceNumber);
|
241
|
referenceIdSet.add(String.valueOf(refId));
|
242
|
}
|
243
|
return referenceIdSet;
|
244
|
}
|
245
|
|
246
|
private Set<String> handleOldNames(Set<String> oldNamesSet) {
|
247
|
Set<String> oldNameIdSet = new HashSet<>();
|
248
|
|
249
|
try {
|
250
|
for(String oldName : oldNamesSet){
|
251
|
if (isNotBlank(oldName)){
|
252
|
Set<Integer> nameIds = nameCache2NameIdMap.get(oldName);
|
253
|
if (nameIds != null){
|
254
|
for (Integer nameId : nameIds){
|
255
|
oldNameIdSet.add(String.valueOf(nameId));
|
256
|
}
|
257
|
}
|
258
|
}
|
259
|
}
|
260
|
} catch (Exception e) {
|
261
|
e.printStackTrace();
|
262
|
logger.error("Exception in handleOldNames" + e.getMessage());
|
263
|
}
|
264
|
return oldNameIdSet;
|
265
|
}
|
266
|
|
267
|
|
268
|
|
269
|
/**
|
270
|
* @param state
|
271
|
* @param oldName
|
272
|
* @param oldNameFk
|
273
|
* @return
|
274
|
*/
|
275
|
private TaxonName getName(BerlinModelImportState state, String oldName, Integer oldNameFk, Integer occSourceId, Distribution distribution) {
|
276
|
TaxonName taxonName = (TaxonName)state.getRelatedObject(BerlinModelTaxonNameImport.NAMESPACE, String.valueOf(oldNameFk));
|
277
|
if (oldNameFk != null && taxonName == null){
|
278
|
logger.warn("OldNameFk "+oldNameFk+" exists but taxonName not found for occSource: " + occSourceId);
|
279
|
}
|
280
|
if (isNotBlank(oldName)){
|
281
|
if (taxonName == null){
|
282
|
return handleOldFreetextNameOnly(state, oldName, occSourceId, distribution);
|
283
|
}else if (!oldName.equals(taxonName.getNameCache())){
|
284
|
logger.warn("Old name freetext and linked name nameCache are not equal: " + oldName + "/" + taxonName.getNameCache() + "; occSourceId: " + occSourceId);
|
285
|
checkSynonymie(state, oldNameFk, occSourceId, distribution, taxonName);
|
286
|
return taxonName;
|
287
|
}else{
|
288
|
checkSynonymie(state, oldNameFk, occSourceId, distribution, taxonName);
|
289
|
return taxonName;
|
290
|
}
|
291
|
}else{ //taxonName != null
|
292
|
if (taxonName != null){
|
293
|
checkSynonymie(state, oldNameFk, occSourceId, distribution, taxonName);
|
294
|
}
|
295
|
return taxonName;
|
296
|
}
|
297
|
}
|
298
|
|
299
|
/**
|
300
|
* @param state
|
301
|
* @param oldName
|
302
|
* @param occSourceId
|
303
|
* @param distribution
|
304
|
* @return
|
305
|
*/
|
306
|
protected TaxonName handleOldFreetextNameOnly(BerlinModelImportState state, String oldName, Integer occSourceId,
|
307
|
Distribution distribution) {
|
308
|
Set<TaxonName> names = getOldNames(state, oldName);
|
309
|
if (names.isEmpty()){
|
310
|
if (getNameIds(oldName).isEmpty()){
|
311
|
if (state.getConfig().isLogNotMatchingOldNames()){
|
312
|
logger.warn("No name found for freetext oldName '"+oldName+"'; occSourceId: " + occSourceId);
|
313
|
}
|
314
|
}else{
|
315
|
if (state.getConfig().isLogMatchingNotExportedOldNames()){
|
316
|
logger.warn("Matching name exists in BM but not in CDM. OldName: " + oldName + "; Taxon: "+getTaxonStr(distribution)+"; occSourceId: " + occSourceId);
|
317
|
}
|
318
|
}
|
319
|
return null;
|
320
|
}else {
|
321
|
TaxonName result = names.iterator().next();
|
322
|
boolean checkOldNameIsSynonym = state.getConfig().isCheckOldNameIsSynonym();
|
323
|
if (names.size()> 1){
|
324
|
TaxonName synName = getFirstSynonymName(state, names, distribution, null, occSourceId, true);
|
325
|
if (synName == null){
|
326
|
//TODO should we really use a name if not available in synonymy?
|
327
|
String message = "There is more than one matching oldName for '"+oldName+"' but none of them is a synonym of the accepted taxon '"+getTaxonStr(distribution)+"'.";
|
328
|
message += (!checkOldNameIsSynonym ? "Take arbitrary one. ":"") + "OccSourceId: " + occSourceId;
|
329
|
logger.warn(message);
|
330
|
return checkOldNameIsSynonym ? null : result;
|
331
|
}else{
|
332
|
return synName;
|
333
|
}
|
334
|
}else{
|
335
|
//names.size() = 1
|
336
|
if (checkOldNameIsSynonym){
|
337
|
TaxonName synName = getFirstSynonymName(state, names, distribution, null, occSourceId, true);
|
338
|
if (synName == null){
|
339
|
if (state.getConfig().isCheckOldNameIsSynonym()){
|
340
|
// logger.warn("There is a matching oldName for '"+oldName+"' but it is not a synonym/misapplication of the accepted taxon '"+getTaxonStr(distribution)+"'. OccSourceId: " + occSourceId);
|
341
|
return null;
|
342
|
}else{
|
343
|
return result;
|
344
|
}
|
345
|
}else if (!synName.equals(result)){
|
346
|
//TODO strange, how can this happen if it is the only matching?
|
347
|
logger.warn("There is a matching oldName for '"+oldName+"'("+result.getUuid()+") but another matching name "+synName.getUuid()+"exists in the synonymy of the accepted taxon '"+getTaxonStr(distribution)+"'. OccSourceId: " + occSourceId);
|
348
|
return synName;
|
349
|
}else{
|
350
|
return result;
|
351
|
}
|
352
|
}else{
|
353
|
return result;
|
354
|
}
|
355
|
}
|
356
|
}
|
357
|
}
|
358
|
|
359
|
/**
|
360
|
* @param state
|
361
|
* @param oldNameFk
|
362
|
* @param occSourceId
|
363
|
* @param distribution
|
364
|
* @param taxonName
|
365
|
*/
|
366
|
protected void checkSynonymie(BerlinModelImportState state, Integer oldNameFk, Integer occSourceId,
|
367
|
Distribution distribution, TaxonName taxonName) {
|
368
|
if (state.getConfig().isCheckOldNameIsSynonym()){
|
369
|
Set<TaxonName> names = new HashSet<>();
|
370
|
names.add(taxonName);
|
371
|
boolean hasTaxon = !taxonName.getTaxonBases().isEmpty();
|
372
|
String orphaned = hasTaxon ? "" : "Orphaned name: ";
|
373
|
TaxonName synName = getFirstSynonymName(state, names, distribution, null, occSourceId, false);
|
374
|
if (synName == null){
|
375
|
Set<TaxonName> existingNames = getOldNames(state, taxonName.getNameCache());
|
376
|
existingNames.remove(taxonName);
|
377
|
if (existingNames.isEmpty()){
|
378
|
logger.warn(orphaned + "NameInSource (" + oldNameFk + " - " +taxonName.getTitleCache() + ") could not be found in synonymy of "+getTaxonStr(distribution)+". OccSourceId: " + occSourceId);
|
379
|
}else{
|
380
|
TaxonName existingSynonym = getFirstSynonymName(state, existingNames, distribution, null, occSourceId, false);
|
381
|
if (existingSynonym != null){
|
382
|
boolean isExact = CdmUtils.nullSafeEqual(existingSynonym.getTitleCache(),taxonName.getTitleCache());
|
383
|
String exact = isExact ? EXACT : "";
|
384
|
logger.warn(exact + orphaned + "A similar name ("+existingSynonym.getUuid()+") can be found in synonymy but is not the nameInSource (" + oldNameFk + " - " +taxonName.getTitleCache() + "); Taxon: "+getTaxonStr(distribution)+". OccSourceId: " + occSourceId);
|
385
|
}else{
|
386
|
TaxonName existingMisapplication = getFirstMisapplication(state, existingNames, distribution, occSourceId);
|
387
|
if (existingMisapplication != null){
|
388
|
boolean isExact = CdmUtils.nullSafeEqual(existingMisapplication.getTitleCache(),taxonName.getTitleCache());
|
389
|
String exact = isExact ? EXACT : "";
|
390
|
|
391
|
logger.warn(exact + orphaned + "A similar misapplied name ("+existingMisapplication.getUuid()+") can be found in misapplications but not is not the nameInSource (" + oldNameFk + " - " +taxonName.getTitleCache() + "); Taxon: "+getTaxonStr(distribution)+". OccSourceId: " + occSourceId);
|
392
|
}
|
393
|
}
|
394
|
}
|
395
|
}
|
396
|
}
|
397
|
}
|
398
|
|
399
|
/**
|
400
|
* @param state
|
401
|
* @param names
|
402
|
* @param taxon
|
403
|
* @param taxon
|
404
|
* @return
|
405
|
*/
|
406
|
private TaxonName getFirstSynonymName(BerlinModelImportState state, Set<TaxonName> names, Distribution distribution, Taxon taxon, Integer occSourceId, boolean includeMisapplications) {
|
407
|
TaxonName result = null;
|
408
|
taxon = taxon == null ? getTaxon(distribution): taxon;
|
409
|
Set<Synonym> synonyms = taxon.getSynonyms();
|
410
|
Set<TaxonName> synonymNames = new HashSet<>();
|
411
|
|
412
|
synonymNames.add(taxon.getName());
|
413
|
synonymNames.addAll(getOrthographicVariants(taxon));
|
414
|
|
415
|
for (Synonym synonym : synonyms){
|
416
|
synonymNames.add(synonym.getName());
|
417
|
synonymNames.addAll(getOrthographicVariants(synonym));
|
418
|
}
|
419
|
for (TaxonName name : names){
|
420
|
if (synonymNames.contains(name)){
|
421
|
if (result != null){
|
422
|
logger.warn("There is more than 1 matching synonym/taxon for " + name.getNameCache() + "; occSourceId: " + occSourceId);
|
423
|
}
|
424
|
result = name;
|
425
|
}
|
426
|
}
|
427
|
|
428
|
//parent
|
429
|
if (result == null){
|
430
|
if (taxon.getName().isInfraSpecific()){
|
431
|
if (!taxon.getTaxonNodes().isEmpty()){
|
432
|
TaxonNode parent = taxon.getTaxonNodes().iterator().next().getParent();
|
433
|
if (parent != null && parent.getTaxon() != null){
|
434
|
Set<TaxonName> parentNames = new HashSet<>();
|
435
|
TaxonName parentName = parent.getTaxon().getName();
|
436
|
parentNames.add(parentName);
|
437
|
parentNames.addAll(getOrthographicVariants(parent.getTaxon()));
|
438
|
|
439
|
for (TaxonName name : names){
|
440
|
if (parentNames.contains(name)){
|
441
|
if (result != null){
|
442
|
logger.warn("There is more than 1 matching parent for " + name.getNameCache() + "; occSourceId: " + occSourceId);
|
443
|
}
|
444
|
result = name;
|
445
|
}
|
446
|
}
|
447
|
if (result == null){
|
448
|
TaxonName parentSyn = getFirstSynonymName(state, names, distribution, parent.getTaxon(), occSourceId, includeMisapplications);
|
449
|
if (parentSyn != null){
|
450
|
result = parentSyn;
|
451
|
}
|
452
|
}
|
453
|
}
|
454
|
}
|
455
|
}
|
456
|
}
|
457
|
|
458
|
//child
|
459
|
if (result == null){
|
460
|
if (taxon.getName().isSpecies() || taxon.getName().isSupraSpecific()){
|
461
|
if (!taxon.getTaxonNodes().isEmpty()){
|
462
|
List<TaxonNode> children = taxon.getTaxonNodes().iterator().next().getChildNodes();
|
463
|
Set<TaxonName> childNames = new HashSet<>();
|
464
|
for (TaxonNode child : children){
|
465
|
childNames.add(child.getTaxon().getName());
|
466
|
childNames.addAll(getOrthographicVariants(child.getTaxon()));
|
467
|
}
|
468
|
for (TaxonName name : names){
|
469
|
if (childNames.contains(name)){
|
470
|
if (result != null){
|
471
|
logger.warn("There is more than 1 matching child for " + name.getNameCache() + "; occSourceId: " + occSourceId);
|
472
|
}
|
473
|
result = name;
|
474
|
}
|
475
|
}
|
476
|
}
|
477
|
}
|
478
|
}
|
479
|
|
480
|
if (result == null && includeMisapplications){
|
481
|
result = getFirstMisapplication(state, synonymNames, distribution, occSourceId);
|
482
|
}
|
483
|
|
484
|
return result;
|
485
|
}
|
486
|
|
487
|
/**
|
488
|
* @param state
|
489
|
* @param names
|
490
|
* @param taxon
|
491
|
* @return
|
492
|
*/
|
493
|
private TaxonName getFirstMisapplication(BerlinModelImportState state, Set<TaxonName> names, Distribution distribution, Integer occSourceId) {
|
494
|
TaxonName result = null;
|
495
|
Taxon taxon = getTaxon(distribution);
|
496
|
|
497
|
//MAN
|
498
|
Set<Taxon> misappliedTaxa = taxon.getMisappliedNames(true);
|
499
|
misappliedTaxa.addAll(taxon.getInvalidDesignations());
|
500
|
Set<TaxonName> misappliedNames = new HashSet<>();
|
501
|
for (Taxon misTaxon : misappliedTaxa){
|
502
|
misappliedNames.add(misTaxon.getName());
|
503
|
misappliedNames.addAll(getOrthographicVariants(misTaxon));
|
504
|
}
|
505
|
|
506
|
for (TaxonName name : names){
|
507
|
if (misappliedNames.contains(name)){
|
508
|
if (result != null){
|
509
|
logger.warn("There is more than 1 matching misapplied name or invalid designation for " + name.getNameCache() + "; occSourceId: " + occSourceId);
|
510
|
}
|
511
|
result = name;
|
512
|
}
|
513
|
}
|
514
|
return result;
|
515
|
}
|
516
|
|
517
|
/**
|
518
|
* @param taxon
|
519
|
* @return
|
520
|
*/
|
521
|
protected Set<TaxonName> getOrthographicVariants(TaxonBase<?> taxonBase) {
|
522
|
Set<TaxonName> result = taxonBase.getName().getRelatedNames(Direction.relatedTo, NameRelationshipType.ORTHOGRAPHIC_VARIANT());
|
523
|
result.addAll(taxonBase.getName().getRelatedNames(Direction.relatedTo, NameRelationshipType.MISSPELLING()));
|
524
|
result.addAll(taxonBase.getName().getRelatedNames(Direction.relatedTo, NameRelationshipType.ORIGINAL_SPELLING()));
|
525
|
return result;
|
526
|
}
|
527
|
|
528
|
/**
|
529
|
* @param distribution
|
530
|
* @return
|
531
|
*/
|
532
|
protected String getTaxonStr(Distribution distribution) {
|
533
|
Taxon taxon = CdmBase.deproxy(distribution.getInDescription(), TaxonDescription.class).getTaxon();
|
534
|
String areaStr = distribution.getArea().getIdInVocabulary();
|
535
|
return areaStr + ": " + taxon.getName().getTitleCache();
|
536
|
}
|
537
|
|
538
|
protected Taxon getTaxon(Distribution distribution) {
|
539
|
Taxon taxon = CdmBase.deproxy(distribution.getInDescription(), TaxonDescription.class).getTaxon();
|
540
|
return taxon;
|
541
|
}
|
542
|
|
543
|
/**
|
544
|
* @param state
|
545
|
* @param oldName
|
546
|
* @return
|
547
|
*/
|
548
|
private Set<TaxonName> getOldNames(BerlinModelImportState state, String oldName) {
|
549
|
Set<TaxonName> names = new HashSet<>();
|
550
|
Set<Integer> nameIds = getNameIds(oldName);
|
551
|
for (Integer id : nameIds){
|
552
|
TaxonName name = (TaxonName)state.getRelatedObject(BerlinModelTaxonNameImport.NAMESPACE, String.valueOf(id));
|
553
|
if (name != null){
|
554
|
names.add(name);
|
555
|
}
|
556
|
}
|
557
|
return names;
|
558
|
}
|
559
|
|
560
|
/**
|
561
|
* @param oldName
|
562
|
* @return
|
563
|
*/
|
564
|
private Set<Integer> getNameIds(String oldName) {
|
565
|
Set<Integer> result = nameCache2NameIdMap.get(oldName);
|
566
|
return result == null ? new HashSet<>(): result;
|
567
|
}
|
568
|
|
569
|
/**
|
570
|
* Creates a map which maps source numbers on references
|
571
|
* @param state
|
572
|
* @return
|
573
|
* @throws SQLException
|
574
|
*/
|
575
|
private Map<String, Integer> makeSourceNumberReferenceIdMap(BerlinModelImportState state) throws SQLException {
|
576
|
Map<String, Integer> result = new HashMap<>();
|
577
|
|
578
|
Source source = state.getConfig().getSource();
|
579
|
String strQuery = " SELECT RefId, IdInSource " +
|
580
|
" FROM Reference " +
|
581
|
" WHERE (IdInSource IS NOT NULL) AND (IdInSource NOT LIKE '') ";
|
582
|
|
583
|
ResultSet rs = source.getResultSet(strQuery) ;
|
584
|
while (rs.next()){
|
585
|
int refId = rs.getInt("RefId");
|
586
|
String idInSource = rs.getString("IdInSource");
|
587
|
if (idInSource != null){
|
588
|
String[] singleSources = idInSource.split("\\|");
|
589
|
for (String singleSource : singleSources){
|
590
|
singleSource = singleSource.trim();
|
591
|
result.put(singleSource, refId);
|
592
|
}
|
593
|
}
|
594
|
}
|
595
|
return result;
|
596
|
}
|
597
|
|
598
|
/**
|
599
|
* Creates a map which maps nameCaches to nameIDs numbers on references
|
600
|
* @param state
|
601
|
* @return
|
602
|
* @throws SQLException
|
603
|
*/
|
604
|
private Map<String, Set<Integer>> makeNameCache2NameIdMap(BerlinModelImportState state) throws SQLException {
|
605
|
Map<String, Set<Integer>> result = new HashMap<>();
|
606
|
try {
|
607
|
|
608
|
Source source = state.getConfig().getSource();
|
609
|
String strQuery = " SELECT NameId, nameCache " +
|
610
|
" FROM Name " +
|
611
|
" WHERE (nameCache IS NOT NULL) AND (nameCache NOT LIKE '') ";
|
612
|
|
613
|
ResultSet rs = source.getResultSet(strQuery) ;
|
614
|
while (rs.next()){
|
615
|
int nameId = rs.getInt("NameId");
|
616
|
String nameCache = rs.getString("nameCache");
|
617
|
if (isNotBlank(nameCache)){
|
618
|
nameCache = nameCache.trim();
|
619
|
Set<Integer> set = result.get(nameCache);
|
620
|
if (set == null){
|
621
|
set = new HashSet<>();
|
622
|
result.put(nameCache, set);
|
623
|
}
|
624
|
set.add(nameId);
|
625
|
}
|
626
|
}
|
627
|
} catch (Exception e) {
|
628
|
e.printStackTrace();
|
629
|
logger.error("Exception in makeNameCache2NameIdMap" + e.getMessage());
|
630
|
}
|
631
|
return result;
|
632
|
}
|
633
|
|
634
|
@Override
|
635
|
protected boolean doCheck(BerlinModelImportState state){
|
636
|
IOValidator<BerlinModelImportState> validator = new BerlinModelOccurrenceSourceImportValidator();
|
637
|
return validator.validate(state);
|
638
|
}
|
639
|
|
640
|
@Override
|
641
|
protected boolean isIgnore(BerlinModelImportState state){
|
642
|
if (! state.getConfig().isDoOccurrenceSources()){
|
643
|
return true;
|
644
|
}else{
|
645
|
if (!this.checkSqlServerColumnExists(state.getConfig().getSource(), "emOccurrenceSource", "OccurrenceSourceId")){
|
646
|
logger.error("emOccurrenceSource table or emOccurrenceSourceId does not exist. Must ignore occurrence import");
|
647
|
return true;
|
648
|
}else{
|
649
|
return false;
|
650
|
}
|
651
|
}
|
652
|
}
|
653
|
|
654
|
}
|