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.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.common.OrderedTermVocabulary;
25
import eu.etaxonomy.cdm.model.common.OriginalSourceType;
26
import eu.etaxonomy.cdm.model.common.Representation;
27
import eu.etaxonomy.cdm.model.common.TermType;
28
import eu.etaxonomy.cdm.model.description.Distribution;
29
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
30
import eu.etaxonomy.cdm.model.description.TaxonDescription;
31
import eu.etaxonomy.cdm.model.location.NamedArea;
32
import eu.etaxonomy.cdm.model.location.NamedAreaLevel;
33
import eu.etaxonomy.cdm.model.location.NamedAreaType;
34
import eu.etaxonomy.cdm.model.reference.Reference;
35
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
36
import eu.etaxonomy.cdm.model.taxon.Taxon;
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
    @SuppressWarnings("unchecked")
63
    private void initTaxa() {
64
        if (taxonIdMap == null){
65
            Set<String> existingKeys = MexicoConabioTaxonImport.taxonIdMap.keySet();
66
            taxonIdMap = (Map<String, Taxon>)getCommonService().getSourcedObjectsByIdInSource(Taxon.class,
67
                    existingKeys, MexicoConabioTaxonImport.TAXON_NAMESPACE);
68
        }
69
    }
70

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

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

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

    
86
            String distrStatusStr = getValue(record, "TipoDistribucion");
87
            try {
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);
94
                    }
95
                    if (mexicanDistributionStatus != null){
96
                        Distribution mexicanDistribution = Distribution.NewInstance(mexico, mexicanDistributionStatus);
97
                        desc.addElement(mexicanDistribution);
98
                        String refStr = getValue(record, "ReferenciaTipoDistribucion");
99
                        Reference ref = getReference(state, refStr);
100
                        if (ref != null){
101
                            mexicanDistribution.addSource(OriginalSourceType.PrimaryTaxonomicSource,
102
                                    null, null, ref, null);
103
                        }
104
                    }
105
                }
106
            } catch (UndefinedTransformerMethodException e) {}
107

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

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

    
144

    
145
    }
146

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

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

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

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

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

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

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

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

    
276
        return;
277
    }
278

    
279

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

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

    
288

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

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

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

    
322
}
(4-4/9)