1
|
/**
|
2
|
* Copyright (C) 2016 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.redlist.germanSL;
|
10
|
|
11
|
import java.util.HashSet;
|
12
|
import java.util.Map;
|
13
|
import java.util.Set;
|
14
|
|
15
|
import org.apache.log4j.Logger;
|
16
|
import org.springframework.stereotype.Component;
|
17
|
|
18
|
import eu.etaxonomy.cdm.io.mexico.SimpleExcelTaxonImportState;
|
19
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
20
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
21
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
22
|
import eu.etaxonomy.cdm.model.taxon.SynonymType;
|
23
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
24
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
25
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
26
|
|
27
|
/**
|
28
|
* @author a.mueller
|
29
|
* @since 25.11.2016
|
30
|
*
|
31
|
*/
|
32
|
@Component
|
33
|
public class GermanSLTaxonRelationImport extends GermanSLTaxonImport {
|
34
|
|
35
|
private static final long serialVersionUID = 3381597141845204995L;
|
36
|
|
37
|
private static final Logger logger = Logger.getLogger(GermanSLTaxonRelationImport.class);
|
38
|
|
39
|
public static final String TAXON_NAMESPACE = "1.3.4";
|
40
|
|
41
|
@Override
|
42
|
protected String getWorksheetName(GermanSLImportConfigurator config) {
|
43
|
return "1.3.4";
|
44
|
}
|
45
|
|
46
|
private Classification classification;
|
47
|
private Set<TaxonNode> nodesToSave = new HashSet<>();
|
48
|
private int count = 0;
|
49
|
|
50
|
@Override
|
51
|
protected void firstPass(SimpleExcelTaxonImportState<GermanSLImportConfigurator> state) {
|
52
|
count++;
|
53
|
Map<String, String> record = state.getOriginalRecord();
|
54
|
String line = state.getCurrentLine() + ": ";
|
55
|
|
56
|
String parentStr = getValue(record, GermanSLTaxonImport.AGG);
|
57
|
String acceptedStr = getValue(record, GermanSLTaxonImport.VALID_NR);
|
58
|
String idStr = getValue(record, GermanSLTaxonImport.SPECIES_NR);
|
59
|
String statusStr = getValue(record, GermanSLTaxonImport.SYNONYM);
|
60
|
NameResult nameResult = makeName (line, record, state);
|
61
|
|
62
|
Classification classification = getClassification(state);
|
63
|
TaxonBase<?> taxonBase = GermanSLTaxonImport.taxonIdMap.get(idStr);
|
64
|
Taxon parent;
|
65
|
TaxonNode taxonNode = null;
|
66
|
if (isAccepted(statusStr, nameResult)){
|
67
|
TaxonBase<?> parentTmp = GermanSLTaxonImport.taxonIdMap.get(parentStr);
|
68
|
if (parentTmp == null){
|
69
|
logger.warn(line + "Parent is missing: "+ parentStr);
|
70
|
}else if (parentTmp.isInstanceOf(Synonym.class)){
|
71
|
logger.warn(line + "Parent is not of type Taxon");
|
72
|
}else{
|
73
|
parent = (Taxon)parentTmp;
|
74
|
Taxon taxon = (Taxon)taxonBase;
|
75
|
Reference relRef = null; //TODO
|
76
|
if ("0".equals(idStr)){
|
77
|
taxonNode = classification.addChildTaxon(taxon, relRef, null);
|
78
|
}else{
|
79
|
taxonNode = classification.addParentChild(parent, taxon, relRef, null);
|
80
|
}
|
81
|
|
82
|
}
|
83
|
} else {
|
84
|
TaxonBase<?> parentTmp = GermanSLTaxonImport.taxonIdMap.get(acceptedStr);
|
85
|
if (parentTmp == null){
|
86
|
logger.warn(line + "Accepted taxon is missing: " + acceptedStr);
|
87
|
}else if (parentTmp.isInstanceOf(Synonym.class)){
|
88
|
logger.warn(line + "Accepted taxon is not of type Taxon");
|
89
|
}else{
|
90
|
parent = (Taxon)parentTmp;
|
91
|
Synonym synonym = (Synonym)taxonBase;
|
92
|
parent.addSynonym(synonym, SynonymType.SYNONYM_OF());
|
93
|
}
|
94
|
}
|
95
|
if (taxonNode != null){
|
96
|
nodesToSave.add(taxonNode);
|
97
|
}
|
98
|
if ((count % 1) == 0){
|
99
|
count = 0;
|
100
|
getTaxonNodeService().saveOrUpdate(nodesToSave);
|
101
|
nodesToSave = new HashSet<>();
|
102
|
}
|
103
|
}
|
104
|
|
105
|
|
106
|
boolean needsFinalSave = true;
|
107
|
/**
|
108
|
* {@inheritDoc}
|
109
|
*/
|
110
|
@Override
|
111
|
protected void secondPass(SimpleExcelTaxonImportState<GermanSLImportConfigurator> state) {
|
112
|
if (needsFinalSave){
|
113
|
getTaxonNodeService().saveOrUpdate(nodesToSave);
|
114
|
needsFinalSave = false;
|
115
|
}
|
116
|
}
|
117
|
|
118
|
|
119
|
/**
|
120
|
* @return
|
121
|
*/
|
122
|
private Classification getClassification(SimpleExcelTaxonImportState<GermanSLImportConfigurator> state) {
|
123
|
if (classification == null){
|
124
|
GermanSLImportConfigurator config = state.getConfig();
|
125
|
classification = Classification.NewInstance(config.getClassificationName());
|
126
|
classification.setUuid(config.getClassificationUuid());
|
127
|
classification.setReference(config.getSecReference());
|
128
|
getClassificationService().save(classification);
|
129
|
}
|
130
|
return classification;
|
131
|
}
|
132
|
|
133
|
@Override
|
134
|
protected boolean isIgnore(SimpleExcelTaxonImportState<GermanSLImportConfigurator> state) {
|
135
|
return ! state.getConfig().isDoTaxa();
|
136
|
}
|
137
|
}
|