1
|
/**
|
2
|
* Copyright (C) 2007 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
|
|
10
|
package eu.etaxonomy.cdm.io.wp6;
|
11
|
|
12
|
import java.util.HashMap;
|
13
|
import java.util.List;
|
14
|
import java.util.Map;
|
15
|
import java.util.Set;
|
16
|
import java.util.UUID;
|
17
|
|
18
|
import org.apache.commons.lang.StringUtils;
|
19
|
import org.apache.logging.log4j.LogManager;
|
20
|
import org.apache.logging.log4j.Logger;
|
21
|
import org.hibernate.criterion.Criterion;
|
22
|
import org.springframework.stereotype.Component;
|
23
|
|
24
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
25
|
import eu.etaxonomy.cdm.io.common.mapping.UndefinedTransformerMethodException;
|
26
|
import eu.etaxonomy.cdm.io.excel.common.ExcelImportBase;
|
27
|
import eu.etaxonomy.cdm.io.excel.common.ExcelRowBase;
|
28
|
import eu.etaxonomy.cdm.model.common.Language;
|
29
|
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
|
30
|
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
|
31
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
32
|
import eu.etaxonomy.cdm.model.location.Country;
|
33
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
34
|
import eu.etaxonomy.cdm.model.reference.OriginalSourceType;
|
35
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
36
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
37
|
import eu.etaxonomy.cdm.model.term.TermType;
|
38
|
import eu.etaxonomy.cdm.model.term.TermVocabulary;
|
39
|
import eu.etaxonomy.cdm.persistence.query.OrderHint;
|
40
|
|
41
|
/**
|
42
|
* @author a.babadshanjan
|
43
|
* @since 08.01.2009
|
44
|
*/
|
45
|
@Component
|
46
|
public class CichorieaeCommonNameExcelImport
|
47
|
extends ExcelImportBase<CichorieaeCommonNameImportState, CommonNameImportConfigurator, ExcelRowBase> {
|
48
|
|
49
|
private static final long serialVersionUID = 747044451715401750L;
|
50
|
private static final Logger logger = LogManager.getLogger();
|
51
|
|
52
|
protected static final String SPECIES_COLUMN = "Art";
|
53
|
protected static final String COMMON_NAME_COLUMN = "common name";
|
54
|
protected static final String REFERENCE_COLUMN = "Literaturnummer";
|
55
|
protected static final String DISTIRBUTION_COLUMN = "Verbreitung";
|
56
|
protected static final String AREA_COLUMN = "Vorschlag Bezeichnung L�nder/Regionen";
|
57
|
|
58
|
private Map<String, NamedArea> areaStore = new HashMap<>();
|
59
|
private Map<String, Language> languageStore = new HashMap<>();
|
60
|
|
61
|
|
62
|
@Override
|
63
|
protected boolean isIgnore(CichorieaeCommonNameImportState state) {
|
64
|
return false;
|
65
|
}
|
66
|
|
67
|
@Override
|
68
|
protected boolean doCheck(CichorieaeCommonNameImportState state) {
|
69
|
logger.warn("DoCheck not yet implemented for CommonNameExcelImport");
|
70
|
return true;
|
71
|
}
|
72
|
|
73
|
|
74
|
@Override
|
75
|
protected void analyzeRecord(Map<String, String> record, CichorieaeCommonNameImportState state) {
|
76
|
Set<String> keys = record.keySet();
|
77
|
|
78
|
CommonNameRow row = new CommonNameRow();
|
79
|
state.setCommonNameRow(row);
|
80
|
|
81
|
for (String originalKey: keys) {
|
82
|
String indexedKey = CdmUtils.removeDuplicateWhitespace(originalKey.trim()).toString();
|
83
|
String[] split = indexedKey.split("_");
|
84
|
String key = split[0];
|
85
|
if (split.length > 1){
|
86
|
String indexString = split[1];
|
87
|
try {
|
88
|
Integer.valueOf(indexString);
|
89
|
} catch (NumberFormatException e) {
|
90
|
String message = "Index must be integer";
|
91
|
logger.error(message);
|
92
|
continue;
|
93
|
}
|
94
|
}
|
95
|
|
96
|
String value = record.get(indexedKey);
|
97
|
if (! StringUtils.isBlank(value)) {
|
98
|
if (logger.isDebugEnabled()) { logger.debug(key + ": " + value); }
|
99
|
value = CdmUtils.removeDuplicateWhitespace(value.trim()).toString();
|
100
|
}else{
|
101
|
continue;
|
102
|
}
|
103
|
|
104
|
|
105
|
if (key.equalsIgnoreCase(SPECIES_COLUMN)) {
|
106
|
row.parseSpecies(value);
|
107
|
|
108
|
} else if(key.equalsIgnoreCase(COMMON_NAME_COLUMN)) {
|
109
|
row.setCommonNames(value);
|
110
|
|
111
|
} else if(key.equalsIgnoreCase(REFERENCE_COLUMN)) {
|
112
|
row.setReference(value);
|
113
|
} else if(key.equalsIgnoreCase(DISTIRBUTION_COLUMN)) {
|
114
|
//do nothing
|
115
|
|
116
|
} else if(key.equalsIgnoreCase(AREA_COLUMN)) {
|
117
|
row.setArea(value);
|
118
|
} else {
|
119
|
state.setUnsuccessfull();
|
120
|
logger.error("Unexpected column header " + key);
|
121
|
}
|
122
|
}
|
123
|
return;
|
124
|
}
|
125
|
|
126
|
|
127
|
/**
|
128
|
* Stores taxa records in DB
|
129
|
*/
|
130
|
@Override
|
131
|
protected void firstPass(CichorieaeCommonNameImportState state) {
|
132
|
|
133
|
CommonNameRow taxonLight = state.getCommonNameRow();
|
134
|
//species name
|
135
|
String speciesStr = taxonLight.getSpecies();
|
136
|
TaxonDescription taxonDesc = getTaxon(state, speciesStr);
|
137
|
Reference ref = getReference(state, taxonLight);
|
138
|
|
139
|
NamedArea area = getArea(state, taxonLight.getArea());
|
140
|
|
141
|
makeCommonNames(state, taxonLight.getCommonNames(), taxonDesc, ref, area, taxonLight.getNameUsedInSource());
|
142
|
|
143
|
// OLD
|
144
|
// TaxonNameBase nameUsedInSource = getNameUsedInSource(state, taxonLight.getNameUsedInSource());
|
145
|
|
146
|
getTaxonService().save(taxonDesc.getTaxon());
|
147
|
return;
|
148
|
}
|
149
|
|
150
|
|
151
|
|
152
|
private Map<String, Reference> referenceStore = new HashMap<String, Reference>();
|
153
|
private Reference getReference(CichorieaeCommonNameImportState state, CommonNameRow taxonLight) {
|
154
|
String reference = taxonLight.getReference();
|
155
|
Reference result = referenceStore.get(reference);
|
156
|
if (result == null){
|
157
|
result = getCommonService().getSourcedObjectByIdInSource(Reference.class, reference, "import to Berlin Model");
|
158
|
if (result == null){
|
159
|
logger.warn("Reference not found: " + reference + " for taxon " + taxonLight.getSpecies());
|
160
|
// result = ReferenceFactory.newGeneric();
|
161
|
// result.setTitleCache(reference);
|
162
|
}else{
|
163
|
referenceStore.put(reference, result);
|
164
|
}
|
165
|
}
|
166
|
return result;
|
167
|
}
|
168
|
|
169
|
|
170
|
|
171
|
private NamedArea getArea(CichorieaeCommonNameImportState state, String area) {
|
172
|
NamedArea result;
|
173
|
List<OrderHint> orderHints = null;
|
174
|
List<Criterion> criteria = null;
|
175
|
result = areaStore.get(area);
|
176
|
|
177
|
if (result == null){
|
178
|
try {
|
179
|
result = state.getTransformer().getNamedAreaByKey(area);
|
180
|
} catch (UndefinedTransformerMethodException e) {
|
181
|
e.printStackTrace();
|
182
|
}
|
183
|
if (result == null){
|
184
|
List<? extends NamedArea> candidates = getTermService().findByTitle(Country.class, area, null, criteria, null, null, orderHints, null).getRecords();
|
185
|
if (candidates.size() == 0){
|
186
|
candidates = getTermService().findByTitle(NamedArea.class, area, null, criteria, null, null, orderHints, null).getRecords();
|
187
|
}
|
188
|
if (candidates.size()>0){
|
189
|
//TODO
|
190
|
result = candidates.get(0);
|
191
|
}else{
|
192
|
UUID uuidArea;
|
193
|
try {
|
194
|
uuidArea = state.getTransformer().getNamedAreaUuid(area);
|
195
|
if (uuidArea == null){
|
196
|
logger.warn("Area not defined: " + area) ;
|
197
|
}
|
198
|
result = getNamedArea(state, uuidArea, area, area, null, null, null);
|
199
|
} catch (UndefinedTransformerMethodException e) {
|
200
|
e.printStackTrace();
|
201
|
}
|
202
|
if (result == null){
|
203
|
logger.warn("Area not defined: " + area) ;
|
204
|
}
|
205
|
}
|
206
|
}
|
207
|
areaStore.put(area, result);
|
208
|
}
|
209
|
return result;
|
210
|
}
|
211
|
|
212
|
|
213
|
Map<String, TaxonDescription> taxonStore = new HashMap<String, TaxonDescription>();
|
214
|
|
215
|
private TaxonDescription getTaxon(CichorieaeCommonNameImportState state, String taxonNameStr) {
|
216
|
TaxonDescription desc;
|
217
|
Taxon taxon;
|
218
|
|
219
|
if (taxonStore.get(taxonNameStr) != null){
|
220
|
desc = taxonStore.get(taxonNameStr);
|
221
|
}else{
|
222
|
taxon = getTaxonService().findBestMatchingTaxon(taxonNameStr);
|
223
|
// TaxonNameBase name = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
|
224
|
// name.setTitleCache(taxonNameStr, true);
|
225
|
//
|
226
|
// result = Taxon.NewInstance(name, null);
|
227
|
if (taxon == null){
|
228
|
logger.warn("Taxon not found: " + taxonNameStr);
|
229
|
desc = null;
|
230
|
}else{
|
231
|
desc = getNewDescription(state, taxon);
|
232
|
taxonStore.put(taxonNameStr, desc);
|
233
|
}
|
234
|
}
|
235
|
return desc;
|
236
|
}
|
237
|
|
238
|
private TaxonDescription getNewDescription(CichorieaeCommonNameImportState state, Taxon taxon) {
|
239
|
Reference excelRef = state.getConfig().getSourceReference();
|
240
|
TaxonDescription desc = TaxonDescription.NewInstance(taxon, false);
|
241
|
desc.setTitleCache("Common Names Excel import", true);
|
242
|
desc.addSource(OriginalSourceType.Import, null, null, excelRef, null);
|
243
|
return desc;
|
244
|
}
|
245
|
|
246
|
|
247
|
private void makeCommonNames(CichorieaeCommonNameImportState state, Map<String, List<String>> commonNamesMap, TaxonDescription description, Reference ref, NamedArea area, String nameUsedInSource) {
|
248
|
//Common Names
|
249
|
// TaxonDescription td = this.getTaxonDescription(mainTaxon, false, true);
|
250
|
for (String languageKey : commonNamesMap.keySet()){
|
251
|
Language language = getLanguage(state, languageKey);
|
252
|
List<String> commonNamesList = commonNamesMap.get(languageKey);
|
253
|
for (String strCommonName : commonNamesList){
|
254
|
CommonTaxonName commonName = CommonTaxonName.NewInstance(strCommonName, language, area);
|
255
|
if (ref != null || StringUtils.isNotBlank(nameUsedInSource)){
|
256
|
DescriptionElementSource source = DescriptionElementSource.NewPrimarySourceInstance(ref, null);
|
257
|
source.setOriginalInfo(nameUsedInSource);
|
258
|
commonName.addSource(source);
|
259
|
}else{
|
260
|
logger.debug("No reference defined");
|
261
|
}
|
262
|
description.addElement(commonName);
|
263
|
}
|
264
|
}
|
265
|
}
|
266
|
|
267
|
private Language getLanguage(CichorieaeCommonNameImportState state, String languageKey) {
|
268
|
if (languageKey.equals("*")){
|
269
|
return null;
|
270
|
}
|
271
|
Language result;
|
272
|
languageKey = languageKey.replace(", no ISO-Code", "");
|
273
|
result = languageStore.get(languageKey);
|
274
|
if (result == null){
|
275
|
try{
|
276
|
if (languageKey.length()<4){
|
277
|
try {
|
278
|
result = getTermService().getLanguageByIso(languageKey);
|
279
|
} catch (Exception e) {
|
280
|
// TODO Auto-generated catch block
|
281
|
e.printStackTrace();
|
282
|
}
|
283
|
}else{
|
284
|
UUID uuid = state.getTransformer().getLanguageUuid(languageKey);
|
285
|
result = (Language)getTermService().find(uuid);
|
286
|
}
|
287
|
if (result == null){
|
288
|
result = state.getTransformer().getLanguageByKey(languageKey);
|
289
|
if (result == null){
|
290
|
UUID uuidLanguage;
|
291
|
uuidLanguage = state.getTransformer().getLanguageUuid(languageKey);
|
292
|
if (uuidLanguage == null){
|
293
|
logger.warn("Language not defined: " + languageKey) ;
|
294
|
}
|
295
|
result = getLanguage(state, uuidLanguage, languageKey, languageKey, null);
|
296
|
if (result == null){
|
297
|
logger.warn("Language not defined: " + languageKey) ;
|
298
|
}
|
299
|
}else if (result.getId() == 0){
|
300
|
// UUID uuidLanguageVoc = UUID.fromString("45ac7043-7f5e-4f37-92f2-3874aaaef2de");
|
301
|
UUID uuidLanguageVoc = UUID.fromString("434cea89-9052-4567-b2db-ff77f42e9084");
|
302
|
TermVocabulary<Language> voc = getVocabulary(state, TermType.Language, uuidLanguageVoc, "User Defined Languages", "User Defined Languages", null, null, false, result);
|
303
|
// TermVocabulary<Language> voc = getVocabularyService().find(uuidLanguageVoc);
|
304
|
voc.addTerm(result);
|
305
|
getTermService().saveOrUpdate(result);
|
306
|
state.putLanguage(result);
|
307
|
}
|
308
|
}
|
309
|
languageStore.put(languageKey, result);
|
310
|
} catch (UndefinedTransformerMethodException e) {
|
311
|
e.printStackTrace();
|
312
|
}
|
313
|
|
314
|
}
|
315
|
return result;
|
316
|
}
|
317
|
|
318
|
|
319
|
/**
|
320
|
* Stores parent-child, synonym and common name relationships
|
321
|
*/
|
322
|
@Override
|
323
|
protected void secondPass(CichorieaeCommonNameImportState state) {
|
324
|
//no second pass
|
325
|
return;
|
326
|
}
|
327
|
|
328
|
|
329
|
|
330
|
// private TaxonNameBase getNameUsedInSource(CichorieaeCommonNameImportState state, String nameUsedInSource) {
|
331
|
// if (StringUtils.isBlank(nameUsedInSource)){
|
332
|
// return null;
|
333
|
// }else{
|
334
|
// Pager<TaxonNameBase> list = getNameService().findByName(BotanicalName.class, nameUsedInSource, null, null, null, null, null, null);
|
335
|
// if (list.getCount() > 0){
|
336
|
// return list.getRecords().get(0);
|
337
|
// }else{
|
338
|
// return null;
|
339
|
// }
|
340
|
// }
|
341
|
//
|
342
|
// }
|
343
|
|
344
|
|
345
|
|
346
|
}
|