Project

General

Profile

Download (21.4 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

    
10
package eu.etaxonomy.cdm.io.redlist.gefaesspflanzen;
11

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

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

    
23
import eu.etaxonomy.cdm.common.CdmUtils;
24
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
25
import eu.etaxonomy.cdm.io.common.DbImportBase;
26
import eu.etaxonomy.cdm.io.common.IPartitionedIO;
27
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
28
import eu.etaxonomy.cdm.model.common.CdmBase;
29
import eu.etaxonomy.cdm.model.common.Language;
30
import eu.etaxonomy.cdm.model.common.TimePeriod;
31
import eu.etaxonomy.cdm.model.reference.Reference;
32
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
33
import eu.etaxonomy.cdm.model.taxon.Classification;
34
import eu.etaxonomy.cdm.model.taxon.Synonym;
35
import eu.etaxonomy.cdm.model.taxon.SynonymRelationship;
36
import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
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
 *
43
 * @author pplitzner
44
 * @date Mar 1, 2016
45
 *
46
 */
47

    
48
@Component
49
@SuppressWarnings("serial")
50
public class RedListGefaesspflanzenImportClassification extends DbImportBase<RedListGefaesspflanzenImportState, RedListGefaesspflanzenImportConfigurator> {
51

    
52
    private static final Logger logger = Logger.getLogger(RedListGefaesspflanzenImportClassification.class);
53

    
54
    private static final String tableName = "Rote Liste Gefäßpflanzen";
55

    
56
    private static final String pluralString = "classifications";
57

    
58
    public RedListGefaesspflanzenImportClassification() {
59
        super(tableName, pluralString);
60
    }
61

    
62
    @Override
63
    protected String getIdQuery(RedListGefaesspflanzenImportState state) {
64
        return "SELECT NAMNR "
65
                + "FROM V_TAXATLAS_D20_EXPORT t "
66
                + " ORDER BY NAMNR";
67
    }
68

    
69
    @Override
70
    protected String getRecordQuery(RedListGefaesspflanzenImportConfigurator config) {
71
        String result = "select e.*, f.FAMILIE "
72
                + "from V_TAXATLAS_D20_EXPORT e, V_TAXATLAS_D20_FAMILIEN f "
73
                + "where e.NAMNR = f.NAMNR and e.NAMNR IN (@IDSET)";
74
        result = result.replace("@IDSET", IPartitionedIO.ID_LIST_TOKEN);
75
        return result;
76
    }
77

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

    
94

    
95
    private void importFamilies(Classification gesamtListe, Classification checkliste,
96
            RedListGefaesspflanzenImportState state) {
97
        for(UUID uuid:state.getFamilyMap().values()){
98
            Taxon familyGL = HibernateProxyHelper.deproxy(getTaxonService().load(uuid), Taxon.class);
99
            Taxon familyCL = (Taxon) familyGL.clone();
100

    
101
            gesamtListe.addChildTaxon(familyGL, null, null);
102
            familyGL.setSec(gesamtListe.getReference());
103

    
104
            checkliste.addChildTaxon(familyCL, null, null);
105
            familyCL.setSec(checkliste.getReference());
106
        }
107
    }
108

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

    
128
            }
129
        } catch (SQLException e) {
130
            e.printStackTrace();
131
        }
132

    
133
        logger.info("Update classification (1000 nodes)");
134
        getClassificationService().saveOrUpdate(gesamtListeClassification);
135
        getClassificationService().saveOrUpdate(checklistClassification);
136
        return true;
137
    }
138

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

    
151
        String relationE = rs.getString(RedListUtil.E);
152
        String relationW = rs.getString(RedListUtil.W);
153
        String relationK = rs.getString(RedListUtil.K);
154
        String relationAW = rs.getString(RedListUtil.AW);
155
        String relationAO = rs.getString(RedListUtil.AO);
156
        String relationR = rs.getString(RedListUtil.R);
157
        String relationO = rs.getString(RedListUtil.O);
158
        String relationS = rs.getString(RedListUtil.S);
