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.casearia;
|
10
|
|
11
|
import java.util.Map;
|
12
|
import java.util.UUID;
|
13
|
|
14
|
import org.apache.log4j.Logger;
|
15
|
import org.springframework.stereotype.Component;
|
16
|
|
17
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
18
|
import eu.etaxonomy.cdm.io.mexico.SimpleExcelTaxonImport;
|
19
|
import eu.etaxonomy.cdm.io.mexico.SimpleExcelTaxonImportState;
|
20
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
21
|
import eu.etaxonomy.cdm.model.description.Distribution;
|
22
|
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
|
23
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
24
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
25
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
26
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
27
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
28
|
import eu.etaxonomy.cdm.model.term.TermVocabulary;
|
29
|
|
30
|
/**
|
31
|
* @author a.mueller
|
32
|
* @since 12.05.2020
|
33
|
*/
|
34
|
@Component
|
35
|
public class CaseariaDistributionImport extends SimpleExcelTaxonImport<CaseariaImportConfigurator>{
|
36
|
|
37
|
private static final long serialVersionUID = -7841292708411771648L;
|
38
|
|
39
|
private static final Logger logger = Logger.getLogger(CaseariaDistributionImport.class);
|
40
|
|
41
|
private static final String NAME_CIT = "NameCit";
|
42
|
private static final String IPNI_ID = "ipni_id";
|
43
|
private static final String PLANT_NAME_ID = "plant_name_id";
|
44
|
private static final String TAXON_RANK = "taxon_rank";
|
45
|
|
46
|
private static final String CONTINENT_L1 = "continent_code_l1";
|
47
|
private static final String CONTINENT_LABEL = "continent";
|
48
|
private static final String REGION_L2 = "region_code_l2";
|
49
|
private static final String REGION_LABEL = "region";
|
50
|
private static final String AREA_L3 = "area_code_l3";
|
51
|
private static final String AREA_LABEL = "area";
|
52
|
private static final String INTRODUCED = "introduced";
|
53
|
private static final String EXTINCT = "extinct";
|
54
|
private static final String LOCATION_DOUBTFUL = "location_doubtful";
|
55
|
|
56
|
private static final int RECORDS_PER_TRANSACTION = 500;
|
57
|
|
58
|
private SimpleExcelTaxonImportState<CaseariaImportConfigurator> state;
|
59
|
|
60
|
@Override
|
61
|
protected String getWorksheetName(CaseariaImportConfigurator config) {
|
62
|
return "OutputWDistribution";
|
63
|
}
|
64
|
|
65
|
@Override
|
66
|
protected void firstPass(SimpleExcelTaxonImportState<CaseariaImportConfigurator> state) {
|
67
|
this.state = state;
|
68
|
int line = state.getCurrentLine();
|
69
|
if ((line % RECORDS_PER_TRANSACTION) == 0){
|
70
|
newTransaction(state);
|
71
|
System.out.println(line);
|
72
|
}
|
73
|
|
74
|
Map<String, String> record = state.getOriginalRecord();
|
75
|
|
76
|
String fullCitation = getValue(record, NAME_CIT);
|
77
|
String ipniId = getValue(record, IPNI_ID);
|
78
|
String sourceId = getValue(record, PLANT_NAME_ID);
|
79
|
// String rankStr = getValue(record, TAXON_RANK);
|
80
|
String continent_l1 = getValue(record, CONTINENT_L1);
|
81
|
String continent_label = getValue(record, CONTINENT_LABEL);
|
82
|
String region_l2 = getValue(record, REGION_L2);
|
83
|
String region_label = getValue(record, REGION_LABEL);
|
84
|
String area_l3 = getValue(record, AREA_L3);
|
85
|
String area_label = getValue(record, AREA_LABEL);
|
86
|
String introduced = getValue(record, INTRODUCED);
|
87
|
String extinct = getValue(record, EXTINCT);
|
88
|
String location_doubtful = getValue(record, LOCATION_DOUBTFUL);
|
89
|
|
90
|
|
91
|
String fullNameStr = CdmUtils.concat(" ", "","");
|
92
|
String row = String.valueOf(line) + "("+fullNameStr+"): ";
|
93
|
|
94
|
UUID uuid = getTaxonMapping().get(sourceId);
|
95
|
if (uuid == null){
|
96
|
logger.warn(row + "Taxon uuid not found in taxon mapping for " + sourceId);
|
97
|
}
|
98
|
TaxonBase<?> taxonBase = getTaxonService().find(uuid);
|
99
|
if (taxonBase == null){
|
100
|
logger.warn(row + "Taxon " + sourceId + " for distribution "+area_label +" does not exist: " + fullCitation);
|
101
|
return;
|
102
|
}
|
103
|
String taxonName = taxonBase.getName().getTitleCache();
|
104
|
Taxon taxon;
|
105
|
if (taxonBase.isInstanceOf(Synonym.class)){
|
106
|
taxon = CdmBase.deproxy(taxonBase, Synonym.class).getAcceptedTaxon();
|
107
|
if (taxon == null){
|
108
|
logger.warn(row + "Taxon "+taxonName+" for distribution "+area_label +" is synonym and synonym has no accepted taxon. Distribution"+area_label+"was ignored.");
|
109
|
return;
|
110
|
}else{
|
111
|
logger.warn(row + "Taxon "+taxonName+" for distribution " + area_label + " is synonym. Distribution was moved to accepted taxon " + taxon.getName().getTitleCache());
|
112
|
}
|
113
|
}else {
|
114
|
taxon = CdmBase.deproxy(taxonBase, Taxon.class);
|
115
|
}
|
116
|
@SuppressWarnings("unchecked")
|
117
|
TermVocabulary<NamedArea> voc = getVocabularyService().find(NamedArea.uuidTdwgAreaVocabulary);
|
118
|
NamedArea area = voc.getTermByIdInvocabulary(area_l3);
|
119
|
if (area == null){
|
120
|
logger.warn(row + "TDWG area not found: " + area_l3 + "; " + area_label);
|
121
|
}else{
|
122
|
if (taxon.getDescriptions().isEmpty()){
|
123
|
TaxonDescription.NewInstance(taxon);
|
124
|
}
|
125
|
TaxonDescription desc = taxon.getDescriptions().iterator().next();
|
126
|
PresenceAbsenceTerm status = PresenceAbsenceTerm.PRESENT();
|
127
|
if (introduced.equals("1")){
|
128
|
status = PresenceAbsenceTerm.INTRODUCED();
|
129
|
}else if (extinct.equals("1")){
|
130
|
//TODO extinct
|
131
|
logger.warn(row + "Improve status to extinct");
|
132
|
status = PresenceAbsenceTerm.ABSENT();
|
133
|
}else if (location_doubtful.equals("1")){
|
134
|
status = PresenceAbsenceTerm.PRESENT_DOUBTFULLY();
|
135
|
}
|
136
|
Distribution distribution = Distribution.NewInstance(area, status);
|
137
|
desc.addElement(distribution);
|
138
|
}
|
139
|
}
|
140
|
|
141
|
@SuppressWarnings("unchecked")
|
142
|
private Map<String, UUID> getTaxonMapping() {
|
143
|
return (Map<String, UUID>)state.getStatusItem(CaseariaTaxonImport.TAXON_MAPPING);
|
144
|
}
|
145
|
|
146
|
private void newTransaction(SimpleExcelTaxonImportState<CaseariaImportConfigurator> state) {
|
147
|
commitTransaction(state.getTransactionStatus());
|
148
|
// secRef = null;
|
149
|
// dedupHelper = null;
|
150
|
state.setSourceReference(null);
|
151
|
System.gc();
|
152
|
state.setTransactionStatus(startTransaction());
|
153
|
}
|
154
|
}
|