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.mexico;
|
10
|
|
11
|
import java.util.Arrays;
|
12
|
import java.util.HashMap;
|
13
|
import java.util.List;
|
14
|
import java.util.Set;
|
15
|
|
16
|
import org.apache.log4j.Logger;
|
17
|
|
18
|
import eu.etaxonomy.cdm.io.excel.common.ExcelImportBase;
|
19
|
import eu.etaxonomy.cdm.io.excel.common.ExcelImportConfiguratorBase;
|
20
|
import eu.etaxonomy.cdm.io.excel.common.ExcelRowBase;
|
21
|
import eu.etaxonomy.cdm.model.agent.Person;
|
22
|
import eu.etaxonomy.cdm.model.agent.Team;
|
23
|
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
|
24
|
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
|
25
|
import eu.etaxonomy.cdm.model.name.IBotanicalName;
|
26
|
import eu.etaxonomy.cdm.model.name.Rank;
|
27
|
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
|
28
|
import eu.etaxonomy.cdm.model.reference.INomenclaturalReference;
|
29
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
30
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
31
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
32
|
import eu.etaxonomy.cdm.strategy.parser.INonViralNameParser;
|
33
|
import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
|
34
|
|
35
|
/**
|
36
|
* Simple Excel import class that works without default state class
|
37
|
* {@link SimpleExcelTaxonImportState}
|
38
|
* @author a.mueller
|
39
|
* @date 16.06.2016
|
40
|
*/
|
41
|
public abstract class SimpleExcelTaxonImport<CONFIG extends ExcelImportConfiguratorBase>
|
42
|
extends ExcelImportBase<SimpleExcelTaxonImportState<CONFIG>, CONFIG, ExcelRowBase>{
|
43
|
|
44
|
private static final long serialVersionUID = -4345647703312616421L;
|
45
|
|
46
|
private static final Logger logger = Logger.getLogger(SimpleExcelTaxonImport.class);
|
47
|
|
48
|
protected static INonViralNameParser<?> nameParser = NonViralNameParserImpl.NewInstance();
|
49
|
|
50
|
|
51
|
@Override
|
52
|
protected void analyzeRecord(HashMap<String, String> record, SimpleExcelTaxonImportState<CONFIG> state) {
|
53
|
//override only if needed
|
54
|
}
|
55
|
|
56
|
@Override
|
57
|
protected void secondPass(SimpleExcelTaxonImportState<CONFIG> state) {
|
58
|
//override only if needed
|
59
|
}
|
60
|
|
61
|
@Override
|
62
|
protected boolean isIgnore(SimpleExcelTaxonImportState<CONFIG> state) {
|
63
|
return false;
|
64
|
}
|
65
|
|
66
|
//***************************** METHODS *********************************/
|
67
|
|
68
|
|
69
|
/**
|
70
|
* @param state
|
71
|
* @return
|
72
|
*/
|
73
|
protected IdentifiableSource makeOriginalSource(SimpleExcelTaxonImportState<CONFIG> state) {
|
74
|
return IdentifiableSource.NewDataImportInstance("line: " + state.getCurrentLine(), null, state.getConfig().getSourceReference());
|
75
|
}
|
76
|
|
77
|
/**
|
78
|
* @param state
|
79
|
* @param speciesName
|
80
|
* @param sec
|
81
|
* @param taxon
|
82
|
* @param familyNode
|
83
|
*/
|
84
|
protected void makeGenus(SimpleExcelTaxonImportState<CONFIG> state,
|
85
|
IBotanicalName speciesName,
|
86
|
Reference sec,
|
87
|
Taxon taxon,
|
88
|
TaxonNode familyNode) {
|
89
|
|
90
|
TaxonNode higherNode;
|
91
|
if (speciesName.isProtectedTitleCache()){
|
92
|
higherNode = familyNode;
|
93
|
}else{
|
94
|
String genusStr = speciesName.getGenusOrUninomial();
|
95
|
Taxon genus = state.getHigherTaxon(genusStr);
|
96
|
if (genus != null){
|
97
|
higherNode = genus.getTaxonNodes().iterator().next();
|
98
|
}else{
|
99
|
IBotanicalName genusName = TaxonNameFactory.NewBotanicalInstance(Rank.GENUS());
|
100
|
genusName.addSource(makeOriginalSource(state));
|
101
|
genusName.setGenusOrUninomial(genusStr);
|
102
|
genus = Taxon.NewInstance(genusName, sec);
|
103
|
genus.addSource(makeOriginalSource(state));
|
104
|
higherNode = familyNode.addChildTaxon(genus, null, null);
|
105
|
state.putHigherTaxon(genusStr, genus);
|
106
|
}
|
107
|
}
|
108
|
|
109
|
higherNode.addChildTaxon(taxon, null, null);
|
110
|
}
|
111
|
|
112
|
/**
|
113
|
* @param line
|
114
|
* @param keys
|
115
|
* @param expectedKeys
|
116
|
*/
|
117
|
protected void checkAllKeysExist(String line, Set<String> keys, List<String> expectedKeys) {
|
118
|
for (String key: keys) {
|
119
|
if (! expectedKeys.contains(key)){
|
120
|
logger.warn(line + "Unexpected Key: " + key);
|
121
|
}
|
122
|
}
|
123
|
}
|
124
|
|
125
|
|
126
|
/**
|
127
|
* @param state
|
128
|
* @param name
|
129
|
*/
|
130
|
protected void replaceAuthorNamesAndNomRef(SimpleExcelTaxonImportState<CONFIG> state,
|
131
|
IBotanicalName name) {
|
132
|
TeamOrPersonBase<?> combAuthor = name.getCombinationAuthorship();
|
133
|
name.setCombinationAuthorship(getExistingAuthor(state, combAuthor));
|
134
|
|
135
|
TeamOrPersonBase<?> exAuthor = name.getExCombinationAuthorship();
|
136
|
name.setExCombinationAuthorship(getExistingAuthor(state, exAuthor));
|
137
|
|
138
|
TeamOrPersonBase<?> basioAuthor = name.getBasionymAuthorship();
|
139
|
name.setBasionymAuthorship(getExistingAuthor(state, basioAuthor));
|
140
|
|
141
|
TeamOrPersonBase<?> exBasioAuthor = name.getExBasionymAuthorship();
|
142
|
name.setExBasionymAuthorship(getExistingAuthor(state, exBasioAuthor));
|
143
|
|
144
|
INomenclaturalReference nomRef = name.getNomenclaturalReference();
|
145
|
if (nomRef != null){
|
146
|
TeamOrPersonBase<?> refAuthor = nomRef.getAuthorship();
|
147
|
nomRef.setAuthorship(getExistingAuthor(state, refAuthor));
|
148
|
|
149
|
Reference existingRef = getExistingReference(state, (Reference)nomRef);
|
150
|
if (existingRef != null){
|
151
|
name.setNomenclaturalReference(existingRef);
|
152
|
}
|
153
|
}
|
154
|
}
|
155
|
|
156
|
/**
|
157
|
* @param state
|
158
|
* @param nomRef
|
159
|
*/
|
160
|
private Reference getExistingReference(SimpleExcelTaxonImportState<CONFIG> state, Reference ref) {
|
161
|
if (ref == null){
|
162
|
return null;
|
163
|
}else{
|
164
|
initRerenceMap(state);
|
165
|
Reference result = state.getReference(ref.getTitleCache());
|
166
|
if (result == null){
|
167
|
result = ref;
|
168
|
Reference inRef = result.getInReference();
|
169
|
if (inRef != null){
|
170
|
result.setInReference(getExistingReference(state, result.getInReference()));
|
171
|
}
|
172
|
state.putReference(result.getTitleCache(), result);
|
173
|
}
|
174
|
return result;
|
175
|
}
|
176
|
}
|
177
|
|
178
|
boolean referenceMapIsInitialized;
|
179
|
|
180
|
/**
|
181
|
* @param state
|
182
|
*/
|
183
|
private void initRerenceMap(SimpleExcelTaxonImportState<CONFIG> state) {
|
184
|
if (!referenceMapIsInitialized){
|
185
|
List<String> propertyPaths = Arrays.asList("");
|
186
|
List<Reference> existingReferences = this.getReferenceService().list(null, null, null, null, propertyPaths);
|
187
|
for (Reference ref : existingReferences){
|
188
|
state.putReference(ref.getTitleCache(), ref);
|
189
|
}
|
190
|
referenceMapIsInitialized = true;
|
191
|
}
|
192
|
|
193
|
}
|
194
|
|
195
|
boolean agentMapIsInitialized = false;
|
196
|
|
197
|
/**
|
198
|
* @param state
|
199
|
*
|
200
|
*/
|
201
|
@SuppressWarnings("rawtypes")
|
202
|
private void initAgentMap(SimpleExcelTaxonImportState<CONFIG> state) {
|
203
|
if (!agentMapIsInitialized){
|
204
|
List<String> propertyPaths = Arrays.asList("");
|
205
|
List<TeamOrPersonBase> existingAgents = this.getAgentService().list(null, null, null, null, propertyPaths);
|
206
|
for (TeamOrPersonBase agent : existingAgents){
|
207
|
state.putAgentBase(agent.getTitleCache(), agent);
|
208
|
}
|
209
|
agentMapIsInitialized = true;
|
210
|
}
|
211
|
}
|
212
|
|
213
|
/**
|
214
|
* @param state
|
215
|
* @param combAuthor
|
216
|
* @return
|
217
|
*/
|
218
|
protected TeamOrPersonBase<?> getExistingAuthor(SimpleExcelTaxonImportState<CONFIG> state,
|
219
|
TeamOrPersonBase<?> author) {
|
220
|
if (author == null){
|
221
|
return null;
|
222
|
}else{
|
223
|
initAgentMap(state);
|
224
|
TeamOrPersonBase<?> result = state.getAgentBase(author.getTitleCache());
|
225
|
if (result == null){
|
226
|
state.putAgentBase(author.getTitleCache(), author);
|
227
|
if (author instanceof Team){
|
228
|
handleTeam(state, (Team)author);
|
229
|
}
|
230
|
result = author;
|
231
|
}
|
232
|
return result;
|
233
|
}
|
234
|
}
|
235
|
|
236
|
|
237
|
/**
|
238
|
* @param state
|
239
|
* @param author
|
240
|
*/
|
241
|
private void handleTeam(SimpleExcelTaxonImportState<CONFIG> state, Team team) {
|
242
|
List<Person> members = team.getTeamMembers();
|
243
|
for (int i =0; i< members.size(); i++){
|
244
|
Person person = members.get(i);
|
245
|
//FIXME cast find a better way to guarantee that only persons are returned
|
246
|
Person existingPerson = (Person)state.getAgentBase(person.getTitleCache());
|
247
|
if (existingPerson != null){
|
248
|
members.set(i, existingPerson);
|
249
|
}else{
|
250
|
state.putAgentBase(person.getTitleCache(), person);
|
251
|
}
|
252
|
}
|
253
|
|
254
|
}
|
255
|
|
256
|
|
257
|
}
|