Project

General

Profile

Download (15.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.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.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.NomenclaturalStatus;
33
import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
34
import eu.etaxonomy.cdm.model.name.Rank;
35
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
36
import eu.etaxonomy.cdm.model.taxon.Synonym;
37
import eu.etaxonomy.cdm.model.taxon.Taxon;
38
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
39
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
40
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
41

    
42
/**
43
 *
44
 * @author pplitzner
45
 * @date Mar 1, 2016
46
 *
47
 */
48

    
49
@Component
50
@SuppressWarnings("serial")
51
public class RedListGefaesspflanzenImportNames extends DbImportBase<RedListGefaesspflanzenImportState, RedListGefaesspflanzenImportConfigurator> {
52

    
53
    private static final Logger logger = Logger.getLogger(RedListGefaesspflanzenImportNames.class);
54

    
55
    private static final String tableName = "Rote Liste Gefäßpflanzen";
56

    
57
    private static final String pluralString = "names";
58

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

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

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

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

    
84

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

    
94
            }
95
        } catch (SQLException e) {
96
            e.printStackTrace();
97
        }
98

    
99
        getNameService().saveOrUpdate(namesToSave);
100
        getTaxonService().saveOrUpdate(taxaToSave);
101
        return true;
102
    }
103

    
104
    private void makeSingleNameAndTaxon(RedListGefaesspflanzenImportState state, ResultSet rs, Set<TaxonNameBase> namesToSave, Set<TaxonBase> taxaToSave)
105
            throws SQLException {
106
        long id = rs.getLong(RedListUtil.NAMNR);
107
        String taxNameString = rs.getString(RedListUtil.TAXNAME);
108
        String gueltString = rs.getString(RedListUtil.GUELT);
109
        String rangString = rs.getString(RedListUtil.RANG);
110
        String ep1String = rs.getString(RedListUtil.EPI1);
111
        String ep2String = rs.getString(RedListUtil.EPI2);
112
        String ep3String = rs.getString(RedListUtil.EPI3);
113
        String nomZusatzString = rs.getString(RedListUtil.NOM_ZUSATZ);
114
        String taxZusatzString = rs.getString(RedListUtil.TAX_ZUSATZ);
115
        String zusatzString = rs.getString(RedListUtil.ZUSATZ);
116
        String authorKombString = rs.getString(RedListUtil.AUTOR_KOMB);
117
        String authorBasiString = rs.getString(RedListUtil.AUTOR_BASI);
118

    
119
        //---NAME---
120
        if(CdmUtils.isBlank(taxNameString) && CdmUtils.isBlank(ep1String)){
121
            RedListUtil.logMessage(id, "No name found!", logger);
122
        }
123

    
124
        Rank rank = makeRank(id, state, rangString);
125
        BotanicalName name = BotanicalName.NewInstance(rank);
126

    
127
        //ep1 should always be present
128
        if(CdmUtils.isBlank(ep1String)){
129
            RedListUtil.logMessage(id, RedListUtil.EPI1+" is empty!", logger);
130
        }
131
        name.setGenusOrUninomial(ep1String);
132
        if(CdmUtils.isNotBlank(ep2String)){
133
            name.setSpecificEpithet(ep2String);
134
        }
135
        if(CdmUtils.isNotBlank(ep3String)){
136
            if(rank==Rank.SUBSPECIES() ||
137
                    rank==Rank.VARIETY()){
138
                name.setInfraSpecificEpithet(ep3String);
139
            }
140
        }
141
        //nomenclatural status
142
        if(CdmUtils.isNotBlank(nomZusatzString)){
143
            NomenclaturalStatusType status = makeNomenclaturalStatus(id, state, nomZusatzString);
144
            if(status!=null){
145
                name.addStatus(NomenclaturalStatus.NewInstance(status));
146
            }
147
        }
148

    
149

    
150
        //--- AUTHORS ---
151
        //combination author
152
        if(authorKombString.contains(RedListUtil.EX)){
153
            //TODO: what happens with multiple ex authors??
154
            String[] kombSplit = authorKombString.split(RedListUtil.EX);
155
            if(kombSplit.length!=2){
156
                RedListUtil.logMessage(id, "Multiple ex combination authors found", logger);
157
            }
158
            for (int i = 0; i < kombSplit.length; i++) {
159
                if(i==0){
160
                    //first author is ex author
161
                    TeamOrPersonBase authorKomb = (TeamOrPersonBase) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, kombSplit[i]);
162
                    name.setExCombinationAuthorship(authorKomb);
163
                }
164
                else{
165
                    TeamOrPersonBase authorKomb = (TeamOrPersonBase) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, kombSplit[i]);
166
                    name.setCombinationAuthorship(authorKomb);
167
                }
168
            }
169
        }
170
        else if(authorKombString.trim().contains(RedListUtil.AUCT)){
171
            RedListUtil.logMessage(id, "AUCT information in "+RedListUtil.AUTOR_KOMB+" column", logger);
172
        }
