Project

General

Profile

Download (27.3 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.AgentBase;
30
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
31
import eu.etaxonomy.cdm.model.common.CdmBase;
32
import eu.etaxonomy.cdm.model.common.Language;
33
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
34
import eu.etaxonomy.cdm.model.description.TaxonDescription;
35
import eu.etaxonomy.cdm.model.name.BotanicalName;
36
import eu.etaxonomy.cdm.model.name.NomenclaturalStatus;
37
import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
38
import eu.etaxonomy.cdm.model.name.NonViralName;
39
import eu.etaxonomy.cdm.model.name.Rank;
40
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
41
import eu.etaxonomy.cdm.model.taxon.Synonym;
42
import eu.etaxonomy.cdm.model.taxon.Taxon;
43
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
44
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
45
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
46
import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
47

    
48
/**
49
 *
50
 * @author pplitzner
51
 * @date Mar 1, 2016
52
 *
53
 */
54

    
55
@Component
56
@SuppressWarnings("serial")
57
public class RedListGefaesspflanzenImportNames extends DbImportBase<RedListGefaesspflanzenImportState, RedListGefaesspflanzenImportConfigurator> {
58

    
59
    private static final Logger logger = Logger.getLogger(RedListGefaesspflanzenImportNames.class);
60

    
61
    private static final String tableName = "Rote Liste Gefäßpflanzen";
62

    
63
    private static final String pluralString = "names";
64

    
65
    public RedListGefaesspflanzenImportNames() {
66
        super(tableName, pluralString);
67
    }
68

    
69
    @Override
70
    protected String getIdQuery(RedListGefaesspflanzenImportState state) {
71
        return "SELECT NAMNR "
72
                + "FROM V_TAXATLAS_D20_EXPORT t "
73
                + " ORDER BY NAMNR";
74
    }
75

    
76
    @Override
77
    protected String getRecordQuery(RedListGefaesspflanzenImportConfigurator config) {
78
        String result = " SELECT * "
79
                + " FROM V_TAXATLAS_D20_EXPORT t "
80
                + " WHERE t.NAMNR IN (@IDSET)";
81
        result = result.replace("@IDSET", IPartitionedIO.ID_LIST_TOKEN);
82
        return result;
83
    }
84

    
85
    @Override
86
    protected void doInvoke(RedListGefaesspflanzenImportState state) {
87
        super.doInvoke(state);
88
    }
89

    
90

    
91
    @Override
92
    public boolean doPartition(ResultSetPartitioner partitioner, RedListGefaesspflanzenImportState state) {
93
        ResultSet rs = partitioner.getResultSet();
94
        Set<TaxonNameBase> namesToSave = new HashSet<TaxonNameBase>();
95
        Set<TaxonBase> taxaToSave = new HashSet<TaxonBase>();
96
        try {
97
            while (rs.next()){
98
                makeSingleNameAndTaxon(state, rs, namesToSave, taxaToSave);
99

    
100
            }
101
        } catch (SQLException e) {
102
            e.printStackTrace();
103
        }
104

    
105
        getNameService().saveOrUpdate(namesToSave);
106
        getTaxonService().saveOrUpdate(taxaToSave);
107
        return true;
108
    }
109

    
110
    private void makeSingleNameAndTaxon(RedListGefaesspflanzenImportState state, ResultSet rs, Set<TaxonNameBase> namesToSave, Set<TaxonBase> taxaToSave)
111
            throws SQLException {
112
        long id = rs.getLong(RedListUtil.NAMNR);
113
        String clTaxonString = rs.getString(RedListUtil.CL_TAXON);
114
        String relationE = rs.getString(RedListUtil.E);
115
        String relationW = rs.getString(RedListUtil.W);
116
        String relationK = rs.getString(RedListUtil.K);
117
        String relationAW = rs.getString(RedListUtil.AW);
118
        String relationAO = rs.getString(RedListUtil.AO);
119
        String relationR = rs.getString(RedListUtil.R);
120
        String relationO = rs.getString(RedListUtil.O);
121
        String relationS = rs.getString(RedListUtil.S);
122

    
123
        //---NAME---
124
        NonViralName<?> name = importName(state, rs, namesToSave);
125

    
126

    
127
        //--- AUTHORS ---
128
        importAuthors(state, rs, name);
129

    
130
        //---TAXON---
131
        TaxonBase<?> taxonBase = importTaxon(rs, name);
132
        if(taxonBase==null){
133
            RedListUtil.logMessage(id, "Taxon for name "+name+" could not be created.", logger);
134
            return;
135
        }
136

    
137
        //---CONCEPT RELATIONSHIPS---
138
        //checklist
139
        TaxonBase<?> checklistTaxon = null;
140
        if(CdmUtils.isNotBlank(clTaxonString) && !clTaxonString.trim().equals("-")){
141
            checklistTaxon = (TaxonBase<?>) taxonBase.clone();
142
            if(checklistTaxon.isInstanceOf(Taxon.class)){
143
                TaxonRelationship relation = HibernateProxyHelper.deproxy(checklistTaxon, Taxon.class).addTaxonRelation(HibernateProxyHelper.deproxy(taxonBase, Taxon.class), TaxonRelationshipType.CONGRUENT_TO(), null, null);
144
                relation.setDoubtful(true);
145
            }
146

    
147
            ImportHelper.setOriginalSource(checklistTaxon, state.getTransactionalSourceReference(), id, RedListUtil.TAXON_CHECKLISTE_NAMESPACE);
148
            taxaToSave.add(checklistTaxon);
149
        }
150
        //E, W, K, AW, AO, R, O, S
151
        addConceptRelation(relationE, RedListUtil.CLASSIFICATION_NAMESPACE_E, taxonBase, checklistTaxon, taxaToSave, id, state);
152
        addConceptRelation(relationW, RedListUtil.CLASSIFICATION_NAMESPACE_W, taxonBase, checklistTaxon, taxaToSave, id, state);
153
        addConceptRelation(relationK, RedListUtil.CLASSIFICATION_NAMESPACE_K, taxonBase, checklistTaxon, taxaToSave, id, state);
154
        addConceptRelation(relationAW, RedListUtil.CLASSIFICATION_NAMESPACE_AW, taxonBase, checklistTaxon, taxaToSave, id, state);
155
        addConceptRelation(relationAO, RedListUtil.CLASSIFICATION_NAMESPACE_AO, taxonBase, checklistTaxon, taxaToSave, id, state);
156
        addConceptRelation(relationR, RedListUtil.CLASSIFICATION_NAMESPACE_R, taxonBase, checklistTaxon, taxaToSave, id, state);
157
        addConceptRelation(relationO, RedListUtil.CLASSIFICATION_NAMESPACE_O, taxonBase, checklistTaxon, taxaToSave, id, state);
158
        addConceptRelation(relationS, RedListUtil.CLASSIFICATION_NAMESPACE_S, taxonBase, checklistTaxon, taxaToSave, id, state);
159

    
160
        //NOTE: the source has to be added after cloning or otherwise the clone would also get the source
161
        ImportHelper.setOriginalSource(taxonBase, state.getTransactionalSourceReference(), id, RedListUtil.TAXON_GESAMTLISTE_NAMESPACE);
162
        taxaToSave.add(taxonBase);
163
    }
164

    
165
    private void addConceptRelation(String relationString, String classificationNamespace, TaxonBase<?> gesamtListeTaxon, TaxonBase<?> checkListenTaxon, Set<TaxonBase> taxaToSave, long id, RedListGefaesspflanzenImportState state){
166
        if(CdmUtils.isNotBlank(relationString) && !relationString.equals(".")){
167
            String substring = relationString.substring(relationString.length()-1, relationString.length());
168
            TaxonRelationshipType taxonRelationshipTypeByKey = new RedListGefaesspflanzenTransformer().getTaxonRelationshipTypeByKey(substring);
169
            if(taxonRelationshipTypeByKey==null){
170
                RedListUtil.logMessage(id, "Could not interpret relationship "+relationString+" for taxon "+gesamtListeTaxon.generateTitle(), logger);
171
            }
172
            //there is no type "included in" so we have to reverse the direction
173
            if(substring.equals("<")){
174
                cloneTaxon(gesamtListeTaxon, checkListenTaxon, taxonRelationshipTypeByKey, taxaToSave, id, classificationNamespace, true, false, state);
175
            }
176
            else{
177
                cloneTaxon(gesamtListeTaxon, checkListenTaxon, taxonRelationshipTypeByKey, taxaToSave, id, classificationNamespace, false, false, state);
178
            }
179
        }
180
    }
181

    
182
    /**
183
     * 1. clone new taxon of gesamtListeTaxon with the same name (in that classification)<br>
184
     * 2. create concept relationship from clone to gesamtListeTaxon/checklisteTaxon or from its accepted taxon if it is synonym<br>
185
     *<br>
186
     * <b>NOTE:</b> the {@link TaxonRelationshipType} passed as parameter is
187
     * directed <b>from the clone</b> to the taxon.<br>
188
     * This can be changed with parameter <i>reverseRelation</i>
189
     * @return cloned taxon
190
     */
191
    private Taxon cloneTaxon(final TaxonBase<?> gesamtListeTaxon, final TaxonBase<?> checklisteTaxon, TaxonRelationshipType relationFromCloneToTaxon, Set<TaxonBase> taxaToSave, long id, String sourceNameSpace, boolean reverseRelation, boolean doubtful, RedListGefaesspflanzenImportState state){
192
        Taxon acceptedGesamtListeTaxon = getAcceptedTaxon(gesamtListeTaxon);
193
        Taxon acceptedChecklistTaxon = getAcceptedTaxon(checklisteTaxon);
194
        Taxon clonedTaxon = null;
195

    
196
        if(gesamtListeTaxon.isInstanceOf(Taxon.class)){
197
            clonedTaxon = HibernateProxyHelper.deproxy(gesamtListeTaxon.clone(), Taxon.class);
198
        }
199
        else if(gesamtListeTaxon.isInstanceOf(Synonym.class)){
200
            clonedTaxon = Taxon.NewInstance(gesamtListeTaxon.getName(), gesamtListeTaxon.getSec());
201
        }
202
        else{
203
            RedListUtil.logMessage(id, "Taxon base "+gesamtListeTaxon+" is neither taxon nor synonym! Taxon could not be cloned", logger);
204
            return null;
205
        }
206

    
207
        if(reverseRelation){
208
            if(acceptedGesamtListeTaxon!=null){
209
                TaxonRelationship taxonRelation = acceptedGesamtListeTaxon.addTaxonRelation(clonedTaxon, relationFromCloneToTaxon, null, null);
210
                taxonRelation.setDoubtful(doubtful);
211
            }
212
            if(acceptedChecklistTaxon!=null) {
213
                TaxonRelationship taxonRelation = acceptedChecklistTaxon.addTaxonRelation(clonedTaxon, relationFromCloneToTaxon, null, null);
214
                taxonRelation.setDoubtful(doubtful);
215
            }
216
        }
217
        else {
218
            if(acceptedGesamtListeTaxon!=null){
219
                TaxonRelationship taxonRelation = clonedTaxon.addTaxonRelation(acceptedGesamtListeTaxon, relationFromCloneToTaxon, null, null);
220
                taxonRelation.setDoubtful(doubtful);
221
            }
222
            if(acceptedChecklistTaxon!=null) {
223
                TaxonRelationship taxonRelation = clonedTaxon.addTaxonRelation(acceptedChecklistTaxon, relationFromCloneToTaxon, null, null);
224
                taxonRelation.setDoubtful(doubtful);
225
            }
226
        }
227

    
228
        ImportHelper.setOriginalSource(clonedTaxon, state.getTransactionalSourceReference(), id, sourceNameSpace);
229
        taxaToSave.add(clonedTaxon);
230
        return clonedTaxon;
231
    }
232

    
233
    private TaxonBase<?> importTaxon(ResultSet rs, NonViralName<?> name) throws SQLException {
234

    
235
        long id = rs.getLong(RedListUtil.NAMNR);
236
        String taxNameString = rs.getString(RedListUtil.TAXNAME);
237
        String gueltString = rs.getString(RedListUtil.GUELT);
238
        String trivialString = rs.getString(RedListUtil.TRIVIAL);
239
        String authorBasiString = rs.getString(RedListUtil.AUTOR_BASI);
240
        String hybString = rs.getString(RedListUtil.HYB);
241

    
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
        else{
254
            return null;
255
        }
256

    
257
        //common name
258
        if(taxonBase.isInstanceOf(Taxon.class) && trivialString!=null){
259
            Taxon taxon = HibernateProxyHelper.deproxy(taxonBase, Taxon.class);
260
            TaxonDescription description = TaxonDescription.NewInstance(taxon);
261
            description.addElement(CommonTaxonName.NewInstance(trivialString, Language.getDefaultLanguage()));
262
        }
263

    
264
        //check taxon name consistency
265
        checkTaxonNameConsistency(id, taxNameString, hybString, taxonBase);
266
        return taxonBase;
267
    }
268

    
269
    private void importAuthors(RedListGefaesspflanzenImportState state, ResultSet rs, NonViralName<?> name) throws SQLException {
270

    
271
        long id = rs.getLong(RedListUtil.NAMNR);
272
        String nomZusatzString = rs.getString(RedListUtil.NOM_ZUSATZ);
273
        String taxZusatzString = rs.getString(RedListUtil.TAX_ZUSATZ);
274
        String zusatzString = rs.getString(RedListUtil.ZUSATZ);
275
        String authorKombString = rs.getString(RedListUtil.AUTOR_KOMB);
276
        String authorBasiString = rs.getString(RedListUtil.AUTOR_BASI);
277

    
278
        //combination author
279
        if(authorKombString.contains(RedListUtil.EX)){
280
            //TODO: what happens with multiple ex authors??
281
            String[] kombSplit = authorKombString.split(RedListUtil.EX);
282
            if(kombSplit.length!=2){
283
                RedListUtil.logMessage(id, "Multiple ex combination authors found", logger);
284
            }
285
            for (int i = 0; i < kombSplit.length; i++) {
286
                if(i==0){
287
                    //first author is ex author
288
                    TeamOrPersonBase<?> authorKomb = (TeamOrPersonBase<?>) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, kombSplit[i]);
289
                    name.setExCombinationAuthorship(authorKomb);
290
                }
291
                else{
292
                    TeamOrPersonBase<?> authorKomb = (TeamOrPersonBase<?>) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, kombSplit[i]);
293
                    name.setCombinationAuthorship(authorKomb);
294
                }
295
            }
296
        }
297
        else if(authorKombString.trim().contains(RedListUtil.AUCT)){
298
            RedListUtil.logMessage(id, "AUCT information in "+RedListUtil.AUTOR_KOMB+" column", logger);
299
        }
300
        else if(CdmUtils.isNotBlank(authorKombString)){
301
            TeamOrPersonBase<?> authorKomb = (TeamOrPersonBase<?>) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, authorKombString);
302
            name.setCombinationAuthorship(authorKomb);
303
        }
