Project

General

Profile

Download (28.6 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

    
401
                if(hybString.equals(RedListUtil.HYB_X)){
402
                    name.setBinomHybrid(true);
403
                }
404
                else if(hybString.equals(RedListUtil.HYB_G)){
405
                    name.setMonomHybrid(true);
406
                }
407
                else if(hybString.equals(RedListUtil.HYB_XF)){
408
                    name.setHybridFormula(true);
409
                    if(ep1String.contains(RedListUtil.HYB_SIGN)){
410
                        RedListUtil.logMessage(id, "EPI1 has hybrid signs but with flag: "+RedListUtil.HYB_XF, logger);
411
                    }
412
                    else if(ep2String.contains(RedListUtil.HYB_SIGN)){
413
                        String[] split = ep2String.split(RedListUtil.HYB_SIGN);
414
                        if(split.length!=2){
415
                            RedListUtil.logMessage(id, "Multiple hybrid signs found in "+ep2String, logger);
416
                        }
417
                        String hybridFormula1 = ep1String+" "+split[0].trim();
418
                        String hybridFormula2 = ep1String+" "+split[1].trim();
419
                        if(CdmUtils.isNotBlank(ep3String)){
420
                            hybridFormula1 += " "+ep3String;
421
                            hybridFormula2 += " "+ep3String;
422
                        }
423
                        String fullFormula = hybridFormula1+" "+RedListUtil.HYB_SIGN+" "+hybridFormula2;
424
                        name = NonViralNameParserImpl.NewInstance().parseFullName(fullFormula, NomenclaturalCode.ICNAFP, rank);
425
                    }
426
                    else if(ep3String.contains(RedListUtil.HYB_SIGN)){
427
                        String[] split = ep3String.split(RedListUtil.HYB_SIGN);
428
                        if(split.length!=2){
429
                            RedListUtil.logMessage(id, "Multiple hybrid signs found in "+ep3String, logger);
430
                        }
431
                        String hybridFormula1 = ep1String+" "+ep2String+" "+split[0];
432
                        String hybridFormula2 = ep1String+" "+ep2String+" "+split[1];
433
                        String fullFormula = hybridFormula1+" "+RedListUtil.HYB_SIGN+" "+hybridFormula2;
434
                        name = NonViralNameParserImpl.NewInstance().parseFullName(fullFormula);
435
                    }
436
                }
437
                else if(hybString.equals(RedListUtil.HYB_N)){
438
                    name = NonViralNameParserImpl.NewInstance().parseFullName(taxNameString);
439
                }
440
                else if(hybString.equals(RedListUtil.HYB_GF)){
441
                    if(ep1String.contains(RedListUtil.HYB_SIGN)){
442
                        name = NonViralNameParserImpl.NewInstance().parseFullName(ep1String);
443
                    }
444
                    else{
445
                        RedListUtil.logMessage(id, "HYB is "+hybString+" but "+RedListUtil.HYB+" does not contain "+RedListUtil.HYB_SIGN, logger);
446
                    }
447
                }
448
                else if(hybString.equals(RedListUtil.HYB_XS)){
449
                    //nothing to do
450
                }
451
                else{
452
                    logger.error("HYB value "+hybString+" not yet handled");
453
                }
454
            }
455
        }
456
        //add source
457
        ImportHelper.setOriginalSource(name, state.getTransactionalSourceReference(), id, RedListUtil.NAME_NAMESPACE);
458

    
459
        namesToSave.add(name);
460
        return name;
461
    }
462

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

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

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

    
503
        if(taxNameString.endsWith("agg.")){
504
            taxNameString = taxNameString.replace("agg.", "aggr.");
505
        }
506

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

    
518
        if(taxNameString.endsWith("- Gruppe")){
519
            taxNameString = taxNameString.replaceAll("- Gruppe", "species group");
520
        }
521
        if(taxNameString.endsWith("- group")){
522
            taxNameString = taxNameString.replaceAll("- group", "species group");
523
        }
524

    
525
        taxNameString = taxNameString.replace("[ranglos]", "[unranked]");
526
        if(STRICT_TITLE_CHECK){
527
            if(!taxNameString.trim().equals(nameCache)){
528
                RedListUtil.logMessage(id, "Taxon name inconsistent! taxon.nameCache <-> Column "+RedListUtil.TAXNAME+": "+nameCache+" <-> "+taxNameString, logger);
529
            }
530
        }
531
        else{
532
            if(!taxNameString.startsWith(nameCache)){
533
                RedListUtil.logMessage(id, "Taxon name inconsistent! taxon.nameCache <-> Column "+RedListUtil.TAXNAME+": "+nameCache+" <-> "+taxNameString, logger);
534
            }
535
        }
536
    }
537

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

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

    
575

    
576

    
577
    @Override
578
    public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
579
            RedListGefaesspflanzenImportState state) {
580
        Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
581
        Map<String, AgentBase<?>> authorMap = new HashMap<String, AgentBase<?>>();
582

    
583
        try {
584
            while (rs.next()){
585
                String authorKombString = rs.getString(RedListUtil.AUTOR_KOMB);
586

    
587
                if(authorKombString.contains(RedListUtil.EX)){
588
                    String[] kombSplit = authorKombString.split(RedListUtil.EX);
589
                    for (int i = 0; i < kombSplit.length; i++) {
590
                        if(!authorMap.containsKey(kombSplit[i])){
591
                            authorMap.put(kombSplit[i], getAgentService().load(state.getAuthorMap().get(kombSplit[i])));
592
                        }
593
                    }
594
                }
595
                else if(CdmUtils.isNotBlank(authorKombString) && !authorMap.containsKey(authorKombString)){
596
                    authorMap.put(authorKombString, getAgentService().load(state.getAuthorMap().get(authorKombString)));
597
                }
598

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

    
618
        return result;
619
    }
620

    
621
    @Override
622
    protected boolean doCheck(RedListGefaesspflanzenImportState state) {
623
        return false;
624
    }
625

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

    
631
}
(5-5/8)