Project

General

Profile

Download (33.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.ExtensionType;
37
import eu.etaxonomy.cdm.model.common.Language;
38
import eu.etaxonomy.cdm.model.common.OrderedTermVocabulary;
39
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
40
import eu.etaxonomy.cdm.model.description.TaxonDescription;
41
import eu.etaxonomy.cdm.model.name.ICultivarPlantName;
42
import eu.etaxonomy.cdm.model.name.INonViralName;
43
import eu.etaxonomy.cdm.model.name.ITaxonNameBase;
44
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
45
import eu.etaxonomy.cdm.model.name.NomenclaturalStatus;
46
import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
47
import eu.etaxonomy.cdm.model.name.Rank;
48
import eu.etaxonomy.cdm.model.name.RankClass;
49
import eu.etaxonomy.cdm.model.name.TaxonName;
50
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
51
import eu.etaxonomy.cdm.model.taxon.Synonym;
52
import eu.etaxonomy.cdm.model.taxon.Taxon;
53
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
54
import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
55

    
56
/**
57
 *
58
 * @author pplitzner
59
 * @since Mar 1, 2016
60
 *
61
 */
62

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

    
67
    private static final Logger logger = Logger.getLogger(RedListGefaesspflanzenImportNames.class);
68

    
69
    private static final String tableName = "Rote Liste Gefäßpflanzen";
70

    
71
    private static final String pluralString = "names";
72

    
73
    private static final boolean STRICT_TITLE_CHECK = false;
74

    
75
    private ExtensionType extensionTypeFlor;
76

    
77
    private ExtensionType extensionTypeAtlasIdx;
78

    
79
    private ExtensionType extensionTypeKart;
80

    
81
    private ExtensionType extensionTypeRl2015;
82

    
83
    private ExtensionType extensionTypeEhrd;
84

    
85
    private ExtensionType extensionTypeWissk;
86

    
87
    public RedListGefaesspflanzenImportNames() {
88
        super(tableName, pluralString);
89
    }
90

    
91
    @Override
92
    protected String getIdQuery(RedListGefaesspflanzenImportState state) {
93
        return "SELECT SEQNUM "
94
                + "FROM V_TAXATLAS_D20_EXPORT t "
95
                + " ORDER BY SEQNUM";
96
    }
97

    
98
    @Override
99
    protected String getRecordQuery(RedListGefaesspflanzenImportConfigurator config) {
100
        String result = " SELECT * "
101
                + " FROM V_TAXATLAS_D20_EXPORT t "
102
                + " WHERE t.SEQNUM IN (@IDSET)";
103
        result = result.replace("@IDSET", IPartitionedIO.ID_LIST_TOKEN);
104
        return result;
105
    }
106

    
107
    @Override
108
    protected void doInvoke(RedListGefaesspflanzenImportState state) {
109
        makeExtensionTypes();
110
        super.doInvoke(state);
111
    }
112

    
113

    
114
    private void makeExtensionTypes() {
115
        extensionTypeFlor = ExtensionType.NewInstance(RedListUtil.FLOR, RedListUtil.FLOR, "");
116
        extensionTypeAtlasIdx = ExtensionType.NewInstance(RedListUtil.ATLAS_IDX, RedListUtil.ATLAS_IDX, "");
117
        extensionTypeKart = ExtensionType.NewInstance(RedListUtil.KART, RedListUtil.KART, "");
118
        extensionTypeRl2015 = ExtensionType.NewInstance(RedListUtil.RL2015, RedListUtil.RL2015, "");
119
        extensionTypeEhrd = ExtensionType.NewInstance(RedListUtil.EHRD, RedListUtil.EHRD, "");
120
        extensionTypeWissk = ExtensionType.NewInstance(RedListUtil.WISSK, RedListUtil.WISSK, "");
121
        getTermService().saveOrUpdate(extensionTypeFlor);
122
        getTermService().saveOrUpdate(extensionTypeAtlasIdx);
123
        getTermService().saveOrUpdate(extensionTypeKart);
124
        getTermService().saveOrUpdate(extensionTypeRl2015);
125
        getTermService().saveOrUpdate(extensionTypeEhrd);
126
        getTermService().saveOrUpdate(extensionTypeWissk);
127
    }
128

    
129
    @Override
130
    public boolean doPartition(ResultSetPartitioner partitioner, RedListGefaesspflanzenImportState state) {
131
        ResultSet rs = partitioner.getResultSet();
132
        Set<ITaxonNameBase> namesToSave = new HashSet<>();
133
        Set<TaxonBase> taxaToSave = new HashSet<>();
134
        try {
135
            while (rs.next()){
136
                makeSingleNameAndTaxon(state, rs, namesToSave, taxaToSave);
137

    
138
            }
139
        } catch (SQLException e) {
140
            e.printStackTrace();
141
        }
142

    
143
        getNameService().saveOrUpdate(TaxonName.castAndDeproxy(namesToSave));
144
        getTaxonService().saveOrUpdate(taxaToSave);
145
        return true;
146
    }
147

    
148
    private void makeSingleNameAndTaxon(RedListGefaesspflanzenImportState state, ResultSet rs, Set<ITaxonNameBase> namesToSave, Set<TaxonBase> taxaToSave)
149
            throws SQLException {
150
        long id = rs.getLong(RedListUtil.NAMNR);
151
        String relationE = rs.getString(RedListUtil.E);
152
        String relationW = rs.getString(RedListUtil.W);
153
        String relationK = rs.getString(RedListUtil.K);
154
        String relationAW = rs.getString(RedListUtil.AW);
155
        String relationAO = rs.getString(RedListUtil.AO);
156
        String relationR = rs.getString(RedListUtil.R);
157
        String relationO = rs.getString(RedListUtil.O);
158
        String relationS = rs.getString(RedListUtil.S);
159

    
160
        //---NAME---
161
        INonViralName name = importName(state, rs, namesToSave);
162

    
163

    
164
        //--- AUTHORS ---
165
        importAuthors(state, rs, name);
166

    
167
        //---TAXON---
168
        TaxonBase<?> taxonBase = importTaxon(rs, name, state);
169
        if(taxonBase==null){
170
            RedListUtil.logMessage(id, "!SERIOUS ERROR! Taxon for name "+name+" could not be created!", logger);
171
            return;
172
        }
173

    
174
        //---CONCEPT RELATIONSHIPS---
175
        //E, W, K, AW, AO, R, O, S
176
        cloneTaxon(taxonBase, relationE, RedListUtil.CLASSIFICATION_NAMESPACE_E, taxaToSave, id, state);
177
        cloneTaxon(taxonBase, relationW, RedListUtil.CLASSIFICATION_NAMESPACE_W, taxaToSave, id, state);
178
        cloneTaxon(taxonBase, relationK, RedListUtil.CLASSIFICATION_NAMESPACE_K, taxaToSave, id, state);
179
        cloneTaxon(taxonBase, relationAW, RedListUtil.CLASSIFICATION_NAMESPACE_AW, taxaToSave, id, state);
180
        cloneTaxon(taxonBase, relationAO, RedListUtil.CLASSIFICATION_NAMESPACE_AO, taxaToSave, id, state);
181
        cloneTaxon(taxonBase, relationR, RedListUtil.CLASSIFICATION_NAMESPACE_R, taxaToSave, id, state);
182
        cloneTaxon(taxonBase, relationO, RedListUtil.CLASSIFICATION_NAMESPACE_O, taxaToSave, id, state);
183
        cloneTaxon(taxonBase, relationS, RedListUtil.CLASSIFICATION_NAMESPACE_S, taxaToSave, id, state);
184

    
185
        //NOTE: the source has to be added after cloning or otherwise the clone would also get the source
186
        ImportHelper.setOriginalSource(taxonBase, state.getTransactionalSourceReference(), id, RedListUtil.TAXON_GESAMTLISTE_NAMESPACE);
187
        taxaToSave.add(taxonBase);
188
    }
189

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

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

    
209
    private TaxonBase<?> importTaxon(ResultSet rs, INonViralName name, RedListGefaesspflanzenImportState state) throws SQLException {
210

    
211
        long id = rs.getLong(RedListUtil.NAMNR);
212
        String taxNameString = rs.getString(RedListUtil.TAXNAME);
213
        String epi1String = rs.getString(RedListUtil.EPI1);
214
        String epi2String = rs.getString(RedListUtil.EPI2);
215
        String epi3String = rs.getString(RedListUtil.EPI3);
216
        String gueltString = rs.getString(RedListUtil.GUELT);
217
        String trivialString = rs.getString(RedListUtil.TRIVIAL);
218
        String authorBasiString = rs.getString(RedListUtil.AUTOR_BASI);
219
        String hybString = rs.getString(RedListUtil.HYB);
220
        String florString = rs.getString(RedListUtil.FLOR);
221
        String atlasIdxString = rs.getString(RedListUtil.ATLAS_IDX);
222
        String kartString = rs.getString(RedListUtil.KART);
223
        String rl2015String = rs.getString(RedListUtil.RL2015);
224
        String ehrdString = rs.getString(RedListUtil.EHRD);
225
        String wisskString = rs.getString(RedListUtil.WISSK);
226

    
227
        TaxonBase<?> taxonBase = null;
228
        if(authorBasiString.trim().contains(RedListUtil.AUCT)){
229
            taxonBase = Taxon.NewInstance(name, null);
230
            taxonBase.setAppendedPhrase(RedListUtil.AUCT);
231
        }
232
        else if(gueltString.equals(RedListUtil.GUELT_ACCEPTED_TAXON)){
233
            taxonBase = Taxon.NewInstance(name, null);
234
        }
235
        else if(gueltString.equals(RedListUtil.GUELT_SYNONYM) || gueltString.equals(RedListUtil.GUELT_BASIONYM)){
236
            taxonBase = Synonym.NewInstance(name, null);
237
        }
238
        else{
239
            RedListUtil.logMessage(id, "Taxon was not created!! Unknown value for "+RedListUtil.GUELT+"!", logger);
240
            return null;
241
        }
242

    
243
        //common name
244
        if(taxonBase.isInstanceOf(Taxon.class) && trivialString!=null){
245
            Taxon taxon = HibernateProxyHelper.deproxy(taxonBase, Taxon.class);
246
            TaxonDescription description = TaxonDescription.NewInstance(taxon);
247
            description.addElement(CommonTaxonName.NewInstance(trivialString, Language.GERMAN()));
248
        }
249

    
250
        //add annotations
251
        taxonBase.addExtension(florString, extensionTypeFlor);
252
        taxonBase.addExtension(atlasIdxString, extensionTypeAtlasIdx);
253
        taxonBase.addExtension(kartString, extensionTypeKart);
254
        taxonBase.addExtension(rl2015String, extensionTypeRl2015);
255
        taxonBase.addExtension(ehrdString, extensionTypeEhrd);
256
        taxonBase.addExtension(wisskString, extensionTypeWissk);
257

    
258
        //check taxon name consistency
259
        checkTaxonConsistency(id, taxNameString, hybString, epi1String, epi2String, epi3String, taxonBase, state);
260
        return taxonBase;
261
    }
262

    
263
    private void importAuthors(RedListGefaesspflanzenImportState state, ResultSet rs, INonViralName name) throws SQLException {
264

    
265
        long id = rs.getLong(RedListUtil.NAMNR);
266
        String nomZusatzString = rs.getString(RedListUtil.NOM_ZUSATZ);
267
        String taxZusatzString = rs.getString(RedListUtil.TAX_ZUSATZ);
268
        String zusatzString = rs.getString(RedListUtil.ZUSATZ);
269
        String authorKombString = rs.getString(RedListUtil.AUTOR_KOMB);
270
        String authorBasiString = rs.getString(RedListUtil.AUTOR_BASI);
271
        String hybString = rs.getString(RedListUtil.HYB);
272

    
273
        //combination author
274
        if(authorKombString.contains(RedListUtil.EX)){
275
            // multiple ex authors will be reduced to only the last one
276
            // e.g. Almq. ex Sternström ex Dahlst. -> Almq. ex Dahlst.
277
            //first author is ex combination author
278
            String exAuthorString = RedListUtil.getExAuthorOfExAuthorshipString(authorKombString);
279
            TeamOrPersonBase<?> exAuthor = (TeamOrPersonBase<?>) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, exAuthorString);
280
            name.setExCombinationAuthorship(exAuthor);
281
            //the last author is the combination author
282
            String authorString = RedListUtil.getAuthorOfExAuthorshipString(authorKombString);
283
            TeamOrPersonBase<?> combAuthor = (TeamOrPersonBase<?>) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, authorString);
284
            name.setCombinationAuthorship(combAuthor);
285
        }
