Project

General

Profile

Download (10.4 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
    public RedListGefaesspflanzenImportNames() {
57
        super(tableName, pluralString);
58
    }
59

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

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

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

    
81

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

    
90
            }
91
        } catch (SQLException e) {
92
            e.printStackTrace();
93
        }
94

    
95
        getNameService().saveOrUpdate(namesToSave);
96
        return true;
97
    }
98

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

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

    
116
        Rank rank = makeRank(state, rangString);
117
        if(rank==null){
118
            logger.error("NAMNR: "+id+" Rank could not be resolved.");
119
            return;
120
        }
121
        BotanicalName name = BotanicalName.NewInstance(rank);
122

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

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

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

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

    
192
        //id
193
        ImportHelper.setOriginalSource(name, state.getTransactionalSourceReference(), id, Namespace.NAME_NAMESPACE);
194
        state.getNameMap().put(id, name.getUuid());
195

    
196
        namesToSave.add(name);
197
    }
198

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

    
212

    
213

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

    
267
        return result;
268
    }
269

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

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

    
280
}
(4-4/7)