304
        //basionym author
305
        if(authorBasiString.contains(RedListUtil.EX)){
306
            String[] basiSplit = authorBasiString.split(RedListUtil.EX);
307
            for (int i = 0; i < basiSplit.length; i++) {
308
                if(basiSplit.length!=2){
309
                    RedListUtil.logMessage(id, "Multiple ex basionymn authors found", logger);
310
                }
311
                if(i==0){
312
                    TeamOrPersonBase<?> authorBasi= (TeamOrPersonBase<?>) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, basiSplit[i]);
313
                    if(CdmUtils.isBlank(authorKombString)){
314
                        name.setExCombinationAuthorship(authorBasi);
315
                    }
316
                    else{
317
                        name.setExBasionymAuthorship(authorBasi);
318
                    }
319
                }
320
                else{
321
                    TeamOrPersonBase<?> authorBasi= (TeamOrPersonBase<?>) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, basiSplit[i]);
322
                    if(CdmUtils.isBlank(authorKombString)){
323
                        name.setCombinationAuthorship(authorBasi);
324
                    }
325
                    else{
326
                        name.setBasionymAuthorship(authorBasi);
327
                    }
328
                }
329
            }
330
        }
331
        else if(CdmUtils.isNotBlank(authorBasiString)){
332
            //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
333
            TeamOrPersonBase<?> authorBasi= (TeamOrPersonBase<?>) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, authorBasiString);
