Project

General

Profile

Download (10.5 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
        Rank rank = makeRank(state, rangString);
119
        if(rank==null){
120
            logger.error("NAMNR: "+id+" Rank could not be resolved.");
121
            return;
122
        }
123
        BotanicalName name = BotanicalName.NewInstance(rank);
124

    
125
        //ep1 should always be present
126
        if(CdmUtils.isBlank(ep1String)){
127
            logger.error("NAMNR: "+id+" EPI1 is empty!");
128
        }
129
        name.setGenusOrUninomial(ep1String);
130
        if(!CdmUtils.isBlank(ep2String)){
131
            name.setSpecificEpithet(ep2String);
132
        }
133
        if(!CdmUtils.isBlank(ep3String)){
134
            if(rank==Rank.SUBSPECIES() ||
135
                    rank==Rank.VARIETY()){
136
                name.setInfraSpecificEpithet(ep3String);
137
            }
138
        }
139

    
140
        //--- AUTHORS ---
141
        //combination author
142
        if(authorKombString.contains(EX)){
143
            //TODO: what happens with multiple ex authors??
144
            String[] kombSplit = authorKombString.split(EX);
145
            for (int i = 0; i < kombSplit.length; i++) {
146
                if(i==0){
147
                    //first author is ex author
148
                    TeamOrPersonBase authorKomb = HibernateProxyHelper.deproxy(getAgentService().load(state.getAuthorMap().get(kombSplit[i])), TeamOrPersonBase.class);
149
                    name.setExCombinationAuthorship(authorKomb);
150
                }
151
                else{
152
                    TeamOrPersonBase authorKomb = HibernateProxyHelper.deproxy(getAgentService().load(state.getAuthorMap().get(kombSplit[i])), TeamOrPersonBase.class);
153
                    name.setCombinationAuthorship(authorKomb);
154
                }
155
            }
156
        }
157
        else if(!CdmUtils.isBlank(authorKombString)){
158
            TeamOrPersonBase authorKomb = HibernateProxyHelper.deproxy(getAgentService().load(state.getAuthorMap().get(authorKombString)), TeamOrPersonBase.class);
159
            name.setCombinationAuthorship(authorKomb);
160
        }
161
        //basionym author
162
        if(authorBasiString.contains(EX)){
163
            String[] basiSplit = authorBasiString.split(EX);
164
            for (int i = 0; i < basiSplit.length; i++) {
165
                if(i==0){
166
                    TeamOrPersonBase authorBasi = HibernateProxyHelper.deproxy(getAgentService().load(state.getAuthorMap().get(basiSplit[i])), TeamOrPersonBase.class);
167
                    name.setExBasionymAuthorship(authorBasi);
168
                }
169
                else{
170
                    TeamOrPersonBase authorBasi = HibernateProxyHelper.deproxy(getAgentService().load(state.getAuthorMap().get(basiSplit[i])), TeamOrPersonBase.class);
171
                    name.setBasionymAuthorship(authorBasi);
172
                }
173
            }
174
        }
175
        else if(!CdmUtils.isBlank(authorBasiString)){
176
            TeamOrPersonBase authorBasi = HibernateProxyHelper.deproxy(getAgentService().load(state.getAuthorMap().get(authorBasiString)), TeamOrPersonBase.class);
177
            name.setBasionymAuthorship(authorBasi);
178
        }
179

    
180
        //check authorship consistency
181
        String authorString = rs.getString("AUTOR");
182
        String authorshipCache = name.getAuthorshipCache();
183

    
184
        if(!CdmUtils.isBlank(zusatzString)){
185
            authorString = authorString.replace(", "+zusatzString, "");
186
        }
187
        if(CdmUtils.isBlank(authorKombString) && !CdmUtils.isBlank(authorBasiString)){
188
            authorString = "("+authorString+")";
189
        }
190
        if(!authorString.equals(authorshipCache)){
191
            logger.warn("NAMNR: "+id+" Authorship inconsistent! name.authorhshipCache <-> Column AUTOR: "+authorshipCache+" <-> "+authorString);
192
        }
193

    
194
        //id
195
        ImportHelper.setOriginalSource(name, state.getTransactionalSourceReference(), id, NAME_NAMESPACE);
196
        state.getNameMap().put(id, name.getUuid());
197

    
198
        namesToSave.add(name);
199
    }
200

    
201
    private Rank makeRank(RedListGefaesspflanzenImportState state, String rankStr) {
202
        Rank rank = null;
203
        try {
204
            rank = state.getTransformer().getRankByKey(rankStr);
205
        } catch (UndefinedTransformerMethodException e) {
206
            e.printStackTrace();
207
        }
208
        if(rank==null){
209
            logger.error(rankStr+" could not be associated to a known rank.");
210
        }
211
        return rank;
212
    }
213

    
214

    
215

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

    
269
        return result;
270
    }
271

    
272
    @Override
273
    protected boolean doCheck(RedListGefaesspflanzenImportState state) {
274
        return false;
275
    }
276

    
277
    @Override
278
    protected boolean isIgnore(RedListGefaesspflanzenImportState state) {
279
        return false;
280
    }
281

    
282
}
(3-3/6)