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
.abcd206
.in
;
12 import java
.io
.IOException
;
13 import java
.io
.InputStream
;
14 import java
.net
.MalformedURLException
;
17 import java
.util
.ArrayList
;
18 import java
.util
.HashMap
;
19 import java
.util
.HashSet
;
20 import java
.util
.Iterator
;
21 import java
.util
.List
;
24 import java
.util
.UUID
;
26 import javax
.xml
.parsers
.DocumentBuilder
;
27 import javax
.xml
.parsers
.DocumentBuilderFactory
;
28 import javax
.xml
.parsers
.ParserConfigurationException
;
30 import org
.apache
.commons
.lang
.StringUtils
;
31 import org
.apache
.log4j
.Logger
;
32 import org
.springframework
.stereotype
.Component
;
33 import org
.w3c
.dom
.Document
;
34 import org
.w3c
.dom
.Element
;
35 import org
.w3c
.dom
.NodeList
;
36 import org
.xml
.sax
.SAXException
;
38 import eu
.etaxonomy
.cdm
.api
.facade
.DerivedUnitFacade
;
39 import eu
.etaxonomy
.cdm
.api
.facade
.DerivedUnitFacade
.DerivedUnitType
;
40 import eu
.etaxonomy
.cdm
.io
.specimen
.SpecimenImportBase
;
41 import eu
.etaxonomy
.cdm
.io
.specimen
.UnitsGatheringArea
;
42 import eu
.etaxonomy
.cdm
.io
.specimen
.UnitsGatheringEvent
;
43 import eu
.etaxonomy
.cdm
.model
.agent
.AgentBase
;
44 import eu
.etaxonomy
.cdm
.model
.agent
.Institution
;
45 import eu
.etaxonomy
.cdm
.model
.agent
.Person
;
46 import eu
.etaxonomy
.cdm
.model
.agent
.Team
;
47 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
48 import eu
.etaxonomy
.cdm
.model
.common
.DescriptionElementSource
;
49 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
50 import eu
.etaxonomy
.cdm
.model
.common
.OriginalSourceType
;
51 import eu
.etaxonomy
.cdm
.model
.common
.UuidAndTitleCache
;
52 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
53 import eu
.etaxonomy
.cdm
.model
.description
.IndividualsAssociation
;
54 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
55 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
56 import eu
.etaxonomy
.cdm
.model
.media
.Media
;
57 import eu
.etaxonomy
.cdm
.model
.name
.BacterialName
;
58 import eu
.etaxonomy
.cdm
.model
.name
.BotanicalName
;
59 import eu
.etaxonomy
.cdm
.model
.name
.CultivarPlantName
;
60 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCode
;
61 import eu
.etaxonomy
.cdm
.model
.name
.NonViralName
;
62 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
63 import eu
.etaxonomy
.cdm
.model
.name
.SpecimenTypeDesignation
;
64 import eu
.etaxonomy
.cdm
.model
.name
.SpecimenTypeDesignationStatus
;
65 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
66 import eu
.etaxonomy
.cdm
.model
.name
.ZoologicalName
;
67 import eu
.etaxonomy
.cdm
.model
.occurrence
.Collection
;
68 import eu
.etaxonomy
.cdm
.model
.occurrence
.DerivedUnit
;
69 import eu
.etaxonomy
.cdm
.model
.occurrence
.DerivedUnitBase
;
70 import eu
.etaxonomy
.cdm
.model
.occurrence
.DeterminationEvent
;
71 import eu
.etaxonomy
.cdm
.model
.occurrence
.FieldObservation
;
72 import eu
.etaxonomy
.cdm
.model
.occurrence
.Fossil
;
73 import eu
.etaxonomy
.cdm
.model
.occurrence
.GatheringEvent
;
74 import eu
.etaxonomy
.cdm
.model
.occurrence
.LivingBeing
;
75 import eu
.etaxonomy
.cdm
.model
.occurrence
.Observation
;
76 import eu
.etaxonomy
.cdm
.model
.occurrence
.Specimen
;
77 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
78 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
79 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceFactory
;
80 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
81 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
82 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
83 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
84 import eu
.etaxonomy
.cdm
.strategy
.parser
.NonViralNameParserImpl
;
92 public class Abcd206Import
extends SpecimenImportBase
<Abcd206ImportConfigurator
, Abcd206ImportState
> {
93 private static final Logger logger
= Logger
.getLogger(Abcd206Import
.class);
96 private final boolean DEBUG
= false;
98 private static final String SEC
= "sec. ";
99 private static final String PREFERRED
= "_preferred_";
100 private static final String CODE
= "_code_";
101 private static final String COLON
= ":";
102 private static final String SPLITTER
= "--";
105 private static String prefix
= "";
107 //TODO make all fields ABCD206ImportState variables
108 private Classification classification
= null;
109 private Reference
<?
> ref
= null;
111 private Abcd206DataHolder dataHolder
;
112 private DerivedUnitBase
<?
> derivedUnitBase
;
114 public Abcd206Import() {
119 protected boolean doCheck(Abcd206ImportState state
) {
120 logger
.warn("Checking not yet implemented for " + this.getClass().getSimpleName());
125 * getClassification : get the classification declared in the ImportState
130 private void setClassification(Abcd206ImportState state
) {
131 if (classification
== null) {
132 String name
= NB(state
.getConfig().getClassificationName());
134 classification
= Classification
.NewInstance(name
, ref
, Language
.DEFAULT());
135 if (state
.getConfig().getClassificationUuid() != null) {
136 classification
.setUuid(state
.getConfig().getClassificationUuid());
138 getClassificationService().saveOrUpdate(classification
);
139 refreshTransaction(state
);
145 public void doInvoke(Abcd206ImportState state
) {
146 state
.setTx(startTransaction());
148 logger
.info("INVOKE Specimen Import from ABCD2.06 XML ");
149 URI sourceName
= state
.getConfig().getSource();
150 NodeList unitsList
= getUnitsNodeList(sourceName
);
152 ref
= state
.getConfig().getSourceReference();
153 setClassification(state
);
155 if (unitsList
!= null) {
156 String message
= "nb units to insert: " + unitsList
.getLength();
157 logger
.info(message
);
158 updateProgress(state
, message
);
159 dataHolder
= new Abcd206DataHolder();
161 Abcd206XMLFieldGetter abcdFieldGetter
= new Abcd206XMLFieldGetter(dataHolder
, prefix
);
163 prepareCollectors(state
, unitsList
, abcdFieldGetter
);
165 for (int i
= 0; i
< unitsList
.getLength(); i
++) {
167 this.setUnitPropertiesXML( (Element
) unitsList
.item(i
), abcdFieldGetter
);
168 // refreshTransaction(state);
169 this.handleSingleUnit(state
, i
);
171 // compare the ABCD elements added in to the CDM and the
172 // unhandled ABCD elements
173 //compareABCDtoCDM(sourceName, dataHolder.knownABCDelements, abcdFieldGetter);
175 // reset the ABCD elements added in CDM
176 // knownABCDelements = new ArrayList<String>();
177 dataHolder
.allABCDelements
= new HashMap
<String
, String
>();
179 //refreshTransaction(state);
182 commitTransaction(state
.getTx());
188 * Return the list of root nodes for an ABCD 2.06 XML file
190 * @param fileName: the file's location
192 * @return the list of root nodes ("Unit")
194 protected NodeList
getUnitsNodeList(URI urlFileName
) {
195 NodeList unitList
= null;
197 DocumentBuilderFactory factory
= DocumentBuilderFactory
.newInstance();
198 DocumentBuilder builder
= factory
.newDocumentBuilder();
199 URL url
= urlFileName
.toURL();
200 Object o
= url
.getContent();
201 InputStream is
= (InputStream
) o
;
202 Document document
= builder
.parse(is
);
203 Element root
= document
.getDocumentElement();
204 unitList
= root
.getElementsByTagName("Unit");
205 if (unitList
.getLength() == 0) {
206 unitList
= root
.getElementsByTagName("abcd:Unit");
209 } catch (Exception e
) {
216 * Stores the unit with its Gathering informations in the CDM
218 private void handleSingleUnit(Abcd206ImportState state
, int i
) {
219 logger
.info("handleSingleUnit");
222 updateProgress(state
, "Importing data for unit: " + dataHolder
.unitID
);
226 DerivedUnitFacade derivedUnitFacade
= getFacade();
227 derivedUnitBase
= derivedUnitFacade
.innerDerivedUnit();
234 UnitsGatheringEvent unitsGatheringEvent
= new UnitsGatheringEvent(getTermService(), dataHolder
.locality
, dataHolder
.languageIso
,
235 dataHolder
.longitude
, dataHolder
.latitude
, dataHolder
.gatheringAgentList
, dataHolder
.gatheringTeamList
, state
.getConfig());
238 UnitsGatheringArea unitsGatheringArea
= new UnitsGatheringArea(NB(dataHolder
.isocountry
), NB(dataHolder
.country
), this);
239 NamedArea areaCountry
= unitsGatheringArea
.getArea();
242 unitsGatheringArea
= new UnitsGatheringArea(dataHolder
.namedAreaList
);
243 ArrayList
<NamedArea
> otherAreas
= unitsGatheringArea
.getAreas();
244 for (NamedArea namedArea
: otherAreas
) {
245 unitsGatheringEvent
.addArea(namedArea
);
248 // copy gathering event to facade
249 GatheringEvent gatheringEvent
= unitsGatheringEvent
.getGatheringEvent();
250 derivedUnitFacade
.setLocality(gatheringEvent
.getLocality());
251 derivedUnitFacade
.setExactLocation(gatheringEvent
.getExactLocation());
252 derivedUnitFacade
.setCollector(gatheringEvent
.getCollector());
253 derivedUnitFacade
.setCountry(areaCountry
);
254 derivedUnitFacade
.addCollectingAreas(unitsGatheringArea
.getAreas());
259 derivedUnitFacade
.setFieldNumber(NB(dataHolder
.fieldNumber
));
261 // //add Multimedia URLs
262 if (dataHolder
.multimediaObjects
.size() != -1) {
263 for (String multimediaObject
: dataHolder
.multimediaObjects
) {
266 media
= getImageMedia(multimediaObject
, READ_MEDIA_DATA
, false);
267 derivedUnitFacade
.addDerivedUnitMedia(media
);
268 } catch (MalformedURLException e
) {
269 // TODO Auto-generated catch block
277 * merge AND STORE DATA
279 getTermService().saveOrUpdate(areaCountry
);// TODO save area sooner
281 for (NamedArea area
: otherAreas
) {
282 getTermService().saveOrUpdate(area
);// merge it sooner (foreach area)
284 getTermService().saveLanguageData(unitsGatheringEvent
.getLocality());
286 // handle collection data
287 setCollectionData(state
.getConfig(), derivedUnitFacade
);
290 getOccurrenceService().saveOrUpdate(derivedUnitBase
);
291 refreshTransaction(state
);
293 // handle identifications
294 handleIdentifications(state
, derivedUnitFacade
);
296 logger
.info("saved ABCD specimen ...");
298 } catch (Exception e
) {
299 logger
.warn("Error when reading record!!");
301 state
.setUnsuccessfull();
308 * setCollectionData : store the collection object into the
313 private void setCollectionData(Abcd206ImportConfigurator config
,
314 DerivedUnitFacade derivedUnitFacade
) {
315 // set catalogue number (unitID)
316 derivedUnitFacade
.setCatalogNumber(NB(dataHolder
.unitID
));
317 derivedUnitFacade
.setAccessionNumber(NB(dataHolder
.accessionNumber
));
318 // derivedUnitFacade.setCollectorsNumber(NB(dataHolder.collectorsNumber));
321 * INSTITUTION & COLLECTION
323 // manage institution
324 Institution institution
= this.getInstitution(NB(dataHolder
.institutionCode
), config
);
326 Collection collection
= this.getCollection(institution
, config
);
327 // link specimen & collection
328 derivedUnitFacade
.setCollection(collection
);
332 * getFacade : get the DerivedUnitFacade based on the recordBasis
334 * @return DerivedUnitFacade
336 private DerivedUnitFacade
getFacade() {
338 logger
.info("getFacade()");
340 DerivedUnitType type
= null;
343 if (NB((dataHolder
.recordBasis
)) != null) {
344 if (dataHolder
.recordBasis
.toLowerCase().startsWith("s") || dataHolder
.recordBasis
.toLowerCase().contains("specimen")) {// specimen
345 type
= DerivedUnitType
.Specimen
;
347 if (dataHolder
.recordBasis
.toLowerCase().startsWith("o")) {
348 type
= DerivedUnitType
.Observation
;
350 if (dataHolder
.recordBasis
.toLowerCase().contains("fossil")){
351 type
= DerivedUnitType
.Fossil
;
353 if (dataHolder
.recordBasis
.toLowerCase().startsWith("l")) {
354 type
= DerivedUnitType
.LivingBeing
;
357 logger
.info("The basis of record does not seem to be known: " + dataHolder
.recordBasis
);
358 type
= DerivedUnitType
.DerivedUnit
;
362 logger
.info("The basis of record is null");
363 type
= DerivedUnitType
.DerivedUnit
;
365 DerivedUnitFacade derivedUnitFacade
= DerivedUnitFacade
.NewInstance(type
);
366 return derivedUnitFacade
;
369 private void getCollectorsFromXML(Element root
, Abcd206XMLFieldGetter abcdFieldGetter
) {
372 group
= root
.getChildNodes();
373 for (int i
= 0; i
< group
.getLength(); i
++) {
374 if (group
.item(i
).getNodeName().equals(prefix
+ "Identifications")) {
375 group
= group
.item(i
).getChildNodes();
379 dataHolder
.gatheringAgentList
= new ArrayList
<String
>();
380 dataHolder
.gatheringTeamList
= new ArrayList
<String
>();
381 abcdFieldGetter
.getType(root
);
382 abcdFieldGetter
.getGatheringPeople(root
);
386 * Store the unit's properties into variables Look which unit is the
387 * preferred one Look what kind of name it is supposed to be, for the
388 * parsing (Botanical, Zoological)
390 * @param racine: the root node for a single unit
392 private void setUnitPropertiesXML(Element root
, Abcd206XMLFieldGetter abcdFieldGetter
) {
396 group
= root
.getChildNodes();
397 for (int i
= 0; i
< group
.getLength(); i
++) {
398 if (group
.item(i
).getNodeName().equals(prefix
+ "Identifications")) {
399 group
= group
.item(i
).getChildNodes();
403 dataHolder
.identificationList
= new ArrayList
<String
>();
404 dataHolder
.statusList
= new ArrayList
<SpecimenTypeDesignationStatus
>();
405 dataHolder
.atomisedIdentificationList
= new ArrayList
<HashMap
<String
, String
>>();
406 dataHolder
.referenceList
= new ArrayList
<String
>();
407 dataHolder
.multimediaObjects
= new ArrayList
<String
>();
409 abcdFieldGetter
.getScientificNames(group
);
410 abcdFieldGetter
.getType(root
);
413 logger
.info("this.identificationList "+dataHolder
.identificationList
.toString());
415 abcdFieldGetter
.getIDs(root
);
416 abcdFieldGetter
.getRecordBasis(root
);
417 abcdFieldGetter
.getMultimedia(root
);
418 abcdFieldGetter
.getNumbers(root
);
419 abcdFieldGetter
.getGeolocation(root
);
420 abcdFieldGetter
.getGatheringPeople(root
);
421 boolean referencefound
= abcdFieldGetter
.getReferences(root
);
422 if (!referencefound
) {
423 dataHolder
.referenceList
.add(ref
.getTitleCache());
426 } catch (Exception e
) {
427 logger
.info("Error occured while parsing XML file" + e
);
431 private Institution
getInstitution(String institutionCode
, Abcd206ImportConfigurator config
) {
432 Institution institution
;
433 List
<Institution
> institutions
;
435 if(DEBUG
){ logger
.info(dataHolder
.institutionCode
);}
436 institutions
= getAgentService().searchInstitutionByCode(dataHolder
.institutionCode
);
437 } catch (Exception e
) {
438 institutions
= new ArrayList
<Institution
>();
440 if (institutions
.size() == 0 || !config
.isReUseExistingMetadata()) {
442 logger
.info("Institution (agent) unknown or not allowed to reuse existing metadata");
444 // create institution
445 institution
= Institution
.NewInstance();
446 institution
.setCode(NB(dataHolder
.institutionCode
));
450 logger
.info("Institution (agent) already in the db");
452 institution
= institutions
.get(0);
454 if(DEBUG
) logger
.info("getinstitution " + institution
.toString());
459 * Look if the Collection does already exists
460 * @param collectionCode: a string
461 * @param institution: the current Institution
463 * @return the Collection (existing or new)
465 private Collection
getCollection(Institution institution
, Abcd206ImportConfigurator config
) {
466 Collection collection
= Collection
.NewInstance();
467 List
<Collection
> collections
;
469 collections
= getCollectionService().searchByCode(dataHolder
.collectionCode
);
470 } catch (Exception e
) {
471 collections
= new ArrayList
<Collection
>();
473 if (collections
.size() == 0 || !config
.isReUseExistingMetadata()) {
474 if(DEBUG
) logger
.info("Collection not found or do not reuse existing metadata " + dataHolder
.collectionCode
);
475 // create new collection
476 collection
.setCode(NB(dataHolder
.collectionCode
));
477 collection
.setCodeStandard(NB("GBIF"));
478 collection
.setInstitute(institution
);
480 boolean collectionFound
= false;
481 for (int i
= 0; i
< collections
.size(); i
++) {
482 collection
= collections
.get(i
);
484 if (collection
.getInstitute().getCode().equalsIgnoreCase(institution
.getCode())) {
485 // found a collection with the same code and the same institution
486 collectionFound
= true;
489 } catch (NullPointerException e
) {
493 if (!collectionFound
) {
494 collection
.setCode(NB(dataHolder
.collectionCode
));
495 collection
.setCodeStandard(NB("GBIF"));
496 collection
.setInstitute(institution
);
504 * join DeterminationEvent to the Taxon Object
505 * @param taxon current Taxon Object
506 * @param preferredFlag preferred name, boolean
507 * @param config current ABCD Import configurator
510 private void linkDeterminationEvent(Abcd206ImportState state
, Taxon taxon
, boolean preferredFlag
, DerivedUnitFacade derivedFacade
) {
511 Abcd206ImportConfigurator config
= state
.getConfig();
514 logger
.info("start linkdetermination with taxon:" + taxon
.getUuid()+", "+taxon
);
517 // refreshTransaction(state);
520 taxon
= (Taxon
) getTaxonService().find(taxon
.getUuid());
524 DeterminationEvent determinationEvent
= DeterminationEvent
.NewInstance();
525 determinationEvent
.setTaxon(taxon
);
526 determinationEvent
.setPreferredFlag(preferredFlag
);
528 determinationEvent
.setIdentifiedUnit(derivedUnitBase
);
530 derivedUnitBase
.addDetermination(determinationEvent
);
531 // refreshTransaction(state);
535 logger
.info("NB TYPES INFO: "+ dataHolder
.statusList
.size());
537 for (SpecimenTypeDesignationStatus specimenTypeDesignationstatus
: dataHolder
.statusList
) {
538 if (specimenTypeDesignationstatus
!= null) {
540 logger
.info("specimenTypeDesignationstatus :"+ specimenTypeDesignationstatus
);
543 taxon
= (Taxon
) getTaxonService().find(taxon
.getUuid());
545 specimenTypeDesignationstatus
= (SpecimenTypeDesignationStatus
) getTermService().find(specimenTypeDesignationstatus
.getUuid());
547 TaxonNameBase
<?
,?
> name
= taxon
.getName();
548 SpecimenTypeDesignation designation
= SpecimenTypeDesignation
.NewInstance();
550 designation
.setTypeStatus(specimenTypeDesignationstatus
);
551 designation
.setTypeSpecimen(derivedUnitBase
);
552 name
.addTypeDesignation(designation
, true);
553 refreshTransaction(state
);
557 } catch (Exception e
) {
558 logger
.warn("PB addding SpecimenType " + e
);
561 for (String strReference
: dataHolder
.referenceList
) {
562 if (isNotBlank(strReference
)){
563 Reference
<?
> reference
= ReferenceFactory
.newGeneric();
564 reference
.setTitleCache(strReference
, true);
565 getReferenceService().saveOrUpdate(reference
);
567 determinationEvent
.addReference(reference
);
571 getOccurrenceService().saveOrUpdate(derivedUnitBase
);
572 refreshTransaction(state
);
575 if (config
.isDoCreateIndividualsAssociations()) {
576 if(DEBUG
){ logger
.info("isDoCreateIndividualsAssociations");}
578 makeIndividualsAssociation(state
, taxon
, determinationEvent
);
579 getOccurrenceService().saveOrUpdate(derivedUnitBase
);
583 private void makeIndividualsAssociation(Abcd206ImportState state
, Taxon taxon
, DeterminationEvent determinationEvent
) {
586 taxon
= (Taxon
) getTaxonService().find(taxon
.getUuid());
588 TaxonDescription taxonDescription
= getTaxonDescription(taxon
, ref
, false, true);
589 taxonDescription
.setTitleCache(ref
.getTitleCache(), true);
590 taxon
.addDescription(taxonDescription
);
592 IndividualsAssociation indAssociation
= IndividualsAssociation
.NewInstance();
593 Feature feature
= makeFeature(derivedUnitBase
);
594 indAssociation
.setAssociatedSpecimenOrObservation(derivedUnitBase
);
595 indAssociation
.setFeature(feature
);
597 for (Reference
<?
> citation
: determinationEvent
.getReferences()) {
598 indAssociation
.addSource(DescriptionElementSource
.NewInstance(OriginalSourceType
.PrimaryTaxonomicSource
,
599 null, null, citation
, null));
602 taxonDescription
.addElement(indAssociation
);
603 taxonDescription
.setTaxon(taxon
);
605 getDescriptionService().saveOrUpdate(taxonDescription
);
606 getTaxonService().saveOrUpdate(taxon
);
609 private Feature
makeFeature(SpecimenOrObservationBase
<?
> unit
) {
610 if (unit
.isInstanceOf(DerivedUnit
.class)) {
611 return Feature
.INDIVIDUALS_ASSOCIATION();
613 else if (unit
.isInstanceOf(FieldObservation
.class) || unit
.isInstanceOf(Observation
.class)) {
614 return Feature
.OBSERVATION();
616 else if (unit
.isInstanceOf(Fossil
.class) || unit
.isInstanceOf(LivingBeing
.class) || unit
.isInstanceOf(Specimen
.class)) {
617 return Feature
.SPECIMEN();
619 logger
.warn("No feature defined for derived unit class: " + unit
.getClass().getSimpleName());
623 private void refreshTransaction(Abcd206ImportState state
){
624 commitTransaction(state
.getTx());
625 state
.setTx(startTransaction());
628 ref
= getReferenceService().find(ref
.getUuid());
630 if (classification
!= null){
631 classification
= getClassificationService().find(classification
.getUuid());
633 if (derivedUnitBase
!= null){
634 derivedUnitBase
= (DerivedUnitBase
<?
>) getOccurrenceService().find(derivedUnitBase
.getUuid());
638 state
.setSuccess(false);
639 rollbackTransaction(state
.getTx());
647 * getTaxon : search for an existing taxon in the database, for the same
651 * @param scientificName
652 * @param preferredFlag
654 * @param taxonnametoinsert
655 * @param preferredtaxonnametoinsert
659 private Taxon
getTaxon(Abcd206ImportState state
, String scientificName
, int i
, Rank rank
, String nomenclature
) {
660 Abcd206ImportConfigurator config
= state
.getConfig();
663 System
.out
.println("getTaxon "+scientificName
);
665 System
.out
.println("getTaxon "+scientificName
+", "+rank
.generateTitle());
668 NonViralName
<?
> taxonName
= null;
670 if (config
.isDoReUseTaxon()){
671 List
<TaxonBase
> c
= null;
673 Taxon cc
= getTaxonService().findBestMatchingTaxon(scientificName
);
674 if (cc
!= null && cc
.getSec()!=null && cc
.getSec().getTitleCache().equalsIgnoreCase(ref
.getTitleCache())){
678 c
= getTaxonService().searchTaxaByName(scientificName
, ref
);
679 for (TaxonBase
<?
> b
: c
) {
683 } catch (Exception e
) {
684 logger
.info("Searchtaxabyname failed" + e
);
688 if (!config
.isDoReUseTaxon() || taxon
== null){
689 System
.out
.println("create new taxonName instance");
690 if (config
.isDoAutomaticParsing()){
691 taxonName
= parseScientificName(scientificName
);
694 if (i
>=0 && (dataHolder
.atomisedIdentificationList
!= null || dataHolder
.atomisedIdentificationList
.size() > 0)) {
695 taxonName
= setTaxonNameByType(dataHolder
.atomisedIdentificationList
.get(i
), scientificName
);
700 if(taxonName
== null){
701 taxonName
= NonViralName
.NewInstance(rank
);
702 taxonName
.setFullTitleCache(scientificName
,true);
703 taxonName
.setTitleCache(scientificName
, true);
705 System
.out
.println("ADD NEW TAXON *"+taxonName
.getRank()+"*"+taxonName
);
706 if (rank
!= null && (taxonName
.getRank() ==null || taxonName
.getRank().toString().trim().isEmpty())) {
707 taxonName
.setRank(rank
);
709 getNameService().save(taxonName
);
710 taxon
= Taxon
.NewInstance(taxonName
, ref
); //sec set null
711 getTaxonService().save(taxon
);
712 refreshTransaction(state
);
713 taxon
= (Taxon
) getTaxonService().find(taxon
.getUuid());
718 System
.out
.println("taxon.getUuid() suite :"+taxon
.getUuid());
725 * getParentTaxon : get the taxonomic hierarchy for the current Taxon
729 * @param originalName
731 * @return a map with the parenttaxon and the parenttaxonname
733 private HashMap
<Taxon
, NonViralName
<?
>> getParentTaxon(Abcd206ImportState state
, Taxon taxon
, NonViralName
<?
> taxonName
, NonViralName
<?
> originalName
) {
735 Abcd206ImportConfigurator config
= state
.getConfig();
736 Taxon parenttaxon
= null;
737 NonViralName
<?
> parentName
= null;
738 List
<TaxonBase
> c
= null;
740 List
<String
> highername
= new ArrayList
<String
>();
741 Rank higherrank
= null;
742 Rank taxonrank
= taxonName
.getRank();
744 if(DEBUG
) logger
.info("getParentTaxon childname " + taxonName
.getFullTitleCache() + ", rank " + taxonrank
+ ", originalname " + originalName
.getFullTitleCache());
746 HashMap
<Taxon
, NonViralName
<?
>> map
= new HashMap
<Taxon
, NonViralName
<?
>>();
749 refreshTransaction(state
);
751 taxon
= (Taxon
) getTaxonService().find(taxon
.getUuid());
753 if (taxonrank
.isGenus()) {
754 // to change and add test DoReusetaxa
755 for (TaxonNode p
: classification
.getAllNodes()) {
756 if(DEBUG
) logger
.info("p UUID "+p
.getUuid().toString());
757 if (classification
.getTopmostNode(p
.getTaxon()) == null) {
759 logger
.info("taxon1 "+p
.getTaxon().getTitleCache());
760 logger
.info("taxon2 "+taxon
.getTitleCache());
762 if (taxon
.getTitleCache().contains(p
.getTaxon().getTitleCache().split(SEC
+ ref
)[0])) {
763 this.addParentChild(state
, p
.getTaxon(), taxon
);
765 refreshTransaction(state
);
766 taxon
= (Taxon
) getTaxonService().find(taxon
.getUuid());
772 // add the genus to the root of the classification
773 TaxonNode p
= this.addChildTaxon(state
, taxon
);
774 this.addChildNode(state
, p
);
776 refreshTransaction(state
);
778 taxon
= (Taxon
) getTaxonService().find(taxon
.getUuid());
783 else if (taxonrank
.isInfraGeneric()) {
784 if(DEBUG
) logger
.info("isInfrageneric");
785 highername
.add(originalName
.getGenusOrUninomial());
786 higherrank
= Rank
.GENUS();
789 else if (taxonrank
.isSpecies()) {
790 if(DEBUG
) logger
.info("isSpecies");
791 if (originalName
.getGenusOrUninomial() != null) {
792 highername
.add(originalName
.getGenusOrUninomial());
793 higherrank
= Rank
.GENUS();
796 highername
.add(originalName
.getFullTitleCache().trim().split(" ")[0]);
797 higherrank
= Rank
.GENUS();
799 if (originalName
.getInfraGenericEpithet() != null) {
800 highername
.add(originalName
.getInfraGenericEpithet());
801 higherrank
= Rank
.INFRAGENUS();
805 else if (taxonrank
.isInfraSpecific()) {
806 if(DEBUG
) logger
.info("isInfraSpecies");
807 if (originalName
.getGenusOrUninomial() != null){
808 highername
.add(originalName
.getGenusOrUninomial());
810 if (originalName
.getInfraGenericEpithet() != null) {
811 highername
.add(originalName
.getInfraGenericEpithet());
813 if (originalName
.getSpecificEpithet() != null){
814 highername
.add(originalName
.getSpecificEpithet());
816 higherrank
= Rank
.SPECIES();
819 String highernamestr
= StringUtils
.join(highername
.iterator(), " ").split(SEC
+ ref
.getTitleCache())[0].trim();
820 if(DEBUG
) logger
.info("higherNamest :: " + highernamestr
);
821 if (config
.isDoReUseTaxon() && highername
.size() > 0 && isNotBlank(highernamestr
)) {
822 boolean parentFound
= false;
824 c
= getTaxonService().searchTaxaByName(highernamestr
, ref
);
826 for (TaxonBase
<?
> b
: c
) {
827 parenttaxon
= (Taxon
) b
;
828 Iterator
<TaxonNode
> it
= parenttaxon
.getTaxonNodes().iterator();
829 // logger.warn("ICI2");
830 while (it
.hasNext()) {
831 TaxonNode tmpNode
= it
.next();
832 Taxon tmp
= tmpNode
.getTaxon();
833 if (tmp
.getTitleCache().split(SEC
+ ref
.getTitleCache())[0].trim().equalsIgnoreCase(highernamestr
)) {
835 parentName
= (NonViralName
<?
>) b
.getName();
841 logger
.info("parent not found");
843 logger
.info("parent found: " + parenttaxon
.getTitleCache());
847 } catch (Exception e
) {
848 logger
.info("Problem while trying to reuse existing taxon" + e
);
855 if ((parenttaxon
== null && highername
.size() > 0 && isNotBlank(highernamestr
)) || !config
.isDoReUseTaxon()) {
856 // logger.info("ICI BIS");
857 parentName
= NonViralName
.NewInstance(null);
858 parentName
.setFullTitleCache(highernamestr
);
859 parentName
.setNameCache(highernamestr
);
860 parentName
.setRank(higherrank
);
861 if (higherrank
== Rank
.GENUS()){
862 parentName
.setGenusOrUninomial(highernamestr
.split(" ")[0]);
865 getNameService().save(parentName
);
866 parenttaxon
= Taxon
.NewInstance(parentName
, ref
);
867 getTaxonService().save(parenttaxon
);
871 map
.put(parenttaxon
, parentName
);
875 private TaxonNode
addChildNode(Abcd206ImportState state
, TaxonNode childNode
){
877 boolean exists
=false;
878 if (state
.getConfig().isDoReUseTaxon()){
879 Taxon taxon
= childNode
.getTaxon();
880 Set
<TaxonNode
> allNodes
= classification
.getAllNodes();
882 if (allNodes
.size()>0)
883 for (TaxonNode tn
:allNodes
){
885 if (tmp
.equals(taxon
)){
892 if(! state
.getConfig().isDoReUseTaxon() || !exists
){
893 refreshTransaction(state
);
894 childNode
= getTaxonNodeService().find(childNode
.getUuid());
895 re
= classification
.addChildNode(childNode
, ref
, "", null);
900 private void addParentChild(Abcd206ImportState state
, Taxon parent
, Taxon child
){
901 if(DEBUG
) logger
.info("addParentChild");
902 boolean exists
= false;
903 Taxon taxonFromHiber
= null;
904 if (state
.getConfig().isDoReUseTaxon()){
905 Set
<TaxonNode
> allNodes
= classification
.getAllNodes();
907 if (allNodes
.size()>0){
908 for (TaxonNode tn
:allNodes
){
910 if (tmp
.equals(parent
)){
911 taxonFromHiber
= tmp
;
915 if (taxonFromHiber
!= null){
916 Set
<TaxonNode
> children
= taxonFromHiber
.getTaxonNodes();
917 if (children
.size()>0)
918 for (TaxonNode cn
:children
){
919 Taxon tmpt
= cn
.getTaxon();
920 if (tmpt
.equals(child
))
925 if (! state
.getConfig().isDoReUseTaxon() || !exists
){
926 refreshTransaction(state
);
927 parent
= (Taxon
) getTaxonService().find(parent
.getUuid());
928 child
= (Taxon
) getTaxonService().find(child
.getUuid());
929 classification
.addParentChild(parent
,child
, ref
, null);
933 private TaxonNode
addChildTaxon(Abcd206ImportState state
,Taxon child
){
935 boolean exists
=false;
936 if (state
.getConfig().isDoReUseTaxon()){
937 Set
<TaxonNode
> allNodes
= classification
.getAllNodes();
939 if (allNodes
.size()>0){
940 for (TaxonNode tn
:allNodes
){
942 if (tmp
.equals(child
)){
950 if(! state
.getConfig().isDoReUseTaxon() || !exists
){
951 refreshTransaction(state
);
952 child
= (Taxon
) getTaxonService().find(child
.getUuid());
953 re
= classification
.addChildTaxon(child
, ref
, "", null);
961 * @param scientificName
965 private Taxon
getTaxon(Abcd206ImportState state
, String scientificName
, int index
, String nomenclature
) {
966 return getTaxon(state
, scientificName
, index
, null, nomenclature
);
970 * HandleIdentifications : get the scientific names present in the ABCD
971 * document and store link them with the observation/specimen data
975 private void handleIdentifications(Abcd206ImportState state
, DerivedUnitFacade derivedUnitFacade
) {
977 Abcd206ImportConfigurator config
= state
.getConfig();
979 String fullScientificNameString
;
984 String scientificName
= "";
985 boolean preferredFlag
= false;
986 boolean onePreferred
= false;
988 List
<String
> scientificNames
= new ArrayList
<String
>();
989 if (dataHolder
.nomenclatureCode
== ""){
990 dataHolder
.nomenclatureCode
= config
.getNomenclaturalCode().toString();
993 for (int i
= 0; i
< dataHolder
.identificationList
.size(); i
++) {
995 fullScientificNameString
= dataHolder
.identificationList
.get(i
);
996 fullScientificNameString
= fullScientificNameString
.replaceAll(" et ", " & ");
998 if (fullScientificNameString
.indexOf(PREFERRED
) != -1) {
999 scientificName
= fullScientificNameString
.split(PREFERRED
)[0];
1000 String pTmp
= fullScientificNameString
.split(PREFERRED
)[1].split(CODE
)[0];
1001 if (pTmp
.equals("1") || pTmp
.toLowerCase().indexOf("true") != -1) {
1002 preferredFlag
= true;
1003 onePreferred
= true;
1006 preferredFlag
= false;
1010 scientificName
= fullScientificNameString
;
1013 logger
.info("fullscientificname " + fullScientificNameString
+ ", *" + dataHolder
.nomenclatureCode
+ "*");
1015 if (fullScientificNameString
.indexOf(CODE
) != -1) {
1016 if (fullScientificNameString
.indexOf(':') != -1) {
1017 dataHolder
.nomenclatureCode
= fullScientificNameString
.split(CODE
)[1].split(COLON
)[1];
1020 dataHolder
.nomenclatureCode
= fullScientificNameString
.split(CODE
)[1];
1023 scientificNames
.add(scientificName
+SPLITTER
+preferredFlag
+SPLITTER
+i
);
1025 for (String name
:scientificNames
) {
1026 scientificName
= name
.split(SPLITTER
)[0];
1027 String pref
= name
.split(SPLITTER
)[1];
1028 String index
= name
.split(SPLITTER
)[2];
1029 if (pref
.equalsIgnoreCase("true") || scientificNames
.size()==1) {
1030 preferredFlag
= true;
1032 preferredFlag
=false;
1034 taxon
= getTaxon(state
, scientificName
,Integer
.parseInt(index
),dataHolder
.nomenclatureCode
);
1035 addTaxonNode(taxon
, state
,dataHolder
.nomenclatureCode
);
1036 taxon
= (Taxon
) getTaxonService().find(taxon
.getUuid());
1037 linkDeterminationEvent(state
, taxon
, preferredFlag
, derivedUnitFacade
);
1038 // refreshTransaction();
1040 refreshTransaction(state
);
1041 taxon
= (Taxon
) getTaxonService().find(taxon
.getUuid());
1049 private void addTaxonNode(Taxon taxon
, Abcd206ImportState state
, String nomenclature
) {
1050 logger
.info("link taxon to a taxonNode");
1051 boolean exist
= false;
1052 for (TaxonNode p
: classification
.getAllNodes()){
1053 if(p
.getTaxon().equals(taxon
)) {
1058 addParentTaxon(taxon
, state
,nomenclature
);
1060 refreshTransaction(state
);
1063 private void addParentTaxon(Taxon taxon
, Abcd206ImportState state
,String nomenclature
){
1064 System
.out
.println("addParentTaxon "+taxon
.getTitleCache());
1066 NonViralName
<?
> nvname
= CdmBase
.deproxy(taxon
.getName(), NonViralName
.class);
1067 Rank rank
= nvname
.getRank();
1069 Taxon subgenus
=null;
1070 Taxon species
= null;
1071 Taxon subspecies
= null;
1072 if (rank
.isLower(Rank
.GENUS() )){
1073 String prefix
= nvname
.getGenusOrUninomial();
1074 genus
= getTaxon(state
, prefix
, -1, Rank
.GENUS(),nomenclature
);
1075 saveOrUpdateClassification(null, genus
);
1077 if (rank
.isLower(Rank
.SUBGENUS())){
1078 String prefix
= nvname
.getGenusOrUninomial();
1079 String name
= nvname
.getInfraGenericEpithet();
1081 subgenus
= getTaxon(state
, prefix
+" "+name
, -1, Rank
.SUBGENUS(),nomenclature
);
1082 saveOrUpdateClassification(genus
, subgenus
);
1085 if (rank
.isLower(Rank
.SPECIES())){
1086 if (subgenus
!=null){
1087 String prefix
= nvname
.getGenusOrUninomial();
1088 String name
= nvname
.getInfraGenericEpithet();
1089 String spe
= nvname
.getSpecificEpithet();
1091 species
= getTaxon(state
, prefix
+" "+name
+" "+spe
, -1, Rank
.SPECIES(),nomenclature
);
1092 saveOrUpdateClassification(subgenus
, species
);
1096 String prefix
= nvname
.getGenusOrUninomial();
1097 String name
= nvname
.getSpecificEpithet();
1099 species
= getTaxon(state
, prefix
+" "+name
, -1, Rank
.SPECIES(),nomenclature
);
1100 saveOrUpdateClassification(genus
, species
);
1104 if (rank
.isInfraSpecific()){
1105 subspecies
= getTaxon(state
, nvname
.getFullTitleCache(), -1, Rank
.SUBSPECIES(),nomenclature
);
1106 saveOrUpdateClassification(species
, subspecies
);
1111 * @param currentTaxon
1114 private void saveOrUpdateClassification(Taxon parent
, Taxon child
) {
1115 System
.out
.println("ADD CLASSIFICATION parent child "+parent
+"," +child
);
1116 if (parent
!= null) {
1117 parent
= (Taxon
) getTaxonService().find(parent
.getUuid());
1118 child
= (Taxon
) getTaxonService().find(child
.getUuid());
1119 classification
.addParentChild(parent
, child
, ref
, "");
1121 if (parent
== null) {
1122 child
= (Taxon
) getTaxonService().find(child
.getUuid());
1123 classification
.addChildTaxon(child
, ref
, "", null);
1125 getClassificationService().saveOrUpdate(classification
);
1130 private NonViralName
<?
> parseScientificName(String scientificName
) {
1131 NonViralNameParserImpl nvnpi
= NonViralNameParserImpl
.NewInstance();
1132 NonViralName
<?
> taxonName
= null;
1133 boolean problem
= false;
1136 logger
.info("parseScientificName " + dataHolder
.nomenclatureCode
.toString());
1139 if (dataHolder
.nomenclatureCode
.toString().equals("Zoological") || dataHolder
.nomenclatureCode
.toString().contains("ICZN")) {
1140 taxonName
= nvnpi
.parseFullName(scientificName
, NomenclaturalCode
.ICZN
, null);
1141 if (taxonName
.hasProblem()) {
1145 if (dataHolder
.nomenclatureCode
.toString().equals("Botanical") || dataHolder
.nomenclatureCode
.toString().contains("ICBN")) {
1146 taxonName
= nvnpi
.parseFullName(scientificName
, NomenclaturalCode
.ICNAFP
, null);
1147 if (taxonName
.hasProblem()) {
1151 if (dataHolder
.nomenclatureCode
.toString().equals("Bacterial") || dataHolder
.nomenclatureCode
.toString().contains("ICBN")) {
1152 taxonName
= nvnpi
.parseFullName(scientificName
, NomenclaturalCode
.ICNB
, null);
1153 if (taxonName
.hasProblem()) {
1157 if (dataHolder
.nomenclatureCode
.toString().equals("Cultivar") || dataHolder
.nomenclatureCode
.toString().contains("ICNCP")) {
1158 taxonName
= nvnpi
.parseFullName(scientificName
, NomenclaturalCode
.ICNCP
, null);
1159 if (taxonName
.hasProblem()) {
1164 logger
.info("Parsing with problem in parseScientificName " + scientificName
);
1171 private NonViralName
<?
> setTaxonNameByType(
1172 HashMap
<String
, String
> atomisedMap
, String fullName
) {
1173 boolean problem
= false;
1174 if(DEBUG
) logger
.info("settaxonnamebytype " + dataHolder
.nomenclatureCode
.toString());
1176 if (dataHolder
.nomenclatureCode
.equals("Zoological")) {
1177 NonViralName
<ZoologicalName
> taxonName
= ZoologicalName
.NewInstance(null);
1178 taxonName
.setFullTitleCache(fullName
, true);
1179 taxonName
.setGenusOrUninomial(NB(getFromMap(atomisedMap
, "Genus")));
1180 taxonName
.setInfraGenericEpithet(NB(getFromMap(atomisedMap
, "SubGenus")));
1181 taxonName
.setSpecificEpithet(NB(getFromMap(atomisedMap
,"SpeciesEpithet")));
1182 taxonName
.setInfraSpecificEpithet(NB(getFromMap(atomisedMap
,"SubspeciesEpithet")));
1184 if (taxonName
.getGenusOrUninomial() != null){
1185 taxonName
.setRank(Rank
.GENUS());
1188 else if (taxonName
.getInfraGenericEpithet() != null){
1189 taxonName
.setRank(Rank
.SUBGENUS());
1192 else if (taxonName
.getSpecificEpithet() != null){
1193 taxonName
.setRank(Rank
.SPECIES());
1196 else if (taxonName
.getInfraSpecificEpithet() != null){
1197 taxonName
.setRank(Rank
.SUBSPECIES());
1201 if (getFromMap(atomisedMap
, "AuthorTeamParenthesis") != null) {
1202 team
= Team
.NewInstance();
1203 team
.setTitleCache(getFromMap(atomisedMap
, "AuthorTeamParenthesis"), true);
1206 if (getFromMap(atomisedMap
, "AuthorTeamAndYear") != null) {
1207 team
= Team
.NewInstance();
1208 team
.setTitleCache(getFromMap(atomisedMap
, "AuthorTeamAndYear"), true);
1212 taxonName
.setBasionymAuthorTeam(team
);
1215 if (getFromMap(atomisedMap
, "AuthorTeamParenthesis") != null) {
1216 taxonName
.setAuthorshipCache(getFromMap(atomisedMap
, "AuthorTeamParenthesis"));
1218 else if (getFromMap(atomisedMap
, "AuthorTeamAndYear") != null) {
1219 taxonName
.setAuthorshipCache(getFromMap(atomisedMap
, "AuthorTeamAndYear"));
1222 if (getFromMap(atomisedMap
, "CombinationAuthorTeamAndYear") != null) {
1223 team
= Team
.NewInstance();
1224 team
.setTitleCache(getFromMap(atomisedMap
, "CombinationAuthorTeamAndYear"), true);
1225 taxonName
.setCombinationAuthorTeam(team
);
1227 if (taxonName
.hasProblem()) {
1228 logger
.info("pb ICZN");
1235 else if (dataHolder
.nomenclatureCode
.equals("Botanical")) {
1236 BotanicalName taxonName
= (BotanicalName
) parseScientificName(fullName
);
1237 if (taxonName
!= null){
1241 taxonName
= BotanicalName
.NewInstance(null);
1243 taxonName
.setFullTitleCache(fullName
, true);
1244 taxonName
.setGenusOrUninomial(NB(getFromMap(atomisedMap
, "Genus")));
1245 taxonName
.setInfraGenericEpithet(NB(getFromMap(atomisedMap
, "FirstEpithet")));
1246 taxonName
.setInfraSpecificEpithet(NB(getFromMap(atomisedMap
, "InfraSpeEpithet")));
1248 taxonName
.setRank(Rank
.getRankByName(getFromMap(atomisedMap
, "Rank")));
1249 } catch (Exception e
) {
1250 if (taxonName
.getGenusOrUninomial() != null){
1251 taxonName
.setRank(Rank
.GENUS());
1253 else if (taxonName
.getInfraGenericEpithet() != null){
1254 taxonName
.setRank(Rank
.SUBGENUS());
1256 else if (taxonName
.getSpecificEpithet() != null){
1257 taxonName
.setRank(Rank
.SPECIES());
1259 else if (taxonName
.getInfraSpecificEpithet() != null){
1260 taxonName
.setRank(Rank
.SUBSPECIES());
1264 if (getFromMap(atomisedMap
, "AuthorTeamParenthesis") != null) {
1265 team
= Team
.NewInstance();
1266 team
.setTitleCache(getFromMap(atomisedMap
, "AuthorTeamParenthesis"), true);
1267 taxonName
.setBasionymAuthorTeam(team
);
1269 if (getFromMap(atomisedMap
, "AuthorTeam") != null) {
1270 team
= Team
.NewInstance();
1271 team
.setTitleCache(getFromMap(atomisedMap
, "AuthorTeam"), true);
1272 taxonName
.setCombinationAuthorTeam(team
);
1275 if (getFromMap(atomisedMap
, "AuthorTeamParenthesis") != null) {
1276 taxonName
.setAuthorshipCache(getFromMap(atomisedMap
, "AuthorTeamParenthesis"));
1278 else if (getFromMap(atomisedMap
, "AuthorTeam") != null) {
1279 taxonName
.setAuthorshipCache(getFromMap(atomisedMap
, "AuthorTeam"));
1282 if (getFromMap(atomisedMap
, "CombinationAuthorTeamAndYear") != null) {
1283 team
= Team
.NewInstance();
1284 team
.setTitleCache(getFromMap(atomisedMap
, "CombinationAuthorTeamAndYear"), true);
1285 taxonName
.setCombinationAuthorTeam(team
);
1287 if (taxonName
.hasProblem()) {
1288 logger
.info("pb ICBN");
1295 else if (dataHolder
.nomenclatureCode
.equals("Bacterial")) {
1296 NonViralName
<BacterialName
> taxonName
= BacterialName
.NewInstance(null);
1297 taxonName
.setFullTitleCache(fullName
, true);
1298 taxonName
.setGenusOrUninomial(getFromMap(atomisedMap
, "Genus"));
1299 taxonName
.setInfraGenericEpithet(NB(getFromMap(atomisedMap
, "SubGenus")));
1300 taxonName
.setSpecificEpithet(NB(getFromMap(atomisedMap
, "Species")));
1301 taxonName
.setInfraSpecificEpithet(NB(getFromMap(atomisedMap
, "SubspeciesEpithet")));
1303 if (taxonName
.getGenusOrUninomial() != null){
1304 taxonName
.setRank(Rank
.GENUS());
1306 else if (taxonName
.getInfraGenericEpithet() != null){
1307 taxonName
.setRank(Rank
.SUBGENUS());
1309 else if (taxonName
.getSpecificEpithet() != null){
1310 taxonName
.setRank(Rank
.SPECIES());
1312 else if (taxonName
.getInfraSpecificEpithet() != null){
1313 taxonName
.setRank(Rank
.SUBSPECIES());
1316 if (getFromMap(atomisedMap
, "AuthorTeamAndYear") != null) {
1317 Team team
= Team
.NewInstance();
1318 team
.setTitleCache(getFromMap(atomisedMap
, "AuthorTeamAndYear"), true);
1319 taxonName
.setCombinationAuthorTeam(team
);
1321 if (getFromMap(atomisedMap
, "ParentheticalAuthorTeamAndYear") != null) {
1322 Team team
= Team
.NewInstance();
1323 team
.setTitleCache(getFromMap(atomisedMap
, "ParentheticalAuthorTeamAndYear"), true);
1324 taxonName
.setBasionymAuthorTeam(team
);
1326 if (taxonName
.hasProblem()) {
1327 logger
.info("pb ICNB");
1334 else if (dataHolder
.nomenclatureCode
.equals("Cultivar")) {
1335 CultivarPlantName taxonName
= CultivarPlantName
.NewInstance(null);
1337 if (taxonName
.hasProblem()) {
1338 logger
.info("pb ICNCP");
1348 logger
.info("Problem im setTaxonNameByType ");
1349 NonViralName
<?
> taxonName
= NonViralName
.NewInstance(null);
1350 taxonName
.setFullTitleCache(fullName
, true);
1353 NonViralName
<?
> tn
= NonViralName
.NewInstance(null);
1357 private String
getFromMap(HashMap
<String
, String
> atomisedMap
, String key
) {
1358 String value
= null;
1359 if (atomisedMap
.containsKey(key
)) {
1360 value
= atomisedMap
.get(key
);
1364 if (value
!= null && key
.matches(".*Year.*")) {
1365 value
= value
.trim();
1366 if (value
.matches("[a-z A-Z ]*[0-9]{4}$")) {
1367 String tmp
= value
.split("[0-9]{4}$")[0];
1368 int year
= Integer
.parseInt(value
.split(tmp
)[1]);
1381 catch (Exception e
) {
1387 private void compareABCDtoCDM(URI urlFileName
, List
<String
> knownElts
, Abcd206XMLFieldGetter abcdFieldGetter
) {
1389 DocumentBuilderFactory factory
= DocumentBuilderFactory
.newInstance();
1390 DocumentBuilder constructeur
= factory
.newDocumentBuilder();
1391 URL url
= urlFileName
.toURL();
1392 Object o
= url
.getContent();
1393 InputStream is
= (InputStream
) o
;
1394 Document document
= constructeur
.parse(is
);
1395 Element root
= document
.getDocumentElement();
1396 abcdFieldGetter
.traverse(root
);
1398 catch (ParserConfigurationException e
){
1399 e
.printStackTrace();
1401 catch (SAXException e
) {
1402 e
.printStackTrace();
1404 catch (IOException e
) {
1405 e
.printStackTrace();
1407 Set
<String
> elts
= dataHolder
.allABCDelements
.keySet();
1408 Iterator
<String
> it
= elts
.iterator();
1410 while (it
.hasNext()) {
1412 if (knownElts
.indexOf(elt
) == -1) {
1413 if(DEBUG
) logger
.info("Unmerged ABCD element: " + elt
+ " - "+ dataHolder
.allABCDelements
.get(elt
));
1422 private void prepareCollectors(Abcd206ImportState state
, NodeList unitsList
, Abcd206XMLFieldGetter abcdFieldGetter
) {
1423 List
<String
> collectors
= new ArrayList
<String
>();
1424 List
<String
> teams
= new ArrayList
<String
>();
1425 List
<List
<String
>> collectorinteams
= new ArrayList
<List
<String
>>();
1427 for (int i
= 0; i
< unitsList
.getLength(); i
++) {
1428 this.getCollectorsFromXML((Element
) unitsList
.item(i
), abcdFieldGetter
);
1429 for (String agent
: dataHolder
.gatheringAgentList
) {
1430 collectors
.add(agent
);
1432 List
<String
> tmpTeam
= new ArrayList
<String
>(new HashSet
<String
>(dataHolder
.gatheringTeamList
));
1433 if(!tmpTeam
.isEmpty()) {
1434 teams
.add(StringUtils
.join(tmpTeam
.toArray()," & "));
1436 for (String agent
:tmpTeam
) {
1437 collectors
.add(agent
);
1441 List
<String
> collectorsU
= new ArrayList
<String
>(new HashSet
<String
>(collectors
));
1442 List
<String
> teamsU
= new ArrayList
<String
>(new HashSet
<String
>(teams
));
1445 //existing teams in DB
1446 Map
<String
,Team
> titleCacheTeam
= new HashMap
<String
, Team
>();
1447 List
<UuidAndTitleCache
<Team
>> hiberTeam
= getAgentService().getTeamUuidAndTitleCache();
1449 Set
<UUID
> uuids
= new HashSet
<UUID
>();
1450 for (UuidAndTitleCache
<Team
> hibernateT
:hiberTeam
){
1451 uuids
.add(hibernateT
.getUuid());
1453 if (!uuids
.isEmpty()){
1454 List
<AgentBase
> existingTeams
= getAgentService().find(uuids
);
1455 for (AgentBase
<?
> existingP
:existingTeams
){
1456 titleCacheTeam
.put(existingP
.getTitleCache(),(Team
) existingP
);
1461 Map
<String
,UUID
> teamMap
= new HashMap
<String
, UUID
>();
1462 for (UuidAndTitleCache
<Team
> uuidt
:hiberTeam
){
1463 teamMap
.put(uuidt
.getTitleCache(), uuidt
.getUuid());
1466 //existing persons in DB
1467 List
<UuidAndTitleCache
<Person
>> hiberPersons
= getAgentService().getPersonUuidAndTitleCache();
1468 Map
<String
,Person
> titleCachePerson
= new HashMap
<String
, Person
>();
1469 uuids
= new HashSet
<UUID
>();
1470 for (UuidAndTitleCache
<Person
> hibernateP
:hiberPersons
){
1471 uuids
.add(hibernateP
.getUuid());
1474 if (!uuids
.isEmpty()){
1475 List
<AgentBase
> existingPersons
= getAgentService().find(uuids
);
1476 for (AgentBase
<?
> existingP
:existingPersons
){
1477 titleCachePerson
.put(existingP
.getTitleCache(),(Person
) existingP
);
1481 Map
<String
,UUID
> personMap
= new HashMap
<String
, UUID
>();
1482 for (UuidAndTitleCache
<Person
> person
:hiberPersons
){
1483 personMap
.put(person
.getTitleCache(), person
.getUuid());
1486 java
.util
.Collection
<AgentBase
> personToadd
= new ArrayList
<AgentBase
>();
1487 java
.util
.Collection
<AgentBase
> teamToAdd
= new ArrayList
<AgentBase
>();
1489 for (String collector
:collectorsU
){
1490 Person p
= Person
.NewInstance();
1491 p
.setTitleCache(collector
,true);
1492 if (!personMap
.containsKey(p
.getTitleCache())){
1496 for (String team
:teamsU
){
1497 Team p
= Team
.NewInstance();
1498 p
.setTitleCache(team
,true);
1499 if (!teamMap
.containsKey(p
.getTitleCache())){
1504 if(!personToadd
.isEmpty()){
1505 Map
<UUID
, AgentBase
> uuuidPerson
= getAgentService().save(personToadd
);
1506 for (UUID u
:uuuidPerson
.keySet()){
1507 titleCachePerson
.put(uuuidPerson
.get(u
).getTitleCache(),(Person
) uuuidPerson
.get(u
) );
1512 Map
<String
,Integer
>teamdone
= new HashMap
<String
, Integer
>();
1513 for (List
<String
> collteam
: collectorinteams
){
1514 if (!teamdone
.containsKey(StringUtils
.join(collteam
.toArray(),"-"))){
1515 Team team
= new Team();
1517 for (String collector
:collteam
){
1518 ptmp
= Person
.NewInstance();
1519 ptmp
.setTitleCache(collector
,true);
1520 Person p2
= titleCachePerson
.get(ptmp
.getTitleCache());
1521 team
.addTeamMember(p2
);
1525 teamToAdd
.add(team
);
1527 teamdone
.put(StringUtils
.join(collteam
.toArray(),"-"),0);
1531 if(!teamToAdd
.isEmpty()){
1532 Map
<UUID
, AgentBase
> uuuidTeam
= getAgentService().save(teamToAdd
);
1533 for (UUID u
:uuuidTeam
.keySet()){
1534 titleCacheTeam
.put(uuuidTeam
.get(u
).getTitleCache(), (Team
) uuuidTeam
.get(u
) );
1538 state
.getConfig().setTeams(titleCacheTeam
);
1539 state
.getConfig().setPersons(titleCachePerson
);
1543 protected boolean isIgnore(Abcd206ImportState state
) {