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