334
            if(CdmUtils.isBlank(authorKombString)){
335
                name.setCombinationAuthorship(authorBasi);
336
            }
337
            else{
338
                name.setBasionymAuthorship(authorBasi);
339
            }
340
        }
341

    
342
        //check authorship consistency
343
        String authorString = rs.getString(RedListUtil.AUTOR);
344
        String authorshipCache = name.getAuthorshipCache();
345
        checkAuthorShipConsistency(id, nomZusatzString, taxZusatzString, zusatzString, authorString, authorshipCache);
346
    }
347

    
348
    private NonViralName<?> importName(RedListGefaesspflanzenImportState state, ResultSet rs, Set<TaxonNameBase> namesToSave) throws SQLException {
349

    
350
        long id = rs.getLong(RedListUtil.NAMNR);
351
        String taxNameString = rs.getString(RedListUtil.TAXNAME);
352
        String rangString = rs.getString(RedListUtil.RANG);
353
        String ep1String = rs.getString(RedListUtil.EPI1);
354
        String ep2String = rs.getString(RedListUtil.EPI2);
355
        String ep3String = rs.getString(RedListUtil.EPI3);
356
        String nomZusatzString = rs.getString(RedListUtil.NOM_ZUSATZ);
357
        String hybString = rs.getString(RedListUtil.HYB);
358

    
359
        if(CdmUtils.isBlank(taxNameString) && CdmUtils.isBlank(ep1String)){
360
            RedListUtil.logMessage(id, "No name found!", logger);
361
        }
362

    
363
        Rank rank = makeRank(id, state, rangString, ep3String!=null);
364
        NonViralName<?> name = BotanicalName.NewInstance(rank);
365

    
366
        //ep1 should always be present
367
        if(CdmUtils.isBlank(ep1String)){
368
            RedListUtil.logMessage(id, RedListUtil.EPI1+" is empty!", logger);
369
        }
370
        name.setGenusOrUninomial(ep1String);
371
        if(CdmUtils.isNotBlank(ep2String)){
372
            if(rank!=null && rank.isInfraGeneric()){
373
                name.setInfraGenericEpithet(ep2String);
374
            }
375
            else{
376
                name.setSpecificEpithet(ep2String);
377
            }
378
        }
379
        if(CdmUtils.isNotBlank(ep3String)){
380
            name.setInfraSpecificEpithet(ep3String);
381
        }
382
        //nomenclatural status
383
        if(CdmUtils.isNotBlank(nomZusatzString)){
384
            NomenclaturalStatusType status = makeNomenclaturalStatus(id, state, nomZusatzString);
385
            if(status!=null){
386
                name.addStatus(NomenclaturalStatus.NewInstance(status));
387
            }
388
        }
389
        //hybrid
390
        if(CdmUtils.isNotBlank(hybString)){
391
            if(hybString.equals(RedListUtil.HYB_X)){
392
                name.setBinomHybrid(true);
393
            }
394
            else if(hybString.equals(RedListUtil.HYB_G)){
395
                name.setMonomHybrid(true);
396
            }
397
            else if(hybString.equals(RedListUtil.HYB_XF)){
398
                name.setHybridFormula(true);
399
                if(ep1String.contains(RedListUtil.HYB_SIGN)){
400
                    RedListUtil.logMessage(id, "EPI1 has hybrid signs but with flag: "+RedListUtil.HYB_XF, logger);
401
                }
402
                else if(ep2String.contains(RedListUtil.HYB_SIGN)){
403
                    String[] split = ep2String.split(RedListUtil.HYB_SIGN);
404
                    if(split.length!=2){
405
                        RedListUtil.logMessage(id, "Multiple hybrid signs found in "+ep2String, logger);
406
                    }
407
                    String hybridFormula1 = ep1String+" "+split[0].trim();
408
                    String hybridFormula2 = ep1String+" "+split[1].trim();
409
                    if(CdmUtils.isNotBlank(ep3String)){
410
                        hybridFormula1 += " "+ep3String;
411
                        hybridFormula2 += " "+ep3String;
412
                    }
413
                    String fullFormula = hybridFormula1+" "+RedListUtil.HYB_SIGN+" "+hybridFormula2;
414
                    name = NonViralNameParserImpl.NewInstance().parseFullName(fullFormula);
415
                }
416
                else if(ep3String.contains(RedListUtil.HYB_SIGN)){
417
                    String[] split = ep3String.split(RedListUtil.HYB_SIGN);
418
                    if(split.length!=2){
419
                        RedListUtil.logMessage(id, "Multiple hybrid signs found in "+ep3String, logger);
420
                    }
421
                    String hybridFormula1 = ep1String+" "+ep2String+" "+split[0];
422
                    String hybridFormula2 = ep1String+" "+ep2String+" "+split[1];
423
                    String fullFormula = hybridFormula1+" "+RedListUtil.HYB_SIGN+" "+hybridFormula2;
424
                    name = NonViralNameParserImpl.NewInstance().parseFullName(fullFormula);
425
                }
426
            }
427
            else if(hybString.equals(RedListUtil.HYB_N)){
428
                name = NonViralNameParserImpl.NewInstance().parseFullName(ep1String+" "+ep2String+" nothosubsp. "+ep3String);
429
            }
430
            else if(hybString.equals(RedListUtil.HYB_GF)){
431
                if(ep1String.contains(RedListUtil.HYB_SIGN)){
432
                    name = NonViralNameParserImpl.NewInstance().parseFullName(ep1String);
433
                }
434
                else{
435
                    RedListUtil.logMessage(id, "HYB is "+hybString+" but "+RedListUtil.HYB+" does not contain "+RedListUtil.HYB_SIGN, logger);
436
                }
437
            }
438
            else{
439
                logger.error("HYB value "+hybString+" not yet handled");
440
            }
441
        }
442
        //add source
443
        ImportHelper.setOriginalSource(name, state.getTransactionalSourceReference(), id, RedListUtil.NAME_NAMESPACE);
444

    
445
        namesToSave.add(name);
446
        return name;
447
    }
