Project

General

Profile

Download (19.8 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 * "
72
                + " FROM V_TAXATLAS_D20_EXPORT t "
73
                + " WHERE t.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
        makeClassification("Gesamtliste", state.getConfig().getClassificationUuid(), "Gesamtliste", null, RedListUtil.gesamtListeReferenceUuid, state);
81
        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
        super.doInvoke(state);
91
    }
92

    
93

    
94
    @Override
95
    public boolean doPartition(ResultSetPartitioner partitioner, RedListGefaesspflanzenImportState state) {
96
        ResultSet rs = partitioner.getResultSet();
97
        Classification gesamtListeClassification = getClassificationService().load(state.getConfig().getClassificationUuid());
98
        Classification checklistClassification = getClassificationService().load(RedListUtil.checkListClassificationUuid);
99
        Classification classificationE = getClassificationService().load(RedListUtil.uuidClassificationE);
100
        Classification classificationW = getClassificationService().load(RedListUtil.uuidClassificationW);
101
        Classification classificationK = getClassificationService().load(RedListUtil.uuidClassificationK);
102
        Classification classificationAW = getClassificationService().load(RedListUtil.uuidClassificationAW);
103
        Classification classificationAO = getClassificationService().load(RedListUtil.uuidClassificationAO);
104
        Classification classificationR = getClassificationService().load(RedListUtil.uuidClassificationR);
105
        Classification classificationO = getClassificationService().load(RedListUtil.uuidClassificationO);
106
        Classification classificationS = getClassificationService().load(RedListUtil.uuidClassificationS);
107
        try {
108
            while (rs.next()){
109
                makeSingleTaxonNode(state, rs, gesamtListeClassification, checklistClassification,
110
                        classificationE, classificationW, classificationK, classificationAW
111
                        , classificationAO, classificationR, classificationO, classificationS);
112

    
113
            }
114
        } catch (SQLException e) {
115
            e.printStackTrace();
116
        }
117

    
118
        logger.info("Update classification (1000 nodes)");
119
        getClassificationService().saveOrUpdate(gesamtListeClassification);
120
        getClassificationService().saveOrUpdate(checklistClassification);
121
        return true;
122
    }
123

    
124
    private void makeSingleTaxonNode(RedListGefaesspflanzenImportState state, ResultSet rs,
125
            Classification gesamtListeClassification, Classification checklistClassification,
126
            Classification classificationE, Classification classificationW, Classification classificationK,
127
            Classification classificationAW, Classification classificationAO, Classification classificationR,
128
            Classification classificationO, Classification classificationS)