173
        else if(CdmUtils.isNotBlank(authorKombString)){
174
            TeamOrPersonBase authorKomb = (TeamOrPersonBase) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, authorKombString);
175
            name.setCombinationAuthorship(authorKomb);
176
        }
177
        //basionym author
178
        if(authorBasiString.contains(RedListUtil.EX)){
179
            String[] basiSplit = authorBasiString.split(RedListUtil.EX);
180
            for (int i = 0; i < basiSplit.length; i++) {
181
                if(basiSplit.length!=2){
182
                    RedListUtil.logMessage(id, "Multiple ex basionymn authors found", logger);
183
                }
184
                if(i==0){
185
                    TeamOrPersonBase authorBasi= (TeamOrPersonBase) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, basiSplit[i]);
186
                    if(CdmUtils.isBlank(authorKombString)){
187
                        name.setExCombinationAuthorship(authorBasi);
188
                    }
189
                    else{
190
                        name.setExBasionymAuthorship(authorBasi);
191
                    }
192
                }
193
                else{
194
                    TeamOrPersonBase authorBasi= (TeamOrPersonBase) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, basiSplit[i]);
195
                    if(CdmUtils.isBlank(authorKombString)){
196
                        name.setCombinationAuthorship(authorBasi);
197
                    }
198
                    else{
199
                        name.setBasionymAuthorship(authorBasi);
200
                    }
201
                }
202
            }
203
        }
204
        else if(CdmUtils.isNotBlank(authorBasiString)){
205
            //this seems to be a convention in the source database: When there is only a single author then only the "AUTOR_BASI" column is used
206
            TeamOrPersonBase authorBasi= (TeamOrPersonBase) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, authorBasiString);
207
            if(CdmUtils.isBlank(authorKombString)){
208
                name.setCombinationAuthorship(authorBasi);
209
            }
210
            else{
211
                name.setBasionymAuthorship(authorBasi);
212
            }
213
        }
214

    
215
        //check authorship consistency
216
        String authorString = rs.getString(RedListUtil.AUTOR);
217
        String authorshipCache = name.getAuthorshipCache();
218

    
219
        if(CdmUtils.isNotBlank(zusatzString)){
220
            authorString = authorString.replace(", "+zusatzString, "");
221
        }
222
        if(CdmUtils.isNotBlank(nomZusatzString)){
223
            authorString = authorString.replace(", "+nomZusatzString, "");
224
        }
225
        if(CdmUtils.isNotBlank(taxZusatzString)){
226
            authorString = authorString.replace(", "+taxZusatzString, "");
227
        }
228
        if(authorString.equals(RedListUtil.AUCT)){
229
            authorString = "";
230
        }
231
        if(!authorString.equals(authorshipCache)){
232
            RedListUtil.logMessage(id, "Authorship inconsistent! name.authorhshipCache <-> Column "+RedListUtil.AUTOR+": "+authorshipCache+" <-> "+authorString, logger);
233
        }
234

    
235
        //id
236
        ImportHelper.setOriginalSource(name, state.getTransactionalSourceReference(), id, RedListUtil.NAME_NAMESPACE);
237
        state.getNameMap().put(id, name.getUuid());
238

    
239
        namesToSave.add(name);
240

    
241
        //---TAXON---
242
        TaxonBase taxonBase = null;
243
        if(authorBasiString.trim().contains(RedListUtil.AUCT)){
244
            taxonBase = Taxon.NewInstance(name, null);
245
            taxonBase.setAppendedPhrase(RedListUtil.AUCT);
246
        }
247
        else if(gueltString.equals(RedListUtil.GUELT_ACCEPTED_TAXON)){
248
            taxonBase = Taxon.NewInstance(name, null);
249
        }
250
        else if(gueltString.equals(RedListUtil.GUELT_SYNONYM) || gueltString.equals(RedListUtil.GUELT_BASIONYM)){
251
            taxonBase = Synonym.NewInstance(name, null);
252
        }
253
        if(taxonBase==null){
254
            RedListUtil.logMessage(id, "Taxon for name "+name+" could not be created.", logger);
255
            return;
256
        }
257

    
258
        //check taxon name consistency
259
        if(taxNameString.endsWith("agg.")){
260
            taxNameString = taxNameString.replace("agg.", "aggr.");
261
        }
262
        String nameCache = ((BotanicalName)taxonBase.getName()).getNameCache().trim();
263
        if(!taxNameString.trim().equals(nameCache)){
264
            RedListUtil.logMessage(id, "Taxon name inconsistent! taxon.titleCache <-> Column "+RedListUtil.TAXNAME+": "+nameCache+" <-> "+taxNameString, logger);
265
        }
266

    
267

    
268
        taxaToSave.add(taxonBase);
269

    
270
        //id
