Project

General

Profile

Download (29.2 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
import java.util.regex.Matcher;
19
import java.util.regex.Pattern;
20

    
21
import org.apache.log4j.Logger;
22
import org.springframework.stereotype.Component;
23

    
24
import eu.etaxonomy.cdm.common.CdmUtils;
25
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
26
import eu.etaxonomy.cdm.io.common.DbImportBase;
27
import eu.etaxonomy.cdm.io.common.IPartitionedIO;
28
import eu.etaxonomy.cdm.io.common.ImportHelper;
29
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
30
import eu.etaxonomy.cdm.io.common.mapping.UndefinedTransformerMethodException;
31
import eu.etaxonomy.cdm.model.agent.AgentBase;
32
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
33
import eu.etaxonomy.cdm.model.common.Annotation;
34
import eu.etaxonomy.cdm.model.common.AnnotationType;
35
import eu.etaxonomy.cdm.model.common.CdmBase;
36
import eu.etaxonomy.cdm.model.common.Language;
37
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
38
import eu.etaxonomy.cdm.model.description.TaxonDescription;
39
import eu.etaxonomy.cdm.model.name.BotanicalName;
40
import eu.etaxonomy.cdm.model.name.CultivarPlantName;
41
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
42
import eu.etaxonomy.cdm.model.name.NomenclaturalStatus;
43
import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
44
import eu.etaxonomy.cdm.model.name.NonViralName;
45
import eu.etaxonomy.cdm.model.name.Rank;
46
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
47
import eu.etaxonomy.cdm.model.taxon.Synonym;
48
import eu.etaxonomy.cdm.model.taxon.Taxon;
49
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
50
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
51
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
52
import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
53

    
54
/**
55
 *
56
 * @author pplitzner
57
 * @date Mar 1, 2016
58
 *
59
 */
60

    
61
@Component
62
@SuppressWarnings("serial")
63
public class RedListGefaesspflanzenImportNames extends DbImportBase<RedListGefaesspflanzenImportState, RedListGefaesspflanzenImportConfigurator> {
64

    
65
    private static final Logger logger = Logger.getLogger(RedListGefaesspflanzenImportNames.class);
66

    
67
    private static final String tableName = "Rote Liste Gefäßpflanzen";
68

    
69
    private static final String pluralString = "names";
70

    
71
    private static final boolean STRICT_TITLE_CHECK = false;
72

    
73
    public RedListGefaesspflanzenImportNames() {
74
        super(tableName, pluralString);
75
    }
76

    
77
    @Override
78
    protected String getIdQuery(RedListGefaesspflanzenImportState state) {
79
        return "SELECT NAMNR "
80
                + "FROM V_TAXATLAS_D20_EXPORT t "
81
                + " ORDER BY NAMNR";
82
    }
83

    
84
    @Override
85
    protected String getRecordQuery(RedListGefaesspflanzenImportConfigurator config) {
86
        String result = " SELECT * "
87
                + " FROM V_TAXATLAS_D20_EXPORT t "
88
                + " WHERE t.NAMNR IN (@IDSET)";
89
        result = result.replace("@IDSET", IPartitionedIO.ID_LIST_TOKEN);
90
        return result;
91
    }
92

    
93
    @Override
94
    protected void doInvoke(RedListGefaesspflanzenImportState state) {
95
        super.doInvoke(state);
96
    }
97

    
98

    
99
    @Override
100
    public boolean doPartition(ResultSetPartitioner partitioner, RedListGefaesspflanzenImportState state) {
101
        ResultSet rs = partitioner.getResultSet();
102
        Set<TaxonNameBase> namesToSave = new HashSet<TaxonNameBase>();
103
        Set<TaxonBase> taxaToSave = new HashSet<TaxonBase>();
104
        try {
105
            while (rs.next()){
106
                makeSingleNameAndTaxon(state, rs, namesToSave, taxaToSave);
107

    
108
            }
109
        } catch (SQLException e) {
110
            e.printStackTrace();
111
        }
112

    
113
        getNameService().saveOrUpdate(namesToSave);
114
        getTaxonService().saveOrUpdate(taxaToSave);
115
        return true;
116
    }
117

    
118
    private void makeSingleNameAndTaxon(RedListGefaesspflanzenImportState state, ResultSet rs, Set<TaxonNameBase> namesToSave, Set<TaxonBase> taxaToSave)
119
            throws SQLException {
120
        long id = rs.getLong(RedListUtil.NAMNR);
121
        String clTaxonString = rs.getString(RedListUtil.CL_TAXON);
122
        String relationE = rs.getString(RedListUtil.E);
123
        String relationW = rs.getString(RedListUtil.W);
124
        String relationK = rs.getString(RedListUtil.K);
125
        String relationAW = rs.getString(RedListUtil.AW);
126
        String relationAO = rs.getString(RedListUtil.AO);
127
        String relationR = rs.getString(RedListUtil.R);
128
        String relationO = rs.getString(RedListUtil.O);
129
        String relationS = rs.getString(RedListUtil.S);
130

    
131
        //---NAME---
132
        NonViralName<?> name = importName(state, rs, namesToSave);
133

    
134

    
135
        //--- AUTHORS ---
136
        importAuthors(state, rs, name);
137

    
138
        //---TAXON---
139
        TaxonBase<?> taxonBase = importTaxon(rs, name);
140
        if(taxonBase==null){
141
            RedListUtil.logMessage(id, "Taxon for name "+name+" could not be created.", logger);
142
            return;
143
        }
144

    
145
        //---CONCEPT RELATIONSHIPS---
146
        //E, W, K, AW, AO, R, O, S
147
        cloneTaxon(taxonBase, relationE, RedListUtil.CLASSIFICATION_NAMESPACE_E, taxaToSave, id, state);
148
        cloneTaxon(taxonBase, relationW, RedListUtil.CLASSIFICATION_NAMESPACE_W, taxaToSave, id, state);
149
        cloneTaxon(taxonBase, relationK, RedListUtil.CLASSIFICATION_NAMESPACE_K, taxaToSave, id, state);
150
        cloneTaxon(taxonBase, relationAW, RedListUtil.CLASSIFICATION_NAMESPACE_AW, taxaToSave, id, state);
151
        cloneTaxon(taxonBase, relationAO, RedListUtil.CLASSIFICATION_NAMESPACE_AO, taxaToSave, id, state);
152
        cloneTaxon(taxonBase, relationR, RedListUtil.CLASSIFICATION_NAMESPACE_R, taxaToSave, id, state);
153
        cloneTaxon(taxonBase, relationO, RedListUtil.CLASSIFICATION_NAMESPACE_O, taxaToSave, id, state);
154
        cloneTaxon(taxonBase, relationS, RedListUtil.CLASSIFICATION_NAMESPACE_S, taxaToSave, id, state);
155
        //checklist
156
        TaxonBase<?> checklistTaxon = null;
157
        if(CdmUtils.isNotBlank(clTaxonString) && !clTaxonString.trim().equals("-")){
158
            checklistTaxon = (TaxonBase<?>) taxonBase.clone();
159
            if(checklistTaxon.isInstanceOf(Taxon.class)){
160
                TaxonRelationship relation = HibernateProxyHelper.deproxy(checklistTaxon, Taxon.class).addTaxonRelation(HibernateProxyHelper.deproxy(taxonBase, Taxon.class), TaxonRelationshipType.CONGRUENT_TO(), null, null);
161
                relation.setDoubtful(true);
162
            }
163

    
164
            ImportHelper.setOriginalSource(checklistTaxon, state.getTransactionalSourceReference(), id, RedListUtil.TAXON_CHECKLISTE_NAMESPACE);
165
            taxaToSave.add(checklistTaxon);
166
        }
167

    
168
        //NOTE: the source has to be added after cloning or otherwise the clone would also get the source
169
        ImportHelper.setOriginalSource(taxonBase, state.getTransactionalSourceReference(), id, RedListUtil.TAXON_GESAMTLISTE_NAMESPACE);
170
        taxaToSave.add(taxonBase);
171
    }
172

    
173
    private void cloneTaxon(final TaxonBase<?> gesamtListeTaxon, String relationString, String sourceNameSpace, Set<TaxonBase> taxaToSave, long id, RedListGefaesspflanzenImportState state){
174
        if(CdmUtils.isNotBlank(relationString) && !relationString.equals(".")){
175
            Taxon clonedTaxon = null;
176

    
177
            if(gesamtListeTaxon.isInstanceOf(Taxon.class)){
178
                clonedTaxon = HibernateProxyHelper.deproxy(gesamtListeTaxon.clone(), Taxon.class);
179
            }
180
            else if(gesamtListeTaxon.isInstanceOf(Synonym.class)){
181
                clonedTaxon = Taxon.NewInstance(gesamtListeTaxon.getName(), gesamtListeTaxon.getSec());
182
            }
183
            else{
184
                RedListUtil.logMessage(id, "Taxon base "+gesamtListeTaxon+" is neither taxon nor synonym! Taxon could not be cloned", logger);
185
                return;
186
            }
187
            ImportHelper.setOriginalSource(clonedTaxon, state.getTransactionalSourceReference(), id, sourceNameSpace);
188
            taxaToSave.add(clonedTaxon);
189
        }
190
    }
191

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

    
194
        long id = rs.getLong(RedListUtil.NAMNR);
195
        String taxNameString = rs.getString(RedListUtil.TAXNAME);
196
        String gueltString = rs.getString(RedListUtil.GUELT);
197
        String trivialString = rs.getString(RedListUtil.TRIVIAL);
198
        String authorBasiString = rs.getString(RedListUtil.AUTOR_BASI);
199
        String hybString = rs.getString(RedListUtil.HYB);
200
        String florString = rs.getString(RedListUtil.FLOR);
201
        String atlasIdxString = rs.getString(RedListUtil.ATLAS_IDX);
202
        String kartString = rs.getString(RedListUtil.KART);
203
        String rl2015String = rs.getString(RedListUtil.RL2015);
204
        String ehrdString = rs.getString(RedListUtil.EHRD);
205
        String wisskString = rs.getString(RedListUtil.WISSK);
206

    
207
        TaxonBase<?> taxonBase = null;
208
        if(authorBasiString.trim().contains(RedListUtil.AUCT)){
209
            taxonBase = Taxon.NewInstance(name, null);
210
            taxonBase.setAppendedPhrase(RedListUtil.AUCT);
211
        }
212
        else if(gueltString.equals(RedListUtil.GUELT_ACCEPTED_TAXON)){
213
            taxonBase = Taxon.NewInstance(name, null);
214
        }
215
        else if(gueltString.equals(RedListUtil.GUELT_SYNONYM) || gueltString.equals(RedListUtil.GUELT_BASIONYM)){
216
            taxonBase = Synonym.NewInstance(name, null);
217
        }
218
        else{
219
            return null;
220
        }
221

    
222
        //common name
223
        if(taxonBase.isInstanceOf(Taxon.class) && trivialString!=null){
224
            Taxon taxon = HibernateProxyHelper.deproxy(taxonBase, Taxon.class);
225
            TaxonDescription description = TaxonDescription.NewInstance(taxon);
226
            description.addElement(CommonTaxonName.NewInstance(trivialString, Language.GERMAN()));
227
        }
228

    
229
        //add annotations
230
        addAnnotation(RedListUtil.FLOR+": "+florString, taxonBase);
231
        addAnnotation(RedListUtil.ATLAS_IDX+": "+atlasIdxString, taxonBase);
232
        addAnnotation(RedListUtil.KART+": "+kartString, taxonBase);
233
        addAnnotation(RedListUtil.RL2015+": "+rl2015String, taxonBase);
234
        addAnnotation(RedListUtil.EHRD+": "+ehrdString, taxonBase);
235
        addAnnotation(RedListUtil.WISSK+": "+wisskString, taxonBase);
236

    
237
        //check taxon name consistency
238
        checkTaxonConsistency(id, taxNameString, hybString, taxonBase);
239
        return taxonBase;
240
    }
241

    
242
    private void addAnnotation(String string, TaxonBase<?> taxonBase) {
243
        if(CdmUtils.isNotBlank(string)){
244
            taxonBase.addAnnotation(Annotation.NewInstance(string, AnnotationType.TECHNICAL(), Language.GERMAN()));
245
        }
246
    }
247

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

    
250
        long id = rs.getLong(RedListUtil.NAMNR);
251
        String nomZusatzString = rs.getString(RedListUtil.NOM_ZUSATZ);
252
        String taxZusatzString = rs.getString(RedListUtil.TAX_ZUSATZ);
253
        String zusatzString = rs.getString(RedListUtil.ZUSATZ);
254
        String authorKombString = rs.getString(RedListUtil.AUTOR_KOMB);
255
        String authorBasiString = rs.getString(RedListUtil.AUTOR_BASI);
256
        String hybString = rs.getString(RedListUtil.HYB);
257

    
258
        //combination author
259
        if(authorKombString.contains(RedListUtil.EX)){
260
            //TODO: what happens with multiple ex authors??
261
            String[] kombSplit = authorKombString.split(RedListUtil.EX);
262
            if(kombSplit.length!=2){
263
                RedListUtil.logMessage(id, "Multiple ex combination authors found", logger);
264
            }
265
            for (int i = 0; i < kombSplit.length; i++) {
266
                if(i==0){
267
                    //first author is ex author
268
                    TeamOrPersonBase<?> authorKomb = (TeamOrPersonBase<?>) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, kombSplit[i]);
269
                    name.setExCombinationAuthorship(authorKomb);
270
                }
271
                else{
272
                    TeamOrPersonBase<?> authorKomb = (TeamOrPersonBase<?>) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, kombSplit[i]);
273
                    name.setCombinationAuthorship(authorKomb);
274
                }
275
            }
