Project

General

Profile

Download (6.29 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 FROM V_TAXATLAS_D20_EXPORT t "
57
//                + " ORDER BY NAMNR"
58
                ;
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
                taxaToSave = makeSingleTaxon(state, rs);
83

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

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

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

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

    
103
        taxaToSave.add(taxon);
104

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

    
109
        return taxaToSave;
110
    }
111

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

    
122

    
123

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

    
173
        return result;
174
    }
175

    
176
    @Override
177
    protected boolean doCheck(RedListGefaesspflanzenImportState state) {
178
        return false;
179
    }
180

    
181
    @Override
182
    protected boolean isIgnore(RedListGefaesspflanzenImportState state) {
183
        return false;
184
    }
185

    
186
}
(3-3/4)