271
        ImportHelper.setOriginalSource(taxonBase, state.getTransactionalSourceReference(), id, RedListUtil.TAXON_GESAMTLISTE_NAMESPACE);
272
        state.getTaxonMap().put(id, taxonBase.getUuid());
273

    
274
        /*check if taxon/synonym is also in checklist
275
         * 1. create new taxon with the same name (in the checklist classification)
276
         * 2. create congruent concept relationship between both
277
         */
278
        String clTaxonString = rs.getString(RedListUtil.CL_TAXON);
279
        if(CdmUtils.isNotBlank(clTaxonString) && !clTaxonString.trim().equals("-")){
280
            TaxonBase clone = (TaxonBase) taxonBase.clone();
281
            clone.setName(name);
282
            if(taxonBase.isInstanceOf(Taxon.class)){
283
                TaxonRelationship taxonRelation = ((Taxon) taxonBase).addTaxonRelation((Taxon) clone, TaxonRelationshipType.CONGRUENT_TO(), null, null);
284
                taxonRelation.setDoubtful(true);//TODO Ist das mit " mit Fragezeichen" gemeint?
285
            }
286
            ImportHelper.setOriginalSource(clone, state.getTransactionalSourceReference(), id, RedListUtil.TAXON_CHECKLISTE_NAMESPACE);
287
            state.getTaxonMap().put(id, clone.getUuid());
288
            taxaToSave.add(clone);
289
        }
290

    
291
    }
292

    
293
    private Rank makeRank(long id, RedListGefaesspflanzenImportState state, String rankStr) {
294
        Rank rank = null;
295
        try {
296
            rank = state.getTransformer().getRankByKey(rankStr);
297
        } catch (UndefinedTransformerMethodException e) {
298
            e.printStackTrace();
299
        }
300
        if(rank==null){
301
            RedListUtil.logMessage(id, rankStr+" could not be associated to a known rank.", logger);
302
        }
303
        return rank;
304
    }
305

    
306
    private NomenclaturalStatusType makeNomenclaturalStatus(long id, RedListGefaesspflanzenImportState state, String nomZusatzString) {
307
        NomenclaturalStatusType status = null;
308
        try {
309
            status = state.getTransformer().getNomenclaturalStatusByKey(nomZusatzString);
310
        } catch (UndefinedTransformerMethodException e) {
311
            e.printStackTrace();
312
        }
313
        if(status==null){
314
            RedListUtil.logMessage(id, nomZusatzString+" could not be associated to a known nomenclatural status.", logger);
315
        }
316
        return status;
317
    }
318

    
319

    
320

    
321
    @Override
322
    public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
323
            RedListGefaesspflanzenImportState state) {
324
        Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
325
        Map<String, AgentBase<?>> authorMap = new HashMap<String, AgentBase<?>>();
326

    
327
        try {
328
            while (rs.next()){
329
                String authorKombString = rs.getString(RedListUtil.AUTOR_KOMB);
330

    
331
                if(authorKombString.contains(RedListUtil.EX)){
332
                    String[] kombSplit = authorKombString.split(RedListUtil.EX);
333
                    for (int i = 0; i < kombSplit.length; i++) {
334
                        if(!authorMap.containsKey(kombSplit[i])){
335
                            authorMap.put(kombSplit[i], getAgentService().load(state.getAuthorMap().get(kombSplit[i])));
336
                        }
337
                    }
338
                }
339
                else if(CdmUtils.isNotBlank(authorKombString) && !authorMap.containsKey(authorKombString)){
340
                    authorMap.put(authorKombString, getAgentService().load(state.getAuthorMap().get(authorKombString)));
341
                }
342

    
343
                String authorBasiString = rs.getString(RedListUtil.AUTOR_BASI);
344
                //basionym author
345
                if(authorBasiString.contains(RedListUtil.EX)){
346
                    String[] basiSplit = authorBasiString.split(RedListUtil.EX);
347
                    for (int i = 0; i < basiSplit.length; i++) {
348
                        if(!authorMap.containsKey(basiSplit[i])){
349
                            authorMap.put(basiSplit[i], getAgentService().load(state.getAuthorMap().get(basiSplit[i])));
350
                        }
351
                    }
352
                }
353
                else if(CdmUtils.isNotBlank(authorBasiString) && !authorMap.containsKey(authorBasiString)){
354
                    authorMap.put(authorBasiString, getAgentService().load(state.getAuthorMap().get(authorBasiString)));
355
                }
356
            }
357
        } catch (SQLException e) {
358
            e.printStackTrace();
359
        }
360
        result.put(RedListUtil.AUTHOR_NAMESPACE, authorMap);
361

    
362
        return result;
363
    }
364

    
365
    @Override
366
    protected boolean doCheck(RedListGefaesspflanzenImportState state) {
367
        return false;
368
    }
369

    
370
    @Override
371
    protected boolean isIgnore(RedListGefaesspflanzenImportState state) {
372
        return false;
373
    }
374

    
375
}
(4-4/7)