Project

General

Profile

Download (29.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.CdmBase;
34
import eu.etaxonomy.cdm.model.common.Language;
35
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
36
import eu.etaxonomy.cdm.model.description.TaxonDescription;
37
import eu.etaxonomy.cdm.model.name.BotanicalName;
38
import eu.etaxonomy.cdm.model.name.NomenclaturalStatus;
39
import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
40
import eu.etaxonomy.cdm.model.name.NonViralName;
41
import eu.etaxonomy.cdm.model.name.Rank;
42
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
43
import eu.etaxonomy.cdm.model.taxon.Synonym;
44
import eu.etaxonomy.cdm.model.taxon.Taxon;
45
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
46
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
47
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
48
import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
49

    
50
/**
51
 *
52
 * @author pplitzner
53
 * @date Mar 1, 2016
54
 *
55
 */
56

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

    
61
    private static final Logger logger = Logger.getLogger(RedListGefaesspflanzenImportNames.class);
62

    
63
    private static final String tableName = "Rote Liste Gefäßpflanzen";
64

    
65
    private static final String pluralString = "names";
66

    
67
    private static final boolean STRICT_TITLE_CHECK = false;
68

    
69
    public RedListGefaesspflanzenImportNames() {
70
        super(tableName, pluralString);
71
    }
72

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

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

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

    
94

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

    
104
            }
105
        } catch (SQLException e) {
106
            e.printStackTrace();
107
        }
108

    
109
        getNameService().saveOrUpdate(namesToSave);
110
        getTaxonService().saveOrUpdate(taxaToSave);
111
        return true;
112
    }
113

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

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

    
130

    
131
        //--- AUTHORS ---
132
        importAuthors(state, rs, name);
133

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

    
141
        //---CONCEPT RELATIONSHIPS---
142
        //checklist
143
        TaxonBase<?> checklistTaxon = null;
144
        if(CdmUtils.isNotBlank(clTaxonString) && !clTaxonString.trim().equals("-")){
145
            checklistTaxon = (TaxonBase<?>) taxonBase.clone();
146
            if(checklistTaxon.isInstanceOf(Taxon.class)){
147
                TaxonRelationship relation = HibernateProxyHelper.deproxy(checklistTaxon, Taxon.class).addTaxonRelation(HibernateProxyHelper.deproxy(taxonBase, Taxon.class), TaxonRelationshipType.CONGRUENT_TO(), null, null);
148
                relation.setDoubtful(true);
149
            }
150

    
151
            ImportHelper.setOriginalSource(checklistTaxon, state.getTransactionalSourceReference(), id, RedListUtil.TAXON_CHECKLISTE_NAMESPACE);
152
            taxaToSave.add(checklistTaxon);
153
        }
154
        //E, W, K, AW, AO, R, O, S
155
        addConceptRelation(relationE, RedListUtil.CLASSIFICATION_NAMESPACE_E, taxonBase, checklistTaxon, taxaToSave, id, state);
156
        addConceptRelation(relationW, RedListUtil.CLASSIFICATION_NAMESPACE_W, taxonBase, checklistTaxon, taxaToSave, id, state);
157
        addConceptRelation(relationK, RedListUtil.CLASSIFICATION_NAMESPACE_K, taxonBase, checklistTaxon, taxaToSave, id, state);
158
        addConceptRelation(relationAW, RedListUtil.CLASSIFICATION_NAMESPACE_AW, taxonBase, checklistTaxon, taxaToSave, id, state);
159
        addConceptRelation(relationAO, RedListUtil.CLASSIFICATION_NAMESPACE_AO, taxonBase, checklistTaxon, taxaToSave, id, state);
160
        addConceptRelation(relationR, RedListUtil.CLASSIFICATION_NAMESPACE_R, taxonBase, checklistTaxon, taxaToSave, id, state);
161
        addConceptRelation(relationO, RedListUtil.CLASSIFICATION_NAMESPACE_O, taxonBase, checklistTaxon, taxaToSave, id, state);
162
        addConceptRelation(relationS, RedListUtil.CLASSIFICATION_NAMESPACE_S, taxonBase, checklistTaxon, taxaToSave, id, state);
