Project

General

Profile

Download (21.7 KB) Statistics
| Branch: | Revision:
1
/**
2
 * Copyright (C) 2007 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.redlist.gefaesspflanzen;
10

    
11
import java.sql.ResultSet;
12
import java.sql.SQLException;
13
import java.util.Arrays;
14
import java.util.HashMap;
15
import java.util.HashSet;
16
import java.util.Map;
17
import java.util.Map.Entry;
18
import java.util.Set;
19
import java.util.UUID;
20

    
21
import org.apache.log4j.Logger;
22
import org.springframework.stereotype.Component;
23

    
24
import eu.etaxonomy.cdm.common.CdmUtils;
25
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
26
import eu.etaxonomy.cdm.io.common.DbImportBase;
27
import eu.etaxonomy.cdm.io.common.IPartitionedIO;
28
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
29
import eu.etaxonomy.cdm.model.common.CdmBase;
30
import eu.etaxonomy.cdm.model.common.Language;
31
import eu.etaxonomy.cdm.model.common.VerbatimTimePeriod;
32
import eu.etaxonomy.cdm.model.reference.Reference;
33
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
34
import eu.etaxonomy.cdm.model.taxon.Classification;
35
import eu.etaxonomy.cdm.model.taxon.Synonym;
36
import eu.etaxonomy.cdm.model.taxon.SynonymType;
37
import eu.etaxonomy.cdm.model.taxon.Taxon;
38
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
39
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
40

    
41
/**
42
 * @author pplitzner
43
 * @since Mar 1, 2016
44
 */
