Project

General

Profile

Download (15.5 KB) Statistics
| Branch: | Revision:
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.Set;
16
import java.util.UUID;
17

    
18
import org.apache.commons.lang3.StringUtils;
19
import org.apache.log4j.Logger;
20
import org.springframework.stereotype.Component;
21

    
22
import eu.etaxonomy.cdm.ext.geo.GeoServiceArea;
23
import eu.etaxonomy.cdm.ext.geo.GeoServiceAreaAnnotatedMapping;
24
import eu.etaxonomy.cdm.io.common.mapping.UndefinedTransformerMethodException;
25
import eu.etaxonomy.cdm.model.common.Language;
26
import eu.etaxonomy.cdm.model.common.OrderedTermVocabulary;
27
import eu.etaxonomy.cdm.model.common.OriginalSourceType;
28
import eu.etaxonomy.cdm.model.common.Representation;
29
import eu.etaxonomy.cdm.model.common.TermType;
30
import eu.etaxonomy.cdm.model.description.Distribution;
31
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
32
import eu.etaxonomy.cdm.model.description.TaxonDescription;
33
import eu.etaxonomy.cdm.model.location.NamedArea;
34
import eu.etaxonomy.cdm.model.location.NamedAreaLevel;
35
import eu.etaxonomy.cdm.model.location.NamedAreaType;
36
import eu.etaxonomy.cdm.model.reference.Reference;
37
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
38
import eu.etaxonomy.cdm.model.taxon.Taxon;
39

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

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

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

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

    
56
    private Map<String, Taxon> taxonIdMap;
57

    
58

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

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

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

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

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

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

    
110
            handleDistribution(state, desc, "AGUASCALIENTES", MexicoConabioTransformer.uuidAguascalientes, line);
111
            handleDistribution(state, desc, "BAJA CALIFORNIA", MexicoConabioTransformer.uuidBaja_california, line);
112
            handleDistribution(state, desc, "BAJA CALIFORNIA SUR", MexicoConabioTransformer.uuidBaja_california_sur, line);
113
            handleDistribution(state, desc, "CAMPECHE", MexicoConabioTransformer.uuidCampeche, line);
114
            handleDistribution(state, desc, "COAHUILA DE ZARAGOZA", MexicoConabioTransformer.uuidCoahuila_de_zaragoza, line);
115
            handleDistribution(state, desc, "COLIMA", MexicoConabioTransformer.uuidColima, line);
116
            handleDistribution(state, desc, "CHIAPAS", MexicoConabioTransformer.uuidChiapas, line);
117
            handleDistribution(state, desc, "CHIHUAHUA", MexicoConabioTransformer.uuidChihuahua, line);
118
            handleDistribution(state, desc, "DISTRITO FEDERAL", MexicoConabioTransformer.uuidDistrito_federal, line);
119
            handleDistribution(state, desc, "DURANGO", MexicoConabioTransformer.uuidDurango, line);
120
            handleDistribution(state, desc, "GUANAJUATO", MexicoConabioTransformer.uuidGuanajuato, line);
121
            handleDistribution(state, desc, "GUERRERO", MexicoConabioTransformer.uuidGuerrero, line);
122
            handleDistribution(state, desc, "HIDALGO", MexicoConabioTransformer.uuidHidalgo, line);
123
            handleDistribution(state, desc, "JALISCO", MexicoConabioTransformer.uuidJalisco, line);
124
            handleDistribution(state, desc, "MEXICO", MexicoConabioTransformer.uuidMexico, line);
125
            handleDistribution(state, desc, "MICHOACAN DE OCAMPO", MexicoConabioTransformer.uuidMichoacan_de_ocampo, line);
126
            handleDistribution(state, desc, "MORELOS", MexicoConabioTransformer.uuidMorelos, line);
127
            handleDistribution(state, desc, "NAYARIT", MexicoConabioTransformer.uuidNayarit, line);
128
            handleDistribution(state, desc, "NUEVO LEON", MexicoConabioTransformer.uuidNuevo_leon, line);
129
            handleDistribution(state, desc, "OAXACA", MexicoConabioTransformer.uuidOaxaca, line);
130
            handleDistribution(state, desc, "PUEBLA", MexicoConabioTransformer.uuidPuebla, line);
131
            handleDistribution(state, desc, "QUERETARO DE ARTEAGA", MexicoConabioTransformer.uuidQueretaro_de_arteaga, line);
132
            handleDistribution(state, desc, "QUINTANA ROO", MexicoConabioTransformer.uuidQuintana_roo, line);
133
            handleDistribution(state, desc, "SAN LUIS POTOSI", MexicoConabioTransformer.uuidSan_luis_potosi, line);
134
            handleDistribution(state, desc, "SINALOA", MexicoConabioTransformer.uuidSinaloa, line);
135
            handleDistribution(state, desc, "SONORA", MexicoConabioTransformer.uuidSonora, line);
136
            handleDistribution(state, desc, "TABASCO", MexicoConabioTransformer.uuidTabasco, line);
137
            handleDistribution(state, desc, "TAMAULIPAS", MexicoConabioTransformer.uuidTamaulipas, line);
138
            handleDistribution(state, desc, "TLAXCALA", MexicoConabioTransformer.uuidTlaxcala, line);
139
            handleDistribution(state, desc, "VERACRUZ DE IGNACIO DE LA LLAVE", MexicoConabioTransformer.uuidVeracruz_de_ignacio_de_la_llave, line);
140
            handleDistribution(state, desc, "YUCATAN", MexicoConabioTransformer.uuidYucatan, line);
141
            handleDistribution(state, desc, "ZACATECAS", MexicoConabioTransformer.uuidZacatecas, line);
142

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

    
146

    
147
    }
148

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

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

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

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

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

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

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

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

    
278
        return;
279
    }
280

    
281

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

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

    
290

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

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

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

    
324
}
(4-4/9)