Project

General

Profile

Download (15.4 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 eu.etaxonomy.cdm.common.URI;
12
import java.util.Map;
13
import java.util.Set;
14
import java.util.UUID;
15

    
16
import org.apache.commons.lang3.StringUtils;
17
import org.apache.log4j.Logger;
18
import org.springframework.stereotype.Component;
19

    
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.Language;
24
import eu.etaxonomy.cdm.model.description.Distribution;
25
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
26
import eu.etaxonomy.cdm.model.description.TaxonDescription;
27
import eu.etaxonomy.cdm.model.location.NamedArea;
28
import eu.etaxonomy.cdm.model.location.NamedAreaLevel;
29
import eu.etaxonomy.cdm.model.location.NamedAreaType;
30
import eu.etaxonomy.cdm.model.reference.OriginalSourceType;
31
import eu.etaxonomy.cdm.model.reference.Reference;
32
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
33
import eu.etaxonomy.cdm.model.taxon.Taxon;
34
import eu.etaxonomy.cdm.model.term.OrderedTermVocabulary;
35
import eu.etaxonomy.cdm.model.term.Representation;
36
import eu.etaxonomy.cdm.model.term.TermType;
37

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

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

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

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

    
54
    private Map<String, Taxon> taxonIdMap;
55

    
56

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

    
62
    private void initTaxa() {
63
        if (taxonIdMap == null){
64
            Set<String> existingKeys = MexicoConabioTaxonImport.taxonIdMap.keySet();
65
            taxonIdMap = getCommonService().getSourcedObjectsByIdInSourceC(Taxon.class,
66
                    existingKeys, MexicoConabioTaxonImport.TAXON_NAMESPACE);
67
        }
68
    }
69

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

    
75
        String line = state.getCurrentLine() + ": ";
76
        Map<String, String> record = state.getOriginalRecord();
77

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

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

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

    
140
            getTaxonService().save(taxon);
141
        }
142

    
143

    
144
    }
145

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

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

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

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

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

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

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

    
273
        this.getVocabularyService().save(stateAreasVoc);
274

    
275
        return;
276
    }
277

    
278

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

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

    
287

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

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

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

    
321
}
(4-4/9)