Project

General

Profile

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

    
19
import org.apache.log4j.Logger;
20
import org.springframework.stereotype.Component;
21

    
22
import eu.etaxonomy.cdm.common.CdmUtils;
23
import eu.etaxonomy.cdm.io.common.DbImportBase;
24
import eu.etaxonomy.cdm.io.common.IPartitionedIO;
25
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
26
import eu.etaxonomy.cdm.model.common.CdmBase;
27
import eu.etaxonomy.cdm.model.taxon.Classification;
28
import eu.etaxonomy.cdm.model.taxon.Synonym;
29
import eu.etaxonomy.cdm.model.taxon.SynonymRelationship;
30
import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
31
import eu.etaxonomy.cdm.model.taxon.Taxon;
32
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
33

    
34
/**
35
 *
36
 * @author pplitzner
37
 * @date Mar 1, 2016
38
 *
39
 */
40

    
41
@Component
42
@SuppressWarnings("serial")
43
public class RedListGefaesspflanzenImportClassification extends DbImportBase<RedListGefaesspflanzenImportState, RedListGefaesspflanzenImportConfigurator> {
44

    
45
    private static final Logger logger = Logger.getLogger(RedListGefaesspflanzenImportClassification.class);
46

    
47
    private static final String tableName = "Rote Liste Gefäßpflanzen";
48

    
49
    private static final String pluralString = "classifications";
50

    
51
    public RedListGefaesspflanzenImportClassification() {
52
        super(tableName, pluralString);
53
    }
54

    
55
    @Override
56
    protected String getIdQuery(RedListGefaesspflanzenImportState state) {
57
        return "SELECT NAMNR "
58
                + "FROM V_TAXATLAS_D20_EXPORT t "
59
                + " ORDER BY NAMNR";
60
    }
61

    
62
    @Override
63
    protected String getRecordQuery(RedListGefaesspflanzenImportConfigurator config) {
64
        String result = " SELECT * "
65
                + " FROM V_TAXATLAS_D20_EXPORT t "
66
                + " WHERE t.NAMNR IN (@IDSET)";
67
        result = result.replace("@IDSET", IPartitionedIO.ID_LIST_TOKEN);
68
        return result;
69
    }
70

    
71
    @Override
72
    protected void doInvoke(RedListGefaesspflanzenImportState state) {
73
        makeClassification(state);
74
        super.doInvoke(state);
75
    }
76

    
77

    
78
    @Override
79
    public boolean doPartition(ResultSetPartitioner partitioner, RedListGefaesspflanzenImportState state) {
80
        ResultSet rs = partitioner.getResultSet();
81
        Classification gesamtListeClassification = getClassificationService().load(state.getConfig().getClassificationUuid());
82
        Classification checklistClassification = getClassificationService().load(RedListUtil.checkListClassificationUuid);
83
        try {
84
            while (rs.next()){
85
                makeSingleTaxonNode(state, rs, gesamtListeClassification, checklistClassification);
86

    
87
            }
88
        } catch (SQLException e) {
89
            e.printStackTrace();
90
        }
91

    
92
        logger.info("Update classification (1000 nodes)");
93
        getClassificationService().saveOrUpdate(gesamtListeClassification);
94
        getClassificationService().saveOrUpdate(checklistClassification);
95
        return true;
96
    }
97

    
98
    private void makeSingleTaxonNode(RedListGefaesspflanzenImportState state, ResultSet rs, Classification gesamtListeClassification, Classification checklistClassification)
99
            throws SQLException {
100
        long id = rs.getLong(RedListUtil.NAMNR);
101
        String parentId = String.valueOf(rs.getLong(RedListUtil.LOWER));
102
        String gueltString = rs.getString(RedListUtil.GUELT);
103
        String taxZusatzString = rs.getString(RedListUtil.TAX_ZUSATZ);
104

    
105
        //Gesamtliste
106
        TaxonBase taxonBaseGL = state.getRelatedObject(RedListUtil.TAXON_GESAMTLISTE_NAMESPACE, String.valueOf(id), TaxonBase.class);
107
        Taxon parentGL = (Taxon) state.getRelatedObject(RedListUtil.TAXON_GESAMTLISTE_NAMESPACE, parentId, TaxonBase.class);
108
        createParentChildNodes(gesamtListeClassification, id, gueltString, taxZusatzString, taxonBaseGL, parentGL);
109

    
110
        //Checkliste
111
        TaxonBase taxonBaseCL = state.getRelatedObject(RedListUtil.TAXON_CHECKLISTE_NAMESPACE, String.valueOf(id), TaxonBase.class);
112
        Taxon parentCL = (Taxon) state.getRelatedObject(RedListUtil.TAXON_CHECKLISTE_NAMESPACE, parentId, TaxonBase.class);
113
        if(taxonBaseCL!=null){//null check necessary because not all taxa exist in the checklist
114
            createParentChildNodes(checklistClassification, id, gueltString, taxZusatzString, taxonBaseCL, parentCL);
115
        }
116
    }
117

    
118
    private void createParentChildNodes(Classification classification, long id, String gueltString,
119
            String taxZusatzString, TaxonBase taxonBase, Taxon parent) {
120
        if(parent==null){
121
            RedListUtil.logMessage(id, "parent taxon of "+taxonBase+"  is null." , logger);
122
            return;
123
        }
124
        if(taxonBase==null){
125
            RedListUtil.logMessage(id, "child taxon/synonym of "+parent+"  is null." , logger);
126
            return;
127
        }
128
        //taxon
129
        if(taxonBase.isInstanceOf(Taxon.class)){
130
            //misapplied name
131
            String appendedPhrase = taxonBase.getName().getAppendedPhrase();
132
            if(appendedPhrase!=null && appendedPhrase.contains(RedListUtil.AUCT)){
133
                parent.addMisappliedName((Taxon) taxonBase, null, null);
134
            }
135
            else{
136
                classification.addParentChild(parent, (Taxon)taxonBase, null, null);
137
            }
138

    
139
            if(CdmUtils.isNotBlank(taxZusatzString)){
140
                if(taxZusatzString.trim().equals("p. p.")){
141
                    RedListUtil.logMessage(id, "pro parte for accepted taxon "+taxonBase, logger);
142
                }
143
            }
144
        }
145
        //synonym
146
        else if(taxonBase.isInstanceOf(Synonym.class)){
147
            //basionym
148
            if(gueltString.equals(RedListUtil.GUELT_BASIONYM)){
149
                parent.addHomotypicSynonym((Synonym) taxonBase, null, null);
150
                parent.getName().addBasionym(taxonBase.getName());
151
            }
152
            //regular synonym
153
            else{
154
                SynonymRelationship synonymRelationship = parent.addSynonym((Synonym) taxonBase, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF(), null, null);
155

    
156
                //TAX_ZUSATZ
157
                if(CdmUtils.isNotBlank(taxZusatzString)){
158
                    if(taxZusatzString.trim().equals("p. p.")){
159
                        synonymRelationship.setProParte(true);
160
                    }
161
                    else if(taxZusatzString.trim().equals("s. l. p. p.")){
162
                        synonymRelationship.setProParte(true);
163
                        taxonBase.setAppendedPhrase("s. l.");
164
                    }
165
                    else if(taxZusatzString.trim().equals("s. str. p. p.")){
166
                        synonymRelationship.setProParte(true);
167
                        taxonBase.setAppendedPhrase("s. str.");
168
                    }
169
                    else if(taxZusatzString.trim().equals("s. l.")
170
                            || taxZusatzString.trim().equals("s. str.")){
171
                        taxonBase.setAppendedPhrase(taxZusatzString);
172
                    }
173
                    else{
174
                        RedListUtil.logMessage(id, "unknown value "+taxZusatzString+" for column "+RedListUtil.TAX_ZUSATZ, logger);
175
                    }
176
                }
177
            }
178
        }
179
    }
180

    
181
    @Override
182
    public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
183
            RedListGefaesspflanzenImportState state) {
184
        Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
185

    
186
        Set<String> idSet = new HashSet<String>();
187
        try {
188
            while (rs.next()){
189
                idSet.add(String.valueOf(rs.getLong(RedListUtil.NAMNR)));
190
                idSet.add(String.valueOf(rs.getLong(RedListUtil.LOWER)));
191
            }
192
        } catch (SQLException e) {
193
            e.printStackTrace();
194
        }
195
        Map<String, TaxonBase> taxonMap = (Map<String, TaxonBase>) getCommonService().getSourcedObjectsByIdInSource(TaxonBase.class, idSet, RedListUtil.TAXON_GESAMTLISTE_NAMESPACE);
196
        result.put(RedListUtil.TAXON_GESAMTLISTE_NAMESPACE, taxonMap);
197
        return result;
198
    }
199

    
200
    private void makeClassification(RedListGefaesspflanzenImportState state) {
201
        //Gesamtliste
202
        Classification classification = Classification.NewInstance(state.getConfig().getClassificationName());
203
        classification.setUuid(state.getConfig().getClassificationUuid());
204
        getClassificationService().save(classification);
205
        //checkliste
206
        Classification checklistClassification = Classification.NewInstance("Checkliste");
207
        checklistClassification.setUuid(RedListUtil.checkListClassificationUuid);
208
        getClassificationService().save(checklistClassification);
209
    }
210

    
211
    @Override
212
    protected boolean doCheck(RedListGefaesspflanzenImportState state) {
213
        return false;
214
    }
215

    
216
    @Override
217
    protected boolean isIgnore(RedListGefaesspflanzenImportState state) {
218
        return false;
219
    }
220

    
221
}
(2-2/7)