276
        }
277
        else if(authorKombString.trim().contains(RedListUtil.AUCT)){
278
            RedListUtil.logMessage(id, "AUCT information in "+RedListUtil.AUTOR_KOMB+" column", logger);
279
        }
280
        else if(CdmUtils.isNotBlank(authorKombString)){
281
            TeamOrPersonBase<?> authorKomb = (TeamOrPersonBase<?>) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, authorKombString);
282
            name.setCombinationAuthorship(authorKomb);
283
        }
284
        //basionym author
285
        if(authorBasiString.contains(RedListUtil.EX)){
286
            String[] basiSplit = authorBasiString.split(RedListUtil.EX);
287
            for (int i = 0; i < basiSplit.length; i++) {
288
                if(basiSplit.length!=2){
289
                    RedListUtil.logMessage(id, "Multiple ex basionymn authors found", logger);
290
                }
291
                if(i==0){
292
                    TeamOrPersonBase<?> authorBasi= (TeamOrPersonBase<?>) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, basiSplit[i]);
293
                    if(CdmUtils.isBlank(authorKombString)){
294
                        name.setExCombinationAuthorship(authorBasi);
295
                    }
296
                    else{
297
                        name.setExBasionymAuthorship(authorBasi);
298
                    }
299
                }
300
                else{
301
                    TeamOrPersonBase<?> authorBasi= (TeamOrPersonBase<?>) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, basiSplit[i]);
302
                    if(CdmUtils.isBlank(authorKombString)){
303
                        name.setCombinationAuthorship(authorBasi);
304
                    }
305
                    else{
306
                        name.setBasionymAuthorship(authorBasi);
307
                    }
308
                }
