Project

General

Profile

Download (33.2 KB) Statistics
| Branch: | Revision:
1
/**
2
 * Copyright (C) 2007 EDIT
3
 * European Distributed Institute of Taxonomy
4
 * http://www.e-taxonomy.eu
5
 *
6
 * The contents of this file are subject to the Mozilla Public License Version 1.1
7
 * See LICENSE.TXT at the top of this package for the full license terms.
8
 */
9

    
10
package eu.etaxonomy.cdm.io.redlist.gefaesspflanzen;
11

    
12
import java.sql.ResultSet;
13
import java.sql.SQLException;
14
import java.util.HashMap;
15
import java.util.HashSet;
16
import java.util.Map;
17
import java.util.Set;
18
import java.util.regex.Matcher;
19
import java.util.regex.Pattern;
20

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

    
24
import eu.etaxonomy.cdm.common.CdmUtils;
25
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
26
import eu.etaxonomy.cdm.io.common.DbImportBase;
27
import eu.etaxonomy.cdm.io.common.IPartitionedIO;
28
import eu.etaxonomy.cdm.io.common.ImportHelper;
29
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
30
import eu.etaxonomy.cdm.io.common.mapping.UndefinedTransformerMethodException;
31
import eu.etaxonomy.cdm.model.agent.AgentBase;
32
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
33
import eu.etaxonomy.cdm.model.common.AnnotatableEntity;
34
import eu.etaxonomy.cdm.model.common.Annotation;
35
import eu.etaxonomy.cdm.model.common.AnnotationType;
36
import eu.etaxonomy.cdm.model.common.CdmBase;
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.BotanicalName;
42
import eu.etaxonomy.cdm.model.name.CultivarPlantName;
43
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
44
import eu.etaxonomy.cdm.model.name.NomenclaturalStatus;
45
import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
46
import eu.etaxonomy.cdm.model.name.NonViralName;
47
import eu.etaxonomy.cdm.model.name.Rank;
48
import eu.etaxonomy.cdm.model.name.RankClass;
49
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
50
import eu.etaxonomy.cdm.model.taxon.Synonym;
51
import eu.etaxonomy.cdm.model.taxon.Taxon;
52
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
53
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
54
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
55
import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
56

    
57
/**
58
 *
59
 * @author pplitzner
60
 * @date Mar 1, 2016
61
 *
62
 */
63

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

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

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

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

    
74
    private static final boolean STRICT_TITLE_CHECK = false;
75

    
76
    public RedListGefaesspflanzenImportNames() {
77
        super(tableName, pluralString);
78
    }
79

    
80
    @Override
81
    protected String getIdQuery(RedListGefaesspflanzenImportState state) {
82
        return "SELECT SEQNUM "
83
                + "FROM V_TAXATLAS_D20_EXPORT t "
84
                + " ORDER BY SEQNUM";
85
    }
86

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

    
96
    @Override
97
    protected void doInvoke(RedListGefaesspflanzenImportState state) {
98
        super.doInvoke(state);
99
    }
100

    
101

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

    
111
            }
112
        } catch (SQLException e) {
113
            e.printStackTrace();
114
        }
115

    
116
        getNameService().saveOrUpdate(namesToSave);
117
        getTaxonService().saveOrUpdate(taxaToSave);
118
        return true;
119
    }
120

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

    
134
        //---NAME---
135
        NonViralName<?> name = importName(state, rs, namesToSave);
136

    
137

    
138
        //--- AUTHORS ---
139
        importAuthors(state, rs, name);
140

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

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

    
167
            ImportHelper.setOriginalSource(checklistTaxon, state.getTransactionalSourceReference(), id, RedListUtil.TAXON_CHECKLISTE_NAMESPACE);
168
            taxaToSave.add(checklistTaxon);
169
        }
