Project

General

Profile

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

    
215
        if(taxon!=null){//not all taxa exist in these classifications
216
            taxon.setSec(classification.getReference());
217
            taxon.setTitleCache(null);//Reset title cache to see sec ref in title
218
            classification.addChildTaxon(taxon, null, null);
219
        }
220
    }
221

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

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

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

    
292
    @Override
293
    public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
294
            RedListGefaesspflanzenImportState state) {
295
        Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
296

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

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

    
332
    @Override
333
    protected boolean doCheck(RedListGefaesspflanzenImportState state) {
334
        return false;
335
    }
336

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

    
342
}
(2-2/7)