Project

General

Profile

Download (15.5 KB) Statistics
| Branch: | Revision:
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.net.URI;
12
import java.util.HashMap;
13
import java.util.Map;
14
import java.util.Set;
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.ext.geo.GeoServiceArea;
22
import eu.etaxonomy.cdm.ext.geo.GeoServiceAreaAnnotatedMapping;
23
import eu.etaxonomy.cdm.io.common.mapping.UndefinedTransformerMethodException;
24
import eu.etaxonomy.cdm.model.common.Language;
25
import eu.etaxonomy.cdm.model.common.OrderedTermVocabulary;
26
import eu.etaxonomy.cdm.model.common.OriginalSourceType;
27
import eu.etaxonomy.cdm.model.common.Representation;
28
import eu.etaxonomy.cdm.model.common.TermType;
29
import eu.etaxonomy.cdm.model.description.Distribution;
30
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
31
import eu.etaxonomy.cdm.model.description.TaxonDescription;
32
import eu.etaxonomy.cdm.model.location.NamedArea;
33
import eu.etaxonomy.cdm.model.location.NamedAreaLevel;
34
import eu.etaxonomy.cdm.model.location.NamedAreaType;
35
import eu.etaxonomy.cdm.model.reference.Reference;
36
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
37
import eu.etaxonomy.cdm.model.taxon.Taxon;
38

    
39
/**
40
 * @author a.mueller
41
 * @since 16.06.2016
42
 *
43
 */
44
@Component
45
public class MexicoConabioDistributionImport<CONFIG extends MexicoConabioImportConfigurator>
46
            extends SimpleExcelTaxonImport<CONFIG> {
47

    
48
    private static final long serialVersionUID = -1733208053395372094L;
49

    
50
    private static final Logger logger = Logger.getLogger(MexicoConabioDistributionImport.class);
51

    
52
    private OrderedTermVocabulary<NamedArea> stateAreasVoc;
53
    private NamedArea mexico;
54

    
55
    private Map<String, Taxon> taxonIdMap;
56

    
57

    
58
    @Override
59
    protected String getWorksheetName() {
60
        return "DistribucionEstatal";
61
    }
62

    
63
    @SuppressWarnings("unchecked")
64
    private void initTaxa() {
65
        if (taxonIdMap == null){
66
            Set<String> existingKeys = MexicoConabioTaxonImport.taxonIdMap.keySet();
67
            taxonIdMap = (Map<String, Taxon>)getCommonService().getSourcedObjectsByIdInSource(Taxon.class,
68
                    existingKeys, MexicoConabioTaxonImport.TAXON_NAMESPACE);
69
        }
70
    }
71

    
72
    @Override
73
    protected void firstPass(SimpleExcelTaxonImportState<CONFIG> state) {
74
        initAreaVocabulary(state);
75
        initTaxa();
76

    
77
        String line = state.getCurrentLine() + ": ";
78
        HashMap<String, String> record = state.getOriginalRecord();
79

    
80
        String idCat = getValue(record, "IdCAT");
81
        Taxon taxon = taxonIdMap.get(idCat);
82
        if (taxon == null){
83
            logger.warn(line + "Taxon could not be found: " + idCat);
84
        }else{
85
            TaxonDescription desc = getTaxonDescription(taxon);
86

    
87
            String distrStatusStr = getValue(record, "TipoDistribucion");
88
            try {
89
                if (distrStatusStr != null){
90
                    PresenceAbsenceTerm mexicanDistributionStatus = state.getTransformer().getPresenceTermByKey(distrStatusStr);
91
                    if (mexicanDistributionStatus == null){
92
                        UUID statusUuid = state.getTransformer().getPresenceTermUuid(distrStatusStr);
93
                        mexicanDistributionStatus = getPresenceTerm(state, statusUuid,
94
                                distrStatusStr, distrStatusStr, null, false);
95
                    }
96
                    if (mexicanDistributionStatus != null){
97
                        Distribution mexicanDistribution = Distribution.NewInstance(mexico, mexicanDistributionStatus);
98
                        desc.addElement(mexicanDistribution);
99
                        String refStr = getValue(record, "ReferenciaTipoDistribucion");
100
                        Reference ref = getReference(state, refStr);
101
                        if (ref != null){
102
                            mexicanDistribution.addSource(OriginalSourceType.PrimaryTaxonomicSource,
103
                                    null, null, ref, null);
104
                        }
105
                    }
106
                }
107
            } catch (UndefinedTransformerMethodException e) {}
108

    
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);
141

    
142
            getTaxonService().save(taxon);
143
        }
144

    
145

    
146
    }
147

    
148
    /**
149
     * @param state
150
     * @param refStr
151
     * @return
152
     */