309
            }
310
        }
311
        else if(CdmUtils.isNotBlank(authorBasiString)){
312
            //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
313
            TeamOrPersonBase<?> authorBasi= (TeamOrPersonBase<?>) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, authorBasiString);
314
            if(CdmUtils.isBlank(authorKombString)){
315
                name.setCombinationAuthorship(authorBasi);
316
            }
317
            else{
318
                name.setBasionymAuthorship(authorBasi);
319
            }
320
        }
321

    
322
        //check authorship consistency
323
        String authorString = rs.getString(RedListUtil.AUTOR);
324
        checkNameConsistency(id, nomZusatzString, taxZusatzString, zusatzString, authorString, hybString, name);
325
    }
326

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

    
329
        long id = rs.getLong(RedListUtil.NAMNR);
330
        String taxNameString = rs.getString(RedListUtil.TAXNAME);
331
        String rangString = rs.getString(RedListUtil.RANG);
332
        String ep1String = rs.getString(RedListUtil.EPI1);
333
        String ep2String = rs.getString(RedListUtil.EPI2);
334
        String ep3String = rs.getString(RedListUtil.EPI3);
335
        String nomZusatzString = rs.getString(RedListUtil.NOM_ZUSATZ);
336
        String hybString = rs.getString(RedListUtil.HYB);
