Project

General

Profile

Download (7.36 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 classification = getClassificationService().load(state.getConfig().getClassificationUuid());
82
        try {
83
            while (rs.next()){
84
                makeSingleTaxonNode(state, rs, classification);
85

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

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

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

    
103
        TaxonBase taxonBase = state.getRelatedObject(RedListUtil.TAXON_NAMESPACE, String.valueOf(id), TaxonBase.class);
104
        Taxon parent = (Taxon) state.getRelatedObject(RedListUtil.TAXON_NAMESPACE, parentId, TaxonBase.class);
105

    
106
        //taxon
107
        if(taxonBase.isInstanceOf(Taxon.class)){
108
            //misapplied name
109
            String appendedPhrase = taxonBase.getName().getAppendedPhrase();
110
            if(appendedPhrase!=null && appendedPhrase.contains(RedListUtil.AUCT)){
111
                parent.addMisappliedName((Taxon) taxonBase, null, null);
112
            }
113
            else{
114
                classification.addParentChild(parent, (Taxon)taxonBase, null, null);
115
            }
116

    
117
            if(CdmUtils.isNotBlank(taxZusatzString)){
118
                if(taxZusatzString.trim().equals("p. p.")){
119
                    RedListUtil.logMessage(id, "pro parte for accepted taxon "+taxonBase, logger);
120
                }
121
            }
122
        }
123
        else if(taxonBase.isInstanceOf(Synonym.class)){
124
            //basionym
125
            if(gueltString.equals(RedListUtil.GUELT_BASIONYM)){
126
                parent.addHomotypicSynonym((Synonym) taxonBase, null, null);
127
                parent.getName().addBasionym(taxonBase.getName());
128
            }
129
            //regular synonym
130
            else{
131
                //TODO: how to correctly add a synonym?
132
                SynonymRelationship synonymRelationship = parent.addSynonym((Synonym) taxonBase, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF(), null, null);
133

    
134
                //TAX_ZUSATZ
135
                if(CdmUtils.isNotBlank(taxZusatzString)){
136
                    if(taxZusatzString.trim().equals("p. p.")){
137
                        synonymRelationship.setProParte(true);
138
                    }
139
                    else if(taxZusatzString.trim().equals("s. l. p. p.")){
140
                        synonymRelationship.setProParte(true);
141
                        taxonBase.setAppendedPhrase("s. l.");
142
                    }
143
                    else if(taxZusatzString.trim().equals("s. str. p. p.")){
144
                        synonymRelationship.setProParte(true);
145
                        taxonBase.setAppendedPhrase("s. str.");
146
                    }
147
                    else if(taxZusatzString.trim().equals("s. l.")
148
                            || taxZusatzString.trim().equals("s. str.")){
149
                        taxonBase.setAppendedPhrase(taxZusatzString);
150
                    }
151
                    else{
152
                        RedListUtil.logMessage(id, "unknown value for column "+RedListUtil.TAX_ZUSATZ, logger);
153
                    }
154
                }
155
            }
156
        }
157
    }
158

    
159
    @Override
160
    public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
161
            RedListGefaesspflanzenImportState state) {
162
        Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
163

    
164
        Set<String> idSet = new HashSet<String>();
165
        try {
166
            while (rs.next()){
167
                idSet.add(String.valueOf(rs.getLong(RedListUtil.NAMNR)));
168
                idSet.add(String.valueOf(rs.getLong(RedListUtil.LOWER)));
169
            }
170
        } catch (SQLException e) {
171
            e.printStackTrace();
172
        }
173
        Map<String, TaxonBase> taxonMap = (Map<String, TaxonBase>) getCommonService().getSourcedObjectsByIdInSource(TaxonBase.class, idSet, RedListUtil.TAXON_NAMESPACE);
174
        result.put(RedListUtil.TAXON_NAMESPACE, taxonMap);
175
        return result;
176
    }
177

    
178
    private void makeClassification(RedListGefaesspflanzenImportState state) {
179
        Classification classification = Classification.NewInstance(state.getConfig().getClassificationName());
180
        classification.setUuid(state.getConfig().getClassificationUuid());
181
        getClassificationService().save(classification);
182
    }
183

    
184
    @Override
185
    protected boolean doCheck(RedListGefaesspflanzenImportState state) {
186
        return false;
187
    }
188

    
189
    @Override
190
    protected boolean isIgnore(RedListGefaesspflanzenImportState state) {
191
        return false;
192
    }
193

    
194
}
(2-2/7)