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
.InputStream
;
13 import java
.net
.MalformedURLException
;
15 import java
.util
.ArrayList
;
16 import java
.util
.HashMap
;
17 import java
.util
.HashSet
;
18 import java
.util
.List
;
21 import java
.util
.UUID
;
23 import javax
.xml
.parsers
.DocumentBuilder
;
24 import javax
.xml
.parsers
.DocumentBuilderFactory
;
26 import org
.apache
.commons
.lang
.StringUtils
;
27 import org
.apache
.log4j
.Logger
;
28 import org
.springframework
.stereotype
.Component
;
29 import org
.w3c
.dom
.Document
;
30 import org
.w3c
.dom
.Element
;
31 import org
.w3c
.dom
.NodeList
;
33 import eu
.etaxonomy
.cdm
.api
.application
.ICdmApplicationConfiguration
;
34 import eu
.etaxonomy
.cdm
.api
.facade
.DerivedUnitFacade
;
35 import eu
.etaxonomy
.cdm
.common
.UriUtils
;
36 import eu
.etaxonomy
.cdm
.io
.specimen
.SpecimenImportBase
;
37 import eu
.etaxonomy
.cdm
.io
.specimen
.SpecimenUserInteraction
;
38 import eu
.etaxonomy
.cdm
.io
.specimen
.UnitsGatheringArea
;
39 import eu
.etaxonomy
.cdm
.io
.specimen
.UnitsGatheringEvent
;
40 import eu
.etaxonomy
.cdm
.model
.agent
.AgentBase
;
41 import eu
.etaxonomy
.cdm
.model
.agent
.Institution
;
42 import eu
.etaxonomy
.cdm
.model
.agent
.Person
;
43 import eu
.etaxonomy
.cdm
.model
.agent
.Team
;
44 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
45 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTerm
;
46 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTermBase
;
47 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableSource
;
48 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
49 import eu
.etaxonomy
.cdm
.model
.common
.LanguageString
;
50 import eu
.etaxonomy
.cdm
.model
.common
.OriginalSourceBase
;
51 import eu
.etaxonomy
.cdm
.model
.common
.OriginalSourceType
;
52 import eu
.etaxonomy
.cdm
.model
.common
.UuidAndTitleCache
;
53 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
54 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementSource
;
55 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
56 import eu
.etaxonomy
.cdm
.model
.description
.IndividualsAssociation
;
57 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
58 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
59 import eu
.etaxonomy
.cdm
.model
.media
.Media
;
60 import eu
.etaxonomy
.cdm
.model
.name
.BacterialName
;
61 import eu
.etaxonomy
.cdm
.model
.name
.BotanicalName
;
62 import eu
.etaxonomy
.cdm
.model
.name
.CultivarPlantName
;
63 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCode
;
64 import eu
.etaxonomy
.cdm
.model
.name
.NonViralName
;
65 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
66 import eu
.etaxonomy
.cdm
.model
.name
.SpecimenTypeDesignation
;
67 import eu
.etaxonomy
.cdm
.model
.name
.SpecimenTypeDesignationStatus
;
68 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
69 import eu
.etaxonomy
.cdm
.model
.name
.ZoologicalName
;
70 import eu
.etaxonomy
.cdm
.model
.occurrence
.Collection
;
71 import eu
.etaxonomy
.cdm
.model
.occurrence
.DerivationEvent
;
72 import eu
.etaxonomy
.cdm
.model
.occurrence
.DerivationEventType
;
73 import eu
.etaxonomy
.cdm
.model
.occurrence
.DerivedUnit
;
74 import eu
.etaxonomy
.cdm
.model
.occurrence
.DeterminationEvent
;
75 import eu
.etaxonomy
.cdm
.model
.occurrence
.FieldUnit
;
76 import eu
.etaxonomy
.cdm
.model
.occurrence
.GatheringEvent
;
77 import eu
.etaxonomy
.cdm
.model
.occurrence
.MediaSpecimen
;
78 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
79 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationType
;
80 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
81 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceFactory
;
82 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
83 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
84 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
85 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
86 import eu
.etaxonomy
.cdm
.persistence
.query
.MatchMode
;
87 import eu
.etaxonomy
.cdm
.strategy
.parser
.NonViralNameParserImpl
;
94 public class Abcd206Import
extends SpecimenImportBase
<Abcd206ImportConfigurator
, Abcd206ImportState
> {
95 private static final Logger logger
= Logger
.getLogger(Abcd206Import
.class);
97 private final boolean DEBUG
= true;
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
= "--";
103 private static String prefix
= "";
105 //TODO make all fields ABCD206ImportState variables
106 private Classification classification
= null;
107 private Reference
<?
> ref
= null;
109 private Abcd206DataHolder dataHolder
;
110 private DerivedUnit derivedUnitBase
;
111 private FieldUnit fieldUnit
;
113 private List
<OriginalSourceBase
<?
>> associationRefs
= new ArrayList
<OriginalSourceBase
<?
>>();
114 boolean associationSourcesSet
=false;
115 private List
<OriginalSourceBase
<?
>> descriptionRefs
= new ArrayList
<OriginalSourceBase
<?
>>();
116 boolean descriptionSourcesSet
=false;
117 private List
<OriginalSourceBase
<?
>> derivedUnitSources
= new ArrayList
<OriginalSourceBase
<?
>>();
118 boolean derivedUnitSourcesSet
=false;
119 private boolean descriptionGroupSet
= false;
120 private TaxonDescription descriptionGroup
= null;
122 public Abcd206Import() {
127 * TODO: quick and dirty fix to avoid NonUniqueObjectExceptions
128 * They occurred because this import is a bean therefore a singleton but uses
129 * class variables which should be unique for every single import.
131 private void resetFields(){
132 classification
= null;
136 derivedUnitBase
= null;
139 associationRefs
= new ArrayList
<OriginalSourceBase
<?
>>();
140 associationSourcesSet
=false;
141 descriptionRefs
= new ArrayList
<OriginalSourceBase
<?
>>();
142 descriptionSourcesSet
=false;
143 derivedUnitSources
= new ArrayList
<OriginalSourceBase
<?
>>();
144 derivedUnitSourcesSet
=false;
145 descriptionGroupSet
= false;
146 descriptionGroup
= null;
150 protected boolean doCheck(Abcd206ImportState state
) {
151 logger
.warn("Checking not yet implemented for " + this.getClass().getSimpleName());
157 @SuppressWarnings("rawtypes")
158 public void doInvoke(Abcd206ImportState state
) {
161 state
.setTx(startTransaction());
162 logger
.info("INVOKE Specimen Import from ABCD2.06 XML ");
164 SpecimenUserInteraction sui
= state
.getConfig().getSpecimenUserInteraction();
166 List
<Reference
> references
= getReferenceService().list(Reference
.class, null, null, null, null);
168 if (state
.getConfig().isInteractWithUser()){
169 Map
<String
,Reference
> refMap
= new HashMap
<String
, Reference
>();
170 for (Reference reference
: references
) {
171 if (! StringUtils
.isBlank(reference
.getTitleCache())) {
172 refMap
.put(reference
.getTitleCache(),reference
);
175 ref
= sui
.askForReference(refMap
);
178 String cla
= sui
.createNewReference();
179 if (refMap
.get(cla
)!= null) {
180 ref
= refMap
.get(cla
);
182 ref
= ReferenceFactory
.newGeneric();
187 ref
= getReferenceService().find(ref
.getUuid());
191 String name
= NB(state
.getConfig().getSourceReferenceTitle());
192 for (Reference reference
: references
) {
193 if (! StringUtils
.isBlank(reference
.getTitleCache())) {
194 if (reference
.getTitleCache().equalsIgnoreCase(name
)) {
196 // System.out.println("FIND SAME REFERENCE");
201 ref
= ReferenceFactory
.newGeneric();
202 ref
.setTitle("ABCD classic");
207 state
.getConfig().setSourceReference(ref
);
209 if(state
.getConfig().getClassificationUuid()!=null){
210 //load classification from config if it exists
211 classification
= getClassificationService().load(state
.getConfig().getClassificationUuid());
213 if(classification
==null){//no existing classification was set in config
214 List
<Classification
> classificationList
= getClassificationService().list(Classification
.class, null, null, null, null);
215 //get classification via user interaction
216 if (state
.getConfig().isUseClassification() && state
.getConfig().isInteractWithUser()){
217 Map
<String
,Classification
> classMap
= new HashMap
<String
, Classification
>();
218 for (Classification tree
: classificationList
) {
219 if (! StringUtils
.isBlank(tree
.getTitleCache())) {
220 classMap
.put(tree
.getTitleCache(),tree
);
223 classification
= sui
.askForClassification(classMap
);
224 if (classification
== null){
225 String cla
= sui
.createNewClassification();
226 if (classMap
.get(cla
)!= null) {
227 classification
= classMap
.get(cla
);
229 classification
= Classification
.NewInstance(cla
, ref
, Language
.DEFAULT());
232 save(classification
, state
);
234 // create default classification
235 if (classification
== null) {
236 String name
= NB(state
.getConfig().getClassificationName());
237 for (Classification classif
: classificationList
){
238 if (classif
.getTitleCache().equalsIgnoreCase(name
) && classif
.getCitation().equals(ref
)) {
239 classification
=classif
;
240 // System.out.println("FIND SAME CLASSIF");
243 if (classification
== null){
244 classification
= Classification
.NewInstance(name
, ref
, Language
.DEFAULT());
246 // if (state.getConfig().getClassificationUuid() != null) {
247 // classification.setUuid(state.getConfig().getClassificationUuid());
249 save(classification
, state
);
253 InputStream source
= state
.getConfig().getSource();
254 NodeList unitsList
= getUnitsNodeList(source
);
256 if (unitsList
!= null) {
257 String message
= "nb units to insert: " + unitsList
.getLength();
258 logger
.info(message
);
259 updateProgress(state
, message
);
261 dataHolder
= new Abcd206DataHolder();
263 Abcd206XMLFieldGetter abcdFieldGetter
= new Abcd206XMLFieldGetter(dataHolder
, prefix
);
265 prepareCollectors(state
, unitsList
, abcdFieldGetter
);
267 associationRefs
= new ArrayList
<OriginalSourceBase
<?
>>();
268 descriptionRefs
= new ArrayList
<OriginalSourceBase
<?
>>();
269 derivedUnitSources
= new ArrayList
<OriginalSourceBase
<?
>>();
271 for (int i
= 0; i
< unitsList
.getLength(); i
++) {
272 System
.out
.println("------------------------------------------------------------------------------------------");
274 this.setUnitPropertiesXML( (Element
) unitsList
.item(i
), abcdFieldGetter
);
275 // refreshTransaction(state);
276 this.handleSingleUnit(state
);
278 // compare the ABCD elements added in to the CDM and the
279 // unhandled ABCD elements
280 //compareABCDtoCDM(sourceName, dataHolder.knownABCDelements, abcdFieldGetter);
282 // reset the ABCD elements added in CDM
283 // knownABCDelements = new ArrayList<String>();
284 dataHolder
.allABCDelements
= new HashMap
<String
, String
>();
286 if(state
.getConfig().isDeduplicateReferences()){
287 getReferenceService().deduplicate(Reference
.class, null, null);
289 if(state
.getConfig().isDeduplicateClassifications()){
290 getClassificationService().deduplicate(Classification
.class, null, null);
293 commitTransaction(state
.getTx());
298 protected NodeList
getUnitsNodeList(URI source
) {
300 InputStream is
= UriUtils
.getInputStream(source
);
301 return getUnitsNodeList(is
);
302 } catch (Exception e
) {
304 throw new RuntimeException(e
);
309 * Return the list of root nodes for an ABCD 2.06 XML file
310 * @param fileName: the file's location
311 * @return the list of root nodes ("Unit")
313 protected NodeList
getUnitsNodeList(InputStream inputStream
) {
314 NodeList unitList
= null;
316 DocumentBuilderFactory factory
= DocumentBuilderFactory
.newInstance();
317 DocumentBuilder builder
= factory
.newDocumentBuilder();
319 Document document
= builder
.parse(inputStream
);
320 Element root
= document
.getDocumentElement();
321 unitList
= root
.getElementsByTagName("Unit");
322 if (unitList
.getLength() == 0) {
323 unitList
= root
.getElementsByTagName("abcd:Unit");
326 } catch (Exception e
) {
333 * Handle a single unit
336 @SuppressWarnings("rawtypes")
337 private void handleSingleUnit(Abcd206ImportState state
) {
339 logger
.info("handleSingleUnit "+ref
);
342 updateProgress(state
, "Importing data for unit: " + dataHolder
.unitID
);
345 DerivedUnitFacade derivedUnitFacade
= getFacade();
346 derivedUnitBase
= derivedUnitFacade
.innerDerivedUnit();
347 fieldUnit
= derivedUnitFacade
.getFieldUnit(true);
353 ICdmApplicationConfiguration cdmAppController
= state
.getConfig().getCdmAppController();
354 if(cdmAppController
==null){
355 cdmAppController
= this;
357 UnitsGatheringEvent unitsGatheringEvent
= new UnitsGatheringEvent(cdmAppController
.getTermService(),
358 dataHolder
.locality
, dataHolder
.languageIso
, dataHolder
.longitude
, dataHolder
.latitude
,
359 dataHolder
.gatheringElevationText
, dataHolder
.gatheringElevationMin
,
360 dataHolder
.gatheringElevationMax
, dataHolder
.gatheringElevationUnit
, dataHolder
.gatheringDateText
,
361 dataHolder
.gatheringAgentList
, dataHolder
.gatheringTeamList
, state
.getConfig());
364 UnitsGatheringArea unitsGatheringArea
= new UnitsGatheringArea();
365 // unitsGatheringArea.setConfig(state.getConfig(),getOccurrenceService(), getTermService());
366 unitsGatheringArea
.setParams(dataHolder
.isocountry
, dataHolder
.country
, state
.getConfig(), cdmAppController
.getTermService(), getOccurrenceService());
368 DefinedTermBase
<?
> areaCountry
= unitsGatheringArea
.getCountry();
371 unitsGatheringArea
= new UnitsGatheringArea();
372 // unitsGatheringArea.setConfig(state.getConfig(),getOccurrenceService(),getTermService());
373 unitsGatheringArea
.setAreas(dataHolder
.namedAreaList
,state
.getConfig(), cdmAppController
.getTermService());
374 ArrayList
<DefinedTermBase
> nas
= unitsGatheringArea
.getAreas();
375 for (DefinedTermBase namedArea
: nas
) {
376 unitsGatheringEvent
.addArea(namedArea
);
379 // copy gathering event to facade
380 GatheringEvent gatheringEvent
= unitsGatheringEvent
.getGatheringEvent();
381 derivedUnitFacade
.setLocality(gatheringEvent
.getLocality());
382 derivedUnitFacade
.setExactLocation(gatheringEvent
.getExactLocation());
383 derivedUnitFacade
.setCollector(gatheringEvent
.getCollector());
384 derivedUnitFacade
.setCountry((NamedArea
)areaCountry
);
385 derivedUnitFacade
.setAbsoluteElevationText(gatheringEvent
.getAbsoluteElevationText());
386 derivedUnitFacade
.setAbsoluteElevation(gatheringEvent
.getAbsoluteElevation());
387 derivedUnitFacade
.setAbsoluteElevationMax(gatheringEvent
.getAbsoluteElevationMax());
388 derivedUnitFacade
.setGatheringPeriod(gatheringEvent
.getTimeperiod());
390 for(DefinedTermBase
<?
> area
:unitsGatheringArea
.getAreas()){
391 derivedUnitFacade
.addCollectingArea((NamedArea
) area
);
393 // derivedUnitFacade.addCollectingAreas(unitsGatheringArea.getAreas());
397 derivedUnitFacade
.setFieldNumber(NB(dataHolder
.fieldNumber
));
399 // //add Multimedia URLs
400 if (dataHolder
.multimediaObjects
.size() != -1) {
401 for (String multimediaObject
: dataHolder
.multimediaObjects
) {
404 media
= getImageMedia(multimediaObject
, READ_MEDIA_DATA
);
405 derivedUnitFacade
.addDerivedUnitMedia(media
);
406 if(state
.getConfig().isAddMediaAsMediaSpecimen()){
407 //add media also as specimen scan
408 MediaSpecimen mediaSpecimen
= MediaSpecimen
.NewInstance(SpecimenOrObservationType
.Media
);
409 mediaSpecimen
.setMediaSpecimen(media
);
410 DefinedTermBase specimenScanTerm
= getTermService().load(UUID
.fromString("acda15be-c0e2-4ea8-8783-b9b0c4ad7f03"));
411 if(specimenScanTerm
instanceof DefinedTerm
){
412 mediaSpecimen
.setKindOfUnit((DefinedTerm
) specimenScanTerm
);
414 DerivationEvent derivationEvent
= DerivationEvent
.NewInstance(DerivationEventType
.PREPARATION());
415 derivationEvent
.addDerivative(mediaSpecimen
);
416 derivedUnitFacade
.innerDerivedUnit().addDerivationEvent(derivationEvent
);
419 } catch (MalformedURLException e
) {
420 // TODO Auto-generated catch block
428 // * merge AND STORE DATA
430 // getTermService().saveOrUpdate(areaCountry);// TODO save area sooner
432 // for (NamedArea area : otherAreas) {
433 // getTermService().saveOrUpdate(area);// merge it sooner (foreach area)
436 save(unitsGatheringEvent
.getLocality(), state
);
438 // handle collection data
439 setCollectionData(state
, derivedUnitFacade
);
442 SpecimenUserInteraction sui
= state
.getConfig().getSpecimenUserInteraction();
443 Map
<String
,OriginalSourceBase
<?
>> sourceMap
= new HashMap
<String
, OriginalSourceBase
<?
>>();
445 dataHolder
.docSources
= new ArrayList
<String
>();
446 for (String
[] fullReference
: dataHolder
.referenceList
) {
447 String strReference
=fullReference
[0];
448 String citationDetail
= fullReference
[1];
449 String citationURL
= fullReference
[2];
451 if (!citationURL
.isEmpty()) {
452 citationDetail
+=", "+citationURL
;
455 Reference
<?
> reference
;
456 if(strReference
.equals(ref
.getTitleCache())){
460 reference
= ReferenceFactory
.newGeneric();
461 reference
.setTitle(strReference
);
464 IdentifiableSource sour
= getIdentifiableSource(reference
,citationDetail
);
467 if (sour
.getCitation() != null){
468 if(StringUtils
.isNotBlank(sour
.getCitationMicroReference())) {
469 dataHolder
.docSources
.add(sour
.getCitation().getTitleCache()+ "---"+sour
.getCitationMicroReference());
471 dataHolder
.docSources
.add(sour
.getCitation().getTitleCache());
477 reference
.addSource(sour
);
478 save(reference
, state
);
481 List
<IdentifiableSource
> issTmp
= getCommonService().list(IdentifiableSource
.class, null, null, null, null);
482 List
<DescriptionElementSource
> issTmp2
= getCommonService().list(DescriptionElementSource
.class, null, null, null, null);
484 Set
<OriginalSourceBase
> osbSet
= new HashSet
<OriginalSourceBase
>();
486 osbSet
.addAll(issTmp2
);
489 osbSet
.addAll(issTmp
);
493 for( OriginalSourceBase
<?
> osb
:osbSet
) {
494 if(osb
.getCitationMicroReference() !=null && !osb
.getCitationMicroReference().isEmpty()) {
496 sourceMap
.put(osb
.getCitation().getTitleCache()+ "---"+osb
.getCitationMicroReference(),osb
);
497 }catch(NullPointerException e
){logger
.warn("null pointer problem (no ref?) with "+osb
);}
500 sourceMap
.put(osb
.getCitation().getTitleCache(),osb
);
501 }catch(NullPointerException e
){logger
.warn("null pointer problem (no ref?) with "+osb
);}
505 if( state
.getConfig().isInteractWithUser()){
506 List
<OriginalSourceBase
<?
>>sources
=null;
507 if(!derivedUnitSourcesSet
){
508 sources
= sui
.askForSource(sourceMap
, "the unit itself","",getReferenceService(), dataHolder
.docSources
);
509 derivedUnitSources
=sources
;
510 derivedUnitSourcesSet
=true;
513 sources
=derivedUnitSources
;
515 // System.out.println("nb sources: "+sources.size());
516 // System.out.println("derivedunitfacade : "+derivedUnitFacade.getTitleCache());
517 for (OriginalSourceBase
<?
> sour
:sources
){
518 if(sour
.isInstanceOf(IdentifiableSource
.class)){
519 if(sourceNotLinkedToElement(derivedUnitFacade
,sour
)) {
520 // System.out.println("add source to derivedunitfacade1 "+derivedUnitFacade.getTitleCache());
521 derivedUnitFacade
.addSource((IdentifiableSource
)sour
.clone());
524 if(sourceNotLinkedToElement(derivedUnitFacade
,sour
)) {
525 // System.out.println("add source to derivedunitfacade2 "+derivedUnitFacade.getTitleCache());
526 derivedUnitFacade
.addSource(OriginalSourceType
.Import
,sour
.getCitation(),sour
.getCitationMicroReference(), ioName
);
531 for (OriginalSourceBase
<?
> sr
: sourceMap
.values()){
532 if(sr
.isInstanceOf(IdentifiableSource
.class)){
533 if(sourceNotLinkedToElement(derivedUnitFacade
,sr
)) {
534 // System.out.println("add source to derivedunitfacade3 "+derivedUnitFacade.getTitleCache());
535 derivedUnitFacade
.addSource((IdentifiableSource
)sr
.clone());
538 if(sourceNotLinkedToElement(derivedUnitFacade
,sr
)) {
539 // System.out.println("add source to derivedunitfacade4 "+derivedUnitFacade.getTitleCache());
540 derivedUnitFacade
.addSource(OriginalSourceType
.Import
,sr
.getCitation(),sr
.getCitationMicroReference(), ioName
);
546 save(derivedUnitBase
, state
);
548 // handle identifications
549 handleIdentifications(state
, derivedUnitFacade
);
552 logger
.info("saved ABCD specimen ...");
555 } catch (Exception e
) {
556 logger
.warn("Error when reading record!!");
558 state
.setUnsuccessfull();
565 * @param derivedUnitFacade
569 private boolean sourceNotLinkedToElement(DerivedUnitFacade derivedUnitFacade
, OriginalSourceBase
<?
> source
) {
570 Set
<IdentifiableSource
> linkedSources
= derivedUnitFacade
.getSources();
571 for (IdentifiableSource is
:linkedSources
){
572 if (is
.getCitation()!=null && source
.getCitation()!=null &&
573 is
.getCitation().getTitleCache().equalsIgnoreCase(source
.getCitation().getTitleCache())){
574 String isDetail
= is
.getCitationMicroReference();
575 if ((StringUtils
.isBlank(isDetail
) && StringUtils
.isBlank(source
.getCitationMicroReference()))
576 || (isDetail
!= null && isDetail
.equalsIgnoreCase(source
.getCitationMicroReference())) ) {
586 * @param citationDetail
589 //FIXME this method is highly critical, because
590 // * it will have serious performance and memory problems with large databases
591 // (databases may easily have >1 Mio source records)
592 // * it does not make sense to search for existing sources and then clone them
593 // we need to search for existing references instead and use them (if exist)
594 // for our new source.
595 private IdentifiableSource
getIdentifiableSource(Reference
<?
> reference
, String citationDetail
) {
597 List
<IdentifiableSource
> issTmp
= getCommonService().list(IdentifiableSource
.class, null, null, null, null);
600 if (reference
!= null){
602 for (OriginalSourceBase
<?
> osb
: issTmp
){
603 if (osb
.getCitation() != null && reference
!=null && osb
.getCitation().getTitleCache().equalsIgnoreCase(reference
.getTitleCache())){
604 String osbDetail
= osb
.getCitationMicroReference();
605 if ((StringUtils
.isBlank(osbDetail
) && StringUtils
.isBlank(citationDetail
))
606 || (osbDetail
!= null && osbDetail
.equalsIgnoreCase(citationDetail
)) ) {
607 // System.out.println("REFERENCE FOUND RETURN EXISTING SOURCE");
608 return (IdentifiableSource
) osb
.clone();
612 } catch (CloneNotSupportedException e
) {
613 throw new RuntimeException(e
);
614 } catch (Exception e1
){
615 e1
.printStackTrace();
619 IdentifiableSource sour
= IdentifiableSource
.NewInstance(OriginalSourceType
.Import
,null,null, reference
,citationDetail
);
624 // * @param reference
625 // * @param citationDetail
628 // private DescriptionElementSource getDescriptionSource(Reference<?> reference, String citationDetail) {
630 // List<OriginalSourceBase> issTmp2 = getCommonService().list(DescriptionElementSource.class, null, null, null, null);
633 // for (OriginalSourceBase<?> osb:issTmp2){
634 // if (osb.getCitation().equals(reference) && osb.getCitationMicroReference().equalsIgnoreCase(citationDetail)) {
635 // return (DescriptionElementSource) osb.clone();
638 // } catch (CloneNotSupportedException e) {
639 // // TODO Auto-generated catch block
640 // e.printStackTrace();
643 // DescriptionElementSource sour = DescriptionElementSource.NewInstance(OriginalSourceType.Import,null,null, reference,citationDetail);
649 * Very fast and dirty implementation to allow handling of transient objects as described in
650 * https://dev.e-taxonomy.eu/trac/ticket/3726
657 private void save(CdmBase cdmBase
, Abcd206ImportState state
) {
658 ICdmApplicationConfiguration cdmRepository
= state
.getConfig().getCdmAppController();
659 if (cdmRepository
== null){
660 cdmRepository
= this;
663 if (cdmBase
.isInstanceOf(LanguageString
.class)){
664 cdmRepository
.getTermService().saveLanguageData(CdmBase
.deproxy(cdmBase
, LanguageString
.class));
665 }else if (cdmBase
.isInstanceOf(SpecimenOrObservationBase
.class)){
666 cdmRepository
.getOccurrenceService().saveOrUpdate(CdmBase
.deproxy(cdmBase
, SpecimenOrObservationBase
.class));
667 }else if (cdmBase
.isInstanceOf(Reference
.class)){
668 cdmRepository
.getReferenceService().saveOrUpdate(CdmBase
.deproxy(cdmBase
, Reference
.class));
669 }else if (cdmBase
.isInstanceOf(Classification
.class)){
670 cdmRepository
.getClassificationService().saveOrUpdate(CdmBase
.deproxy(cdmBase
, Classification
.class));
671 }else if (cdmBase
.isInstanceOf(AgentBase
.class)){
672 cdmRepository
.getAgentService().saveOrUpdate(CdmBase
.deproxy(cdmBase
, AgentBase
.class));
673 }else if (cdmBase
.isInstanceOf(Collection
.class)){
674 cdmRepository
.getCollectionService().saveOrUpdate(CdmBase
.deproxy(cdmBase
, Collection
.class));
675 }else if (cdmBase
.isInstanceOf(DescriptionBase
.class)){
676 cdmRepository
.getDescriptionService().saveOrUpdate(CdmBase
.deproxy(cdmBase
, DescriptionBase
.class));
677 }else if (cdmBase
.isInstanceOf(TaxonBase
.class)){
678 cdmRepository
.getTaxonService().saveOrUpdate(CdmBase
.deproxy(cdmBase
, TaxonBase
.class));
679 }else if (cdmBase
.isInstanceOf(TaxonNameBase
.class)){
680 cdmRepository
.getNameService().saveOrUpdate(CdmBase
.deproxy(cdmBase
, TaxonNameBase
.class));
682 throw new IllegalArgumentException("Class not supported in save method: " + CdmBase
.deproxy(cdmBase
, CdmBase
.class).getClass().getSimpleName());
688 * setCollectionData : store the collection object into the
693 private void setCollectionData(Abcd206ImportState state
, DerivedUnitFacade derivedUnitFacade
) {
694 // set catalogue number (unitID)
695 derivedUnitFacade
.setCatalogNumber(NB(dataHolder
.unitID
));
696 derivedUnitFacade
.setAccessionNumber(NB(dataHolder
.accessionNumber
));
697 // derivedUnitFacade.setCollectorsNumber(NB(dataHolder.collectorsNumber));
700 * INSTITUTION & COLLECTION
702 // manage institution
703 Institution institution
= this.getInstitution(NB(dataHolder
.institutionCode
), state
);
705 Collection collection
= this.getCollection(institution
, NB(dataHolder
.collectionCode
), state
);
706 // link specimen & collection
707 derivedUnitFacade
.setCollection(collection
);
711 * getFacade : get the DerivedUnitFacade based on the recordBasis
713 * @return DerivedUnitFacade
715 private DerivedUnitFacade
getFacade() {
717 logger
.info("getFacade()");
719 SpecimenOrObservationType type
= null;
722 if (NB((dataHolder
.recordBasis
)) != null) {
723 if (dataHolder
.recordBasis
.toLowerCase().startsWith("s") || dataHolder
.recordBasis
.toLowerCase().indexOf("specimen")>-1) {// specimen
724 type
= SpecimenOrObservationType
.PreservedSpecimen
;
726 if (dataHolder
.recordBasis
.toLowerCase().startsWith("o") ||dataHolder
.recordBasis
.toLowerCase().indexOf("observation")>-1 ) {
727 type
= SpecimenOrObservationType
.Observation
;
729 if (dataHolder
.recordBasis
.toLowerCase().indexOf("fossil")>-1){
730 type
= SpecimenOrObservationType
.Fossil
;
732 if (dataHolder
.recordBasis
.toLowerCase().indexOf("living")>-1) {
733 type
= SpecimenOrObservationType
.LivingSpecimen
;
736 logger
.info("The basis of record does not seem to be known: " + dataHolder
.recordBasis
);
737 type
= SpecimenOrObservationType
.DerivedUnit
;
741 logger
.info("The basis of record is null");
742 type
= SpecimenOrObservationType
.DerivedUnit
;
744 DerivedUnitFacade derivedUnitFacade
= DerivedUnitFacade
.NewInstance(type
);
745 return derivedUnitFacade
;
748 private void getCollectorsFromXML(Element root
, Abcd206XMLFieldGetter abcdFieldGetter
) {
751 group
= root
.getChildNodes();
752 for (int i
= 0; i
< group
.getLength(); i
++) {
753 if (group
.item(i
).getNodeName().equals(prefix
+ "Identifications")) {
754 group
= group
.item(i
).getChildNodes();
758 dataHolder
.gatheringAgentList
= new ArrayList
<String
>();
759 dataHolder
.gatheringTeamList
= new ArrayList
<String
>();
760 abcdFieldGetter
.getType(root
);
761 abcdFieldGetter
.getGatheringPeople(root
);
765 * Store the unit's properties into variables Look which unit is the
766 * preferred one Look what kind of name it is supposed to be, for the
767 * parsing (Botanical, Zoological)
769 * @param racine: the root node for a single unit
771 private void setUnitPropertiesXML(Element root
, Abcd206XMLFieldGetter abcdFieldGetter
) {
775 group
= root
.getChildNodes();
776 for (int i
= 0; i
< group
.getLength(); i
++) {
777 if (group
.item(i
).getNodeName().equals(prefix
+ "Identifications")) {
778 group
= group
.item(i
).getChildNodes();
782 dataHolder
.identificationList
= new ArrayList
<Identification
>();
783 dataHolder
.statusList
= new ArrayList
<SpecimenTypeDesignationStatus
>();
784 dataHolder
.atomisedIdentificationList
= new ArrayList
<HashMap
<String
, String
>>();
785 dataHolder
.referenceList
= new ArrayList
<String
[]>();
786 dataHolder
.multimediaObjects
= new ArrayList
<String
>();
788 abcdFieldGetter
.getScientificNames(group
);
789 abcdFieldGetter
.getType(root
);
792 logger
.info("this.identificationList "+dataHolder
.identificationList
.toString());
794 abcdFieldGetter
.getIDs(root
);
795 abcdFieldGetter
.getRecordBasis(root
);
796 abcdFieldGetter
.getMultimedia(root
);
797 abcdFieldGetter
.getNumbers(root
);
798 abcdFieldGetter
.getGeolocation(root
);
799 abcdFieldGetter
.getGatheringPeople(root
);
800 abcdFieldGetter
.getGatheringDate(root
);
801 abcdFieldGetter
.getGatheringElevation(root
);
802 boolean referencefound
= abcdFieldGetter
.getReferences(root
);
803 if (!referencefound
) {
804 String
[]a
= {ref
.getTitleCache(),"",""};
805 dataHolder
.referenceList
.add(a
);
808 } catch (Exception e
) {
809 logger
.info("Error occured while parsing XML file" + e
);
814 * Look if the Institution does already exist
815 * @param institutionCode: a string with the institutioncode
816 * @param config : the configurator
817 * @return the Institution (existing or new)
819 @SuppressWarnings("rawtypes")
820 private Institution
getInstitution(String institutionCode
, Abcd206ImportState state
) {
821 Institution institution
=null;
822 List
<Institution
> institutions
;
824 institutions
= getAgentService().list(Institution
.class, null, null, null, null);
825 } catch (Exception e
) {
826 institutions
= new ArrayList
<Institution
>();
829 if (institutions
.size() > 0 && state
.getConfig().isReUseExistingMetadata()) {
830 for (Institution institut
:institutions
){
832 if (institut
.getCode().equalsIgnoreCase(institutionCode
)) {
833 institution
=institut
;
835 }catch(Exception e
){logger
.warn("no institution code in the db");}
839 if(institution
!=null) {
840 logger
.info("getinstitution " + institution
.toString());
843 if (institution
== null){
844 // create institution
845 institution
= Institution
.NewInstance();
846 institution
.setCode(institutionCode
);
847 institution
.setTitleCache(institutionCode
, true);
849 save(institution
, state
);
854 * Look if the Collection does already exist
855 * @param collectionCode
856 * @param collectionCode: a string
857 * @param config : the configurator
858 * @return the Collection (existing or new)
860 private Collection
getCollection(Institution institution
, String collectionCode
, Abcd206ImportState state
) {
861 Collection collection
= null;
862 List
<Collection
> collections
;
864 collections
= getCollectionService().list(Collection
.class, null, null, null, null);
865 } catch (Exception e
) {
866 collections
= new ArrayList
<Collection
>();
868 if (collections
.size() > 0 && state
.getConfig().isReUseExistingMetadata()) {
869 for (Collection coll
:collections
){
870 if (coll
.getInstitute() != null) {
871 if (coll
.getCode().equalsIgnoreCase(collectionCode
) && coll
.getInstitute().equals(institution
)) {
878 if(collection
== null){
879 collection
=Collection
.NewInstance();
880 collection
.setCode(collectionCode
);
881 collection
.setInstitute(institution
);
882 collection
.setTitleCache(collectionCode
);
884 save(collection
, state
);
890 * join DeterminationEvent to the Taxon Object
891 * @param state : the ABCD import state
892 * @param taxon: the current Taxon
893 * @param preferredFlag :if the current name is preferred
894 * @param derivedFacade : the derived Unit Facade
896 @SuppressWarnings("rawtypes")
897 private void linkDeterminationEvent(Abcd206ImportState state
, Taxon taxon
, boolean preferredFlag
, DerivedUnitFacade derivedFacade
) {
898 Abcd206ImportConfigurator config
= state
.getConfig();
900 logger
.info("start linkdetermination with taxon:" + taxon
.getUuid()+", "+taxon
);
903 DeterminationEvent determinationEvent
= DeterminationEvent
.NewInstance();
904 determinationEvent
.setTaxon(taxon
);
905 determinationEvent
.setPreferredFlag(preferredFlag
);
907 determinationEvent
.setIdentifiedUnit(derivedUnitBase
);
908 derivedUnitBase
.addDetermination(determinationEvent
);
911 logger
.info("NB TYPES INFO: "+ dataHolder
.statusList
.size());
913 for (SpecimenTypeDesignationStatus specimenTypeDesignationstatus
: dataHolder
.statusList
) {
914 if (specimenTypeDesignationstatus
!= null) {
916 logger
.info("specimenTypeDesignationstatus :"+ specimenTypeDesignationstatus
);
919 ICdmApplicationConfiguration cdmAppController
= config
.getCdmAppController();
920 if(cdmAppController
== null){
921 cdmAppController
= this;
923 specimenTypeDesignationstatus
= (SpecimenTypeDesignationStatus
) cdmAppController
.getTermService().find(specimenTypeDesignationstatus
.getUuid());
925 TaxonNameBase
<?
,?
> name
= taxon
.getName();
926 SpecimenTypeDesignation designation
= SpecimenTypeDesignation
.NewInstance();
928 designation
.setTypeStatus(specimenTypeDesignationstatus
);
929 designation
.setTypeSpecimen(derivedUnitBase
);
930 name
.addTypeDesignation(designation
, true);
934 for (String
[] fullReference
: dataHolder
.referenceList
) {
935 List
<Reference
> references
= getReferenceService().list(Reference
.class, null, null, null, null);
937 String strReference
=fullReference
[0];
938 String citationDetail
= fullReference
[1];
939 String citationURL
= fullReference
[2];
941 if (isNotBlank(strReference
)){
942 Reference
<?
> reference
= null;
943 for (Reference
<?
> refe
: references
) {
944 if (refe
.getTitleCache().equalsIgnoreCase(strReference
)) {
949 if (reference
==null){
950 reference
= ReferenceFactory
.newGeneric();
951 reference
.setTitleCache(strReference
, true);
952 save(reference
, state
);
954 determinationEvent
.addReference(reference
);
957 save(derivedUnitBase
, state
);
959 if (config
.isAddIndividualsAssociationsSuchAsSpecimenAndObservations()) {
961 logger
.info("isDoCreateIndividualsAssociations");
964 makeIndividualsAssociation(state
, taxon
, determinationEvent
);
966 if(state
.getConfig().isDeterminationOnFieldUnitLevel()){
967 DeterminationEvent fieldUnitDeterminationEvent
= DeterminationEvent
.NewInstance();
968 fieldUnitDeterminationEvent
.setTaxon(determinationEvent
.getTaxon());
969 fieldUnitDeterminationEvent
.setPreferredFlag(determinationEvent
.getPreferredFlag());
970 fieldUnitDeterminationEvent
.setIdentifiedUnit(fieldUnit
);
971 Set
<Reference
> references
= determinationEvent
.getReferences();
972 for (Reference reference
: references
) {
973 fieldUnitDeterminationEvent
.addReference(reference
);
975 fieldUnit
.addDetermination(fieldUnitDeterminationEvent
);
978 save(derivedUnitBase
, state
);
983 * create and link each association (specimen, observation..) to the accepted taxon
984 * @param state : the ABCD import state
985 * @param taxon: the current Taxon
986 * @param determinationEvent:the determinationevent
988 @SuppressWarnings("unused")
989 private void makeIndividualsAssociation(Abcd206ImportState state
, Taxon taxon
, DeterminationEvent determinationEvent
) {
990 SpecimenUserInteraction sui
= state
.getConfig().getSpecimenUserInteraction();
993 System
.out
.println("MAKE INDIVIDUALS ASSOCIATION");
996 TaxonDescription taxonDescription
= null;
997 Set
<TaxonDescription
> descriptions
= taxon
.getDescriptions();
998 if (state
.getConfig().isInteractWithUser()){
999 if(!descriptionGroupSet
){
1000 taxonDescription
= sui
.askForDescriptionGroup(descriptions
);
1001 descriptionGroup
=taxonDescription
;
1002 descriptionGroupSet
=true;
1004 taxonDescription
=descriptionGroup
;
1007 for (TaxonDescription description
: descriptions
){
1008 Set
<IdentifiableSource
> sources
= description
.getTaxon().getSources();
1009 sources
.addAll(description
.getSources());
1010 for (IdentifiableSource source
:sources
){
1011 if(ref
.equals(source
.getCitation())) {
1012 taxonDescription
= description
;
1017 if (taxonDescription
== null){
1018 taxonDescription
= TaxonDescription
.NewInstance(taxon
, false);
1019 if(sourceNotLinkedToElement(taxonDescription
,ref
,null)) {
1020 taxonDescription
.addSource(OriginalSourceType
.Import
, null, null, ref
, null);
1022 descriptionGroup
=taxonDescription
;
1023 taxon
.addDescription(taxonDescription
);
1026 //PREPARE REFERENCE QUESTIONS
1028 Map
<String
,OriginalSourceBase
<?
>> sourceMap
= new HashMap
<String
, OriginalSourceBase
<?
>>();
1030 List
<IdentifiableSource
> issTmp
= getCommonService().list(IdentifiableSource
.class, null, null, null, null);
1031 List
<DescriptionElementSource
> issTmp2
= getCommonService().list(DescriptionElementSource
.class, null, null, null, null);
1033 Set
<OriginalSourceBase
> osbSet
= new HashSet
<OriginalSourceBase
>();
1035 osbSet
.addAll(issTmp2
);
1038 osbSet
.addAll(issTmp
);
1042 for( OriginalSourceBase
<?
> osb
:osbSet
) {
1043 if(osb
.getCitationMicroReference() !=null && !osb
.getCitationMicroReference().isEmpty()) {
1045 sourceMap
.put(osb
.getCitation().getTitleCache()+ "---"+osb
.getCitationMicroReference(),osb
);
1046 }catch(NullPointerException e
){logger
.warn("null pointer problem (no ref?) with "+osb
);}
1049 sourceMap
.put(osb
.getCitation().getTitleCache(),osb
);
1050 }catch(NullPointerException e
){logger
.warn("null pointer problem (no ref?) with "+osb
);}
1054 if (state
.getConfig().isInteractWithUser()){
1055 List
<OriginalSourceBase
<?
>> res
= null;
1056 if(!descriptionSourcesSet
){
1057 res
= sui
.askForSource(sourceMap
, "the description group ("+taxon
+")",
1058 "The current reference is "+ref
.getTitleCache(),getReferenceService(), dataHolder
.docSources
);
1059 descriptionRefs
=res
;
1060 descriptionSourcesSet
=true;
1063 res
=descriptionRefs
;
1066 for (OriginalSourceBase
<?
> sour
:res
){
1067 if(sour
.isInstanceOf(IdentifiableSource
.class)){
1069 if(sourceNotLinkedToElement(taxonDescription
,sour
)) {
1070 taxonDescription
.addSource((IdentifiableSource
)sour
.clone());
1072 } catch (CloneNotSupportedException e
) {
1073 logger
.warn("no cloning?");
1076 if(sourceNotLinkedToElement(taxonDescription
,sour
)) {
1077 taxonDescription
.addSource(OriginalSourceType
.Import
,null, null, sour
.getCitation(),sour
.getCitationMicroReference());
1084 if(sourceNotLinkedToElement(taxonDescription
,ref
,null)) {
1085 taxonDescription
.addSource(OriginalSourceType
.Import
,null, null, ref
, null);
1088 descriptionGroup
=taxonDescription
;
1090 IndividualsAssociation indAssociation
= IndividualsAssociation
.NewInstance();
1091 Feature feature
= makeFeature(derivedUnitBase
);
1092 indAssociation
.setAssociatedSpecimenOrObservation(derivedUnitBase
);
1093 indAssociation
.setFeature(feature
);
1095 if (state
.getConfig().isInteractWithUser()){
1096 sourceMap
= new HashMap
<String
, OriginalSourceBase
<?
>>();
1098 issTmp
= getCommonService().list(IdentifiableSource
.class, null, null, null, null);
1099 issTmp2
= getCommonService().list(DescriptionElementSource
.class, null, null, null, null);
1101 osbSet
= new HashSet
<OriginalSourceBase
>();
1103 osbSet
.addAll(issTmp2
);
1106 osbSet
.addAll(issTmp
);
1110 for( OriginalSourceBase
<?
> osb
:osbSet
) {
1111 if(osb
.getCitationMicroReference() !=null && !osb
.getCitationMicroReference().isEmpty()) {
1113 sourceMap
.put(osb
.getCitation().getTitleCache()+ "---"+osb
.getCitationMicroReference(),osb
);
1114 }catch(NullPointerException e
){logger
.warn("null pointer problem (no ref?) with "+osb
);}
1117 sourceMap
.put(osb
.getCitation().getTitleCache(),osb
);
1118 }catch(NullPointerException e
){logger
.warn("null pointer problem (no ref?) with "+osb
);}
1122 List
<OriginalSourceBase
<?
>> sources
=null;
1123 if(!associationSourcesSet
) {
1124 sources
= sui
.askForSource(sourceMap
, "descriptive element (association) ",taxon
.toString(),
1125 getReferenceService(),dataHolder
.docSources
);
1126 associationRefs
=sources
;
1127 associationSourcesSet
=true;
1130 sources
=associationRefs
;
1132 if(sources
!=null) {
1133 for (OriginalSourceBase
<?
> source
: sources
) {
1135 if(source
.isInstanceOf(DescriptionElementSource
.class)){
1137 if(sourceNotLinkedToElement(indAssociation
,source
)) {
1138 indAssociation
.addSource((DescriptionElementSource
)source
.clone());
1140 } catch (CloneNotSupportedException e
) {
1141 logger
.warn("clone forbidden?");
1144 if(sourceNotLinkedToElement(indAssociation
,source
)) {
1145 indAssociation
.addSource(OriginalSourceType
.Import
,null, null, source
.getCitation(),source
.getCitationMicroReference());
1148 if(sourceNotLinkedToElement(derivedUnitBase
, source
)) {
1149 derivedUnitBase
.addSource((IdentifiableSource
) source
.clone());
1151 } catch (CloneNotSupportedException e
) {
1152 // TODO Auto-generated catch block
1153 e
.printStackTrace();
1161 if(sourceNotLinkedToElement(indAssociation
,ref
,null)) {
1162 indAssociation
.addSource(OriginalSourceType
.Import
,null, null, ref
, null);
1164 if(sourceNotLinkedToElement(derivedUnitBase
, ref
,null)) {
1165 derivedUnitBase
.addSource(OriginalSourceType
.Import
,null, null, ref
, null);
1167 for (Reference
<?
> citation
: determinationEvent
.getReferences()) {
1168 if(sourceNotLinkedToElement(indAssociation
,citation
,null))
1170 indAssociation
.addSource(DescriptionElementSource
.NewInstance(OriginalSourceType
.Import
, null, null, citation
, null));
1172 if(sourceNotLinkedToElement(derivedUnitBase
, ref
,null)) {
1173 derivedUnitBase
.addSource(OriginalSourceType
.Import
,null, null, ref
, null);
1178 taxonDescription
.addElement(indAssociation
);
1180 save(taxonDescription
, state
);
1185 * @param derivedUnitBase2
1190 private boolean sourceNotLinkedToElement(DerivedUnit derivedUnitBase2
, Reference
<?
> b
, String d
) {
1191 Set
<IdentifiableSource
> linkedSources
= derivedUnitBase2
.getSources();
1192 for (IdentifiableSource is
:linkedSources
){
1193 Reference a
= is
.getCitation();
1194 String c
= is
.getCitationMicroReference();
1196 boolean refMatch
=false;
1197 boolean microMatch
=false;
1200 if (a
==null && b
==null) {
1203 if (a
!=null && b
!=null) {
1204 if (a
.getTitleCache().equalsIgnoreCase(b
.getTitleCache())) {
1208 }catch(Exception e
){}
1212 if (c
==null && d
==null) {
1215 if(c
!=null && d
!=null) {
1216 if(c
.equalsIgnoreCase(d
)) {
1221 catch(Exception e
){}
1223 if (microMatch
&& refMatch
) {
1237 private boolean sourceNotLinkedToElement(SpecimenOrObservationBase
<?
> specimen
, OriginalSourceBase
<?
> source
) {
1238 Set
<IdentifiableSource
> linkedSources
= specimen
.getSources();
1239 for (IdentifiableSource is
:linkedSources
){
1240 Reference a
= is
.getCitation();
1241 Reference b
= source
.getCitation();
1242 String c
= is
.getCitationMicroReference();
1243 String d
= source
.getCitationMicroReference();
1245 boolean refMatch
=false;
1246 boolean microMatch
=false;
1249 if (a
==null && b
==null) {
1252 if (a
!=null && b
!=null) {
1253 if (a
.getTitleCache().equalsIgnoreCase(b
.getTitleCache())) {
1257 }catch(Exception e
){}
1261 if (c
==null && d
==null) {
1264 if(c
!=null && d
!=null) {
1265 if(c
.equalsIgnoreCase(d
)) {
1270 catch(Exception e
){}
1272 if (microMatch
&& refMatch
) {
1282 * @param indAssociation
1287 private boolean sourceNotLinkedToElement(IndividualsAssociation indAssociation
, Reference
<?
> a
, String d
) {
1288 Set
<DescriptionElementSource
> linkedSources
= indAssociation
.getSources();
1289 for (DescriptionElementSource is
:linkedSources
){
1290 Reference b
= is
.getCitation();
1291 String c
= is
.getCitationMicroReference();
1293 boolean refMatch
=false;
1294 boolean microMatch
=false;
1297 if (a
==null && b
==null) {
1300 if (a
!=null && b
!=null) {
1301 if (a
.getTitleCache().equalsIgnoreCase(b
.getTitleCache())) {
1305 }catch(Exception e
){}
1309 if (c
==null && d
==null) {
1312 if(c
!=null && d
!=null) {
1313 if(c
.equalsIgnoreCase(d
)) {
1318 catch(Exception e
){}
1320 if (microMatch
&& refMatch
) {
1328 * @param taxonDescription
1333 private boolean sourceNotLinkedToElement(TaxonDescription taxonDescription
, Reference
<?
> a
, String d
) {
1334 Set
<IdentifiableSource
> linkedSources
= taxonDescription
.getSources();
1335 for (IdentifiableSource is
:linkedSources
){
1336 Reference b
= is
.getCitation();
1337 String c
= is
.getCitationMicroReference();
1339 boolean refMatch
=false;
1340 boolean microMatch
=false;
1343 if (a
==null && b
==null) {
1346 if (a
!=null && b
!=null) {
1347 if (a
.getTitleCache().equalsIgnoreCase(b
.getTitleCache())) {
1351 }catch(Exception e
){}
1355 if (c
==null && d
==null) {
1358 if(c
!=null && d
!=null) {
1359 if(c
.equalsIgnoreCase(d
)) {
1364 catch(Exception e
){}
1366 if (microMatch
&& refMatch
) {
1374 * @param indAssociation
1378 private boolean sourceNotLinkedToElement(IndividualsAssociation indAssociation
, OriginalSourceBase
<?
> source
) {
1379 Set
<DescriptionElementSource
> linkedSources
= indAssociation
.getSources();
1380 for (DescriptionElementSource is
:linkedSources
){
1381 Reference a
= is
.getCitation();
1382 Reference b
= source
.getCitation();
1383 String c
= is
.getCitationMicroReference();
1384 String d
= source
.getCitationMicroReference();
1386 boolean refMatch
=false;
1387 boolean microMatch
=false;
1390 if (a
==null && b
==null) {
1393 if (a
!=null && b
!=null) {
1394 if (a
.getTitleCache().equalsIgnoreCase(b
.getTitleCache())) {
1398 }catch(Exception e
){}
1402 if (c
==null && d
==null) {
1405 if(c
!=null && d
!=null) {
1406 if(c
.equalsIgnoreCase(d
)) {
1411 catch(Exception e
){}
1413 if (microMatch
&& refMatch
) {
1421 * @param taxonDescription
1425 private boolean sourceNotLinkedToElement(TaxonDescription taxonDescription
, OriginalSourceBase
<?
> sour
) {
1426 Set
<IdentifiableSource
> linkedSources
= taxonDescription
.getSources();
1427 for (IdentifiableSource is
:linkedSources
){
1428 Reference a
= is
.getCitation();
1429 Reference b
= sour
.getCitation();
1430 String c
= is
.getCitationMicroReference();
1431 String d
= sour
.getCitationMicroReference();
1433 boolean refMatch
=false;
1434 boolean microMatch
=false;
1437 if (a
==null && b
==null) {
1440 if (a
!=null && b
!=null) {
1441 if (a
.getTitleCache().equalsIgnoreCase(b
.getTitleCache())) {
1445 }catch(Exception e
){}
1449 if (c
==null && d
==null) {
1452 if(c
!=null && d
!=null) {
1453 if(c
.equalsIgnoreCase(d
)) {
1458 catch(Exception e
){}
1460 if (microMatch
&& refMatch
) {
1470 * look for the Feature object (FieldObs, Specimen,...)
1471 * @param unit : a specimen or obersvation base
1472 * @return the corresponding Feature
1474 private Feature
makeFeature(SpecimenOrObservationBase
<?
> unit
) {
1475 SpecimenOrObservationType type
= unit
.getRecordBasis();
1479 if (type
.isFeatureObservation()){
1480 return Feature
.OBSERVATION();
1481 }else if (type
.isFeatureSpecimen()){
1482 return Feature
.SPECIMEN();
1483 }else if (type
== SpecimenOrObservationType
.DerivedUnit
){
1484 return Feature
.OBSERVATION();
1485 // return getFeature("Specimen or observation");
1487 String message
= "Unhandled record basis '%s' for defining individuals association feature type. Use default.";
1488 logger
.warn(String
.format(message
, type
.getMessage()));
1489 return Feature
.OBSERVATION();
1490 // return getFeature("Specimen or observation");
1495 private Feature
getFeature(String featureName
, Abcd206ImportState state
){
1496 List
<Feature
> features
= getTermService().list(Feature
.class, null,null,null,null);
1497 Feature currentFeature
=null;
1498 for (Feature feature
: features
){
1499 String tmpF
= feature
.getTitleCache();
1500 if (tmpF
.equalsIgnoreCase(featureName
)) {
1501 currentFeature
=feature
;
1504 if (currentFeature
== null) {
1505 currentFeature
=Feature
.NewInstance(featureName
, featureName
, featureName
);
1506 save(currentFeature
, state
);
1508 return currentFeature
;
1512 * Search for an existing taxon in the database and <b>create</b> it if it is not found.
1513 * @param state : the ABCD import state
1514 * @param scientificName : the name (string)
1515 * @param i : the current unit position in the abcd file
1516 * @param rank : the rank for the taxon
1519 @SuppressWarnings("rawtypes")
1520 private Taxon
getOrCreateTaxon(Abcd206ImportState state
, String scientificName
, int i
, Rank rank
) {
1521 // System.out.println("GETTAXON "+scientificName);
1522 Abcd206ImportConfigurator config
= state
.getConfig();
1524 NonViralName
<?
> taxonName
= null;
1526 SpecimenUserInteraction sui
= state
.getConfig().getSpecimenUserInteraction();
1528 // System.out.println("config.isReuseExistingTaxaWhenPossible() :"+config.isReuseExistingTaxaWhenPossible());
1529 if (config
.isReuseExistingTaxaWhenPossible()){
1531 List
<TaxonBase
> taxonbaseList
= getTaxonService().listByTitle(Taxon
.class, scientificName
, MatchMode
.BEGINNING
, null, null, null, null, null);
1532 if (taxonbaseList
.size()>0){
1533 if(config
.isInteractWithUser() && config
.isAllowReuseOtherClassifications()){
1534 taxon
= sui
.askWhereToFixData(scientificName
,taxonbaseList
, classification
);
1536 taxon
= sui
.lookForTaxaIntoCurrentClassification(taxonbaseList
, classification
);
1540 List
<TaxonBase
> searchByNameList
= getTaxonService().searchTaxaByName(scientificName
, ref
);
1541 if(config
.isInteractWithUser() && config
.isAllowReuseOtherClassifications()){
1542 taxon
= sui
.askWhereToFixData(scientificName
,searchByNameList
, classification
);
1545 taxon
= sui
.lookForTaxaIntoCurrentClassification(searchByNameList
, classification
);
1548 } catch (Exception e
) {
1549 logger
.info("Searchtaxabyname failed" + e
);
1553 if (!config
.isReuseExistingTaxaWhenPossible() || taxon
== null){
1554 // System.out.println("create new taxonName instance "+i+", "+config.isParseNameAutomatically());
1555 if (config
.isParseNameAutomatically()){
1556 taxonName
= parseScientificName(scientificName
);
1559 if (i
>=0 && (dataHolder
.atomisedIdentificationList
!= null || dataHolder
.atomisedIdentificationList
.size() > 0)) {
1560 taxonName
= setTaxonNameByType(dataHolder
.atomisedIdentificationList
.get(i
), scientificName
);
1565 // if (taxonName != null) {
1566 // System.out.println(taxonName.getTitleCache());
1568 // System.out.println("taxonname: "+taxonName);
1570 if(taxonName
== null){
1571 taxonName
= NonViralName
.NewInstance(rank
);
1572 taxonName
.setFullTitleCache(scientificName
,true);
1573 taxonName
.setTitleCache(scientificName
, true);
1575 // System.out.println("ADD NEW TAXON *"+taxonName.getRank()+"*"+taxonName.getTitleCache());
1576 if (rank
!= null && (taxonName
.getRank() ==null || taxonName
.getRank().toString().trim().isEmpty())) {
1577 taxonName
.setRank(rank
);
1579 save(taxonName
, state
);
1580 taxon
= Taxon
.NewInstance(taxonName
, ref
); //sec set null
1587 * HandleIdentifications : get the scientific names present in the ABCD
1588 * document and store link them with the observation/specimen data
1589 * @param state: the current ABCD import state
1590 * @param derivedUnitFacade : the current derivedunitfacade
1592 private void handleIdentifications(Abcd206ImportState state
, DerivedUnitFacade derivedUnitFacade
) {
1593 Abcd206ImportConfigurator config
= state
.getConfig();
1597 String scientificName
= "";
1598 boolean preferredFlag
= false;
1600 if (dataHolder
.nomenclatureCode
== ""){
1601 dataHolder
.nomenclatureCode
= config
.getNomenclaturalCode().toString();
1604 for (int i
= 0; i
< dataHolder
.identificationList
.size(); i
++) {
1605 Identification identification
= dataHolder
.identificationList
.get(i
);
1606 scientificName
= identification
.getScientificName().replaceAll(" et ", " & ");
1608 String preferred
= identification
.getPreferred();
1609 if (preferred
.equals("1") || preferred
.toLowerCase().indexOf("true") != -1 || dataHolder
.identificationList
.size()==1) {
1610 preferredFlag
= true;
1613 preferredFlag
= false;
1616 if (identification
.getCode().indexOf(':') != -1) {
1617 dataHolder
.nomenclatureCode
= identification
.getCode().split(COLON
)[1];
1620 dataHolder
.nomenclatureCode
= identification
.getCode();
1622 taxon
= getOrCreateTaxon(state
, scientificName
, i
,null);
1623 addTaxonNode(taxon
, state
,preferredFlag
);
1624 linkDeterminationEvent(state
, taxon
, preferredFlag
, derivedUnitFacade
);
1629 private TaxonNameBase
<?
, ?
> getOrCreateTaxonName(String scientificName
, Rank rank
, Abcd206ImportState state
){
1630 List
<TaxonNameBase
> names
= getNameService().listByTitle(TaxonNameBase
.class, scientificName
, MatchMode
.EXACT
, null, null, null, null, null);
1633 logger
.warn("More than one taxon name was found for "+scientificName
+"!");
1634 logger
.warn("The first one found was chosen.");
1636 return names
.iterator().next();
1639 NonViralName taxonName
= NonViralName
.NewInstance(rank
);
1640 taxonName
.setFullTitleCache(scientificName
,true);
1641 taxonName
.setTitleCache(scientificName
, true);
1643 taxonName
.setRank(rank
);
1645 save(taxonName
, state
);
1650 private TaxonBase
<?
> getTaxonForName(TaxonNameBase
<?
, ?
> taxonNameBase
){
1651 Set
<Taxon
> acceptedTaxa
= taxonNameBase
.getTaxa();
1652 if(acceptedTaxa
.size()>0){
1653 if(acceptedTaxa
.size()>1){
1654 logger
.warn("More than one accepted taxon was found for taxon name: "+taxonNameBase
.getTitleCache()+"!");
1655 logger
.warn("the first one was chosen");
1657 return acceptedTaxa
.iterator().next();
1660 Set
<TaxonBase
> taxonAndSynonyms
= taxonNameBase
.getTaxonBases();
1661 if(taxonAndSynonyms
.size()>0){
1662 if(taxonAndSynonyms
.size()>1){
1663 logger
.warn("More than one synonym was found for taxon name: "+taxonNameBase
.getTitleCache()+"!");
1664 logger
.warn("the first one was chosen");
1666 return taxonAndSynonyms
.iterator().next();
1673 * @param taxon : a taxon to add as a node
1674 * @param state : the ABCD import state
1676 private void addTaxonNode(Taxon taxon
, Abcd206ImportState state
, boolean preferredFlag
) {
1677 logger
.info("link taxon to a taxonNode "+taxon
.getTitleCache());
1678 boolean exist
= false;
1679 for (TaxonNode p
: classification
.getAllNodes()){
1681 if(p
.getTaxon().equals(taxon
)) {
1686 logger
.warn("TaxonNode doesn't seem to have a taxon");
1690 addParentTaxon(taxon
, state
, preferredFlag
);
1694 private boolean hasTaxonNodeInClassification(Taxon taxon
){
1695 for (TaxonNode node
: taxon
.getTaxonNodes()){
1696 if(node
.getClassification().equals(classification
)){
1704 * Add the hierarchy for a Taxon(add higher taxa)
1705 * @param taxon: a taxon to add as a node
1706 * @param state: the ABCD import state
1708 private void addParentTaxon(Taxon taxon
, Abcd206ImportState state
, boolean preferredFlag
){
1709 NonViralName
<?
> nvname
= CdmBase
.deproxy(taxon
.getName(), NonViralName
.class);
1710 Rank rank
= nvname
.getRank();
1712 Taxon subgenus
=null;
1713 Taxon species
= null;
1714 Taxon subspecies
= null;
1715 Taxon parent
= null;
1716 if (rank
.isLower(Rank
.GENUS() )){
1717 String prefix
= nvname
.getGenusOrUninomial();
1718 genus
= getOrCreateTaxon(state
, prefix
, -1, Rank
.GENUS());
1719 if (preferredFlag
) {
1720 parent
= saveOrUpdateClassification(null, genus
, state
);
1724 if (rank
.isLower(Rank
.SUBGENUS())){
1725 String prefix
= nvname
.getGenusOrUninomial();
1726 String name
= nvname
.getInfraGenericEpithet();
1728 subgenus
= getOrCreateTaxon(state
, prefix
+" "+name
, -1, Rank
.SUBGENUS());
1729 if (preferredFlag
) {
1730 parent
= saveOrUpdateClassification(genus
, subgenus
, state
);
1733 if (rank
.isLower(Rank
.SPECIES())){
1734 if (subgenus
!=null){
1735 String prefix
= nvname
.getGenusOrUninomial();
1736 String name
= nvname
.getInfraGenericEpithet();
1737 String spe
= nvname
.getSpecificEpithet();
1739 species
= getOrCreateTaxon(state
, prefix
+" "+name
+" "+spe
, -1, Rank
.SPECIES());
1740 if (preferredFlag
) {
1741 parent
= saveOrUpdateClassification(subgenus
, species
, state
);
1746 String prefix
= nvname
.getGenusOrUninomial();
1747 String name
= nvname
.getSpecificEpithet();
1749 species
= getOrCreateTaxon(state
, prefix
+" "+name
, -1, Rank
.SPECIES());
1750 if (preferredFlag
) {
1751 parent
= saveOrUpdateClassification(genus
, species
, state
);
1756 if (rank
.isLower(Rank
.INFRASPECIES())){
1757 subspecies
= getOrCreateTaxon(state
, nvname
.getFullTitleCache(), -1, Rank
.SUBSPECIES());
1758 if (preferredFlag
) {
1759 parent
= saveOrUpdateClassification(species
, subspecies
, state
);
1762 if (preferredFlag
) {
1763 saveOrUpdateClassification(parent
, taxon
, state
);
1768 * Link a parent to a child and save it in the current classification
1769 * @param parent: the higher Taxon
1770 * @param child : the lower (or current) Taxon
1771 * return the Taxon from the new created Node
1774 private Taxon
saveOrUpdateClassification(Taxon parent
, Taxon child
, Abcd206ImportState state
) {
1775 TaxonNode node
=null;
1776 if (parent
!= null) {
1777 parent
= (Taxon
) getTaxonService().find(parent
.getUuid());
1778 child
= (Taxon
) getTaxonService().find(child
.getUuid());
1779 //here we do not have to check if the taxon nodes already exists
1780 //this is done by classification.addParentChild()
1781 //do not add child node if it already exists
1782 if(hasTaxonNodeInClassification(child
)){
1785 node
= classification
.addParentChild(parent
, child
, ref
, "");
1788 child
= (Taxon
) getTaxonService().find(child
.getUuid());
1789 //do not add child node if it already exists
1790 if(hasTaxonNodeInClassification(child
)){
1793 node
=classification
.addChildTaxon(child
, ref
, null);
1795 save(classification
, state
);
1796 return node
.getTaxon();
1800 * Parse automatically the scientific name
1801 * @param scientificName: the scientific name to parse
1802 * @return a parsed name
1804 private NonViralName
<?
> parseScientificName(String scientificName
) {
1805 NonViralNameParserImpl nvnpi
= NonViralNameParserImpl
.NewInstance();
1806 NonViralName
<?
> taxonName
= null;
1807 boolean problem
= false;
1810 logger
.info("parseScientificName " + dataHolder
.nomenclatureCode
.toString());
1813 if (dataHolder
.nomenclatureCode
.toString().equals("Zoological") || dataHolder
.nomenclatureCode
.toString().contains("ICZN")) {
1814 taxonName
= nvnpi
.parseFullName(scientificName
, NomenclaturalCode
.ICZN
, null);
1815 if (taxonName
.hasProblem()) {
1819 if (dataHolder
.nomenclatureCode
.toString().equals("Botanical") || dataHolder
.nomenclatureCode
.toString().contains("ICBN")) {
1820 taxonName
= nvnpi
.parseFullName(scientificName
, NomenclaturalCode
.ICNAFP
, null);
1821 if (taxonName
.hasProblem()) {
1825 if (dataHolder
.nomenclatureCode
.toString().equals("Bacterial") || dataHolder
.nomenclatureCode
.toString().contains("ICBN")) {
1826 taxonName
= nvnpi
.parseFullName(scientificName
, NomenclaturalCode
.ICNB
, null);
1827 if (taxonName
.hasProblem()) {
1831 if (dataHolder
.nomenclatureCode
.toString().equals("Cultivar") || dataHolder
.nomenclatureCode
.toString().contains("ICNCP")) {
1832 taxonName
= nvnpi
.parseFullName(scientificName
, NomenclaturalCode
.ICNCP
, null);
1833 if (taxonName
.hasProblem()) {
1838 logger
.info("Parsing with problem in parseScientificName " + scientificName
);
1846 * Create the name without automatic parsing, either because it failed, or because the user deactivated it.
1847 * The name is built upon the ABCD fields
1848 * @param atomisedMap : the ABCD atomised fields
1849 * @param fullName : the full scientific name
1850 * @return the corresponding Botanical or Zoological or... name
1852 private NonViralName
<?
> setTaxonNameByType(
1853 HashMap
<String
, String
> atomisedMap
, String fullName
) {
1854 boolean problem
= false;
1856 logger
.info("settaxonnamebytype " + dataHolder
.nomenclatureCode
.toString());
1859 if (dataHolder
.nomenclatureCode
.equals("Zoological")) {
1860 NonViralName
<ZoologicalName
> taxonName
= ZoologicalName
.NewInstance(null);
1861 taxonName
.setFullTitleCache(fullName
, true);
1862 taxonName
.setGenusOrUninomial(NB(getFromMap(atomisedMap
, "Genus")));
1863 taxonName
.setInfraGenericEpithet(NB(getFromMap(atomisedMap
, "SubGenus")));
1864 taxonName
.setSpecificEpithet(NB(getFromMap(atomisedMap
,"SpeciesEpithet")));
1865 taxonName
.setInfraSpecificEpithet(NB(getFromMap(atomisedMap
,"SubspeciesEpithet")));
1867 if (taxonName
.getGenusOrUninomial() != null){
1868 taxonName
.setRank(Rank
.GENUS());
1871 if (taxonName
.getInfraGenericEpithet() != null){
1872 taxonName
.setRank(Rank
.SUBGENUS());
1875 if (taxonName
.getSpecificEpithet() != null){
1876 taxonName
.setRank(Rank
.SPECIES());
1879 if (taxonName
.getInfraSpecificEpithet() != null){
1880 taxonName
.setRank(Rank
.SUBSPECIES());
1884 if (getFromMap(atomisedMap
, "AuthorTeamParenthesis") != null) {
1885 team
= Team
.NewInstance();
1886 team
.setTitleCache(getFromMap(atomisedMap
, "AuthorTeamParenthesis"), true);
1889 if (getFromMap(atomisedMap
, "AuthorTeamAndYear") != null) {
1890 team
= Team
.NewInstance();
1891 team
.setTitleCache(getFromMap(atomisedMap
, "AuthorTeamAndYear"), true);
1895 taxonName
.setBasionymAuthorTeam(team
);
1898 if (getFromMap(atomisedMap
, "AuthorTeamParenthesis") != null) {
1899 taxonName
.setAuthorshipCache(getFromMap(atomisedMap
, "AuthorTeamParenthesis"));
1901 else if (getFromMap(atomisedMap
, "AuthorTeamAndYear") != null) {
1902 taxonName
.setAuthorshipCache(getFromMap(atomisedMap
, "AuthorTeamAndYear"));
1905 if (getFromMap(atomisedMap
, "CombinationAuthorTeamAndYear") != null) {
1906 team
= Team
.NewInstance();
1907 team
.setTitleCache(getFromMap(atomisedMap
, "CombinationAuthorTeamAndYear"), true);
1908 taxonName
.setCombinationAuthorTeam(team
);
1910 if (taxonName
.hasProblem()) {
1911 logger
.info("pb ICZN");
1918 else if (dataHolder
.nomenclatureCode
.equals("Botanical")) {
1919 BotanicalName taxonName
= (BotanicalName
) parseScientificName(fullName
);
1920 if (taxonName
!= null){
1924 taxonName
= BotanicalName
.NewInstance(null);
1926 taxonName
.setFullTitleCache(fullName
, true);
1927 taxonName
.setGenusOrUninomial(NB(getFromMap(atomisedMap
, "Genus")));
1928 taxonName
.setInfraGenericEpithet(NB(getFromMap(atomisedMap
, "FirstEpithet")));
1929 taxonName
.setInfraSpecificEpithet(NB(getFromMap(atomisedMap
, "InfraSpeEpithet")));
1931 taxonName
.setRank(Rank
.getRankByName(getFromMap(atomisedMap
, "Rank")));
1932 } catch (Exception e
) {
1933 if (taxonName
.getGenusOrUninomial() != null){
1934 taxonName
.setRank(Rank
.GENUS());
1936 else if (taxonName
.getInfraGenericEpithet() != null){
1937 taxonName
.setRank(Rank
.SUBGENUS());
1939 else if (taxonName
.getSpecificEpithet() != null){
1940 taxonName
.setRank(Rank
.SPECIES());
1942 else if (taxonName
.getInfraSpecificEpithet() != null){
1943 taxonName
.setRank(Rank
.SUBSPECIES());
1947 if (getFromMap(atomisedMap
, "AuthorTeamParenthesis") != null) {
1948 team
= Team
.NewInstance();
1949 team
.setTitleCache(getFromMap(atomisedMap
, "AuthorTeamParenthesis"), true);
1950 taxonName
.setBasionymAuthorTeam(team
);
1952 if (getFromMap(atomisedMap
, "AuthorTeam") != null) {
1953 team
= Team
.NewInstance();
1954 team
.setTitleCache(getFromMap(atomisedMap
, "AuthorTeam"), true);
1955 taxonName
.setCombinationAuthorTeam(team
);
1958 if (getFromMap(atomisedMap
, "AuthorTeamParenthesis") != null) {
1959 taxonName
.setAuthorshipCache(getFromMap(atomisedMap
, "AuthorTeamParenthesis"));
1961 else if (getFromMap(atomisedMap
, "AuthorTeam") != null) {
1962 taxonName
.setAuthorshipCache(getFromMap(atomisedMap
, "AuthorTeam"));
1965 if (getFromMap(atomisedMap
, "CombinationAuthorTeamAndYear") != null) {
1966 team
= Team
.NewInstance();
1967 team
.setTitleCache(getFromMap(atomisedMap
, "CombinationAuthorTeamAndYear"), true);
1968 taxonName
.setCombinationAuthorTeam(team
);
1970 if (taxonName
.hasProblem()) {
1971 logger
.info("pb ICBN");
1978 else if (dataHolder
.nomenclatureCode
.equals("Bacterial")) {
1979 NonViralName
<BacterialName
> taxonName
= BacterialName
.NewInstance(null);
1980 taxonName
.setFullTitleCache(fullName
, true);
1981 taxonName
.setGenusOrUninomial(getFromMap(atomisedMap
, "Genus"));
1982 taxonName
.setInfraGenericEpithet(NB(getFromMap(atomisedMap
, "SubGenus")));
1983 taxonName
.setSpecificEpithet(NB(getFromMap(atomisedMap
, "Species")));
1984 taxonName
.setInfraSpecificEpithet(NB(getFromMap(atomisedMap
, "SubspeciesEpithet")));
1986 if (taxonName
.getGenusOrUninomial() != null){
1987 taxonName
.setRank(Rank
.GENUS());
1989 else if (taxonName
.getInfraGenericEpithet() != null){
1990 taxonName
.setRank(Rank
.SUBGENUS());
1992 else if (taxonName
.getSpecificEpithet() != null){
1993 taxonName
.setRank(Rank
.SPECIES());
1995 else if (taxonName
.getInfraSpecificEpithet() != null){
1996 taxonName
.setRank(Rank
.SUBSPECIES());
1999 if (getFromMap(atomisedMap
, "AuthorTeamAndYear") != null) {
2000 Team team
= Team
.NewInstance();
2001 team
.setTitleCache(getFromMap(atomisedMap
, "AuthorTeamAndYear"), true);
2002 taxonName
.setCombinationAuthorTeam(team
);
2004 if (getFromMap(atomisedMap
, "ParentheticalAuthorTeamAndYear") != null) {
2005 Team team
= Team
.NewInstance();
2006 team
.setTitleCache(getFromMap(atomisedMap
, "ParentheticalAuthorTeamAndYear"), true);
2007 taxonName
.setBasionymAuthorTeam(team
);
2009 if (taxonName
.hasProblem()) {
2010 logger
.info("pb ICNB");
2017 else if (dataHolder
.nomenclatureCode
.equals("Cultivar")) {
2018 CultivarPlantName taxonName
= CultivarPlantName
.NewInstance(null);
2020 if (taxonName
.hasProblem()) {
2021 logger
.info("pb ICNCP");
2031 logger
.info("Problem im setTaxonNameByType ");
2032 NonViralName
<?
> taxonName
= NonViralName
.NewInstance(null);
2033 taxonName
.setFullTitleCache(fullName
, true);
2036 NonViralName
<?
> tn
= NonViralName
.NewInstance(null);
2042 * Get a formated string from a hashmap
2043 * @param atomisedMap
2047 private String
getFromMap(HashMap
<String
, String
> atomisedMap
, String key
) {
2048 String value
= null;
2049 if (atomisedMap
.containsKey(key
)) {
2050 value
= atomisedMap
.get(key
);
2054 if (value
!= null && key
.matches(".*Year.*")) {
2055 value
= value
.trim();
2056 if (value
.matches("[a-z A-Z ]*[0-9]{4}$")) {
2057 String tmp
= value
.split("[0-9]{4}$")[0];
2058 int year
= Integer
.parseInt(value
.split(tmp
)[1]);
2071 catch (Exception e
) {
2077 // private void compareABCDtoCDM(URI urlFileName, List<String> knownElts, Abcd206XMLFieldGetter abcdFieldGetter) {
2079 // DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
2080 // DocumentBuilder constructeur = factory.newDocumentBuilder();
2081 // URL url = urlFileName.toURL();
2082 // Object o = url.getContent();
2083 // InputStream is = (InputStream) o;
2084 // Document document = constructeur.parse(is);
2085 // Element root = document.getDocumentElement();
2086 // abcdFieldGetter.traverse(root);
2088 // catch (ParserConfigurationException e){
2089 // e.printStackTrace();
2091 // catch (SAXException e) {
2092 // e.printStackTrace();
2094 // catch (IOException e) {
2095 // e.printStackTrace();
2097 // Set<String> elts = dataHolder.allABCDelements.keySet();
2098 // Iterator<String> it = elts.iterator();
2100 // while (it.hasNext()) {
2102 // if (knownElts.indexOf(elt) == -1) {
2104 // logger.info("Unmerged ABCD element: " + elt + " - "+ dataHolder.allABCDelements.get(elt));
2111 * Load the list of names from the ABCD file and save them
2112 * @param state : the current ABCD import state
2113 * @param unitsList : the unit list from the ABCD file
2114 * @param abcdFieldGetter : the ABCD parser
2116 private void prepareCollectors(Abcd206ImportState state
, NodeList unitsList
, Abcd206XMLFieldGetter abcdFieldGetter
) {
2117 List
<String
> collectors
= new ArrayList
<String
>();
2118 List
<String
> teams
= new ArrayList
<String
>();
2119 List
<List
<String
>> collectorinteams
= new ArrayList
<List
<String
>>();
2121 for (int i
= 0; i
< unitsList
.getLength(); i
++) {
2122 this.getCollectorsFromXML((Element
) unitsList
.item(i
), abcdFieldGetter
);
2123 for (String agent
: dataHolder
.gatheringAgentList
) {
2124 collectors
.add(agent
);
2126 List
<String
> tmpTeam
= new ArrayList
<String
>(new HashSet
<String
>(dataHolder
.gatheringTeamList
));
2127 if(!tmpTeam
.isEmpty()) {
2128 teams
.add(StringUtils
.join(tmpTeam
.toArray()," & "));
2130 for (String agent
:tmpTeam
) {
2131 collectors
.add(agent
);
2135 List
<String
> collectorsU
= new ArrayList
<String
>(new HashSet
<String
>(collectors
));
2136 List
<String
> teamsU
= new ArrayList
<String
>(new HashSet
<String
>(teams
));
2139 //existing teams in DB
2140 Map
<String
,Team
> titleCacheTeam
= new HashMap
<String
, Team
>();
2141 List
<UuidAndTitleCache
<Team
>> hiberTeam
= getAgentService().getTeamUuidAndTitleCache();
2143 Set
<UUID
> uuids
= new HashSet
<UUID
>();
2144 for (UuidAndTitleCache
<Team
> hibernateT
:hiberTeam
){
2145 uuids
.add(hibernateT
.getUuid());
2147 if (!uuids
.isEmpty()){
2148 List
<AgentBase
> existingTeams
= getAgentService().find(uuids
);
2149 for (AgentBase
<?
> existingP
:existingTeams
){
2150 titleCacheTeam
.put(existingP
.getTitleCache(),CdmBase
.deproxy(existingP
,Team
.class));
2155 Map
<String
,UUID
> teamMap
= new HashMap
<String
, UUID
>();
2156 for (UuidAndTitleCache
<Team
> uuidt
:hiberTeam
){
2157 teamMap
.put(uuidt
.getTitleCache(), uuidt
.getUuid());
2160 //existing persons in DB
2161 List
<UuidAndTitleCache
<Person
>> hiberPersons
= getAgentService().getPersonUuidAndTitleCache();
2162 Map
<String
,Person
> titleCachePerson
= new HashMap
<String
, Person
>();
2163 uuids
= new HashSet
<UUID
>();
2164 for (UuidAndTitleCache
<Person
> hibernateP
:hiberPersons
){
2165 uuids
.add(hibernateP
.getUuid());
2168 if (!uuids
.isEmpty()){
2169 List
<AgentBase
> existingPersons
= getAgentService().find(uuids
);
2170 for (AgentBase
<?
> existingP
:existingPersons
){
2171 titleCachePerson
.put(existingP
.getTitleCache(),CdmBase
.deproxy(existingP
,Person
.class));
2175 Map
<String
,UUID
> personMap
= new HashMap
<String
, UUID
>();
2176 for (UuidAndTitleCache
<Person
> person
:hiberPersons
){
2177 personMap
.put(person
.getTitleCache(), person
.getUuid());
2180 java
.util
.Collection
<Person
> personToadd
= new ArrayList
<Person
>();
2181 java
.util
.Collection
<Team
> teamToAdd
= new ArrayList
<Team
>();
2183 for (String collector
:collectorsU
){
2184 Person p
= Person
.NewInstance();
2185 p
.setTitleCache(collector
,true);
2186 if (!personMap
.containsKey(p
.getTitleCache())){
2190 for (String team
:teamsU
){
2191 Team p
= Team
.NewInstance();
2192 p
.setTitleCache(team
,true);
2193 if (!teamMap
.containsKey(p
.getTitleCache())){
2198 if(!personToadd
.isEmpty()){
2199 for (Person agent
: personToadd
){
2201 titleCachePerson
.put(agent
.getTitleCache(),CdmBase
.deproxy(agent
, Person
.class) );
2206 Map
<String
,Integer
>teamdone
= new HashMap
<String
, Integer
>();
2207 for (List
<String
> collteam
: collectorinteams
){
2208 if (!teamdone
.containsKey(StringUtils
.join(collteam
.toArray(),"-"))){
2209 Team team
= new Team();
2211 for (String collector
:collteam
){
2212 ptmp
= Person
.NewInstance();
2213 ptmp
.setTitleCache(collector
,true);
2214 Person p2
= titleCachePerson
.get(ptmp
.getTitleCache());
2215 team
.addTeamMember(p2
);
2219 teamToAdd
.add(team
);
2221 teamdone
.put(StringUtils
.join(collteam
.toArray(),"-"),0);
2225 if(!teamToAdd
.isEmpty()){
2226 for (Team agent
: teamToAdd
){
2228 titleCacheTeam
.put(agent
.getTitleCache(), CdmBase
.deproxy( agent
,Team
.class) );
2232 state
.getConfig().setTeams(titleCacheTeam
);
2233 state
.getConfig().setPersons(titleCachePerson
);
2237 protected boolean isIgnore(Abcd206ImportState state
) {