337
        String formelString = rs.getString(RedListUtil.FORMEL);
338

    
339
        if(CdmUtils.isBlank(taxNameString) && CdmUtils.isBlank(ep1String)){
340
            RedListUtil.logMessage(id, "No name found!", logger);
341
        }
342

    
343
        NonViralName<?> name = null;
344
        Rank rank = makeRank(id, state, rangString, ep3String!=null);
345
        //cultivar
346
        if(rank!= null && rank.equals(Rank.CULTIVAR())){
347
            CultivarPlantName cultivar = CultivarPlantName.NewInstance(rank);
348
            cultivar.setGenusOrUninomial(ep1String);
349
            cultivar.setSpecificEpithet(ep2String);
350
            cultivar.setCultivarName(ep3String);
351
            name = cultivar;
352
        }
353
        //botanical names
354
        else{
355
            name = BotanicalName.NewInstance(rank);
356

    
357
            //ep1 should always be present
358
            if(CdmUtils.isBlank(ep1String)){
359
                RedListUtil.logMessage(id, RedListUtil.EPI1+" is empty!", logger);
360
            }
361
            name.setGenusOrUninomial(ep1String);
362
            if(CdmUtils.isNotBlank(ep2String)){
363
                if(rank!=null && rank.isInfraGenericButNotSpeciesGroup()){
364
                    name.setInfraGenericEpithet(ep2String);
365
                }
366
                else{
367
                    name.setSpecificEpithet(ep2String);
368
                }
369
            }
370
            if(CdmUtils.isNotBlank(ep3String)){
371
                name.setInfraSpecificEpithet(ep3String);
372
            }
373

    
374

    
375
            //nomenclatural status
376
            if(CdmUtils.isNotBlank(nomZusatzString)){
377
                NomenclaturalStatusType statusType = makeNomenclaturalStatus(id, state, nomZusatzString);
378
                if(statusType!=null){
379
                    NomenclaturalStatus status = NomenclaturalStatus.NewInstance(statusType);
380
                    //special case for invalid names where the DB entry contains
381
                    //additional information in brackets e.g. "nom. inval. (sine basion.)"
382
                    if(statusType.equals(NomenclaturalStatusType.INVALID())){
383
                        Pattern pattern = Pattern.compile("\\((.*?)\\)");
384
                        Matcher matcher = pattern.matcher(nomZusatzString);
385
                        if (matcher.find()){
386
                            status.setRuleConsidered(matcher.group(1));
387
                        }
388
                    }
389
                    name.addStatus(status);
390
                }
391
            }
392
            //hybrid
393
            if(CdmUtils.isNotBlank(hybString)){
394
                //save hybrid formula
395
                if(CdmUtils.isNotBlank(formelString)){
396
                    Annotation annotation = Annotation.NewDefaultLanguageInstance(formelString);
397
                    annotation.setAnnotationType(AnnotationType.TECHNICAL());
398
                    name.addAnnotation(annotation);
399
                }
400
                //more than two hybrids not yet handled by name parser
401
                //TODO: use parser when implemented to fully support hybrids
402
                if(taxNameString.split(RedListUtil.HYB_SIGN).length>2){
403
                    name = BotanicalName.NewInstance(rank);
404
                    name.setTitleCache(taxNameString, true);
405
                }
406
                else if(hybString.equals(RedListUtil.HYB_X)){
407
                    name.setBinomHybrid(true);
408
                }
409
                else if(hybString.equals(RedListUtil.HYB_G)){
410
                    name.setMonomHybrid(true);
411
                }
412
                else if(hybString.equals(RedListUtil.HYB_XF)){
413
                    name.setHybridFormula(true);
414
                    if(ep1String.contains(RedListUtil.HYB_SIGN)){
415
                        RedListUtil.logMessage(id, "EPI1 has hybrid signs but with flag: "+RedListUtil.HYB_XF, logger);
416
                    }
417
                    else if(ep2String.contains(RedListUtil.HYB_SIGN)){
418
                        String[] split = ep2String.split(RedListUtil.HYB_SIGN);
419
                        String hybridFormula1 = ep1String+" "+split[0].trim();
420
                        String hybridFormula2 = ep1String+" "+split[1].trim();
421
                        //check if the specific epithets are from the same genus or not like e.g. EPI2 = pratensis × Lolium multiflorum
422
                        if(split[1].split(" ").length>1){
423
                            hybridFormula2 = split[1];
424
                        }
425
                        if(CdmUtils.isNotBlank(ep3String)){
426
                            hybridFormula1 += " "+ep3String;
427
                            hybridFormula2 += " "+ep3String;
428
                        }
429
                        String fullFormula = hybridFormula1+" "+RedListUtil.HYB_SIGN+" "+hybridFormula2;
430
                        name = NonViralNameParserImpl.NewInstance().parseFullName(fullFormula, NomenclaturalCode.ICNAFP, rank);
431
                    }
432
                    else if(ep3String.contains(RedListUtil.HYB_SIGN)){
433
                        String[] split = ep3String.split(RedListUtil.HYB_SIGN);
434
                        String hybridFormula1 = ep1String+" "+ep2String+" "+split[0];
435
                        String hybridFormula2 = ep1String+" "+ep2String+" "+split[1];
436
                        String fullFormula = hybridFormula1+" "+RedListUtil.HYB_SIGN+" "+hybridFormula2;
437
                        name = NonViralNameParserImpl.NewInstance().parseFullName(fullFormula, NomenclaturalCode.ICNAFP, rank);
438
                    }
439
                }
440
                else if(hybString.equals(RedListUtil.HYB_N)){
441
                    name = NonViralNameParserImpl.NewInstance().parseFullName(taxNameString, NomenclaturalCode.ICNAFP, rank);
442
                }
443
                else if(hybString.equals(RedListUtil.HYB_GF)){
444
                    if(ep1String.contains(RedListUtil.HYB_SIGN)){
445
                        name = NonViralNameParserImpl.NewInstance().parseFullName(ep1String, NomenclaturalCode.ICNAFP, rank);
446
                    }
447
                    else{
448
                        RedListUtil.logMessage(id, "HYB is "+hybString+" but "+RedListUtil.HYB+" does not contain "+RedListUtil.HYB_SIGN, logger);
449
                    }
450
                }
451
                else if(hybString.equals(RedListUtil.HYB_XS)){
452
                    //nothing to do
453
                }
454
                else{
455
                    logger.error("HYB value "+hybString+" not yet handled");
456
                }
457
            }
458
        }