129
            throws SQLException {
130
        long id = rs.getLong(RedListUtil.NAMNR);
131
        String parentId = String.valueOf(rs.getLong(RedListUtil.LOWER));
132
        String gueltString = rs.getString(RedListUtil.GUELT);
133
        String taxZusatzString = rs.getString(RedListUtil.TAX_ZUSATZ);
134

    
135
        String relationE = rs.getString(RedListUtil.E);
136
        String relationW = rs.getString(RedListUtil.W);
137
        String relationK = rs.getString(RedListUtil.K);
138
        String relationAW = rs.getString(RedListUtil.AW);
139
        String relationAO = rs.getString(RedListUtil.AO);
140
        String relationR = rs.getString(RedListUtil.R);
141
        String relationO = rs.getString(RedListUtil.O);
142
        String relationS = rs.getString(RedListUtil.S);
143

    
144
        //Gesamtliste
145
        TaxonBase<?> taxonBaseGL = state.getRelatedObject(RedListUtil.TAXON_GESAMTLISTE_NAMESPACE, String.valueOf(id), TaxonBase.class);
146
        TaxonBase<?> parentBaseGL = state.getRelatedObject(RedListUtil.TAXON_GESAMTLISTE_NAMESPACE, parentId, TaxonBase.class);
147
        if(parentBaseGL!=null && !parentBaseGL.isInstanceOf(Taxon.class)){
148
            RedListUtil.logMessage(id, parentBaseGL+" is no taxon but is a parent of "+taxonBaseGL+" (Gesamtliste)", logger);
149
        }
150
        Taxon parentGL = (Taxon) state.getRelatedObject(RedListUtil.TAXON_GESAMTLISTE_NAMESPACE, parentId, TaxonBase.class);
151
        createParentChildNodes(gesamtListeClassification, id, gueltString, taxZusatzString, taxonBaseGL, parentGL);
152

    
153
        //Checkliste
154
        TaxonBase<?> taxonBaseCL = state.getRelatedObject(RedListUtil.TAXON_CHECKLISTE_NAMESPACE, String.valueOf(id), TaxonBase.class);
155
        TaxonBase<?> parentBaseCL = state.getRelatedObject(RedListUtil.TAXON_CHECKLISTE_NAMESPACE, parentId, TaxonBase.class);
156
        if(parentBaseCL!=null && !parentBaseCL.isInstanceOf(Taxon.class)){
157
            RedListUtil.logMessage(id, parentBaseCL+" is no taxon but is a parent of "+taxonBaseCL+" (Checkliste)", logger);
158
        }
159
        Taxon parentCL = (Taxon) state.getRelatedObject(RedListUtil.TAXON_CHECKLISTE_NAMESPACE, parentId, TaxonBase.class);
160
        if(taxonBaseCL!=null){//null check necessary because not all taxa exist in the checklist
161
            createParentChildNodes(checklistClassification, id, gueltString, taxZusatzString, taxonBaseCL, parentCL);
162
        }
163

    
164
        if(taxonBaseGL!= null && taxonBaseCL!=null
165
                && taxonBaseGL.getUuid().equals(taxonBaseCL.getUuid())){
166
            RedListUtil.logMessage(id, "Same UUID for "+taxonBaseGL+ " (Gesamtliste) and "+taxonBaseCL+" (Checkliste)", logger);
167
        }
168
        if(parentGL!=null && parentCL!=null && parentGL.getUuid().equals(parentCL.getUuid())){
169
            RedListUtil.logMessage(id, "Same UUID for "+parentGL+ " (Gesamtliste) and "+parentCL+" (Checkliste)", logger);
170
        }
171

    
172
        //add taxa for concept relationships to E, W, K, AW, AO, R, O, S
173
        addTaxonToClassification(classificationE, RedListUtil.CLASSIFICATION_NAMESPACE_E, relationE, taxonBaseGL, taxonBaseCL, id, state);
174
        addTaxonToClassification(classificationW, RedListUtil.CLASSIFICATION_NAMESPACE_W, relationW, taxonBaseGL, taxonBaseCL, id, state);
175
        addTaxonToClassification(classificationK, RedListUtil.CLASSIFICATION_NAMESPACE_K, relationK, taxonBaseGL, taxonBaseCL, id, state);
176
        addTaxonToClassification(classificationAW, RedListUtil.CLASSIFICATION_NAMESPACE_AW, relationAW, taxonBaseGL, taxonBaseCL, id, state);
177
        addTaxonToClassification(classificationAO, RedListUtil.CLASSIFICATION_NAMESPACE_AO, relationAO, taxonBaseGL, taxonBaseCL, id, state);
178
        addTaxonToClassification(classificationR, RedListUtil.CLASSIFICATION_NAMESPACE_R, relationR, taxonBaseGL, taxonBaseCL, id, state);
179
        addTaxonToClassification(classificationO, RedListUtil.CLASSIFICATION_NAMESPACE_O, relationO, taxonBaseGL, taxonBaseCL, id, state);
180
        addTaxonToClassification(classificationS, RedListUtil.CLASSIFICATION_NAMESPACE_S, relationS, taxonBaseGL, taxonBaseCL, id, state);
181
    }
182

    
183
    private void addTaxonToClassification(Classification classification, String classificationNamespace, String relationString, final TaxonBase<?> gesamtListeTaxon, final TaxonBase<?> checklisteTaxon, long id, RedListGefaesspflanzenImportState state){
184
        Taxon taxon = HibernateProxyHelper.deproxy(state.getRelatedObject(classificationNamespace, String.valueOf(id), TaxonBase.class), Taxon.class);
185
        //add concept relation to gesamtliste and checkliste
186
        if(taxon!=null && CdmUtils.isNotBlank(relationString) && !relationString.equals(".")){
187
            //if the related concept in gesamtliste/checkliste is a synonym then we
188
            //create a relation to the accepted taxon
189
            Taxon acceptedGesamtListeTaxon = getAcceptedTaxon(gesamtListeTaxon);
190
            Taxon acceptedChecklistTaxon = getAcceptedTaxon(checklisteTaxon);
191
            String relationSubstring = relationString.substring(relationString.length()-1, relationString.length());
192
            TaxonRelationshipType taxonRelationshipTypeByKey = new RedListGefaesspflanzenTransformer().getTaxonRelationshipTypeByKey(relationSubstring);
193
            if(taxonRelationshipTypeByKey==null){
194
                RedListUtil.logMessage(id, "Could not interpret relationship "+relationString+" for taxon "+gesamtListeTaxon.generateTitle(), logger);
195
            }
196
            //there is no type "included in" so we have to reverse the direction
197
            if(relationSubstring.equals("<")){
198
                if(acceptedGesamtListeTaxon!=null){
199
                    acceptedGesamtListeTaxon.addTaxonRelation(taxon, taxonRelationshipTypeByKey, null, null);
200
                }
201
                if(acceptedChecklistTaxon!=null) {
202
                    acceptedChecklistTaxon.addTaxonRelation(taxon, taxonRelationshipTypeByKey, null, null);
203
                }
204
            }
205
            else{
206
                if(acceptedGesamtListeTaxon!=null){
207
                    taxon.addTaxonRelation(acceptedGesamtListeTaxon, taxonRelationshipTypeByKey, null, null);
208
                }
209
                if(acceptedChecklistTaxon!=null) {
210
                    taxon.addTaxonRelation(acceptedChecklistTaxon, taxonRelationshipTypeByKey, null, null);
211
                }
212
            }
213

    
214
            taxon.setSec(classification.getReference());
215
            classification.addChildTaxon(taxon, null, null);
216
            //TODO is saving and setting the title cache to null neccessary?
217
            getTaxonService().saveOrUpdate(taxon);
218
            getTaxonService().saveOrUpdate(gesamtListeTaxon);
219
            if(checklisteTaxon!=null){
220
                getTaxonService().saveOrUpdate(checklisteTaxon);
221
            }
222
            taxon.setTitleCache(null);//Reset title cache to see sec ref in title
223
        }
224
    }