170

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

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

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

    
195
    private TaxonBase<?> importTaxon(ResultSet rs, NonViralName<?> name, RedListGefaesspflanzenImportState state) throws SQLException {
196

    
197
        long id = rs.getLong(RedListUtil.NAMNR);
198
        String taxNameString = rs.getString(RedListUtil.TAXNAME);
199
        String epi1String = rs.getString(RedListUtil.EPI1);
200
        String epi2String = rs.getString(RedListUtil.EPI2);
201
        String epi3String = rs.getString(RedListUtil.EPI3);
202
        String gueltString = rs.getString(RedListUtil.GUELT);
203
        String trivialString = rs.getString(RedListUtil.TRIVIAL);
204
        String authorBasiString = rs.getString(RedListUtil.AUTOR_BASI);
205
        String hybString = rs.getString(RedListUtil.HYB);
206
        String florString = rs.getString(RedListUtil.FLOR);
207
        String atlasIdxString = rs.getString(RedListUtil.ATLAS_IDX);
208
        String kartString = rs.getString(RedListUtil.KART);
209
        String rl2015String = rs.getString(RedListUtil.RL2015);
210
        String ehrdString = rs.getString(RedListUtil.EHRD);
211
        String wisskString = rs.getString(RedListUtil.WISSK);
212

    
213
        TaxonBase<?> taxonBase = null;
214
        if(authorBasiString.trim().contains(RedListUtil.AUCT)){
215
            taxonBase = Taxon.NewInstance(name, null);
216
            taxonBase.setAppendedPhrase(RedListUtil.AUCT);
217
        }
218
        else if(gueltString.equals(RedListUtil.GUELT_ACCEPTED_TAXON)){
219
            taxonBase = Taxon.NewInstance(name, null);
220
        }
221
        else if(gueltString.equals(RedListUtil.GUELT_SYNONYM) || gueltString.equals(RedListUtil.GUELT_BASIONYM)){
222
            taxonBase = Synonym.NewInstance(name, null);
223
        }
224
        else{
225
            return null;
226
        }
227

    
228
        //common name
229
        if(taxonBase.isInstanceOf(Taxon.class) && trivialString!=null){
230
            Taxon taxon = HibernateProxyHelper.deproxy(taxonBase, Taxon.class);
231
            TaxonDescription description = TaxonDescription.NewInstance(taxon);
232
            description.addElement(CommonTaxonName.NewInstance(trivialString, Language.GERMAN()));
233
        }
234

    
235
        //add annotations
236
        addAnnotation(RedListUtil.FLOR+": "+florString, taxonBase);
237
        addAnnotation(RedListUtil.ATLAS_IDX+": "+atlasIdxString, taxonBase);
238
        addAnnotation(RedListUtil.KART+": "+kartString, taxonBase);
239
        addAnnotation(RedListUtil.RL2015+": "+rl2015String, taxonBase);
240
        addAnnotation(RedListUtil.EHRD+": "+ehrdString, taxonBase);
241
        addAnnotation(RedListUtil.WISSK+": "+wisskString, taxonBase);
242

    
243
        //check taxon name consistency
244
        checkTaxonConsistency(id, taxNameString, hybString, epi1String, epi2String, epi3String, taxonBase, state);
245
        return taxonBase;
246
    }
247

    
248
    private void addAnnotation(String string, AnnotatableEntity entity) {
249
        if(CdmUtils.isNotBlank(string)){
250
            entity.addAnnotation(Annotation.NewInstance(string, AnnotationType.TECHNICAL(), Language.GERMAN()));
251
        }
252
    }
253

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

    
256
        long id = rs.getLong(RedListUtil.NAMNR);
257
        String nomZusatzString = rs.getString(RedListUtil.NOM_ZUSATZ);
258
        String taxZusatzString = rs.getString(RedListUtil.TAX_ZUSATZ);
259
        String zusatzString = rs.getString(RedListUtil.ZUSATZ);
260
        String authorKombString = rs.getString(RedListUtil.AUTOR_KOMB);
261
        String authorBasiString = rs.getString(RedListUtil.AUTOR_BASI);
262
        String hybString = rs.getString(RedListUtil.HYB);
263

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

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

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

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

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

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

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

    
375

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

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

    
441
        namesToSave.add(name);
442
        return name;
443
    }
444

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

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

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

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

    
529
        String nameCache = HibernateProxyHelper.deproxy(taxonBase.getName(), NonViralName.class).getNameCache().trim();
530
        taxNameString = taxNameString.trim();
531
        taxNameString = taxNameString.replaceAll(" +", " ");
532

    
533

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

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

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

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

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

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

    
642

    
643

    
644
    @Override
645
    public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
646
            RedListGefaesspflanzenImportState state) {
647
        Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
648
        Map<String, AgentBase<?>> authorMap = new HashMap<String, AgentBase<?>>();
649

    
650
        try {
651
            while (rs.next()){
652
                String authorKombString = rs.getString(RedListUtil.AUTOR_KOMB);
653

    
654
                if(authorKombString.contains(RedListUtil.EX)){
655
                    String[] kombSplit = authorKombString.split(RedListUtil.EX);
656
                    for (int i = 0; i < kombSplit.length; i++) {
657
                        if(!authorMap.containsKey(kombSplit[i])){
658
                            authorMap.put(kombSplit[i], getAgentService().load(state.getAuthorMap().get(kombSplit[i])));
659
                        }
660
                    }
661
                }
662
                else if(CdmUtils.isNotBlank(authorKombString) && !authorMap.containsKey(authorKombString)){
663
                    authorMap.put(authorKombString, getAgentService().load(state.getAuthorMap().get(authorKombString)));
664
                }
665

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

    
685
        return result;
686
    }
687

    
688
    @Override
689
    protected boolean doCheck(RedListGefaesspflanzenImportState state) {
690
        return false;
691
    }
692

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

    
698
}
(5-5/8)