Project

General

Profile

Download (11.1 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.common.Language;
28
import eu.etaxonomy.cdm.model.common.LanguageString;
29
import eu.etaxonomy.cdm.model.reference.Reference;
30
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
31
import eu.etaxonomy.cdm.model.taxon.Classification;
32
import eu.etaxonomy.cdm.model.taxon.Synonym;
33
import eu.etaxonomy.cdm.model.taxon.SynonymRelationship;
34
import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
35
import eu.etaxonomy.cdm.model.taxon.Taxon;
36
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
37

    
38
/**
39
 *
40
 * @author pplitzner
41
 * @date Mar 1, 2016
42
 *
43
 */
44

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

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

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

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

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

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

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

    
75
    @Override
76
    protected void doInvoke(RedListGefaesspflanzenImportState state) {
77
        makeClassification(state);
78
        super.doInvoke(state);
79
    }
80

    
81

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

    
91
            }
92
        } catch (SQLException e) {
93
            e.printStackTrace();
94
        }
95

    
96
        logger.info("Update classification (1000 nodes)");
97
        getClassificationService().saveOrUpdate(gesamtListeClassification);
98
        getClassificationService().saveOrUpdate(checklistClassification);
99
        return true;
100
    }
101

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

    
109
        //Gesamtliste
110
        TaxonBase taxonBaseGL = state.getRelatedObject(RedListUtil.TAXON_GESAMTLISTE_NAMESPACE, String.valueOf(id), TaxonBase.class);
111
        Taxon parentGL = (Taxon) state.getRelatedObject(RedListUtil.TAXON_GESAMTLISTE_NAMESPACE, parentId, TaxonBase.class);
112
        createParentChildNodes(gesamtListeClassification, id, gueltString, taxZusatzString, taxonBaseGL, parentGL);
113

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

    
121
        if(taxonBaseGL!= null && taxonBaseCL!=null
122
                && taxonBaseGL.getUuid().equals(taxonBaseCL.getUuid())){
123
            RedListUtil.logMessage(id, "Same UUID for "+taxonBaseGL+ " (Gesamtliste) and "+taxonBaseCL+" (Checkliste", logger);
124
        }
125
        if(parentGL!=null && parentCL!=null && parentGL.getUuid().equals(parentCL.getUuid())){
126
            RedListUtil.logMessage(id, "Same UUID for "+parentGL+ " (Gesamtliste) and "+parentCL+" (Checkliste", logger);
127
        }
128
    }
129

    
130
    private void createParentChildNodes(Classification classification, long id, String gueltString,
131
            String taxZusatzString, TaxonBase taxonBase, Taxon parent) {
132
        if(taxonBase==null){
133
            RedListUtil.logMessage(id, "child taxon/synonym of "+parent+"  is null. ("+classification.getTitleCache()+")" , logger);
134
            return;
135
        }
136
        //taxon
137
        if(taxonBase.isInstanceOf(Taxon.class)){
138
            //misapplied name
139
            String appendedPhrase = taxonBase.getAppendedPhrase();
140
            if(appendedPhrase!=null && appendedPhrase.equals(RedListUtil.AUCT)){
141
                if(parent==null){
142
                    RedListUtil.logMessage(id, "parent taxon of misapplied name "+taxonBase+"  is null. ("+classification.getTitleCache()+")" , logger);
143
                    return;
144
                }
145
                parent.addMisappliedName((Taxon) taxonBase, null, null);
146
            }
147
            else{
148
                classification.addParentChild(parent, (Taxon)taxonBase, null, null);
149
            }
150

    
151
            if(CdmUtils.isNotBlank(taxZusatzString)){
152
                if(taxZusatzString.trim().equals("p. p.")){
153
                    RedListUtil.logMessage(id, "pro parte for accepted taxon "+taxonBase, logger);
154
                }
155
            }
156
        }
157
        //synonym
158
        else if(taxonBase.isInstanceOf(Synonym.class)){
159
            if(parent==null){
160
                RedListUtil.logMessage(id, "parent taxon of synonym "+taxonBase+"  is null. ("+classification.getTitleCache()+")" , logger);
161
                return;
162
            }
163
            //basionym
164
            if(gueltString.equals(RedListUtil.GUELT_BASIONYM)){
165
                parent.addHomotypicSynonym((Synonym) taxonBase, null, null);
166
                parent.getName().addBasionym(taxonBase.getName());
167
            }
168
            //regular synonym
169
            else{
170
                SynonymRelationship synonymRelationship = parent.addSynonym((Synonym) taxonBase, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF(), null, null);
171

    
172
                //TAX_ZUSATZ
173
                if(CdmUtils.isNotBlank(taxZusatzString)){
174
                    if(taxZusatzString.trim().equals("p. p.")){
175
                        synonymRelationship.setProParte(true);
176
                    }
177
                    else if(taxZusatzString.trim().equals("s. l. p. p.")){
178
                        synonymRelationship.setProParte(true);
179
                        taxonBase.setAppendedPhrase("s. l.");
180
                    }
181
                    else if(taxZusatzString.trim().equals("s. str. p. p.")){
182
                        synonymRelationship.setProParte(true);
183
                        taxonBase.setAppendedPhrase("s. str.");
184
                    }
185
                    else if(taxZusatzString.trim().equals("s. l.")
186
                            || taxZusatzString.trim().equals("s. str.")){
187
                        taxonBase.setAppendedPhrase(taxZusatzString);
188
                    }
189
                    else{
190
                        RedListUtil.logMessage(id, "unknown value "+taxZusatzString+" for column "+RedListUtil.TAX_ZUSATZ, logger);
191
                    }
192
                }
193
            }
194
        }
195
        //set sec reference
196
        taxonBase.setSec(classification.getReference());
197
        taxonBase.setTitleCache(null, false);//refresh title cache
198
    }