459
        //add source
460
        ImportHelper.setOriginalSource(name, state.getTransactionalSourceReference(), id, RedListUtil.NAME_NAMESPACE);
461

    
462
        namesToSave.add(name);
463
        return name;
464
    }
465

    
466
    private void checkNameConsistency(long id, String nomZusatzString, String taxZusatzString,
467
            String zusatzString, String authorString, String hybString, NonViralName<?> name) {
468
        String authorshipCache = name.getAuthorshipCache();
469
        if(hybString.equals(RedListUtil.HYB_XF)){
470
            if(name.getHybridChildRelations().isEmpty()){
471
                RedListUtil.logMessage(id, "Hybrid formula but no hybrid child relations: "+name.getTitleCache(), logger);
472
                return;
473
            }
474
            return;
475
        }
476

    
477
        if(CdmUtils.isNotBlank(zusatzString)){
478
            authorString = authorString.replace(", "+zusatzString, "");
479
        }
480
        if(CdmUtils.isNotBlank(nomZusatzString)){
481
            authorString = authorString.replace(", "+nomZusatzString, "");
482
        }
483
        if(CdmUtils.isNotBlank(taxZusatzString)){
484
            authorString = authorString.replace(", "+taxZusatzString, "");
485
        }
486
        if(authorString.equals(RedListUtil.AUCT)){
487
            authorString = "";
488
        }
489
        if(STRICT_TITLE_CHECK){
490
            if(!authorString.equals(authorshipCache)){
491
                RedListUtil.logMessage(id, "Authorship inconsistent! name.authorhshipCache <-> Column "+RedListUtil.AUTOR+": "+authorshipCache+" <-> "+authorString, logger);
492
            }
493
        }
494
        else{
495
            if(CdmUtils.isNotBlank(authorString) && !authorString.startsWith(authorshipCache)){
496
                RedListUtil.logMessage(id, "Authorship inconsistent! name.authorhshipCache <-> Column "+RedListUtil.AUTOR+": "+authorshipCache+" <-> "+authorString, logger);
497
            }
498
        }
499
    }
