1
|
/**
|
2
|
* Copyright (C) 2017 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.fact.temporal.in;
|
10
|
|
11
|
import java.util.Map;
|
12
|
import java.util.UUID;
|
13
|
import java.util.regex.Matcher;
|
14
|
import java.util.regex.Pattern;
|
15
|
|
16
|
import org.apache.commons.lang3.StringUtils;
|
17
|
import org.springframework.stereotype.Component;
|
18
|
|
19
|
import eu.etaxonomy.cdm.io.excel.common.ExcelRowBase;
|
20
|
import eu.etaxonomy.cdm.io.fact.in.FactExcelImportBase;
|
21
|
import eu.etaxonomy.cdm.model.common.ExtendedTimePeriod;
|
22
|
import eu.etaxonomy.cdm.model.description.Feature;
|
23
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
24
|
import eu.etaxonomy.cdm.model.description.TemporalData;
|
25
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
26
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
27
|
|
28
|
/**
|
29
|
* Import for taxon based temporal data.
|
30
|
*
|
31
|
* @author a.mueller
|
32
|
* @since 15.07.2020
|
33
|
*/
|
34
|
@Component
|
35
|
public class TemporalDataExcelImport<STATE extends TemporalDataExcelImportState<CONFIG>, CONFIG extends TemporalDataExcelImportConfigurator<?>>
|
36
|
extends FactExcelImportBase<STATE, CONFIG, ExcelRowBase>{
|
37
|
|
38
|
private static final long serialVersionUID = -2885338554616141176L;
|
39
|
|
40
|
@Override
|
41
|
protected String getWorksheetName(CONFIG config) {
|
42
|
return "Data";
|
43
|
}
|
44
|
|
45
|
@Override
|
46
|
protected void doFirstPass(STATE state, Taxon taxon,
|
47
|
String line, String linePure){
|
48
|
|
49
|
if (taxon == null){
|
50
|
// return;
|
51
|
taxon = Taxon.NewInstance(null, null);
|
52
|
}
|
53
|
|
54
|
Map<String, String> record = state.getOriginalRecord();
|
55
|
|
56
|
Feature feature = null;
|
57
|
UUID uuidFeature = state.getConfig().getFeatureUuid();
|
58
|
if (uuidFeature != null){
|
59
|
feature = (Feature)getTermService().find(uuidFeature);
|
60
|
}
|
61
|
if (feature == null){
|
62
|
String message = "Feature could not be defined. Import not possible.";
|
63
|
state.addError(message);
|
64
|
return;
|
65
|
}
|
66
|
|
67
|
String colLabelStart = state.getConfig().getColumnLabelStart();
|
68
|
String colLabelEnd = state.getConfig().getColumnLabelEnd();
|
69
|
|
70
|
handleFeature(state, taxon, line, linePure, record, feature, colLabelStart, colLabelEnd);
|
71
|
|
72
|
}
|
73
|
|
74
|
protected void handleFeature(STATE state, Taxon taxon, String line, String linePure,
|
75
|
Map<String, String> record, Feature feature, String startColLabel, String endColLabel) {
|
76
|
|
77
|
TemporalData temporalData = TemporalData.NewInstance(feature);
|
78
|
String startMonthStr = getValue(record, startColLabel);
|
79
|
String endMonthStr = getValue(record, endColLabel);
|
80
|
Integer startMonth = monthToInteger(state, startMonthStr, startColLabel, false);
|
81
|
Integer startMonthExtreme = monthToInteger(state, startMonthStr, startColLabel, true);
|
82
|
Integer endMonth = monthToInteger(state, endMonthStr, endColLabel, false);
|
83
|
Integer endMonthExtreme = monthToInteger(state, endMonthStr, endColLabel, true);
|
84
|
ExtendedTimePeriod period = ExtendedTimePeriod.NewExtendedMonthInstance(startMonth, endMonth, startMonthExtreme, endMonthExtreme);
|
85
|
temporalData.setPeriod(period);
|
86
|
|
87
|
//source
|
88
|
String id = null;
|
89
|
String idNamespace = getWorksheetName(state.getConfig());
|
90
|
Reference reference = getSourceReference(state);
|
91
|
|
92
|
//description
|
93
|
if (!temporalData.getPeriod().isEmpty()){
|
94
|
TaxonDescription taxonDescription = this.getTaxonDescription(taxon, reference, !IMAGE_GALLERY, true);
|
95
|
taxonDescription.addElement(temporalData);
|
96
|
temporalData.addImportSource(id, idNamespace, reference, linePure);
|
97
|
}
|
98
|
}
|
99
|
|
100
|
private Integer monthToInteger(STATE state, String monthStr, String colLabel, boolean isExtreme) {
|
101
|
if(StringUtils.isBlank(monthStr)){
|
102
|
return null;
|
103
|
}else {
|
104
|
Matcher matcher = getPattern().matcher(monthStr);
|
105
|
if (matcher.matches()){
|
106
|
if(isExtreme){
|
107
|
if (matcher.group(2) != null){
|
108
|
String extreme = matcher.group(2);
|
109
|
return Integer.valueOf(extreme);
|
110
|
}else{
|
111
|
return null; //extreme value does not exist
|
112
|
}
|
113
|
}else{
|
114
|
String normal = matcher.group(1);
|
115
|
return Integer.valueOf(normal);
|
116
|
}
|
117
|
}else{
|
118
|
if (!isExtreme){ //we have to record this only once
|
119
|
String message = "Value " + monthStr + " for " + colLabel + " could not be transformed to a valid month number. Value not imported." ;
|
120
|
state.addError(message);
|
121
|
}
|
122
|
}
|
123
|
}
|
124
|
return null;
|
125
|
}
|
126
|
|
127
|
private Pattern monthPattern;
|
128
|
private Pattern getPattern() {
|
129
|
if (monthPattern == null){
|
130
|
String nr = "(0?[1-9]|1[0-2])";
|
131
|
monthPattern = Pattern.compile(nr + "\\s*(?:\\(" + nr + "\\))?");
|
132
|
}
|
133
|
return monthPattern;
|
134
|
}
|
135
|
|
136
|
@Override
|
137
|
protected boolean requiresNomenclaturalCode() {
|
138
|
return false;
|
139
|
}
|
140
|
|
141
|
@Override
|
142
|
protected boolean isIgnore(STATE state) {
|
143
|
return false;
|
144
|
}
|
145
|
}
|