448

    
449
    private void checkAuthorShipConsistency(long id, String nomZusatzString, String taxZusatzString,
450
            String zusatzString, String authorString, String authorshipCache) {
451
        if(CdmUtils.isNotBlank(zusatzString)){
452
            authorString = authorString.replace(", "+zusatzString, "");
453
        }
454
        if(CdmUtils.isNotBlank(nomZusatzString)){
455
            authorString = authorString.replace(", "+nomZusatzString, "");
456
        }
457
        if(CdmUtils.isNotBlank(taxZusatzString)){
458
            authorString = authorString.replace(", "+taxZusatzString, "");
459
        }
460
        if(authorString.equals(RedListUtil.AUCT)){
461
            authorString = "";
462
        }
463
        if(!authorString.equals(authorshipCache)){
464
            RedListUtil.logMessage(id, "Authorship inconsistent! name.authorhshipCache <-> Column "+RedListUtil.AUTOR+": "+authorshipCache+" <-> "+authorString, logger);
465
        }
466
    }
467

    
468
    private void checkTaxonNameConsistency(long id, String taxNameString, String hybString, TaxonBase<?> taxonBase) {
469
        if(hybString.equals(RedListUtil.HYB_XF)){
470
            if(HibernateProxyHelper.deproxy(taxonBase.getName(),NonViralName.class).getHybridChildRelations().isEmpty()){
471
                RedListUtil.logMessage(id, "Hybrid name but no hybrid child relations", logger);
472
                return;
473
            }
474
            return;
475
        }
476

    
477

    
478
        String nameCache = HibernateProxyHelper.deproxy(taxonBase.getName(), NonViralName.class).getNameCache().trim();
479

    
480
        if(taxNameString.endsWith("agg.")){
481
            taxNameString = taxNameString.replace("agg.", "aggr.");
482
        }
483
        if(hybString.equals(RedListUtil.HYB_X)){
484
            taxNameString = taxNameString.replace(RedListUtil.HYB_SIGN+" ", RedListUtil.HYB_SIGN);//hybrid sign has no space after it in titleCache for binomial hybrids
485
        }
486
        if(taxNameString.endsWith("- Gruppe")){
487
            taxNameString = taxNameString.replaceAll("- Gruppe", "species group");
488
        }
489
        if(taxNameString.endsWith("- group")){
490
            taxNameString = taxNameString.replaceAll("- group", "species group");
491
        }
492
        taxNameString = taxNameString.replace("[ranglos]", "[unranked]");
493
        if(!taxNameString.trim().equals(nameCache)){
494
            RedListUtil.logMessage(id, "Taxon name inconsistent! taxon.titleCache <-> Column "+RedListUtil.TAXNAME+": "+nameCache+" <-> "+taxNameString, logger);
495
        }
496
    }