163

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

    
169
    private void addConceptRelation(String relationString, String classificationNamespace, TaxonBase<?> gesamtListeTaxon, TaxonBase<?> checkListenTaxon, Set<TaxonBase> taxaToSave, long id, RedListGefaesspflanzenImportState state){
170
        if(CdmUtils.isNotBlank(relationString) && !relationString.equals(".")){
171
            String substring = relationString.substring(relationString.length()-1, relationString.length());
172
            TaxonRelationshipType taxonRelationshipTypeByKey = new RedListGefaesspflanzenTransformer().getTaxonRelationshipTypeByKey(substring);
173
            if(taxonRelationshipTypeByKey==null){
174
                RedListUtil.logMessage(id, "Could not interpret relationship "+relationString+" for taxon "+gesamtListeTaxon.generateTitle(), logger);
175
            }
176
            //there is no type "included in" so we have to reverse the direction
177
            if(substring.equals("<")){
178
                cloneTaxon(gesamtListeTaxon, checkListenTaxon, taxonRelationshipTypeByKey, taxaToSave, id, classificationNamespace, true, false, state);
179
            }
180
            else{
181
                cloneTaxon(gesamtListeTaxon, checkListenTaxon, taxonRelationshipTypeByKey, taxaToSave, id, classificationNamespace, false, false, state);
182
            }
183
        }
184
    }
185

    
186
    /**
187
     * 1. clone new taxon of gesamtListeTaxon with the same name (in that classification)<br>
188
     * 2. create concept relationship from clone to gesamtListeTaxon/checklisteTaxon or from its accepted taxon if it is synonym<br>
189
     *<br>
190
     * <b>NOTE:</b> the {@link TaxonRelationshipType} passed as parameter is
191
     * directed <b>from the clone</b> to the taxon.<br>
192
     * This can be changed with parameter <i>reverseRelation</i>
193
     * @return cloned taxon
194
     */
195
    private Taxon cloneTaxon(final TaxonBase<?> gesamtListeTaxon, final TaxonBase<?> checklisteTaxon, TaxonRelationshipType relationFromCloneToTaxon, Set<TaxonBase> taxaToSave, long id, String sourceNameSpace, boolean reverseRelation, boolean doubtful, RedListGefaesspflanzenImportState state){
196
        Taxon acceptedGesamtListeTaxon = getAcceptedTaxon(gesamtListeTaxon);
197
        Taxon acceptedChecklistTaxon = getAcceptedTaxon(checklisteTaxon);
198
        Taxon clonedTaxon = null;
199

    
200
        if(gesamtListeTaxon.isInstanceOf(Taxon.class)){
201
            clonedTaxon = HibernateProxyHelper.deproxy(gesamtListeTaxon.clone(), Taxon.class);
202
        }
203
        else if(gesamtListeTaxon.isInstanceOf(Synonym.class)){
204
            clonedTaxon = Taxon.NewInstance(gesamtListeTaxon.getName(), gesamtListeTaxon.getSec());
205
        }
206
        else{
207
            RedListUtil.logMessage(id, "Taxon base "+gesamtListeTaxon+" is neither taxon nor synonym! Taxon could not be cloned", logger);
208
            return null;
209
        }
210

    
211
        if(reverseRelation){
212
            if(acceptedGesamtListeTaxon!=null){
213
                TaxonRelationship taxonRelation = acceptedGesamtListeTaxon.addTaxonRelation(clonedTaxon, relationFromCloneToTaxon, null, null);
214
                taxonRelation.setDoubtful(doubtful);
215
            }
216
            if(acceptedChecklistTaxon!=null) {
217
                TaxonRelationship taxonRelation = acceptedChecklistTaxon.addTaxonRelation(clonedTaxon, relationFromCloneToTaxon, null, null);
218
                taxonRelation.setDoubtful(doubtful);
219
            }
220
        }
221
        else {
222
            if(acceptedGesamtListeTaxon!=null){
223
                TaxonRelationship taxonRelation = clonedTaxon.addTaxonRelation(acceptedGesamtListeTaxon, relationFromCloneToTaxon, null, null);
224
                taxonRelation.setDoubtful(doubtful);
225
            }
226
            if(acceptedChecklistTaxon!=null) {
227
                TaxonRelationship taxonRelation = clonedTaxon.addTaxonRelation(acceptedChecklistTaxon, relationFromCloneToTaxon, null, null);
228
                taxonRelation.setDoubtful(doubtful);
229
            }
230
        }
231

    
232
        ImportHelper.setOriginalSource(clonedTaxon, state.getTransactionalSourceReference(), id, sourceNameSpace);
233
        taxaToSave.add(clonedTaxon);
234
        return clonedTaxon;
235
    }
