Project

General

Profile

Download (7.24 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.ImportHelper;
26
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
27
import eu.etaxonomy.cdm.io.common.mapping.UndefinedTransformerMethodException;
28
import eu.etaxonomy.cdm.model.agent.AgentBase;
29
import eu.etaxonomy.cdm.model.common.CdmBase;
30
import eu.etaxonomy.cdm.model.name.BotanicalName;
31
import eu.etaxonomy.cdm.model.name.Rank;
32
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
33

    
34
/**
35
 *
36
 * @author pplitzner
37
 * @date Mar 1, 2016
38
 *
39
 */
40

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

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

    
48
    private static final String pluralString = "names";
49

    
50
    private static final String TAXON_NAMESPACE = "name";
51
    private static final String AUTHOR_KOMB_NAMESPACE = "author_komb";
52
    private static final String AUTHOR_BASI_NAMESPACE = "author_basi";
53

    
54
    public RedListGefaesspflanzenImportNames() {
55
        super(tableName, pluralString);
56
    }
57

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

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

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

    
79

    
80
    @Override
81
    public boolean doPartition(ResultSetPartitioner partitioner, RedListGefaesspflanzenImportState state) {
82
        ResultSet rs = partitioner.getResultSet();
83
        Set<TaxonNameBase> taxaToSave = new HashSet<TaxonNameBase>();
84
        try {
85
            while (rs.next()){
86
                makeSingleName(state, rs, taxaToSave);
87

    
88
            }
89
        } catch (SQLException e) {
90
            e.printStackTrace();
91
        }
92

    
93
        getNameService().saveOrUpdate(taxaToSave);
94
        return true;
95
    }
96

    
97
    private void makeSingleName(RedListGefaesspflanzenImportState state, ResultSet rs, Set<TaxonNameBase> namesToSave)
98
            throws SQLException {
99
        long id = rs.getLong("NAMNR");
100
        String taxNameString = rs.getString("TAXNAME");
101
        String rangString = rs.getString("RANG");
102
        String ep1String = rs.getString("EPI1");
103
        String ep2String = rs.getString("EPI2");
104
        String ep3String = rs.getString("EPI3");
105
        String nomZusatzString = rs.getString("NOM_ZUSATZ");
106
        String zusatzString = rs.getString("ZUSATZ");
107

    
108
        if(CdmUtils.isBlank(taxNameString) && CdmUtils.isBlank(ep1String)){
109
            logger.error("NAMNR: "+id+" No name found!");
110
        }
111

    
112

    
113
        Rank rank = makeRank(state, rangString);
114
        BotanicalName name = BotanicalName.NewInstance(rank);
115

    
116
        //ep1 should always be present
117
        name.setGenusOrUninomial(ep1String);
118
        if(rank==Rank.SPECIES()){
119
            name.setGenusOrUninomial(ep1String);
120
        }
121

    
122
        //add author
123
        AgentBase authorKomb = getAgentService().load(state.getAuthorKombMap().get(id));
124
//        name.setCombinationAuthorship(authorKomb);
125

    
126
        //id
127
        ImportHelper.setOriginalSource(name, state.getTransactionalSourceReference(), id, TAXON_NAMESPACE);
128
    }
129

    
130
    private Rank makeRank(RedListGefaesspflanzenImportState state, String rankStr) {
131
        Rank rank = null;
132
        try {
133
            rank = state.getTransformer().getRankByKey(rankStr);
134
        } catch (UndefinedTransformerMethodException e) {
135
            e.printStackTrace();
136
        }
137
        if(rank==null){
138
            logger.error(rankStr+" could not be associated to a known rank.");
139
        }
140
        return rank;
141
    }
142

    
143

    
144

    
145
    @Override
146
    public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
147
            RedListGefaesspflanzenImportState state) {
148
        Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
149
//        Map<Long, AgentBase<?>> authorKombMap = new HashMap<>();
150
//        Map<Long, AgentBase<?>> authorBasiMap = new HashMap<>();
151
//
152
//        //load authors
153
//        for(Entry<Long, UUID> entry:state.getAuthorKombMap().entrySet()){
154
//            authorKombMap.put(entry.getKey(), getAgentService().load(entry.getValue()));
155
//        }
156
//        for(Entry<Long, UUID> entry:state.getAuthorBasiMap().entrySet()){
157
//            authorBasiMap.put(entry.getKey(), getAgentService().load(entry.getValue()));
158
//        }
159
//        try {
160
//            while (rs.next()){
161
//                long id = rs.getLong("NAMNR");
162
//            }
163
//        } catch (SQLException e) {
164
//            e.printStackTrace();
165
//        }
166
//
167
//        //Authors
168
//        Set<UUID> uuidSet = new HashSet<>();
169
//        for (String authorStr : authorKombSet){
170
//            UUID uuid = state.getAuthorUuid(authorStr);
171
//            uuidSet.add(uuid);
172
//        }
173
//        List<TeamOrPersonBase<?>> authors = (List)getAgentService().find(uuidSet);
174
//        Map<UUID, TeamOrPersonBase<?>> authorUuidMap = new HashMap<>();
175
//        for (TeamOrPersonBase<?> author : authors){
176
//            authorUuidMap.put(author.getUuid(), author);
177
//        }
178
//
179
//        for (String authorStr : authorKombSet){
180
//            UUID uuid = state.getAuthorUuid(authorStr);
181
//            TeamOrPersonBase<?> author = authorUuidMap.get(uuid);
182
//            authorMap.put(authorStr, author);
183
//        }
184
//        result.put(AUTHOR_NAMESPACE, authorMap);
185
//
186
//        //reference map
187
//        String nameSpace = REFERENCE_NAMESPACE;
188
//        Class<?> cdmClass = Reference.class;
189
//        Set<String> idSet = referenceIdSet;
190
//        Map<String, Reference<?>> referenceMap = (Map<String, Reference<?>>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
191
//        result.put(nameSpace, referenceMap);
192
//
193
//        //secundum
194
//        UUID secUuid = state.getConfig().getSecUuid();
195
//        Reference<?> secRef = getReferenceService().find(secUuid);
196
//        referenceMap.put(secUuid.toString(), secRef);
197

    
198
        return result;
199
    }
200

    
201
    @Override
202
    protected boolean doCheck(RedListGefaesspflanzenImportState state) {
203
        return false;
204
    }
205

    
206
    @Override
207
    protected boolean isIgnore(RedListGefaesspflanzenImportState state) {
208
        return false;
209
    }
210

    
211
}
(3-3/6)