1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2016 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
package eu.etaxonomy.cdm.io.mexico;
|
11
|
|
12
|
import java.net.URI;
|
13
|
import java.util.HashMap;
|
14
|
import java.util.Map;
|
15
|
import java.util.UUID;
|
16
|
|
17
|
import org.apache.commons.lang3.StringUtils;
|
18
|
import org.apache.log4j.Logger;
|
19
|
import org.springframework.stereotype.Component;
|
20
|
|
21
|
import eu.etaxonomy.cdm.model.common.Language;
|
22
|
import eu.etaxonomy.cdm.model.common.OriginalSourceType;
|
23
|
import eu.etaxonomy.cdm.model.common.TermType;
|
24
|
import eu.etaxonomy.cdm.model.common.TermVocabulary;
|
25
|
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
|
26
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
27
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
28
|
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
|
29
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
30
|
|
31
|
/**
|
32
|
* @author a.mueller
|
33
|
* @date 16.06.2016
|
34
|
*
|
35
|
*/
|
36
|
@Component
|
37
|
public class MexicoConabioCommonNamesImport<CONFIG extends MexicoConabioImportConfigurator>
|
38
|
extends SimpleExcelTaxonImport<CONFIG>{
|
39
|
|
40
|
private static final long serialVersionUID = 3579868489510261569L;
|
41
|
|
42
|
private static final Logger logger = Logger.getLogger(MexicoConabioCommonNamesImport.class);
|
43
|
|
44
|
private TermVocabulary<Language> languagesVoc;
|
45
|
private Map<String, Language> languagesMap = new HashMap<>();
|
46
|
|
47
|
|
48
|
@Override
|
49
|
protected void firstPass(SimpleExcelTaxonImportState<CONFIG> state) {
|
50
|
initLanguageVocabulary(state);
|
51
|
|
52
|
String line = state.getCurrentLine() + ": ";
|
53
|
HashMap<String, String> record = state.getOriginalRecord();
|
54
|
|
55
|
String idCat = getValue(record, "IdCAT");
|
56
|
Taxon taxon = state.getHigherTaxon(idCat);
|
57
|
if (taxon == null){
|
58
|
logger.warn(line + "Taxon could not be found: " + idCat);
|
59
|
}else{
|
60
|
TaxonDescription desc = getTaxonDescription(taxon);
|
61
|
String nomComunStr = getValue(record, "NomComun");
|
62
|
String langStr = getValue(record, "Lengua");
|
63
|
Language language = languagesMap.get(langStr);
|
64
|
if (language == null){
|
65
|
logger.warn("Language not found: " + langStr);
|
66
|
}
|
67
|
|
68
|
String refStr = getValue(record, "ReferenciasNombreComun");
|
69
|
CommonTaxonName commonName = CommonTaxonName.NewInstance(nomComunStr,
|
70
|
language, null);
|
71
|
|
72
|
Reference ref = getReference(state, refStr);
|
73
|
if (ref != null){
|
74
|
commonName.addSource(OriginalSourceType.PrimaryTaxonomicSource,
|
75
|
null, null, ref, null);
|
76
|
}
|
77
|
|
78
|
|
79
|
getTaxonService().save(taxon);
|
80
|
}
|
81
|
}
|
82
|
|
83
|
/**
|
84
|
* @param state
|
85
|
*/
|
86
|
@SuppressWarnings("unchecked")
|
87
|
private void initLanguageVocabulary(SimpleExcelTaxonImportState<CONFIG> state) {
|
88
|
if (languagesVoc == null){
|
89
|
languagesVoc = this.getVocabularyService().find(MexicoConabioTransformer.uuidMexicanLanguagesVoc);
|
90
|
if (languagesVoc == null){
|
91
|
createLanguagesVoc(state);
|
92
|
}
|
93
|
}
|
94
|
}
|
95
|
|
96
|
/**
|
97
|
* @param state
|
98
|
* @return
|
99
|
*/
|
100
|
private void createLanguagesVoc(SimpleExcelTaxonImportState<CONFIG> state) {
|
101
|
URI termSourceUri = null;
|
102
|
String label = "Mexican States";
|
103
|
String description = "Mexican States as used by the CONABIO Rubiaceae database";
|
104
|
languagesVoc = TermVocabulary.NewInstance(TermType.Language,
|
105
|
description, label, null, termSourceUri);
|
106
|
languagesVoc.setUuid(MexicoConabioTransformer.uuidMexicanLanguagesVoc);
|
107
|
|
108
|
addLanguage(state, "Aguascalientes", MexicoConabioTransformer.uuidAguascalientes);
|
109
|
|
110
|
this.getVocabularyService().save(languagesVoc);
|
111
|
|
112
|
return;
|
113
|
}
|
114
|
|
115
|
/**
|
116
|
* @param state
|
117
|
* @param string
|
118
|
* @param uuidaguascalientes
|
119
|
*/
|
120
|
private void addLanguage(SimpleExcelTaxonImportState<CONFIG> state, String label, UUID uuid) {
|
121
|
String abbrev = null;
|
122
|
Language language = Language.NewInstance(
|
123
|
label, label, abbrev);
|
124
|
language.setUuid(uuid);
|
125
|
languagesVoc.addTerm(language);
|
126
|
languagesMap.put(label, language);
|
127
|
}
|
128
|
|
129
|
/**
|
130
|
* @param state
|
131
|
* @param refStr
|
132
|
* @return
|
133
|
*/
|
134
|
private Reference getReference(SimpleExcelTaxonImportState<CONFIG> state, String refStr) {
|
135
|
if (StringUtils.isNoneBlank(refStr)){
|
136
|
return null;
|
137
|
}
|
138
|
Reference ref = state.getReference(refStr);
|
139
|
if (ref == null){
|
140
|
ref = ReferenceFactory.newBook();
|
141
|
ref.setTitleCache(refStr, true);
|
142
|
state.putReference(refStr, ref);
|
143
|
}
|
144
|
return ref;
|
145
|
}
|
146
|
|
147
|
/**
|
148
|
* @param taxon
|
149
|
* @return
|
150
|
*/
|
151
|
private TaxonDescription getTaxonDescription(Taxon taxon) {
|
152
|
if (!taxon.getDescriptions().isEmpty()){
|
153
|
return taxon.getDescriptions().iterator().next();
|
154
|
}else{
|
155
|
TaxonDescription desc = TaxonDescription.NewInstance(taxon);
|
156
|
return desc;
|
157
|
}
|
158
|
}
|
159
|
|
160
|
@Override
|
161
|
protected boolean isIgnore(SimpleExcelTaxonImportState<CONFIG> state) {
|
162
|
return ! state.getConfig().isDoCommonNames();
|
163
|
}
|
164
|
|
165
|
}
|