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.
10 package eu
.etaxonomy
.cdm
.io
.specimen
;
13 import java
.util
.ArrayList
;
14 import java
.util
.HashMap
;
15 import java
.util
.HashSet
;
16 import java
.util
.List
;
19 import java
.util
.UUID
;
21 import org
.apache
.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
.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
;
77 public abstract class SpecimenImportBase
<CONFIG
extends IImportConfigurator
, STATE
extends SpecimenImportStateBase
>
78 extends CdmImportBase
<CONFIG
, STATE
> {
80 private static final long serialVersionUID
= 4423065367998125678L;
81 private static final Logger logger
= Logger
.getLogger(SpecimenImportBase
.class);
83 protected static final UUID SPECIMEN_SCAN_TERM
= UUID
.fromString("acda15be-c0e2-4ea8-8783-b9b0c4ad7f03");
85 private static final String COLON
= ":";
87 protected Map
<String
, DefinedTerm
> kindOfUnitsMap
;
92 protected abstract void doInvoke(STATE state
);
95 * Handle a single unit
99 protected abstract void handleSingleUnit(STATE state
, Object item
) ;
103 protected TaxonName
getOrCreateTaxonName(String scientificName
, Rank rank
, boolean preferredFlag
, STATE state
, int unitIndexInAbcdFile
){
104 TaxonName taxonName
= null;
105 SpecimenImportConfiguratorBase
<?
,?
,?
> config
= state
.getConfig();
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();
116 if(config
.isReuseExistingTaxaWhenPossible()){
117 TaxonName parsedName
= atomisedTaxonName
;
118 if(parsedName
==null){
120 parsedName
= parseScientificName(scientificName
, state
, state
.getReport(), rank
);
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
);
132 if (taxonName
== null && !names
.isEmpty()){
133 taxonName
= names
.get(0);
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
146 names
= getNameService().listByTitleWithRestrictions(TaxonName
.class, atomisedTaxonName
.getTitleCache(), MatchMode
.EXACT
, null, null, null, null, null);
147 taxonName
= getBestMatchingName(atomisedTaxonName
.getTitleCache(), names
, state
);
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
));
162 if(!atomisedTaxonName
.getTitleCache().equals(scientificName
)){
163 state
.getReport().addInfoMessage(String
.format("Taxon %s was parsed as %s", scientificName
, atomisedTaxonName
.getTitleCache()));
166 else if(taxonName
==null){
167 //create new taxon name
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
);
174 taxonName
= TaxonNameFactory
.NewNonViralInstance(rank
);
176 taxonName
.setFullTitleCache(scientificName
,true);
177 taxonName
.setTitleCache(scientificName
, true);
178 state
.getReport().addName(taxonName
);
179 logger
.info("Created new taxon name "+taxonName
);
181 if (taxonName
!= null){
182 state
.names
.put(taxonName
.getNameCache(), taxonName
);
184 if(!taxonName
.isPersited()) {
185 save(taxonName
, state
);
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
);
203 namesWithAcceptedTaxa
.add(name
);
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){
217 state
.getReport().addInfoMessage(message
);
218 logger
.warn(message
);
221 return namesWithAcceptedTaxaInClassification
.iterator().next();
223 //check for any names with accepted taxa
224 if(namesWithAcceptedTaxa
.size()>0){
225 if(namesWithAcceptedTaxa
.size()>1){
227 state
.getReport().addInfoMessage(message
);
228 logger
.warn(message
);
231 return namesWithAcceptedTaxa
.iterator().next();
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());
244 if(taxaFromSynonyms
.size()>0){
245 if(taxaFromSynonyms
.size()>1){
246 state
.getReport().addInfoMessage(message
);
247 logger
.warn(message
);
250 return taxaFromSynonyms
.iterator().next().getName();
252 //no accepted and no synonyms -> return one of the names and create a new taxon
253 if (names
.isEmpty()){
256 return names
.iterator().next();
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
267 protected TaxonName
parseScientificName(String scientificName
, STATE state
, SpecimenImportReport report
, Rank rank
) {
269 NonViralNameParserImpl nvnpi
= NonViralNameParserImpl
.NewInstance();
270 TaxonName taxonName
= null;
271 boolean problem
= false;
273 if (logger
.isDebugEnabled()){
274 logger
.debug("parseScientificName " + state
.getDataHolder().getNomenclatureCode().toString());
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()) {
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()) {
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()) {
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()) {
302 String message
= String
.format("Parsing problems for %s", scientificName
);
304 for (ParserProblem parserProblem
: taxonName
.getParsingProblems()) {
305 message
+= "\n\t- "+parserProblem
;
308 report
.addInfoMessage(message
);
309 logger
.info(message
);
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
321 * @return the corresponding Botanical or Zoological or... name
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());
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")));
338 if (taxonName
.getGenusOrUninomial() != null){
339 taxonName
.setRank(Rank
.GENUS());
342 if (taxonName
.getInfraGenericEpithet() != null){
343 taxonName
.setRank(Rank
.SUBGENUS());
346 if (taxonName
.getSpecificEpithet() != null){
347 taxonName
.setRank(Rank
.SPECIES());
350 if (taxonName
.getInfraSpecificEpithet() != null){
351 taxonName
.setRank(Rank
.SUBSPECIES());
355 if (getFromMap(atomisedMap
, "AuthorTeamParenthesis") != null) {
356 team
= Team
.NewInstance();
357 team
.setTitleCache(getFromMap(atomisedMap
, "AuthorTeamParenthesis"), true);
360 if (getFromMap(atomisedMap
, "AuthorTeamAndYear") != null) {
361 team
= Team
.NewInstance();
362 team
.setTitleCache(getFromMap(atomisedMap
, "AuthorTeamAndYear"), true);
366 taxonName
.setBasionymAuthorship(team
);
369 if (getFromMap(atomisedMap
, "AuthorTeamParenthesis") != null) {
370 taxonName
.setAuthorshipCache(getFromMap(atomisedMap
, "AuthorTeamParenthesis"));
372 else if (getFromMap(atomisedMap
, "AuthorTeamAndYear") != null) {
373 taxonName
.setAuthorshipCache(getFromMap(atomisedMap
, "AuthorTeamAndYear"));
376 if (getFromMap(atomisedMap
, "CombinationAuthorTeamAndYear") != null) {
377 team
= Team
.NewInstance();
378 team
.setTitleCache(getFromMap(atomisedMap
, "CombinationAuthorTeamAndYear"), true);
379 taxonName
.setCombinationAuthorship(team
);
381 if (taxonName
.hasProblem()) {
382 logger
.info("pb ICZN");
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){
395 taxonName
= TaxonNameFactory
.NewBotanicalInstance(null);
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")));
402 taxonName
.setRank(Rank
.getRankByName(getFromMap(atomisedMap
, "Rank")));
403 } catch (Exception e
) {
404 if (taxonName
.getInfraSpecificEpithet() != null){
405 taxonName
.setRank(Rank
.SUBSPECIES());
407 else if (taxonName
.getSpecificEpithet() != null){
408 taxonName
.setRank(Rank
.SPECIES());
410 else if (taxonName
.getInfraGenericEpithet() != null){
411 taxonName
.setRank(Rank
.SUBGENUS());
413 else if (taxonName
.getGenusOrUninomial() != null){
414 taxonName
.setRank(Rank
.GENUS());
418 if (getFromMap(atomisedMap
, "AuthorTeamParenthesis") != null) {
419 team
= Team
.NewInstance();
420 team
.setTitleCache(getFromMap(atomisedMap
, "AuthorTeamParenthesis"), true);
421 taxonName
.setBasionymAuthorship(team
);
423 if (getFromMap(atomisedMap
, "AuthorTeam") != null) {
424 team
= Team
.NewInstance();
425 team
.setTitleCache(getFromMap(atomisedMap
, "AuthorTeam"), true);
426 taxonName
.setCombinationAuthorship(team
);
429 if (getFromMap(atomisedMap
, "AuthorTeamParenthesis") != null) {
430 taxonName
.setAuthorshipCache(getFromMap(atomisedMap
, "AuthorTeamParenthesis"));
432 else if (getFromMap(atomisedMap
, "AuthorTeam") != null) {
433 taxonName
.setAuthorshipCache(getFromMap(atomisedMap
, "AuthorTeam"));
436 if (getFromMap(atomisedMap
, "CombinationAuthorTeamAndYear") != null) {
437 team
= Team
.NewInstance();
438 team
.setTitleCache(getFromMap(atomisedMap
, "CombinationAuthorTeamAndYear"), true);
439 taxonName
.setCombinationAuthorship(team
);
441 if (taxonName
.hasProblem()) {
442 logger
.info("pb ICBN");
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")));
457 if (taxonName
.getGenusOrUninomial() != null){
458 taxonName
.setRank(Rank
.GENUS());
460 else if (taxonName
.getInfraGenericEpithet() != null){
461 taxonName
.setRank(Rank
.SUBGENUS());
463 else if (taxonName
.getSpecificEpithet() != null){
464 taxonName
.setRank(Rank
.SPECIES());
466 else if (taxonName
.getInfraSpecificEpithet() != null){
467 taxonName
.setRank(Rank
.SUBSPECIES());
470 if (getFromMap(atomisedMap
, "AuthorTeamAndYear") != null) {
471 Team team
= Team
.NewInstance();
472 team
.setTitleCache(getFromMap(atomisedMap
, "AuthorTeamAndYear"), true);
473 taxonName
.setCombinationAuthorship(team
);
475 if (getFromMap(atomisedMap
, "ParentheticalAuthorTeamAndYear") != null) {
476 Team team
= Team
.NewInstance();
477 team
.setTitleCache(getFromMap(atomisedMap
, "ParentheticalAuthorTeamAndYear"), true);
478 taxonName
.setBasionymAuthorship(team
);
480 if (taxonName
.hasProblem()) {
481 logger
.info("pb ICNB");
488 else if (state
.getDataHolder().getNomenclatureCode().equals("Cultivar")) {
489 TaxonName taxonName
= TaxonNameFactory
.NewCultivarInstance(null);
491 if (taxonName
.hasProblem()) {
492 logger
.info("pb ICNCP");
502 logger
.info("Problem im setTaxonNameByType ");
503 TaxonName taxonName
= TaxonNameFactory
.NewNonViralInstance(null);
504 taxonName
.setFullTitleCache(fullName
, true);
507 TaxonName tn
= TaxonNameFactory
.NewNonViralInstance(null);
512 * Get a formated string from a hashmap
517 private String
getFromMap(HashMap
<String
, String
> atomisedMap
, String key
) {
519 if (atomisedMap
.containsKey(key
)) {
520 value
= atomisedMap
.get(key
);
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]);
541 catch (Exception e
) {
548 * Very fast and dirty implementation to allow handling of transient objects as described in
549 * https://dev.e-taxonomy.eu/trac/ticket/3726
556 protected UUID
save(CdmBase cdmBase
, SpecimenImportStateBase state
) {
557 ICdmRepository cdmRepository
= state
.getConfig().getCdmAppController();
558 if (cdmRepository
== null){
559 cdmRepository
= this;
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);
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);
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));
587 throw new IllegalArgumentException("Class not supported in save method: " + CdmBase
.deproxy(cdmBase
, CdmBase
.class).getClass().getSimpleName());
593 protected SpecimenOrObservationBase
findExistingSpecimen(String unitId
, SpecimenImportStateBase state
){
594 ICdmRepository cdmAppController
= state
.getConfig().getCdmAppController();
595 if(cdmAppController
==null){
596 cdmAppController
= this;
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());
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();
613 }catch(NullPointerException e
){
614 logger
.error("searching for existing specimen creates NPE: " + config
.getSignificantIdentifier());
622 protected abstract void importAssociatedUnits(STATE state
, Object item
, DerivedUnitFacade derivedUnitFacade
);
625 * getFacade : get the DerivedUnitFacade based on the recordBasis
628 * @return DerivedUnitFacade
630 protected DerivedUnitFacade
getFacade(STATE state
) {
631 if (logger
.isDebugEnabled()){
632 logger
.info("getFacade()");
634 SpecimenOrObservationType type
= null;
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
;
641 if (state
.getDataHolder().getRecordBasis().toLowerCase().startsWith("o") ||state
.getDataHolder().getRecordBasis().toLowerCase().indexOf("observation")>-1 ) {
642 type
= SpecimenOrObservationType
.Observation
;
644 if (state
.getDataHolder().getRecordBasis().toLowerCase().indexOf("fossil")>-1){
645 type
= SpecimenOrObservationType
.Fossil
;
647 if (state
.getDataHolder().getRecordBasis().toLowerCase().indexOf("living")>-1) {
648 type
= SpecimenOrObservationType
.LivingSpecimen
;
651 logger
.info("The basis of record does not seem to be known: " + state
.getDataHolder().getRecordBasis());
652 type
= SpecimenOrObservationType
.DerivedUnit
;
656 logger
.info("The basis of record is null");
657 type
= SpecimenOrObservationType
.DerivedUnit
;
659 DerivedUnitFacade derivedUnitFacade
= DerivedUnitFacade
.NewInstance(type
);
660 return derivedUnitFacade
;
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)
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){
676 List
<Institution
> institutions
;
678 institutions
= getAgentService().searchInstitutionByCode(institutionCode
);
680 } catch (Exception e
) {
681 institutions
= new ArrayList
<Institution
>();
684 if (institutions
.size() > 0 && config
.isReuseExistingMetaData()) {
685 for (Institution institut
:institutions
){
687 if (institut
.getCode().equalsIgnoreCase(institutionCode
)) {
688 institution
=institut
;
691 }catch(Exception e
){logger
.warn("no institution code in the db");}
694 if (logger
.isDebugEnabled()){
695 if(institution
!=null) {
696 logger
.info("getinstitution " + institution
.toString());
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
);
708 state
.institutions
.put(institutionCode
, institution
);
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)
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){
728 collections
= getCollectionService().searchByCode(collectionCode
);
729 } catch (Exception e
) {
730 collections
= new ArrayList
<Collection
>();
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
)) {
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
);
752 state
.collections
.put(collectionCode
, collection
);
759 * @param citationDetail
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
) {
770 /* List<IdentifiableSource> issTmp = getCommonService().list(IdentifiableSource.class, null, null, null, null);
773 if (reference != null){
775 for (OriginalSourceBase<?> osb: issTmp){
776 if (osb.getCitation() != null && osb.getCitation().getTitleCache().equalsIgnoreCase(reference.getTitleCache())){
777 String osbDetail = osb.getCitationMicroReference();
778 if ((StringUtils.isBlank(osbDetail) && StringUtils.isBlank(citationDetail))
779 || (osbDetail != null && osbDetail.equalsIgnoreCase(citationDetail)) ) {
780 // System.out.println("REFERENCE FOUND RETURN EXISTING SOURCE");
781 return (IdentifiableSource) osb.clone();
785 } catch (CloneNotSupportedException e) {
786 throw new RuntimeException(e);
787 } catch (Exception e1){
788 e1.printStackTrace();
792 IdentifiableSource sour
= IdentifiableSource
.NewInstance(OriginalSourceType
.Import
,null,null, reference
,citationDetail
);
797 * Add the hierarchy for a Taxon(add higher taxa)
798 * @param classification
799 * @param taxon: a taxon to add as a node
800 * @param state: the ABCD import state
802 protected void addParentTaxon(Taxon taxon
, STATE state
, boolean preferredFlag
, Classification classification
){
803 INonViralName nvname
= taxon
.getName();
804 Rank rank
= nvname
.getRank();
806 Taxon subgenus
=null;
807 Taxon species
= null;
808 Taxon subspecies
= null;
811 if (rank
.isLower(Rank
.GENUS() )){
812 String genusOrUninomial
= nvname
.getGenusOrUninomial();
813 TaxonName taxonName
= getOrCreateTaxonName(genusOrUninomial
, Rank
.GENUS(), preferredFlag
, state
, -1);
814 genus
= getOrCreateTaxonForName(taxonName
, state
);
816 logger
.debug("The genus should not be null " + taxonName
);
819 parent
= linkParentChildNode(null, genus
, classification
, state
);
823 if (rank
.isLower(Rank
.SUBGENUS())){
824 String prefix
= nvname
.getGenusOrUninomial();
825 String name
= nvname
.getInfraGenericEpithet();
827 TaxonName taxonName
= getOrCreateTaxonName(prefix
+" "+name
, Rank
.SUBGENUS(), preferredFlag
, state
, -1);
828 subgenus
= getOrCreateTaxonForName(taxonName
, state
);
830 parent
= linkParentChildNode(genus
, subgenus
, classification
, state
);
833 if (rank
.isLower(Rank
.SPECIES())){
835 String prefix
= nvname
.getGenusOrUninomial();
836 String name
= nvname
.getInfraGenericEpithet();
837 String spe
= nvname
.getSpecificEpithet();
839 TaxonName taxonName
= getOrCreateTaxonName(prefix
+" "+name
+" "+spe
, Rank
.SPECIES(), preferredFlag
, state
, -1);
840 species
= getOrCreateTaxonForName(taxonName
, state
);
842 parent
= linkParentChildNode(subgenus
, species
, classification
, state
);
847 String prefix
= nvname
.getGenusOrUninomial();
848 String name
= nvname
.getSpecificEpithet();
850 TaxonName taxonName
= getOrCreateTaxonName(prefix
+" "+name
, Rank
.SPECIES(), preferredFlag
, state
, -1);
851 species
= getOrCreateTaxonForName(taxonName
, state
);
853 parent
= linkParentChildNode(genus
, species
, classification
, state
);
858 if (rank
.isLower(Rank
.INFRASPECIES())){
859 TaxonName taxonName
= getOrCreateTaxonName(nvname
.getFullTitleCache(), Rank
.SUBSPECIES(), preferredFlag
, state
, -1);
860 subspecies
= getOrCreateTaxonForName(taxonName
, state
);
862 parent
= linkParentChildNode(species
, subspecies
, classification
, state
);
866 //handle cf. and aff. taxa
867 String genusEpithet
= null;
868 if (nvname
.getTitleCache().contains("cf.")){
869 genusEpithet
= nvname
.getTitleCache().substring(0, nvname
.getTitleCache().indexOf("cf."));
870 } else if (nvname
.getTitleCache().contains("aff.")){
871 genusEpithet
= nvname
.getTitleCache().substring(0, nvname
.getTitleCache().indexOf("aff."));
873 if (genusEpithet
!= null){
874 genusEpithet
= genusEpithet
.trim();
875 TaxonName taxonName
= null;
876 if (genusEpithet
.contains(" ")){
877 taxonName
= getOrCreateTaxonName(genusEpithet
, Rank
.SPECIES(), preferredFlag
, state
, -1);
879 taxonName
= getOrCreateTaxonName(genusEpithet
, Rank
.GENUS(), preferredFlag
, state
, -1);
881 genus
= getOrCreateTaxonForName(taxonName
, state
);
883 logger
.debug("The genus should not be null " + taxonName
);
886 parent
= linkParentChildNode(null, genus
, classification
, state
);
890 if (preferredFlag
&& parent
!=taxon
) {
891 linkParentChildNode(parent
, taxon
, classification
, state
);
896 * Link a parent to a child and save it in the current classification
897 * @param parent: the higher Taxon
898 * @param child : the lower (or current) Taxon
899 * return the Taxon from the new created Node
900 * @param classification
903 protected Taxon
linkParentChildNode(Taxon parent
, Taxon child
, Classification classification
, STATE state
) {
904 TaxonNode node
=null;
905 if (parent
!= null) {
906 parent
= (Taxon
) getTaxonService().find(parent
.getUuid());
907 child
= (Taxon
) getTaxonService().find(child
.getUuid());
908 //here we do not have to check if the taxon nodes already exists
909 //this is done by classification.addParentChild()
910 //do not add child node if it already exists
911 if(hasTaxonNodeInClassification(child
, classification
)){
915 node
= classification
.addParentChild(parent
, child
, state
.getRef(), "");
921 logger
.debug("The child should not be null!");
923 child
= (Taxon
) getTaxonService().find(child
.getUuid());
924 //do not add child node if it already exists
925 if(hasTaxonNodeInClassification(child
, classification
)){
929 node
= classification
.addChildTaxon(child
, state
.getRef(), null);
934 state
.getReport().addTaxonNode(node
);
935 return node
.getTaxon();
937 String message
= "Could not create taxon node for " +child
;
938 state
.getReport().addInfoMessage(message
);
939 logger
.warn(message
);
943 protected Taxon
getOrCreateTaxonForName(TaxonName taxonName
, STATE state
){
944 if (taxonName
!= null){
945 Set
<Taxon
> acceptedTaxa
= taxonName
.getTaxa();
946 if(acceptedTaxa
.size()>0){
947 Taxon firstAcceptedTaxon
= acceptedTaxa
.iterator().next();
948 if(acceptedTaxa
.size()>1){
949 String message
= "More than one accepted taxon was found for taxon name: "
950 + taxonName
.getTitleCache() + "!\n" + firstAcceptedTaxon
+ "was chosen for "+state
.getDerivedUnitBase();
951 state
.getReport().addInfoMessage(message
);
952 logger
.warn(message
);
955 return firstAcceptedTaxon
;
959 Set
<TaxonBase
> taxonAndSynonyms
= taxonName
.getTaxonBases();
960 for (TaxonBase taxonBase
: taxonAndSynonyms
) {
961 if(taxonBase
.isInstanceOf(Synonym
.class)){
962 Synonym synonym
= HibernateProxyHelper
.deproxy(taxonBase
, Synonym
.class);
963 Taxon acceptedTaxonOfSynonym
= synonym
.getAcceptedTaxon();
964 if(acceptedTaxonOfSynonym
== null){
965 String message
= "No accepted taxon could be found for taxon name: "
966 + taxonName
.getTitleCache()
968 state
.getReport().addInfoMessage(message
);
969 logger
.warn(message
);
972 return acceptedTaxonOfSynonym
;
977 Taxon taxon
= Taxon
.NewInstance(taxonName
, state
.getRef());
979 state
.getReport().addTaxon(taxon
);
980 logger
.info("Created new taxon "+ taxon
);
987 private boolean hasTaxonNodeInClassification(Taxon taxon
, Classification classification
){
988 if(taxon
.getTaxonNodes()!=null){
989 for (TaxonNode node
: taxon
.getTaxonNodes()){
990 if(node
.getClassification().equals(classification
)){
999 * HandleIdentifications : get the scientific names present in the ABCD
1000 * document and store link them with the observation/specimen data
1001 * @param state: the current ABCD import state
1002 * @param derivedUnitFacade : the current derivedunitfacade
1004 protected void handleIdentifications(STATE state
, DerivedUnitFacade derivedUnitFacade
) {
1005 SpecimenImportConfiguratorBase config
= state
.getConfig();
1008 String scientificName
= "";
1009 boolean preferredFlag
= false;
1011 if (state
.getDataHolder().getNomenclatureCode() == ""){
1012 if (config
.getNomenclaturalCode() != null){
1013 if (config
.getNomenclaturalCode() != null){
1014 state
.getDataHolder().setNomenclatureCode(config
.getNomenclaturalCode().toString());
1020 for (int i
= 0; i
< state
.getDataHolder().getIdentificationList().size(); i
++) {
1021 Identification identification
= state
.getDataHolder().getIdentificationList().get(i
);
1022 scientificName
= identification
.getScientificName().replaceAll(" et ", " & ");
1024 String preferred
= identification
.getPreferred();
1025 preferredFlag
= false;
1026 if (preferred
!= null || state
.getDataHolder().getIdentificationList().size()==1){
1027 if (state
.getDataHolder().getIdentificationList().size()==1){
1028 preferredFlag
= true;
1029 }else if (preferred
!= null && (preferred
.equals("1") || preferred
.toLowerCase().indexOf("true") != -1) ) {
1030 preferredFlag
= true;
1034 if (identification
.getCode() != null){
1035 if (identification
.getCode().indexOf(':') != -1) {
1036 state
.getDataHolder().setNomenclatureCode(identification
.getCode().split(COLON
)[1]);
1039 state
.getDataHolder().setNomenclatureCode(identification
.getCode());
1042 TaxonName taxonName
= getOrCreateTaxonName(scientificName
, null, preferredFlag
, state
, i
);
1043 Taxon taxon
= getOrCreateTaxonForName(taxonName
, state
);
1044 addTaxonNode(taxon
, state
,preferredFlag
);
1045 linkDeterminationEvent(state
, taxon
, preferredFlag
, derivedUnitFacade
, identification
.getIdentifier(), identification
.getDate(), identification
.getModifier());
1050 * @param taxon : a taxon to add as a node
1051 * @param state : the ABCD import state
1053 protected void addTaxonNode(Taxon taxon
, STATE state
, boolean preferredFlag
) {
1054 SpecimenImportConfiguratorBase
<?
,?
,?
> config
= state
.getConfig();
1055 logger
.info("link taxon to a taxonNode "+taxon
.getTitleCache());
1056 //only add nodes if not already existing in current classification or default classification
1058 //check if node exists in current classification
1059 //NOTE: we cannot use hasTaxonNodeInClassification() here because we are first creating it here
1060 if (!existsInClassification(taxon
,state
.getClassification(), state
)){
1061 if(config
.isMoveNewTaxaToDefaultClassification()){
1062 //check if node exists in default classification
1063 if (!existsInClassification(taxon
, state
.getDefaultClassification(true), state
)){
1064 addParentTaxon(taxon
, state
, preferredFlag
, state
.getDefaultClassification(true));
1067 //add non-existing taxon to current classification
1068 addParentTaxon(taxon
, state
, preferredFlag
, state
.getClassification());
1075 private boolean existsInClassification(Taxon taxon
, Classification classification
, STATE state
){
1076 boolean exist
= false;
1077 ICdmRepository cdmAppController
= state
.getConfig().getCdmAppController();
1078 if(cdmAppController
==null){
1079 cdmAppController
= this;
1081 if (classification
!= null){
1082 if (!taxon
.getTaxonNodes().isEmpty()){
1083 for (TaxonNode node
:taxon
.getTaxonNodes()){
1084 if (node
.getClassification().equals(classification
)){
1089 // we do not need this because we already searched for taxa in db in the previous steps
1090 // List<UuidAndTitleCache<TaxonNode>> uuidAndTitleCacheOfAllTaxa = cdmAppController.getClassificationService().getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(classification.getUuid());
1091 // if (uuidAndTitleCacheOfAllTaxa != null){
1092 // for (UuidAndTitleCache p : uuidAndTitleCacheOfAllTaxa){
1094 // if(p.getTitleCache().equals(taxon.getTitleCache())) {
1098 // catch(Exception e){
1099 // logger.warn("TaxonNode doesn't seem to have a taxon");
1108 * join DeterminationEvent to the Taxon Object
1109 * @param state : the ABCD import state
1110 * @param taxon: the current Taxon
1111 * @param preferredFlag :if the current name is preferred
1112 * @param derivedFacade : the derived Unit Facade
1114 @SuppressWarnings("rawtypes")
1115 protected void linkDeterminationEvent(STATE state
, Taxon taxon
, boolean preferredFlag
, DerivedUnitFacade derivedFacade
, String identifierStr
, String dateStr
, String modifier
) {
1116 SpecimenImportConfiguratorBase config
= state
.getConfig();
1117 if (logger
.isDebugEnabled()){
1118 logger
.info("start linkdetermination with taxon:" + taxon
.getUuid()+", "+taxon
);
1121 DeterminationEvent determinationEvent
= DeterminationEvent
.NewInstance();
1122 //determinationEvent.setTaxon(taxon);
1123 determinationEvent
.setTaxonName(taxon
.getName());
1124 determinationEvent
.setPreferredFlag(preferredFlag
);
1127 determinationEvent
.setIdentifiedUnit(state
.getDerivedUnitBase());
1128 if (state
.getPersonStore().get(identifierStr
) != null){
1129 determinationEvent
.setActor((AgentBase
)state
.getPersonStore().get(identifierStr
));
1130 } else if (identifierStr
!= null){
1131 Person identifier
= Person
.NewTitledInstance(identifierStr
);
1132 determinationEvent
.setActor(identifier
);
1134 if (dateStr
!= null){
1135 determinationEvent
.setTimeperiod(TimePeriodParser
.parseString(dateStr
));
1137 if (modifier
!= null){
1138 if (modifier
.equals("cf.")){
1139 determinationEvent
.setModifier(DefinedTerm
.DETERMINATION_MODIFIER_CONFER());
1140 }else if (modifier
.equals("aff.")){
1141 determinationEvent
.setModifier(DefinedTerm
.DETERMINATION_MODIFIER_AFFINIS());
1144 state
.getDerivedUnitBase().addDetermination(determinationEvent
);
1146 if (logger
.isDebugEnabled()){
1147 logger
.debug("NB TYPES INFO: "+ state
.getDataHolder().getStatusList().size());
1149 for (SpecimenTypeDesignationStatus specimenTypeDesignationstatus
: state
.getDataHolder().getStatusList()) {
1150 if (specimenTypeDesignationstatus
!= null) {
1151 if (logger
.isDebugEnabled()){
1152 logger
.debug("specimenTypeDesignationstatus :"+ specimenTypeDesignationstatus
);
1155 ICdmRepository cdmAppController
= config
.getCdmAppController();
1156 if(cdmAppController
== null){
1157 cdmAppController
= this;
1159 specimenTypeDesignationstatus
= HibernateProxyHelper
.deproxy(cdmAppController
.getTermService().find(specimenTypeDesignationstatus
.getUuid()), SpecimenTypeDesignationStatus
.class);
1161 TaxonName name
= taxon
.getName();
1162 SpecimenTypeDesignation designation
= SpecimenTypeDesignation
.NewInstance();
1164 designation
.setTypeStatus(specimenTypeDesignationstatus
);
1165 designation
.setTypeSpecimen(state
.getDerivedUnitBase());
1166 name
.addTypeDesignation(designation
, true);
1169 save(state
.getDerivedUnitBase(), state
);
1171 for (String
[] fullReference
: state
.getDataHolder().getReferenceList()) {
1174 String strReference
=fullReference
[0];
1175 String citationDetail
= fullReference
[1];
1176 String citationURL
= fullReference
[2];
1177 List
<Reference
> references
= getReferenceService().listByTitleWithRestrictions(Reference
.class, "strReference", MatchMode
.EXACT
, null, null, null, null, null);
1179 if (!references
.isEmpty()){
1180 Reference reference
= null;
1181 for (Reference refe
: references
) {
1182 if (refe
.getTitleCache().equalsIgnoreCase(strReference
)) {
1187 if (reference
==null){
1188 reference
= ReferenceFactory
.newGeneric();
1189 reference
.setTitleCache(strReference
, true);
1190 save(reference
, state
);
1192 determinationEvent
.addReference(reference
);
1195 save(state
.getDerivedUnitBase(), state
);
1197 if (config
.isAddIndividualsAssociationsSuchAsSpecimenAndObservations() && preferredFlag
) {
1198 //do not add IndividualsAssociation to non-preferred taxa
1199 if (logger
.isDebugEnabled()){
1200 logger
.debug("isDoCreateIndividualsAssociations");
1203 makeIndividualsAssociation(state
, taxon
, determinationEvent
);
1205 save(state
.getDerivedUnitBase(), state
);
1210 * create and link each association (specimen, observation..) to the accepted taxon
1211 * @param state : the ABCD import state
1212 * @param taxon: the current Taxon
1213 * @param determinationEvent:the determinationevent
1215 protected void makeIndividualsAssociation(STATE state
, Taxon taxon
, DeterminationEvent determinationEvent
) {
1216 SpecimenImportConfiguratorBase
<?
,?
,?
> config
= state
.getConfig();
1217 SpecimenUserInteraction sui
= config
.getSpecimenUserInteraction();
1219 if (logger
.isDebugEnabled()){
1220 logger
.info("MAKE INDIVIDUALS ASSOCIATION");
1223 TaxonDescription taxonDescription
= null;
1224 Set
<TaxonDescription
> descriptions
= taxon
.getDescriptions();
1225 if (!descriptions
.isEmpty()){ taxonDescription
= descriptions
.iterator().next();}
1227 // for (TaxonDescription description : descriptions){
1228 // Set<IdentifiableSource> sources = new HashSet<>();
1229 // sources.addAll(description.getTaxon().getSources());
1230 // sources.addAll(description.getSources());
1231 // for (IdentifiableSource source:sources){
1232 // if(state.getRef().equals(source.getCitation())) {
1233 // taxonDescription = description;
1238 if (taxonDescription
== null){
1239 taxonDescription
= TaxonDescription
.NewInstance(taxon
, false);
1240 if(sourceNotLinkedToElement(taxonDescription
,state
.getRef(),null)) {
1241 taxonDescription
.addSource(OriginalSourceType
.Import
, null, null, state
.getRef(), null);
1243 state
.setDescriptionGroup(taxonDescription
);
1244 taxon
.addDescription(taxonDescription
);
1247 //PREPARE REFERENCE QUESTIONS
1249 Map
<String
,OriginalSourceBase
<?
>> sourceMap
= new HashMap
<String
, OriginalSourceBase
<?
>>();
1251 List
<IdentifiableSource
> issTmp
= new ArrayList
<>();//getCommonService().list(IdentifiableSource.class, null, null, null, null);
1252 List
<DescriptionElementSource
> issTmp2
= new ArrayList
<>();//getCommonService().list(DescriptionElementSource.class, null, null, null, null);
1254 Set
<OriginalSourceBase
> osbSet
= new HashSet
<OriginalSourceBase
>();
1256 osbSet
.addAll(issTmp2
);
1259 osbSet
.addAll(issTmp
);
1263 addToSourceMap(sourceMap
, osbSet
);
1265 // if (((Abcd206ImportConfigurator) state.getConfig()).isInteractWithUser()){
1266 // List<OriginalSourceBase<?>> res = null;
1267 // if(!state.isDescriptionSourcesSet()){
1268 // res = sui.askForSource(sourceMap, "the description group ("+taxon+")",
1269 // "The current reference is "+state.getRef().getTitleCache(),getReferenceService(), state.getDataHolder().docSources);
1270 // state.setDescriptionRefs(res);
1271 // state.setDescriptionSourcesSet(true);
1274 // res=state.getDescriptionRefs();
1277 // for (OriginalSourceBase<?> sour:res){
1278 // if(sour.isInstanceOf(IdentifiableSource.class)){
1280 // if(sourceNotLinkedToElement(taxonDescription,sour)) {
1281 // taxonDescription.addSource((IdentifiableSource)sour.clone());
1283 // } catch (CloneNotSupportedException e) {
1284 // logger.warn("no cloning?");
1287 // if(sourceNotLinkedToElement(taxonDescription,sour)) {
1288 // taxonDescription.addSource(OriginalSourceType.Import,null, null, sour.getCitation(),sour.getCitationMicroReference());
1295 if(sourceNotLinkedToElement(taxonDescription
,state
.getRef(),null)) {
1296 taxonDescription
.addSource(OriginalSourceType
.Import
,null, null, state
.getRef(), null);
1299 state
.setDescriptionGroup(taxonDescription
);
1301 IndividualsAssociation indAssociation
= IndividualsAssociation
.NewInstance();
1302 Feature feature
= makeFeature(state
.getDerivedUnitBase());
1303 indAssociation
.setAssociatedSpecimenOrObservation(state
.getDerivedUnitBase());
1304 indAssociation
.setFeature(feature
);
1306 // if (((Abcd206ImportConfigurator) state.getConfig()).isInteractWithUser()){
1307 // sourceMap = new HashMap<String, OriginalSourceBase<?>>();
1309 // issTmp = getCommonService().list(IdentifiableSource.class, null, null, null, null);
1310 // issTmp2 = getCommonService().list(DescriptionElementSource.class, null, null, null, null);
1312 // osbSet = new HashSet<OriginalSourceBase>();
1313 // if(issTmp2!=null) {
1314 // osbSet.addAll(issTmp2);
1316 // if(issTmp!=null) {
1317 // osbSet.addAll(issTmp);
1321 // addToSourceMap(sourceMap, osbSet);
1323 // List<OriginalSourceBase<?>> sources =null;
1324 // if(!state.isAssociationSourcesSet()) {
1325 // sources = sui.askForSource(sourceMap, "descriptive element (association) ",taxon.toString(),
1326 // getReferenceService(),state.getDataHolder().getDocSources());
1327 // state.setAssociationRefs(sources);
1328 // state.setAssociationSourcesSet(true);
1331 // sources=state.getAssociationRefs();
1333 // if(sources !=null) {
1334 // for (OriginalSourceBase<?> source: sources) {
1335 // if(source !=null) {
1336 // if(source.isInstanceOf(DescriptionElementSource.class)){
1338 // if(sourceNotLinkedToElement(indAssociation,source)) {
1339 // indAssociation.addSource((DescriptionElementSource)source.clone());
1341 // } catch (CloneNotSupportedException e) {
1342 // logger.warn("clone forbidden?");
1345 // if(sourceNotLinkedToElement(indAssociation,source)) {
1346 // indAssociation.addSource(OriginalSourceType.Import,null, null, source.getCitation(),source.getCitationMicroReference());
1349 // if(sourceNotLinkedToElement(state.getDerivedUnitBase(), source)) {
1350 // state.getDerivedUnitBase().addSource((IdentifiableSource) source.clone());
1352 // } catch (CloneNotSupportedException e) {
1353 // // TODO Auto-generated catch block
1354 // e.printStackTrace();
1362 if(sourceNotLinkedToElement(indAssociation
,state
.getImportReference(state
.getActualAccessPoint()),null)) {
1363 indAssociation
.addSource(OriginalSourceType
.Import
,null, null, state
.getImportReference(state
.getActualAccessPoint()), null);
1365 if(sourceNotLinkedToElement(state
.getDerivedUnitBase(), state
.getImportReference(state
.getActualAccessPoint()),null)) {
1366 state
.getDerivedUnitBase().addSource(OriginalSourceType
.Import
,null, null, state
.getImportReference(state
.getActualAccessPoint()), null);
1368 for (Reference citation
: determinationEvent
.getReferences()) {
1369 if(sourceNotLinkedToElement(indAssociation
,citation
,null))
1371 indAssociation
.addSource(DescriptionElementSource
.NewInstance(OriginalSourceType
.Import
, null, null, citation
, null));
1373 if(sourceNotLinkedToElement(state
.getDerivedUnitBase(), state
.getImportReference(state
.getActualAccessPoint()),null)) {
1374 state
.getDerivedUnitBase().addSource(OriginalSourceType
.Import
,null, null, state
.getImportReference(state
.getActualAccessPoint()), null);
1379 taxonDescription
.addElement(indAssociation
);
1381 save(taxonDescription
, state
);
1383 state
.getReport().addDerivate(state
.getDerivedUnitBase(), config
);
1384 state
.getReport().addIndividualAssociation(taxon
, state
.getDataHolder().getUnitID(), state
.getDerivedUnitBase());
1388 * @param derivedUnitBase2
1393 private boolean sourceNotLinkedToElement(DerivedUnit derivedUnitBase2
, Reference b
, String d
) {
1394 Set
<IdentifiableSource
> linkedSources
= derivedUnitBase2
.getSources();
1395 for (IdentifiableSource is
:linkedSources
){
1396 Reference a
= is
.getCitation();
1397 String c
= is
.getCitationMicroReference();
1399 boolean refMatch
=false;
1400 boolean microMatch
=false;
1403 if (a
==null && b
==null) {
1406 if (a
!=null && b
!=null) {
1407 if (a
.getTitleCache().equalsIgnoreCase(b
.getTitleCache())) {
1411 }catch(Exception e
){}
1415 if (c
==null && d
==null) {
1418 if(c
!=null && d
!=null) {
1419 if(c
.equalsIgnoreCase(d
)) {
1424 catch(Exception e
){}
1426 if (microMatch
&& refMatch
) {
1435 private <T
extends OriginalSourceBase
<?
>> boolean sourceNotLinkedToElement(ISourceable
<T
> sourcable
, Reference reference
, String microReference
) {
1436 Set
<T
> linkedSources
= sourcable
.getSources();
1437 for (T is
:linkedSources
){
1438 Reference unitReference
= is
.getCitation();
1439 String unitMicroReference
= is
.getCitationMicroReference();
1441 boolean refMatch
=false;
1442 boolean microMatch
=false;
1445 if (unitReference
==null && reference
==null) {
1448 if (unitReference
!=null && reference
!=null) {
1449 if (unitReference
.getTitleCache().equalsIgnoreCase(reference
.getTitleCache())) {
1453 }catch(Exception e
){}
1456 if (unitMicroReference
==null && microReference
==null) {
1459 if(unitMicroReference
!=null && microReference
!=null) {
1460 if(unitMicroReference
.equalsIgnoreCase(microReference
)) {
1465 catch(Exception e
){}
1467 if (microMatch
&& refMatch
) {
1475 * look for the Feature object (FieldObs, Specimen,...)
1476 * @param unit : a specimen or obersvation base
1477 * @return the corresponding Feature
1479 private Feature
makeFeature(SpecimenOrObservationBase
<?
> unit
) {
1480 SpecimenOrObservationType type
= unit
.getRecordBasis();
1484 if (type
.isFeatureObservation()){
1485 return Feature
.OBSERVATION();
1486 }else if (type
.isFeatureSpecimen()){
1487 return Feature
.SPECIMEN();
1488 }else if (type
== SpecimenOrObservationType
.DerivedUnit
){
1489 return Feature
.OBSERVATION();
1490 // return getFeature("Specimen or observation");
1492 String message
= "Unhandled record basis '%s' for defining individuals association feature type. Use default.";
1493 logger
.warn(String
.format(message
, type
.getMessage()));
1494 return Feature
.OBSERVATION();
1495 // return getFeature("Specimen or observation");
1505 protected void addToSourceMap(Map
<String
, OriginalSourceBase
<?
>> sourceMap
, Set
<OriginalSourceBase
> osbSet
) {
1506 for( OriginalSourceBase
<?
> osb
:osbSet
) {
1507 if(osb
.getCitation()!=null && osb
.getCitationMicroReference() !=null && !osb
.getCitationMicroReference().isEmpty()) {
1509 sourceMap
.put(osb
.getCitation().getTitleCache()+ "---"+osb
.getCitationMicroReference(),osb
);
1510 }catch(NullPointerException e
){logger
.warn("null pointer problem (no ref?) with "+osb
);}
1511 } else if(osb
.getCitation()!=null){
1513 sourceMap
.put(osb
.getCitation().getTitleCache(),osb
);
1514 }catch(NullPointerException e
){logger
.warn("null pointer problem (no ref?) with "+osb
);}