Project

General

Profile

Download (62.3 KB) Statistics
| Branch: | Tag: | 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.specimen;
11

    
12

    
13
import java.util.ArrayList;
14
import java.util.HashMap;
15
import java.util.HashSet;
16
import java.util.List;
17
import java.util.Map;
18
import java.util.Set;
19
import java.util.UUID;
20

    
21
import org.apache.log4j.Logger;
22

    
23
import eu.etaxonomy.cdm.api.application.ICdmRepository;
24
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
25
import eu.etaxonomy.cdm.api.service.config.FindOccurrencesConfigurator;
26
import eu.etaxonomy.cdm.api.service.pager.Pager;
27
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
28
import eu.etaxonomy.cdm.io.common.CdmImportBase;
29
import eu.etaxonomy.cdm.io.common.IImportConfigurator;
30
import eu.etaxonomy.cdm.io.specimen.abcd206.in.Identification;
31
import eu.etaxonomy.cdm.io.specimen.abcd206.in.SpecimenImportReport;
32
import eu.etaxonomy.cdm.model.agent.AgentBase;
33
import eu.etaxonomy.cdm.model.agent.Institution;
34
import eu.etaxonomy.cdm.model.agent.Person;
35
import eu.etaxonomy.cdm.model.agent.Team;
36
import eu.etaxonomy.cdm.model.common.CdmBase;
37
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
38
import eu.etaxonomy.cdm.model.common.LanguageString;
39
import eu.etaxonomy.cdm.model.description.DescriptionBase;
40
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
41
import eu.etaxonomy.cdm.model.description.Feature;
42
import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
43
import eu.etaxonomy.cdm.model.description.TaxonDescription;
44
import eu.etaxonomy.cdm.model.name.INonViralName;
45
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
46
import eu.etaxonomy.cdm.model.name.Rank;
47
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
48
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
49
import eu.etaxonomy.cdm.model.name.TaxonName;
50
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
51
import eu.etaxonomy.cdm.model.occurrence.Collection;
52
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
53
import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
54
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
55
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
56
import eu.etaxonomy.cdm.model.reference.ISourceable;
57
import eu.etaxonomy.cdm.model.reference.OriginalSourceBase;
58
import eu.etaxonomy.cdm.model.reference.OriginalSourceType;
59
import eu.etaxonomy.cdm.model.reference.Reference;
60
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
61
import eu.etaxonomy.cdm.model.taxon.Classification;
62
import eu.etaxonomy.cdm.model.taxon.Synonym;
63
import eu.etaxonomy.cdm.model.taxon.Taxon;
64
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
65
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
66
import eu.etaxonomy.cdm.model.term.DefinedTerm;
67
import eu.etaxonomy.cdm.persistence.query.MatchMode;
68
import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
69
import eu.etaxonomy.cdm.strategy.parser.ParserProblem;
70
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
71

    
72

    
73
/**
74
 * @author p.kelbert
75
 * @since 20.10.2008
76
 */