286
        else if(authorKombString.trim().contains(RedListUtil.AUCT)){
287
            RedListUtil.logMessage(id, "AUCT information in "+RedListUtil.AUTOR_KOMB+" column", logger);
288
        }
289
        else if(CdmUtils.isNotBlank(authorKombString)){
290
            TeamOrPersonBase<?> authorKomb = (TeamOrPersonBase<?>) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, authorKombString);
291
            name.setCombinationAuthorship(authorKomb);
292
        }
293
        //basionym author
294
        if(authorBasiString.contains(RedListUtil.EX)){
295
            TeamOrPersonBase<?> authorExBasi= (TeamOrPersonBase<?>) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, RedListUtil.getExAuthorOfExAuthorshipString(authorBasiString));
296
            if(CdmUtils.isBlank(authorKombString)){
297
                name.setExCombinationAuthorship(authorExBasi);
298
            }
299
            else{
300
                name.setExBasionymAuthorship(authorExBasi);
301
            }
302
            TeamOrPersonBase<?> authorBasi= (TeamOrPersonBase<?>) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, RedListUtil.getAuthorOfExAuthorshipString(authorBasiString));
303
            if(CdmUtils.isBlank(authorKombString)){
304
                name.setCombinationAuthorship(authorBasi);
305
            }