45
@Component
46
@SuppressWarnings("serial")
47
public class RedListGefaesspflanzenImportClassification extends DbImportBase<RedListGefaesspflanzenImportState, RedListGefaesspflanzenImportConfigurator> {
48

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

    
51
    private static final String tableName = "Rote Liste Gefäßpflanzen";
52

    
53
    private static final String pluralString = "classifications";
54

    
55
    public RedListGefaesspflanzenImportClassification() {
56
        super(tableName, pluralString);
57
    }
58

    
59
    @Override
60
    protected String getIdQuery(RedListGefaesspflanzenImportState state) {
61
        return "SELECT SEQNUM "
62
                + "FROM V_TAXATLAS_D20_EXPORT t "
63
                + " ORDER BY SEQNUM";
64
    }
65

    
66
    @Override
67
    protected String getRecordQuery(RedListGefaesspflanzenImportConfigurator config) {
68
        String result = "select distinct e.*, f.FAMILIE "
69
                + "from V_TAXATLAS_D20_EXPORT e, GATTUNG_FAMILIE f "
70
                + "where e.EPI1 = f.GATTUNG and e.SEQNUM IN (@IDSET)";
71
        result = result.replace("@IDSET", IPartitionedIO.ID_LIST_TOKEN);
72
        return result;
73
    }
74

    
75
    @Override
76
    protected void doInvoke(RedListGefaesspflanzenImportState state) {
77
        Classification gesamtListe = makeClassification("Gesamtliste", state.getConfig().getClassificationUuid(), "Gesamtliste", null, RedListUtil.gesamtListeReferenceUuid, state);
78
        Classification checkliste = makeClassification("Checkliste", RedListUtil.checkListClassificationUuid, "Checkliste", null, RedListUtil.checkListReferenceUuid, state);
79
        makeClassification("Ehrendorfer", RedListUtil.uuidClassificationE, "Ehrendorfer", null, RedListUtil.uuidClassificationReferenceE, state);
80
        makeClassification("Wisskirchen (Standardliste)", RedListUtil.uuidClassificationW, "Wisskirchen (Standardliste)", 1998, RedListUtil.uuidClassificationReferenceW, state);
81
        makeClassification("Korneck (Rote Liste)", RedListUtil.uuidClassificationK, "Korneck (Rote Liste)", 1996, RedListUtil.uuidClassificationReferenceK, state);
82
        makeClassification("Atlas (Westdeutschland)", RedListUtil.uuidClassificationAW, "Atlas (Westdeutschland)", 1988, RedListUtil.uuidClassificationReferenceAW, state);
83
        makeClassification("Atlas (Ostdeutschland)", RedListUtil.uuidClassificationAO, "Atlas (Ostdeutschland)", 1996, RedListUtil.uuidClassificationReferenceAO, state);
84
        makeClassification("Rothmaler", RedListUtil.uuidClassificationR, "Rothmaler", 2011, RedListUtil.uuidClassificationReferenceR, state);
85
        makeClassification("Oberdorfer", RedListUtil.uuidClassificationO, "Oberdorfer", 2001, RedListUtil.uuidClassificationReferenceO, state);
86
        makeClassification("Schmeil-Fitschen", RedListUtil.uuidClassificationS, "Schmeil-Fitschen", 2011, RedListUtil.uuidClassificationReferenceS, state);
87
        importFamilies(gesamtListe, checkliste, state);
88
        super.doInvoke(state);
89
    }
90

    
91
    private void importFamilies(Classification gesamtListe, Classification checkliste, RedListGefaesspflanzenImportState state) {
92
        for(UUID uuid:state.getFamilyMap().values()){
93
            Taxon family = HibernateProxyHelper.deproxy(getTaxonService().load(uuid, true, Arrays.asList(new String[]{"*"})), Taxon.class);
94

    
95
            gesamtListe.addParentChild(null, family, null, null);
96
            checkliste.addParentChild(null, family, null, null);
97
            family.setSec(gesamtListe.getReference());
98
            family.setTitleCache(null);
99

    
100
            getClassificationService().saveOrUpdate(gesamtListe);
101
            getClassificationService().saveOrUpdate(checkliste);
102
        }
103
    }
104

    
105
    @Override
106
    public boolean doPartition(ResultSetPartitioner partitioner, RedListGefaesspflanzenImportState state) {
107
        ResultSet rs = partitioner.getResultSet();
108
        Classification gesamtListeClassification = getClassificationService().load(state.getConfig().getClassificationUuid());
109
        Classification checklistClassification = getClassificationService().load(RedListUtil.checkListClassificationUuid);
110
        Classification classificationE = getClassificationService().load(RedListUtil.uuidClassificationE);
111
        Classification classificationW = getClassificationService().load(RedListUtil.uuidClassificationW);
112
        Classification classificationK = getClassificationService().load(RedListUtil.uuidClassificationK);
113
        Classification classificationAW = getClassificationService().load(RedListUtil.uuidClassificationAW);
114
        Classification classificationAO = getClassificationService().load(RedListUtil.uuidClassificationAO);
115
        Classification classificationR = getClassificationService().load(RedListUtil.uuidClassificationR);
116
        Classification classificationO = getClassificationService().load(RedListUtil.uuidClassificationO);
117
        Classification classificationS = getClassificationService().load(RedListUtil.uuidClassificationS);
118
        try {
119
            while (rs.next()){
120
                makeSingleTaxonNode(state, rs, gesamtListeClassification, checklistClassification,
121
                        classificationE, classificationW, classificationK, classificationAW
122
                        , classificationAO, classificationR, classificationO, classificationS);
123

    
124
            }
125
        } catch (SQLException e) {
126
            e.printStackTrace();
127
        }
128

    
129
        logger.info("Update classification (1000 nodes)");
130
        getClassificationService().saveOrUpdate(gesamtListeClassification);
131
        getClassificationService().saveOrUpdate(checklistClassification);
132
        getClassificationService().saveOrUpdate(classificationE);
133
        getClassificationService().saveOrUpdate(classificationW);
134
        getClassificationService().saveOrUpdate(classificationK);
135
        getClassificationService().saveOrUpdate(classificationAW);
136
        getClassificationService().saveOrUpdate(classificationAO);
137
        getClassificationService().saveOrUpdate(classificationR);
138
        getClassificationService().saveOrUpdate(classificationO);
139
        getClassificationService().saveOrUpdate(classificationS);
140
        return true;
141
    }
142

    
143
    private void makeSingleTaxonNode(RedListGefaesspflanzenImportState state, ResultSet rs,
144
            Classification gesamtListeClassification, Classification checklistClassification,
145
            Classification classificationE, Classification classificationW, Classification classificationK,
146
            Classification classificationAW, Classification classificationAO, Classification classificationR,
147
            Classification classificationO, Classification classificationS)
148
            throws SQLException {
149
        long id = rs.getLong(RedListUtil.NAMNR);
150
        String parentId = String.valueOf(rs.getLong(RedListUtil.LOWER));
151
        String gueltString = rs.getString(RedListUtil.GUELT);
152
        String taxZusatzString = rs.getString(RedListUtil.TAX_ZUSATZ);
153
        String familieString = rs.getString(RedListUtil.FAMILIE);
154
        String clTaxonString = rs.getString(RedListUtil.CL_TAXON);
155

    
156
        String relationE = rs.getString(RedListUtil.E);
157
        String relationW = rs.getString(RedListUtil.W);
158
        String relationK = rs.getString(RedListUtil.K);
159
        String relationAW = rs.getString(RedListUtil.AW);
160
        String relationAO = rs.getString(RedListUtil.AO);
161
        String relationR = rs.getString(RedListUtil.R);
162
        String relationO = rs.getString(RedListUtil.O);
163
        String relationS = rs.getString(RedListUtil.S);
164

    
165
        //Gesamtliste
166
        TaxonBase<?> taxonBase = state.getRelatedObject(RedListUtil.TAXON_GESAMTLISTE_NAMESPACE, String.valueOf(id), TaxonBase.class);
167
        taxonBase.setSec(gesamtListeClassification.getReference());
168
        Taxon parent = state.getRelatedObject(RedListUtil.TAXON_GESAMTLISTE_NAMESPACE, parentId, Taxon.class);
169
        if(parent!=null && !parent.isInstanceOf(Taxon.class)){
170
            RedListUtil.logMessage(id, parent+" is no taxon but is a parent of "+taxonBase+" (Gesamtliste)", logger);
171
        }
172
        //add to family if no parent found
173
        if(parent==null){
174
            if(!taxonBase.isInstanceOf(Taxon.class)){
175
                RedListUtil.logMessage(id, taxonBase+" has no parent but is not a taxon.", logger);
176
            }
177
            else{
178
                Taxon family = (Taxon) state.getRelatedObject(RedListUtil.FAMILY_NAMESPACE_GESAMTLISTE, familieString);
179
                gesamtListeClassification.addParentChild(family, HibernateProxyHelper.deproxy(taxonBase, Taxon.class), null, null);
180
                //Buttler/Checklist taxon
181
                if(CdmUtils.isNotBlank(clTaxonString) && clTaxonString.equals(RedListUtil.CL_TAXON_B)){
182
                    checklistClassification.addParentChild(family, HibernateProxyHelper.deproxy(taxonBase, Taxon.class), null, null);
183
                    taxonBase.setSec(checklistClassification.getReference());
184
                }
185
                if(family.getTaxonNodes().isEmpty()){
186
                    gesamtListeClassification.addChildTaxon(family, null, null);
187
                    //do not add empty families to checklist classification
188
                    if(!getClassificationService().listChildNodesOfTaxon(family.getUuid(), RedListUtil.checkListClassificationUuid,
189
                            true, null, null, null).isEmpty()){
190
                        checklistClassification.addChildTaxon(family, null, null);
191
                    }
192
                }
193
            }
194
        }
195
        //add to higher taxon
196
        else{
197
            createParentChildNodes(gesamtListeClassification, id, gueltString, taxZusatzString, taxonBase, parent);
198
            //Buttler/Checklist taxon
199
            if(CdmUtils.isNotBlank(clTaxonString) && clTaxonString.equals(RedListUtil.CL_TAXON_B)){
200
                if(!checklistClassification.isTaxonInTree(parent)){
201
                    RedListUtil.logInfoMessage(id, parent+" is parent taxon but is not in checklist. Skipping child "+taxonBase, logger);
202
                }
203
                else{
204
                    if(taxonBase.isInstanceOf(Taxon.class)){
205
                        createParentChildNodes(checklistClassification, id, gueltString, taxZusatzString, taxonBase, parent);
206
                    }
207
                    else if(taxonBase.isInstanceOf(Synonym.class)){
208
                        //if it is a synonym it is already added to the accepted taxon
209
                        //so we just change the sec reference
210
                        taxonBase.setSec(checklistClassification.getReference());
211
                        taxonBase.setTitleCache(null);
212
                    }
213
                }
214

    
215
            }
216
        }
217
        taxonBase.setTitleCache(null, false);//refresh title cache
218

    
219
        //add taxa for concept relationships to E, W, K, AW, AO, R, O, S
220
        addTaxonToClassification(classificationE, RedListUtil.CLASSIFICATION_NAMESPACE_E, relationE, taxonBase, id, state);
221
        addTaxonToClassification(classificationW, RedListUtil.CLASSIFICATION_NAMESPACE_W, relationW, taxonBase, id, state);
222
        addTaxonToClassification(classificationK, RedListUtil.CLASSIFICATION_NAMESPACE_K, relationK, taxonBase, id, state);
223
        addTaxonToClassification(classificationAW, RedListUtil.CLASSIFICATION_NAMESPACE_AW, relationAW, taxonBase, id, state);
224
        addTaxonToClassification(classificationAO, RedListUtil.CLASSIFICATION_NAMESPACE_AO, relationAO, taxonBase, id, state);
225
        addTaxonToClassification(classificationR, RedListUtil.CLASSIFICATION_NAMESPACE_R, relationR, taxonBase, id, state);
226
        addTaxonToClassification(classificationO, RedListUtil.CLASSIFICATION_NAMESPACE_O, relationO, taxonBase, id, state);
227
        addTaxonToClassification(classificationS, RedListUtil.CLASSIFICATION_NAMESPACE_S, relationS, taxonBase, id, state);
228
    }
229

    
230
    private void addTaxonToClassification(Classification classification, String classificationNamespace, String relationString, final TaxonBase<?> gesamtListeTaxon, long id, RedListGefaesspflanzenImportState state){
231
        Taxon taxon = HibernateProxyHelper.deproxy(state.getRelatedObject(classificationNamespace, String.valueOf(id), TaxonBase.class), Taxon.class);
232
        //add concept relation to gesamtliste/checkliste
233
        if(taxon!=null && isNotBlank(relationString) && !relationString.equals(".")){
234
            //if the related concept in gesamtliste/checkliste is a synonym then we
235
            //create a relation to the accepted taxon
236
            Taxon acceptedGesamtListeTaxon = getAcceptedTaxon(gesamtListeTaxon);
237
            String relationSubstring = relationString.substring(relationString.length()-1, relationString.length());
238
            TaxonRelationshipType taxonRelationshipTypeByKey = new RedListGefaesspflanzenTransformer().getTaxonRelationshipTypeByKey(relationSubstring);
239
            if(taxonRelationshipTypeByKey==null){
240
                RedListUtil.logMessage(id, "Could not interpret relationship "+relationString+" for taxon "+gesamtListeTaxon.generateTitle(), logger);
241
            }
242
            //there is no type "included in" so we have to reverse the direction
243
            if(relationSubstring.equals("<")){
244
                if(acceptedGesamtListeTaxon!=null){
245
                    acceptedGesamtListeTaxon.addTaxonRelation(taxon, taxonRelationshipTypeByKey, null, null);
246
                }
247
            }
248
            else{
249
                if(acceptedGesamtListeTaxon!=null){
250
                    taxon.addTaxonRelation(acceptedGesamtListeTaxon, taxonRelationshipTypeByKey, null, null);
251
                }
252
            }
253

    
254
            taxon.setSec(classification.getReference());
255
            classification.addChildTaxon(taxon, null, null);
256
            taxon.setTitleCache(null);//Reset title cache to see sec ref in title
257
        }
258
    }
259

    
260
    private void createParentChildNodes(Classification classification, long id, String gueltString,
261
            String taxZusatzString, TaxonBase<?> taxonBase, Taxon parent) {
262
        if(taxonBase==null){
263
            RedListUtil.logMessage(id, "child taxon/synonym of "+parent+"  is null. ("+classification.generateTitle()+")" , logger);
264
            return;
265
        }
266
        //taxon
267
        if(taxonBase.isInstanceOf(Taxon.class)){
268
            //misapplied name
269
            String appendedPhrase = taxonBase.getAppendedPhrase();
270
            if(appendedPhrase!=null && appendedPhrase.equals(RedListUtil.AUCT)){
271
                if(parent==null){
272
                    RedListUtil.logMessage(id, "parent taxon of misapplied name "+taxonBase+"  is null. ("+classification.getTitleCache()+")" , logger);
273
                    return;
274
                }
275
                parent.addMisappliedName((Taxon) taxonBase, null, null);
276
                //return because misapplied names do not have sec references
277
                return;
278
            }
279
            else{
280
                classification.addParentChild(parent, (Taxon)taxonBase, null, null);
281
            }
282

    
283
            if(isNotBlank(taxZusatzString)){
284
                if(taxZusatzString.trim().equals("p. p.")){
285
                    RedListUtil.logMessage(id, "pro parte for accepted taxon "+taxonBase, logger);
286
                }
287
            }
288
        }
289
        //synonym
290
        else if(taxonBase.isInstanceOf(Synonym.class)){
291
            if(parent==null){
292
                RedListUtil.logMessage(id, "parent taxon of synonym "+taxonBase+"  is null. ("+classification.getTitleCache()+")" , logger);
293
                return;
294
            }
295
            //basionym
296
            if(gueltString.equals(RedListUtil.GUELT_BASIONYM)){
297
                parent.addHomotypicSynonym((Synonym) taxonBase);
298
                parent.getName().addBasionym(taxonBase.getName());
299
            }
300
            //regular synonym
301
            else{
302
                Synonym synonym = (Synonym) taxonBase;
303
                parent.addSynonym(synonym, SynonymType.HETEROTYPIC_SYNONYM_OF());
304

    
305
                //TAX_ZUSATZ
306
                if(isNotBlank(taxZusatzString)){
307
                    if(taxZusatzString.trim().equals("p. p.")){
308
                        logger.warn(id + ": p. p. not implemented anymore");
309
                    }
310
                    else if(taxZusatzString.trim().equals("s. l. p. p.")){
311
                        logger.warn(id + ": p. p. not implemented anymore");
312
                        taxonBase.setAppendedPhrase("s. l.");
313
                    }
314
                    else if(taxZusatzString.trim().equals("s. str. p. p.")){
315
                        logger.warn(id + ": p. p. not implemented anymore");
316
                        taxonBase.setAppendedPhrase("s. str.");
317
                    }
318
                    else if(taxZusatzString.trim().equals("s. l.")
319
                            || taxZusatzString.trim().equals("s. str.")){
320
                        taxonBase.setAppendedPhrase(taxZusatzString);
321
                    }
322
                    else{
323
                        RedListUtil.logMessage(id, "unknown value "+taxZusatzString+" for column "+RedListUtil.TAX_ZUSATZ, logger);
324
                    }
325
                }
326
            }
327
        }
328
        //set sec reference
329
        taxonBase.setSec(classification.getReference());
330
    }
331

    
332
    @Override
333
    public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
334
            RedListGefaesspflanzenImportState state) {
335
        Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
336

    
337
        Set<String> idSet = new HashSet<>();
338
        try {
339
            while (rs.next()){
340
                idSet.add(String.valueOf(rs.getLong(RedListUtil.NAMNR)));
341
                idSet.add(String.valueOf(rs.getLong(RedListUtil.LOWER)));
342
            }
343
        } catch (SQLException e) {
344
            e.printStackTrace();
345
        }
346
        //add taxa and their parent taxa
347
        result.put(RedListUtil.TAXON_GESAMTLISTE_NAMESPACE, getCommonService().getSourcedObjectsByIdInSourceC(TaxonBase.class, idSet, RedListUtil.TAXON_GESAMTLISTE_NAMESPACE));
348
        result.put(RedListUtil.CLASSIFICATION_NAMESPACE_E, getCommonService().getSourcedObjectsByIdInSourceC(TaxonBase.class, idSet, RedListUtil.CLASSIFICATION_NAMESPACE_E));
349
        result.put(RedListUtil.CLASSIFICATION_NAMESPACE_W, getCommonService().getSourcedObjectsByIdInSourceC(TaxonBase.class, idSet, RedListUtil.CLASSIFICATION_NAMESPACE_W));
350
        result.put(RedListUtil.CLASSIFICATION_NAMESPACE_K, getCommonService().getSourcedObjectsByIdInSourceC(TaxonBase.class, idSet, RedListUtil.CLASSIFICATION_NAMESPACE_K));
351
        result.put(RedListUtil.CLASSIFICATION_NAMESPACE_AW, getCommonService().getSourcedObjectsByIdInSourceC(TaxonBase.class, idSet, RedListUtil.CLASSIFICATION_NAMESPACE_AW));
352
        result.put(RedListUtil.CLASSIFICATION_NAMESPACE_AO, getCommonService().getSourcedObjectsByIdInSourceC(TaxonBase.class, idSet, RedListUtil.CLASSIFICATION_NAMESPACE_AO));
353
        result.put(RedListUtil.CLASSIFICATION_NAMESPACE_R, getCommonService().getSourcedObjectsByIdInSourceC(TaxonBase.class, idSet, RedListUtil.CLASSIFICATION_NAMESPACE_R));
354
        result.put(RedListUtil.CLASSIFICATION_NAMESPACE_O, getCommonService().getSourcedObjectsByIdInSourceC(TaxonBase.class, idSet, RedListUtil.CLASSIFICATION_NAMESPACE_O));
355
        result.put(RedListUtil.CLASSIFICATION_NAMESPACE_S, getCommonService().getSourcedObjectsByIdInSourceC(TaxonBase.class, idSet, RedListUtil.CLASSIFICATION_NAMESPACE_S));
356

    
357
        //add families
358
        //gesamtliste
359
        Map<String, Taxon> familyMapGL = new HashMap<String, Taxon>();
360
        for (Entry<String, UUID> entry: state.getFamilyMap().entrySet()) {
361
            familyMapGL.put(entry.getKey(), HibernateProxyHelper.deproxy(getTaxonService().load(entry.getValue()), Taxon.class));
362
        }
363
        result.put(RedListUtil.FAMILY_NAMESPACE_GESAMTLISTE, familyMapGL);
364
        return result;
365
    }
366

    
367
    private Classification makeClassification(String classificationName, UUID classificationUuid, String referenceName, Integer yearPublished, UUID referenceUuid, RedListGefaesspflanzenImportState state) {
368
        Classification classification = Classification.NewInstance(classificationName, Language.DEFAULT());
369
        classification.setUuid(classificationUuid);
370
        Reference reference = ReferenceFactory.newGeneric();
371
        reference.setTitle(referenceName);
372
        reference.setUuid(referenceUuid);
373
        classification.setReference(reference);
374
        if(yearPublished!=null){
375
            reference.setDatePublished(VerbatimTimePeriod.NewVerbatimInstance(yearPublished));
376
        }
377
        return getClassificationService().save(classification);
378
    }
379

    
380
    @Override
381
    protected boolean doCheck(RedListGefaesspflanzenImportState state) {
382
        return false;
383
    }
384

    
385
    @Override
386
    protected boolean isIgnore(RedListGefaesspflanzenImportState state) {
387
        return false;
388
    }
389

    
390
}
(2-2/9)