Project

General

Profile

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

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

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

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

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

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

    
69
    private static final boolean STRICT_TITLE_CHECK = false;
70

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

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

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

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

    
96

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

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

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

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

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

    
132

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
341
        Rank rank = makeRank(id, state, rangString, ep3String!=null);
342
        NonViralName<?> name = BotanicalName.NewInstance(rank);
343

    
344
        //ep1 should always be present
345
        if(CdmUtils.isBlank(ep1String)){
346
            RedListUtil.logMessage(id, RedListUtil.EPI1+" is empty!", logger);
347
        }
348
        name.setGenusOrUninomial(ep1String);
349
        if(CdmUtils.isNotBlank(ep2String)){
350
            if(rank!=null && rank.isInfraGenericButNotSpeciesGroup()){
351
                name.setInfraGenericEpithet(ep2String);
352
            }
353
            else{
354
                name.setSpecificEpithet(ep2String);
355
            }
356
        }
357
        if(CdmUtils.isNotBlank(ep3String)){
358
            name.setInfraSpecificEpithet(ep3String);
359
        }
360
        //nomenclatural status
361
        if(CdmUtils.isNotBlank(nomZusatzString)){
362
            NomenclaturalStatusType statusType = makeNomenclaturalStatus(id, state, nomZusatzString);
363
            if(statusType!=null){
364
                NomenclaturalStatus status = NomenclaturalStatus.NewInstance(statusType);
365
                //special case for invalid names where the DB entry contains
366
                //additional information in brackets e.g. "nom. inval. (sine basion.)"
367
                if(statusType.equals(NomenclaturalStatusType.INVALID())){
368
                    Pattern pattern = Pattern.compile("\\((.*?)\\)");
369
                    Matcher matcher = pattern.matcher(nomZusatzString);
370
                    if (matcher.find()){
371
                        status.setRuleConsidered(matcher.group(1));
372
                    }
373
                }
374
                name.addStatus(status);
375
            }
376
        }
377
        //hybrid
378
        if(CdmUtils.isNotBlank(hybString)){
379
            //save hybrid formula
380
            if(CdmUtils.isNotBlank(formelString)){
381
                Annotation annotation = Annotation.NewDefaultLanguageInstance(formelString);
382
                annotation.setAnnotationType(AnnotationType.TECHNICAL());
383
                name.addAnnotation(annotation);
384
            }
385

    
386
            if(hybString.equals(RedListUtil.HYB_X)){
387
                name.setBinomHybrid(true);
388
            }
389
            else if(hybString.equals(RedListUtil.HYB_G)){
390
                name.setMonomHybrid(true);
391
            }
392
            else if(hybString.equals(RedListUtil.HYB_XF)){
393
                name.setHybridFormula(true);
394
                if(ep1String.contains(RedListUtil.HYB_SIGN)){
395
                    RedListUtil.logMessage(id, "EPI1 has hybrid signs but with flag: "+RedListUtil.HYB_XF, logger);
396
                }
397
                else if(ep2String.contains(RedListUtil.HYB_SIGN)){
398
                    String[] split = ep2String.split(RedListUtil.HYB_SIGN);
399
                    if(split.length!=2){
400
                        RedListUtil.logMessage(id, "Multiple hybrid signs found in "+ep2String, logger);
401
                    }
402
                    String hybridFormula1 = ep1String+" "+split[0].trim();
403
                    String hybridFormula2 = ep1String+" "+split[1].trim();
404
                    if(CdmUtils.isNotBlank(ep3String)){
405
                        hybridFormula1 += " "+ep3String;
406
                        hybridFormula2 += " "+ep3String;
407
                    }
408
                    String fullFormula = hybridFormula1+" "+RedListUtil.HYB_SIGN+" "+hybridFormula2;
409
                    name = NonViralNameParserImpl.NewInstance().parseFullName(fullFormula);
410
                }
411
                else if(ep3String.contains(RedListUtil.HYB_SIGN)){
412
                    String[] split = ep3String.split(RedListUtil.HYB_SIGN);
413
                    if(split.length!=2){
414
                        RedListUtil.logMessage(id, "Multiple hybrid signs found in "+ep3String, logger);
415
                    }
416
                    String hybridFormula1 = ep1String+" "+ep2String+" "+split[0];
417
                    String hybridFormula2 = ep1String+" "+ep2String+" "+split[1];
418
                    String fullFormula = hybridFormula1+" "+RedListUtil.HYB_SIGN+" "+hybridFormula2;
419
                    name = NonViralNameParserImpl.NewInstance().parseFullName(fullFormula);
420
                }
421
            }
422
            else if(hybString.equals(RedListUtil.HYB_N)){
423
                name = NonViralNameParserImpl.NewInstance().parseFullName(ep1String+" "+ep2String+" nothosubsp. "+ep3String);
424
            }
425
            else if(hybString.equals(RedListUtil.HYB_GF)){
426
                if(ep1String.contains(RedListUtil.HYB_SIGN)){
427
                    name = NonViralNameParserImpl.NewInstance().parseFullName(ep1String);
428
                }
429
                else{
430
                    RedListUtil.logMessage(id, "HYB is "+hybString+" but "+RedListUtil.HYB+" does not contain "+RedListUtil.HYB_SIGN, logger);
431
                }
432
            }
433
            else if(hybString.equals(RedListUtil.HYB_XS)){
434
                //nothing to do
435
            }
436
            else{
437
                logger.error("HYB value "+hybString+" not yet handled");
438
            }
439
        }