500

    
501
    private void checkTaxonConsistency(long id, String taxNameString, String hybString, TaxonBase<?> taxonBase) {
502
        String nameCache = HibernateProxyHelper.deproxy(taxonBase.getName(), NonViralName.class).getNameCache().trim();
503
        taxNameString = taxNameString.trim();
504
        taxNameString = taxNameString.replaceAll(" +", " ");
505

    
506
        if(taxNameString.endsWith("agg.")){
507
            taxNameString = taxNameString.replace("agg.", "aggr.");
508
        }
509

    
510
        if(hybString.equals(RedListUtil.HYB_X) || hybString.equals(RedListUtil.HYB_N)){
511
            taxNameString = taxNameString.replace(" "+RedListUtil.HYB_SIGN+" ", " "+RedListUtil.HYB_SIGN);//hybrid sign has no space after it in titleCache for binomial hybrids
512
            taxNameString = taxNameString.replace(" x ", " "+RedListUtil.HYB_SIGN);//in some cases a standard 'x' is used
513
        }
514
        else if(hybString.equals(RedListUtil.HYB_G)){
515
            taxNameString = taxNameString.replace("X ", RedListUtil.HYB_SIGN);
516
        }
517
        else if(hybString.equals(RedListUtil.HYB_GF)){
518
            taxNameString = taxNameString.replace(" "+RedListUtil.HYB_SIGN, " x");
519
        }
520

    
521
        if(taxNameString.endsWith("- Gruppe")){
522
            taxNameString = taxNameString.replaceAll("- Gruppe", "species group");
523
        }
524
        if(taxNameString.endsWith("- group")){
525
            taxNameString = taxNameString.replaceAll("- group", "species group");
526
        }
527

    
528
        taxNameString = taxNameString.replace("[ranglos]", "[unranked]");
529
        if(taxonBase.getName().getRank()!=null && taxonBase.getName().getRank().equals(Rank.PROLES())){
530
            taxNameString = taxNameString.replace("proles", "prol.");
531
        }
532
        if(STRICT_TITLE_CHECK){
533
            if(!taxNameString.trim().equals(nameCache)){
534
                RedListUtil.logMessage(id, "Taxon name inconsistent! taxon.nameCache <-> Column "+RedListUtil.TAXNAME+": "+nameCache+" <-> "+taxNameString, logger);
535
            }
536
        }
537
        else{
538
            if(!taxNameString.startsWith(nameCache)){
539
                RedListUtil.logMessage(id, "Taxon name inconsistent! taxon.nameCache <-> Column "+RedListUtil.TAXNAME+": "+nameCache+" <-> "+taxNameString, logger);
540
            }
541
        }
542
    }