159

    
160
        //Gesamtliste
161
        TaxonBase<?> taxonBaseGL = state.getRelatedObject(RedListUtil.TAXON_GESAMTLISTE_NAMESPACE, String.valueOf(id), TaxonBase.class);
162
        TaxonBase<?> parentBaseGL = state.getRelatedObject(RedListUtil.TAXON_GESAMTLISTE_NAMESPACE, parentId, TaxonBase.class);
163
        if(parentBaseGL!=null && !parentBaseGL.isInstanceOf(Taxon.class)){
164
            RedListUtil.logMessage(id, parentBaseGL+" is no taxon but is a parent of "+taxonBaseGL+" (Gesamtliste)", logger);
165
        }
166
        Taxon parentGL = (Taxon) state.getRelatedObject(RedListUtil.TAXON_GESAMTLISTE_NAMESPACE, parentId, TaxonBase.class);
167
        //add to family if no parent found
168
        if(parentGL==null){
169
            if(taxonBaseGL.isInstanceOf(Taxon.class)){
170
                Taxon family = HibernateProxyHelper.deproxy(getTaxonService().load(state.getFamilyMap().get(familieString)), Taxon.class);
171
                gesamtListeClassification.addParentChild(family, HibernateProxyHelper.deproxy(taxonBaseGL, Taxon.class), null, null);
172
            }
173
            else{
174
                RedListUtil.logMessage(id, taxonBaseGL+" has no parent but is not a taxon.", logger);
175
            }
176
        }
177
        else{
178
            createParentChildNodes(gesamtListeClassification, id, gueltString, taxZusatzString, taxonBaseGL, parentGL);
179
        }
180

    
181
        //Checkliste
182
        TaxonBase<?> taxonBaseCL = state.getRelatedObject(RedListUtil.TAXON_CHECKLISTE_NAMESPACE, String.valueOf(id), TaxonBase.class);
183
        TaxonBase<?> parentBaseCL = state.getRelatedObject(RedListUtil.TAXON_CHECKLISTE_NAMESPACE, parentId, TaxonBase.class);
184
        if(parentBaseCL!=null && !parentBaseCL.isInstanceOf(Taxon.class)){
185
            RedListUtil.logMessage(id, parentBaseCL+" is no taxon but is a parent of "+taxonBaseCL+" (Checkliste)", logger);
186
        }
187
        Taxon parentCL = (Taxon) state.getRelatedObject(RedListUtil.TAXON_CHECKLISTE_NAMESPACE, parentId, TaxonBase.class);
188
        //add to family if no parent found
189
        if(parentCL==null){
190

    
191
        }
192
        else{
193
            if(taxonBaseCL!=null){//null check necessary because not all taxa exist in the checklist
194
                createParentChildNodes(checklistClassification, id, gueltString, taxZusatzString, taxonBaseCL, parentCL);
195
            }
196
        }
197

    
198
        //check uuids
199
        if(taxonBaseGL!= null && taxonBaseCL!=null
200
                && taxonBaseGL.getUuid().equals(taxonBaseCL.getUuid())){
201
            RedListUtil.logMessage(id, "Same UUID for "+taxonBaseGL+ " (Gesamtliste) and "+taxonBaseCL+" (Checkliste)", logger);
202
        }
203
        if(parentGL!=null && parentCL!=null && parentGL.getUuid().equals(parentCL.getUuid())){
204
            RedListUtil.logMessage(id, "Same UUID for "+parentGL+ " (Gesamtliste) and "+parentCL+" (Checkliste)", logger);
205
        }
206

    
207
        //add taxa for concept relationships to E, W, K, AW, AO, R, O, S
208
        addTaxonToClassification(classificationE, RedListUtil.CLASSIFICATION_NAMESPACE_E, relationE, taxonBaseGL, taxonBaseCL, id, state);
209
        addTaxonToClassification(classificationW, RedListUtil.CLASSIFICATION_NAMESPACE_W, relationW, taxonBaseGL, taxonBaseCL, id, state);