440
        //add source
441
        ImportHelper.setOriginalSource(name, state.getTransactionalSourceReference(), id, RedListUtil.NAME_NAMESPACE);
442

    
443
        namesToSave.add(name);
444
        return name;
445
    }
446

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

    
473
    private void checkTaxonNameConsistency(long id, String taxNameString, String hybString, TaxonBase<?> taxonBase) {
474
        if(hybString.equals(RedListUtil.HYB_XF)){
475
            if(HibernateProxyHelper.deproxy(taxonBase.getName(),NonViralName.class).getHybridChildRelations().isEmpty()){
476
                RedListUtil.logMessage(id, "Hybrid name but no hybrid child relations: "+taxonBase.getTitleCache(), logger);
477
                return;
478
            }
479
            return;
480
        }
481

    
482

    
483
        String nameCache = HibernateProxyHelper.deproxy(taxonBase.getName(), NonViralName.class).getNameCache().trim();
484
        taxNameString = taxNameString.trim();
485
        taxNameString.replaceAll(" +", " ");
486

    
487
        if(taxNameString.endsWith("agg.")){
488
            taxNameString = taxNameString.replace("agg.", "aggr.");
489
        }
490

    
491
        if(hybString.equals(RedListUtil.HYB_X)){
492
            taxNameString = taxNameString.replace(" "+RedListUtil.HYB_SIGN+" ", " "+RedListUtil.HYB_SIGN);//hybrid sign has no space after it in titleCache for binomial hybrids
493
            taxNameString = taxNameString.replace(" x ", " "+RedListUtil.HYB_SIGN);//in some cases a standard 'x' is used
494
        }
495
        else if(hybString.equals(RedListUtil.HYB_G)){
496
            taxNameString = taxNameString.replace("X ", RedListUtil.HYB_SIGN);
497
        }
498
        else if(hybString.equals(RedListUtil.HYB_GF)){
499
            taxNameString = taxNameString.replace(" "+RedListUtil.HYB_SIGN, " x");
500
        }
501

    
502
        if(taxNameString.endsWith("- Gruppe")){String a ="Festuca ×xx Lolium <-> Festuca ×× Lolium";
503
            taxNameString = taxNameString.replaceAll("- Gruppe", "species group");
504
        }
505
        if(taxNameString.endsWith("- group")){
506
            taxNameString = taxNameString.replaceAll("- group", "species group");
507
        }
508

    
509
        taxNameString = taxNameString.replace("[ranglos]", "[unranked]");
510
        if(STRICT_TITLE_CHECK){
511
            if(!taxNameString.trim().equals(nameCache)){
512
                RedListUtil.logMessage(id, "Taxon name inconsistent! taxon.titleCache <-> Column "+RedListUtil.TAXNAME+": "+nameCache+" <-> "+taxNameString, logger);
513
            }
514
        }
515
        else{
516
            if(!taxNameString.startsWith(nameCache)){
517
                RedListUtil.logMessage(id, "Taxon name inconsistent! taxon.titleCache <-> Column "+RedListUtil.TAXNAME+": "+nameCache+" <-> "+taxNameString, logger);
518
            }
519
        }
520
    }
