2 * Copyright (C) 2016 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.io
.mexico
;
11 import eu
.etaxonomy
.cdm
.common
.URI
;
14 import java
.util
.UUID
;
16 import org
.apache
.commons
.lang3
.StringUtils
;
17 import org
.apache
.log4j
.Logger
;
18 import org
.springframework
.stereotype
.Component
;
20 import eu
.etaxonomy
.cdm
.ext
.geo
.GeoServiceArea
;
21 import eu
.etaxonomy
.cdm
.ext
.geo
.GeoServiceAreaAnnotatedMapping
;
22 import eu
.etaxonomy
.cdm
.io
.common
.mapping
.UndefinedTransformerMethodException
;
23 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
24 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
25 import eu
.etaxonomy
.cdm
.model
.description
.Distribution
;
26 import eu
.etaxonomy
.cdm
.model
.description
.PresenceAbsenceTerm
;
27 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
28 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
29 import eu
.etaxonomy
.cdm
.model
.location
.NamedAreaLevel
;
30 import eu
.etaxonomy
.cdm
.model
.location
.NamedAreaType
;
31 import eu
.etaxonomy
.cdm
.model
.reference
.OriginalSourceType
;
32 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
33 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceFactory
;
34 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
35 import eu
.etaxonomy
.cdm
.model
.term
.OrderedTermVocabulary
;
36 import eu
.etaxonomy
.cdm
.model
.term
.Representation
;
37 import eu
.etaxonomy
.cdm
.model
.term
.TermType
;
45 public class MexicoConabioDistributionImport
<CONFIG
extends MexicoConabioImportConfigurator
>
46 extends SimpleExcelTaxonImport
<CONFIG
> {
48 private static final long serialVersionUID
= -1733208053395372094L;
50 private static final Logger logger
= Logger
.getLogger(MexicoConabioDistributionImport
.class);
52 private OrderedTermVocabulary
<NamedArea
> stateAreasVoc
;
53 private NamedArea mexico
;
55 private Map
<String
, Taxon
> taxonIdMap
;
59 protected String
getWorksheetName(CONFIG config
) {
60 return "DistribucionEstatal";
63 private void initTaxa() {
64 if (taxonIdMap
== null){
65 Set
<String
> existingKeys
= MexicoConabioTaxonImport
.taxonIdMap
.keySet();
66 taxonIdMap
= getCommonService().getSourcedObjectsByIdInSourceC(Taxon
.class,
67 existingKeys
, MexicoConabioTaxonImport
.TAXON_NAMESPACE
);
72 protected void firstPass(SimpleExcelTaxonImportState
<CONFIG
> state
) {
73 initAreaVocabulary(state
);
76 String line
= state
.getCurrentLine() + ": ";
77 Map
<String
, String
> record
= state
.getOriginalRecord();
79 String idCat
= getValue(record
, "IdCAT");
80 Taxon taxon
= taxonIdMap
.get(idCat
);
82 logger
.warn(line
+ "Taxon could not be found: " + idCat
);
84 TaxonDescription desc
= getTaxonDescription(taxon
);
86 String distrStatusStr
= getValue(record
, "TipoDistribucion");
88 if (distrStatusStr
!= null){
89 PresenceAbsenceTerm mexicanDistributionStatus
= state
.getTransformer().getPresenceTermByKey(distrStatusStr
);
90 if (mexicanDistributionStatus
== null){
91 UUID statusUuid
= state
.getTransformer().getPresenceTermUuid(distrStatusStr
);
92 mexicanDistributionStatus
= getPresenceTerm(state
, statusUuid
,
93 distrStatusStr
, distrStatusStr
, null, false);
95 if (mexicanDistributionStatus
!= null){
96 NamedArea mexicoCountry
= getMexico();
97 Distribution mexicanDistribution
= Distribution
.NewInstance(mexicoCountry
, mexicanDistributionStatus
);
98 desc
.addElement(mexicanDistribution
);
99 String refStr
= getValue(record
, "ReferenciaTipoDistribucion");
100 Reference ref
= getReference(state
, refStr
);
102 mexicanDistribution
.addSource(OriginalSourceType
.PrimaryTaxonomicSource
,
103 null, null, ref
, null);
107 } catch (UndefinedTransformerMethodException e
) {}
109 handleDistribution(state
, desc
, "AGUASCALIENTES", MexicoConabioTransformer
.uuidAguascalientes
, line
);
110 handleDistribution(state
, desc
, "BAJA CALIFORNIA", MexicoConabioTransformer
.uuidBaja_california
, line
);
111 handleDistribution(state
, desc
, "BAJA CALIFORNIA SUR", MexicoConabioTransformer
.uuidBaja_california_sur
, line
);
112 handleDistribution(state
, desc
, "CAMPECHE", MexicoConabioTransformer
.uuidCampeche
, line
);
113 handleDistribution(state
, desc
, "COAHUILA DE ZARAGOZA", MexicoConabioTransformer
.uuidCoahuila_de_zaragoza
, line
);
114 handleDistribution(state
, desc
, "COLIMA", MexicoConabioTransformer
.uuidColima
, line
);
115 handleDistribution(state
, desc
, "CHIAPAS", MexicoConabioTransformer
.uuidChiapas
, line
);
116 handleDistribution(state
, desc
, "CHIHUAHUA", MexicoConabioTransformer
.uuidChihuahua
, line
);
117 handleDistribution(state
, desc
, "DISTRITO FEDERAL", MexicoConabioTransformer
.uuidDistrito_federal
, line
);
118 handleDistribution(state
, desc
, "DURANGO", MexicoConabioTransformer
.uuidDurango
, line
);
119 handleDistribution(state
, desc
, "GUANAJUATO", MexicoConabioTransformer
.uuidGuanajuato
, line
);
120 handleDistribution(state
, desc
, "GUERRERO", MexicoConabioTransformer
.uuidGuerrero
, line
);
121 handleDistribution(state
, desc
, "HIDALGO", MexicoConabioTransformer
.uuidHidalgo
, line
);
122 handleDistribution(state
, desc
, "JALISCO", MexicoConabioTransformer
.uuidJalisco
, line
);
123 handleDistribution(state
, desc
, "MEXICO", MexicoConabioTransformer
.uuidMexico
, line
);
124 handleDistribution(state
, desc
, "MICHOACAN DE OCAMPO", MexicoConabioTransformer
.uuidMichoacan_de_ocampo
, line
);
125 handleDistribution(state
, desc
, "MORELOS", MexicoConabioTransformer
.uuidMorelos
, line
);
126 handleDistribution(state
, desc
, "NAYARIT", MexicoConabioTransformer
.uuidNayarit
, line
);
127 handleDistribution(state
, desc
, "NUEVO LEON", MexicoConabioTransformer
.uuidNuevo_leon
, line
);
128 handleDistribution(state
, desc
, "OAXACA", MexicoConabioTransformer
.uuidOaxaca
, line
);
129 handleDistribution(state
, desc
, "PUEBLA", MexicoConabioTransformer
.uuidPuebla
, line
);
130 handleDistribution(state
, desc
, "QUERETARO DE ARTEAGA", MexicoConabioTransformer
.uuidQueretaro_de_arteaga
, line
);
131 handleDistribution(state
, desc
, "QUINTANA ROO", MexicoConabioTransformer
.uuidQuintana_roo
, line
);
132 handleDistribution(state
, desc
, "SAN LUIS POTOSI", MexicoConabioTransformer
.uuidSan_luis_potosi
, line
);
133 handleDistribution(state
, desc
, "SINALOA", MexicoConabioTransformer
.uuidSinaloa
, line
);
134 handleDistribution(state
, desc
, "SONORA", MexicoConabioTransformer
.uuidSonora
, line
);
135 handleDistribution(state
, desc
, "TABASCO", MexicoConabioTransformer
.uuidTabasco
, line
);
136 handleDistribution(state
, desc
, "TAMAULIPAS", MexicoConabioTransformer
.uuidTamaulipas
, line
);
137 handleDistribution(state
, desc
, "TLAXCALA", MexicoConabioTransformer
.uuidTlaxcala
, line
);
138 handleDistribution(state
, desc
, "VERACRUZ DE IGNACIO DE LA LLAVE", MexicoConabioTransformer
.uuidVeracruz_de_ignacio_de_la_llave
, line
);
139 handleDistribution(state
, desc
, "YUCATAN", MexicoConabioTransformer
.uuidYucatan
, line
);
140 handleDistribution(state
, desc
, "ZACATECAS", MexicoConabioTransformer
.uuidZacatecas
, line
);
142 getTaxonService().save(taxon
);
146 private NamedArea
getMexico() {
148 mexico
= CdmBase
.deproxy(getTermService().find(MexicoConabioTransformer
.uuidMexicoCountry
), NamedArea
.class);
150 logger
.warn("Mexico country not found");
161 private Reference
getReference(SimpleExcelTaxonImportState
<CONFIG
> state
, String refStr
) {
162 if (StringUtils
.isBlank(refStr
)){
165 Reference ref
= state
.getReference(refStr
);
167 ref
= ReferenceFactory
.newBook();
168 ref
.setTitleCache(refStr
, true);
169 state
.putReference(refStr
, ref
);
177 * @param uuidUserDefinedAnnotationTypeVocabulary
179 private void handleDistribution(SimpleExcelTaxonImportState
<CONFIG
> state
, TaxonDescription desc
, String key
,
180 UUID uuid
, String line
) {
181 Map
<String
, String
> record
= state
.getOriginalRecord();
182 String value
= getValue(record
, key
);
183 if ("1".equals(value
)){
184 NamedArea area
= getNamedArea(state
, uuid
, null, null, null, null, null);
185 Distribution dist
= Distribution
.NewInstance(area
, PresenceAbsenceTerm
.PRESENT());
186 desc
.addElement(dist
);
187 }else if (value
!= null){
188 logger
.warn(line
+ "Unrecognized distribution status '" + value
+ "' for " + key
);
196 private TaxonDescription
getTaxonDescription(Taxon taxon
) {
197 if (!taxon
.getDescriptions().isEmpty()){
198 return taxon
.getDescriptions().iterator().next();
200 TaxonDescription desc
= TaxonDescription
.NewInstance(taxon
);
208 @SuppressWarnings("unchecked")
209 private void initAreaVocabulary(SimpleExcelTaxonImportState
<CONFIG
> state
) {
210 if (stateAreasVoc
== null){
211 stateAreasVoc
= (OrderedTermVocabulary
<NamedArea
>)this.getVocabularyService().find(MexicoConabioTransformer
.uuidMexicanStatesVoc
);
212 if (stateAreasVoc
== null){
213 createStateAreasVoc(state
);
222 @SuppressWarnings("unchecked")
223 private void createStateAreasVoc(SimpleExcelTaxonImportState
<CONFIG
> state
) {
225 URI termSourceUri
= null;
226 String label
= "Mexican States";
227 String description
= "Mexican States as used by the CONABIO Rubiaceae database";
228 stateAreasVoc
= OrderedTermVocabulary
.NewInstance(TermType
.NamedArea
,
229 description
, label
, null, termSourceUri
);
230 stateAreasVoc
.setUuid(MexicoConabioTransformer
.uuidMexicanStatesVoc
);
231 Representation rep
= Representation
.NewInstance("Estados Méxicanos", "Estados Méxicanos", null, Language
.SPANISH_CASTILIAN());
232 stateAreasVoc
.addRepresentation(rep
);
235 String mexicoLabel
= "Mexico (Country)";
236 mexico
= NamedArea
.NewInstance(
237 mexicoLabel
, mexicoLabel
, null);
238 mexico
.setUuid(MexicoConabioTransformer
.uuidMexicoCountry
);
239 stateAreasVoc
.addTerm(mexico
);
240 addMapping(mexico
, "mex_adm0", "iso", "MEX");
242 //Example with almost all areas is Chiococca alba
243 addArea(state
, "Aguascalientes", MexicoConabioTransformer
.uuidAguascalientes
);
244 addArea(state
, "Baja California", MexicoConabioTransformer
.uuidBaja_california
);
245 addArea(state
, "Baja California Sur", MexicoConabioTransformer
.uuidBaja_california_sur
);
246 addArea(state
, "Campeche", MexicoConabioTransformer
.uuidCampeche
);
247 addArea(state
, "Coahuila de Zaragoza", MexicoConabioTransformer
.uuidCoahuila_de_zaragoza
, "Coahuila");
248 addArea(state
, "Colima", MexicoConabioTransformer
.uuidColima
);
249 addArea(state
, "Chiapas", MexicoConabioTransformer
.uuidChiapas
);
250 addArea(state
, "Chihuahua", MexicoConabioTransformer
.uuidChihuahua
);
251 addArea(state
, "Distrito Federal", MexicoConabioTransformer
.uuidDistrito_federal
);
252 addArea(state
, "Durango", MexicoConabioTransformer
.uuidDurango
);
253 addArea(state
, "Guanajuato", MexicoConabioTransformer
.uuidGuanajuato
);
254 addArea(state
, "Guerrero", MexicoConabioTransformer
.uuidGuerrero
);
255 addArea(state
, "Hidalgo", MexicoConabioTransformer
.uuidHidalgo
);
256 addArea(state
, "Jalisco", MexicoConabioTransformer
.uuidJalisco
);
258 addArea(state
, "México", MexicoConabioTransformer
.uuidMexico
, null, 15);
260 addArea(state
, "Michoacan de Ocampo", MexicoConabioTransformer
.uuidMichoacan_de_ocampo
, "Michoacán", 16);
261 addArea(state
, "Morelos", MexicoConabioTransformer
.uuidMorelos
);
262 addArea(state
, "Nayarit", MexicoConabioTransformer
.uuidNayarit
);
263 //gibt beim mapping vielleicht Probleme wg. des Accents
265 addArea(state
, "Nuevo Leon", MexicoConabioTransformer
.uuidNuevo_leon
, "Nuevo León", 19);
266 addArea(state
, "Oaxaca", MexicoConabioTransformer
.uuidOaxaca
);
267 addArea(state
, "Puebla", MexicoConabioTransformer
.uuidPuebla
);
269 addArea(state
, "Queretaro de Arteaga", MexicoConabioTransformer
.uuidQueretaro_de_arteaga
, "Querétaro", 22);
270 addArea(state
, "Quintana Roo", MexicoConabioTransformer
.uuidQuintana_roo
);
272 addArea(state
, "San Luis Potosí", MexicoConabioTransformer
.uuidSan_luis_potosi
,null ,24);
273 addArea(state
, "Sinaloa", MexicoConabioTransformer
.uuidSinaloa
);
274 addArea(state
, "Sonora", MexicoConabioTransformer
.uuidSonora
);
275 addArea(state
, "Tabasco", MexicoConabioTransformer
.uuidTabasco
);
276 addArea(state
, "Tamaulipas", MexicoConabioTransformer
.uuidTamaulipas
);
277 addArea(state
, "Tlaxcala", MexicoConabioTransformer
.uuidTlaxcala
);
278 addArea(state
, "Veracruz de Ignacio de la Llave", MexicoConabioTransformer
.uuidVeracruz_de_ignacio_de_la_llave
, "Veracruz");
280 addArea(state
, "Yucatán", MexicoConabioTransformer
.uuidYucatan
, null, 31);
281 addArea(state
, "Zacatecas", MexicoConabioTransformer
.uuidZacatecas
);
283 this.getVocabularyService().save(stateAreasVoc
);
289 private void addArea(SimpleExcelTaxonImportState
<CONFIG
> state
, String areaLabel
, UUID uuid
) {
290 addArea(state
, areaLabel
, uuid
, areaLabel
); //short cut if label and mapping label are equal
293 private void addArea(SimpleExcelTaxonImportState
<CONFIG
> state
, String areaLabel
, UUID uuid
, String mappingLabel
) {
294 addArea(state
, areaLabel
, uuid
, mappingLabel
, null); //short cut if label and mapping label are equal
301 * @param uuidaguascalientes
303 private void addArea(SimpleExcelTaxonImportState
<CONFIG
> state
, String areaLabel
, UUID uuid
, String mappingLabel
, Integer id1
) {
304 String abbrev
= null;
305 NamedArea newArea
= NamedArea
.NewInstance(
306 areaLabel
, areaLabel
, abbrev
);
307 newArea
.setUuid(uuid
);
308 newArea
.setPartOf(mexico
);
309 newArea
.setLevel(NamedAreaLevel
.STATE());
310 newArea
.setType(NamedAreaType
.ADMINISTRATION_AREA());
311 stateAreasVoc
.addTerm(newArea
);
313 addMapping(newArea
, "mex_adm1", "id_1", String
.valueOf(id1
));
314 }else if (mappingLabel
!= null){
315 addMapping(newArea
, "mex_adm1", "name_1", mappingLabel
);
319 private void addMapping(NamedArea area
, String mapping_layer
, String mapping_field
, String abbrev
) {
320 GeoServiceAreaAnnotatedMapping mapping
= (GeoServiceAreaAnnotatedMapping
)this.getBean("geoServiceAreaAnnotatedMapping");
321 GeoServiceArea geoServiceArea
= new GeoServiceArea();
322 geoServiceArea
.add(mapping_layer
, mapping_field
, abbrev
);
323 mapping
.set(area
, geoServiceArea
);
327 protected boolean isIgnore(SimpleExcelTaxonImportState
<CONFIG
> state
) {
328 return ! state
.getConfig().isDoDistributions();