77
public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STATE extends SpecimenImportStateBase>
78
        extends CdmImportBase<CONFIG, STATE> {
79

    
80
    private static final long serialVersionUID = 4423065367998125678L;
81
    private static final Logger logger = Logger.getLogger(SpecimenImportBase.class);
82

    
83
	protected static final UUID SPECIMEN_SCAN_TERM = UUID.fromString("acda15be-c0e2-4ea8-8783-b9b0c4ad7f03");
84

    
85
	private static final String COLON = ":";
86

    
87
	protected Map<String, DefinedTerm> kindOfUnitsMap;
88

    
89

    
90

    
91
	@Override
92
    protected abstract void doInvoke(STATE state);
93

    
94
	/**
95
	 * Handle a single unit
96
	 * @param state
97
	 * @param item
98
	 */
99
	protected abstract void handleSingleUnit(STATE state, Object item) ;
100

    
101

    
102

    
103
	protected TaxonName getOrCreateTaxonName(String scientificName, Rank rank, boolean preferredFlag, STATE state, int unitIndexInAbcdFile){
104
	    TaxonName taxonName = null;
105
        SpecimenImportConfiguratorBase<?,?,?> config = state.getConfig();
106

    
107
        //check atomised name data for rank
108
        //new name will be created
109
        TaxonName atomisedTaxonName = null;
110
        if (rank==null && unitIndexInAbcdFile>=0 && ((state.getDataHolder().getAtomisedIdentificationList() != null && !state.getDataHolder().getAtomisedIdentificationList().isEmpty())|| state.getDataHolder().getAtomisedIdentificationList().size() > 0)) {
111
            atomisedTaxonName = setTaxonNameByType(state.getDataHolder().getAtomisedIdentificationList().get(unitIndexInAbcdFile), scientificName, state);
112
            if(atomisedTaxonName!=null){
113
                rank = atomisedTaxonName.getRank();
114
            }
115
        }
116
        if(config.isReuseExistingTaxaWhenPossible()){
117
            TaxonName parsedName = atomisedTaxonName;
118
            if(parsedName==null){
119

    
120
                parsedName = parseScientificName(scientificName, state, state.getReport(), rank);
121

    
122
            }
123
            atomisedTaxonName = parsedName;
124
            if(config.isIgnoreAuthorship() && parsedName!=null){// && preferredFlag){
125
                // do not ignore authorship for non-preferred names because they need
126
                // to be created for the determination history
127
                String nameCache = TaxonName.castAndDeproxy(parsedName).getNameCache();
128
                List<TaxonName> names = getNameService().findNamesByNameCache(nameCache, MatchMode.EXACT, null);
129
                if (!names.isEmpty()){
130
                     taxonName = getBestMatchingName(scientificName, new ArrayList<>(names), state);
131
                }
132
                if (taxonName == null && !names.isEmpty()){
133
                    taxonName = names.get(0);
134
                }
135

    
136
            } else {
137
                //search for existing names
138
                List<TaxonName> names = getNameService().listByTitleWithRestrictions(TaxonName.class, scientificName, MatchMode.EXACT, null, null, null, null, null);
139
                taxonName = getBestMatchingName(scientificName, names, state);
140
                //still nothing found -> try with the atomised name full title cache
141
                if(taxonName==null && atomisedTaxonName!=null){
142
                    names = getNameService().listByTitleWithRestrictions(TaxonName.class, atomisedTaxonName.getFullTitleCache(), MatchMode.EXACT, null, null, null, null, null);
143
                    taxonName = getBestMatchingName(atomisedTaxonName.getTitleCache(), names, state);
144
                    //still nothing found -> try with the atomised name title cache
145
                    if(taxonName==null){
146
                        names = getNameService().listByTitleWithRestrictions(TaxonName.class, atomisedTaxonName.getTitleCache(), MatchMode.EXACT, null, null, null, null, null);
147
                        taxonName = getBestMatchingName(atomisedTaxonName.getTitleCache(), names, state);
148
                    }
149
                }
150

    
151
            }
152

    
153
        }
154

    
155
        if(taxonName==null && atomisedTaxonName!=null){
156
            taxonName = atomisedTaxonName;
157
            state.getReport().addName(taxonName);
158
            logger.info("Created new taxon name "+taxonName);
159
            if(taxonName.hasProblem()){
160
                state.getReport().addInfoMessage(String.format("Created %s with parsing problems", taxonName));
161
            }
162
            if(!atomisedTaxonName.getTitleCache().equals(scientificName)){
163
                state.getReport().addInfoMessage(String.format("Taxon %s was parsed as %s", scientificName, atomisedTaxonName.getTitleCache()));
164
            }
165
        }
166
        else if(taxonName==null){
167
            //create new taxon name
168

    
169
            if (state.getDataHolder().getNomenclatureCode().equals(NomenclaturalCode.ICNAFP)){
170
                taxonName = TaxonNameFactory.NewBotanicalInstance(rank);
171
            }else if (state.getDataHolder().getNomenclatureCode().equals(NomenclaturalCode.ICZN)){
172
                taxonName = TaxonNameFactory.NewZoologicalInstance(rank);
173
            }else{
174
                taxonName = TaxonNameFactory.NewNonViralInstance(rank);
175
            }
176
            taxonName.setFullTitleCache(scientificName,true);
177
            taxonName.setTitleCache(scientificName, true);
178
            state.getReport().addName(taxonName);
179
            logger.info("Created new taxon name "+taxonName);
180
        }
181
        if (taxonName != null){
182
            state.names.put(taxonName.getNameCache(), taxonName);
183
        }
184
        if(!taxonName.isPersited()) {
185
            save(taxonName, state);
186
        }
187
        return taxonName;
188
    }
189

    
190
	 protected TaxonName getBestMatchingName(String scientificName, java.util.Collection<TaxonName> names, STATE state){
191
	        Set<TaxonName> namesWithAcceptedTaxa = new HashSet<>();
192
	        List<TaxonName> namesWithAcceptedTaxaInClassification = new ArrayList<>();
193
	        for (TaxonName name : names) {
194
	            if(!name.getTaxa().isEmpty()){
195
	                Set<Taxon> taxa = name.getTaxa();
196
	                for (Taxon taxon:taxa){
197
	                    if (!taxon.getTaxonNodes().isEmpty()){
198
	                        //use only taxa included in a classification
199
	                        for (TaxonNode node:taxon.getTaxonNodes()){
200
	                            if (state.getClassification() != null && node.getClassification().equals(state.getClassification())){
201
	                                namesWithAcceptedTaxaInClassification.add(name);
202
	                            }else {
203
	                                namesWithAcceptedTaxa.add(name);
204
	                            }
205
	                        }
206

    
207
	                    }
208
	                }
209

    
210
	            }
211
	        }
212
	        String message = String.format("More than one taxon name was found for %s, maybe in other classifications!", scientificName);
213
	        //check for names with accepted taxa in classification
214
	        if(namesWithAcceptedTaxaInClassification.size()>0){
215
                if(namesWithAcceptedTaxaInClassification.size()>1){
216

    
217
                    state.getReport().addInfoMessage(message);
218
                    logger.warn(message);
219
                    return null;
220
                }
221
                return namesWithAcceptedTaxaInClassification.iterator().next();
222
            }
223
	      //check for any names with accepted taxa
224
	        if(namesWithAcceptedTaxa.size()>0){
225
	            if(namesWithAcceptedTaxa.size()>1){
226

    
227
	                state.getReport().addInfoMessage(message);
228
	                logger.warn(message);
229
	                return null;
230
	            }
231
	            return namesWithAcceptedTaxa.iterator().next();
232
	        }
233
	        //no names with accepted taxa found -> check accepted taxa of synonyms
234
	        List<Taxon> taxaFromSynonyms = new ArrayList<>();
235
	        for (TaxonName name : names) {
236
	            Set<TaxonBase> taxonBases = name.getTaxonBases();
237
	            for (TaxonBase taxonBase : taxonBases) {
238
	                if(taxonBase.isInstanceOf(Synonym.class)){
239
	                    Synonym synonym = HibernateProxyHelper.deproxy(taxonBase, Synonym.class);
240
	                    taxaFromSynonyms.add(synonym.getAcceptedTaxon());
241
	                }
242
	            }
243
	        }
244
	        if(taxaFromSynonyms.size()>0){
245
	            if(taxaFromSynonyms.size()>1){
246
	                state.getReport().addInfoMessage(message);
247
	                logger.warn(message);
248
	                return null;
249
	            }
250
	            return taxaFromSynonyms.iterator().next().getName();
251
	        }
252
	        //no accepted and no synonyms -> return one of the names and create a new taxon
253
	        if (names.isEmpty()){
254
	            return null;
255
	        }else{
256
	            return names.iterator().next();
257
	        }
258
	    }
259
	 /**
260
	     * Parse automatically the scientific name
261
	     * @param scientificName the scientific name to parse
262
	     * @param state the current import state
263
	     * @param report the import report
264
	     * @return a parsed name
265
	     */
266

    
267
	    protected TaxonName parseScientificName(String scientificName, STATE state, SpecimenImportReport report, Rank rank) {
268

    
269
	        NonViralNameParserImpl nvnpi = NonViralNameParserImpl.NewInstance();
270
	        TaxonName taxonName = null;
271
	        boolean problem = false;
272

    
273
	        if (logger.isDebugEnabled()){
274
	            logger.debug("parseScientificName " + state.getDataHolder().getNomenclatureCode().toString());
275
	        }
276

    
277
	        if (state.getDataHolder().getNomenclatureCode() != null && (state.getDataHolder().getNomenclatureCode().toString().equals("Zoological") || state.getDataHolder().getNomenclatureCode().toString().contains("ICZN"))) {
278
	            taxonName = (TaxonName)nvnpi.parseFullName(scientificName, NomenclaturalCode.ICZN, rank);
279
	            if (taxonName.hasProblem()) {
280
	                problem = true;
281
	            }
282
	        }
283
	        else if (state.getDataHolder().getNomenclatureCode() != null && (state.getDataHolder().getNomenclatureCode().toString().equals("Botanical") || state.getDataHolder().getNomenclatureCode().toString().contains("ICBN")  || state.getDataHolder().getNomenclatureCode().toString().contains("ICNAFP"))) {
284
	            taxonName = (TaxonName)nvnpi.parseFullName(scientificName, NomenclaturalCode.ICNAFP, rank);
285
	            if (taxonName.hasProblem()) {
286
	                problem = true;
287
	            }
288
	        }
289
	        else if (state.getDataHolder().getNomenclatureCode() != null && (state.getDataHolder().getNomenclatureCode().toString().equals("Bacterial") || state.getDataHolder().getNomenclatureCode().toString().contains("ICBN"))) {
290
	            taxonName = (TaxonName)nvnpi.parseFullName(scientificName, NomenclaturalCode.ICNB, rank);
291
	            if (taxonName.hasProblem()) {
292
	                problem = true;
293
	            }
294
	        }
295
	        else if (state.getDataHolder().getNomenclatureCode() != null && (state.getDataHolder().getNomenclatureCode().toString().equals("Cultivar") || state.getDataHolder().getNomenclatureCode().toString().contains("ICNCP"))) {
296
	            taxonName = (TaxonName)nvnpi.parseFullName(scientificName, NomenclaturalCode.ICNCP, rank);
297
	            if (taxonName.hasProblem()) {
298
	                problem = true;
299
	            }
300
	        }
301
	        if (problem) {
302
	            String message = String.format("Parsing problems for %s", scientificName);
303
	            if(taxonName!=null){
304
	                for (ParserProblem parserProblem : taxonName.getParsingProblems()) {
305
	                    message += "\n\t- "+parserProblem;
306
	                }
307
	            }
308
	            report.addInfoMessage(message);
309
	            logger.info(message);
310
	        }
311
	        return taxonName;
312

    
313
	    }
314

    
315
	    /**
316
	     * Create the name without automatic parsing, either because it failed, or because the user deactivated it.
317
	     * The name is built upon the ABCD fields
318
	     * @param atomisedMap : the ABCD atomised fields
319
	     * @param fullName : the full scientific name
320
	     * @param state
321
	     * @return the corresponding Botanical or Zoological or... name
322
	     */
323
	    protected TaxonName setTaxonNameByType(
324
	            HashMap<String, String> atomisedMap, String fullName, STATE state) {
325
	        boolean problem = false;
326
	        if (logger.isDebugEnabled()){
327
	            logger.debug("settaxonnamebytype " + state.getDataHolder().getNomenclatureCode().toString());
328
	        }
329

    
330
	        if (state.getDataHolder().getNomenclatureCode().equals("Zoological") || state.getDataHolder().getNomenclatureCode().equals(NomenclaturalCode.ICZN.getUuid())) {
331
	            TaxonName taxonName = TaxonNameFactory.NewZoologicalInstance(null);
332
	            taxonName.setFullTitleCache(fullName, true);
333
	            taxonName.setGenusOrUninomial(NB(getFromMap(atomisedMap, "Genus")));
334
	            taxonName.setInfraGenericEpithet(NB(getFromMap(atomisedMap, "SubGenus")));
335
	            taxonName.setSpecificEpithet(NB(getFromMap(atomisedMap,"SpeciesEpithet")));
336
	            taxonName.setInfraSpecificEpithet(NB(getFromMap(atomisedMap,"SubspeciesEpithet")));
337

    
338
	            if (taxonName.getGenusOrUninomial() != null){
339
	                taxonName.setRank(Rank.GENUS());
340
	            }
341

    
342
	            if (taxonName.getInfraGenericEpithet() != null){
343
	                taxonName.setRank(Rank.SUBGENUS());
344
	            }
345

    
346
	            if (taxonName.getSpecificEpithet() != null){
347
	                taxonName.setRank(Rank.SPECIES());
348
	            }
349

    
350
	            if (taxonName.getInfraSpecificEpithet() != null){
351
	                taxonName.setRank(Rank.SUBSPECIES());
352
	            }
353

    
354
	            Team team = null;
355
	            if (getFromMap(atomisedMap, "AuthorTeamParenthesis") != null) {
356
	                team = Team.NewInstance();
357
	                team.setTitleCache(getFromMap(atomisedMap, "AuthorTeamParenthesis"), true);
358
	            }
359
	            else {
360
	                if (getFromMap(atomisedMap, "AuthorTeamAndYear") != null) {
361
	                    team = Team.NewInstance();
362
	                    team.setTitleCache(getFromMap(atomisedMap, "AuthorTeamAndYear"), true);
363
	                }
364
	            }
365
	            if (team != null) {
366
	                taxonName.setBasionymAuthorship(team);
367
	            }
368
	            else {
369
	                if (getFromMap(atomisedMap, "AuthorTeamParenthesis") != null) {
370
	                    taxonName.setAuthorshipCache(getFromMap(atomisedMap, "AuthorTeamParenthesis"));
371
	                }
372
	                else if (getFromMap(atomisedMap, "AuthorTeamAndYear") != null) {
373
	                    taxonName.setAuthorshipCache(getFromMap(atomisedMap, "AuthorTeamAndYear"));
374
	                }
375
	            }
376
	            if (getFromMap(atomisedMap, "CombinationAuthorTeamAndYear") != null) {
377
	                team = Team.NewInstance();
378
	                team.setTitleCache(getFromMap(atomisedMap, "CombinationAuthorTeamAndYear"), true);
379
	                taxonName.setCombinationAuthorship(team);
380
	            }
381
	            if (taxonName.hasProblem()) {
382
	                logger.info("pb ICZN");
383
	                problem = true;
384
	            }
385
	            else {
386
	                return taxonName;
387
	            }
388
	        }
389
	        else if (state.getDataHolder().getNomenclatureCode().equals("Botanical") || state.getDataHolder().getNomenclatureCode().equals(NomenclaturalCode.ICNAFP.getUuid())) {
390
	            TaxonName taxonName = parseScientificName(fullName, state, state.getReport(), null);
391
	            if (taxonName != null){
392
	                return taxonName;
393
	            }
394
	            else{
395
	                taxonName = TaxonNameFactory.NewBotanicalInstance(null);
396
	            }
397
	            taxonName.setFullTitleCache(fullName, true);
398
	            taxonName.setGenusOrUninomial(NB(getFromMap(atomisedMap, "Genus")));
399
	            taxonName.setSpecificEpithet(NB(getFromMap(atomisedMap, "FirstEpithet")));
400
	            taxonName.setInfraSpecificEpithet(NB(getFromMap(atomisedMap, "InfraSpeEpithet")));
401
	            try {
402
	                taxonName.setRank(Rank.getRankByName(getFromMap(atomisedMap, "Rank")));
403
	            } catch (Exception e) {
404
	                if (taxonName.getInfraSpecificEpithet() != null){
405
	                    taxonName.setRank(Rank.SUBSPECIES());
406
	                }
407
	                else if (taxonName.getSpecificEpithet() != null){
408
	                    taxonName.setRank(Rank.SPECIES());
409
	                }
410
	                else if (taxonName.getInfraGenericEpithet() != null){
411
	                    taxonName.setRank(Rank.SUBGENUS());
412
	                }
413
	                else if (taxonName.getGenusOrUninomial() != null){
414
	                    taxonName.setRank(Rank.GENUS());
415
	                }
416
	            }
417
	            Team team = null;
418
	            if (getFromMap(atomisedMap, "AuthorTeamParenthesis") != null) {
419
	                team = Team.NewInstance();
420
	                team.setTitleCache(getFromMap(atomisedMap, "AuthorTeamParenthesis"), true);
421
	                taxonName.setBasionymAuthorship(team);
422
	            }
423
	            if (getFromMap(atomisedMap, "AuthorTeam") != null) {
424
	                team = Team.NewInstance();
425
	                team.setTitleCache(getFromMap(atomisedMap, "AuthorTeam"), true);
426
	                taxonName.setCombinationAuthorship(team);
427
	            }
428
	            if (team == null) {
429
	                if (getFromMap(atomisedMap, "AuthorTeamParenthesis") != null) {
430
	                    taxonName.setAuthorshipCache(getFromMap(atomisedMap, "AuthorTeamParenthesis"));
431
	                }
432
	                else if (getFromMap(atomisedMap, "AuthorTeam") != null) {
433
	                    taxonName.setAuthorshipCache(getFromMap(atomisedMap, "AuthorTeam"));
434
	                }
435
	            }
436
	            if (getFromMap(atomisedMap, "CombinationAuthorTeamAndYear") != null) {
437
	                team = Team.NewInstance();
438
	                team.setTitleCache(getFromMap(atomisedMap, "CombinationAuthorTeamAndYear"), true);
439
	                taxonName.setCombinationAuthorship(team);
440
	            }
441
	            if (taxonName.hasProblem()) {
442
	                logger.info("pb ICBN");
443
	                problem = true;
444
	            }
445
	            else {
446
	                return taxonName;
447
	            }
448
	        }
449
	        else if (state.getDataHolder().getNomenclatureCode().equals("Bacterial") || state.getDataHolder().getNomenclatureCode().equals(NomenclaturalCode.ICNB.getUuid())) {
450
	            TaxonName taxonName = TaxonNameFactory.NewBacterialInstance(null);
451
	            taxonName.setFullTitleCache(fullName, true);
452
	            taxonName.setGenusOrUninomial(getFromMap(atomisedMap, "Genus"));
453
	            taxonName.setInfraGenericEpithet(NB(getFromMap(atomisedMap, "SubGenus")));
454
	            taxonName.setSpecificEpithet(NB(getFromMap(atomisedMap, "Species")));
455
	            taxonName.setInfraSpecificEpithet(NB(getFromMap(atomisedMap, "SubspeciesEpithet")));
456

    
457
	            if (taxonName.getGenusOrUninomial() != null){
458
	                taxonName.setRank(Rank.GENUS());
459
	            }
460
	            else if (taxonName.getInfraGenericEpithet() != null){
461
	                taxonName.setRank(Rank.SUBGENUS());
462
	            }
463
	            else if (taxonName.getSpecificEpithet() != null){
464
	                taxonName.setRank(Rank.SPECIES());
465
	            }
466
	            else if (taxonName.getInfraSpecificEpithet() != null){
467
	                taxonName.setRank(Rank.SUBSPECIES());
468
	            }
469

    
470
	            if (getFromMap(atomisedMap, "AuthorTeamAndYear") != null) {
471
	                Team team = Team.NewInstance();
472
	                team.setTitleCache(getFromMap(atomisedMap, "AuthorTeamAndYear"), true);
473
	                taxonName.setCombinationAuthorship(team);
474
	            }
475
	            if (getFromMap(atomisedMap, "ParentheticalAuthorTeamAndYear") != null) {
476
	                Team team = Team.NewInstance();
477
	                team.setTitleCache(getFromMap(atomisedMap, "ParentheticalAuthorTeamAndYear"), true);
478
	                taxonName.setBasionymAuthorship(team);
479
	            }
480
	            if (taxonName.hasProblem()) {
481
	                logger.info("pb ICNB");
482
	                problem = true;
483
	            }
484
	            else {
485
	                return taxonName;
486
	            }
487
	        }
488
	        else if (state.getDataHolder().getNomenclatureCode().equals("Cultivar")) {
489
	            TaxonName taxonName = TaxonNameFactory.NewCultivarInstance(null);
490

    
491
	            if (taxonName.hasProblem()) {
492
	                logger.info("pb ICNCP");
493
	                problem = true;
494
	            }
495
	            else {
496
	                return taxonName;
497
	            }
498
	            return taxonName;
499
	        }
500

    
501
	        if (problem) {
502
	            logger.info("Problem im setTaxonNameByType ");
503
	            TaxonName taxonName = TaxonNameFactory.NewNonViralInstance(null);
504
	            taxonName.setFullTitleCache(fullName, true);
505
	            return taxonName;
506
	        }
507
	        TaxonName tn = TaxonNameFactory.NewNonViralInstance(null);
508
	        return tn;
509
	    }
510

    
511
	    /**
512
	     * Get a formated string from a hashmap
513
	     * @param atomisedMap
514
	     * @param key
515
	     * @return
516
	     */
517
	    private String getFromMap(HashMap<String, String> atomisedMap, String key) {
518
	        String value = null;
519
	        if (atomisedMap.containsKey(key)) {
520
	            value = atomisedMap.get(key);
521
	        }
522

    
523
	        try {
524
	            if (value != null && key.matches(".*Year.*")) {
525
	                value = value.trim();
526
	                if (value.matches("[a-z A-Z ]*[0-9]{4}$")) {
527
	                    String tmp = value.split("[0-9]{4}$")[0];
528
	                    int year = Integer.parseInt(value.split(tmp)[1]);
529
	                    if (year >= 1752) {
530
	                        value = tmp;
531
	                    }
532
	                    else {
533
	                        value = null;
534
	                    }
535
	                }
536
	                else {
537
	                    value = null;
538
	                }
539
	            }
540
	        }
541
	        catch (Exception e) {
542
	            value = null;
543
	        }
544
	        return value;
545
	    }
546

    
547
	    /**
548
	     * Very fast and dirty implementation to allow handling of transient objects as described in
549
	     * https://dev.e-taxonomy.eu/redmine/issues/3726
550
	     *
551
	     * Not yet complete.
552
	     *
553
	     * @param cdmBase
554
	     * @param state
555
	     */
556
	    protected UUID save(CdmBase cdmBase, SpecimenImportStateBase state) {
557
	        ICdmRepository cdmRepository = state.getConfig().getCdmAppController();
558
	        if (cdmRepository == null){
559
	            cdmRepository = this;
560
	        }
561

    
562
	        if (cdmBase.isInstanceOf(LanguageString.class)){
563
	            return cdmRepository.getTermService().saveLanguageData(CdmBase.deproxy(cdmBase, LanguageString.class));
564
	        }else if (cdmBase.isInstanceOf(SpecimenOrObservationBase.class)){
565
	            SpecimenOrObservationBase specimen = CdmBase.deproxy(cdmBase, SpecimenOrObservationBase.class);
566

    
567
	            return cdmRepository.getOccurrenceService().saveOrUpdate(specimen);
568
	        }else if (cdmBase.isInstanceOf(Reference.class)){
569
	            return cdmRepository.getReferenceService().saveOrUpdate(CdmBase.deproxy(cdmBase, Reference.class));
570
	        }else if (cdmBase.isInstanceOf(Classification.class)){
571
	            return cdmRepository.getClassificationService().saveOrUpdate(CdmBase.deproxy(cdmBase, Classification.class));
572
	        }else if (cdmBase.isInstanceOf(AgentBase.class)){
573
	            return cdmRepository.getAgentService().saveOrUpdate(CdmBase.deproxy(cdmBase, AgentBase.class));
574
	        }else if (cdmBase.isInstanceOf(Collection.class)){
575
	            return cdmRepository.getCollectionService().saveOrUpdate(CdmBase.deproxy(cdmBase, Collection.class));
576
	        }else if (cdmBase.isInstanceOf(DescriptionBase.class)){
577
	            DescriptionBase description = CdmBase.deproxy(cdmBase, DescriptionBase.class);
578

    
579
	            return cdmRepository.getDescriptionService().saveOrUpdate(description);
580
	        }else if (cdmBase.isInstanceOf(TaxonBase.class)){
581
	            return cdmRepository.getTaxonService().saveOrUpdate(CdmBase.deproxy(cdmBase, TaxonBase.class));
582
	        }else if (cdmBase.isInstanceOf(TaxonName.class)){
583
	            return cdmRepository.getNameService().saveOrUpdate(CdmBase.deproxy(cdmBase, TaxonName.class));
584
	        }else if (cdmBase.isInstanceOf(TaxonNode.class)){
585
                return cdmRepository.getTaxonNodeService().saveOrUpdate(CdmBase.deproxy(cdmBase, TaxonNode.class));
586
            }else{
587
	            throw new IllegalArgumentException("Class not supported in save method: " + CdmBase.deproxy(cdmBase, CdmBase.class).getClass().getSimpleName());
588
	        }
589

    
590
	    }
591

    
592

    
593
	    protected SpecimenOrObservationBase findExistingSpecimen(String unitId, SpecimenImportStateBase state){
594
	        ICdmRepository cdmAppController = state.getConfig().getCdmAppController();
595
	        if(cdmAppController==null){
596
	            cdmAppController = this;
597
	        }
598
	        FindOccurrencesConfigurator config = new FindOccurrencesConfigurator();
599
	        config.setSignificantIdentifier(unitId);
600
	        List<String> propertyPaths = new ArrayList<>();
601
	        propertyPaths.add("derivedFrom.*");
602
	        config.setPropertyPaths(propertyPaths);
603
	        commitTransaction(state.getTx());
604
	        state.setTx(startTransaction());
605
	        try{
606
		        Pager<SpecimenOrObservationBase> existingSpecimens = cdmAppController.getOccurrenceService().findByTitle(config);
607
		        if(!existingSpecimens.getRecords().isEmpty()){
608
		            if(existingSpecimens.getRecords().size()==1){
609
		                return existingSpecimens.getRecords().iterator().next();
610
		            }
611
		        }
612

    
613
	        }catch(NullPointerException e){
614
	        	logger.error("searching for existing specimen creates NPE: " + config.getSignificantIdentifier());
615
	        	e.printStackTrace();
616
	        }
617

    
618

    
619
	        return null;
620
	    }
621

    
622
	    protected abstract void importAssociatedUnits(STATE state, Object item, DerivedUnitFacade derivedUnitFacade);
623

    
624
	    /**
625
	     * getFacade : get the DerivedUnitFacade based on the recordBasis
626
	     * @param state
627
	     *
628
	     * @return DerivedUnitFacade
629
	     */
630
	    protected DerivedUnitFacade getFacade(STATE state) {
631
	        if (logger.isDebugEnabled()){
632
	            logger.info("getFacade()");
633
	        }
634
	        SpecimenOrObservationType type = null;
635

    
636
	        // create specimen
637
	        if (NB((state.getDataHolder().getRecordBasis())) != null) {
638
	            if (state.getDataHolder().getRecordBasis().toLowerCase().startsWith("s") || state.getDataHolder().getRecordBasis().toLowerCase().indexOf("specimen")>-1) {// specimen
639
	                type = SpecimenOrObservationType.PreservedSpecimen;
640
	            }
641
	            if (state.getDataHolder().getRecordBasis().toLowerCase().startsWith("o") ||state.getDataHolder().getRecordBasis().toLowerCase().indexOf("observation")>-1 ) {
642
	                type = SpecimenOrObservationType.Observation;
643
	            }
644
	            if (state.getDataHolder().getRecordBasis().toLowerCase().indexOf("fossil")>-1){
645
	                type = SpecimenOrObservationType.Fossil;
646
	            }
647
	            if (state.getDataHolder().getRecordBasis().toLowerCase().indexOf("living")>-1) {
648
	                type = SpecimenOrObservationType.LivingSpecimen;
649
	            }
650
	            if (type == null) {
651
	                logger.info("The basis of record does not seem to be known: " + state.getDataHolder().getRecordBasis());
652
	                type = SpecimenOrObservationType.DerivedUnit;
653
	            }
654
	            // TODO fossils?
655
	        } else {
656
	            logger.info("The basis of record is null");
657
	            type = SpecimenOrObservationType.DerivedUnit;
658
	        }
659
	        DerivedUnitFacade derivedUnitFacade = DerivedUnitFacade.NewInstance(type);
660
	        return derivedUnitFacade;
661
	    }
662

    
663
	    /**
664
	     * Look if the Institution does already exist
665
	     * @param institutionCode: a string with the institutioncode
666
	     * @param config : the configurator
667
	     * @return the Institution (existing or new)
668
	     */
669
	    protected Institution getInstitution(String institutionCode, STATE state) {
670
	        SpecimenImportConfiguratorBase config = state.getConfig();
671
	        Institution institution=null;
672
	        institution = (Institution)state.institutions.get(institutionCode);
673
	        if (institution != null){
674
	            return institution;
675
	        }
676
	        List<Institution> institutions;
677
	        try {
678
	            institutions = getAgentService().searchInstitutionByCode(institutionCode);
679

    
680
	        } catch (Exception e) {
681
	            institutions = new ArrayList<Institution>();
682
	            logger.warn(e);
683
	        }
684
	        if (institutions.size() > 0 && config.isReuseExistingMetaData()) {
685
	            for (Institution institut:institutions){
686
	                try{
687
	                    if (institut.getCode().equalsIgnoreCase(institutionCode)) {
688
	                        institution=institut;
689
	                        break;
690
	                    }
691
	                }catch(Exception e){logger.warn("no institution code in the db");}
692
	            }
693
	        }
694
	        if (logger.isDebugEnabled()){
695
	            if(institution !=null) {
696
	                logger.info("getinstitution " + institution.toString());
697
	            }
698
	        }
699
	        if (institution == null){
700
	            // create institution
701
	            institution = Institution.NewInstance();
702
	            institution.setCode(institutionCode);
703
	            institution.setTitleCache(institutionCode, true);
704
	            UUID uuid = save(institution, state);
705
	        }
706

    
707

    
708
	        state.institutions.put(institutionCode, institution);
709
	        return institution;
710
	    }
711

    
712
	    /**
713
	     * Look if the Collection does already exist
714
	     * @param collectionCode
715
	     * @param collectionCode: a string
716
	     * @param config : the configurator
717
	     * @return the Collection (existing or new)
718
	     */
719
	    protected Collection getCollection(Institution institution, String collectionCode, STATE state) {
720
	        SpecimenImportConfiguratorBase config = state.getConfig();
721
	        Collection collection = null;
722
	        List<Collection> collections;
723
	        collection = (Collection) state.collections.get(collectionCode);
724
	        if (collection != null){
725
	            return collection;
726
	        }
727
	        try {
728
	            collections = getCollectionService().searchByCode(collectionCode);
729
	        } catch (Exception e) {
730
	            collections = new ArrayList<Collection>();
731
	        }
732
	        if (collections.size() > 0 && config.isReuseExistingMetaData()) {
733
	            for (Collection coll:collections){
734
	                if (coll.getCode() != null && coll.getInstitute() != null
735
	                        && coll.getCode().equalsIgnoreCase(collectionCode) && coll.getInstitute().equals(institution)) {
736
	                    collection = coll;
737
	                    break;
738
	                }
739
	            }
740
	        }
741

    
742
	        if(collection == null){
743
	            collection =Collection.NewInstance();
744
	            collection.setCode(collectionCode);
745
	            collection.setInstitute(institution);
746
	            collection.setTitleCache(collectionCode);
747
	            UUID uuid = save(collection, state);
748
	        }
749

    
750

    
751

    
752
	        state.collections.put(collectionCode, collection);
753

    
754
	        return collection;
755
	    }
756

    
757
	    /**
758
	     * @param reference
759
	     * @param citationDetail
760
	     * @return
761
	     */
762
	    //FIXME this method is highly critical, because
763
	    //  * it will have serious performance and memory problems with large databases
764
	    //        (databases may easily have >1 Mio source records)
765
	    //  * it does not make sense to search for existing sources and then clone them
766
	    //    we need to search for existing references instead and use them (if exist)
767
	    //    for our new source.
768
	    protected IdentifiableSource getIdentifiableSource(Reference reference, String citationDetail) {
769

    
770
	        IdentifiableSource sour = IdentifiableSource.NewInstance(OriginalSourceType.Import,null,null, reference,citationDetail);
771
	        return sour;
772
	    }
773

    
774
	    /**
775
	     * Add the hierarchy for a Taxon(add higher taxa)
776
	     * @param classification
777
	     * @param taxon: a taxon to add as a node
778
	     * @param state: the ABCD import state
779
	     */
780
	    protected void addParentTaxon(Taxon taxon, STATE state, boolean preferredFlag, Classification classification){
781
	        INonViralName  nvname = taxon.getName();
782
	        Rank rank = nvname.getRank();
783
	        Taxon genus =null;
784
	        Taxon subgenus =null;
785
	        Taxon species = null;
786
	        Taxon subspecies = null;
787
	        Taxon parent = null;
788
	        if(rank!=null){
789
	            if (rank.isLower(Rank.GENUS() )){
790
	                String genusOrUninomial = nvname.getGenusOrUninomial();
791
	                TaxonName taxonName = getOrCreateTaxonName(genusOrUninomial, Rank.GENUS(), preferredFlag, state, -1);
792
	                genus = getOrCreateTaxonForName(taxonName, state);
793
	                if (genus == null){
794
	                    logger.debug("The genus should not be null " + taxonName);
795
	                }
796
	                if (preferredFlag) {
797
	                    parent = linkParentChildNode(null, genus, classification, state);
798
	                }
799

    
800
	            }
801
	            if (rank.isLower(Rank.SUBGENUS())){
802
	                String prefix = nvname.getGenusOrUninomial();
803
	                String name = nvname.getInfraGenericEpithet();
804
	                if (name != null){
805
	                    TaxonName taxonName = getOrCreateTaxonName(prefix+" "+name, Rank.SUBGENUS(), preferredFlag, state, -1);
806
	                    subgenus = getOrCreateTaxonForName(taxonName, state);
807
	                    if (preferredFlag) {
808
	                        parent = linkParentChildNode(genus, subgenus, classification, state);
809
	                    }            }
810
	            }
811
	            if (rank.isLower(Rank.SPECIES())){
812
	                if (subgenus!=null){
813
	                    String prefix = nvname.getGenusOrUninomial();
814
	                    String name = nvname.getInfraGenericEpithet();
815
	                    String spe = nvname.getSpecificEpithet();
816
	                    if (spe != null){
817
	                        TaxonName taxonName = getOrCreateTaxonName(prefix+" "+name+" "+spe, Rank.SPECIES(), preferredFlag, state, -1);
818
	                        species = getOrCreateTaxonForName(taxonName, state);
819
	                        if (preferredFlag) {
820
	                            parent = linkParentChildNode(subgenus, species, classification, state);
821
	                        }
822
	                    }
823
	                }
824
	                else{
825
	                    String prefix = nvname.getGenusOrUninomial();
826
	                    String name = nvname.getSpecificEpithet();
827
	                    if (name != null){
828
	                        TaxonName taxonName = getOrCreateTaxonName(prefix+" "+name, Rank.SPECIES(), preferredFlag, state, -1);
829
	                        species = getOrCreateTaxonForName(taxonName, state);
830
	                        if (preferredFlag) {
831
	                            parent = linkParentChildNode(genus, species, classification, state);
832
	                        }
833
	                    }
834
	                }
835
	            }
836
	            if (rank.isLower(Rank.INFRASPECIES())){
837
	                TaxonName taxonName = getOrCreateTaxonName(nvname.getFullTitleCache(), Rank.SUBSPECIES(), preferredFlag, state, -1);
838
	                subspecies = getOrCreateTaxonForName(taxonName, state);
839
	                if (preferredFlag) {
840
	                    parent = linkParentChildNode(species, subspecies, classification, state);
841
	                }
842
	            }
843
	        }else{
844
	            //handle cf. and aff. taxa
845
	            String genusEpithet = null;
846
	            if (nvname.getTitleCache().contains("cf.")){
847
	                genusEpithet = nvname.getTitleCache().substring(0, nvname.getTitleCache().indexOf("cf."));
848
	            } else if (nvname.getTitleCache().contains("aff.")){
849
	                genusEpithet = nvname.getTitleCache().substring(0, nvname.getTitleCache().indexOf("aff."));
850
	            }
851
	            if (genusEpithet != null){
852
    	            genusEpithet = genusEpithet.trim();
853
    	            TaxonName taxonName = null;
854
    	            if (genusEpithet.contains(" ")){
855
    	                taxonName = getOrCreateTaxonName(genusEpithet, Rank.SPECIES(), preferredFlag, state, -1);
856
    	            }else{
857
    	                taxonName = getOrCreateTaxonName(genusEpithet, Rank.GENUS(), preferredFlag, state, -1);
858
    	            }
859
    	            genus = getOrCreateTaxonForName(taxonName, state);
860
                    if (genus == null){
861
                        logger.debug("The genus should not be null " + taxonName);
862
                    }
863
                    if (preferredFlag) {
864
                        parent = linkParentChildNode(null, genus, classification, state);
865
                    }
866
	            }
867
	        }
868
	        if (preferredFlag && parent!=taxon ) {
869
	            linkParentChildNode(parent, taxon, classification, state);
870
	        }
871
	    }
872

    
873
	    /**
874
	     * Link a parent to a child and save it in the current classification
875
	     * @param parent: the higher Taxon
876
	     * @param child : the lower (or current) Taxon
877
	     * return the Taxon from the new created Node
878
	     * @param classification
879
	     * @param state
880
	     */
881
	    protected Taxon linkParentChildNode(Taxon parent, Taxon child, Classification classification, STATE state) {
882
	        TaxonNode node =null;
883
	        List<String> propertyPaths = new ArrayList<>();
884
            propertyPaths.add("childNodes");
885
	        if (parent != null) {
886

    
887
	            parent = (Taxon) getTaxonService().load(parent.getUuid(), propertyPaths);
888
	            child = (Taxon) getTaxonService().load(child.getUuid(), propertyPaths);
889
	            //here we do not have to check if the taxon nodes already exists
890
	            //this is done by classification.addParentChild()
891
	            //do not add child node if it already exists
892
	            if(hasTaxonNodeInClassification(child, classification)){
893
	                return child;
894
	            }
895
	            else{
896
	                node = classification.addParentChild(parent, child, state.getRef(), "");
897
	                save(node, state);
898
	            }
899
	        }
900
	        else {
901
	            if (child == null){
902
	                logger.debug("The child should not be null!");
903
	            }
904
	            child = (Taxon) getTaxonService().find(child.getUuid());
905
	            //do not add child node if it already exists
906
	            if(hasTaxonNodeInClassification(child, classification)){
907
	                return child;
908
	            }
909
	            else{
910
	                node = classification.addChildTaxon(child, state.getRef(), null);
911
	                save(node, state);
912
	            }
913
	        }
914
	        if(node!=null){
915
	            state.getReport().addTaxonNode(node);
916
	            return node.getTaxon();
917
	        }
918
	        String message = "Could not create taxon node for " +child;
919
	        state.getReport().addInfoMessage(message);
920
	        logger.warn(message);
921
	        return null;
922
	    }
923

    
924
	    protected Taxon getOrCreateTaxonForName(TaxonName taxonName, STATE state){
925
	        if (taxonName != null){
926
    	        Set<Taxon> acceptedTaxa = taxonName.getTaxa();
927
    	        if(acceptedTaxa.size()>0){
928
    	            Taxon firstAcceptedTaxon = acceptedTaxa.iterator().next();
929
    	            if(acceptedTaxa.size()>1){
930
    	                String message = "More than one accepted taxon was found for taxon name: "
931
    	                        + taxonName.getTitleCache() + "!\n" + firstAcceptedTaxon + "was chosen for "+state.getDerivedUnitBase();
932
    	                state.getReport().addInfoMessage(message);
933
    	                logger.warn(message);
934
    	            }
935
    	            else{
936
    	                return firstAcceptedTaxon;
937
    	            }
938
    	        }
939
    	        else{
940
    	            Set<TaxonBase> taxonAndSynonyms = taxonName.getTaxonBases();
941
    	            for (TaxonBase taxonBase : taxonAndSynonyms) {
942
    	                if(taxonBase.isInstanceOf(Synonym.class)){
943
    	                    Synonym synonym = HibernateProxyHelper.deproxy(taxonBase, Synonym.class);
944
    	                    Taxon acceptedTaxonOfSynonym = synonym.getAcceptedTaxon();
945
    	                    if(acceptedTaxonOfSynonym == null){
946
    	                        String message = "No accepted taxon could be found for taxon name: "
947
    	                                + taxonName.getTitleCache()
948
    	                                + "!";
949
    	                        state.getReport().addInfoMessage(message);
950
    	                        logger.warn(message);
951
    	                    }
952
    	                    else{
953
    	                        return acceptedTaxonOfSynonym;
954
    	                    }
955
    	                }
956
    	            }
957
    	        }
958
    	        Taxon taxon = Taxon.NewInstance(taxonName, state.getRef());
959
    	        save(taxon, state);
960
    	        state.getReport().addTaxon(taxon);
961
    	        logger.info("Created new taxon "+ taxon);
962
    	        return taxon;
963
	        }
964
	        return null;
965

    
966
	    }
967

    
968
	    private boolean hasTaxonNodeInClassification(Taxon taxon, Classification classification){
969
	        if(taxon.getTaxonNodes()!=null){
970
	            for (TaxonNode node : taxon.getTaxonNodes()){
971
	                if(node.getClassification().equals(classification)){
972
	                    return true;
973
	                }
974
	            }
975
	        }
976
	        return false;
977
	    }
978

    
979
	    /**
980
	     * HandleIdentifications : get the scientific names present in the ABCD
981
	     * document and store link them with the observation/specimen data
982
	     * @param state: the current ABCD import state
983
	     * @param derivedUnitFacade : the current derivedunitfacade
984
	     */
985
	    protected void handleIdentifications(STATE state, DerivedUnitFacade derivedUnitFacade) {
986
	       SpecimenImportConfiguratorBase config = state.getConfig();
987

    
988

    
989
	        String scientificName = "";
990
	        boolean preferredFlag = false;
991

    
992
	        if (state.getDataHolder().getNomenclatureCode() == ""){
993
	            if (config.getNomenclaturalCode() != null){
994
	                if (config.getNomenclaturalCode() != null){
995
	                    state.getDataHolder().setNomenclatureCode(config.getNomenclaturalCode().toString());
996

    
997
	                }
998
	            }
999
	        }
1000

    
1001
	        for (int i = 0; i < state.getDataHolder().getIdentificationList().size(); i++) {
1002
	            Identification identification = state.getDataHolder().getIdentificationList().get(i);
1003
	            scientificName = identification.getScientificName().replaceAll(" et ", " & ");
1004

    
1005
	            String preferred = identification.getPreferred();
1006
	            preferredFlag = false;
1007
	            if (preferred != null || state.getDataHolder().getIdentificationList().size()==1){
1008
	                if (state.getDataHolder().getIdentificationList().size()==1){
1009
	                    preferredFlag = true;
1010
	                }else if (preferred != null && (preferred.equals("1") || preferred.toLowerCase().indexOf("true") != -1) ) {
1011
    	                preferredFlag = true;
1012
    	            }
1013

    
1014
	            }
1015
	            if (identification.getCode() != null){
1016
    	            if (identification.getCode().indexOf(':') != -1) {
1017
    	                state.getDataHolder().setNomenclatureCode(identification.getCode().split(COLON)[1]);
1018
    	            }
1019
    	            else{
1020
    	                state.getDataHolder().setNomenclatureCode(identification.getCode());
1021
    	            }
1022
	            }
1023
	            TaxonName taxonName = getOrCreateTaxonName(scientificName, null, preferredFlag, state, i);
1024
	            Taxon taxon = getOrCreateTaxonForName(taxonName, state);
1025
	            addTaxonNode(taxon, state,preferredFlag);
1026
	            linkDeterminationEvent(state, taxon, preferredFlag, derivedUnitFacade, identification.getIdentifier(), identification.getDate(), identification.getModifier());
1027
	        }
1028
	    }
1029

    
1030
	    /**
1031
	     * @param taxon : a taxon to add as a node
1032
	     * @param state : the ABCD import state
1033
	     */
1034
	    protected void addTaxonNode(Taxon taxon, STATE state, boolean preferredFlag) {
1035
	        SpecimenImportConfiguratorBase<?,?,?> config = state.getConfig();
1036
	        logger.info("link taxon to a taxonNode "+taxon.getTitleCache());
1037
	        //only add nodes if not already existing in current classification or default classification
1038

    
1039
	        //check if node exists in current classification
1040
	        //NOTE: we cannot use hasTaxonNodeInClassification() here because we are first creating it here
1041
	        if (!existsInClassification(taxon,state.getClassification(), state)){
1042
	            if(config.isMoveNewTaxaToDefaultClassification()){
1043
	                //check if node exists in default classification
1044
	                if (!existsInClassification(taxon, state.getDefaultClassification(true), state)){
1045
	                    addParentTaxon(taxon, state, preferredFlag, state.getDefaultClassification(true));
1046
	                }
1047
	            }else{
1048
	                //add non-existing taxon to current classification
1049
	                addParentTaxon(taxon, state, preferredFlag, state.getClassification());
1050
	            }
1051

    
1052
	        }
1053
	    }
1054

    
1055

    
1056
	    private boolean existsInClassification(Taxon taxon, Classification classification, STATE state){
1057
	        boolean exist = false;
1058
	        ICdmRepository cdmAppController = state.getConfig().getCdmAppController();
1059
            if(cdmAppController==null){
1060
                cdmAppController = this;
1061
            }
1062
            if (classification != null){
1063
                if (!taxon.getTaxonNodes().isEmpty()){
1064
                    for (TaxonNode node:taxon.getTaxonNodes()){
1065
                        if (node.getClassification().equals(classification)){
1066
                            return true;
1067
                        }
1068
                    }
1069
                }
1070
// we do not need this because we already searched for taxa in db in the previous steps
1071
//    	        List<UuidAndTitleCache<TaxonNode>> uuidAndTitleCacheOfAllTaxa = cdmAppController.getClassificationService().getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(classification.getUuid());
1072
//    	        if (uuidAndTitleCacheOfAllTaxa != null){
1073
//        	        for (UuidAndTitleCache p : uuidAndTitleCacheOfAllTaxa){
1074
//        	            try{
1075
//        	                if(p.getTitleCache().equals(taxon.getTitleCache())) {
1076
//        	                    exist = true;
1077
//        	                }
1078
//        	            }
1079
//        	            catch(Exception e){
1080
//        	                logger.warn("TaxonNode doesn't seem to have a taxon");
1081
//        	            }
1082
//        	        }
1083
//    	        }
1084
            }
1085
	        return exist;
1086
	    }
1087

    
1088
	    /**
1089
	     * join DeterminationEvent to the Taxon Object
1090
	     * @param state : the ABCD import state
1091
	     * @param taxon: the current Taxon
1092
	     * @param preferredFlag :if the current name is preferred
1093
	     * @param derivedFacade : the derived Unit Facade
1094
	     */
1095
	    @SuppressWarnings("rawtypes")
1096
        protected void linkDeterminationEvent(STATE state, Taxon taxon, boolean preferredFlag,  DerivedUnitFacade derivedFacade, String identifierStr, String dateStr, String modifier) {
1097
	        SpecimenImportConfiguratorBase config = state.getConfig();
1098
	        if (logger.isDebugEnabled()){
1099
	            logger.info("start linkdetermination with taxon:" + taxon.getUuid()+", "+taxon);
1100
	        }
1101

    
1102
	        DeterminationEvent determinationEvent = DeterminationEvent.NewInstance();
1103
	        //determinationEvent.setTaxon(taxon);
1104
	        determinationEvent.setTaxonName(taxon.getName());
1105
	        determinationEvent.setPreferredFlag(preferredFlag);
1106

    
1107

    
1108
	        determinationEvent.setIdentifiedUnit(state.getDerivedUnitBase());
1109
	        if (state.getPersonStore().get(identifierStr) != null){
1110
	            determinationEvent.setActor((AgentBase)state.getPersonStore().get(identifierStr));
1111
	        } else if (identifierStr != null){
1112
	            Person identifier = Person.NewTitledInstance(identifierStr);
1113
	            determinationEvent.setActor(identifier);
1114
	        }
1115
	        if (dateStr != null){
1116
	            determinationEvent.setTimeperiod(TimePeriodParser.parseString(dateStr));
1117
	        }
1118
	        if (modifier != null){
1119
	            if (modifier.equals("cf.")){
1120
	                determinationEvent.setModifier(DefinedTerm.DETERMINATION_MODIFIER_CONFER());
1121
	            }else if (modifier.equals("aff.")){
1122
	                determinationEvent.setModifier(DefinedTerm.DETERMINATION_MODIFIER_AFFINIS());
1123
	            }
1124
	        }
1125
	        state.getDerivedUnitBase().addDetermination(determinationEvent);
1126

    
1127
	        if (logger.isDebugEnabled()){
1128
	            logger.debug("NB TYPES INFO: "+ state.getDataHolder().getStatusList().size());
1129
	        }
1130
	        for (SpecimenTypeDesignationStatus specimenTypeDesignationstatus : state.getDataHolder().getStatusList()) {
1131
	            if (specimenTypeDesignationstatus != null) {
1132
	                if (logger.isDebugEnabled()){
1133
	                    logger.debug("specimenTypeDesignationstatus :"+ specimenTypeDesignationstatus);
1134
	                }
1135

    
1136
	                ICdmRepository cdmAppController = config.getCdmAppController();
1137
	                if(cdmAppController == null){
1138
	                    cdmAppController = this;
1139
	                }
1140
	                specimenTypeDesignationstatus = HibernateProxyHelper.deproxy(cdmAppController.getTermService().find(specimenTypeDesignationstatus.getUuid()), SpecimenTypeDesignationStatus.class);
1141
	                //Designation
1142
	                TaxonName name = taxon.getName();
1143
	                SpecimenTypeDesignation designation = SpecimenTypeDesignation.NewInstance();
1144

    
1145
	                designation.setTypeStatus(specimenTypeDesignationstatus);
1146
	                designation.setTypeSpecimen(state.getDerivedUnitBase());
1147
	                name.addTypeDesignation(designation, true);
1148
	            }
1149
	        }
1150
	        save(state.getDerivedUnitBase(), state);
1151

    
1152
	        for (String[] fullReference : state.getDataHolder().getReferenceList()) {
1153

    
1154

    
1155
	            String strReference=fullReference[0];
1156
	            String citationDetail = fullReference[1];
1157
	            String citationURL = fullReference[2];
1158
	            List<Reference> references = getReferenceService().listByTitleWithRestrictions(Reference.class, "strReference", MatchMode.EXACT, null, null, null, null, null);
1159

    
1160
	            if (!references.isEmpty()){
1161
	                Reference reference = null;
1162
	                for (Reference refe: references) {
1163
	                    if (refe.getTitleCache().equalsIgnoreCase(strReference)) {
1164
	                        reference =refe;
1165
	                        break;
1166
	                    }
1167
	                }
1168
	                if (reference ==null){
1169
	                    reference = ReferenceFactory.newGeneric();
1170
	                    reference.setTitleCache(strReference, true);
1171
	                    save(reference, state);
1172
	                }
1173
	                determinationEvent.addReference(reference);
1174
	            }
1175
	        }
1176
	        save(state.getDerivedUnitBase(), state);
1177

    
1178
	        if (config.isAddIndividualsAssociationsSuchAsSpecimenAndObservations() && preferredFlag) {
1179
	            //do not add IndividualsAssociation to non-preferred taxa
1180
	            if (logger.isDebugEnabled()){
1181
	                logger.debug("isDoCreateIndividualsAssociations");
1182
	            }
1183

    
1184
	            makeIndividualsAssociation(state, taxon, determinationEvent);
1185

    
1186
	            save(state.getDerivedUnitBase(), state);
1187
	        }
1188
	    }
1189

    
1190
	    /**
1191
	     * create and link each association (specimen, observation..) to the accepted taxon
1192
	     * @param state : the ABCD import state
1193
	     * @param taxon: the current Taxon
1194
	     * @param determinationEvent:the determinationevent
1195
	     */
1196
	    protected void makeIndividualsAssociation(STATE state, Taxon taxon, DeterminationEvent determinationEvent) {
1197
	        SpecimenImportConfiguratorBase<?,?,?> config = state.getConfig();
1198
	        SpecimenUserInteraction sui = config.getSpecimenUserInteraction();
1199

    
1200
	        if (logger.isDebugEnabled()){
1201
	            logger.info("MAKE INDIVIDUALS ASSOCIATION");
1202
	        }
1203

    
1204
	        TaxonDescription taxonDescription = null;
1205
	        Set<TaxonDescription> descriptions= taxon.getDescriptions();
1206
	        if (state.getDescriptionPerTaxon(taxon.getUuid()) != null){
1207
	            taxonDescription = state.getDescriptionPerTaxon(taxon.getUuid());
1208
	        }
1209
	       if (taxonDescription == null && !descriptions.isEmpty() && state.getConfig().isReuseExistingDescriptiveGroups()){
1210
	           taxonDescription = descriptions.iterator().next();
1211
	       }
1212

    
1213
	       if (taxonDescription == null){
1214
	            taxonDescription = TaxonDescription.NewInstance(taxon, false);
1215
	            if(sourceNotLinkedToElement(taxonDescription,state.getRef(),null)) {
1216
	                taxonDescription.addSource(OriginalSourceType.Import, null, null, state.getRef(), null);
1217
	            }
1218
	            state.setDescriptionGroup(taxonDescription);
1219
	            taxon.addDescription(taxonDescription);
1220
	        }
1221

    
1222
	        //PREPARE REFERENCE QUESTIONS
1223

    
1224
	        Map<String,OriginalSourceBase<?>> sourceMap = new HashMap<String, OriginalSourceBase<?>>();
1225

    
1226
	        List<IdentifiableSource> issTmp = new ArrayList<>();//getCommonService().list(IdentifiableSource.class, null, null, null, null);
1227
	        List<DescriptionElementSource> issTmp2 = new ArrayList<>();//getCommonService().list(DescriptionElementSource.class, null, null, null, null);
1228

    
1229
	        Set<OriginalSourceBase> osbSet = new HashSet<OriginalSourceBase>();
1230
	        if(issTmp2!=null) {
1231
	            osbSet.addAll(issTmp2);
1232
	        }
1233
	        if(issTmp!=null) {
1234
	            osbSet.addAll(issTmp);
1235
	        }
1236

    
1237

    
1238
	        addToSourceMap(sourceMap, osbSet);
1239

    
1240

    
1241
            if(sourceNotLinkedToElement(taxonDescription,state.getRef(),null)) {
1242
                taxonDescription.addSource(OriginalSourceType.Import,null, null, state.getRef(), null);
1243
            }
1244

    
1245
	        state.setDescriptionGroup(taxonDescription);
1246

    
1247
	        IndividualsAssociation indAssociation = IndividualsAssociation.NewInstance();
1248
	        Feature feature = makeFeature(state.getDerivedUnitBase());
1249
	        indAssociation.setAssociatedSpecimenOrObservation(state.getDerivedUnitBase());
1250
	        indAssociation.setFeature(feature);
1251

    
1252
            if(sourceNotLinkedToElement(indAssociation,state.getImportReference(state.getActualAccessPoint()),null)) {
1253
                indAssociation.addSource(OriginalSourceType.Import,null, null, state.getImportReference(state.getActualAccessPoint()), null);
1254
            }
1255
            if(sourceNotLinkedToElement(state.getDerivedUnitBase(), state.getImportReference(state.getActualAccessPoint()),null)) {
1256
                state.getDerivedUnitBase().addSource(OriginalSourceType.Import,null, null, state.getImportReference(state.getActualAccessPoint()), null);
1257
            }
1258
            for (Reference citation : determinationEvent.getReferences()) {
1259
                if(sourceNotLinkedToElement(indAssociation,citation,null))
1260
                {
1261
                    indAssociation.addSource(DescriptionElementSource.NewInstance(OriginalSourceType.Import, null, null, citation, null));
1262
                }
1263
                if(sourceNotLinkedToElement(state.getDerivedUnitBase(), state.getImportReference(state.getActualAccessPoint()),null)) {
1264
                    state.getDerivedUnitBase().addSource(OriginalSourceType.Import,null, null, state.getImportReference(state.getActualAccessPoint()), null);
1265
                }
1266
            }
1267

    
1268

    
1269
	        taxonDescription.addElement(indAssociation);
1270

    
1271
	        save(taxonDescription, state);
1272
	        save(taxon, state);
1273
	        state.getReport().addDerivate(state.getDerivedUnitBase(), config);
1274
	        state.getReport().addIndividualAssociation(taxon, state.getDataHolder().getUnitID(), state.getDerivedUnitBase());
1275
	    }
1276

    
1277
	    /**
1278
	     * @param derivedUnitBase2
1279
	     * @param ref2
1280
	     * @param object
1281
	     * @return
1282
	     */
1283
	    private boolean sourceNotLinkedToElement(DerivedUnit derivedUnitBase2, Reference b, String d) {
1284
	        Set<IdentifiableSource> linkedSources = derivedUnitBase2.getSources();
1285
	        for (IdentifiableSource is:linkedSources){
1286
	            Reference a = is.getCitation();
1287
	            String c = is.getCitationMicroReference();
1288

    
1289
	            boolean refMatch=false;
1290
	            boolean microMatch=false;
1291

    
1292
	            try{
1293
	                if (a==null && b==null) {
1294
	                    refMatch=true;
1295
	                }
1296
	                if (a!=null && b!=null) {
1297
	                    if (a.getTitleCache().equalsIgnoreCase(b.getTitleCache())) {
1298
	                        refMatch=true;
1299
	                    }
1300
	                }
1301
	            }catch(Exception e){}
1302

    
1303

    
1304
	            try{
1305
	                if (c==null && d==null) {
1306
	                    microMatch=true;
1307
	                }
1308
	                if(c!=null && d!=null) {
1309
	                    if(c.equalsIgnoreCase(d)) {
1310
	                        microMatch=true;
1311
	                    }
1312
	                }
1313
	            }
1314
	            catch(Exception e){}
1315

    
1316
	            if (microMatch && refMatch) {
1317
	                return false;
1318
	            }
1319

    
1320

    
1321
	        }
1322
	        return true;
1323
	    }
1324

    
1325
	    private <T extends OriginalSourceBase<?>> boolean  sourceNotLinkedToElement(ISourceable<T> sourcable, Reference reference, String microReference) {
1326
	        Set<T> linkedSources = sourcable.getSources();
1327
	        for (T is:linkedSources){
1328
	            Reference unitReference = is.getCitation();
1329
	            String unitMicroReference = is.getCitationMicroReference();
1330

    
1331
	            boolean refMatch=false;
1332
	            boolean microMatch=false;
1333

    
1334
	            try{
1335
	                if (unitReference==null && reference==null) {
1336
	                    refMatch=true;
1337
	                }
1338
	                if (unitReference!=null && reference!=null) {
1339
	                    if (unitReference.getTitleCache().equalsIgnoreCase(reference.getTitleCache())) {
1340
	                        refMatch=true;
1341
	                    }
1342
	                }
1343
	            }catch(Exception e){}
1344

    
1345
	            try{
1346
	                if (unitMicroReference==null && microReference==null) {
1347
	                    microMatch=true;
1348
	                }
1349
	                if(unitMicroReference!=null && microReference!=null) {
1350
	                    if(unitMicroReference.equalsIgnoreCase(microReference)) {
1351
	                        microMatch=true;
1352
	                    }
1353
	                }
1354
	            }
1355
	            catch(Exception e){}
1356

    
1357
	            if (microMatch && refMatch) {
1358
	                return false;
1359
	            }
1360
	        }
1361
	        return true;
1362
	    }
1363

    
1364
	    /**
1365
	     * look for the Feature object (FieldObs, Specimen,...)
1366
	     * @param unit : a specimen or obersvation base
1367
	     * @return the corresponding Feature
1368
	     */
1369
	    private Feature makeFeature(SpecimenOrObservationBase<?> unit) {
1370
	        SpecimenOrObservationType type = unit.getRecordBasis();
1371

    
1372

    
1373

    
1374
	        if (type.isFeatureObservation()){
1375
	            return Feature.OBSERVATION();
1376
	        }else if (type.isFeatureSpecimen()){
1377
	            return Feature.SPECIMEN();
1378
	        }else if (type == SpecimenOrObservationType.DerivedUnit){
1379
	            return Feature.OBSERVATION();
1380
	            //            return getFeature("Specimen or observation");
1381
	        }else{
1382
	            String message = "Unhandled record basis '%s' for defining individuals association feature type. Use default.";
1383
	            logger.warn(String.format(message, type.getLabel()));
1384
	            return Feature.OBSERVATION();
1385
	            //            return getFeature("Specimen or observation");
1386

    
1387
	        }
1388
	    }
1389

    
1390

    
1391
	    /**
1392
	     * @param sourceMap
1393
	     * @param osbSet
1394
	     */
1395
	    protected void addToSourceMap(Map<String, OriginalSourceBase<?>> sourceMap, Set<OriginalSourceBase> osbSet) {
1396
	        for( OriginalSourceBase<?> osb:osbSet) {
1397
	            if(osb.getCitation()!=null && osb.getCitationMicroReference() !=null  && !osb.getCitationMicroReference().isEmpty()) {
1398
	                try{
1399
	                    sourceMap.put(osb.getCitation().getTitleCache()+ "---"+osb.getCitationMicroReference(),osb);
1400
	                }catch(NullPointerException e){logger.warn("null pointer problem (no ref?) with "+osb);}
1401
	            } else if(osb.getCitation()!=null){
1402
	                try{
1403
	                    sourceMap.put(osb.getCitation().getTitleCache(),osb);
1404
	                }catch(NullPointerException e){logger.warn("null pointer problem (no ref?) with "+osb);}
1405
	            }
1406
	        }
1407
	    }
1408

    
1409

    
1410

    
1411

    
1412
}
(2-2/7)