Project

General

Profile

Download (6.25 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.ImportHelper;
25
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
26
import eu.etaxonomy.cdm.io.common.mapping.UndefinedTransformerMethodException;
27
import eu.etaxonomy.cdm.model.common.CdmBase;
28
import eu.etaxonomy.cdm.model.name.NonViralName;
29
import eu.etaxonomy.cdm.model.name.Rank;
30
import eu.etaxonomy.cdm.model.taxon.Taxon;
31
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
32
import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
33

    
34
/**
35
 * @author a.mueller
36
 * @created 05.01.2016
37
 */
38

    
39
@Component
40
@SuppressWarnings("serial")
41
public class RedListGefaesspflanzenImportTaxa extends DbImportBase<RedListGefaesspflanzenImportState, RedListGefaesspflanzenImportConfigurator> {
42
    private static final Logger logger = Logger.getLogger(RedListGefaesspflanzenImportTaxa.class);
43

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

    
46
    private static final String pluralString = "taxa";
47

    
48
    private static final String TAXON_NAMESPACE = "taxon";
49

    
50
    public RedListGefaesspflanzenImportTaxa() {
51
        super(tableName, pluralString);
52
    }
53

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

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

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

    
75

    
76
    @Override
77
    public boolean doPartition(ResultSetPartitioner partitioner, RedListGefaesspflanzenImportState state) {
78
        ResultSet rs = partitioner.getResultSet();
79
        Set<TaxonBase> taxaToSave = new HashSet<>();
80
        try {
81
            while (rs.next()){
82
                makeSingleTaxon(state, rs, taxaToSave);
83

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

    
89
        getTaxonService().saveOrUpdate(taxaToSave);
90
        return true;
91
    }
92

    
93
    private void makeSingleTaxon(RedListGefaesspflanzenImportState state, ResultSet rs, Set<TaxonBase> taxaToSave)
94
            throws SQLException {
95
        long id = rs.getLong("NAMNR");
96
        String taxonNameString = rs.getString("TAXNAME");
97

    
98
        NonViralNameParserImpl parser = NonViralNameParserImpl.NewInstance();
99
        NonViralName<?> name = parser.parseFullName(taxonNameString);
100
        TaxonBase taxon = Taxon.NewInstance(name, null);
101

    
102
        taxaToSave.add(taxon);
103

    
104
        //id
105
        ImportHelper.setOriginalSource(taxon, state.getTransactionalSourceReference(), id, TAXON_NAMESPACE);
106
        ImportHelper.setOriginalSource(name, state.getTransactionalSourceReference(), id, TAXON_NAMESPACE);
107
    }
108

    
109
    private Rank makeRank(RedListGefaesspflanzenImportState state, String rankStr) {
110
        Rank rank = null;
111
        try {
112
            rank = state.getTransformer().getRankByKey(rankStr);
113
        } catch (UndefinedTransformerMethodException e) {
114
            e.printStackTrace();
115
        }
116
        return rank;
117
    }
118

    
119

    
120

    
121
    @Override
122
    public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
123
            RedListGefaesspflanzenImportState state) {
124
        Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
125
//        Map<String, TeamOrPersonBase<?>> authorMap = new HashMap<>();
126
//        Set<String> authorKombSet = new HashSet<>();
127
//        Set<String> referenceIdSet = new HashSet<String>();
128
//
129
//        try {
130
//            while (rs.next()){
131
//                String authorStr = rs.getString("tax_author_name");
132
//                authorKombSet.add(authorStr);
133
//                handleForeignKey(rs, referenceIdSet, "tax_document");
134
//            }
135
//        } catch (SQLException e) {
136
//            e.printStackTrace();
137
//        }
138
//
139
//        //Authors
140
//        Set<UUID> uuidSet = new HashSet<>();
141
//        for (String authorStr : authorKombSet){
142
//            UUID uuid = state.getAuthorUuid(authorStr);
143
//            uuidSet.add(uuid);
144
//        }
145
//        List<TeamOrPersonBase<?>> authors = (List)getAgentService().find(uuidSet);
146
//        Map<UUID, TeamOrPersonBase<?>> authorUuidMap = new HashMap<>();
147
//        for (TeamOrPersonBase<?> author : authors){
148
//            authorUuidMap.put(author.getUuid(), author);
149
//        }
150
//
151
//        for (String authorStr : authorKombSet){
152
//            UUID uuid = state.getAuthorUuid(authorStr);
153
//            TeamOrPersonBase<?> author = authorUuidMap.get(uuid);
154
//            authorMap.put(authorStr, author);
155
//        }
156
//        result.put(AUTHOR_NAMESPACE, authorMap);
157
//
158
//        //reference map
159
//        String nameSpace = REFERENCE_NAMESPACE;
160
//        Class<?> cdmClass = Reference.class;
161
//        Set<String> idSet = referenceIdSet;
162
//        Map<String, Reference<?>> referenceMap = (Map<String, Reference<?>>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
163
//        result.put(nameSpace, referenceMap);
164
//
165
//        //secundum
166
//        UUID secUuid = state.getConfig().getSecUuid();
167
//        Reference<?> secRef = getReferenceService().find(secUuid);
168
//        referenceMap.put(secUuid.toString(), secRef);
169

    
170
        return result;
171
    }
172

    
173
    @Override
174
    protected boolean doCheck(RedListGefaesspflanzenImportState state) {
175
        return false;
176
    }
177

    
178
    @Override
179
    protected boolean isIgnore(RedListGefaesspflanzenImportState state) {
180
        return false;
181
    }
182

    
183
}
(4-4/5)