210
        addTaxonToClassification(classificationK, RedListUtil.CLASSIFICATION_NAMESPACE_K, relationK, taxonBaseGL, taxonBaseCL, id, state);
211
        addTaxonToClassification(classificationAW, RedListUtil.CLASSIFICATION_NAMESPACE_AW, relationAW, taxonBaseGL, taxonBaseCL, id, state);
212
        addTaxonToClassification(classificationAO, RedListUtil.CLASSIFICATION_NAMESPACE_AO, relationAO, taxonBaseGL, taxonBaseCL, id, state);
213
        addTaxonToClassification(classificationR, RedListUtil.CLASSIFICATION_NAMESPACE_R, relationR, taxonBaseGL, taxonBaseCL, id, state);
214
        addTaxonToClassification(classificationO, RedListUtil.CLASSIFICATION_NAMESPACE_O, relationO, taxonBaseGL, taxonBaseCL, id, state);
215
        addTaxonToClassification(classificationS, RedListUtil.CLASSIFICATION_NAMESPACE_S, relationS, taxonBaseGL, taxonBaseCL, id, state);
216
    }
217

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

    
249
            taxon.setSec(classification.getReference());
250
            classification.addChildTaxon(taxon, null, null);
251
            //TODO is saving and setting the title cache to null neccessary?
252
            getTaxonService().saveOrUpdate(taxon);
253
            getTaxonService().saveOrUpdate(gesamtListeTaxon);
254
            if(checklisteTaxon!=null){
255
                getTaxonService().saveOrUpdate(checklisteTaxon);
256
            }
257
            taxon.setTitleCache(null);//Reset title cache to see sec ref in title
258
        }
259
    }
260

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

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

    
303
                //TAX_ZUSATZ
304
                if(CdmUtils.isNotBlank(taxZusatzString)){
305
                    if(taxZusatzString.trim().equals("p. p.")){
306
                        synonymRelationship.setProParte(true);
307
                    }
308
                    else if(taxZusatzString.trim().equals("s. l. p. p.")){
309
                        synonymRelationship.setProParte(true);
310
                        taxonBase.setAppendedPhrase("s. l.");
311
                    }
312
                    else if(taxZusatzString.trim().equals("s. str. p. p.")){
313
                        synonymRelationship.setProParte(true);
314
                        taxonBase.setAppendedPhrase("s. str.");
315
                    }
316
                    else if(taxZusatzString.trim().equals("s. l.")
317
                            || taxZusatzString.trim().equals("s. str.")){
318
                        taxonBase.setAppendedPhrase(taxZusatzString);
319
                    }
320
                    else{
321
                        RedListUtil.logMessage(id, "unknown value "+taxZusatzString+" for column "+RedListUtil.TAX_ZUSATZ, logger);
322
                    }
323
                }
324
            }
325
        }
326
        //set sec reference
327
        taxonBase.setSec(classification.getReference());
328
        taxonBase.setTitleCache(null, false);//refresh title cache
329
    }
330

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

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

    
358
    private Classification makeClassification(String classificationName, UUID classificationUuid, String referenceName, Integer yearPublished, UUID referenceUuid, RedListGefaesspflanzenImportState state) {
359
        Classification classification = Classification.NewInstance(classificationName, Language.DEFAULT());
360
        classification.setUuid(classificationUuid);
361
        Reference reference = ReferenceFactory.newGeneric();
362
        reference.setTitle(referenceName);
363
        reference.setUuid(referenceUuid);
364
        classification.setReference(reference);
365
        if(yearPublished!=null){
366
            reference.setDatePublished(TimePeriod.NewInstance(yearPublished));
367
        }
368
        getClassificationService().save(classification);
369
        return classification;
370
    }
371

    
372
    @Override
373
    protected boolean doCheck(RedListGefaesspflanzenImportState state) {
374
        return false;
375
    }
376

    
377
    @Override
378
    protected boolean isIgnore(RedListGefaesspflanzenImportState state) {
379
        return false;
380
    }
381

    
382
}
(2-2/8)