2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.io
.specimen
;
11 import java
.util
.ArrayList
;
12 import java
.util
.EnumSet
;
13 import java
.util
.HashMap
;
14 import java
.util
.HashSet
;
15 import java
.util
.List
;
18 import java
.util
.UUID
;
20 import org
.apache
.logging
.log4j
.LogManager
;
21 import org
.apache
.logging
.log4j
.Logger
;
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
.DescriptionType
;
42 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
43 import eu
.etaxonomy
.cdm
.model
.description
.IndividualsAssociation
;
44 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
45 import eu
.etaxonomy
.cdm
.model
.name
.INonViralName
;
46 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCode
;
47 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
48 import eu
.etaxonomy
.cdm
.model
.name
.RankClass
;
49 import eu
.etaxonomy
.cdm
.model
.name
.SpecimenTypeDesignation
;
50 import eu
.etaxonomy
.cdm
.model
.name
.SpecimenTypeDesignationStatus
;
51 import eu
.etaxonomy
.cdm
.model
.name
.TaxonName
;
52 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameFactory
;
53 import eu
.etaxonomy
.cdm
.model
.occurrence
.Collection
;
54 import eu
.etaxonomy
.cdm
.model
.occurrence
.DerivedUnit
;
55 import eu
.etaxonomy
.cdm
.model
.occurrence
.DeterminationEvent
;
56 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
57 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationType
;
58 import eu
.etaxonomy
.cdm
.model
.reference
.ISourceable
;
59 import eu
.etaxonomy
.cdm
.model
.reference
.OriginalSourceBase
;
60 import eu
.etaxonomy
.cdm
.model
.reference
.OriginalSourceType
;
61 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
62 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceFactory
;
63 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
64 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
65 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
66 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
67 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
68 import eu
.etaxonomy
.cdm
.model
.term
.DefinedTerm
;
69 import eu
.etaxonomy
.cdm
.persistence
.query
.MatchMode
;
70 import eu
.etaxonomy
.cdm
.strategy
.parser
.NonViralNameParserImpl
;
71 import eu
.etaxonomy
.cdm
.strategy
.parser
.ParserProblem
;
72 import eu
.etaxonomy
.cdm
.strategy
.parser
.TimePeriodParser
;
78 public abstract class SpecimenImportBase
<CONFIG
extends IImportConfigurator
, STATE
extends SpecimenImportStateBase
>
79 extends CdmImportBase
<CONFIG
, STATE
> {
81 private static final long serialVersionUID
= 4423065367998125678L;
82 private static final Logger logger
= LogManager
.getLogger();
84 protected static final UUID SPECIMEN_SCAN_TERM
= UUID
.fromString("acda15be-c0e2-4ea8-8783-b9b0c4ad7f03");
86 private static final String COLON
= ":";
88 protected Map
<String
, DefinedTerm
> kindOfUnitsMap
;
93 protected abstract void doInvoke(STATE state
);
96 * Handle a single unit
100 protected abstract void handleSingleUnit(STATE state
, Object item
) ;
104 protected TaxonName
getOrCreateTaxonName(String scientificName
, Rank rank
, boolean preferredFlag
, STATE state
, int unitIndexInAbcdFile
){
105 TaxonName taxonName
= null;
106 SpecimenImportConfiguratorBase
<?
,?
,?
> config
= state
.getConfig();
108 //check atomised name data for rank
109 //new name will be created
110 TaxonName atomisedTaxonName
= null;
111 if (rank
==null && unitIndexInAbcdFile
>=0 && ((state
.getDataHolder().getAtomisedIdentificationList() != null && !state
.getDataHolder().getAtomisedIdentificationList().isEmpty())|| state
.getDataHolder().getAtomisedIdentificationList().size() > 0)) {
112 atomisedTaxonName
= setTaxonNameByType(state
.getDataHolder().getAtomisedIdentificationList().get(unitIndexInAbcdFile
), scientificName
, state
);
113 if(atomisedTaxonName
!=null){
114 rank
= atomisedTaxonName
.getRank();
117 if(config
.isReuseExistingTaxaWhenPossible()){
118 TaxonName parsedName
= atomisedTaxonName
;
119 if(parsedName
==null){
121 parsedName
= parseScientificName(scientificName
, state
, state
.getReport(), rank
);
124 atomisedTaxonName
= parsedName
;
125 if(config
.isIgnoreAuthorship() && parsedName
!=null){// && preferredFlag){
126 // do not ignore authorship for non-preferred names because they need
127 // to be created for the determination history
128 String nameCache
= TaxonName
.castAndDeproxy(parsedName
).getNameCache();
129 List
<TaxonName
> names
= getNameService().findNamesByNameCache(nameCache
, MatchMode
.EXACT
, null);
130 if (!names
.isEmpty()){
131 taxonName
= getBestMatchingName(scientificName
, new ArrayList
<>(names
), state
);
133 if (taxonName
== null && !names
.isEmpty()){
134 taxonName
= names
.get(0);
138 //search for existing names
139 List
<TaxonName
> names
= getNameService().listByTitleWithRestrictions(TaxonName
.class, scientificName
, MatchMode
.EXACT
, null, null, null, null, null);
140 taxonName
= getBestMatchingName(scientificName
, names
, state
);
141 //still nothing found -> try with the atomised name full title cache
142 if(taxonName
==null && atomisedTaxonName
!=null){
143 names
= getNameService().listByTitleWithRestrictions(TaxonName
.class, atomisedTaxonName
.getFullTitleCache(), MatchMode
.EXACT
, null, null, null, null, null);
144 taxonName
= getBestMatchingName(atomisedTaxonName
.getTitleCache(), names
, state
);
145 //still nothing found -> try with the atomised name title cache
147 names
= getNameService().listByTitleWithRestrictions(TaxonName
.class, atomisedTaxonName
.getTitleCache(), MatchMode
.EXACT
, null, null, null, null, null);
148 taxonName
= getBestMatchingName(atomisedTaxonName
.getTitleCache(), names
, state
);
156 if(taxonName
==null && atomisedTaxonName
!=null){
157 taxonName
= atomisedTaxonName
;
158 state
.getReport().addName(taxonName
);
159 logger
.info("Created new taxon name "+taxonName
);
160 if(taxonName
.hasProblem()){
161 state
.getReport().addInfoMessage(String
.format("Created %s with parsing problems", taxonName
));
163 if(!atomisedTaxonName
.getTitleCache().equals(scientificName
)){
164 state
.getReport().addInfoMessage(String
.format("Taxon %s was parsed as %s", scientificName
, atomisedTaxonName
.getTitleCache()));
167 else if(taxonName
==null){
168 //create new taxon name
170 if (state
.getDataHolder().getNomenclatureCode().equals(NomenclaturalCode
.ICNAFP
)){
171 taxonName
= TaxonNameFactory
.NewBotanicalInstance(rank
);
172 }else if (state
.getDataHolder().getNomenclatureCode().equals(NomenclaturalCode
.ICZN
)){
173 taxonName
= TaxonNameFactory
.NewZoologicalInstance(rank
);
175 taxonName
= TaxonNameFactory
.NewNonViralInstance(rank
);
177 taxonName
.setFullTitleCache(scientificName
,true);
178 taxonName
.setTitleCache(scientificName
, true);
179 state
.getReport().addName(taxonName
);
180 logger
.info("Created new taxon name "+taxonName
);
182 if (taxonName
!= null){
183 state
.names
.put(taxonName
.getNameCache(), taxonName
);
185 if(!taxonName
.isPersited()) {
186 save(taxonName
, state
);
191 protected TaxonName
getBestMatchingName(String scientificName
, java
.util
.Collection
<TaxonName
> names
, STATE state
){
192 Set
<TaxonName
> namesWithAcceptedTaxa
= new HashSet
<>();
193 List
<TaxonName
> namesWithAcceptedTaxaInClassification
= new ArrayList
<>();
194 for (TaxonName name
: names
) {
195 if(!name
.getTaxa().isEmpty()){
196 Set
<Taxon
> taxa
= name
.getTaxa();
197 for (Taxon taxon
:taxa
){
198 if (!taxon
.getTaxonNodes().isEmpty()){
199 //use only taxa included in a classification
200 for (TaxonNode node
:taxon
.getTaxonNodes()){
201 if (state
.getClassification() != null && node
.getClassification().equals(state
.getClassification())){
202 namesWithAcceptedTaxaInClassification
.add(name
);
204 namesWithAcceptedTaxa
.add(name
);
213 String message
= String
.format("More than one taxon name was found for %s, maybe in other classifications!", scientificName
);
214 //check for names with accepted taxa in classification
215 if(namesWithAcceptedTaxaInClassification
.size()>0){
216 if(namesWithAcceptedTaxaInClassification
.size()>1){
218 state
.getReport().addInfoMessage(message
);
219 logger
.warn(message
);
222 return namesWithAcceptedTaxaInClassification
.iterator().next();
224 //check for any names with accepted taxa
225 if(namesWithAcceptedTaxa
.size()>0){
226 if(namesWithAcceptedTaxa
.size()>1){
228 state
.getReport().addInfoMessage(message
);
229 logger
.warn(message
);
232 return namesWithAcceptedTaxa
.iterator().next();
234 //no names with accepted taxa found -> check accepted taxa of synonyms
235 List
<Taxon
> taxaFromSynonyms
= new ArrayList
<>();
236 for (TaxonName name
: names
) {
237 Set
<TaxonBase
> taxonBases
= name
.getTaxonBases();
238 for (TaxonBase taxonBase
: taxonBases
) {
239 if(taxonBase
.isInstanceOf(Synonym
.class)){
240 Synonym synonym
= HibernateProxyHelper
.deproxy(taxonBase
, Synonym
.class);
241 taxaFromSynonyms
.add(synonym
.getAcceptedTaxon());
245 if(taxaFromSynonyms
.size()>0){
246 if(taxaFromSynonyms
.size()>1){
247 state
.getReport().addInfoMessage(message
);
248 logger
.warn(message
);
251 return taxaFromSynonyms
.iterator().next().getName();
253 //no accepted and no synonyms -> return one of the names and create a new taxon
254 if (names
.isEmpty()){
257 return names
.iterator().next();
261 * Parse automatically the scientific name
262 * @param scientificName the scientific name to parse
263 * @param state the current import state
264 * @param report the import report
265 * @return a parsed name
268 protected TaxonName
parseScientificName(String scientificName
, STATE state
, SpecimenImportReport report
, Rank rank
) {
270 NonViralNameParserImpl nvnpi
= NonViralNameParserImpl
.NewInstance();
271 TaxonName taxonName
= null;
272 boolean problem
= false;
274 if (logger
.isDebugEnabled()){
275 logger
.debug("parseScientificName " + state
.getDataHolder().getNomenclatureCode().toString());
278 if (state
.getDataHolder().getNomenclatureCode() != null && (state
.getDataHolder().getNomenclatureCode().toString().equals("Zoological") || state
.getDataHolder().getNomenclatureCode().toString().contains("ICZN"))) {
279 taxonName
= (TaxonName
)nvnpi
.parseFullName(scientificName
, NomenclaturalCode
.ICZN
, rank
);
280 if (taxonName
.hasProblem()) {
284 else if (state
.getDataHolder().getNomenclatureCode() != null && (state
.getDataHolder().getNomenclatureCode().toString().equals("Botanical") || state
.getDataHolder().getNomenclatureCode().toString().contains("ICBN") || state
.getDataHolder().getNomenclatureCode().toString().contains("ICNAFP"))) {
285 taxonName
= (TaxonName
)nvnpi
.parseFullName(scientificName
, NomenclaturalCode
.ICNAFP
, rank
);
286 if (taxonName
.hasProblem()) {
290 else if (state
.getDataHolder().getNomenclatureCode() != null && (state
.getDataHolder().getNomenclatureCode().toString().equals("Bacterial") || state
.getDataHolder().getNomenclatureCode().toString().contains("ICBN"))) {
291 taxonName
= (TaxonName
)nvnpi
.parseFullName(scientificName
, NomenclaturalCode
.ICNP
, rank
);
292 if (taxonName
.hasProblem()) {
296 else if (state
.getDataHolder().getNomenclatureCode() != null && (state
.getDataHolder().getNomenclatureCode().toString().equals("Cultivar") || state
.getDataHolder().getNomenclatureCode().toString().contains("ICNCP"))) {
297 taxonName
= (TaxonName
)nvnpi
.parseFullName(scientificName
, NomenclaturalCode
.ICNCP
, rank
);
298 if (taxonName
.hasProblem()) {
303 String message
= String
.format("Parsing problems for %s", scientificName
);
305 for (ParserProblem parserProblem
: taxonName
.getParsingProblems()) {
306 message
+= "\n\t- "+parserProblem
;
309 report
.addInfoMessage(message
);
310 logger
.info(message
);
317 * Create the name without automatic parsing, either because it failed, or because the user deactivated it.
318 * The name is built upon the ABCD fields
319 * @param atomisedMap : the ABCD atomised fields
320 * @param fullName : the full scientific name
322 * @return the corresponding Botanical or Zoological or... name
324 protected TaxonName
setTaxonNameByType(
325 HashMap
<String
, String
> atomisedMap
, String fullName
, STATE state
) {
326 boolean problem
= false;
327 if (logger
.isDebugEnabled()){
328 logger
.debug("settaxonnamebytype " + state
.getDataHolder().getNomenclatureCode().toString());
331 if (state
.getDataHolder().getNomenclatureCode().equals("Zoological") || state
.getDataHolder().getNomenclatureCode().equals(NomenclaturalCode
.ICZN
.getUuid())) {
332 TaxonName taxonName
= TaxonNameFactory
.NewZoologicalInstance(null);
333 taxonName
.setFullTitleCache(fullName
, true);
334 taxonName
.setGenusOrUninomial(NB(getFromMap(atomisedMap
, "Genus")));
335 taxonName
.setInfraGenericEpithet(NB(getFromMap(atomisedMap
, "SubGenus")));
336 taxonName
.setSpecificEpithet(NB(getFromMap(atomisedMap
,"SpeciesEpithet")));
337 taxonName
.setInfraSpecificEpithet(NB(getFromMap(atomisedMap
,"SubspeciesEpithet")));
339 if (taxonName
.getGenusOrUninomial() != null){
340 taxonName
.setRank(Rank
.GENUS());
343 if (taxonName
.getInfraGenericEpithet() != null){
344 taxonName
.setRank(Rank
.SUBGENUS());
347 if (taxonName
.getSpecificEpithet() != null){
348 taxonName
.setRank(Rank
.SPECIES());
351 if (taxonName
.getInfraSpecificEpithet() != null){
352 taxonName
.setRank(Rank
.SUBSPECIES());
356 if (getFromMap(atomisedMap
, "AuthorTeamParenthesis") != null) {
357 team
= Team
.NewInstance();
358 team
.setTitleCache(getFromMap(atomisedMap
, "AuthorTeamParenthesis"), true);
361 if (getFromMap(atomisedMap
, "AuthorTeamAndYear") != null) {
362 team
= Team
.NewInstance();
363 team
.setTitleCache(getFromMap(atomisedMap
, "AuthorTeamAndYear"), true);
367 taxonName
.setBasionymAuthorship(team
);
370 if (getFromMap(atomisedMap
, "AuthorTeamParenthesis") != null) {
371 taxonName
.setAuthorshipCache(getFromMap(atomisedMap
, "AuthorTeamParenthesis"));
373 else if (getFromMap(atomisedMap
, "AuthorTeamAndYear") != null) {
374 taxonName
.setAuthorshipCache(getFromMap(atomisedMap
, "AuthorTeamAndYear"));
377 if (getFromMap(atomisedMap
, "CombinationAuthorTeamAndYear") != null) {
378 team
= Team
.NewInstance();
379 team
.setTitleCache(getFromMap(atomisedMap
, "CombinationAuthorTeamAndYear"), true);
380 taxonName
.setCombinationAuthorship(team
);
382 if (taxonName
.hasProblem()) {
383 logger
.info("pb ICZN");
390 else if (state
.getDataHolder().getNomenclatureCode().equals("Botanical") || state
.getDataHolder().getNomenclatureCode().equals(NomenclaturalCode
.ICNAFP
.getUuid())) {
391 TaxonName taxonName
= parseScientificName(fullName
, state
, state
.getReport(), null);
392 if (taxonName
!= null){
396 taxonName
= TaxonNameFactory
.NewBotanicalInstance(null);
398 taxonName
.setFullTitleCache(fullName
, true);
399 taxonName
.setGenusOrUninomial(NB(getFromMap(atomisedMap
, "Genus")));
400 taxonName
.setSpecificEpithet(NB(getFromMap(atomisedMap
, "FirstEpithet")));
401 taxonName
.setInfraSpecificEpithet(NB(getFromMap(atomisedMap
, "InfraSpeEpithet")));
403 taxonName
.setRank(Rank
.getRankByLatinName(getFromMap(atomisedMap
, "Rank")));
404 } catch (Exception e
) {
405 if (taxonName
.getInfraSpecificEpithet() != null){
406 taxonName
.setRank(Rank
.SUBSPECIES());
408 else if (taxonName
.getSpecificEpithet() != null){
409 taxonName
.setRank(Rank
.SPECIES());
411 else if (taxonName
.getInfraGenericEpithet() != null){
412 taxonName
.setRank(Rank
.SUBGENUS());
414 else if (taxonName
.getGenusOrUninomial() != null){
415 taxonName
.setRank(Rank
.GENUS());
419 if (getFromMap(atomisedMap
, "AuthorTeamParenthesis") != null) {
420 team
= Team
.NewInstance();
421 team
.setTitleCache(getFromMap(atomisedMap
, "AuthorTeamParenthesis"), true);
422 taxonName
.setBasionymAuthorship(team
);
424 if (getFromMap(atomisedMap
, "AuthorTeam") != null) {
425 team
= Team
.NewInstance();
426 team
.setTitleCache(getFromMap(atomisedMap
, "AuthorTeam"), true);
427 taxonName
.setCombinationAuthorship(team
);
430 if (getFromMap(atomisedMap
, "AuthorTeamParenthesis") != null) {
431 taxonName
.setAuthorshipCache(getFromMap(atomisedMap
, "AuthorTeamParenthesis"));
433 else if (getFromMap(atomisedMap
, "AuthorTeam") != null) {
434 taxonName
.setAuthorshipCache(getFromMap(atomisedMap
, "AuthorTeam"));
437 if (getFromMap(atomisedMap
, "CombinationAuthorTeamAndYear") != null) {
438 team
= Team
.NewInstance();
439 team
.setTitleCache(getFromMap(atomisedMap
, "CombinationAuthorTeamAndYear"), true);
440 taxonName
.setCombinationAuthorship(team
);
442 if (taxonName
.hasProblem()) {
443 logger
.info("pb ICBN");
450 else if (state
.getDataHolder().getNomenclatureCode().equals("Bacterial") || state
.getDataHolder().getNomenclatureCode().equals(NomenclaturalCode
.ICNP
.getUuid())) {
451 TaxonName taxonName
= TaxonNameFactory
.NewBacterialInstance(null);
452 taxonName
.setFullTitleCache(fullName
, true);
453 taxonName
.setGenusOrUninomial(getFromMap(atomisedMap
, "Genus"));
454 taxonName
.setInfraGenericEpithet(NB(getFromMap(atomisedMap
, "SubGenus")));
455 taxonName
.setSpecificEpithet(NB(getFromMap(atomisedMap
, "Species")));
456 taxonName
.setInfraSpecificEpithet(NB(getFromMap(atomisedMap
, "SubspeciesEpithet")));
458 if (taxonName
.getGenusOrUninomial() != null){
459 taxonName
.setRank(Rank
.GENUS());
461 else if (taxonName
.getInfraGenericEpithet() != null){
462 taxonName
.setRank(Rank
.SUBGENUS());
464 else if (taxonName
.getSpecificEpithet() != null){
465 taxonName
.setRank(Rank
.SPECIES());
467 else if (taxonName
.getInfraSpecificEpithet() != null){
468 taxonName
.setRank(Rank
.SUBSPECIES());
471 if (getFromMap(atomisedMap
, "AuthorTeamAndYear") != null) {
472 Team team
= Team
.NewInstance();
473 team
.setTitleCache(getFromMap(atomisedMap
, "AuthorTeamAndYear"), true);
474 taxonName
.setCombinationAuthorship(team
);
476 if (getFromMap(atomisedMap
, "ParentheticalAuthorTeamAndYear") != null) {
477 Team team
= Team
.NewInstance();
478 team
.setTitleCache(getFromMap(atomisedMap
, "ParentheticalAuthorTeamAndYear"), true);
479 taxonName
.setBasionymAuthorship(team
);
481 if (taxonName
.hasProblem()) {
482 logger
.info("pb ICNP");
489 else if (state
.getDataHolder().getNomenclatureCode().equals("Cultivar")) {
490 TaxonName taxonName
= TaxonNameFactory
.NewCultivarInstance(null);
492 if (taxonName
.hasProblem()) {
493 logger
.info("pb ICNCP");
503 logger
.info("Problem im setTaxonNameByType ");
504 TaxonName taxonName
= TaxonNameFactory
.NewNonViralInstance(null);
505 taxonName
.setFullTitleCache(fullName
, true);
508 TaxonName tn
= TaxonNameFactory
.NewNonViralInstance(null);
513 * Get a formated string from a hashmap
518 private String
getFromMap(HashMap
<String
, String
> atomisedMap
, String key
) {
520 if (atomisedMap
.containsKey(key
)) {
521 value
= atomisedMap
.get(key
);
525 if (value
!= null && key
.matches(".*Year.*")) {
526 value
= value
.trim();
527 if (value
.matches("[a-z A-Z ]*[0-9]{4}$")) {
528 String tmp
= value
.split("[0-9]{4}$")[0];
529 int year
= Integer
.parseInt(value
.split(tmp
)[1]);
542 catch (Exception e
) {
549 * Very fast and dirty implementation to allow handling of transient objects as described in
550 * https://dev.e-taxonomy.eu/redmine/issues/3726
557 protected UUID
save(CdmBase cdmBase
, SpecimenImportStateBase state
) {
558 ICdmRepository cdmRepository
= state
.getConfig().getCdmAppController();
559 if (cdmRepository
== null){
560 cdmRepository
= this;
563 if (cdmBase
.isInstanceOf(LanguageString
.class)){
564 return cdmRepository
.getTermService().saveLanguageData(CdmBase
.deproxy(cdmBase
, LanguageString
.class));
565 }else if (cdmBase
.isInstanceOf(SpecimenOrObservationBase
.class)){
566 SpecimenOrObservationBase specimen
= CdmBase
.deproxy(cdmBase
, SpecimenOrObservationBase
.class);
568 return cdmRepository
.getOccurrenceService().saveOrUpdate(specimen
);
569 }else if (cdmBase
.isInstanceOf(Reference
.class)){
570 return cdmRepository
.getReferenceService().saveOrUpdate(CdmBase
.deproxy(cdmBase
, Reference
.class));
571 }else if (cdmBase
.isInstanceOf(Classification
.class)){
572 return cdmRepository
.getClassificationService().saveOrUpdate(CdmBase
.deproxy(cdmBase
, Classification
.class));
573 }else if (cdmBase
.isInstanceOf(AgentBase
.class)){
574 return cdmRepository
.getAgentService().saveOrUpdate(CdmBase
.deproxy(cdmBase
, AgentBase
.class));
575 }else if (cdmBase
.isInstanceOf(Collection
.class)){
576 return cdmRepository
.getCollectionService().saveOrUpdate(CdmBase
.deproxy(cdmBase
, Collection
.class));
577 }else if (cdmBase
.isInstanceOf(DescriptionBase
.class)){
578 DescriptionBase description
= CdmBase
.deproxy(cdmBase
, DescriptionBase
.class);
580 return cdmRepository
.getDescriptionService().saveOrUpdate(description
);
581 }else if (cdmBase
.isInstanceOf(TaxonBase
.class)){
582 return cdmRepository
.getTaxonService().saveOrUpdate(CdmBase
.deproxy(cdmBase
, TaxonBase
.class));
583 }else if (cdmBase
.isInstanceOf(TaxonName
.class)){
584 return cdmRepository
.getNameService().saveOrUpdate(CdmBase
.deproxy(cdmBase
, TaxonName
.class));
585 }else if (cdmBase
.isInstanceOf(TaxonNode
.class)){
586 return cdmRepository
.getTaxonNodeService().saveOrUpdate(CdmBase
.deproxy(cdmBase
, TaxonNode
.class));
588 throw new IllegalArgumentException("Class not supported in save method: " + CdmBase
.deproxy(cdmBase
, CdmBase
.class).getClass().getSimpleName());
594 protected SpecimenOrObservationBase
findExistingSpecimen(String unitId
, SpecimenImportStateBase state
){
595 ICdmRepository cdmAppController
= state
.getConfig().getCdmAppController();
596 if(cdmAppController
==null){
597 cdmAppController
= this;
599 FindOccurrencesConfigurator config
= new FindOccurrencesConfigurator();
600 config
.setSignificantIdentifier(unitId
);
601 List
<String
> propertyPaths
= new ArrayList
<>();
602 propertyPaths
.add("derivedFrom.*");
603 config
.setPropertyPaths(propertyPaths
);
604 commitTransaction(state
.getTx());
605 state
.setTx(startTransaction());
607 Pager
<SpecimenOrObservationBase
> existingSpecimens
= cdmAppController
.getOccurrenceService().findByTitle(config
);
608 if(!existingSpecimens
.getRecords().isEmpty()){
609 if(existingSpecimens
.getRecords().size()==1){
610 return existingSpecimens
.getRecords().iterator().next();
614 }catch(NullPointerException e
){
615 logger
.error("searching for existing specimen creates NPE: " + config
.getSignificantIdentifier());
623 protected abstract void importAssociatedUnits(STATE state
, Object item
, DerivedUnitFacade derivedUnitFacade
);
626 * getFacade : get the DerivedUnitFacade based on the recordBasis
629 * @return DerivedUnitFacade
631 protected DerivedUnitFacade
getFacade(STATE state
) {
632 if (logger
.isDebugEnabled()){
633 logger
.info("getFacade()");
635 SpecimenOrObservationType type
= null;
638 if (NB((state
.getDataHolder().getRecordBasis())) != null) {
639 if (state
.getDataHolder().getRecordBasis().toLowerCase().startsWith("s") || state
.getDataHolder().getRecordBasis().toLowerCase().indexOf("specimen")>-1) {// specimen
640 type
= SpecimenOrObservationType
.PreservedSpecimen
;
642 if (state
.getDataHolder().getRecordBasis().toLowerCase().startsWith("o") ||state
.getDataHolder().getRecordBasis().toLowerCase().indexOf("observation")>-1 ) {
643 type
= SpecimenOrObservationType
.Observation
;
645 if (state
.getDataHolder().getRecordBasis().toLowerCase().indexOf("fossil")>-1){
646 type
= SpecimenOrObservationType
.Fossil
;
648 if (state
.getDataHolder().getRecordBasis().toLowerCase().indexOf("living")>-1) {
649 type
= SpecimenOrObservationType
.LivingSpecimen
;
652 logger
.info("The basis of record does not seem to be known: " + state
.getDataHolder().getRecordBasis());
653 type
= SpecimenOrObservationType
.DerivedUnit
;
657 logger
.info("The basis of record is null");
658 type
= SpecimenOrObservationType
.DerivedUnit
;
660 DerivedUnitFacade derivedUnitFacade
= DerivedUnitFacade
.NewInstance(type
);
661 return derivedUnitFacade
;
665 * Look if the Institution does already exist
666 * @param institutionCode: a string with the institutioncode
667 * @param config : the configurator
668 * @return the Institution (existing or new)
670 protected Institution
getInstitution(String institutionCode
, STATE state
) {
671 SpecimenImportConfiguratorBase config
= state
.getConfig();
672 Institution institution
=null;
673 institution
= (Institution
)state
.institutions
.get(institutionCode
);
674 if (institution
!= null){
677 List
<Institution
> institutions
;
679 institutions
= getAgentService().searchInstitutionByCode(institutionCode
);
681 } catch (Exception e
) {
682 institutions
= new ArrayList
<Institution
>();
685 if (institutions
.size() > 0 && config
.isReuseExistingMetaData()) {
686 for (Institution institut
:institutions
){
688 if (institut
.getCode().equalsIgnoreCase(institutionCode
)) {
689 institution
=institut
;
692 }catch(Exception e
){logger
.warn("no institution code in the db");}
695 if (logger
.isDebugEnabled()){
696 if(institution
!=null) {
697 logger
.info("getinstitution " + institution
.toString());
700 if (institution
== null){
701 // create institution
702 institution
= Institution
.NewInstance();
703 institution
.setCode(institutionCode
);
704 institution
.setTitleCache(institutionCode
, true);
705 UUID uuid
= save(institution
, state
);
709 state
.institutions
.put(institutionCode
, institution
);
714 * Look if the Collection does already exist
715 * @param collectionCode
716 * @param collectionCode: a string
717 * @param config : the configurator
718 * @return the Collection (existing or new)
720 protected Collection
getCollection(Institution institution
, String collectionCode
, STATE state
) {
721 SpecimenImportConfiguratorBase config
= state
.getConfig();
722 Collection collection
= null;
723 List
<Collection
> collections
;
724 collection
= (Collection
) state
.collections
.get(collectionCode
);
725 if (collection
!= null){
729 collections
= getCollectionService().searchByCode(collectionCode
);
730 } catch (Exception e
) {
731 collections
= new ArrayList
<Collection
>();
733 if (collections
.size() > 0 && config
.isReuseExistingMetaData()) {
734 for (Collection coll
:collections
){
735 if (coll
.getCode() != null && coll
.getInstitute() != null
736 && coll
.getCode().equalsIgnoreCase(collectionCode
) && coll
.getInstitute().equals(institution
)) {
743 if(collection
== null){
744 collection
=Collection
.NewInstance();
745 collection
.setCode(collectionCode
);
746 collection
.setInstitute(institution
);
747 collection
.setTitleCache(collectionCode
);
748 UUID uuid
= save(collection
, state
);
753 state
.collections
.put(collectionCode
, collection
);
760 * @param citationDetail
763 //FIXME this method is highly critical, because
764 // * it will have serious performance and memory problems with large databases
765 // (databases may easily have >1 Mio source records)
766 // * it does not make sense to search for existing sources and then clone them
767 // we need to search for existing references instead and use them (if exist)
768 // for our new source.
769 protected IdentifiableSource
getIdentifiableSource(Reference reference
, String citationDetail
) {
771 IdentifiableSource sour
= IdentifiableSource
.NewInstance(OriginalSourceType
.Import
,null,null, reference
,citationDetail
);
776 * Add the hierarchy for a Taxon(add higher taxa)
777 * @param classification
778 * @param taxon: a taxon to add as a node
779 * @param state: the ABCD import state
781 protected void addParentTaxon(Taxon taxon
, STATE state
, boolean preferredFlag
, Classification classification
){
782 INonViralName nvname
= taxon
.getName();
783 Rank rank
= nvname
.getRank();
785 Taxon subgenus
=null;
786 Taxon species
= null;
787 Taxon subspecies
= null;
790 if (rank
.isLowerThan(RankClass
.Genus
)){
791 String genusOrUninomial
= nvname
.getGenusOrUninomial();
792 TaxonName taxonName
= getOrCreateTaxonName(genusOrUninomial
, Rank
.GENUS(), preferredFlag
, state
, -1);
793 genus
= getOrCreateTaxonForName(taxonName
, state
);
795 logger
.debug("The genus should not be null " + taxonName
);
798 parent
= linkParentChildNode(null, genus
, classification
, state
);
802 if (rank
.isLower(Rank
.SUBGENUS())){
803 String prefix
= nvname
.getGenusOrUninomial();
804 String name
= nvname
.getInfraGenericEpithet();
806 TaxonName taxonName
= getOrCreateTaxonName(prefix
+" "+name
, Rank
.SUBGENUS(), preferredFlag
, state
, -1);
807 subgenus
= getOrCreateTaxonForName(taxonName
, state
);
809 parent
= linkParentChildNode(genus
, subgenus
, classification
, state
);
812 if (rank
.isLowerThan(RankClass
.Species
)){
814 String prefix
= nvname
.getGenusOrUninomial();
815 String name
= nvname
.getInfraGenericEpithet();
816 String spe
= nvname
.getSpecificEpithet();
818 TaxonName taxonName
= getOrCreateTaxonName(prefix
+" "+name
+" "+spe
, Rank
.SPECIES(), preferredFlag
, state
, -1);
819 species
= getOrCreateTaxonForName(taxonName
, state
);
821 parent
= linkParentChildNode(subgenus
, species
, classification
, state
);
826 String prefix
= nvname
.getGenusOrUninomial();
827 String name
= nvname
.getSpecificEpithet();
829 TaxonName taxonName
= getOrCreateTaxonName(prefix
+" "+name
, Rank
.SPECIES(), preferredFlag
, state
, -1);
830 species
= getOrCreateTaxonForName(taxonName
, state
);
832 parent
= linkParentChildNode(genus
, species
, classification
, state
);
837 if (rank
.isLower(Rank
.INFRASPECIES())){
838 TaxonName taxonName
= getOrCreateTaxonName(nvname
.getFullTitleCache(), Rank
.SUBSPECIES(), preferredFlag
, state
, -1);
839 subspecies
= getOrCreateTaxonForName(taxonName
, state
);
841 parent
= linkParentChildNode(species
, subspecies
, classification
, state
);
845 //handle cf. and aff. taxa
846 String genusEpithet
= null;
847 if (nvname
.getTitleCache().contains("cf.")){
848 genusEpithet
= nvname
.getTitleCache().substring(0, nvname
.getTitleCache().indexOf("cf."));
849 } else if (nvname
.getTitleCache().contains("aff.")){
850 genusEpithet
= nvname
.getTitleCache().substring(0, nvname
.getTitleCache().indexOf("aff."));
852 if (genusEpithet
!= null){
853 genusEpithet
= genusEpithet
.trim();
854 TaxonName taxonName
= null;
855 if (genusEpithet
.contains(" ")){
856 taxonName
= getOrCreateTaxonName(genusEpithet
, Rank
.SPECIES(), preferredFlag
, state
, -1);
858 taxonName
= getOrCreateTaxonName(genusEpithet
, Rank
.GENUS(), preferredFlag
, state
, -1);
860 genus
= getOrCreateTaxonForName(taxonName
, state
);
862 logger
.debug("The genus should not be null " + taxonName
);
865 parent
= linkParentChildNode(null, genus
, classification
, state
);
869 if (preferredFlag
&& parent
!=taxon
) {
870 linkParentChildNode(parent
, taxon
, classification
, state
);
875 * Link a parent to a child and save it in the current classification
876 * @param parent: the higher Taxon
877 * @param child : the lower (or current) Taxon
878 * return the Taxon from the new created Node
879 * @param classification
882 protected Taxon
linkParentChildNode(Taxon parent
, Taxon child
, Classification classification
, STATE state
) {
883 TaxonNode node
=null;
884 List
<String
> propertyPaths
= new ArrayList
<>();
885 propertyPaths
.add("childNodes");
886 if (parent
!= null) {
888 parent
= (Taxon
) getTaxonService().load(parent
.getUuid(), propertyPaths
);
889 child
= (Taxon
) getTaxonService().load(child
.getUuid(), propertyPaths
);
890 //here we do not have to check if the taxon nodes already exists
891 //this is done by classification.addParentChild()
892 //do not add child node if it already exists
893 if(hasTaxonNodeInClassification(child
, classification
)){
897 node
= classification
.addParentChild(parent
, child
, state
.getRef(), "");
903 logger
.debug("The child should not be null!");
905 child
= (Taxon
) getTaxonService().find(child
.getUuid());
906 //do not add child node if it already exists
907 if(hasTaxonNodeInClassification(child
, classification
)){
911 node
= classification
.addChildTaxon(child
, state
.getRef(), null);
916 state
.getReport().addTaxonNode(node
);
917 return node
.getTaxon();
919 String message
= "Could not create taxon node for " +child
;
920 state
.getReport().addInfoMessage(message
);
921 logger
.warn(message
);
925 protected Taxon
getOrCreateTaxonForName(TaxonName taxonName
, STATE state
){
926 if (taxonName
!= null){
927 Set
<Taxon
> acceptedTaxa
= taxonName
.getTaxa();
928 if(acceptedTaxa
.size()>0){
929 Taxon firstAcceptedTaxon
= acceptedTaxa
.iterator().next();
930 if(acceptedTaxa
.size()>1){
931 String message
= "More than one accepted taxon was found for taxon name: "
932 + taxonName
.getTitleCache() + "!\n" + firstAcceptedTaxon
+ "was chosen for "+state
.getDerivedUnitBase();
933 state
.getReport().addInfoMessage(message
);
934 logger
.warn(message
);
937 return firstAcceptedTaxon
;
941 Set
<TaxonBase
> taxonAndSynonyms
= taxonName
.getTaxonBases();
942 for (TaxonBase taxonBase
: taxonAndSynonyms
) {
943 if(taxonBase
.isInstanceOf(Synonym
.class)){
944 Synonym synonym
= HibernateProxyHelper
.deproxy(taxonBase
, Synonym
.class);
945 Taxon acceptedTaxonOfSynonym
= synonym
.getAcceptedTaxon();
946 if(acceptedTaxonOfSynonym
== null){
947 String message
= "No accepted taxon could be found for taxon name: "
948 + taxonName
.getTitleCache()
950 state
.getReport().addInfoMessage(message
);
951 logger
.warn(message
);
954 return acceptedTaxonOfSynonym
;
959 Taxon taxon
= Taxon
.NewInstance(taxonName
, state
.getRef());
961 state
.getReport().addTaxon(taxon
);
962 logger
.info("Created new taxon "+ taxon
);
969 private boolean hasTaxonNodeInClassification(Taxon taxon
, Classification classification
){
970 if(taxon
.getTaxonNodes()!=null){
971 for (TaxonNode node
: taxon
.getTaxonNodes()){
972 if(node
.getClassification().equals(classification
)){
981 * HandleIdentifications : get the scientific names present in the ABCD
982 * document and store link them with the observation/specimen data
983 * @param state: the current ABCD import state
984 * @param derivedUnitFacade : the current derivedunitfacade
986 protected void handleIdentifications(STATE state
, DerivedUnitFacade derivedUnitFacade
) {
988 SpecimenImportConfiguratorBase config
= state
.getConfig();
990 String scientificName
= "";
991 boolean preferredFlag
= false;
993 if (state
.getDataHolder().getNomenclatureCode() == ""){
994 if (config
.getNomenclaturalCode() != null){
995 if (config
.getNomenclaturalCode() != null){
996 state
.getDataHolder().setNomenclatureCode(config
.getNomenclaturalCode().toString());
1002 for (int i
= 0; i
< state
.getDataHolder().getIdentificationList().size(); i
++) {
1003 Identification identification
= state
.getDataHolder().getIdentificationList().get(i
);
1004 scientificName
= identification
.getScientificName().replaceAll(" et ", " & ");
1006 String preferred
= identification
.getPreferred();
1007 preferredFlag
= false;
1008 if (preferred
!= null || state
.getDataHolder().getIdentificationList().size()==1){
1009 if (state
.getDataHolder().getIdentificationList().size()==1){
1010 preferredFlag
= true;
1011 }else if (preferred
!= null && (preferred
.equals("1") || preferred
.toLowerCase().indexOf("true") != -1) ) {
1012 preferredFlag
= true;
1016 if (identification
.getCode() != null){
1017 if (identification
.getCode().indexOf(':') != -1) {
1018 state
.getDataHolder().setNomenclatureCode(identification
.getCode().split(COLON
)[1]);
1021 state
.getDataHolder().setNomenclatureCode(identification
.getCode());
1024 TaxonName taxonName
= getOrCreateTaxonName(scientificName
, null, preferredFlag
, state
, i
);
1025 Taxon taxon
= getOrCreateTaxonForName(taxonName
, state
);
1026 addTaxonNode(taxon
, state
,preferredFlag
);
1027 linkDeterminationEvent(state
, taxon
, preferredFlag
, derivedUnitFacade
, identification
.getIdentifier(), identification
.getDate(), identification
.getModifier());
1032 * @param taxon : a taxon to add as a node
1033 * @param state : the ABCD import state
1035 protected void addTaxonNode(Taxon taxon
, STATE state
, boolean preferredFlag
) {
1036 SpecimenImportConfiguratorBase
<?
,?
,?
> config
= state
.getConfig();
1037 logger
.info("link taxon to a taxonNode "+taxon
.getTitleCache());
1038 //only add nodes if not already existing in current classification or default classification
1040 //check if node exists in current classification
1041 //NOTE: we cannot use hasTaxonNodeInClassification() here because we are first creating it here
1042 if (!existsInClassification(taxon
,state
.getClassification(), state
)){
1043 if(config
.isMoveNewTaxaToDefaultClassification()){
1044 //check if node exists in default classification
1045 if (!existsInClassification(taxon
, state
.getDefaultClassification(true), state
)){
1046 addParentTaxon(taxon
, state
, preferredFlag
, state
.getDefaultClassification(true));
1049 //add non-existing taxon to current classification
1050 addParentTaxon(taxon
, state
, preferredFlag
, state
.getClassification());
1057 private boolean existsInClassification(Taxon taxon
, Classification classification
, STATE state
){
1058 boolean exist
= false;
1059 ICdmRepository cdmAppController
= state
.getConfig().getCdmAppController();
1060 if(cdmAppController
==null){
1061 cdmAppController
= this;
1063 if (classification
!= null){
1064 if (!taxon
.getTaxonNodes().isEmpty()){
1065 for (TaxonNode node
:taxon
.getTaxonNodes()){
1066 if (node
.getClassification().equals(classification
)){
1071 // we do not need this because we already searched for taxa in db in the previous steps
1072 // List<UuidAndTitleCache<TaxonNode>> uuidAndTitleCacheOfAllTaxa = cdmAppController.getClassificationService().getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(classification.getUuid());
1073 // if (uuidAndTitleCacheOfAllTaxa != null){
1074 // for (UuidAndTitleCache p : uuidAndTitleCacheOfAllTaxa){
1076 // if(p.getTitleCache().equals(taxon.getTitleCache())) {
1080 // catch(Exception e){
1081 // logger.warn("TaxonNode doesn't seem to have a taxon");
1090 * join DeterminationEvent to the Taxon Object
1091 * @param state : the ABCD import state
1092 * @param taxon: the current Taxon
1093 * @param preferredFlag :if the current name is preferred
1094 * @param derivedFacade : the derived Unit Facade
1096 @SuppressWarnings("rawtypes")
1097 protected void linkDeterminationEvent(STATE state
, Taxon taxon
, boolean preferredFlag
, DerivedUnitFacade derivedFacade
, String identifierStr
, String dateStr
, String modifier
) {
1098 SpecimenImportConfiguratorBase config
= state
.getConfig();
1099 if (logger
.isDebugEnabled()){
1100 logger
.info("start linkdetermination with taxon:" + taxon
.getUuid()+", "+taxon
);
1103 DeterminationEvent determinationEvent
= DeterminationEvent
.NewInstance();
1104 //determinationEvent.setTaxon(taxon);
1105 determinationEvent
.setTaxonName(taxon
.getName());
1106 determinationEvent
.setPreferredFlag(preferredFlag
);
1109 determinationEvent
.setIdentifiedUnit(state
.getDerivedUnitBase());
1110 if (state
.getPersonStore().get(identifierStr
) != null){
1111 determinationEvent
.setActor((AgentBase
)state
.getPersonStore().get(identifierStr
));
1112 } else if (identifierStr
!= null){
1113 Person identifier
= Person
.NewTitledInstance(identifierStr
);
1114 determinationEvent
.setActor(identifier
);
1116 if (dateStr
!= null){
1117 determinationEvent
.setTimeperiod(TimePeriodParser
.parseString(dateStr
));
1119 if (modifier
!= null){
1120 if (modifier
.equals("cf.")){
1121 determinationEvent
.setModifier(DefinedTerm
.DETERMINATION_MODIFIER_CONFER());
1122 }else if (modifier
.equals("aff.")){
1123 determinationEvent
.setModifier(DefinedTerm
.DETERMINATION_MODIFIER_AFFINIS());
1126 state
.getDerivedUnitBase().addDetermination(determinationEvent
);
1128 if (logger
.isDebugEnabled()){
1129 logger
.debug("NB TYPES INFO: "+ state
.getDataHolder().getStatusList().size());
1131 for (SpecimenTypeDesignationStatus specimenTypeDesignationstatus
: state
.getDataHolder().getStatusList()) {
1132 if (specimenTypeDesignationstatus
!= null) {
1133 if (logger
.isDebugEnabled()){
1134 logger
.debug("specimenTypeDesignationstatus :"+ specimenTypeDesignationstatus
);
1137 ICdmRepository cdmAppController
= config
.getCdmAppController();
1138 if(cdmAppController
== null){
1139 cdmAppController
= this;
1141 specimenTypeDesignationstatus
= HibernateProxyHelper
.deproxy(cdmAppController
.getTermService().find(specimenTypeDesignationstatus
.getUuid()), SpecimenTypeDesignationStatus
.class);
1143 TaxonName name
= taxon
.getName();
1144 SpecimenTypeDesignation designation
= SpecimenTypeDesignation
.NewInstance();
1146 designation
.setTypeStatus(specimenTypeDesignationstatus
);
1147 designation
.setTypeSpecimen(state
.getDerivedUnitBase());
1148 name
.addTypeDesignation(designation
, false);
1151 save(state
.getDerivedUnitBase(), state
);
1153 for (String
[] fullReference
: state
.getDataHolder().getReferenceList()) {
1156 String strReference
=fullReference
[0];
1157 String citationDetail
= fullReference
[1];
1158 String citationURL
= fullReference
[2];
1159 List
<Reference
> references
= getReferenceService().listByTitleWithRestrictions(Reference
.class, "strReference", MatchMode
.EXACT
, null, null, null, null, null);
1161 if (!references
.isEmpty()){
1162 Reference reference
= null;
1163 for (Reference refe
: references
) {
1164 if (refe
.getTitleCache().equalsIgnoreCase(strReference
)) {
1169 if (reference
==null){
1170 reference
= ReferenceFactory
.newGeneric();
1171 reference
.setTitleCache(strReference
, true);
1172 save(reference
, state
);
1174 determinationEvent
.addReference(reference
);
1177 save(state
.getDerivedUnitBase(), state
);
1179 if (config
.isAddIndividualsAssociationsSuchAsSpecimenAndObservations() && preferredFlag
) {
1180 //do not add IndividualsAssociation to non-preferred taxa
1181 if (logger
.isDebugEnabled()){
1182 logger
.debug("isDoCreateIndividualsAssociations");
1185 makeIndividualsAssociation(state
, taxon
, determinationEvent
);
1187 save(state
.getDerivedUnitBase(), state
);
1192 * create and link each association (specimen, observation..) to the accepted taxon
1193 * @param state : the ABCD import state
1194 * @param taxon: the current Taxon
1195 * @param determinationEvent:the determinationevent
1197 protected void makeIndividualsAssociation(STATE state
, Taxon taxon
, DeterminationEvent determinationEvent
) {
1198 SpecimenImportConfiguratorBase
<?
,?
,?
> config
= state
.getConfig();
1199 SpecimenUserInteraction sui
= config
.getSpecimenUserInteraction();
1201 if (logger
.isDebugEnabled()){
1202 logger
.info("MAKE INDIVIDUALS ASSOCIATION");
1205 TaxonDescription taxonDescription
= null;
1206 Set
<TaxonDescription
> descriptions
= taxon
.getDescriptions();
1207 if (state
.getIndividualsAssociationDescriptionPerTaxon(taxon
.getUuid()) != null){
1208 taxonDescription
= state
.getIndividualsAssociationDescriptionPerTaxon(taxon
.getUuid());
1210 if (taxonDescription
== null && !descriptions
.isEmpty() && state
.getConfig().isReuseExistingDescriptiveGroups()){
1211 for (TaxonDescription desc
: descriptions
){
1212 if (desc
.getTypes().contains(DescriptionType
.INDIVIDUALS_ASSOCIATION
)){
1213 taxonDescription
= desc
;
1219 if (taxonDescription
== null){
1220 taxonDescription
= TaxonDescription
.NewInstance(taxon
, false);
1221 taxonDescription
.setTypes(EnumSet
.of(DescriptionType
.INDIVIDUALS_ASSOCIATION
));
1222 if(sourceNotLinkedToElement(taxonDescription
,state
.getRef(),null)) {
1223 taxonDescription
.addSource(OriginalSourceType
.Import
, null, null, state
.getRef(), null);
1225 state
.setIndividualsAssociationDescriptionPerTaxon(taxonDescription
);
1226 taxon
.addDescription(taxonDescription
);
1229 //PREPARE REFERENCE QUESTIONS
1231 Map
<String
,OriginalSourceBase
> sourceMap
= new HashMap
<>();
1233 List
<IdentifiableSource
> issTmp
= new ArrayList
<>();//getCommonService().list(IdentifiableSource.class, null, null, null, null);
1234 List
<DescriptionElementSource
> issTmp2
= new ArrayList
<>();//getCommonService().list(DescriptionElementSource.class, null, null, null, null);
1236 Set
<OriginalSourceBase
> osbSet
= new HashSet
<>();
1238 osbSet
.addAll(issTmp2
);
1241 osbSet
.addAll(issTmp
);
1245 addToSourceMap(sourceMap
, osbSet
);
1248 if(sourceNotLinkedToElement(taxonDescription
,state
.getRef(),null)) {
1249 taxonDescription
.addSource(OriginalSourceType
.Import
,null, null, state
.getRef(), null);
1252 state
.setIndividualsAssociationDescriptionPerTaxon(taxonDescription
);
1254 IndividualsAssociation indAssociation
= IndividualsAssociation
.NewInstance();
1255 Feature feature
= makeFeature(state
.getDerivedUnitBase());
1256 indAssociation
.setAssociatedSpecimenOrObservation(state
.getDerivedUnitBase());
1257 indAssociation
.setFeature(feature
);
1259 if(sourceNotLinkedToElement(indAssociation
,state
.getImportReference(state
.getActualAccessPoint()),null)) {
1260 indAssociation
.addSource(OriginalSourceType
.Import
,null, null, state
.getImportReference(state
.getActualAccessPoint()), null);
1262 if(sourceNotLinkedToElement(state
.getDerivedUnitBase(), state
.getImportReference(state
.getActualAccessPoint()),null)) {
1263 state
.getDerivedUnitBase().addSource(OriginalSourceType
.Import
,null, null, state
.getImportReference(state
.getActualAccessPoint()), null);
1265 for (Reference citation
: determinationEvent
.getReferences()) {
1266 if(sourceNotLinkedToElement(indAssociation
,citation
,null))
1268 indAssociation
.addSource(DescriptionElementSource
.NewInstance(OriginalSourceType
.Import
, null, null, citation
, null));
1270 if(sourceNotLinkedToElement(state
.getDerivedUnitBase(), state
.getImportReference(state
.getActualAccessPoint()),null)) {
1271 state
.getDerivedUnitBase().addSource(OriginalSourceType
.Import
,null, null, state
.getImportReference(state
.getActualAccessPoint()), null);
1276 taxonDescription
.addElement(indAssociation
);
1278 save(taxonDescription
, state
);
1280 state
.getReport().addDerivate(state
.getDerivedUnitBase(), config
);
1281 state
.getReport().addIndividualAssociation(taxon
, state
.getDataHolder().getUnitID(), state
.getDerivedUnitBase());
1285 * @param derivedUnitBase2
1290 private boolean sourceNotLinkedToElement(DerivedUnit derivedUnitBase2
, Reference b
, String d
) {
1291 Set
<IdentifiableSource
> linkedSources
= derivedUnitBase2
.getSources();
1292 for (IdentifiableSource is
:linkedSources
){
1293 Reference a
= is
.getCitation();
1294 String c
= is
.getCitationMicroReference();
1296 boolean refMatch
=false;
1297 boolean microMatch
=false;
1300 if (a
==null && b
==null) {
1303 if (a
!=null && b
!=null) {
1304 if (a
.getTitleCache().equalsIgnoreCase(b
.getTitleCache())) {
1308 }catch(Exception e
){}
1312 if (c
==null && d
==null) {
1315 if(c
!=null && d
!=null) {
1316 if(c
.equalsIgnoreCase(d
)) {
1321 catch(Exception e
){}
1323 if (microMatch
&& refMatch
) {
1332 private <T
extends OriginalSourceBase
> boolean sourceNotLinkedToElement(ISourceable
<T
> sourcable
, Reference reference
, String microReference
) {
1333 Set
<T
> linkedSources
= sourcable
.getSources();
1334 for (T is
:linkedSources
){
1335 Reference unitReference
= is
.getCitation();
1336 String unitMicroReference
= is
.getCitationMicroReference();
1338 boolean refMatch
=false;
1339 boolean microMatch
=false;
1342 if (unitReference
==null && reference
==null) {
1345 if (unitReference
!=null && reference
!=null) {
1346 if (unitReference
.getTitleCache().equalsIgnoreCase(reference
.getTitleCache())) {
1350 }catch(Exception e
){}
1353 if (unitMicroReference
==null && microReference
==null) {
1356 if(unitMicroReference
!=null && microReference
!=null) {
1357 if(unitMicroReference
.equalsIgnoreCase(microReference
)) {
1362 catch(Exception e
){}
1364 if (microMatch
&& refMatch
) {
1372 * look for the Feature object (FieldObs, Specimen,...)
1373 * @param unit : a specimen or obersvation base
1374 * @return the corresponding Feature
1376 private Feature
makeFeature(SpecimenOrObservationBase
<?
> unit
) {
1377 SpecimenOrObservationType type
= unit
.getRecordBasis();
1381 if (type
.isFeatureObservation()){
1382 return Feature
.OBSERVATION();
1383 }else if (type
.isFeatureSpecimen()){
1384 return Feature
.SPECIMEN();
1385 }else if (type
== SpecimenOrObservationType
.DerivedUnit
){
1386 return Feature
.OBSERVATION();
1387 // return getFeature("Specimen or observation");
1389 String message
= "Unhandled record basis '%s' for defining individuals association feature type. Use default.";
1390 logger
.warn(String
.format(message
, type
.getLabel()));
1391 return Feature
.OBSERVATION();
1392 // return getFeature("Specimen or observation");
1402 protected void addToSourceMap(Map
<String
, OriginalSourceBase
> sourceMap
, Set
<OriginalSourceBase
> osbSet
) {
1403 for( OriginalSourceBase osb
:osbSet
) {
1404 if(osb
.getCitation()!=null && osb
.getCitationMicroReference() !=null && !osb
.getCitationMicroReference().isEmpty()) {
1406 sourceMap
.put(osb
.getCitation().getTitleCache()+ "---"+osb
.getCitationMicroReference(),osb
);
1407 }catch(NullPointerException e
){logger
.warn("null pointer problem (no ref?) with "+osb
);}
1408 } else if(osb
.getCitation()!=null){
1410 sourceMap
.put(osb
.getCitation().getTitleCache(),osb
);
1411 }catch(NullPointerException e
){logger
.warn("null pointer problem (no ref?) with "+osb
);}