225

    
226
    private void createParentChildNodes(Classification classification, long id, String gueltString,
227
            String taxZusatzString, TaxonBase<?> taxonBase, Taxon parent) {
228
        if(taxonBase==null){
229
            RedListUtil.logMessage(id, "child taxon/synonym of "+parent+"  is null. ("+classification.generateTitle()+")" , logger);
230
            return;
231
        }
232
        //taxon
233
        if(taxonBase.isInstanceOf(Taxon.class)){
234
            //misapplied name
235
            String appendedPhrase = taxonBase.getAppendedPhrase();
236
            if(appendedPhrase!=null && appendedPhrase.equals(RedListUtil.AUCT)){
237
                if(parent==null){
238
                    RedListUtil.logMessage(id, "parent taxon of misapplied name "+taxonBase+"  is null. ("+classification.getTitleCache()+")" , logger);
239
                    return;
240
                }
241
                parent.addMisappliedName((Taxon) taxonBase, null, null);
242
            }
243
            else{
244
                classification.addParentChild(parent, (Taxon)taxonBase, null, null);
245
            }
246

    
247
            if(CdmUtils.isNotBlank(taxZusatzString)){
248
                if(taxZusatzString.trim().equals("p. p.")){
249
                    RedListUtil.logMessage(id, "pro parte for accepted taxon "+taxonBase, logger);
250
                }
251
            }
252
        }
253
        //synonym
254
        else if(taxonBase.isInstanceOf(Synonym.class)){
255
            if(parent==null){
256
                RedListUtil.logMessage(id, "parent taxon of synonym "+taxonBase+"  is null. ("+classification.getTitleCache()+")" , logger);
257
                return;
258
            }
259
            //basionym
260
            if(gueltString.equals(RedListUtil.GUELT_BASIONYM)){
261
                parent.addHomotypicSynonym((Synonym) taxonBase, null, null);
262
                parent.getName().addBasionym(taxonBase.getName());
263
            }
264
            //regular synonym
265
            else{
266
                SynonymRelationship synonymRelationship = parent.addSynonym((Synonym) taxonBase, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF(), null, null);
267

    
268
                //TAX_ZUSATZ
269
                if(CdmUtils.isNotBlank(taxZusatzString)){
270
                    if(taxZusatzString.trim().equals("p. p.")){
271
                        synonymRelationship.setProParte(true);
272
                    }
273
                    else if(taxZusatzString.trim().equals("s. l. p. p.")){
274
                        synonymRelationship.setProParte(true);
275
                        taxonBase.setAppendedPhrase("s. l.");
276
                    }
277
                    else if(taxZusatzString.trim().equals("s. str. p. p.")){
278
                        synonymRelationship.setProParte(true);
279
                        taxonBase.setAppendedPhrase("s. str.");
280
                    }
281
                    else if(taxZusatzString.trim().equals("s. l.")
282
                            || taxZusatzString.trim().equals("s. str.")){
283
                        taxonBase.setAppendedPhrase(taxZusatzString);
284
                    }
285
                    else{
286
                        RedListUtil.logMessage(id, "unknown value "+taxZusatzString+" for column "+RedListUtil.TAX_ZUSATZ, logger);
287
                    }
288
                }
289
            }
290
        }
291
        //set sec reference
292
        taxonBase.setSec(classification.getReference());
293
        taxonBase.setTitleCache(null, false);//refresh title cache
294
    }