521

    
522
    private Rank makeRank(long id, RedListGefaesspflanzenImportState state, String rankStr, boolean hasSpecificEpithet) {
523
        Rank rank = null;
524
        try {
525
            if(rankStr.equals("ORA")){
526
                //special handling for ORA because of two possibilities
527
                if(hasSpecificEpithet){
528
                    return Rank.UNRANKED_INFRASPECIFIC();
529
                }
530
                else{
531
                    return Rank.UNRANKED_INFRAGENERIC();
532
                }
533
            }
534
            else{
535
                rank = state.getTransformer().getRankByKey(rankStr);
536
            }
537
        } catch (UndefinedTransformerMethodException e) {
538
            e.printStackTrace();
539
        }
540
        if(rank==null){
541
            RedListUtil.logMessage(id, rankStr+" could not be associated to a known rank.", logger);
542
        }
543
        return rank;
544
    }
545

    
546
    private NomenclaturalStatusType makeNomenclaturalStatus(long id, RedListGefaesspflanzenImportState state, String nomZusatzString) {
547
        NomenclaturalStatusType status = null;
548
        try {
549
            status = state.getTransformer().getNomenclaturalStatusByKey(nomZusatzString);
550
        } catch (UndefinedTransformerMethodException e) {
551
            e.printStackTrace();
552
        }
553
        if(status==null){
554
            RedListUtil.logMessage(id, nomZusatzString+" could not be associated to a known nomenclatural status.", logger);
555
        }
556
        return status;
557
    }
558

    
559

    
560

    
561
    @Override
562
    public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
563
            RedListGefaesspflanzenImportState state) {
564
        Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
565
        Map<String, AgentBase<?>> authorMap = new HashMap<String, AgentBase<?>>();
566

    
567
        try {
568
            while (rs.next()){
569
                String authorKombString = rs.getString(RedListUtil.AUTOR_KOMB);
570

    
571
                if(authorKombString.contains(RedListUtil.EX)){
572
                    String[] kombSplit = authorKombString.split(RedListUtil.EX);
573
                    for (int i = 0; i < kombSplit.length; i++) {
574
                        if(!authorMap.containsKey(kombSplit[i])){
575
                            authorMap.put(kombSplit[i], getAgentService().load(state.getAuthorMap().get(kombSplit[i])));
576
                        }
577
                    }
578
                }
579
                else if(CdmUtils.isNotBlank(authorKombString) && !authorMap.containsKey(authorKombString)){
580
                    authorMap.put(authorKombString, getAgentService().load(state.getAuthorMap().get(authorKombString)));
581
                }
582

    
583
                String authorBasiString = rs.getString(RedListUtil.AUTOR_BASI);
584
                //basionym author
585
                if(authorBasiString.contains(RedListUtil.EX)){
586
                    String[] basiSplit = authorBasiString.split(RedListUtil.EX);
587
                    for (int i = 0; i < basiSplit.length; i++) {
588
                        if(!authorMap.containsKey(basiSplit[i])){
589
                            authorMap.put(basiSplit[i], getAgentService().load(state.getAuthorMap().get(basiSplit[i])));
590
                        }
591
                    }
592
                }
593
                else if(CdmUtils.isNotBlank(authorBasiString) && !authorMap.containsKey(authorBasiString)){
594
                    authorMap.put(authorBasiString, getAgentService().load(state.getAuthorMap().get(authorBasiString)));
595
                }
596
            }
597
        } catch (SQLException e) {
598
            e.printStackTrace();
599
        }
600
        result.put(RedListUtil.AUTHOR_NAMESPACE, authorMap);
601

    
602
        return result;
603
    }
604

    
605
    @Override
606
    protected boolean doCheck(RedListGefaesspflanzenImportState state) {
607
        return false;
608
    }
609

    
610
    @Override
611
    protected boolean isIgnore(RedListGefaesspflanzenImportState state) {
612
        return false;
613
    }
614

    
615
}
(4-4/7)