153
    private Reference getReference(SimpleExcelTaxonImportState<CONFIG> state, String refStr) {
154
        if (StringUtils.isNoneBlank(refStr)){
155
            return null;
156
        }
157
        Reference ref = state.getReference(refStr);
158
        if (ref == null){
159
            ref = ReferenceFactory.newBook();
160
            ref.setTitleCache(refStr, true);
161
            state.putReference(refStr, ref);
162
        }
163
        return ref;
164
    }
165

    
166
    /**
167
     * @param desc
168
     * @param string
169
     * @param uuidUserDefinedAnnotationTypeVocabulary
170
     */
171
    private void handleDistribution(SimpleExcelTaxonImportState<CONFIG> state, TaxonDescription desc, String key,
172
            UUID uuid, String line) {
173
        HashMap<String, String> record = state.getOriginalRecord();
174
        String value = getValue(record, key);
175
        if ("1".equals(value)){
176
            NamedArea area = getNamedArea(state, uuid, null, null, null, null, null);
177
            Distribution dist = Distribution.NewInstance(area, PresenceAbsenceTerm.PRESENT());
178
            desc.addElement(dist);
179
        }else if (value != null){
180
            logger.warn(line + "Unrecognized distribution status '" + value + "' for " + key);
181
        }
182
    }
183

    
184
    /**
185
     * @param taxon
186
     * @return
187
     */
188
    private TaxonDescription getTaxonDescription(Taxon taxon) {
189
        if (!taxon.getDescriptions().isEmpty()){
190
            return taxon.getDescriptions().iterator().next();
191
        }else{
192
            TaxonDescription desc = TaxonDescription.NewInstance(taxon);
193
            return desc;
194
        }
195
    }
196

    
197
    /**
198
     * @param state
199
     */
200
    @SuppressWarnings("unchecked")
201
    private void initAreaVocabulary(SimpleExcelTaxonImportState<CONFIG> state) {
202
        if (stateAreasVoc == null){
203
            stateAreasVoc = (OrderedTermVocabulary<NamedArea>)this.getVocabularyService().find(MexicoConabioTransformer.uuidMexicanStatesVoc);
204
            if (stateAreasVoc == null){
205
                createStateAreasVoc(state);
206
            }
207
        }
208
    }
209

    
210
    /**
211
     * @param state
212
     * @return
213
     */
214
    @SuppressWarnings("unchecked")