295

    
296
    @Override
297
    public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
298
            RedListGefaesspflanzenImportState state) {
299
        Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
300

    
301
        Set<String> idSet = new HashSet<String>();
302
        try {
303
            while (rs.next()){
304
                idSet.add(String.valueOf(rs.getLong(RedListUtil.NAMNR)));
305
                idSet.add(String.valueOf(rs.getLong(RedListUtil.LOWER)));
306
            }
307
        } catch (SQLException e) {
308
            e.printStackTrace();
309
        }
310
        result.put(RedListUtil.TAXON_GESAMTLISTE_NAMESPACE, (Map<String, TaxonBase>) getCommonService().getSourcedObjectsByIdInSource(TaxonBase.class, idSet, RedListUtil.TAXON_GESAMTLISTE_NAMESPACE));
311
        result.put(RedListUtil.TAXON_CHECKLISTE_NAMESPACE, (Map<String, TaxonBase>) getCommonService().getSourcedObjectsByIdInSource(TaxonBase.class, idSet, RedListUtil.TAXON_CHECKLISTE_NAMESPACE));
312
        result.put(RedListUtil.CLASSIFICATION_NAMESPACE_E, (Map<String, TaxonBase>) getCommonService().getSourcedObjectsByIdInSource(TaxonBase.class, idSet, RedListUtil.CLASSIFICATION_NAMESPACE_E));
313
        result.put(RedListUtil.CLASSIFICATION_NAMESPACE_W, (Map<String, TaxonBase>) getCommonService().getSourcedObjectsByIdInSource(TaxonBase.class, idSet, RedListUtil.CLASSIFICATION_NAMESPACE_W));
314
        result.put(RedListUtil.CLASSIFICATION_NAMESPACE_K, (Map<String, TaxonBase>) getCommonService().getSourcedObjectsByIdInSource(TaxonBase.class, idSet, RedListUtil.CLASSIFICATION_NAMESPACE_K));
315
        result.put(RedListUtil.CLASSIFICATION_NAMESPACE_AW, (Map<String, TaxonBase>) getCommonService().getSourcedObjectsByIdInSource(TaxonBase.class, idSet, RedListUtil.CLASSIFICATION_NAMESPACE_AW));
316
        result.put(RedListUtil.CLASSIFICATION_NAMESPACE_AO, (Map<String, TaxonBase>) getCommonService().getSourcedObjectsByIdInSource(TaxonBase.class, idSet, RedListUtil.CLASSIFICATION_NAMESPACE_AO));
317
        result.put(RedListUtil.CLASSIFICATION_NAMESPACE_R, (Map<String, TaxonBase>) getCommonService().getSourcedObjectsByIdInSource(TaxonBase.class, idSet, RedListUtil.CLASSIFICATION_NAMESPACE_R));
318
        result.put(RedListUtil.CLASSIFICATION_NAMESPACE_O, (Map<String, TaxonBase>) getCommonService().getSourcedObjectsByIdInSource(TaxonBase.class, idSet, RedListUtil.CLASSIFICATION_NAMESPACE_O));
319
        result.put(RedListUtil.CLASSIFICATION_NAMESPACE_S, (Map<String, TaxonBase>) getCommonService().getSourcedObjectsByIdInSource(TaxonBase.class, idSet, RedListUtil.CLASSIFICATION_NAMESPACE_S));
320
        return result;
321
    }
322

    
323
    private void makeClassification(String classificationName, UUID classificationUuid, String referenceName, Integer yearPublished, UUID referenceUuid, RedListGefaesspflanzenImportState state) {
324
        Classification classification = Classification.NewInstance(classificationName, Language.DEFAULT());
325
        classification.setUuid(classificationUuid);
326
        Reference reference = ReferenceFactory.newGeneric();
327
        reference.setTitle(referenceName);
328
        reference.setUuid(referenceUuid);
329
        classification.setReference(reference);
330
        if(yearPublished!=null){
331
            reference.setDatePublished(TimePeriod.NewInstance(yearPublished));
332
        }
333
        getClassificationService().save(classification);
334
    }
335

    
336
    @Override
337
    protected boolean doCheck(RedListGefaesspflanzenImportState state) {
338
        return false;
339
    }
340

    
341
    @Override
342
    protected boolean isIgnore(RedListGefaesspflanzenImportState state) {
343
        return false;
344
    }
345

    
346
}
(2-2/7)