236

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

    
239
        long id = rs.getLong(RedListUtil.NAMNR);
240
        String taxNameString = rs.getString(RedListUtil.TAXNAME);
241
        String gueltString = rs.getString(RedListUtil.GUELT);
242
        String trivialString = rs.getString(RedListUtil.TRIVIAL);
243
        String authorBasiString = rs.getString(RedListUtil.AUTOR_BASI);
244
        String hybString = rs.getString(RedListUtil.HYB);
245

    
246
        TaxonBase<?> taxonBase = null;
247
        if(authorBasiString.trim().contains(RedListUtil.AUCT)){
248
            taxonBase = Taxon.NewInstance(name, null);
249
            taxonBase.setAppendedPhrase(RedListUtil.AUCT);
250
        }
251
        else if(gueltString.equals(RedListUtil.GUELT_ACCEPTED_TAXON)){
252
            taxonBase = Taxon.NewInstance(name, null);
253
        }
254
        else if(gueltString.equals(RedListUtil.GUELT_SYNONYM) || gueltString.equals(RedListUtil.GUELT_BASIONYM)){
255
            taxonBase = Synonym.NewInstance(name, null);
256
        }
257
        else{
258
            return null;
259
        }
260

    
261
        //common name
262
        if(taxonBase.isInstanceOf(Taxon.class) && trivialString!=null){
263
            Taxon taxon = HibernateProxyHelper.deproxy(taxonBase, Taxon.class);
264
            TaxonDescription description = TaxonDescription.NewInstance(taxon);
265
            description.addElement(CommonTaxonName.NewInstance(trivialString, Language.GERMAN()));
266
        }
267

    
268
        //check taxon name consistency
269
        checkTaxonNameConsistency(id, taxNameString, hybString, taxonBase);
270
        return taxonBase;
271
    }
272

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

    
275
        long id = rs.getLong(RedListUtil.NAMNR);
276
        String nomZusatzString = rs.getString(RedListUtil.NOM_ZUSATZ);
277
        String taxZusatzString = rs.getString(RedListUtil.TAX_ZUSATZ);
278
        String zusatzString = rs.getString(RedListUtil.ZUSATZ);
279
        String authorKombString = rs.getString(RedListUtil.AUTOR_KOMB);
280
        String authorBasiString = rs.getString(RedListUtil.AUTOR_BASI);
281

    
282
        //combination author
283
        if(authorKombString.contains(RedListUtil.EX)){
284
            //TODO: what happens with multiple ex authors??
285
            String[] kombSplit = authorKombString.split(RedListUtil.EX);
286
            if(kombSplit.length!=2){
287
                RedListUtil.logMessage(id, "Multiple ex combination authors found", logger);
288
            }
289
            for (int i = 0; i < kombSplit.length; i++) {
290
                if(i==0){
291
                    //first author is ex author
292
                    TeamOrPersonBase<?> authorKomb = (TeamOrPersonBase<?>) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, kombSplit[i]);
293
                    name.setExCombinationAuthorship(authorKomb);
294
                }
295
                else{
296
                    TeamOrPersonBase<?> authorKomb = (TeamOrPersonBase<?>) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, kombSplit[i]);
297
                    name.setCombinationAuthorship(authorKomb);
298
                }
299
            }
300
        }
301
        else if(authorKombString.trim().contains(RedListUtil.AUCT)){
302
            RedListUtil.logMessage(id, "AUCT information in "+RedListUtil.AUTOR_KOMB+" column", logger);
303
        }
304
        else if(CdmUtils.isNotBlank(authorKombString)){
305
            TeamOrPersonBase<?> authorKomb = (TeamOrPersonBase<?>) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, authorKombString);
306
            name.setCombinationAuthorship(authorKomb);
307
        }
308
        //basionym author
