Project

General

Profile

Download (5.95 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.io.common.DbImportBase;
23
import eu.etaxonomy.cdm.io.common.IPartitionedIO;
24
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
25
import eu.etaxonomy.cdm.model.common.CdmBase;
26
import eu.etaxonomy.cdm.model.taxon.Classification;
27
import eu.etaxonomy.cdm.model.taxon.Synonym;
28
import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
29
import eu.etaxonomy.cdm.model.taxon.Taxon;
30
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
31

    
32
/**
33
 *
34
 * @author pplitzner
35
 * @date Mar 1, 2016
36
 *
37
 */
38

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

    
43
    private static final Logger logger = Logger.getLogger(RedListGefaesspflanzenImportClassification.class);
44

    
45
    private static final String tableName = "Rote Liste Gefäßpflanzen";
46

    
47
    private static final String pluralString = "classifications";
48

    
49
    public RedListGefaesspflanzenImportClassification() {
50
        super(tableName, pluralString);
51
    }
52

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

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

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

    
75

    
76
    @Override
77
    public boolean doPartition(ResultSetPartitioner partitioner, RedListGefaesspflanzenImportState state) {
78
        ResultSet rs = partitioner.getResultSet();
79
        Classification classification = getClassificationService().load(state.getConfig().getClassificationUuid());
80
        try {
81
            while (rs.next()){
82
                makeSingleTaxonNode(state, rs, classification);
83

    
84
            }
85
        } catch (SQLException e) {
86
            e.printStackTrace();
87
        }
88

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

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

    
100
        TaxonBase taxonBase = state.getRelatedObject(RedListUtil.TAXON_NAMESPACE, id, TaxonBase.class);
101
        Taxon parent = (Taxon) state.getRelatedObject(RedListUtil.TAXON_NAMESPACE, parentId, TaxonBase.class);
102

    
103
        //taxon
104
        if(taxonBase.isInstanceOf(Taxon.class)){
105
            //misapplied name
106
            String appendedPhrase = taxonBase.getName().getAppendedPhrase();
107
            if(appendedPhrase!=null && appendedPhrase.contains(RedListUtil.AUCT)){
108
                parent.addMisappliedName((Taxon) taxonBase, null, null);
109
            }
110
            else{
111
                classification.addParentChild(parent, (Taxon)taxonBase, null, null);
112
            }
113
        }
114
        else if(taxonBase.isInstanceOf(Synonym.class)){
115
            //basionym
116
            if(gueltString.equals(RedListUtil.GUELT_BASIONYM)){
117
                parent.addHomotypicSynonym((Synonym) taxonBase, null, null);
118
                parent.getName().addBasionym(taxonBase.getName());
119
            }
120
            //regular synonym
121
            else{
122
                //TODO: how to correctly add a synonym?
123
                parent.addSynonym((Synonym) taxonBase, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF(), null, null);
124
//                parent.addSynonym((Synonym) taxonBase, SynonymRelationshipType.SYNONYM_OF(), null, null);
125
            }
126
        }
127
    }
128

    
129
    @Override
130
    public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
131
            RedListGefaesspflanzenImportState state) {
132
        Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
133

    
134
        Set<String> idSet = new HashSet<String>();
135
        try {
136
            while (rs.next()){
137
                idSet.add(String.valueOf(rs.getLong(RedListUtil.NAMNR)));
138
                idSet.add(String.valueOf(rs.getLong(RedListUtil.LOWER)));
139
            }
140
        } catch (SQLException e) {
141
            e.printStackTrace();
142
        }
143
        Map<String, TaxonBase> taxonMap = (Map<String, TaxonBase>) getCommonService().getSourcedObjectsByIdInSource(TaxonBase.class, idSet, RedListUtil.TAXON_NAMESPACE);
144
        result.put(RedListUtil.TAXON_NAMESPACE, taxonMap);
145
        return result;
146
    }
147

    
148
    private void makeClassification(RedListGefaesspflanzenImportState state) {
149
        Classification classification = Classification.NewInstance(state.getConfig().getClassificationName());
150
        classification.setUuid(state.getConfig().getClassificationUuid());
151
        getClassificationService().save(classification);
152
    }
153

    
154
    @Override
155
    protected boolean doCheck(RedListGefaesspflanzenImportState state) {
156
        return false;
157
    }
158

    
159
    @Override
160
    protected boolean isIgnore(RedListGefaesspflanzenImportState state) {
161
        return false;
162
    }
163

    
164
}
(2-2/7)