497

    
498
    private Rank makeRank(long id, RedListGefaesspflanzenImportState state, String rankStr, boolean hasSpecificEpithet) {
499
        Rank rank = null;
500
        try {
501
            if(rankStr.equals("ORA")){
502
                //special handling for ORA because of two possibilities
503
                if(hasSpecificEpithet){
504
                    return Rank.UNRANKED_INFRASPECIFIC();
505
                }
506
                else{
507
                    return Rank.UNRANKED_INFRAGENERIC();
508
                }
509
            }
510
            else{
511
                rank = state.getTransformer().getRankByKey(rankStr);
512
            }
513
        } catch (UndefinedTransformerMethodException e) {
514
            e.printStackTrace();
515
        }
516
        if(rank==null){
517
            RedListUtil.logMessage(id, rankStr+" could not be associated to a known rank.", logger);
518
        }
519
        return rank;
520
    }
521

    
522
    private NomenclaturalStatusType makeNomenclaturalStatus(long id, RedListGefaesspflanzenImportState state, String nomZusatzString) {
523
        NomenclaturalStatusType status = null;
524
        try {
525
            status = state.getTransformer().getNomenclaturalStatusByKey(nomZusatzString);
526
        } catch (UndefinedTransformerMethodException e) {
527
            e.printStackTrace();
528
        }
529
        if(status==null){
530
            RedListUtil.logMessage(id, nomZusatzString+" could not be associated to a known nomenclatural status.", logger);
531
        }
532
        return status;
533
    }