309
        if(authorBasiString.contains(RedListUtil.EX)){
310
            String[] basiSplit = authorBasiString.split(RedListUtil.EX);
311
            for (int i = 0; i < basiSplit.length; i++) {
312
                if(basiSplit.length!=2){
313
                    RedListUtil.logMessage(id, "Multiple ex basionymn authors found", logger);
314
                }
315
                if(i==0){
316
                    TeamOrPersonBase<?> authorBasi= (TeamOrPersonBase<?>) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, basiSplit[i]);
317
                    if(CdmUtils.isBlank(authorKombString)){
318
                        name.setExCombinationAuthorship(authorBasi);
319
                    }
320
                    else{
321
                        name.setExBasionymAuthorship(authorBasi);
322
                    }
323
                }
324
                else{
325
                    TeamOrPersonBase<?> authorBasi= (TeamOrPersonBase<?>) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, basiSplit[i]);
326
                    if(CdmUtils.isBlank(authorKombString)){
327
                        name.setCombinationAuthorship(authorBasi);
328
                    }
329
                    else{
330
                        name.setBasionymAuthorship(authorBasi);
331
                    }
332
                }
333
            }
334
        }
335
        else if(CdmUtils.isNotBlank(authorBasiString)){
336
            //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
337
            TeamOrPersonBase<?> authorBasi= (TeamOrPersonBase<?>) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, authorBasiString);
338
            if(CdmUtils.isBlank(authorKombString)){
339
                name.setCombinationAuthorship(authorBasi);
340
            }
341
            else{
342
                name.setBasionymAuthorship(authorBasi);
343
            }
344
        }
345

    
346
        //check authorship consistency
347
        String authorString = rs.getString(RedListUtil.AUTOR);
348
        String authorshipCache = name.getAuthorshipCache();
349
        checkAuthorShipConsistency(id, nomZusatzString, taxZusatzString, zusatzString, authorString, authorshipCache);
350
    }
351

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

    
354
        long id = rs.getLong(RedListUtil.NAMNR);
355
        String taxNameString = rs.getString(RedListUtil.TAXNAME);
356
        String rangString = rs.getString(RedListUtil.RANG);
357
        String ep1String = rs.getString(RedListUtil.EPI1);
358
        String ep2String = rs.getString(RedListUtil.EPI2);
359
        String ep3String = rs.getString(RedListUtil.EPI3);
360
        String nomZusatzString = rs.getString(RedListUtil.NOM_ZUSATZ);
361
        String hybString = rs.getString(RedListUtil.HYB);
362

    
363
        if(CdmUtils.isBlank(taxNameString) && CdmUtils.isBlank(ep1String)){
364
            RedListUtil.logMessage(id, "No name found!", logger);
365
        }
366

    
367
        Rank rank = makeRank(id, state, rangString, ep3String!=null);
368
        NonViralName<?> name = BotanicalName.NewInstance(rank);
369

    
370
        //ep1 should always be present
371
        if(CdmUtils.isBlank(ep1String)){
372
            RedListUtil.logMessage(id, RedListUtil.EPI1+" is empty!", logger);
373
        }
374
        name.setGenusOrUninomial(ep1String);
375
        if(CdmUtils.isNotBlank(ep2String)){
376
            if(rank!=null && rank.isInfraGeneric()){
377
                name.setInfraGenericEpithet(ep2String);
378
            }
379
            else{
380
                name.setSpecificEpithet(ep2String);
381
            }
382
        }
383
        if(CdmUtils.isNotBlank(ep3String)){
384
            name.setInfraSpecificEpithet(ep3String);
385
        }
386
        //nomenclatural status
387
        if(CdmUtils.isNotBlank(nomZusatzString)){
388
            NomenclaturalStatusType statusType = makeNomenclaturalStatus(id, state, nomZusatzString);
389
            if(statusType!=null){
390
                NomenclaturalStatus status = NomenclaturalStatus.NewInstance(statusType);
391
                //special case for invalid names where the DB entry contains
392
                //additional information in brackets e.g. "nom. inval. (sine basion.)"
393
                if(statusType.equals(NomenclaturalStatusType.INVALID())){
394
                    Pattern pattern = Pattern.compile("\\((.*?)\\)");
395
                    Matcher matcher = pattern.matcher(nomZusatzString);
396
                    if (matcher.find()){
397
                        status.setRuleConsidered(matcher.group(1));
398
                    }
399
                }
400
                name.addStatus(status);
401
            }
402
        }
403
        //hybrid