543

    
544
    private Rank makeRank(long id, RedListGefaesspflanzenImportState state, String rankStr, boolean hasSpecificEpithet) {
545
        Rank rank = null;
546
        try {
547
            if(rankStr.equals("ORA")){
548
                //special handling for ORA because of two possibilities
549
                if(hasSpecificEpithet){
550
                    return Rank.UNRANKED_INFRASPECIFIC();
551
                }
552
                else{
553
                    return Rank.UNRANKED_INFRAGENERIC();
554
                }
555
            }
556
            else{
557
                rank = state.getTransformer().getRankByKey(rankStr);
558
            }
559
        } catch (UndefinedTransformerMethodException e) {
560
            e.printStackTrace();
561
        }
562
        if(rank==null){
563
            RedListUtil.logMessage(id, rankStr+" could not be associated to a known rank.", logger);
564
        }
565
        return rank;
566
    }
567

    
568
    private NomenclaturalStatusType makeNomenclaturalStatus(long id, RedListGefaesspflanzenImportState state, String nomZusatzString) {
569
        NomenclaturalStatusType status = null;
570
        try {
571
            status = state.getTransformer().getNomenclaturalStatusByKey(nomZusatzString);
572
        } catch (UndefinedTransformerMethodException e) {
573
            e.printStackTrace();
574
        }
575
        if(status==null){
576
            RedListUtil.logMessage(id, nomZusatzString+" could not be associated to a known nomenclatural status.", logger);
577
        }
578
        return status;
579
    }