306
            else{
307
                name.setBasionymAuthorship(authorBasi);
308
            }
309
        }
310
        else if(CdmUtils.isNotBlank(authorBasiString)){
311
            //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
312
            TeamOrPersonBase<?> authorBasi= (TeamOrPersonBase<?>) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, authorBasiString);
313
            if(CdmUtils.isBlank(authorKombString)){
314
                name.setCombinationAuthorship(authorBasi);
315
            }
316
            else{
317
                name.setBasionymAuthorship(authorBasi);
318
            }
319
        }
320

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

    
326
    private INonViralName importName(RedListGefaesspflanzenImportState state, ResultSet rs, Set<ITaxonNameBase> 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
        INonViralName name = null;
343
        Rank rank = makeRank(id, state, rangString, CdmUtils.isNotBlank(ep3String));
344
        //cultivar
345
        if(rank!= null && rank.equals(Rank.CULTIVAR())){
346
            ICultivarPlantName cultivar = TaxonNameFactory.NewCultivarInstance(rank);
347
            cultivar.setGenusOrUninomial(ep1String);
348
            cultivar.setSpecificEpithet(ep2String);
349
            cultivar.setCultivarName(ep3String);
350
            name = cultivar;
351
        }
352
        //botanical names
353
        else{
354
            name = TaxonNameFactory.NewBotanicalInstance(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
            if(CdmUtils.isNotBlank(ep3String)){
370
                name.setInfraSpecificEpithet(ep3String);
371
            }
372

    
373

    
374
            //nomenclatural status
375
            if(CdmUtils.isNotBlank(nomZusatzString)){
376
                NomenclaturalStatusType statusType = makeNomenclaturalStatus(id, state, nomZusatzString);
377
                if(statusType!=null){
378
                    NomenclaturalStatus status = NomenclaturalStatus.NewInstance(statusType);
379
                    //special case for invalid names where the DB entry contains
380
                    //additional information in brackets e.g. "nom. inval. (sine basion.)"
381
                    if(statusType.equals(NomenclaturalStatusType.INVALID()) || statusType.equals(NomenclaturalStatusType.REJECTED()) ){
382
                        Pattern pattern = Pattern.compile("\\((.*?)\\)");
383
                        Matcher matcher = pattern.matcher(nomZusatzString);
384
                        if (matcher.find()){
385
                            status.setRuleConsidered(matcher.group(1));
386
                        }
387
                    }
388
                    name.addStatus(status);
389
                }
390
            }
391
            //hybrid
392
            if(CdmUtils.isNotBlank(hybString)){
393
                //more than two hybrids not yet handled by name parser
394
                //TODO: use parser when implemented to fully support hybrids
395
                if(taxNameString.split(RedListUtil.HYB_SIGN).length>2){
396
                    name = TaxonNameFactory.NewBotanicalInstance(rank);
397
                    name.setTitleCache(taxNameString, true);
398
                }
399
                else if(hybString.equals(RedListUtil.HYB_X)){
400
                    name.setBinomHybrid(true);
401
                }
402
                else if(hybString.equals(RedListUtil.HYB_G)){
403
                    name.setMonomHybrid(true);
404
                }
405
                else if(hybString.equals(RedListUtil.HYB_XF) || hybString.equals(RedListUtil.HYB_XU)){
406
                    name.setHybridFormula(true);
407
                    String fullFormula = buildHybridFormula(ep1String, ep2String, ep3String, rank);
408
                    name = NonViralNameParserImpl.NewInstance().parseFullName(fullFormula, NomenclaturalCode.ICNAFP, rank);
409
                }
410
                else if(hybString.equals(RedListUtil.HYB_N)){
411
                    name = NonViralNameParserImpl.NewInstance().parseFullName(taxNameString, NomenclaturalCode.ICNAFP, rank);
412
                }
413
                else if(hybString.equals(RedListUtil.HYB_GF)){
414
                    if(ep1String.contains(RedListUtil.HYB_SIGN)){
415
                        name = NonViralNameParserImpl.NewInstance().parseFullName(ep1String, NomenclaturalCode.ICNAFP, rank);
416
                    }
417
                    else{
418
                        RedListUtil.logMessage(id, "HYB is "+hybString+" but "+RedListUtil.HYB+" does not contain "+RedListUtil.HYB_SIGN, logger);
419
                    }
420
                }
421
                else if(hybString.equals(RedListUtil.HYB_XS)){
422
                    //nothing to do
423
                }
424
                else{
425
                    logger.error("HYB value "+hybString+" not yet handled");
426
                }
427
                //save hybrid formula
428
                if(CdmUtils.isNotBlank(formelString)){
429
                    Annotation annotation = Annotation.NewDefaultLanguageInstance(formelString);
430
                    annotation.setAnnotationType(AnnotationType.TECHNICAL());
431
                    name.addAnnotation(annotation);
432
                }
433
            }
434
        }
435

    
436
        //add source
437
        ImportHelper.setOriginalSource(name, state.getTransactionalSourceReference(), id, RedListUtil.NAME_NAMESPACE);
438

    
439
        namesToSave.add(name);
440
        return name;
441
    }
442

    
443
    private String buildHybridFormula(String ep1String, String ep2String, String ep3String, Rank rank) {
444
        String fullFormula = null;
445
        if(ep1String.contains(RedListUtil.HYB_SIGN)){
446
            fullFormula = ep1String;
447
        }
448
        else if(ep2String.contains(RedListUtil.HYB_SIGN)){
449
            String[] split = ep2String.split(RedListUtil.HYB_SIGN);
450
            String hybridFormula1 = ep1String+" "+split[0].trim();
451
            String hybridFormula2 = ep1String+" "+split[1].trim();
452
            //check if the genus is mentioned in EP2 or not
453
            String[] secondHybrid = split[1].trim().split(" ");
454
            //check if the genus is abbreviated like e.g. Centaurea jacea × C. decipiens
455
            if(secondHybrid.length>1 && secondHybrid[0].matches("[A-Z]\\.")){
456
                hybridFormula2 = ep1String+" "+split[1].trim().substring(3);
457
            }
458
            else if(secondHybrid.length>1 && secondHybrid[0].matches("[A-Z].*")){
459
                hybridFormula2 = split[1].trim();
460
            }
461
            if(CdmUtils.isNotBlank(ep3String)){
462
                hybridFormula1 += " "+rank.getAbbreviation()+" "+ep3String;
463
                hybridFormula2 += " "+rank.getAbbreviation()+" "+ep3String;
464
            }
465
            fullFormula = hybridFormula1+" "+RedListUtil.HYB_SIGN+" "+hybridFormula2;
466
        }
467
        else if(ep3String.contains(RedListUtil.HYB_SIGN)){
468
            String[] split = ep3String.split(RedListUtil.HYB_SIGN);
469
            String hybridFormula1 = ep1String+" "+ep2String+" "+rank.getAbbreviation()+" "+split[0].trim();
470
            String hybridFormula2 = ep1String+" "+ep2String+" "+rank.getAbbreviation()+" "+split[1].trim();
471
            //check if the genus is mentioned in EP3 or not
472
            String[] secondHybrid = split[1].trim().split(" ");
473
            //check if the genus is abbreviated like e.g. Centaurea jacea jacea × C. jacea subsp. decipiens
474
            if(secondHybrid.length>1 && secondHybrid[0].matches("[A-Z]\\.")){
475
                hybridFormula2 = ep1String+" "+split[1].trim().substring(3);
476
            }
477
            else if(secondHybrid.length>1 && secondHybrid[0].matches("[A-Z].*")){
478
                hybridFormula2 = split[1].trim();
479
            }
480
            fullFormula = hybridFormula1+" "+RedListUtil.HYB_SIGN+" "+hybridFormula2;
481
        }
482
        return fullFormula;
483
    }
484

    
485
    private void checkNameConsistency(long id, String nomZusatzString, String taxZusatzString,
486
            String zusatzString, String authorString, String hybString, INonViralName name) {
487
        String authorshipCache = name.getAuthorshipCache();
488
        //FIXME: remove split length check when name parser can parse multiple hybrid parents
489
        if(hybString.equals(RedListUtil.HYB_XF) && name.getTitleCache().split(RedListUtil.HYB_SIGN).length==2){
490
            if(name.getHybridChildRelations().isEmpty()){
491
                RedListUtil.logMessage(id, "Hybrid formula but no hybrid child relations: "+name.getTitleCache(), logger);
492
                return;
493
            }
494
            return;
495
        }
496

    
497
        if(CdmUtils.isNotBlank(zusatzString)){
498
            authorString = authorString.replace(", "+zusatzString, "");
499
        }
500
        if(CdmUtils.isNotBlank(nomZusatzString)){
501
            authorString = authorString.replace(", "+nomZusatzString, "");
502
        }
503
        if(CdmUtils.isNotBlank(taxZusatzString)){
504
            authorString = authorString.replace(", "+taxZusatzString, "");
505
        }
506
        if(authorString.equals(RedListUtil.AUCT)){
507
            authorString = "";
508
        }
509
        if(!STRICT_TITLE_CHECK && authorString.matches(".*ex.*ex.*")){
510
            return;
511
        }
512
        if(STRICT_TITLE_CHECK){
513
            if(!authorString.equals(authorshipCache)){
514
                RedListUtil.logMessage(id, "Authorship inconsistent! name.authorhshipCache <-> Column "+RedListUtil.AUTOR+": "+authorshipCache+" <-> "+authorString, logger);
515
            }
516
        }
517
        else{
518
            if(CdmUtils.isNotBlank(authorString) && !authorString.startsWith(authorshipCache)){
519
                RedListUtil.logMessage(id, "Authorship inconsistent! name.authorhshipCache <-> Column "+RedListUtil.AUTOR+": "+authorshipCache+" <-> "+authorString, logger);
520
            }
521
        }
522
    }
523

    
524
    private void checkTaxonConsistency(long id, String taxNameString, String hybString, String epi1String, String epi2String, String epi3String, TaxonBase<?> taxonBase, RedListGefaesspflanzenImportState state) {
525
        if(taxNameString.split(RedListUtil.HYB_SIGN).length>2){
526
            RedListUtil.logInfoMessage(id, "multiple hybrid signs. No name check for "+taxNameString, logger);
527
            return;
528
        }
529

    
530
        String nameCache = taxonBase.getName().getNameCache().trim();
531
        taxNameString = taxNameString.trim();
532
        taxNameString = taxNameString.replaceAll(" +", " ");
533

    
534

    
535
        if((hybString.equals(RedListUtil.HYB_X) || hybString.equals(RedListUtil.HYB_N))
536
                && nameCache.matches(".*\\s"+RedListUtil.HYB_SIGN+"\\w.*")){
537
            taxNameString = taxNameString.replace(" "+RedListUtil.HYB_SIGN+" ", " "+RedListUtil.HYB_SIGN);//hybrid sign has no space after it in titleCache for binomial hybrids
538
            taxNameString = taxNameString.replace(" x ", " "+RedListUtil.HYB_SIGN);//in some cases a standard 'x' is used
539
        }
540
        else if(hybString.equals(RedListUtil.HYB_G)){
541
            taxNameString = taxNameString.replace("X ", RedListUtil.HYB_SIGN);
542
        }
543
        else if(hybString.equals(RedListUtil.HYB_GF)){
544
            taxNameString = taxNameString.replace(" "+RedListUtil.HYB_SIGN+" ", " "+RedListUtil.HYB_SIGN);
545
        }
546
        else if(hybString.equals(RedListUtil.HYB_XF)){
547
            nameCache = taxonBase.getName().getTitleCache();
548
            if(nameCache.contains("sec")){
549
                nameCache = nameCache.substring(0, nameCache.indexOf("sec"));
550
            }
551
            if(!STRICT_TITLE_CHECK){
552
                taxNameString = buildHybridFormula(epi1String, epi2String, epi3String, taxonBase.getName().getRank());
553
            }
554
            if(taxNameString.split(RedListUtil.HYB_SIGN).length==1){
555
                taxNameString = taxNameString.replace(RedListUtil.HYB_SIGN+" ", RedListUtil.HYB_SIGN);
556
            }
557
        }
558

    
559
        if(taxNameString.endsWith("- Gruppe")){
560
            taxNameString = taxNameString.replaceAll("- Gruppe", "species group");
561
        }
562
        if(taxNameString.endsWith("- group")){
563
            taxNameString = taxNameString.replaceAll("- group", "species group");
564
        }
565

    
566
        taxNameString = taxNameString.replace("agg.", "aggr.");
567
        taxNameString = taxNameString.replace("[ranglos]", "[unranked]");
568

    
569
        if(taxonBase.getName().getRank()!=null){
570
            if(taxonBase.getName().getRank().equals(Rank.PROLES())){
571
                taxNameString = taxNameString.replace("proles", "prol.");
572
            }
573
            else if(taxonBase.getName().getRank().equals(state.getRank(RedListUtil.uuidRankCollectionSpecies))){
574
                taxNameString = taxNameString.replace("\"Sammelart\"", "\"Coll. Species\"");
575
            }
576
        }
577
        if(STRICT_TITLE_CHECK){
578
            if(!taxNameString.trim().equals(nameCache)){
579
                RedListUtil.logMessage(id, "Taxon name inconsistent! taxon.nameCache <-> Column "+RedListUtil.TAXNAME+": "+nameCache+" <-> "+taxNameString, logger);
580
            }
581
        }
582
        else{
583
            if(!taxNameString.startsWith(nameCache)){
584
                RedListUtil.logMessage(id, "Taxon name inconsistent! taxon.nameCache <-> Column "+RedListUtil.TAXNAME+": "+nameCache+" <-> "+taxNameString, logger);
585
            }
586
        }
587
    }
588

    
589
    private Rank makeRank(long id, RedListGefaesspflanzenImportState state, String rankStr, boolean hasSpecificEpithet) {
590
        Rank rank = null;
591
        try {
592
            if(rankStr.equals("ORA")){
593
                //special handling for ORA because of two possibilities
594
                if(hasSpecificEpithet){
595
                    //re-load term because the representation was changed before
596
                    return (Rank) getTermService().load(Rank.uuidInfraspecificTaxon);
597
                }
598
                else{
599
                    return Rank.UNRANKED_INFRAGENERIC();
600
                }
601
            }
602
            else if(rankStr.equals("SAM")){
603
                return getRank(state, RedListUtil.uuidRankCollectionSpecies, "Collective Species", "Collective Species", "\"Coll. Species\"", (OrderedTermVocabulary<Rank>) Rank.GENUS().getVocabulary(), null, RankClass.SpeciesGroup);
604
            }
605
            else if(rankStr.equals("SPR")){
606
                return getRank(state, RedListUtil.uuidRankSubproles, "Subproles", "Subproles", "subproles", (OrderedTermVocabulary<Rank>) Rank.GENUS().getVocabulary(), null, RankClass.Infraspecific);
607
            }
608
            else if(rankStr.equals("MOD")){
609
                return getRank(state, RedListUtil.uuidRankModification, "Modification", "Modification", "modificatio", (OrderedTermVocabulary<Rank>) Rank.GENUS().getVocabulary(), null, RankClass.Infraspecific);
610
            }
611
            else if(rankStr.equals("LUS")){
612
                return getRank(state, RedListUtil.uuidRankLusus, "Lusus", "Lusus", "lusus", (OrderedTermVocabulary<Rank>) Rank.GENUS().getVocabulary(), null, RankClass.Infraspecific);
613
            }
614
            else if(rankStr.equals("SPI")){
615
                return getRank(state, RedListUtil.uuidRankSubspeciesPrincipes, "Subspecies principes", "Subspecies principes", "subsp. princ.", (OrderedTermVocabulary<Rank>) Rank.GENUS().getVocabulary(), null, RankClass.Infraspecific);
616
            }
617
            else if(rankStr.equals("KMB")){
618
                return getRank(state, RedListUtil.uuidRankCombination, "Combination", "Combination", "", (OrderedTermVocabulary<Rank>) Rank.GENUS().getVocabulary(), null, RankClass.Infraspecific);
619
            }
620
            else if(rankStr.equals("'FO")){
621
                return getRank(state, RedListUtil.uuidRankForme, "Forme'", "Forme'", "", (OrderedTermVocabulary<Rank>) Rank.GENUS().getVocabulary(), null, RankClass.Infraspecific);
622
            }
623
            else{
624
                rank = state.getTransformer().getRankByKey(rankStr);
625
            }
626
        } catch (UndefinedTransformerMethodException e) {
627
            e.printStackTrace();
628
        }
629
        if(rank==null){
630
            RedListUtil.logMessage(id, rankStr+" could not be associated to a known rank.", logger);
631
        }
632
        return rank;
633
    }
634

    
635
    private NomenclaturalStatusType makeNomenclaturalStatus(long id, RedListGefaesspflanzenImportState state, String nomZusatzString) {
636
        NomenclaturalStatusType status = null;
637
        try {
638
            status = state.getTransformer().getNomenclaturalStatusByKey(nomZusatzString);
639
        } catch (UndefinedTransformerMethodException e) {
640
            e.printStackTrace();
641
        }
642
        if(status==null){
643
            RedListUtil.logMessage(id, nomZusatzString+" could not be associated to a known nomenclatural status.", logger);
644
        }
645
        return status;
646
    }
647

    
648

    
649

    
650
    @Override
651
    public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
652
            RedListGefaesspflanzenImportState state) {
653
        Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
654
        Map<String, AgentBase<?>> authorMap = new HashMap<String, AgentBase<?>>();
655

    
656
        try {
657
            while (rs.next()){
658
                String authorKombString = rs.getString(RedListUtil.AUTOR_KOMB);
659

    
660
                if(authorKombString.contains(RedListUtil.EX)){
661
                    String[] kombSplit = authorKombString.split(RedListUtil.EX);
662
                    for (int i = 0; i < kombSplit.length; i++) {
663
                        if(!authorMap.containsKey(kombSplit[i])){
664
                            authorMap.put(kombSplit[i], getAgentService().load(state.getAuthorMap().get(kombSplit[i])));
665
                        }
666
                    }
667
                }
668
                else if(CdmUtils.isNotBlank(authorKombString) && !authorMap.containsKey(authorKombString)){
669
                    authorMap.put(authorKombString, getAgentService().load(state.getAuthorMap().get(authorKombString)));
670
                }
671

    
672
                String authorBasiString = rs.getString(RedListUtil.AUTOR_BASI);
673
                //basionym author
674
                if(authorBasiString.contains(RedListUtil.EX)){
675
                    String[] basiSplit = authorBasiString.split(RedListUtil.EX);
676
                    for (int i = 0; i < basiSplit.length; i++) {
677
                        if(!authorMap.containsKey(basiSplit[i])){
678
                            authorMap.put(basiSplit[i], getAgentService().load(state.getAuthorMap().get(basiSplit[i])));
679
                        }
680
                    }
681
                }
682
                else if(CdmUtils.isNotBlank(authorBasiString) && !authorMap.containsKey(authorBasiString)){
683
                    authorMap.put(authorBasiString, getAgentService().load(state.getAuthorMap().get(authorBasiString)));
684
                }
685
            }
686
        } catch (SQLException e) {
687
            e.printStackTrace();
688
        }
689
        result.put(RedListUtil.AUTHOR_NAMESPACE, authorMap);
690

    
691
        return result;
692
    }
693

    
694
    @Override
695
    protected boolean doCheck(RedListGefaesspflanzenImportState state) {
696
        return false;
697
    }
698

    
699
    @Override
700
    protected boolean isIgnore(RedListGefaesspflanzenImportState state) {
701
        return false;
702
    }
703

    
704
}
(5-5/9)