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
@Component
47
@SuppressWarnings("serial")
48
public class RedListGefaesspflanzenImportClassification extends DbImportBase<RedListGefaesspflanzenImportState, RedListGefaesspflanzenImportConfigurator> {
49

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

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

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

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

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

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

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

    
92

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

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

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

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

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

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

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

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

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

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

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

    
232

    
233

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

    
258
            taxon.setSec(classification.getReference());
259
            classification.addChildTaxon(taxon, null, null);
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
                //return because misapplied names do not have sec references
281
                return;
282
            }
283
            else{
284
                classification.addParentChild(parent, (Taxon)taxonBase, null, null);
285
            }
286

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

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

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

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

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

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

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

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

    
394
}
(2-2/9)