Project

General

Profile

Download (10 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.hibernate.HibernateProxyHelper;
24
import eu.etaxonomy.cdm.io.common.DbImportBase;
25
import eu.etaxonomy.cdm.io.common.IPartitionedIO;
26
import eu.etaxonomy.cdm.io.common.ImportHelper;
27
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
28
import eu.etaxonomy.cdm.io.common.mapping.UndefinedTransformerMethodException;
29
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
30
import eu.etaxonomy.cdm.model.common.CdmBase;
31
import eu.etaxonomy.cdm.model.name.BotanicalName;
32
import eu.etaxonomy.cdm.model.name.Rank;
33
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
34

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

    
42
@Component
43
@SuppressWarnings("serial")
44
public class RedListGefaesspflanzenImportNames extends DbImportBase<RedListGefaesspflanzenImportState, RedListGefaesspflanzenImportConfigurator> {
45
    /**
46
     *
47
     */
48
    private static final String EX = " ex ";
49

    
50
    private static final Logger logger = Logger.getLogger(RedListGefaesspflanzenImportNames.class);
51

    
52
    private static final String tableName = "Rote Liste Gefäßpflanzen";
53

    
54
    private static final String pluralString = "names";
55

    
56
    private static final String NAME_NAMESPACE = "name";
57

    
58
    public RedListGefaesspflanzenImportNames() {
59
        super(tableName, pluralString);
60
    }
61

    
62
    @Override
63
    protected String getIdQuery(RedListGefaesspflanzenImportState state) {
64
        return "SELECT NAMNR "
65
                + "FROM V_TAXATLAS_D20_EXPORT t "
66
                + " ORDER BY NAMNR";
67
    }
68

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

    
78
    @Override
79
    protected void doInvoke(RedListGefaesspflanzenImportState state) {
80
        super.doInvoke(state);
81
    }
82

    
83

    
84
    @Override
85
    public boolean doPartition(ResultSetPartitioner partitioner, RedListGefaesspflanzenImportState state) {
86
        ResultSet rs = partitioner.getResultSet();
87
        Set<TaxonNameBase> namesToSave = new HashSet<TaxonNameBase>();
88
        try {
89
            while (rs.next()){
90
                makeSingleName(state, rs, namesToSave);
91

    
92
            }
93
        } catch (SQLException e) {
94
            e.printStackTrace();
95
        }
96

    
97
        getNameService().saveOrUpdate(namesToSave);
98
        return true;
99
    }
100

    
101
    private void makeSingleName(RedListGefaesspflanzenImportState state, ResultSet rs, Set<TaxonNameBase> namesToSave)
102
            throws SQLException {
103
        long id = rs.getLong("NAMNR");
104
        String taxNameString = rs.getString("TAXNAME");
105
        String rangString = rs.getString("RANG");
106
        String ep1String = rs.getString("EPI1");
107
        String ep2String = rs.getString("EPI2");
108
        String ep3String = rs.getString("EPI3");
109
        String nomZusatzString = rs.getString("NOM_ZUSATZ");
110
        String zusatzString = rs.getString("ZUSATZ");
111
        String authorKombString = rs.getString("AUTOR_KOMB");
112
        String authorBasiString = rs.getString("AUTOR_BASI");
113

    
114
        if(CdmUtils.isBlank(taxNameString) && CdmUtils.isBlank(ep1String)){
115
            logger.error("NAMNR: "+id+" No name found!");
116
        }
117

    
118

    
119
        Rank rank = makeRank(state, rangString);
120
        BotanicalName name = BotanicalName.NewInstance(rank);
121

    
122
        //ep1 should always be present
123
        name.setGenusOrUninomial(ep1String);
124
        if(rank==Rank.SPECIES()){
125
            name.setGenusOrUninomial(ep1String);
126
        }
127

    
128
        //--- AUTHORS ---
129
        //combination author
130
        if(authorKombString.contains(EX)){
131
            //TODO: what happens with multiple ex authors??
132
            String[] kombSplit = authorKombString.split(EX);
133
            for (int i = 0; i < kombSplit.length; i++) {
134
                if(i==0){
135
                    //first author is ex author
136
                    TeamOrPersonBase authorKomb = HibernateProxyHelper.deproxy(getAgentService().load(state.getAuthorMap().get(kombSplit[i])), TeamOrPersonBase.class);
137
                    name.setExCombinationAuthorship(authorKomb);
138
                }
139
                else{
140
                    TeamOrPersonBase authorKomb = HibernateProxyHelper.deproxy(getAgentService().load(state.getAuthorMap().get(kombSplit[i])), TeamOrPersonBase.class);
141
                    name.setCombinationAuthorship(authorKomb);
142
                }
143
            }
144
        }
145
        else if(!CdmUtils.isBlank(authorKombString)){
146
            TeamOrPersonBase authorKomb = HibernateProxyHelper.deproxy(getAgentService().load(state.getAuthorMap().get(authorKombString)), TeamOrPersonBase.class);
147
            name.setCombinationAuthorship(authorKomb);
148
        }
149
        //basionym author
150
        if(authorBasiString.contains(EX)){
151
            String[] basiSplit = authorBasiString.split(EX);
152
            for (int i = 0; i < basiSplit.length; i++) {
153
                if(i==0){
154
                    TeamOrPersonBase authorBasi = HibernateProxyHelper.deproxy(getAgentService().load(state.getAuthorMap().get(basiSplit[i])), TeamOrPersonBase.class);
155
                    name.setExBasionymAuthorship(authorBasi);
156
                }
157
                else{
158
                    TeamOrPersonBase authorBasi = HibernateProxyHelper.deproxy(getAgentService().load(state.getAuthorMap().get(basiSplit[i])), TeamOrPersonBase.class);
159
                    name.setBasionymAuthorship(authorBasi);
160
                }
161
            }
162
        }
163
        else if(!CdmUtils.isBlank(authorBasiString)){
164
            TeamOrPersonBase authorBasi = HibernateProxyHelper.deproxy(getAgentService().load(state.getAuthorMap().get(authorBasiString)), TeamOrPersonBase.class);
165
            name.setBasionymAuthorship(authorBasi);
166
        }
167

    
168
        //check authorship consistency
169
        String authorString = rs.getString("AUTOR");
170
        String authorshipCache = name.getAuthorshipCache();
171

    
172
        if(!CdmUtils.isBlank(zusatzString)){
173
            authorString = authorString.replace(", "+zusatzString, "");
174
        }
175
        if(CdmUtils.isBlank(authorKombString) && !CdmUtils.isBlank(authorBasiString)){
176
            authorString = "("+authorString+")";
177
        }
178
        if(!authorString.equals(authorshipCache)){
179
            logger.warn("NAMNR: "+id+" Authorship inconsistent! name.authorhshipCache <-> Column AUTOR: "+authorshipCache+" <-> "+authorString);
180
        }
181

    
182
        //id
183
        ImportHelper.setOriginalSource(name, state.getTransactionalSourceReference(), id, NAME_NAMESPACE);
184

    
185
        namesToSave.add(name);
186
    }
187

    
188
    private Rank makeRank(RedListGefaesspflanzenImportState state, String rankStr) {
189
        Rank rank = null;
190
        try {
191
            rank = state.getTransformer().getRankByKey(rankStr);
192
        } catch (UndefinedTransformerMethodException e) {
193
            e.printStackTrace();
194
        }
195
        if(rank==null){
196
            logger.error(rankStr+" could not be associated to a known rank.");
197
        }
198
        return rank;
199
    }
200

    
201

    
202

    
203
    @Override
204
    public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
205
            RedListGefaesspflanzenImportState state) {
206
        Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
207
//        Map<Long, AgentBase<?>> authorKombMap = new HashMap<>();
208
//        Map<Long, AgentBase<?>> authorBasiMap = new HashMap<>();
209
//
210
//        //load authors
211
//        for(Entry<Long, UUID> entry:state.getAuthorKombMap().entrySet()){
212
//            authorKombMap.put(entry.getKey(), getAgentService().load(entry.getValue()));
213
//        }
214
//        for(Entry<Long, UUID> entry:state.getAuthorBasiMap().entrySet()){
215
//            authorBasiMap.put(entry.getKey(), getAgentService().load(entry.getValue()));
216
//        }
217
//        try {
218
//            while (rs.next()){
219
//                long id = rs.getLong("NAMNR");
220
//            }
221
//        } catch (SQLException e) {
222
//            e.printStackTrace();
223
//        }
224
//
225
//        //Authors
226
//        Set<UUID> uuidSet = new HashSet<>();
227
//        for (String authorStr : authorKombSet){
228
//            UUID uuid = state.getAuthorUuid(authorStr);
229
//            uuidSet.add(uuid);
230
//        }
231
//        List<TeamOrPersonBase<?>> authors = (List)getAgentService().find(uuidSet);
232
//        Map<UUID, TeamOrPersonBase<?>> authorUuidMap = new HashMap<>();
233
//        for (TeamOrPersonBase<?> author : authors){
234
//            authorUuidMap.put(author.getUuid(), author);
235
//        }
236
//
237
//        for (String authorStr : authorKombSet){
238
//            UUID uuid = state.getAuthorUuid(authorStr);
239
//            TeamOrPersonBase<?> author = authorUuidMap.get(uuid);
240
//            authorMap.put(authorStr, author);
241
//        }
242
//        result.put(AUTHOR_NAMESPACE, authorMap);
243
//
244
//        //reference map
245
//        String nameSpace = REFERENCE_NAMESPACE;
246
//        Class<?> cdmClass = Reference.class;
247
//        Set<String> idSet = referenceIdSet;
248
//        Map<String, Reference<?>> referenceMap = (Map<String, Reference<?>>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
249
//        result.put(nameSpace, referenceMap);
250
//
251
//        //secundum
252
//        UUID secUuid = state.getConfig().getSecUuid();
253
//        Reference<?> secRef = getReferenceService().find(secUuid);
254
//        referenceMap.put(secUuid.toString(), secRef);
255

    
256
        return result;
257
    }
258

    
259
    @Override
260
    protected boolean doCheck(RedListGefaesspflanzenImportState state) {
261
        return false;
262
    }
263

    
264
    @Override
265
    protected boolean isIgnore(RedListGefaesspflanzenImportState state) {
266
        return false;
267
    }
268

    
269
}
(3-3/6)