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.NomenclaturalStatus;
42
import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
43
import eu.etaxonomy.cdm.model.name.NonViralName;
44
import eu.etaxonomy.cdm.model.name.Rank;
45
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
46
import eu.etaxonomy.cdm.model.taxon.Synonym;
47
import eu.etaxonomy.cdm.model.taxon.Taxon;
48
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
49
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
50
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
51
import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
52

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

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

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

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

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

    
70
    private static final boolean STRICT_TITLE_CHECK = false;
71

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

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

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

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

    
97

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

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

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

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

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

    
133

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
320
        //check authorship consistency
321
        String authorString = rs.getString(RedListUtil.AUTOR);
322
        String authorshipCache = name.getAuthorshipCache();
323
        checkAuthorShipConsistency(id, nomZusatzString, taxZusatzString, zusatzString, authorString, authorshipCache);
324
    }
325

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

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

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

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

    
356
            //ep1 should always be present
357
            if(CdmUtils.isBlank(ep1String)){
358
                RedListUtil.logMessage(id, RedListUtil.EPI1+" is empty!", logger);
359
            }
360
            name.setGenusOrUninomial(ep1String);
361
            if(CdmUtils.isNotBlank(ep2String)){
362
                if(rank!=null && rank.isInfraGenericButNotSpeciesGroup()){
363
                    name.setInfraGenericEpithet(ep2String);
364
                }
365
                else{
366
                    name.setSpecificEpithet(ep2String);
367
                }
368
            }
369
        }
370
        //hybrid
371
        if(CdmUtils.isNotBlank(hybString)){
372
            //save hybrid formula
373
            if(CdmUtils.isNotBlank(formelString)){
374
                Annotation annotation = Annotation.NewDefaultLanguageInstance(formelString);
375
                annotation.setAnnotationType(AnnotationType.TECHNICAL());
376
                name.addAnnotation(annotation);
377
            }
378

    
379
            if(hybString.equals(RedListUtil.HYB_X)){
380
                name.setBinomHybrid(true);
381
                if(CdmUtils.isNotBlank(ep3String)){
382
                    name.setInfraSpecificEpithet(ep3String);
383
                }
384
                //nomenclatural status
385
                if(CdmUtils.isNotBlank(nomZusatzString)){
386
                    NomenclaturalStatusType statusType = makeNomenclaturalStatus(id, state, nomZusatzString);
387
                    if(statusType!=null){
388
                        NomenclaturalStatus status = NomenclaturalStatus.NewInstance(statusType);
389
                        //special case for invalid names where the DB entry contains
390
                        //additional information in brackets e.g. "nom. inval. (sine basion.)"
391
                        if(statusType.equals(NomenclaturalStatusType.INVALID())){
392
                            Pattern pattern = Pattern.compile("\\((.*?)\\)");
393
                            Matcher matcher = pattern.matcher(nomZusatzString);
394
                            if (matcher.find()){
395
                                status.setRuleConsidered(matcher.group(1));
396
                            }
397
                        }
398
                        name.addStatus(status);
399
                    }
400
                }
401
                //hybrid
402
                if(CdmUtils.isNotBlank(hybString)){
403
                    if(hybString.equals(RedListUtil.HYB_X)){
404
                        name.setBinomHybrid(true);
405
                    }
406
                    else if(hybString.equals(RedListUtil.HYB_G)){
407
                        name.setMonomHybrid(true);
408
                    }
409
                    else if(hybString.equals(RedListUtil.HYB_XF)){
410
                        name.setHybridFormula(true);
411
                        if(ep1String.contains(RedListUtil.HYB_SIGN)){
412
                            RedListUtil.logMessage(id, "EPI1 has hybrid signs but with flag: "+RedListUtil.HYB_XF, logger);
413
                        }
414
                        else if(ep2String.contains(RedListUtil.HYB_SIGN)){
415
                            String[] split = ep2String.split(RedListUtil.HYB_SIGN);
416
                            if(split.length!=2){
417
                                RedListUtil.logMessage(id, "Multiple hybrid signs found in "+ep2String, logger);
418
                            }
419
                            String hybridFormula1 = ep1String+" "+split[0].trim();
420
                            String hybridFormula2 = ep1String+" "+split[1].trim();
421
                            if(CdmUtils.isNotBlank(ep3String)){
422
                                hybridFormula1 += " "+ep3String;
423
                                hybridFormula2 += " "+ep3String;
424
                            }
425
                            String fullFormula = hybridFormula1+" "+RedListUtil.HYB_SIGN+" "+hybridFormula2;
426
                            name = NonViralNameParserImpl.NewInstance().parseFullName(fullFormula);
427
                        }
428
                        else if(ep3String.contains(RedListUtil.HYB_SIGN)){
429
                            String[] split = ep3String.split(RedListUtil.HYB_SIGN);
430
                            if(split.length!=2){
431
                                RedListUtil.logMessage(id, "Multiple hybrid signs found in "+ep3String, logger);
432
                            }
433
                            String hybridFormula1 = ep1String+" "+ep2String+" "+split[0];
434
                            String hybridFormula2 = ep1String+" "+ep2String+" "+split[1];
435
                            String fullFormula = hybridFormula1+" "+RedListUtil.HYB_SIGN+" "+hybridFormula2;
436
                            name = NonViralNameParserImpl.NewInstance().parseFullName(fullFormula);
437
                        }
438
                    }
439
                    else if(hybString.equals(RedListUtil.HYB_N)){
440
                        name = NonViralNameParserImpl.NewInstance().parseFullName(ep1String+" "+ep2String+" nothosubsp. "+ep3String);
441
                    }
442
                    else if(hybString.equals(RedListUtil.HYB_GF)){
443
                        if(ep1String.contains(RedListUtil.HYB_SIGN)){
444
                            name = NonViralNameParserImpl.NewInstance().parseFullName(ep1String);
445
                        }
446
                        else{
447
                            RedListUtil.logMessage(id, "HYB is "+hybString+" but "+RedListUtil.HYB+" does not contain "+RedListUtil.HYB_SIGN, logger);
448
                        }
449
                    }
450
                    else if(hybString.equals(RedListUtil.HYB_XS)){
451
                        //nothing to do
452
                    }
453
                    else{
454
                        logger.error("HYB value "+hybString+" not yet handled");
455
                    }
456
                }
457
                else{
458
                    logger.error("HYB value "+hybString+" not yet handled");
459
                }
460
            }
461
        }
