Project

General

Profile

Download (21.5 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.Arrays;
15
import java.util.HashMap;
16
import java.util.HashSet;
17
import java.util.Map;
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.TimePeriod;
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.SynonymRelationship;
37
import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
38
import eu.etaxonomy.cdm.model.taxon.Taxon;
39
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
40
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
41

    
42
/**
43
 *
44
 * @author pplitzner
45
 * @date Mar 1, 2016
46
 *
47
 */
48

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

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

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

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

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

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

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

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

    
95

    
96
    private void importFamilies(Classification gesamtListe, Classification checkliste,
97
            RedListGefaesspflanzenImportState state) {
98
        for(UUID uuid:state.getFamilyMap().values()){
99
            Taxon familyGL = HibernateProxyHelper.deproxy(getTaxonService().load(uuid, Arrays.asList(new String[]{"*"})), Taxon.class);
100
            Taxon familyCL = (Taxon) familyGL.clone();
101

    
102
            gesamtListe.addChildTaxon(familyGL, null, null);
103
            familyGL.setSec(gesamtListe.getReference());
104
            familyGL.setTitleCache(null);
105

    
106
            checkliste.addChildTaxon(familyCL, null, null);
107
            familyCL.setSec(checkliste.getReference());
108
            familyCL.setTitleCache(null);
109
        }
110
    }
111

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

    
131
            }
132
        } catch (SQLException e) {
133
            e.printStackTrace();
134
        }
135

    
136
        logger.info("Update classification (1000 nodes)");
137
        getClassificationService().saveOrUpdate(gesamtListeClassification);
138
        getClassificationService().saveOrUpdate(checklistClassification);
139
        return true;
140
    }
141

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
375
    @Override
376
    protected boolean doCheck(RedListGefaesspflanzenImportState state) {
377
        return false;
378
    }
379

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

    
385
}
(2-2/8)