1
|
/**
|
2
|
* Copyright (C) 2020 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.caryo;
|
10
|
|
11
|
import java.util.Map;
|
12
|
|
13
|
import org.apache.logging.log4j.LogManager;
|
14
|
import org.apache.logging.log4j.Logger;
|
15
|
import org.springframework.stereotype.Component;
|
16
|
|
17
|
import eu.etaxonomy.cdm.io.mexico.SimpleExcelTaxonImportState;
|
18
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
19
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
20
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
21
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
22
|
|
23
|
/**
|
24
|
* @author a.mueller
|
25
|
* @since 02.02.2023
|
26
|
*/
|
27
|
@Component
|
28
|
public class CaryoSileneaeTaxonImport extends CaryoSileneaeImportBase {
|
29
|
|
30
|
private static final long serialVersionUID = 5594951908819469636L;
|
31
|
private static final Logger logger = LogManager.getLogger();
|
32
|
|
33
|
private static final String ACCEPTED_TAXON_ID = "AcceptedTaxon_ID";
|
34
|
private static final String NOMEN_LINK = "nomen_link";
|
35
|
private static final String PARENT = "parent";
|
36
|
|
37
|
@Override
|
38
|
protected String getWorksheetName(CaryoSileneaeImportConfigurator config) {
|
39
|
return "Taxa";
|
40
|
}
|
41
|
|
42
|
@Override
|
43
|
protected void firstPass(SimpleExcelTaxonImportState<CaryoSileneaeImportConfigurator> state) {
|
44
|
try {
|
45
|
int line = state.getCurrentLine();
|
46
|
// if ((line % 500) == 0){
|
47
|
// newTransaction(state);
|
48
|
// System.out.println(line);
|
49
|
// }
|
50
|
|
51
|
Map<String, String> record = state.getOriginalRecord();
|
52
|
|
53
|
Integer accTaxonId = Integer.valueOf(getValue(record, ACCEPTED_TAXON_ID));
|
54
|
Integer nameLinkID = Integer.valueOf(getValue(record, NOMEN_LINK));
|
55
|
|
56
|
String row = String.valueOf(line) + "("+accTaxonId+"): ";
|
57
|
|
58
|
TaxonName name = getName(nameLinkID);
|
59
|
if (name == null) {
|
60
|
logger.warn(row + "Name does not exist");
|
61
|
return;
|
62
|
}
|
63
|
|
64
|
orphanedNameMap.remove(nameLinkID);
|
65
|
Taxon taxon = Taxon.NewInstance(name, getSecRef(state));
|
66
|
|
67
|
putToTaxonMap(accTaxonId, taxon);
|
68
|
getTaxonService().saveOrUpdate(taxon);
|
69
|
} catch (Exception e) {
|
70
|
e.printStackTrace();
|
71
|
}
|
72
|
}
|
73
|
|
74
|
@Override
|
75
|
protected void secondPass(SimpleExcelTaxonImportState<CaryoSileneaeImportConfigurator> state) {
|
76
|
try {
|
77
|
int line = state.getCurrentLine();
|
78
|
// if ((line % 500) == 0){
|
79
|
// newTransaction(state);
|
80
|
// System.out.println(line);
|
81
|
// }
|
82
|
|
83
|
Map<String, String> record = state.getOriginalRecord();
|
84
|
|
85
|
Integer accTaxonId = Integer.valueOf(getValue(record, ACCEPTED_TAXON_ID));
|
86
|
Integer parentId = getInt(getValue(record, PARENT));
|
87
|
|
88
|
String row = String.valueOf(line) + "("+accTaxonId+"): ";
|
89
|
|
90
|
Taxon taxon = getTaxon(accTaxonId);
|
91
|
if (taxon == null) {
|
92
|
logger.warn(row + "Taxon does not exist");
|
93
|
return;
|
94
|
}
|
95
|
|
96
|
Taxon parent;
|
97
|
if (parentId == null) {
|
98
|
TaxonNode rootNode = getRootNode(state);
|
99
|
parent = rootNode.getTaxon();
|
100
|
}else {
|
101
|
parent = getTaxon(parentId);
|
102
|
|
103
|
}
|
104
|
if (parent == null) {
|
105
|
logger.warn(row + "Taxon does not exist");
|
106
|
return;
|
107
|
}
|
108
|
Classification classification = getClassification(state);
|
109
|
|
110
|
TaxonNode node = classification.addParentChild(parent, taxon, null, null);
|
111
|
|
112
|
getTaxonNodeService().saveOrUpdate(node);
|
113
|
} catch (Exception e) {
|
114
|
e.printStackTrace();
|
115
|
}
|
116
|
}
|
117
|
|
118
|
private TaxonNode rootNode;
|
119
|
private TaxonNode getRootNode(SimpleExcelTaxonImportState<CaryoSileneaeImportConfigurator> state) {
|
120
|
if (rootNode == null) {
|
121
|
rootNode = getTaxonNodeService().find(state.getConfig().getAcceptedNodeUuid());
|
122
|
}
|
123
|
return rootNode;
|
124
|
}
|
125
|
|
126
|
private Classification classification;
|
127
|
private Classification getClassification(SimpleExcelTaxonImportState<CaryoSileneaeImportConfigurator> state) {
|
128
|
if (classification == null) {
|
129
|
classification = getClassificationService().find(state.getConfig().getClassificationUuid());
|
130
|
}
|
131
|
return classification;
|
132
|
}
|
133
|
}
|