404
        if(CdmUtils.isNotBlank(hybString)){
405
            if(hybString.equals(RedListUtil.HYB_X)){
406
                name.setBinomHybrid(true);
407
            }
408
            else if(hybString.equals(RedListUtil.HYB_G)){
409
                name.setMonomHybrid(true);
410
            }
411
            else if(hybString.equals(RedListUtil.HYB_XF)){
412
                name.setHybridFormula(true);
413
                if(ep1String.contains(RedListUtil.HYB_SIGN)){
414
                    RedListUtil.logMessage(id, "EPI1 has hybrid signs but with flag: "+RedListUtil.HYB_XF, logger);
415
                }
416
                else if(ep2String.contains(RedListUtil.HYB_SIGN)){
417
                    String[] split = ep2String.split(RedListUtil.HYB_SIGN);
418
                    if(split.length!=2){
419
                        RedListUtil.logMessage(id, "Multiple hybrid signs found in "+ep2String, logger);
420
                    }
421
                    String hybridFormula1 = ep1String+" "+split[0].trim();
422
                    String hybridFormula2 = ep1String+" "+split[1].trim();
423
                    if(CdmUtils.isNotBlank(ep3String)){
424
                        hybridFormula1 += " "+ep3String;
425
                        hybridFormula2 += " "+ep3String;
426
                    }
427
                    String fullFormula = hybridFormula1+" "+RedListUtil.HYB_SIGN+" "+hybridFormula2;
428
                    name = NonViralNameParserImpl.NewInstance().parseFullName(fullFormula);
429
                }
430
                else if(ep3String.contains(RedListUtil.HYB_SIGN)){
431
                    String[] split = ep3String.split(RedListUtil.HYB_SIGN);
432
                    if(split.length!=2){
433
                        RedListUtil.logMessage(id, "Multiple hybrid signs found in "+ep3String, logger);
434
                    }
435
                    String hybridFormula1 = ep1String+" "+ep2String+" "+split[0];
436
                    String hybridFormula2 = ep1String+" "+ep2String+" "+split[1];
437
                    String fullFormula = hybridFormula1+" "+RedListUtil.HYB_SIGN+" "+hybridFormula2;
438
                    name = NonViralNameParserImpl.NewInstance().parseFullName(fullFormula);
439
                }
440
            }
441
            else if(hybString.equals(RedListUtil.HYB_N)){
442
                name = NonViralNameParserImpl.NewInstance().parseFullName(ep1String+" "+ep2String+" nothosubsp. "+ep3String);
443
            }
444
            else if(hybString.equals(RedListUtil.HYB_GF)){
445
                if(ep1String.contains(RedListUtil.HYB_SIGN)){
446
                    name = NonViralNameParserImpl.NewInstance().parseFullName(ep1String);
447
                }
448
                else{
449
                    RedListUtil.logMessage(id, "HYB is "+hybString+" but "+RedListUtil.HYB+" does not contain "+RedListUtil.HYB_SIGN, logger);
450
                }
451
            }
452
            else{
453
                logger.error("HYB value "+hybString+" not yet handled");
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 checkAuthorShipConsistency(long id, String nomZusatzString, String taxZusatzString,
464
            String zusatzString, String authorString, String authorshipCache) {
465
        if(CdmUtils.isNotBlank(zusatzString)){
466
            authorString = authorString.replace(", "+zusatzString, "");
467
        }
468
        if(CdmUtils.isNotBlank(nomZusatzString)){
469
            authorString = authorString.replace(", "+nomZusatzString, "");
470
        }
471
        if(CdmUtils.isNotBlank(taxZusatzString)){
472
            authorString = authorString.replace(", "+taxZusatzString, "");
473
        }
474
        if(authorString.equals(RedListUtil.AUCT)){
475
            authorString = "";
476
        }
477
        if(STRICT_TITLE_CHECK){
478
            if(!authorString.equals(authorshipCache)){
479
                RedListUtil.logMessage(id, "Authorship inconsistent! name.authorhshipCache <-> Column "+RedListUtil.AUTOR+": "+authorshipCache+" <-> "+authorString, logger);
480
            }
481
        }
482
        else{
483
            if(!authorString.startsWith(authorshipCache)){
484
                RedListUtil.logMessage(id, "Authorship inconsistent! name.authorhshipCache <-> Column "+RedListUtil.AUTOR+": "+authorshipCache+" <-> "+authorString, logger);
485
            }
486
        }
487
    }
488

    
489
    private void checkTaxonNameConsistency(long id, String taxNameString, String hybString, TaxonBase<?> taxonBase) {
490
        if(hybString.equals(RedListUtil.HYB_XF)){
491
            if(HibernateProxyHelper.deproxy(taxonBase.getName(),NonViralName.class).getHybridChildRelations().isEmpty()){
492
                RedListUtil.logMessage(id, "Hybrid name but no hybrid child relations", logger);
493
                return;
494
            }
495
            return;
496
        }
497

    
498

    
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
        if(taxNameString.endsWith("aggr.")){
507
            taxNameString = taxNameString.replaceFirst(" ", " (");
508
            taxNameString = taxNameString.replace(" aggr.", ") aggr.");
509
        }
510

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

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

    
533
        taxNameString = taxNameString.replace("[ranglos]", "[unranked]");
534
        if(STRICT_TITLE_CHECK){
535
            if(!taxNameString.trim().equals(nameCache)){
536
                RedListUtil.logMessage(id, "Taxon name inconsistent! taxon.titleCache <-> Column "+RedListUtil.TAXNAME+": "+nameCache+" <-> "+taxNameString, logger);
537
            }
538
        }
539
        else{
540
            if(!taxNameString.startsWith(nameCache)){
541
                RedListUtil.logMessage(id, "Taxon name inconsistent! taxon.titleCache <-> Column "+RedListUtil.TAXNAME+": "+nameCache+" <-> "+taxNameString, logger);
542
            }
543
        }
544
    }
545

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

    
570
    private NomenclaturalStatusType makeNomenclaturalStatus(long id, RedListGefaesspflanzenImportState state, String nomZusatzString) {
571
        NomenclaturalStatusType status = null;
572
        try {
573
            status = state.getTransformer().getNomenclaturalStatusByKey(nomZusatzString);
574
        } catch (UndefinedTransformerMethodException e) {
575
            e.printStackTrace();
576
        }
577
        if(status==null){
578
            RedListUtil.logMessage(id, nomZusatzString+" could not be associated to a known nomenclatural status.", logger);
579
        }
580
        return status;
581
    }
582

    
583

    
584

    
585
    @Override
586
    public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
587
            RedListGefaesspflanzenImportState state) {
588
        Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
589
        Map<String, AgentBase<?>> authorMap = new HashMap<String, AgentBase<?>>();
590

    
591
        try {
592
            while (rs.next()){
593
                String authorKombString = rs.getString(RedListUtil.AUTOR_KOMB);
594

    
595
                if(authorKombString.contains(RedListUtil.EX)){
596
                    String[] kombSplit = authorKombString.split(RedListUtil.EX);
597
                    for (int i = 0; i < kombSplit.length; i++) {
598
                        if(!authorMap.containsKey(kombSplit[i])){
599
                            authorMap.put(kombSplit[i], getAgentService().load(state.getAuthorMap().get(kombSplit[i])));
600
                        }
601
                    }
602
                }
603
                else if(CdmUtils.isNotBlank(authorKombString) && !authorMap.containsKey(authorKombString)){
604
                    authorMap.put(authorKombString, getAgentService().load(state.getAuthorMap().get(authorKombString)));
605
                }
606

    
607
                String authorBasiString = rs.getString(RedListUtil.AUTOR_BASI);
608
                //basionym author
609
                if(authorBasiString.contains(RedListUtil.EX)){
610
                    String[] basiSplit = authorBasiString.split(RedListUtil.EX);
611
                    for (int i = 0; i < basiSplit.length; i++) {
612
                        if(!authorMap.containsKey(basiSplit[i])){
613
                            authorMap.put(basiSplit[i], getAgentService().load(state.getAuthorMap().get(basiSplit[i])));
614
                        }
615
                    }
616
                }
617
                else if(CdmUtils.isNotBlank(authorBasiString) && !authorMap.containsKey(authorBasiString)){
618
                    authorMap.put(authorBasiString, getAgentService().load(state.getAuthorMap().get(authorBasiString)));
619
                }
620
            }
621
        } catch (SQLException e) {
622
            e.printStackTrace();
623
        }
624
        result.put(RedListUtil.AUTHOR_NAMESPACE, authorMap);
625

    
626
        return result;
627
    }
628

    
629
    @Override
630
    protected boolean doCheck(RedListGefaesspflanzenImportState state) {
631
        return false;
632
    }
633

    
634
    @Override
635
    protected boolean isIgnore(RedListGefaesspflanzenImportState state) {
636
        return false;
637
    }
638

    
639
}
(4-4/7)