580

    
581

    
582

    
583
    @Override
584
    public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
585
            RedListGefaesspflanzenImportState state) {
586
        Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
587
        Map<String, AgentBase<?>> authorMap = new HashMap<String, AgentBase<?>>();
588

    
589
        try {
590
            while (rs.next()){
591
                String authorKombString = rs.getString(RedListUtil.AUTOR_KOMB);
592

    
593
                if(authorKombString.contains(RedListUtil.EX)){
594
                    String[] kombSplit = authorKombString.split(RedListUtil.EX);
595
                    for (int i = 0; i < kombSplit.length; i++) {
596
                        if(!authorMap.containsKey(kombSplit[i])){
597
                            authorMap.put(kombSplit[i], getAgentService().load(state.getAuthorMap().get(kombSplit[i])));
598
                        }
599
                    }
600
                }
601
                else if(CdmUtils.isNotBlank(authorKombString) && !authorMap.containsKey(authorKombString)){
602
                    authorMap.put(authorKombString, getAgentService().load(state.getAuthorMap().get(authorKombString)));
603
                }
604

    
605
                String authorBasiString = rs.getString(RedListUtil.AUTOR_BASI);
606
                //basionym author
607
                if(authorBasiString.contains(RedListUtil.EX)){
608
                    String[] basiSplit = authorBasiString.split(RedListUtil.EX);
609
                    for (int i = 0; i < basiSplit.length; i++) {
610
                        if(!authorMap.containsKey(basiSplit[i])){
611
                            authorMap.put(basiSplit[i], getAgentService().load(state.getAuthorMap().get(basiSplit[i])));
612
                        }
613
                    }
614
                }
615
                else if(CdmUtils.isNotBlank(authorBasiString) && !authorMap.containsKey(authorBasiString)){
616
                    authorMap.put(authorBasiString, getAgentService().load(state.getAuthorMap().get(authorBasiString)));
617
                }
618
            }
619
        } catch (SQLException e) {
620
            e.printStackTrace();
621
        }
622
        result.put(RedListUtil.AUTHOR_NAMESPACE, authorMap);
623

    
624
        return result;
625
    }
626

    
627
    @Override
628
    protected boolean doCheck(RedListGefaesspflanzenImportState state) {
629
        return false;
630
    }
631

    
632
    @Override
633
    protected boolean isIgnore(RedListGefaesspflanzenImportState state) {
634
        return false;
635
    }
636

    
637
}
(5-5/8)