462
        //add source
463
        ImportHelper.setOriginalSource(name, state.getTransactionalSourceReference(), id, RedListUtil.NAME_NAMESPACE);
464

    
465
        namesToSave.add(name);
466
        return name;
467
    }
468

    
469
    private void checkAuthorShipConsistency(long id, String nomZusatzString, String taxZusatzString,
470
            String zusatzString, String authorString, String authorshipCache) {
471
        if(CdmUtils.isNotBlank(zusatzString)){
472
            authorString = authorString.replace(", "+zusatzString, "");
473
        }
474
        if(CdmUtils.isNotBlank(nomZusatzString)){
475
            authorString = authorString.replace(", "+nomZusatzString, "");
476
        }
477
        if(CdmUtils.isNotBlank(taxZusatzString)){
478
            authorString = authorString.replace(", "+taxZusatzString, "");
479
        }
480
        if(authorString.equals(RedListUtil.AUCT)){
481
            authorString = "";
482
        }
483
        if(STRICT_TITLE_CHECK){
484
            if(!authorString.equals(authorshipCache)){
485
                RedListUtil.logMessage(id, "Authorship inconsistent! name.authorhshipCache <-> Column "+RedListUtil.AUTOR+": "+authorshipCache+" <-> "+authorString, logger);
486
            }
487
        }
488
        else{
489
            if(CdmUtils.isNotBlank(authorString) && !authorString.startsWith(authorshipCache)){
490
                RedListUtil.logMessage(id, "Authorship inconsistent! name.authorhshipCache <-> Column "+RedListUtil.AUTOR+": "+authorshipCache+" <-> "+authorString, logger);
491
            }
492
        }
493
    }
494

    
495
    private void checkTaxonNameConsistency(long id, String taxNameString, String hybString, TaxonBase<?> taxonBase) {
496
        if(hybString.equals(RedListUtil.HYB_XF)){
497
            if(HibernateProxyHelper.deproxy(taxonBase.getName(),NonViralName.class).getHybridChildRelations().isEmpty()){
498
                RedListUtil.logMessage(id, "Hybrid name but no hybrid child relations: "+taxonBase.getTitleCache(), logger);
499
                return;
500
            }
501
            return;
502
        }
503

    
504

    
505
        String nameCache = HibernateProxyHelper.deproxy(taxonBase.getName(), NonViralName.class).getNameCache().trim();
506
        taxNameString = taxNameString.trim();
507
        taxNameString.replaceAll(" +", " ");
508

    
509
        if(taxNameString.endsWith("agg.")){
510
            taxNameString = taxNameString.replace("agg.", "aggr.");
511
        }
512

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

    
524
        if(taxNameString.endsWith("- Gruppe")){String a ="Festuca ×xx Lolium <-> Festuca ×× Lolium";
525
            taxNameString = taxNameString.replaceAll("- Gruppe", "species group");
526
        }
527
        if(taxNameString.endsWith("- group")){
528
            taxNameString = taxNameString.replaceAll("- group", "species group");
529
        }
530

    
531
        taxNameString = taxNameString.replace("[ranglos]", "[unranked]");
532
        if(STRICT_TITLE_CHECK){
533
            if(!taxNameString.trim().equals(nameCache)){
534
                RedListUtil.logMessage(id, "Taxon name inconsistent! taxon.titleCache <-> Column "+RedListUtil.TAXNAME+": "+nameCache+" <-> "+taxNameString, logger);
535
            }
536
        }
537
        else{
538
            if(!taxNameString.startsWith(nameCache)){
539
                RedListUtil.logMessage(id, "Taxon name inconsistent! taxon.titleCache <-> 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)