Project

General

Profile

Download (21.9 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.Map.Entry;
19
import java.util.Set;
20
import java.util.UUID;
21

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

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

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

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

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

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

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

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

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

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

    
93

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

    
98
            gesamtListe.addParentChild(null, family, null, null);
99
            checkliste.addParentChild(null, family, null, null);
100
            family.setSec(gesamtListe.getReference());
101
            family.setTitleCache(null);
102

    
103
            getClassificationService().saveOrUpdate(gesamtListe);
104
            getClassificationService().saveOrUpdate(checkliste);
105
        }
106
    }
107

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

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

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

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

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

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

    
218
            }
219
        }
220
        taxonBase.setTitleCache(null, false);//refresh title cache
221

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

    
233

    
234

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

    
259
            taxon.setSec(classification.getReference());
260
            classification.addChildTaxon(taxon, null, null);
261
            taxon.setTitleCache(null);//Reset title cache to see sec ref in title
262
        }
263
    }
264

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

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

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

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

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

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

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

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

    
390
    @Override
391
    protected boolean isIgnore(RedListGefaesspflanzenImportState state) {
392
        return false;
393
    }
394

    
395
}
(2-2/9)