534

    
535

    
536

    
537
    @Override
538
    public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
539
            RedListGefaesspflanzenImportState state) {
540
        Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
541
        Map<String, AgentBase<?>> authorMap = new HashMap<String, AgentBase<?>>();
542

    
543
        try {
544
            while (rs.next()){
545
                String authorKombString = rs.getString(RedListUtil.AUTOR_KOMB);
546

    
547
                if(authorKombString.contains(RedListUtil.EX)){
548
                    String[] kombSplit = authorKombString.split(RedListUtil.EX);
549
                    for (int i = 0; i < kombSplit.length; i++) {
550
                        if(!authorMap.containsKey(kombSplit[i])){
551
                            authorMap.put(kombSplit[i], getAgentService().load(state.getAuthorMap().get(kombSplit[i])));
552
                        }
553
                    }
554
                }
555
                else if(CdmUtils.isNotBlank(authorKombString) && !authorMap.containsKey(authorKombString)){
556
                    authorMap.put(authorKombString, getAgentService().load(state.getAuthorMap().get(authorKombString)));
557
                }
558

    
559
                String authorBasiString = rs.getString(RedListUtil.AUTOR_BASI);
560
                //basionym author
561
                if(authorBasiString.contains(RedListUtil.EX)){
562
                    String[] basiSplit = authorBasiString.split(RedListUtil.EX);
563
                    for (int i = 0; i < basiSplit.length; i++) {
564
                        if(!authorMap.containsKey(basiSplit[i])){
565
                            authorMap.put(basiSplit[i], getAgentService().load(state.getAuthorMap().get(basiSplit[i])));
566
                        }
567
                    }
568
                }
569
                else if(CdmUtils.isNotBlank(authorBasiString) && !authorMap.containsKey(authorBasiString)){
570
                    authorMap.put(authorBasiString, getAgentService().load(state.getAuthorMap().get(authorBasiString)));
571
                }
572
            }
573
        } catch (SQLException e) {
574
            e.printStackTrace();
575
        }
576
        result.put(RedListUtil.AUTHOR_NAMESPACE, authorMap);
577

    
578
        return result;
579
    }
580

    
581
    @Override
582
    protected boolean doCheck(RedListGefaesspflanzenImportState state) {
583
        return false;
584
    }
585

    
586
    @Override
587
    protected boolean isIgnore(RedListGefaesspflanzenImportState state) {
588
        return false;
589
    }
590

    
591
}
(4-4/7)