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
.ICdmApplicationConfiguration
;
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
.Team
;
35 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
36 import eu
.etaxonomy
.cdm
.model
.common
.ISourceable
;
37 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableSource
;
38 import eu
.etaxonomy
.cdm
.model
.common
.LanguageString
;
39 import eu
.etaxonomy
.cdm
.model
.common
.OriginalSourceBase
;
40 import eu
.etaxonomy
.cdm
.model
.common
.OriginalSourceType
;
41 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
42 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementSource
;
43 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
44 import eu
.etaxonomy
.cdm
.model
.description
.IndividualsAssociation
;
45 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
46 import eu
.etaxonomy
.cdm
.model
.name
.BacterialName
;
47 import eu
.etaxonomy
.cdm
.model
.name
.BotanicalName
;
48 import eu
.etaxonomy
.cdm
.model
.name
.CultivarPlantName
;
49 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCode
;
50 import eu
.etaxonomy
.cdm
.model
.name
.NonViralName
;
51 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
52 import eu
.etaxonomy
.cdm
.model
.name
.SpecimenTypeDesignation
;
53 import eu
.etaxonomy
.cdm
.model
.name
.SpecimenTypeDesignationStatus
;
54 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
55 import eu
.etaxonomy
.cdm
.model
.name
.ZoologicalName
;
56 import eu
.etaxonomy
.cdm
.model
.occurrence
.Collection
;
57 import eu
.etaxonomy
.cdm
.model
.occurrence
.DerivedUnit
;
58 import eu
.etaxonomy
.cdm
.model
.occurrence
.DeterminationEvent
;
59 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
60 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationType
;
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
.persistence
.dto
.UuidAndTitleCache
;
69 import eu
.etaxonomy
.cdm
.persistence
.query
.MatchMode
;
70 import eu
.etaxonomy
.cdm
.strategy
.parser
.NonViralNameParserImpl
;
71 import eu
.etaxonomy
.cdm
.strategy
.parser
.ParserProblem
;
78 public abstract class SpecimenImportBase
<CONFIG
extends IImportConfigurator
, STATE
extends SpecimenImportStateBase
> extends CdmImportBase
<CONFIG
, STATE
> {
80 private static final long serialVersionUID
= 4423065367998125678L;
81 private static final Logger logger
= Logger
.getLogger(SpecimenImportBase
.class);
82 protected final boolean DEBUG
= true;
84 protected static final UUID SPECIMEN_SCAN_TERM
= UUID
.fromString("acda15be-c0e2-4ea8-8783-b9b0c4ad7f03");
86 private static final String COLON
= ":";
90 protected abstract void doInvoke(STATE state
);
93 * Handle a single unit
97 protected abstract void handleSingleUnit(STATE state
, Object item
) ;
101 protected TaxonNameBase
<?
, ?
> getOrCreateTaxonName(String scientificName
, Rank rank
, boolean preferredFlag
, STATE state
, int unitIndexInAbcdFile
){
102 TaxonNameBase
<?
, ?
> taxonName
= null;
103 SpecimenImportConfiguratorBase config
= state
.getConfig();
105 //check atomised name data for rank
106 //new name will be created
107 NonViralName
<?
> atomisedTaxonName
= null;
108 if (rank
==null && unitIndexInAbcdFile
>=0 && ((state
.getDataHolder().getAtomisedIdentificationList() != null && !state
.getDataHolder().getAtomisedIdentificationList().isEmpty())|| state
.getDataHolder().getAtomisedIdentificationList().size() > 0)) {
109 atomisedTaxonName
= setTaxonNameByType(state
.getDataHolder().getAtomisedIdentificationList().get(unitIndexInAbcdFile
), scientificName
, state
);
110 if(atomisedTaxonName
!=null){
111 rank
= atomisedTaxonName
.getRank();
114 if(config
.isReuseExistingTaxaWhenPossible()){
115 NonViralName
<?
> parsedName
= atomisedTaxonName
;
116 if(parsedName
==null){
117 parsedName
= parseScientificName(scientificName
, state
, state
.getReport());
119 atomisedTaxonName
= parsedName
;
120 if(config
.isIgnoreAuthorship() && parsedName
!=null && preferredFlag
){
121 // do not ignore authorship for non-preferred names because they need
122 // to be created for the determination history
123 String nameCache
= parsedName
.getNameCache();
124 List
<NonViralName
> names
= getNameService().findNamesByNameCache(nameCache
, MatchMode
.EXACT
, null);
125 if (!names
.isEmpty()){
126 return getBestMatchingName(scientificName
, new ArrayList
<TaxonNameBase
>(names
), state
);
129 //search for existing names
130 List
<TaxonNameBase
> names
= getNameService().listByTitle(TaxonNameBase
.class, scientificName
, MatchMode
.EXACT
, null, null, null, null, null);
131 taxonName
= getBestMatchingName(scientificName
, names
, state
);
132 //still nothing found -> try with the atomised name full title cache
133 if(taxonName
==null && atomisedTaxonName
!=null){
134 names
= getNameService().listByTitle(TaxonNameBase
.class, atomisedTaxonName
.getFullTitleCache(), MatchMode
.EXACT
, null, null, null, null, null);
135 taxonName
= getBestMatchingName(atomisedTaxonName
.getTitleCache(), names
, state
);
136 //still nothing found -> try with the atomised name title cache
138 names
= getNameService().listByTitle(TaxonNameBase
.class, atomisedTaxonName
.getTitleCache(), MatchMode
.EXACT
, null, null, null, null, null);
139 taxonName
= getBestMatchingName(atomisedTaxonName
.getTitleCache(), names
, state
);
147 if(taxonName
==null && atomisedTaxonName
!=null){
148 taxonName
= atomisedTaxonName
;
149 state
.getReport().addName(taxonName
);
150 logger
.info("Created new taxon name "+taxonName
);
151 if(taxonName
.hasProblem()){
152 state
.getReport().addInfoMessage(String
.format("Created %s with parsing problems", taxonName
));
154 if(!atomisedTaxonName
.getTitleCache().equals(scientificName
)){
155 state
.getReport().addInfoMessage(String
.format("Taxon %s was parsed as %s", scientificName
, atomisedTaxonName
.getTitleCache()));
158 else if(taxonName
==null){
159 //create new taxon name
161 if (state
.getDataHolder().getNomenclatureCode().equals(NomenclaturalCode
.ICNAFP
)){
162 taxonName
= BotanicalName
.NewInstance(rank
);
163 }else if (state
.getDataHolder().getNomenclatureCode().equals(NomenclaturalCode
.ICZN
)){
164 taxonName
= ZoologicalName
.NewInstance(rank
);
166 taxonName
= NonViralName
.NewInstance(rank
);
168 taxonName
.setFullTitleCache(scientificName
,true);
169 taxonName
.setTitleCache(scientificName
, true);
170 state
.getReport().addName(taxonName
);
171 logger
.info("Created new taxon name "+taxonName
);
173 save(taxonName
, state
);
177 protected TaxonNameBase
<?
, ?
> getBestMatchingName(String scientificName
, java
.util
.Collection
<TaxonNameBase
> names
, STATE state
){
178 List
<TaxonNameBase
> namesWithAcceptedTaxa
= new ArrayList
<TaxonNameBase
>();
179 for (TaxonNameBase name
: names
) {
180 if(!name
.getTaxa().isEmpty()){
181 namesWithAcceptedTaxa
.add(name
);
184 String message
= "More than one taxon name was found for "+scientificName
+"!";
185 //check for names with accepted taxa
186 if(namesWithAcceptedTaxa
.size()>0){
187 if(namesWithAcceptedTaxa
.size()>1){
188 state
.getReport().addInfoMessage(message
);
189 logger
.warn(message
);
192 return namesWithAcceptedTaxa
.iterator().next();
194 //no names with accepted taxa found -> check accepted taxa of synonyms
195 List
<Taxon
> taxaFromSynonyms
= new ArrayList
<Taxon
>();
196 for (TaxonNameBase name
: names
) {
197 Set
<TaxonBase
> taxonBases
= name
.getTaxonBases();
198 for (TaxonBase taxonBase
: taxonBases
) {
199 if(taxonBase
.isInstanceOf(Synonym
.class)){
200 Synonym synonym
= HibernateProxyHelper
.deproxy(taxonBase
, Synonym
.class);
201 taxaFromSynonyms
.add(synonym
.getAcceptedTaxon());
205 if(taxaFromSynonyms
.size()>0){
206 if(taxaFromSynonyms
.size()>1){
207 state
.getReport().addInfoMessage(message
);
208 logger
.warn(message
);
211 return taxaFromSynonyms
.iterator().next().getName();
216 * Parse automatically the scientific name
217 * @param scientificName the scientific name to parse
218 * @param state the current import state
219 * @param report the import report
220 * @return a parsed name
222 protected NonViralName
<?
> parseScientificName(String scientificName
, STATE state
, SpecimenImportReport report
) {
223 NonViralNameParserImpl nvnpi
= NonViralNameParserImpl
.NewInstance();
224 NonViralName
<?
> taxonName
= null;
225 boolean problem
= false;
228 logger
.info("parseScientificName " + state
.getDataHolder().getNomenclatureCode().toString());
231 if (state
.getDataHolder().getNomenclatureCode().toString().equals("Zoological") || state
.getDataHolder().getNomenclatureCode().toString().contains("ICZN")) {
232 taxonName
= nvnpi
.parseFullName(scientificName
, NomenclaturalCode
.ICZN
, null);
233 if (taxonName
.hasProblem()) {
237 else if (state
.getDataHolder().getNomenclatureCode().toString().equals("Botanical") || state
.getDataHolder().getNomenclatureCode().toString().contains("ICBN")) {
238 taxonName
= nvnpi
.parseFullName(scientificName
, NomenclaturalCode
.ICNAFP
, null);
239 if (taxonName
.hasProblem()) {
243 else if (state
.getDataHolder().getNomenclatureCode().toString().equals("Bacterial") || state
.getDataHolder().getNomenclatureCode().toString().contains("ICBN")) {
244 taxonName
= nvnpi
.parseFullName(scientificName
, NomenclaturalCode
.ICNB
, null);
245 if (taxonName
.hasProblem()) {
249 else if (state
.getDataHolder().getNomenclatureCode().toString().equals("Cultivar") || state
.getDataHolder().getNomenclatureCode().toString().contains("ICNCP")) {
250 taxonName
= nvnpi
.parseFullName(scientificName
, NomenclaturalCode
.ICNCP
, null);
251 if (taxonName
.hasProblem()) {
256 String message
= String
.format("Parsing problems for %s", scientificName
);
258 for (ParserProblem parserProblem
: taxonName
.getParsingProblems()) {
259 message
+= "\n\t- "+parserProblem
;
262 report
.addInfoMessage(message
);
263 logger
.info(message
);
270 * Create the name without automatic parsing, either because it failed, or because the user deactivated it.
271 * The name is built upon the ABCD fields
272 * @param atomisedMap : the ABCD atomised fields
273 * @param fullName : the full scientific name
275 * @return the corresponding Botanical or Zoological or... name
277 protected NonViralName
<?
> setTaxonNameByType(
278 HashMap
<String
, String
> atomisedMap
, String fullName
, STATE state
) {
279 boolean problem
= false;
281 logger
.info("settaxonnamebytype " + state
.getDataHolder().getNomenclatureCode().toString());
284 if (state
.getDataHolder().getNomenclatureCode().equals("Zoological") || state
.getDataHolder().getNomenclatureCode().equals(NomenclaturalCode
.ICZN
.getUuid())) {
285 NonViralName
<ZoologicalName
> taxonName
= ZoologicalName
.NewInstance(null);
286 taxonName
.setFullTitleCache(fullName
, true);
287 taxonName
.setGenusOrUninomial(NB(getFromMap(atomisedMap
, "Genus")));
288 taxonName
.setInfraGenericEpithet(NB(getFromMap(atomisedMap
, "SubGenus")));
289 taxonName
.setSpecificEpithet(NB(getFromMap(atomisedMap
,"SpeciesEpithet")));
290 taxonName
.setInfraSpecificEpithet(NB(getFromMap(atomisedMap
,"SubspeciesEpithet")));
292 if (taxonName
.getGenusOrUninomial() != null){
293 taxonName
.setRank(Rank
.GENUS());
296 if (taxonName
.getInfraGenericEpithet() != null){
297 taxonName
.setRank(Rank
.SUBGENUS());
300 if (taxonName
.getSpecificEpithet() != null){
301 taxonName
.setRank(Rank
.SPECIES());
304 if (taxonName
.getInfraSpecificEpithet() != null){
305 taxonName
.setRank(Rank
.SUBSPECIES());
309 if (getFromMap(atomisedMap
, "AuthorTeamParenthesis") != null) {
310 team
= Team
.NewInstance();
311 team
.setTitleCache(getFromMap(atomisedMap
, "AuthorTeamParenthesis"), true);
314 if (getFromMap(atomisedMap
, "AuthorTeamAndYear") != null) {
315 team
= Team
.NewInstance();
316 team
.setTitleCache(getFromMap(atomisedMap
, "AuthorTeamAndYear"), true);
320 taxonName
.setBasionymAuthorship(team
);
323 if (getFromMap(atomisedMap
, "AuthorTeamParenthesis") != null) {
324 taxonName
.setAuthorshipCache(getFromMap(atomisedMap
, "AuthorTeamParenthesis"));
326 else if (getFromMap(atomisedMap
, "AuthorTeamAndYear") != null) {
327 taxonName
.setAuthorshipCache(getFromMap(atomisedMap
, "AuthorTeamAndYear"));
330 if (getFromMap(atomisedMap
, "CombinationAuthorTeamAndYear") != null) {
331 team
= Team
.NewInstance();
332 team
.setTitleCache(getFromMap(atomisedMap
, "CombinationAuthorTeamAndYear"), true);
333 taxonName
.setCombinationAuthorship(team
);
335 if (taxonName
.hasProblem()) {
336 logger
.info("pb ICZN");
343 else if (state
.getDataHolder().getNomenclatureCode().equals("Botanical") || state
.getDataHolder().getNomenclatureCode().equals(NomenclaturalCode
.ICNAFP
.getUuid())) {
344 BotanicalName taxonName
= (BotanicalName
) parseScientificName(fullName
, state
, state
.getReport());
345 if (taxonName
!= null){
349 taxonName
= BotanicalName
.NewInstance(null);
351 taxonName
.setFullTitleCache(fullName
, true);
352 taxonName
.setGenusOrUninomial(NB(getFromMap(atomisedMap
, "Genus")));
353 taxonName
.setSpecificEpithet(NB(getFromMap(atomisedMap
, "FirstEpithet")));
354 taxonName
.setInfraSpecificEpithet(NB(getFromMap(atomisedMap
, "InfraSpeEpithet")));
356 taxonName
.setRank(Rank
.getRankByName(getFromMap(atomisedMap
, "Rank")));
357 } catch (Exception e
) {
358 if (taxonName
.getInfraSpecificEpithet() != null){
359 taxonName
.setRank(Rank
.SUBSPECIES());
361 else if (taxonName
.getSpecificEpithet() != null){
362 taxonName
.setRank(Rank
.SPECIES());
364 else if (taxonName
.getInfraGenericEpithet() != null){
365 taxonName
.setRank(Rank
.SUBGENUS());
367 else if (taxonName
.getGenusOrUninomial() != null){
368 taxonName
.setRank(Rank
.GENUS());
372 if (getFromMap(atomisedMap
, "AuthorTeamParenthesis") != null) {
373 team
= Team
.NewInstance();
374 team
.setTitleCache(getFromMap(atomisedMap
, "AuthorTeamParenthesis"), true);
375 taxonName
.setBasionymAuthorship(team
);
377 if (getFromMap(atomisedMap
, "AuthorTeam") != null) {
378 team
= Team
.NewInstance();
379 team
.setTitleCache(getFromMap(atomisedMap
, "AuthorTeam"), true);
380 taxonName
.setCombinationAuthorship(team
);
383 if (getFromMap(atomisedMap
, "AuthorTeamParenthesis") != null) {
384 taxonName
.setAuthorshipCache(getFromMap(atomisedMap
, "AuthorTeamParenthesis"));
386 else if (getFromMap(atomisedMap
, "AuthorTeam") != null) {
387 taxonName
.setAuthorshipCache(getFromMap(atomisedMap
, "AuthorTeam"));
390 if (getFromMap(atomisedMap
, "CombinationAuthorTeamAndYear") != null) {
391 team
= Team
.NewInstance();
392 team
.setTitleCache(getFromMap(atomisedMap
, "CombinationAuthorTeamAndYear"), true);
393 taxonName
.setCombinationAuthorship(team
);
395 if (taxonName
.hasProblem()) {
396 logger
.info("pb ICBN");
403 else if (state
.getDataHolder().getNomenclatureCode().equals("Bacterial") || state
.getDataHolder().getNomenclatureCode().equals(NomenclaturalCode
.ICNB
.getUuid())) {
404 NonViralName
<BacterialName
> taxonName
= BacterialName
.NewInstance(null);
405 taxonName
.setFullTitleCache(fullName
, true);
406 taxonName
.setGenusOrUninomial(getFromMap(atomisedMap
, "Genus"));
407 taxonName
.setInfraGenericEpithet(NB(getFromMap(atomisedMap
, "SubGenus")));
408 taxonName
.setSpecificEpithet(NB(getFromMap(atomisedMap
, "Species")));
409 taxonName
.setInfraSpecificEpithet(NB(getFromMap(atomisedMap
, "SubspeciesEpithet")));
411 if (taxonName
.getGenusOrUninomial() != null){
412 taxonName
.setRank(Rank
.GENUS());
414 else if (taxonName
.getInfraGenericEpithet() != null){
415 taxonName
.setRank(Rank
.SUBGENUS());
417 else if (taxonName
.getSpecificEpithet() != null){
418 taxonName
.setRank(Rank
.SPECIES());
420 else if (taxonName
.getInfraSpecificEpithet() != null){
421 taxonName
.setRank(Rank
.SUBSPECIES());
424 if (getFromMap(atomisedMap
, "AuthorTeamAndYear") != null) {
425 Team team
= Team
.NewInstance();
426 team
.setTitleCache(getFromMap(atomisedMap
, "AuthorTeamAndYear"), true);
427 taxonName
.setCombinationAuthorship(team
);
429 if (getFromMap(atomisedMap
, "ParentheticalAuthorTeamAndYear") != null) {
430 Team team
= Team
.NewInstance();
431 team
.setTitleCache(getFromMap(atomisedMap
, "ParentheticalAuthorTeamAndYear"), true);
432 taxonName
.setBasionymAuthorship(team
);
434 if (taxonName
.hasProblem()) {
435 logger
.info("pb ICNB");
442 else if (state
.getDataHolder().getNomenclatureCode().equals("Cultivar")) {
443 CultivarPlantName taxonName
= CultivarPlantName
.NewInstance(null);
445 if (taxonName
.hasProblem()) {
446 logger
.info("pb ICNCP");
456 logger
.info("Problem im setTaxonNameByType ");
457 NonViralName
<?
> taxonName
= NonViralName
.NewInstance(null);
458 taxonName
.setFullTitleCache(fullName
, true);
461 NonViralName
<?
> tn
= NonViralName
.NewInstance(null);
466 * Get a formated string from a hashmap
471 private String
getFromMap(HashMap
<String
, String
> atomisedMap
, String key
) {
473 if (atomisedMap
.containsKey(key
)) {
474 value
= atomisedMap
.get(key
);
478 if (value
!= null && key
.matches(".*Year.*")) {
479 value
= value
.trim();
480 if (value
.matches("[a-z A-Z ]*[0-9]{4}$")) {
481 String tmp
= value
.split("[0-9]{4}$")[0];
482 int year
= Integer
.parseInt(value
.split(tmp
)[1]);
495 catch (Exception e
) {
502 * Very fast and dirty implementation to allow handling of transient objects as described in
503 * https://dev.e-taxonomy.eu/trac/ticket/3726
510 protected UUID
save(CdmBase cdmBase
, SpecimenImportStateBase state
) {
511 ICdmApplicationConfiguration cdmRepository
= state
.getConfig().getCdmAppController();
512 if (cdmRepository
== null){
513 cdmRepository
= this;
516 if (cdmBase
.isInstanceOf(LanguageString
.class)){
517 return cdmRepository
.getTermService().saveLanguageData(CdmBase
.deproxy(cdmBase
, LanguageString
.class));
518 }else if (cdmBase
.isInstanceOf(SpecimenOrObservationBase
.class)){
519 return cdmRepository
.getOccurrenceService().saveOrUpdate(CdmBase
.deproxy(cdmBase
, SpecimenOrObservationBase
.class));
520 }else if (cdmBase
.isInstanceOf(Reference
.class)){
521 return cdmRepository
.getReferenceService().saveOrUpdate(CdmBase
.deproxy(cdmBase
, Reference
.class));
522 }else if (cdmBase
.isInstanceOf(Classification
.class)){
523 return cdmRepository
.getClassificationService().saveOrUpdate(CdmBase
.deproxy(cdmBase
, Classification
.class));
524 }else if (cdmBase
.isInstanceOf(AgentBase
.class)){
525 return cdmRepository
.getAgentService().saveOrUpdate(CdmBase
.deproxy(cdmBase
, AgentBase
.class));
526 }else if (cdmBase
.isInstanceOf(Collection
.class)){
527 return cdmRepository
.getCollectionService().saveOrUpdate(CdmBase
.deproxy(cdmBase
, Collection
.class));
528 }else if (cdmBase
.isInstanceOf(DescriptionBase
.class)){
529 return cdmRepository
.getDescriptionService().saveOrUpdate(CdmBase
.deproxy(cdmBase
, DescriptionBase
.class));
530 }else if (cdmBase
.isInstanceOf(TaxonBase
.class)){
531 return cdmRepository
.getTaxonService().saveOrUpdate(CdmBase
.deproxy(cdmBase
, TaxonBase
.class));
532 }else if (cdmBase
.isInstanceOf(TaxonNameBase
.class)){
533 return cdmRepository
.getNameService().saveOrUpdate(CdmBase
.deproxy(cdmBase
, TaxonNameBase
.class));
534 }else if (cdmBase
.isInstanceOf(TaxonNode
.class)){
535 return cdmRepository
.getTaxonNodeService().saveOrUpdate(CdmBase
.deproxy(cdmBase
, TaxonNode
.class));
537 throw new IllegalArgumentException("Class not supported in save method: " + CdmBase
.deproxy(cdmBase
, CdmBase
.class).getClass().getSimpleName());
543 protected SpecimenOrObservationBase
findExistingSpecimen(String unitId
, SpecimenImportStateBase state
){
544 ICdmApplicationConfiguration cdmAppController
= state
.getConfig().getCdmAppController();
545 if(cdmAppController
==null){
546 cdmAppController
= this;
548 FindOccurrencesConfigurator config
= new FindOccurrencesConfigurator();
549 config
.setSignificantIdentifier(unitId
);
550 Pager
<SpecimenOrObservationBase
> existingSpecimens
= cdmAppController
.getOccurrenceService().findByTitle(config
);
551 if(!existingSpecimens
.getRecords().isEmpty()){
552 if(existingSpecimens
.getRecords().size()==1){
553 return existingSpecimens
.getRecords().iterator().next();
559 protected abstract void importAssociatedUnits(STATE state
, Object item
, DerivedUnitFacade derivedUnitFacade
);
562 * getFacade : get the DerivedUnitFacade based on the recordBasis
565 * @return DerivedUnitFacade
567 protected DerivedUnitFacade
getFacade(STATE state
) {
569 logger
.info("getFacade()");
571 SpecimenOrObservationType type
= null;
574 if (NB((state
.getDataHolder().getRecordBasis())) != null) {
575 if (state
.getDataHolder().getRecordBasis().toLowerCase().startsWith("s") || state
.getDataHolder().getRecordBasis().toLowerCase().indexOf("specimen")>-1) {// specimen
576 type
= SpecimenOrObservationType
.PreservedSpecimen
;
578 if (state
.getDataHolder().getRecordBasis().toLowerCase().startsWith("o") ||state
.getDataHolder().getRecordBasis().toLowerCase().indexOf("observation")>-1 ) {
579 type
= SpecimenOrObservationType
.Observation
;
581 if (state
.getDataHolder().getRecordBasis().toLowerCase().indexOf("fossil")>-1){
582 type
= SpecimenOrObservationType
.Fossil
;
584 if (state
.getDataHolder().getRecordBasis().toLowerCase().indexOf("living")>-1) {
585 type
= SpecimenOrObservationType
.LivingSpecimen
;
588 logger
.info("The basis of record does not seem to be known: " + state
.getDataHolder().getRecordBasis());
589 type
= SpecimenOrObservationType
.DerivedUnit
;
593 logger
.info("The basis of record is null");
594 type
= SpecimenOrObservationType
.DerivedUnit
;
596 DerivedUnitFacade derivedUnitFacade
= DerivedUnitFacade
.NewInstance(type
);
597 return derivedUnitFacade
;
601 * Look if the Institution does already exist
602 * @param institutionCode: a string with the institutioncode
603 * @param config : the configurator
604 * @return the Institution (existing or new)
606 protected Institution
getInstitution(String institutionCode
, STATE state
) {
607 SpecimenImportConfiguratorBase config
= state
.getConfig();
608 Institution institution
=null;
609 institution
= (Institution
)state
.institutions
.get(institutionCode
);
610 if (institution
!= null){
613 List
<Institution
> institutions
;
615 institutions
= getAgentService().searchInstitutionByCode(institutionCode
);
617 } catch (Exception e
) {
618 institutions
= new ArrayList
<Institution
>();
621 if (institutions
.size() > 0 && config
.isReuseExistingMetaData()) {
622 for (Institution institut
:institutions
){
624 if (institut
.getCode().equalsIgnoreCase(institutionCode
)) {
625 institution
=institut
;
628 }catch(Exception e
){logger
.warn("no institution code in the db");}
632 if(institution
!=null) {
633 logger
.info("getinstitution " + institution
.toString());
636 if (institution
== null){
637 // create institution
638 institution
= Institution
.NewInstance();
639 institution
.setCode(institutionCode
);
640 institution
.setTitleCache(institutionCode
, true);
641 UUID uuid
= save(institution
, state
);
645 state
.institutions
.put(institutionCode
, institution
);
650 * Look if the Collection does already exist
651 * @param collectionCode
652 * @param collectionCode: a string
653 * @param config : the configurator
654 * @return the Collection (existing or new)
656 protected Collection
getCollection(Institution institution
, String collectionCode
, STATE state
) {
657 SpecimenImportConfiguratorBase config
= state
.getConfig();
658 Collection collection
= null;
659 List
<Collection
> collections
;
660 collection
= (Collection
) state
.collections
.get(collectionCode
);
661 if (collection
!= null){
665 collections
= getCollectionService().searchByCode(collectionCode
);
666 } catch (Exception e
) {
667 collections
= new ArrayList
<Collection
>();
669 if (collections
.size() > 0 && config
.isReuseExistingMetaData()) {
670 for (Collection coll
:collections
){
671 if (coll
.getCode() != null && coll
.getInstitute() != null
672 && coll
.getCode().equalsIgnoreCase(collectionCode
) && coll
.getInstitute().equals(institution
)) {
679 if(collection
== null){
680 collection
=Collection
.NewInstance();
681 collection
.setCode(collectionCode
);
682 collection
.setInstitute(institution
);
683 collection
.setTitleCache(collectionCode
);
684 UUID uuid
= save(collection
, state
);
689 state
.collections
.put(collectionCode
, collection
);
696 * @param citationDetail
699 //FIXME this method is highly critical, because
700 // * it will have serious performance and memory problems with large databases
701 // (databases may easily have >1 Mio source records)
702 // * it does not make sense to search for existing sources and then clone them
703 // we need to search for existing references instead and use them (if exist)
704 // for our new source.
705 protected IdentifiableSource
getIdentifiableSource(Reference reference
, String citationDetail
) {
707 /* List<IdentifiableSource> issTmp = getCommonService().list(IdentifiableSource.class, null, null, null, null);
710 if (reference != null){
712 for (OriginalSourceBase<?> osb: issTmp){
713 if (osb.getCitation() != null && osb.getCitation().getTitleCache().equalsIgnoreCase(reference.getTitleCache())){
714 String osbDetail = osb.getCitationMicroReference();
715 if ((StringUtils.isBlank(osbDetail) && StringUtils.isBlank(citationDetail))
716 || (osbDetail != null && osbDetail.equalsIgnoreCase(citationDetail)) ) {
717 // System.out.println("REFERENCE FOUND RETURN EXISTING SOURCE");
718 return (IdentifiableSource) osb.clone();
722 } catch (CloneNotSupportedException e) {
723 throw new RuntimeException(e);
724 } catch (Exception e1){
725 e1.printStackTrace();
729 IdentifiableSource sour
= IdentifiableSource
.NewInstance(OriginalSourceType
.Import
,null,null, reference
,citationDetail
);
734 * Add the hierarchy for a Taxon(add higher taxa)
735 * @param classification
736 * @param taxon: a taxon to add as a node
737 * @param state: the ABCD import state
739 protected void addParentTaxon(Taxon taxon
, STATE state
, boolean preferredFlag
, Classification classification
){
740 NonViralName
<?
> nvname
= CdmBase
.deproxy(taxon
.getName(), NonViralName
.class);
741 Rank rank
= nvname
.getRank();
743 Taxon subgenus
=null;
744 Taxon species
= null;
745 Taxon subspecies
= null;
748 if (rank
.isLower(Rank
.GENUS() )){
749 String genusOrUninomial
= nvname
.getGenusOrUninomial();
750 TaxonNameBase
<?
,?
> taxonName
= getOrCreateTaxonName(genusOrUninomial
, Rank
.GENUS(), preferredFlag
, state
, -1);
751 genus
= getOrCreateTaxonForName(taxonName
, state
);
753 parent
= linkParentChildNode(null, genus
, classification
, state
);
757 if (rank
.isLower(Rank
.SUBGENUS())){
758 String prefix
= nvname
.getGenusOrUninomial();
759 String name
= nvname
.getInfraGenericEpithet();
761 TaxonNameBase
<?
,?
> taxonName
= getOrCreateTaxonName(prefix
+" "+name
, Rank
.SUBGENUS(), preferredFlag
, state
, -1);
762 subgenus
= getOrCreateTaxonForName(taxonName
, state
);
764 parent
= linkParentChildNode(genus
, subgenus
, classification
, state
);
767 if (rank
.isLower(Rank
.SPECIES())){
769 String prefix
= nvname
.getGenusOrUninomial();
770 String name
= nvname
.getInfraGenericEpithet();
771 String spe
= nvname
.getSpecificEpithet();
773 TaxonNameBase
<?
,?
> taxonName
= getOrCreateTaxonName(prefix
+" "+name
+" "+spe
, Rank
.SPECIES(), preferredFlag
, state
, -1);
774 species
= getOrCreateTaxonForName(taxonName
, state
);
776 parent
= linkParentChildNode(subgenus
, species
, classification
, state
);
781 String prefix
= nvname
.getGenusOrUninomial();
782 String name
= nvname
.getSpecificEpithet();
784 TaxonNameBase
<?
,?
> taxonName
= getOrCreateTaxonName(prefix
+" "+name
, Rank
.SPECIES(), preferredFlag
, state
, -1);
785 species
= getOrCreateTaxonForName(taxonName
, state
);
787 parent
= linkParentChildNode(genus
, species
, classification
, state
);
792 if (rank
.isLower(Rank
.INFRASPECIES())){
793 TaxonNameBase
<?
,?
> taxonName
= getOrCreateTaxonName(nvname
.getFullTitleCache(), Rank
.SUBSPECIES(), preferredFlag
, state
, -1);
794 subspecies
= getOrCreateTaxonForName(taxonName
, state
);
796 parent
= linkParentChildNode(species
, subspecies
, classification
, state
);
800 if (preferredFlag
&& parent
!=taxon
) {
801 linkParentChildNode(parent
, taxon
, classification
, state
);
806 * Link a parent to a child and save it in the current classification
807 * @param parent: the higher Taxon
808 * @param child : the lower (or current) Taxon
809 * return the Taxon from the new created Node
810 * @param classification
813 protected Taxon
linkParentChildNode(Taxon parent
, Taxon child
, Classification classification
, STATE state
) {
814 TaxonNode node
=null;
815 if (parent
!= null) {
816 parent
= (Taxon
) getTaxonService().find(parent
.getUuid());
817 child
= (Taxon
) getTaxonService().find(child
.getUuid());
818 //here we do not have to check if the taxon nodes already exists
819 //this is done by classification.addParentChild()
820 //do not add child node if it already exists
821 if(hasTaxonNodeInClassification(child
, classification
)){
825 node
= classification
.addParentChild(parent
, child
, state
.getRef(), "");
830 child
= (Taxon
) getTaxonService().find(child
.getUuid());
831 //do not add child node if it already exists
832 if(hasTaxonNodeInClassification(child
, classification
)){
836 node
= classification
.addChildTaxon(child
, state
.getRef(), null);
841 state
.getReport().addTaxonNode(node
);
842 return node
.getTaxon();
844 String message
= "Could not create taxon node for " +child
;
845 state
.getReport().addInfoMessage(message
);
846 logger
.warn(message
);
850 protected Taxon
getOrCreateTaxonForName(TaxonNameBase
<?
, ?
> taxonNameBase
, STATE state
){
852 Set
<Taxon
> acceptedTaxa
= taxonNameBase
.getTaxa();
853 if(acceptedTaxa
.size()>0){
854 Taxon firstAcceptedTaxon
= acceptedTaxa
.iterator().next();
855 if(acceptedTaxa
.size()>1){
856 String message
= "More than one accepted taxon was found for taxon name: "
857 + taxonNameBase
.getTitleCache() + "!\n" + firstAcceptedTaxon
+ "was chosen for "+state
.getDerivedUnitBase();
858 state
.getReport().addInfoMessage(message
);
859 logger
.warn(message
);
862 return firstAcceptedTaxon
;
866 Set
<TaxonBase
> taxonAndSynonyms
= taxonNameBase
.getTaxonBases();
867 for (TaxonBase taxonBase
: taxonAndSynonyms
) {
868 if(taxonBase
.isInstanceOf(Synonym
.class)){
869 Synonym synonym
= HibernateProxyHelper
.deproxy(taxonBase
, Synonym
.class);
870 Taxon acceptedTaxonOfSynonym
= synonym
.getAcceptedTaxon();
871 if(acceptedTaxonOfSynonym
== null){
872 String message
= "No accepted taxon could be found for taxon name: "
873 + taxonNameBase
.getTitleCache()
875 state
.getReport().addInfoMessage(message
);
876 logger
.warn(message
);
879 return acceptedTaxonOfSynonym
;
884 Taxon taxon
= Taxon
.NewInstance(taxonNameBase
, state
.getRef());
886 state
.getReport().addTaxon(taxon
);
887 logger
.info("Created new taxon "+ taxon
);
891 private boolean hasTaxonNodeInClassification(Taxon taxon
, Classification classification
){
892 if(taxon
.getTaxonNodes()!=null){
893 for (TaxonNode node
: taxon
.getTaxonNodes()){
894 if(node
.getClassification().equals(classification
)){
903 * HandleIdentifications : get the scientific names present in the ABCD
904 * document and store link them with the observation/specimen data
905 * @param state: the current ABCD import state
906 * @param derivedUnitFacade : the current derivedunitfacade
908 protected void handleIdentifications(STATE state
, DerivedUnitFacade derivedUnitFacade
) {
909 SpecimenImportConfiguratorBase config
= state
.getConfig();
912 String scientificName
= "";
913 boolean preferredFlag
= false;
915 if (state
.getDataHolder().getNomenclatureCode() == ""){
916 if (config
.getNomenclaturalCode() != null){
917 if (config
.getNomenclaturalCode() != null){
918 state
.getDataHolder().setNomenclatureCode(config
.getNomenclaturalCode().toString());
923 for (int i
= 0; i
< state
.getDataHolder().getIdentificationList().size(); i
++) {
924 Identification identification
= state
.getDataHolder().getIdentificationList().get(i
);
925 scientificName
= identification
.getScientificName().replaceAll(" et ", " & ");
927 String preferred
= identification
.getPreferred();
928 if (preferred
.equals("1") || preferred
.toLowerCase().indexOf("true") != -1 || state
.getDataHolder().getIdentificationList().size()==1) {
929 preferredFlag
= true;
932 preferredFlag
= false;
934 if (identification
.getCode() != null){
935 if (identification
.getCode().indexOf(':') != -1) {
936 state
.getDataHolder().setNomenclatureCode(identification
.getCode().split(COLON
)[1]);
939 state
.getDataHolder().setNomenclatureCode(identification
.getCode());
942 TaxonNameBase
<?
,?
> taxonName
= getOrCreateTaxonName(scientificName
, null, preferredFlag
, state
, i
);
943 Taxon taxon
= getOrCreateTaxonForName(taxonName
, state
);
944 addTaxonNode(taxon
, state
,preferredFlag
);
945 linkDeterminationEvent(state
, taxon
, preferredFlag
, derivedUnitFacade
);
950 * @param taxon : a taxon to add as a node
951 * @param state : the ABCD import state
953 protected void addTaxonNode(Taxon taxon
, STATE state
, boolean preferredFlag
) {
954 SpecimenImportConfiguratorBase config
= state
.getConfig();
955 logger
.info("link taxon to a taxonNode "+taxon
.getTitleCache());
956 //only add nodes if not already existing in current classification or default classification
958 //check if node exists in current classification
959 //NOTE: we cannot use hasTaxonNodeInClassification() here because we are first creating it here
960 if (!existsInClassification(taxon
,state
.getClassification(), state
)){
961 if(config
.isMoveNewTaxaToDefaultClassification()){
962 //check if node exists in default classification
963 if (!existsInClassification(taxon
, state
.getDefaultClassification(), state
)){
964 addParentTaxon(taxon
, state
, preferredFlag
, state
.getDefaultClassification());
968 //add non-existing taxon to current classification
969 addParentTaxon(taxon
, state
, preferredFlag
, state
.getClassification());
975 private boolean existsInClassification(Taxon taxon
, Classification classification
, STATE state
){
976 boolean exist
= false;
977 ICdmApplicationConfiguration cdmAppController
= state
.getConfig().getCdmAppController();
978 if(cdmAppController
==null){
979 cdmAppController
= this;
981 if (classification
!= null){
982 List
<UuidAndTitleCache
<TaxonNode
>> uuidAndTitleCacheOfAllTaxa
= cdmAppController
.getClassificationService().getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(classification
.getUuid());
983 if (uuidAndTitleCacheOfAllTaxa
!= null){
984 for (UuidAndTitleCache p
: uuidAndTitleCacheOfAllTaxa
){
986 if(p
.getTitleCache().equals(taxon
.getTitleCache())) {
991 logger
.warn("TaxonNode doesn't seem to have a taxon");
1000 * join DeterminationEvent to the Taxon Object
1001 * @param state : the ABCD import state
1002 * @param taxon: the current Taxon
1003 * @param preferredFlag :if the current name is preferred
1004 * @param derivedFacade : the derived Unit Facade
1006 @SuppressWarnings("rawtypes")
1007 protected void linkDeterminationEvent(STATE state
, Taxon taxon
, boolean preferredFlag
, DerivedUnitFacade derivedFacade
) {
1008 SpecimenImportConfiguratorBase config
= state
.getConfig();
1010 logger
.info("start linkdetermination with taxon:" + taxon
.getUuid()+", "+taxon
);
1013 DeterminationEvent determinationEvent
= DeterminationEvent
.NewInstance();
1014 determinationEvent
.setTaxonName(taxon
.getName());
1015 determinationEvent
.setPreferredFlag(preferredFlag
);
1017 determinationEvent
.setIdentifiedUnit(state
.getDerivedUnitBase());
1018 state
.getDerivedUnitBase().addDetermination(determinationEvent
);
1021 logger
.info("NB TYPES INFO: "+ state
.getDataHolder().getStatusList().size());
1023 for (SpecimenTypeDesignationStatus specimenTypeDesignationstatus
: state
.getDataHolder().getStatusList()) {
1024 if (specimenTypeDesignationstatus
!= null) {
1026 logger
.info("specimenTypeDesignationstatus :"+ specimenTypeDesignationstatus
);
1029 ICdmApplicationConfiguration cdmAppController
= config
.getCdmAppController();
1030 if(cdmAppController
== null){
1031 cdmAppController
= this;
1033 specimenTypeDesignationstatus
= (SpecimenTypeDesignationStatus
) cdmAppController
.getTermService().find(specimenTypeDesignationstatus
.getUuid());
1035 TaxonNameBase
<?
,?
> name
= taxon
.getName();
1036 SpecimenTypeDesignation designation
= SpecimenTypeDesignation
.NewInstance();
1038 designation
.setTypeStatus(specimenTypeDesignationstatus
);
1039 designation
.setTypeSpecimen(state
.getDerivedUnitBase());
1040 name
.addTypeDesignation(designation
, true);
1044 for (String
[] fullReference
: state
.getDataHolder().getReferenceList()) {
1047 String strReference
=fullReference
[0];
1048 String citationDetail
= fullReference
[1];
1049 String citationURL
= fullReference
[2];
1050 List
<Reference
> references
= getReferenceService().listByTitle(Reference
.class, "strReference", MatchMode
.EXACT
, null, null, null, null, null);
1052 if (!references
.isEmpty()){
1053 Reference reference
= null;
1054 for (Reference refe
: references
) {
1055 if (refe
.getTitleCache().equalsIgnoreCase(strReference
)) {
1060 if (reference
==null){
1061 reference
= ReferenceFactory
.newGeneric();
1062 reference
.setTitleCache(strReference
, true);
1063 save(reference
, state
);
1065 determinationEvent
.addReference(reference
);
1068 save(state
.getDerivedUnitBase(), state
);
1070 if (config
.isAddIndividualsAssociationsSuchAsSpecimenAndObservations() && preferredFlag
) {
1071 //do not add IndividualsAssociation to non-preferred taxa
1073 logger
.info("isDoCreateIndividualsAssociations");
1076 makeIndividualsAssociation(state
, taxon
, determinationEvent
);
1078 save(state
.getDerivedUnitBase(), state
);
1083 * create and link each association (specimen, observation..) to the accepted taxon
1084 * @param state : the ABCD import state
1085 * @param taxon: the current Taxon
1086 * @param determinationEvent:the determinationevent
1088 protected void makeIndividualsAssociation(STATE state
, Taxon taxon
, DeterminationEvent determinationEvent
) {
1089 SpecimenImportConfiguratorBase config
= state
.getConfig();
1090 SpecimenUserInteraction sui
= config
.getSpecimenUserInteraction();
1093 logger
.info("MAKE INDIVIDUALS ASSOCIATION");
1096 TaxonDescription taxonDescription
= null;
1097 Set
<TaxonDescription
> descriptions
= taxon
.getDescriptions();
1098 // if (((Abcd206ImportConfigurator) state.getConfig()).isInteractWithUser()){
1099 // if(!state.isDescriptionGroupSet()){
1100 // taxonDescription = sui.askForDescriptionGroup(descriptions);
1101 // state.setDescriptionGroup(taxonDescription);
1102 // state.setDescriptionGroupSet(true);
1104 // taxonDescription=state.getDescriptionGroup();
1107 for (TaxonDescription description
: descriptions
){
1108 Set
<IdentifiableSource
> sources
= new HashSet
<>();
1109 sources
.addAll(description
.getTaxon().getSources());
1110 sources
.addAll(description
.getSources());
1111 for (IdentifiableSource source
:sources
){
1112 if(state
.getRef().equals(source
.getCitation())) {
1113 taxonDescription
= description
;
1118 if (taxonDescription
== null){
1119 taxonDescription
= TaxonDescription
.NewInstance(taxon
, false);
1120 if(sourceNotLinkedToElement(taxonDescription
,state
.getRef(),null)) {
1121 taxonDescription
.addSource(OriginalSourceType
.Import
, null, null, state
.getRef(), null);
1123 state
.setDescriptionGroup(taxonDescription
);
1124 taxon
.addDescription(taxonDescription
);
1127 //PREPARE REFERENCE QUESTIONS
1129 Map
<String
,OriginalSourceBase
<?
>> sourceMap
= new HashMap
<String
, OriginalSourceBase
<?
>>();
1131 List
<IdentifiableSource
> issTmp
= new ArrayList
<>();//getCommonService().list(IdentifiableSource.class, null, null, null, null);
1132 List
<DescriptionElementSource
> issTmp2
= new ArrayList
<>();//getCommonService().list(DescriptionElementSource.class, null, null, null, null);
1134 Set
<OriginalSourceBase
> osbSet
= new HashSet
<OriginalSourceBase
>();
1136 osbSet
.addAll(issTmp2
);
1139 osbSet
.addAll(issTmp
);
1143 addToSourceMap(sourceMap
, osbSet
);
1145 // if (((Abcd206ImportConfigurator) state.getConfig()).isInteractWithUser()){
1146 // List<OriginalSourceBase<?>> res = null;
1147 // if(!state.isDescriptionSourcesSet()){
1148 // res = sui.askForSource(sourceMap, "the description group ("+taxon+")",
1149 // "The current reference is "+state.getRef().getTitleCache(),getReferenceService(), state.getDataHolder().docSources);
1150 // state.setDescriptionRefs(res);
1151 // state.setDescriptionSourcesSet(true);
1154 // res=state.getDescriptionRefs();
1157 // for (OriginalSourceBase<?> sour:res){
1158 // if(sour.isInstanceOf(IdentifiableSource.class)){
1160 // if(sourceNotLinkedToElement(taxonDescription,sour)) {
1161 // taxonDescription.addSource((IdentifiableSource)sour.clone());
1163 // } catch (CloneNotSupportedException e) {
1164 // logger.warn("no cloning?");
1167 // if(sourceNotLinkedToElement(taxonDescription,sour)) {
1168 // taxonDescription.addSource(OriginalSourceType.Import,null, null, sour.getCitation(),sour.getCitationMicroReference());
1175 if(sourceNotLinkedToElement(taxonDescription
,state
.getRef(),null)) {
1176 taxonDescription
.addSource(OriginalSourceType
.Import
,null, null, state
.getRef(), null);
1179 state
.setDescriptionGroup(taxonDescription
);
1181 IndividualsAssociation indAssociation
= IndividualsAssociation
.NewInstance();
1182 Feature feature
= makeFeature(state
.getDerivedUnitBase());
1183 indAssociation
.setAssociatedSpecimenOrObservation(state
.getDerivedUnitBase());
1184 indAssociation
.setFeature(feature
);
1186 // if (((Abcd206ImportConfigurator) state.getConfig()).isInteractWithUser()){
1187 // sourceMap = new HashMap<String, OriginalSourceBase<?>>();
1189 // issTmp = getCommonService().list(IdentifiableSource.class, null, null, null, null);
1190 // issTmp2 = getCommonService().list(DescriptionElementSource.class, null, null, null, null);
1192 // osbSet = new HashSet<OriginalSourceBase>();
1193 // if(issTmp2!=null) {
1194 // osbSet.addAll(issTmp2);
1196 // if(issTmp!=null) {
1197 // osbSet.addAll(issTmp);
1201 // addToSourceMap(sourceMap, osbSet);
1203 // List<OriginalSourceBase<?>> sources =null;
1204 // if(!state.isAssociationSourcesSet()) {
1205 // sources = sui.askForSource(sourceMap, "descriptive element (association) ",taxon.toString(),
1206 // getReferenceService(),state.getDataHolder().getDocSources());
1207 // state.setAssociationRefs(sources);
1208 // state.setAssociationSourcesSet(true);
1211 // sources=state.getAssociationRefs();
1213 // if(sources !=null) {
1214 // for (OriginalSourceBase<?> source: sources) {
1215 // if(source !=null) {
1216 // if(source.isInstanceOf(DescriptionElementSource.class)){
1218 // if(sourceNotLinkedToElement(indAssociation,source)) {
1219 // indAssociation.addSource((DescriptionElementSource)source.clone());
1221 // } catch (CloneNotSupportedException e) {
1222 // logger.warn("clone forbidden?");
1225 // if(sourceNotLinkedToElement(indAssociation,source)) {
1226 // indAssociation.addSource(OriginalSourceType.Import,null, null, source.getCitation(),source.getCitationMicroReference());
1229 // if(sourceNotLinkedToElement(state.getDerivedUnitBase(), source)) {
1230 // state.getDerivedUnitBase().addSource((IdentifiableSource) source.clone());
1232 // } catch (CloneNotSupportedException e) {
1233 // // TODO Auto-generated catch block
1234 // e.printStackTrace();
1242 if(sourceNotLinkedToElement(indAssociation
,state
.getRef(),null)) {
1243 indAssociation
.addSource(OriginalSourceType
.Import
,null, null, state
.getRef(), null);
1245 if(sourceNotLinkedToElement(state
.getDerivedUnitBase(), state
.getRef(),null)) {
1246 state
.getDerivedUnitBase().addSource(OriginalSourceType
.Import
,null, null, state
.getRef(), null);
1248 for (Reference citation
: determinationEvent
.getReferences()) {
1249 if(sourceNotLinkedToElement(indAssociation
,citation
,null))
1251 indAssociation
.addSource(DescriptionElementSource
.NewInstance(OriginalSourceType
.Import
, null, null, citation
, null));
1253 if(sourceNotLinkedToElement(state
.getDerivedUnitBase(), state
.getRef(),null)) {
1254 state
.getDerivedUnitBase().addSource(OriginalSourceType
.Import
,null, null, state
.getRef(), null);
1259 taxonDescription
.addElement(indAssociation
);
1261 save(taxonDescription
, state
);
1263 state
.getReport().addDerivate(state
.getDerivedUnitBase(), config
);
1264 state
.getReport().addIndividualAssociation(taxon
, state
.getDataHolder().getUnitID(), state
.getDerivedUnitBase());
1268 * @param derivedUnitBase2
1273 private boolean sourceNotLinkedToElement(DerivedUnit derivedUnitBase2
, Reference b
, String d
) {
1274 Set
<IdentifiableSource
> linkedSources
= derivedUnitBase2
.getSources();
1275 for (IdentifiableSource is
:linkedSources
){
1276 Reference a
= is
.getCitation();
1277 String c
= is
.getCitationMicroReference();
1279 boolean refMatch
=false;
1280 boolean microMatch
=false;
1283 if (a
==null && b
==null) {
1286 if (a
!=null && b
!=null) {
1287 if (a
.getTitleCache().equalsIgnoreCase(b
.getTitleCache())) {
1291 }catch(Exception e
){}
1295 if (c
==null && d
==null) {
1298 if(c
!=null && d
!=null) {
1299 if(c
.equalsIgnoreCase(d
)) {
1304 catch(Exception e
){}
1306 if (microMatch
&& refMatch
) {
1315 private <T
extends OriginalSourceBase
<?
>> boolean sourceNotLinkedToElement(ISourceable
<T
> sourcable
, Reference reference
, String microReference
) {
1316 Set
<T
> linkedSources
= sourcable
.getSources();
1317 for (T is
:linkedSources
){
1318 Reference unitReference
= is
.getCitation();
1319 String unitMicroReference
= is
.getCitationMicroReference();
1321 boolean refMatch
=false;
1322 boolean microMatch
=false;
1325 if (unitReference
==null && reference
==null) {
1328 if (unitReference
!=null && reference
!=null) {
1329 if (unitReference
.getTitleCache().equalsIgnoreCase(reference
.getTitleCache())) {
1333 }catch(Exception e
){}
1336 if (unitMicroReference
==null && microReference
==null) {
1339 if(unitMicroReference
!=null && microReference
!=null) {
1340 if(unitMicroReference
.equalsIgnoreCase(microReference
)) {
1345 catch(Exception e
){}
1347 if (microMatch
&& refMatch
) {
1355 * look for the Feature object (FieldObs, Specimen,...)
1356 * @param unit : a specimen or obersvation base
1357 * @return the corresponding Feature
1359 private Feature
makeFeature(SpecimenOrObservationBase
<?
> unit
) {
1360 SpecimenOrObservationType type
= unit
.getRecordBasis();
1364 if (type
.isFeatureObservation()){
1365 return Feature
.OBSERVATION();
1366 }else if (type
.isFeatureSpecimen()){
1367 return Feature
.SPECIMEN();
1368 }else if (type
== SpecimenOrObservationType
.DerivedUnit
){
1369 return Feature
.OBSERVATION();
1370 // return getFeature("Specimen or observation");
1372 String message
= "Unhandled record basis '%s' for defining individuals association feature type. Use default.";
1373 logger
.warn(String
.format(message
, type
.getMessage()));
1374 return Feature
.OBSERVATION();
1375 // return getFeature("Specimen or observation");
1385 protected void addToSourceMap(Map
<String
, OriginalSourceBase
<?
>> sourceMap
, Set
<OriginalSourceBase
> osbSet
) {
1386 for( OriginalSourceBase
<?
> osb
:osbSet
) {
1387 if(osb
.getCitation()!=null && osb
.getCitationMicroReference() !=null && !osb
.getCitationMicroReference().isEmpty()) {
1389 sourceMap
.put(osb
.getCitation().getTitleCache()+ "---"+osb
.getCitationMicroReference(),osb
);
1390 }catch(NullPointerException e
){logger
.warn("null pointer problem (no ref?) with "+osb
);}
1391 } else if(osb
.getCitation()!=null){
1393 sourceMap
.put(osb
.getCitation().getTitleCache(),osb
);
1394 }catch(NullPointerException e
){logger
.warn("null pointer problem (no ref?) with "+osb
);}