215
    private void createStateAreasVoc(SimpleExcelTaxonImportState<CONFIG> state) {
216
        //voc
217
        URI termSourceUri = null;
218
        String label = "Mexican States";
219
        String description = "Mexican States as used by the CONABIO Rubiaceae database";
220
        stateAreasVoc = OrderedTermVocabulary.NewInstance(TermType.NamedArea,
221
                description, label, null, termSourceUri);
222
        stateAreasVoc.setUuid(MexicoConabioTransformer.uuidMexicanStatesVoc);
223
        Representation rep = Representation.NewInstance("Estados Méxicanos", "Estados Méxicanos", null, Language.SPANISH_CASTILIAN());
224
        stateAreasVoc.addRepresentation(rep);
225

    
226
        //mexico country
227
        String mexicoLabel = "Mexico (Country)";
228
        mexico = NamedArea.NewInstance(
229
                mexicoLabel, mexicoLabel, null);
230
        mexico.setUuid(MexicoConabioTransformer.uuidMexicoCountry);
231
        stateAreasVoc.addTerm(mexico);
232
         addMapping(mexico, "mex_adm0", "iso", "MEX");
233

    
234
         //Example with almost all areas is Chiococca alba
235
         addArea(state, "Aguascalientes", MexicoConabioTransformer.uuidAguascalientes);
236
         addArea(state, "Baja California", MexicoConabioTransformer.uuidBaja_california);
237
         addArea(state, "Baja California Sur", MexicoConabioTransformer.uuidBaja_california_sur);
238
         addArea(state, "Campeche", MexicoConabioTransformer.uuidCampeche);
239
         addArea(state, "Coahuila de Zaragoza", MexicoConabioTransformer.uuidCoahuila_de_zaragoza, "Coahuila");
240
         addArea(state, "Colima", MexicoConabioTransformer.uuidColima);
241
         addArea(state, "Chiapas", MexicoConabioTransformer.uuidChiapas);
242
         addArea(state, "Chihuahua", MexicoConabioTransformer.uuidChihuahua);
243
         addArea(state, "Distrito Federal", MexicoConabioTransformer.uuidDistrito_federal);
244
         addArea(state, "Durango", MexicoConabioTransformer.uuidDurango);
245
         addArea(state, "Guanajuato", MexicoConabioTransformer.uuidGuanajuato);
246
         addArea(state, "Guerrero", MexicoConabioTransformer.uuidGuerrero);
247
         addArea(state, "Hidalgo", MexicoConabioTransformer.uuidHidalgo);
248
         addArea(state, "Jalisco", MexicoConabioTransformer.uuidJalisco);
249
        //id_1
250
        addArea(state, "México", MexicoConabioTransformer.uuidMexico, null, 15);
251
        //id_1
252
        addArea(state, "Michoacan de Ocampo", MexicoConabioTransformer.uuidMichoacan_de_ocampo, "Michoacán", 16);
253
         addArea(state, "Morelos", MexicoConabioTransformer.uuidMorelos);
254
         addArea(state, "Nayarit", MexicoConabioTransformer.uuidNayarit);
255
        //gibt beim mapping vielleicht Probleme wg. des Accents
256
        //id_1
257
        addArea(state, "Nuevo Leon", MexicoConabioTransformer.uuidNuevo_leon, "Nuevo León", 19);
258
         addArea(state, "Oaxaca", MexicoConabioTransformer.uuidOaxaca);
259
         addArea(state, "Puebla", MexicoConabioTransformer.uuidPuebla);
260
        //id_1
261
        addArea(state, "Queretaro de Arteaga", MexicoConabioTransformer.uuidQueretaro_de_arteaga, "Querétaro", 22);
262
         addArea(state, "Quintana Roo", MexicoConabioTransformer.uuidQuintana_roo);
263
        //id_1
264
        addArea(state, "San Luis Potosí", MexicoConabioTransformer.uuidSan_luis_potosi,null ,24);
265
         addArea(state, "Sinaloa", MexicoConabioTransformer.uuidSinaloa);
266
         addArea(state, "Sonora", MexicoConabioTransformer.uuidSonora);
267
         addArea(state, "Tabasco", MexicoConabioTransformer.uuidTabasco);
268
         addArea(state, "Tamaulipas", MexicoConabioTransformer.uuidTamaulipas);
269
         addArea(state, "Tlaxcala", MexicoConabioTransformer.uuidTlaxcala);
270
         addArea(state, "Veracruz de Ignacio de la Llave", MexicoConabioTransformer.uuidVeracruz_de_ignacio_de_la_llave, "Veracruz");
271
        //??
272
        addArea(state, "Yucatán", MexicoConabioTransformer.uuidYucatan, null, 31);
273
         addArea(state, "Zacatecas", MexicoConabioTransformer.uuidZacatecas);
274

    
275
        this.getVocabularyService().save(stateAreasVoc);
276

    
277
        return;
278
    }
279

    
280

    
281
    private void addArea(SimpleExcelTaxonImportState<CONFIG> state, String areaLabel, UUID uuid) {
282
        addArea(state, areaLabel, uuid, areaLabel);  //short cut if label and mapping label are equal
283
    }
284

    
285
    private void addArea(SimpleExcelTaxonImportState<CONFIG> state, String areaLabel, UUID uuid, String mappingLabel) {
286
        addArea(state, areaLabel, uuid, mappingLabel, null);  //short cut if label and mapping label are equal
287
    }
288

    
289

    
290
    /**
291
     * @param state
292
     * @param string
293
     * @param uuidaguascalientes
294
     */
295
    private void addArea(SimpleExcelTaxonImportState<CONFIG> state, String areaLabel, UUID uuid, String mappingLabel, Integer id1) {
296
        String abbrev = null;
297
        NamedArea newArea = NamedArea.NewInstance(
298
                areaLabel, areaLabel, abbrev);
299
        newArea.setUuid(uuid);
300
        newArea.setPartOf(mexico);
301
        newArea.setLevel(NamedAreaLevel.STATE());
302
        newArea.setType(NamedAreaType.ADMINISTRATION_AREA());
303
        stateAreasVoc.addTerm(newArea);
304
        if (id1 != null){
305
            addMapping(newArea, "mex_adm1", "id_1", String.valueOf(id1));
306
        }else if (mappingLabel != null){
307
            addMapping(newArea, "mex_adm1", "name_1", mappingLabel);
308
        }
309
    }
310

    
311
    private void addMapping(NamedArea area, String mapping_layer, String mapping_field, String abbrev) {
312
        GeoServiceAreaAnnotatedMapping mapping = (GeoServiceAreaAnnotatedMapping)this.getBean("geoServiceAreaAnnotatedMapping");
313
        GeoServiceArea geoServiceArea = new GeoServiceArea();
314
        geoServiceArea.add(mapping_layer, mapping_field, abbrev);
315
        mapping.set(area, geoServiceArea);
316
    }
317

    
318
    @Override
319
    protected boolean isIgnore(SimpleExcelTaxonImportState<CONFIG> state) {
320
        return ! state.getConfig().isDoDistributions();
321
    }
322

    
323
}
(4-4/9)