199

    
200
    @Override
201
    public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
202
            RedListGefaesspflanzenImportState state) {
203
        Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
204

    
205
        Set<String> idSet = new HashSet<String>();
206
        try {
207
            while (rs.next()){
208
                idSet.add(String.valueOf(rs.getLong(RedListUtil.NAMNR)));
209
                idSet.add(String.valueOf(rs.getLong(RedListUtil.LOWER)));
210
            }
211
        } catch (SQLException e) {
212
            e.printStackTrace();
213
        }
214
        Map<String, TaxonBase> taxonMapGesamtListe = (Map<String, TaxonBase>) getCommonService().getSourcedObjectsByIdInSource(TaxonBase.class, idSet, RedListUtil.TAXON_GESAMTLISTE_NAMESPACE);
215
        result.put(RedListUtil.TAXON_GESAMTLISTE_NAMESPACE, taxonMapGesamtListe);
216
        Map<String, TaxonBase> taxonMapCheckliste = (Map<String, TaxonBase>) getCommonService().getSourcedObjectsByIdInSource(TaxonBase.class, idSet, RedListUtil.TAXON_CHECKLISTE_NAMESPACE);
217
        result.put(RedListUtil.TAXON_CHECKLISTE_NAMESPACE, taxonMapCheckliste);
218
        return result;
219
    }
220

    
221
    private void makeClassification(RedListGefaesspflanzenImportState state) {
222
        //Gesamtliste
223
        Classification classification = Classification.NewInstance(state.getConfig().getClassificationName());
224
        classification.setName(LanguageString.NewInstance("Gesamtliste", Language.DEFAULT()));
225
        classification.setUuid(state.getConfig().getClassificationUuid());
226
        Reference gesamtListeReference = ReferenceFactory.newGeneric();
227
        gesamtListeReference.setUuid(RedListUtil.gesamtListeReferenceUuid);
228
        gesamtListeReference.setTitle("Gesamtliste");
229
        classification.setReference(gesamtListeReference);
230
        getClassificationService().save(classification);
231
        //checkliste
232
        Classification checklistClassification = Classification.NewInstance("Checkliste");
233
        checklistClassification.setUuid(RedListUtil.checkListClassificationUuid);
234
        Reference checklistReference = ReferenceFactory.newGeneric();
235
        checklistReference.setUuid(RedListUtil.checkListReferenceUuid);
236
        checklistReference.setTitle("Checkliste");
237
        checklistClassification.setReference(checklistReference);
238
        getClassificationService().save(checklistClassification);
239
    }
240

    
241
    @Override
242
    protected boolean doCheck(RedListGefaesspflanzenImportState state) {
243
        return false;
244
    }
245

    
246
    @Override
247
    protected boolean isIgnore(RedListGefaesspflanzenImportState state) {
248
        return false;
249
    }
250

    
251
}
(2-2/7)