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.phycobank;
|
11
|
|
12
|
import java.util.ArrayList;
|
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
|
import org.springframework.stereotype.Component;
|
20
|
|
21
|
import eu.etaxonomy.cdm.io.mexico.SimpleExcelTaxonImport;
|
22
|
import eu.etaxonomy.cdm.io.mexico.SimpleExcelTaxonImportState;
|
23
|
import eu.etaxonomy.cdm.model.name.Rank;
|
24
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
25
|
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
|
26
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
27
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
28
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
29
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
30
|
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
|
31
|
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
|
32
|
import eu.etaxonomy.cdm.persistence.query.MatchMode;
|
33
|
|
34
|
/**
|
35
|
* @author a.mueller
|
36
|
* @since 2018-08-09
|
37
|
*/
|
38
|
|
39
|
@Component
|
40
|
public class PhycobankHigherClassificationExcelImport<CONFIG extends PhycobankHigherClassificationImportConfigurator>
|
41
|
extends SimpleExcelTaxonImport<CONFIG> {
|
42
|
|
43
|
private static final long serialVersionUID = -77504409820284052L;
|
44
|
private static final Logger logger = Logger.getLogger(PhycobankHigherClassificationExcelImport.class);
|
45
|
|
46
|
private static final String GENUS = "genus";
|
47
|
private static final String FAMILIA = "familia";
|
48
|
private static final String SUBORDO = "subordo";
|
49
|
private static final String ORDO = "ordo";
|
50
|
private static final String SUBCLASSIS = "subclassis";
|
51
|
private static final String CLASSIS = "classis";
|
52
|
private static final String PHYLUM = "phylum";
|
53
|
|
54
|
private static final List<String> propertyPaths = null;
|
55
|
private static TaxonRelationshipType relType;
|
56
|
|
57
|
|
58
|
@Override
|
59
|
protected String getWorksheetName(CONFIG config) {
|
60
|
return config.getWorksheetName();
|
61
|
}
|
62
|
|
63
|
private class RankedUninomial{
|
64
|
public RankedUninomial(String uninomial, Rank rank) {
|
65
|
this.uninomial = uninomial;
|
66
|
this.rank = rank;
|
67
|
}
|
68
|
String uninomial;
|
69
|
Rank rank;
|
70
|
@Override
|
71
|
public String toString() {
|
72
|
return "RankedUninomial [uninomial=" + uninomial + ", rank=" + rank + "]";
|
73
|
}
|
74
|
}
|
75
|
|
76
|
/**
|
77
|
* Creates higher taxonomy
|
78
|
*/
|
79
|
@Override
|
80
|
protected void firstPass(SimpleExcelTaxonImportState<CONFIG> state) {
|
81
|
//TODO correct?
|
82
|
relType = TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN();
|
83
|
|
84
|
String line = "line " + state.getCurrentLine();
|
85
|
Map<String, String> record = state.getOriginalRecord();
|
86
|
|
87
|
List<RankedUninomial> rankedUninomials = makeRankedUninomials(state, record);
|
88
|
|
89
|
createOrVerifyRankedUninomials(state, rankedUninomials, line);
|
90
|
|
91
|
return;
|
92
|
}
|
93
|
|
94
|
|
95
|
/**
|
96
|
* For the given list of {@link RankedUninomial}s names and taxa are created
|
97
|
* if they do not yet exist. Parent child relationships as well as concept
|
98
|
* relationships are created if they do not yet exists.
|
99
|
* If the data is inconsistent to existing data inconsistency is logged and
|
100
|
* no higher classification is created.
|
101
|
* @param state
|
102
|
* @param rankedUninomials
|
103
|
* @param line
|
104
|
*/
|
105
|
private TaxonNode createOrVerifyRankedUninomials(SimpleExcelTaxonImportState<CONFIG> state,
|
106
|
List<RankedUninomial> rankedUninomials, String line) {
|
107
|
|
108
|
if (rankedUninomials.isEmpty()){
|
109
|
return getClassification(state).getRootNode();
|
110
|
}else{
|
111
|
RankedUninomial rankedUninomial = rankedUninomials.get(0);
|
112
|
rankedUninomials.remove(0);
|
113
|
Taxon taxon = getOrMakeTaxon(state, rankedUninomial, line);
|
114
|
TaxonNode existingNode = taxon.getTaxonNode(getClassification(state));
|
115
|
TaxonNode existingHigherNode = existingNode == null? null : existingNode.getParent();
|
116
|
TaxonNode createdHigher = createOrVerifyRankedUninomials(state, rankedUninomials, line);
|
117
|
// boolean exists = verifyNextHigher(state, rankedUninomials, existingHigher);
|
118
|
|
119
|
if (existingNode != null){
|
120
|
if (existingHigherNode == null){
|
121
|
logger.warn(line + ": Higher node does not exist. This should not happen. Please check classification.");
|
122
|
}else if (existingHigherNode.equals(createdHigher)){
|
123
|
//nothing to do;
|
124
|
}else{
|
125
|
logger.warn(line + ": Inconsistency in data. Higher taxon for rank " + rankedUninomial.rank +
|
126
|
" differs from existing higher taxon. Higher taxonomy not created. Please check classification.");
|
127
|
}
|
128
|
}else { //existingNode == null
|
129
|
if (createdHigher == null){
|
130
|
logger.warn(line + ": Created higher node is null. This should not happen. Please check classification and concept relationships.");
|
131
|
return null;
|
132
|
}else{
|
133
|
existingNode = createdHigher.addChildTaxon(taxon, getSecReference(state), line);
|
134
|
getTaxonNodeService().saveOrUpdate(existingNode);
|
135
|
}
|
136
|
}
|
137
|
makeConceptRelation(state, existingNode, line);
|
138
|
return existingNode;
|
139
|
}
|
140
|
}
|
141
|
//
|
142
|
//
|
143
|
// /**
|
144
|
// *
|
145
|
// * @param state
|
146
|
// * @param rankedUninomials
|
147
|
// * @param nextHigher
|
148
|
// * @return
|
149
|
// */
|
150
|
// private boolean verifyNextHigher(SimpleExcelTaxonImportState<CONFIG> state,
|
151
|
// List<RankedUninomial> rankedUninomials,
|
152
|
// TaxonNode nextHigher) {
|
153
|
// if (nextHigher == null){
|
154
|
// return false;
|
155
|
// }else{
|
156
|
// boolean result = true;
|
157
|
// RankedUninomial rankedUninomial = rankedUninomials.get(0);
|
158
|
// TaxonName name = nextHigher.getTaxon().getName();
|
159
|
// if (!rankedUninomial.rank.equals(name.getRank())){
|
160
|
// result = false;
|
161
|
// }
|
162
|
// if (!rankedUninomial.uninomial.equals(name.getNameCache())){
|
163
|
// result = false;
|
164
|
// }
|
165
|
// return result;
|
166
|
// }
|
167
|
// }
|
168
|
|
169
|
|
170
|
/**
|
171
|
* Creates a list of {@link RankedUninomial}s for the given record.
|
172
|
* Empty fields are not listed.
|
173
|
* @param state
|
174
|
* @param record
|
175
|
* @return
|
176
|
*/
|
177
|
private List<RankedUninomial> makeRankedUninomials(
|
178
|
SimpleExcelTaxonImportState<CONFIG> state, Map<String, String> record) {
|
179
|
|
180
|
List<RankedUninomial> result = new ArrayList<>();
|
181
|
addRankedUninomial(result, record, GENUS, Rank.GENUS());
|
182
|
addRankedUninomial(result, record, FAMILIA, Rank.FAMILY());
|
183
|
addRankedUninomial(result, record, SUBORDO, Rank.SUBORDER());
|
184
|
addRankedUninomial(result, record, ORDO, Rank.ORDER());
|
185
|
addRankedUninomial(result, record, SUBCLASSIS, Rank.SUBCLASS());
|
186
|
addRankedUninomial(result, record, CLASSIS, Rank.CLASS());
|
187
|
addRankedUninomial(result, record, PHYLUM, Rank.PHYLUM());
|
188
|
return result;
|
189
|
}
|
190
|
|
191
|
/**
|
192
|
* Creates a {@link RankedUninomial} for a given record field and
|
193
|
* adds it to the list. If the field is empty no {@link RankedUninomial} is
|
194
|
* created.
|
195
|
* @param list
|
196
|
* @param record
|
197
|
* @param fieldName
|
198
|
* @param rank
|
199
|
*/
|
200
|
private void addRankedUninomial(List<RankedUninomial> list, Map<String, String> record,
|
201
|
String fieldName, Rank rank) {
|
202
|
String uninomial = record.get(fieldName);
|
203
|
if (isNotBlank(uninomial)){
|
204
|
list.add(new RankedUninomial(uninomial, rank));
|
205
|
}
|
206
|
}
|
207
|
|
208
|
|
209
|
/**
|
210
|
* @param state
|
211
|
* @param uninomial
|
212
|
* @param rank
|
213
|
* @param sec
|
214
|
* @return
|
215
|
*/
|
216
|
protected Taxon getOrMakeTaxon(SimpleExcelTaxonImportState<CONFIG> state,
|
217
|
RankedUninomial rankedUninomial, String line) {
|
218
|
|
219
|
Reference sec = getSecReference(state);
|
220
|
List<TaxonName> nameCandidates = getNameService().findNamesByNameCache(
|
221
|
rankedUninomial.uninomial, MatchMode.EXACT, propertyPaths);
|
222
|
List<TaxonName> names = rankedNames(state, nameCandidates, rankedUninomial.rank);
|
223
|
|
224
|
Taxon taxon;
|
225
|
if (names.isEmpty()){
|
226
|
taxon = createTaxonAndName(state, rankedUninomial.uninomial,
|
227
|
rankedUninomial.rank, line);
|
228
|
}else{
|
229
|
if (names.size()> 1){
|
230
|
List<Taxon> taxa = new ArrayList<>();
|
231
|
for (TaxonName name : names){
|
232
|
taxa.addAll(getReferencedTaxa(name, sec));
|
233
|
}
|
234
|
if (taxa.isEmpty()){
|
235
|
logger.warn(line + ": More than 1 name matches, but no matching taxon exists. Create new taxon with arbitrary name.");
|
236
|
TaxonName name = names.get(0);
|
237
|
taxon = getOrCreateTaxon(state, name, sec, line);
|
238
|
}else if (taxa.size() == 1){
|
239
|
taxon = taxa.get(0);
|
240
|
}else{
|
241
|
logger.warn(line + ": More than 1 taxon matches, take arbitrary one. This is unexpected and could be improved in code by "
|
242
|
+ "also checking parent relationships.");
|
243
|
taxon = taxa.get(0);
|
244
|
}
|
245
|
|
246
|
}else{
|
247
|
TaxonName name = names.get(0);
|
248
|
taxon = getOrCreateTaxon(state, name, sec, line);
|
249
|
}
|
250
|
}
|
251
|
return taxon;
|
252
|
}
|
253
|
|
254
|
/**
|
255
|
* @param state
|
256
|
* @return
|
257
|
*/
|
258
|
private Classification getClassification(SimpleExcelTaxonImportState<CONFIG> state) {
|
259
|
Classification result = null;
|
260
|
List<Classification> classifications = getClassificationService().list(null, null, null, null, null);
|
261
|
Reference sec = getSecReference(state);
|
262
|
for (Classification classification: classifications){
|
263
|
if (classification.getCitation() != null && classification.getCitation().equals(sec)){
|
264
|
result = classification;
|
265
|
}
|
266
|
}
|
267
|
if (result == null){
|
268
|
result = Classification.NewInstance(sec.getTitleCache());
|
269
|
result.setReference(sec);
|
270
|
getClassificationService().save(result);
|
271
|
}
|
272
|
return result;
|
273
|
}
|
274
|
|
275
|
private Reference secReference;
|
276
|
|
277
|
/**
|
278
|
* @param state
|
279
|
*/
|
280
|
private Reference getSecReference(SimpleExcelTaxonImportState<CONFIG> state) {
|
281
|
UUID uuid = state.getConfig().getSecReference().getUuid();
|
282
|
if (secReference == null || !secReference.getUuid().equals(uuid)){
|
283
|
secReference = getReferenceService().find(uuid);
|
284
|
if (secReference == null){
|
285
|
secReference = state.getConfig().getSecReference();
|
286
|
getReferenceService().save(secReference);
|
287
|
|
288
|
}
|
289
|
}
|
290
|
return secReference;
|
291
|
}
|
292
|
|
293
|
|
294
|
/**
|
295
|
* @param state
|
296
|
* @param uninomial
|
297
|
* @param rank
|
298
|
* @param line
|
299
|
* @return
|
300
|
*/
|
301
|
private Taxon createTaxonAndName(SimpleExcelTaxonImportState<CONFIG> state, String uninomial,
|
302
|
Rank rank, String line) {
|
303
|
TaxonName newName = TaxonNameFactory.NewBotanicalInstance(rank);
|
304
|
newName.setGenusOrUninomial(uninomial);
|
305
|
newName.addImportSource(null, null, getSourceReference(state), line);
|
306
|
|
307
|
Taxon taxon = createTaxon(state, newName, line);
|
308
|
return taxon;
|
309
|
}
|
310
|
|
311
|
/**
|
312
|
* @param state
|
313
|
* @param name
|
314
|
* @param sec
|
315
|
* @param line
|
316
|
* @return
|
317
|
*/
|
318
|
private Taxon getOrCreateTaxon(SimpleExcelTaxonImportState<CONFIG> state, TaxonName name,
|
319
|
Reference sec, String line) {
|
320
|
List<Taxon> taxa = getReferencedTaxa(name, sec);
|
321
|
Taxon result;
|
322
|
if (taxa.isEmpty()){
|
323
|
return createTaxon(state, name, line);
|
324
|
}else{
|
325
|
if (taxa.size()> 1){
|
326
|
//TODO handle
|
327
|
}
|
328
|
result = taxa.get(0);
|
329
|
}
|
330
|
return result;
|
331
|
}
|
332
|
|
333
|
|
334
|
/**
|
335
|
* @param name
|
336
|
* @param sec
|
337
|
* @return
|
338
|
*/
|
339
|
protected List<Taxon> getReferencedTaxa(TaxonName name, Reference sec) {
|
340
|
List<Taxon> taxa = new ArrayList<>();
|
341
|
for (Taxon taxon : name.getTaxa()){
|
342
|
if (sec.equals(taxon.getSec())){
|
343
|
taxa.add(taxon);
|
344
|
}
|
345
|
}
|
346
|
return taxa;
|
347
|
}
|
348
|
|
349
|
/**
|
350
|
* @param state
|
351
|
* @param name
|
352
|
* @param line
|
353
|
* @return
|
354
|
*/
|
355
|
private Taxon createTaxon(SimpleExcelTaxonImportState<CONFIG> state,
|
356
|
TaxonName name, String line) {
|
357
|
Taxon taxon = Taxon.NewInstance(name, getSecReference(state));
|
358
|
taxon.addImportSource(null, null, getSourceReference(state), line);
|
359
|
return taxon;
|
360
|
}
|
361
|
|
362
|
|
363
|
/**
|
364
|
* @param state
|
365
|
* @param nameCandidates
|
366
|
* @param rank
|
367
|
* @return
|
368
|
*/
|
369
|
private List<TaxonName> rankedNames(SimpleExcelTaxonImportState<CONFIG> state,
|
370
|
List<TaxonName> nameCandidates,
|
371
|
Rank rank) {
|
372
|
List<TaxonName> result = new ArrayList<>();
|
373
|
for (TaxonName name : nameCandidates) {
|
374
|
if (rank.equals(name.getRank())){
|
375
|
result.add(name);
|
376
|
}
|
377
|
}
|
378
|
return result;
|
379
|
}
|
380
|
|
381
|
|
382
|
/**
|
383
|
* @param state
|
384
|
* @param childNode
|
385
|
* @param microCitation
|
386
|
*/
|
387
|
private void makeConceptRelation(SimpleExcelTaxonImportState<CONFIG> state,
|
388
|
TaxonNode childNode, String microCitation) {
|
389
|
Taxon child = childNode.getTaxon();
|
390
|
Taxon parent = childNode.getParent().getTaxon();
|
391
|
if (parent == null){
|
392
|
return;
|
393
|
}
|
394
|
|
395
|
Reference sec = getSecReference(state);
|
396
|
Set<TaxonRelationship> rels = child.getRelationsFromThisTaxon();
|
397
|
boolean hasRelation = false;
|
398
|
for (TaxonRelationship rel : rels){
|
399
|
if (rel.getType().equals(relType)
|
400
|
&& rel.getToTaxon().equals(parent)
|
401
|
&& rel.getCitation().equals(sec)){
|
402
|
hasRelation = true;
|
403
|
}
|
404
|
}
|
405
|
if (!hasRelation){
|
406
|
child.addTaxonRelation(parent, relType, sec, microCitation);
|
407
|
}
|
408
|
}
|
409
|
|
410
|
}
|