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
.DefinedTermBase
;
46 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableSource
;
47 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
48 import eu
.etaxonomy
.cdm
.model
.common
.LanguageString
;
49 import eu
.etaxonomy
.cdm
.model
.common
.OriginalSourceBase
;
50 import eu
.etaxonomy
.cdm
.model
.common
.OriginalSourceType
;
51 import eu
.etaxonomy
.cdm
.model
.common
.UuidAndTitleCache
;
52 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
53 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementSource
;
54 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
55 import eu
.etaxonomy
.cdm
.model
.description
.IndividualsAssociation
;
56 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
57 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
58 import eu
.etaxonomy
.cdm
.model
.media
.Media
;
59 import eu
.etaxonomy
.cdm
.model
.name
.BacterialName
;
60 import eu
.etaxonomy
.cdm
.model
.name
.BotanicalName
;
61 import eu
.etaxonomy
.cdm
.model
.name
.CultivarPlantName
;
62 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCode
;
63 import eu
.etaxonomy
.cdm
.model
.name
.NonViralName
;
64 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
65 import eu
.etaxonomy
.cdm
.model
.name
.SpecimenTypeDesignation
;
66 import eu
.etaxonomy
.cdm
.model
.name
.SpecimenTypeDesignationStatus
;
67 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
68 import eu
.etaxonomy
.cdm
.model
.name
.ZoologicalName
;
69 import eu
.etaxonomy
.cdm
.model
.occurrence
.Collection
;
70 import eu
.etaxonomy
.cdm
.model
.occurrence
.DerivedUnit
;
71 import eu
.etaxonomy
.cdm
.model
.occurrence
.DeterminationEvent
;
72 import eu
.etaxonomy
.cdm
.model
.occurrence
.GatheringEvent
;
73 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
74 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationType
;
75 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
76 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceFactory
;
77 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
78 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
79 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
80 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
81 import eu
.etaxonomy
.cdm
.persistence
.query
.MatchMode
;
82 import eu
.etaxonomy
.cdm
.strategy
.parser
.NonViralNameParserImpl
;
89 public class Abcd206Import
extends SpecimenImportBase
<Abcd206ImportConfigurator
, Abcd206ImportState
> {
90 private static final Logger logger
= Logger
.getLogger(Abcd206Import
.class);
92 private final boolean DEBUG
= true;
94 private static final String PREFERRED
= "_preferred_";
95 private static final String CODE
= "_code_";
96 private static final String COLON
= ":";
97 private static final String SPLITTER
= "--";
98 private static String prefix
= "";
100 //TODO make all fields ABCD206ImportState variables
101 private Classification classification
= null;
102 private Reference
<?
> ref
= null;
104 private Abcd206DataHolder dataHolder
;
105 private DerivedUnit derivedUnitBase
;
107 private List
<OriginalSourceBase
<?
>> associationRefs
= new ArrayList
<OriginalSourceBase
<?
>>();
108 boolean associationSourcesSet
=false;
109 private List
<OriginalSourceBase
<?
>> descriptionRefs
= new ArrayList
<OriginalSourceBase
<?
>>();
110 boolean descriptionSourcesSet
=false;
111 private List
<OriginalSourceBase
<?
>> derivedUnitSources
= new ArrayList
<OriginalSourceBase
<?
>>();
112 boolean derivedUnitSourcesSet
=false;
113 private boolean descriptionGroupSet
= false;
114 private TaxonDescription descriptionGroup
= null;
116 public Abcd206Import() {
121 * TODO: quick and dirty fix to avoid NonUniqueObjectExceptions
122 * They occurred because this import is a bean therefore a singleton but uses
123 * class variables which should be unique for every single import.
125 private void resetFields(){
126 classification
= null;
130 derivedUnitBase
= null;
132 associationRefs
= new ArrayList
<OriginalSourceBase
<?
>>();
133 associationSourcesSet
=false;
134 descriptionRefs
= new ArrayList
<OriginalSourceBase
<?
>>();
135 descriptionSourcesSet
=false;
136 derivedUnitSources
= new ArrayList
<OriginalSourceBase
<?
>>();
137 derivedUnitSourcesSet
=false;
138 descriptionGroupSet
= false;
139 descriptionGroup
= null;
143 protected boolean doCheck(Abcd206ImportState state
) {
144 logger
.warn("Checking not yet implemented for " + this.getClass().getSimpleName());
150 @SuppressWarnings("rawtypes")
151 public void doInvoke(Abcd206ImportState state
) {
154 state
.setTx(startTransaction());
155 logger
.info("INVOKE Specimen Import from ABCD2.06 XML ");
157 SpecimenUserInteraction sui
= state
.getConfig().getSpecimenUserInteraction();
159 List
<Reference
> references
= getReferenceService().list(Reference
.class, null, null, null, null);
161 if (state
.getConfig().isInteractWithUser()){
162 Map
<String
,Reference
> refMap
= new HashMap
<String
, Reference
>();
163 for (Reference reference
: references
) {
164 if (! StringUtils
.isBlank(reference
.getTitleCache())) {
165 refMap
.put(reference
.getTitleCache(),reference
);
168 ref
= sui
.askForReference(refMap
);
171 String cla
= sui
.createNewReference();
172 if (refMap
.get(cla
)!= null) {
173 ref
= refMap
.get(cla
);
175 ref
= ReferenceFactory
.newGeneric();
180 ref
= getReferenceService().find(ref
.getUuid());
184 String name
= NB(state
.getConfig().getSourceReferenceTitle());
185 for (Reference reference
: references
) {
186 if (! StringUtils
.isBlank(reference
.getTitleCache())) {
187 if (reference
.getTitleCache().equalsIgnoreCase(name
)) {
189 // System.out.println("FIND SAME REFERENCE");
194 ref
= ReferenceFactory
.newGeneric();
195 ref
.setTitle("ABCD classic");
200 state
.getConfig().setSourceReference(ref
);
202 List
<Classification
> classificationList
= getClassificationService().list(Classification
.class, null, null, null, null);
203 if (state
.getConfig().isUseClassification() && state
.getConfig().isInteractWithUser()){
204 Map
<String
,Classification
> classMap
= new HashMap
<String
, Classification
>();
205 for (Classification tree
: classificationList
) {
206 if (! StringUtils
.isBlank(tree
.getTitleCache())) {
207 classMap
.put(tree
.getTitleCache(),tree
);
210 classification
= sui
.askForClassification(classMap
);
211 if (classification
== null){
212 String cla
= sui
.createNewClassification();
213 if (classMap
.get(cla
)!= null) {
214 classification
= classMap
.get(cla
);
216 classification
= Classification
.NewInstance(cla
, ref
, Language
.DEFAULT());
219 save(classification
, state
);
222 if (classification
== null) {
223 String name
= NB(state
.getConfig().getClassificationName());
224 for (Classification classif
: classificationList
){
225 if (classif
.getTitleCache().equalsIgnoreCase(name
) && classif
.getCitation().equals(ref
)) {
226 classification
=classif
;
227 // System.out.println("FIND SAME CLASSIF");
230 if (classification
== null){
231 classification
= Classification
.NewInstance(name
, ref
, Language
.DEFAULT());
233 // if (state.getConfig().getClassificationUuid() != null) {
234 // classification.setUuid(state.getConfig().getClassificationUuid());
236 save(classification
, state
);
240 InputStream source
= state
.getConfig().getSource();
241 NodeList unitsList
= getUnitsNodeList(source
);
243 if (unitsList
!= null) {
244 String message
= "nb units to insert: " + unitsList
.getLength();
245 logger
.info(message
);
246 updateProgress(state
, message
);
248 dataHolder
= new Abcd206DataHolder();
250 Abcd206XMLFieldGetter abcdFieldGetter
= new Abcd206XMLFieldGetter(dataHolder
, prefix
);
252 prepareCollectors(state
, unitsList
, abcdFieldGetter
);
254 associationRefs
= new ArrayList
<OriginalSourceBase
<?
>>();
255 descriptionRefs
= new ArrayList
<OriginalSourceBase
<?
>>();
256 derivedUnitSources
= new ArrayList
<OriginalSourceBase
<?
>>();
258 for (int i
= 0; i
< unitsList
.getLength(); i
++) {
259 System
.out
.println("------------------------------------------------------------------------------------------");
261 this.setUnitPropertiesXML( (Element
) unitsList
.item(i
), abcdFieldGetter
);
262 // refreshTransaction(state);
263 this.handleSingleUnit(state
);
265 // compare the ABCD elements added in to the CDM and the
266 // unhandled ABCD elements
267 //compareABCDtoCDM(sourceName, dataHolder.knownABCDelements, abcdFieldGetter);
269 // reset the ABCD elements added in CDM
270 // knownABCDelements = new ArrayList<String>();
271 dataHolder
.allABCDelements
= new HashMap
<String
, String
>();
273 getReferenceService().deduplicate(Reference
.class, null, null);
274 getClassificationService().deduplicate(Classification
.class, null, null);
276 commitTransaction(state
.getTx());
282 protected NodeList
getUnitsNodeList(URI source
) {
284 InputStream is
= UriUtils
.getInputStream(source
);
285 return getUnitsNodeList(is
);
286 } catch (Exception e
) {
288 throw new RuntimeException(e
);
293 * Return the list of root nodes for an ABCD 2.06 XML file
294 * @param fileName: the file's location
295 * @return the list of root nodes ("Unit")
297 protected NodeList
getUnitsNodeList(InputStream inputStream
) {
298 NodeList unitList
= null;
300 DocumentBuilderFactory factory
= DocumentBuilderFactory
.newInstance();
301 DocumentBuilder builder
= factory
.newDocumentBuilder();
303 Document document
= builder
.parse(inputStream
);
304 Element root
= document
.getDocumentElement();
305 unitList
= root
.getElementsByTagName("Unit");
306 if (unitList
.getLength() == 0) {
307 unitList
= root
.getElementsByTagName("abcd:Unit");
310 } catch (Exception e
) {
317 * Handle a single unit
320 @SuppressWarnings("rawtypes")
321 private void handleSingleUnit(Abcd206ImportState state
) {
323 logger
.info("handleSingleUnit "+ref
);
326 updateProgress(state
, "Importing data for unit: " + dataHolder
.unitID
);
329 DerivedUnitFacade derivedUnitFacade
= getFacade();
330 derivedUnitBase
= derivedUnitFacade
.innerDerivedUnit();
336 ICdmApplicationConfiguration cdmAppController
= state
.getConfig().getCdmAppController();
337 if(cdmAppController
==null){
338 cdmAppController
= this;
340 UnitsGatheringEvent unitsGatheringEvent
= new UnitsGatheringEvent(cdmAppController
.getTermService(), dataHolder
.locality
, dataHolder
.languageIso
,
341 dataHolder
.longitude
, dataHolder
.latitude
, dataHolder
.gatheringAgentList
, dataHolder
.gatheringTeamList
,state
.getConfig());
344 UnitsGatheringArea unitsGatheringArea
= new UnitsGatheringArea();
345 // unitsGatheringArea.setConfig(state.getConfig(),getOccurrenceService(), getTermService());
346 unitsGatheringArea
.setParams(dataHolder
.isocountry
, dataHolder
.country
, state
.getConfig(), cdmAppController
.getTermService(), getOccurrenceService());
348 DefinedTermBase
<?
> areaCountry
= unitsGatheringArea
.getCountry();
351 unitsGatheringArea
= new UnitsGatheringArea();
352 // unitsGatheringArea.setConfig(state.getConfig(),getOccurrenceService(),getTermService());
353 unitsGatheringArea
.setAreas(dataHolder
.namedAreaList
,state
.getConfig(), cdmAppController
.getTermService());
354 ArrayList
<DefinedTermBase
> nas
= unitsGatheringArea
.getAreas();
355 for (DefinedTermBase namedArea
: nas
) {
356 unitsGatheringEvent
.addArea(namedArea
);
359 // copy gathering event to facade
360 GatheringEvent gatheringEvent
= unitsGatheringEvent
.getGatheringEvent();
361 derivedUnitFacade
.setLocality(gatheringEvent
.getLocality());
362 derivedUnitFacade
.setExactLocation(gatheringEvent
.getExactLocation());
363 derivedUnitFacade
.setCollector(gatheringEvent
.getCollector());
364 derivedUnitFacade
.setCountry((NamedArea
)areaCountry
);
366 for(DefinedTermBase
<?
> area
:unitsGatheringArea
.getAreas()){
367 derivedUnitFacade
.addCollectingArea((NamedArea
) area
);
369 // derivedUnitFacade.addCollectingAreas(unitsGatheringArea.getAreas());
373 derivedUnitFacade
.setFieldNumber(NB(dataHolder
.fieldNumber
));
375 // //add Multimedia URLs
376 if (dataHolder
.multimediaObjects
.size() != -1) {
377 for (String multimediaObject
: dataHolder
.multimediaObjects
) {
380 media
= getImageMedia(multimediaObject
, READ_MEDIA_DATA
);
381 derivedUnitFacade
.addDerivedUnitMedia(media
);
382 } catch (MalformedURLException e
) {
383 // TODO Auto-generated catch block
391 // * merge AND STORE DATA
393 // getTermService().saveOrUpdate(areaCountry);// TODO save area sooner
395 // for (NamedArea area : otherAreas) {
396 // getTermService().saveOrUpdate(area);// merge it sooner (foreach area)
399 save(unitsGatheringEvent
.getLocality(), state
);
401 // handle collection data
402 setCollectionData(state
, derivedUnitFacade
);
405 SpecimenUserInteraction sui
= state
.getConfig().getSpecimenUserInteraction();
406 Map
<String
,OriginalSourceBase
<?
>> sourceMap
= new HashMap
<String
, OriginalSourceBase
<?
>>();
408 dataHolder
.docSources
= new ArrayList
<String
>();
409 for (String
[] fullReference
: dataHolder
.referenceList
) {
410 String strReference
=fullReference
[0];
411 String citationDetail
= fullReference
[1];
412 String citationURL
= fullReference
[2];
414 if (!citationURL
.isEmpty()) {
415 citationDetail
+=", "+citationURL
;
418 Reference
<?
> reference
= ReferenceFactory
.newGeneric();
419 reference
.setTitle(strReference
);
421 IdentifiableSource sour
= getIdentifiableSource(reference
,citationDetail
);
424 if (sour
.getCitation() != null){
425 if(StringUtils
.isNotBlank(sour
.getCitationMicroReference())) {
426 dataHolder
.docSources
.add(sour
.getCitation().getTitleCache()+ "---"+sour
.getCitationMicroReference());
428 dataHolder
.docSources
.add(sour
.getCitation().getTitleCache());
434 reference
.addSource(sour
);
435 save(reference
, state
);
439 List
<IdentifiableSource
> issTmp
= getCommonService().list(IdentifiableSource
.class, null, null, null, null);
440 List
<DescriptionElementSource
> issTmp2
= getCommonService().list(DescriptionElementSource
.class, null, null, null, null);
442 Set
<OriginalSourceBase
> osbSet
= new HashSet
<OriginalSourceBase
>();
444 osbSet
.addAll(issTmp2
);
447 osbSet
.addAll(issTmp
);
451 for( OriginalSourceBase
<?
> osb
:osbSet
) {
452 if(osb
.getCitationMicroReference() !=null && !osb
.getCitationMicroReference().isEmpty()) {
454 sourceMap
.put(osb
.getCitation().getTitleCache()+ "---"+osb
.getCitationMicroReference(),osb
);
455 }catch(NullPointerException e
){logger
.warn("null pointer problem (no ref?) with "+osb
);}
458 sourceMap
.put(osb
.getCitation().getTitleCache(),osb
);
459 }catch(NullPointerException e
){logger
.warn("null pointer problem (no ref?) with "+osb
);}
463 if( state
.getConfig().isInteractWithUser()){
464 List
<OriginalSourceBase
<?
>>sources
=null;
465 if(!derivedUnitSourcesSet
){
466 sources
= sui
.askForSource(sourceMap
, "the unit itself","",getReferenceService(), dataHolder
.docSources
);
467 derivedUnitSources
=sources
;
468 derivedUnitSourcesSet
=true;
471 sources
=derivedUnitSources
;
473 // System.out.println("nb sources: "+sources.size());
474 // System.out.println("derivedunitfacade : "+derivedUnitFacade.getTitleCache());
475 for (OriginalSourceBase
<?
> sour
:sources
){
476 if(sour
.isInstanceOf(IdentifiableSource
.class)){
477 if(sourceNotLinkedToElement(derivedUnitFacade
,sour
)) {
478 // System.out.println("add source to derivedunitfacade1 "+derivedUnitFacade.getTitleCache());
479 derivedUnitFacade
.addSource((IdentifiableSource
)sour
.clone());
482 if(sourceNotLinkedToElement(derivedUnitFacade
,sour
)) {
483 // System.out.println("add source to derivedunitfacade2 "+derivedUnitFacade.getTitleCache());
484 derivedUnitFacade
.addSource(OriginalSourceType
.Import
,sour
.getCitation(),sour
.getCitationMicroReference(), ioName
);
489 for (OriginalSourceBase
<?
> sr
: sourceMap
.values()){
490 if(sr
.isInstanceOf(IdentifiableSource
.class)){
491 if(sourceNotLinkedToElement(derivedUnitFacade
,sr
)) {
492 // System.out.println("add source to derivedunitfacade3 "+derivedUnitFacade.getTitleCache());
493 derivedUnitFacade
.addSource((IdentifiableSource
)sr
.clone());
496 if(sourceNotLinkedToElement(derivedUnitFacade
,sr
)) {
497 // System.out.println("add source to derivedunitfacade4 "+derivedUnitFacade.getTitleCache());
498 derivedUnitFacade
.addSource(OriginalSourceType
.Import
,sr
.getCitation(),sr
.getCitationMicroReference(), ioName
);
504 save(derivedUnitBase
, state
);
506 // handle identifications
507 handleIdentifications(state
, derivedUnitFacade
);
510 logger
.info("saved ABCD specimen ...");
513 } catch (Exception e
) {
514 logger
.warn("Error when reading record!!");
516 state
.setUnsuccessfull();
523 * @param derivedUnitFacade
527 private boolean sourceNotLinkedToElement(DerivedUnitFacade derivedUnitFacade
, OriginalSourceBase
<?
> source
) {
528 Set
<IdentifiableSource
> linkedSources
= derivedUnitFacade
.getSources();
529 for (IdentifiableSource is
:linkedSources
){
530 if (is
.getCitation()!=null && source
.getCitation()!=null &&
531 is
.getCitation().getTitleCache().equalsIgnoreCase(source
.getCitation().getTitleCache())){
532 String isDetail
= is
.getCitationMicroReference();
533 if ((StringUtils
.isBlank(isDetail
) && StringUtils
.isBlank(source
.getCitationMicroReference()))
534 || (isDetail
!= null && isDetail
.equalsIgnoreCase(source
.getCitationMicroReference())) ) {
544 * @param citationDetail
547 //FIXME this method is highly critical, because
548 // * it will have serious performance and memory problems with large databases
549 // (databases may easily have >1 Mio source records)
550 // * it does not make sense to search for existing sources and then clone them
551 // we need to search for existing references instead and use them (if exist)
552 // for our new source.
553 private IdentifiableSource
getIdentifiableSource(Reference
<?
> reference
, String citationDetail
) {
555 List
<IdentifiableSource
> issTmp
= getCommonService().list(IdentifiableSource
.class, null, null, null, null);
558 if (reference
!= null){
560 for (OriginalSourceBase
<?
> osb
: issTmp
){
561 if (osb
.getCitation() != null && reference
!=null && osb
.getCitation().getTitleCache().equalsIgnoreCase(reference
.getTitleCache())){
562 String osbDetail
= osb
.getCitationMicroReference();
563 if ((StringUtils
.isBlank(osbDetail
) && StringUtils
.isBlank(citationDetail
))
564 || (osbDetail
!= null && osbDetail
.equalsIgnoreCase(citationDetail
)) ) {
565 // System.out.println("REFERENCE FOUND RETURN EXISTING SOURCE");
566 return (IdentifiableSource
) osb
.clone();
570 } catch (CloneNotSupportedException e
) {
571 throw new RuntimeException(e
);
572 } catch (Exception e1
){
573 e1
.printStackTrace();
577 IdentifiableSource sour
= IdentifiableSource
.NewInstance(OriginalSourceType
.Import
,null,null, reference
,citationDetail
);
582 // * @param reference
583 // * @param citationDetail
586 // private DescriptionElementSource getDescriptionSource(Reference<?> reference, String citationDetail) {
588 // List<OriginalSourceBase> issTmp2 = getCommonService().list(DescriptionElementSource.class, null, null, null, null);
591 // for (OriginalSourceBase<?> osb:issTmp2){
592 // if (osb.getCitation().equals(reference) && osb.getCitationMicroReference().equalsIgnoreCase(citationDetail)) {
593 // return (DescriptionElementSource) osb.clone();
596 // } catch (CloneNotSupportedException e) {
597 // // TODO Auto-generated catch block
598 // e.printStackTrace();
601 // DescriptionElementSource sour = DescriptionElementSource.NewInstance(OriginalSourceType.Import,null,null, reference,citationDetail);
607 * Very fast and dirty implementation to allow handling of transient objects as described in
608 * https://dev.e-taxonomy.eu/trac/ticket/3726
615 private void save(CdmBase cdmBase
, Abcd206ImportState state
) {
616 ICdmApplicationConfiguration cdmRepository
= state
.getConfig().getCdmAppController();
617 if (cdmRepository
== null){
618 cdmRepository
= this;
621 if (cdmBase
.isInstanceOf(LanguageString
.class)){
622 cdmRepository
.getTermService().saveLanguageData(CdmBase
.deproxy(cdmBase
, LanguageString
.class));
623 }else if (cdmBase
.isInstanceOf(SpecimenOrObservationBase
.class)){
624 cdmRepository
.getOccurrenceService().saveOrUpdate(CdmBase
.deproxy(cdmBase
, SpecimenOrObservationBase
.class));
625 }else if (cdmBase
.isInstanceOf(Reference
.class)){
626 cdmRepository
.getReferenceService().saveOrUpdate(CdmBase
.deproxy(cdmBase
, Reference
.class));
627 }else if (cdmBase
.isInstanceOf(Classification
.class)){
628 cdmRepository
.getClassificationService().saveOrUpdate(CdmBase
.deproxy(cdmBase
, Classification
.class));
629 }else if (cdmBase
.isInstanceOf(AgentBase
.class)){
630 cdmRepository
.getAgentService().saveOrUpdate(CdmBase
.deproxy(cdmBase
, AgentBase
.class));
631 }else if (cdmBase
.isInstanceOf(Collection
.class)){
632 cdmRepository
.getCollectionService().saveOrUpdate(CdmBase
.deproxy(cdmBase
, Collection
.class));
633 }else if (cdmBase
.isInstanceOf(DescriptionBase
.class)){
634 cdmRepository
.getDescriptionService().saveOrUpdate(CdmBase
.deproxy(cdmBase
, DescriptionBase
.class));
635 }else if (cdmBase
.isInstanceOf(TaxonBase
.class)){
636 cdmRepository
.getTaxonService().saveOrUpdate(CdmBase
.deproxy(cdmBase
, TaxonBase
.class));
637 }else if (cdmBase
.isInstanceOf(TaxonNameBase
.class)){
638 cdmRepository
.getNameService().saveOrUpdate(CdmBase
.deproxy(cdmBase
, TaxonNameBase
.class));
640 throw new IllegalArgumentException("Class not supported in save method: " + CdmBase
.deproxy(cdmBase
, CdmBase
.class).getClass().getSimpleName());
646 * setCollectionData : store the collection object into the
651 private void setCollectionData(Abcd206ImportState state
, DerivedUnitFacade derivedUnitFacade
) {
652 // set catalogue number (unitID)
653 derivedUnitFacade
.setCatalogNumber(NB(dataHolder
.unitID
));
654 derivedUnitFacade
.setAccessionNumber(NB(dataHolder
.accessionNumber
));
655 // derivedUnitFacade.setCollectorsNumber(NB(dataHolder.collectorsNumber));
658 * INSTITUTION & COLLECTION
660 // manage institution
661 Institution institution
= this.getInstitution(NB(dataHolder
.institutionCode
), state
);
663 Collection collection
= this.getCollection(institution
, NB(dataHolder
.collectionCode
), state
);
664 // link specimen & collection
665 derivedUnitFacade
.setCollection(collection
);
669 * getFacade : get the DerivedUnitFacade based on the recordBasis
671 * @return DerivedUnitFacade
673 private DerivedUnitFacade
getFacade() {
675 logger
.info("getFacade()");
677 SpecimenOrObservationType type
= null;
680 if (NB((dataHolder
.recordBasis
)) != null) {
681 if (dataHolder
.recordBasis
.toLowerCase().startsWith("s") || dataHolder
.recordBasis
.toLowerCase().indexOf("specimen")>-1) {// specimen
682 type
= SpecimenOrObservationType
.PreservedSpecimen
;
684 if (dataHolder
.recordBasis
.toLowerCase().startsWith("o") ||dataHolder
.recordBasis
.toLowerCase().indexOf("observation")>-1 ) {
685 type
= SpecimenOrObservationType
.Observation
;
687 if (dataHolder
.recordBasis
.toLowerCase().indexOf("fossil")>-1){
688 type
= SpecimenOrObservationType
.Fossil
;
690 if (dataHolder
.recordBasis
.toLowerCase().indexOf("living")>-1) {
691 type
= SpecimenOrObservationType
.LivingSpecimen
;
694 logger
.info("The basis of record does not seem to be known: " + dataHolder
.recordBasis
);
695 type
= SpecimenOrObservationType
.DerivedUnit
;
699 logger
.info("The basis of record is null");
700 type
= SpecimenOrObservationType
.DerivedUnit
;
702 DerivedUnitFacade derivedUnitFacade
= DerivedUnitFacade
.NewInstance(type
);
703 return derivedUnitFacade
;
706 private void getCollectorsFromXML(Element root
, Abcd206XMLFieldGetter abcdFieldGetter
) {
709 group
= root
.getChildNodes();
710 for (int i
= 0; i
< group
.getLength(); i
++) {
711 if (group
.item(i
).getNodeName().equals(prefix
+ "Identifications")) {
712 group
= group
.item(i
).getChildNodes();
716 dataHolder
.gatheringAgentList
= new ArrayList
<String
>();
717 dataHolder
.gatheringTeamList
= new ArrayList
<String
>();
718 abcdFieldGetter
.getType(root
);
719 abcdFieldGetter
.getGatheringPeople(root
);
723 * Store the unit's properties into variables Look which unit is the
724 * preferred one Look what kind of name it is supposed to be, for the
725 * parsing (Botanical, Zoological)
727 * @param racine: the root node for a single unit
729 private void setUnitPropertiesXML(Element root
, Abcd206XMLFieldGetter abcdFieldGetter
) {
733 group
= root
.getChildNodes();
734 for (int i
= 0; i
< group
.getLength(); i
++) {
735 if (group
.item(i
).getNodeName().equals(prefix
+ "Identifications")) {
736 group
= group
.item(i
).getChildNodes();
740 dataHolder
.identificationList
= new ArrayList
<Identification
>();
741 dataHolder
.statusList
= new ArrayList
<SpecimenTypeDesignationStatus
>();
742 dataHolder
.atomisedIdentificationList
= new ArrayList
<HashMap
<String
, String
>>();
743 dataHolder
.referenceList
= new ArrayList
<String
[]>();
744 dataHolder
.multimediaObjects
= new ArrayList
<String
>();
746 abcdFieldGetter
.getScientificNames(group
);
747 abcdFieldGetter
.getType(root
);
750 logger
.info("this.identificationList "+dataHolder
.identificationList
.toString());
752 abcdFieldGetter
.getIDs(root
);
753 abcdFieldGetter
.getRecordBasis(root
);
754 abcdFieldGetter
.getMultimedia(root
);
755 abcdFieldGetter
.getNumbers(root
);
756 abcdFieldGetter
.getGeolocation(root
);
757 abcdFieldGetter
.getGatheringPeople(root
);
758 boolean referencefound
= abcdFieldGetter
.getReferences(root
);
759 if (!referencefound
) {
760 String
[]a
= {ref
.getTitleCache(),"",""};
761 dataHolder
.referenceList
.add(a
);
764 } catch (Exception e
) {
765 logger
.info("Error occured while parsing XML file" + e
);
770 * Look if the Institution does already exist
771 * @param institutionCode: a string with the institutioncode
772 * @param config : the configurator
773 * @return the Institution (existing or new)
775 @SuppressWarnings("rawtypes")
776 private Institution
getInstitution(String institutionCode
, Abcd206ImportState state
) {
777 Institution institution
=null;
778 List
<Institution
> institutions
;
780 institutions
= getAgentService().list(Institution
.class, null, null, null, null);
781 } catch (Exception e
) {
782 institutions
= new ArrayList
<Institution
>();
785 if (institutions
.size() > 0 && state
.getConfig().isReUseExistingMetadata()) {
786 for (Institution institut
:institutions
){
788 if (institut
.getCode().equalsIgnoreCase(institutionCode
)) {
789 institution
=institut
;
791 }catch(Exception e
){logger
.warn("no institution code in the db");}
795 if(institution
!=null) {
796 logger
.info("getinstitution " + institution
.toString());
799 if (institution
== null){
800 // create institution
801 institution
= Institution
.NewInstance();
802 institution
.setCode(institutionCode
);
803 institution
.setTitleCache(institutionCode
, true);
805 save(institution
, state
);
810 * Look if the Collection does already exist
811 * @param collectionCode
812 * @param collectionCode: a string
813 * @param config : the configurator
814 * @return the Collection (existing or new)
816 private Collection
getCollection(Institution institution
, String collectionCode
, Abcd206ImportState state
) {
817 Collection collection
= null;
818 List
<Collection
> collections
;
820 collections
= getCollectionService().list(Collection
.class, null, null, null, null);
821 } catch (Exception e
) {
822 collections
= new ArrayList
<Collection
>();
824 if (collections
.size() > 0 && state
.getConfig().isReUseExistingMetadata()) {
825 for (Collection coll
:collections
){
826 if (coll
.getInstitute() != null) {
827 if (coll
.getCode().equalsIgnoreCase(collectionCode
) && coll
.getInstitute().equals(institution
)) {
834 if(collection
== null){
835 collection
=Collection
.NewInstance();
836 collection
.setCode(collectionCode
);
837 collection
.setInstitute(institution
);
838 collection
.setTitleCache(collectionCode
);
840 save(collection
, state
);
846 * join DeterminationEvent to the Taxon Object
847 * @param state : the ABCD import state
848 * @param taxon: the current Taxon
849 * @param preferredFlag :if the current name is preferred
850 * @param derivedFacade : the derived Unit Facade
852 @SuppressWarnings("rawtypes")
853 private void linkDeterminationEvent(Abcd206ImportState state
, Taxon taxon
, boolean preferredFlag
, DerivedUnitFacade derivedFacade
) {
854 Abcd206ImportConfigurator config
= state
.getConfig();
856 logger
.info("start linkdetermination with taxon:" + taxon
.getUuid()+", "+taxon
);
859 DeterminationEvent determinationEvent
= DeterminationEvent
.NewInstance();
860 determinationEvent
.setTaxon(taxon
);
861 determinationEvent
.setPreferredFlag(preferredFlag
);
863 determinationEvent
.setIdentifiedUnit(derivedUnitBase
);
864 derivedUnitBase
.addDetermination(determinationEvent
);
868 logger
.info("NB TYPES INFO: "+ dataHolder
.statusList
.size());
870 for (SpecimenTypeDesignationStatus specimenTypeDesignationstatus
: dataHolder
.statusList
) {
871 if (specimenTypeDesignationstatus
!= null) {
873 logger
.info("specimenTypeDesignationstatus :"+ specimenTypeDesignationstatus
);
876 ICdmApplicationConfiguration cdmAppController
= config
.getCdmAppController();
877 if(cdmAppController
== null){
878 cdmAppController
= this;
880 specimenTypeDesignationstatus
= (SpecimenTypeDesignationStatus
) cdmAppController
.getTermService().find(specimenTypeDesignationstatus
.getUuid());
882 TaxonNameBase
<?
,?
> name
= taxon
.getName();
883 SpecimenTypeDesignation designation
= SpecimenTypeDesignation
.NewInstance();
885 designation
.setTypeStatus(specimenTypeDesignationstatus
);
886 designation
.setTypeSpecimen(derivedUnitBase
);
887 name
.addTypeDesignation(designation
, true);
890 } catch (Exception e
) {
891 logger
.warn("PB addding SpecimenType " + e
);
894 for (String
[] fullReference
: dataHolder
.referenceList
) {
896 // System.out.println(fullReference);
897 List
<Reference
> references
= getReferenceService().list(Reference
.class, null, null, null, null);
899 String strReference
=fullReference
[0];
900 String citationDetail
= fullReference
[1];
901 String citationURL
= fullReference
[2];
903 if (isNotBlank(strReference
)){
904 Reference
<?
> reference
= null;
905 for (Reference
<?
> refe
: references
) {
906 if (refe
.getTitleCache().equalsIgnoreCase(strReference
)) {
911 if (reference
==null){
912 reference
= ReferenceFactory
.newGeneric();
914 reference.setTitleCache(strReference);
915 System.out.println("reference hasproblem2 "+reference.hasProblem());
916 IdentifiableSource sour = IdentifiableSource.NewInstance(reference,citationDetail);
917 getReferenceService().saveOrUpdate(sour.getCitation());
919 reference
.setTitleCache(strReference
, true);
920 save(reference
, state
);
922 determinationEvent
.addReference(reference
);
924 }catch(Exception e
){logger
.warn("pv getReferenceList "+e
);}
926 save(derivedUnitBase
, state
);
928 if (config
.isAddIndividualsAssociationsSuchAsSpecimenAndObservations()) {
930 logger
.info("isDoCreateIndividualsAssociations");
933 makeIndividualsAssociation(state
, taxon
, determinationEvent
);
934 save(derivedUnitBase
, state
);
939 * create and link each association (specimen, observation..) to the accepted taxon
940 * @param state : the ABCD import state
941 * @param taxon: the current Taxon
942 * @param determinationEvent:the determinationevent
944 @SuppressWarnings("unused")
945 private void makeIndividualsAssociation(Abcd206ImportState state
, Taxon taxon
, DeterminationEvent determinationEvent
) {
946 SpecimenUserInteraction sui
= state
.getConfig().getSpecimenUserInteraction();
949 System
.out
.println("MAKE INDIVIDUALS ASSOCIATION");
952 TaxonDescription taxonDescription
= null;
953 Set
<TaxonDescription
> descriptions
= taxon
.getDescriptions();
954 if (state
.getConfig().isInteractWithUser()){
955 if(!descriptionGroupSet
){
956 taxonDescription
= sui
.askForDescriptionGroup(descriptions
);
957 descriptionGroup
=taxonDescription
;
958 descriptionGroupSet
=true;
960 taxonDescription
=descriptionGroup
;
963 for (TaxonDescription description
: descriptions
){
964 Set
<IdentifiableSource
> sources
= description
.getTaxon().getSources();
965 sources
.addAll(description
.getSources());
966 for (IdentifiableSource source
:sources
){
967 if(ref
.equals(source
.getCitation())) {
968 taxonDescription
= description
;
973 if (taxonDescription
== null){
974 taxonDescription
= TaxonDescription
.NewInstance(taxon
, false);
975 if(sourceNotLinkedToElement(taxonDescription
,ref
,null)) {
976 taxonDescription
.addSource(OriginalSourceType
.Import
, null, null, ref
, null);
978 descriptionGroup
=taxonDescription
;
979 taxon
.addDescription(taxonDescription
);
982 //PREPARE REFERENCE QUESTIONS
984 Map
<String
,OriginalSourceBase
<?
>> sourceMap
= new HashMap
<String
, OriginalSourceBase
<?
>>();
986 List
<IdentifiableSource
> issTmp
= getCommonService().list(IdentifiableSource
.class, null, null, null, null);
987 List
<DescriptionElementSource
> issTmp2
= getCommonService().list(DescriptionElementSource
.class, null, null, null, null);
989 Set
<OriginalSourceBase
> osbSet
= new HashSet
<OriginalSourceBase
>();
991 osbSet
.addAll(issTmp2
);
994 osbSet
.addAll(issTmp
);
998 for( OriginalSourceBase
<?
> osb
:osbSet
) {
999 if(osb
.getCitationMicroReference() !=null && !osb
.getCitationMicroReference().isEmpty()) {
1001 sourceMap
.put(osb
.getCitation().getTitleCache()+ "---"+osb
.getCitationMicroReference(),osb
);
1002 }catch(NullPointerException e
){logger
.warn("null pointer problem (no ref?) with "+osb
);}
1005 sourceMap
.put(osb
.getCitation().getTitleCache(),osb
);
1006 }catch(NullPointerException e
){logger
.warn("null pointer problem (no ref?) with "+osb
);}
1010 if (state
.getConfig().isInteractWithUser()){
1011 List
<OriginalSourceBase
<?
>> res
= null;
1012 if(!descriptionSourcesSet
){
1013 res
= sui
.askForSource(sourceMap
, "the description group ("+taxon
+")",
1014 "The current reference is "+ref
.getTitleCache(),getReferenceService(), dataHolder
.docSources
);
1015 descriptionRefs
=res
;
1016 descriptionSourcesSet
=true;
1019 res
=descriptionRefs
;
1022 for (OriginalSourceBase
<?
> sour
:res
){
1023 if(sour
.isInstanceOf(IdentifiableSource
.class)){
1025 if(sourceNotLinkedToElement(taxonDescription
,sour
)) {
1026 taxonDescription
.addSource((IdentifiableSource
)sour
.clone());
1028 } catch (CloneNotSupportedException e
) {
1029 logger
.warn("no cloning?");
1032 if(sourceNotLinkedToElement(taxonDescription
,sour
)) {
1033 taxonDescription
.addSource(OriginalSourceType
.Import
,null, null, sour
.getCitation(),sour
.getCitationMicroReference());
1040 if(sourceNotLinkedToElement(taxonDescription
,ref
,null)) {
1041 taxonDescription
.addSource(OriginalSourceType
.Import
,null, null, ref
, null);
1044 descriptionGroup
=taxonDescription
;
1046 IndividualsAssociation indAssociation
= IndividualsAssociation
.NewInstance();
1047 Feature feature
= makeFeature(derivedUnitBase
);
1048 indAssociation
.setAssociatedSpecimenOrObservation(derivedUnitBase
);
1049 indAssociation
.setFeature(feature
);
1051 if (state
.getConfig().isInteractWithUser()){
1052 sourceMap
= new HashMap
<String
, OriginalSourceBase
<?
>>();
1054 issTmp
= getCommonService().list(IdentifiableSource
.class, null, null, null, null);
1055 issTmp2
= getCommonService().list(DescriptionElementSource
.class, null, null, null, null);
1057 osbSet
= new HashSet
<OriginalSourceBase
>();
1059 osbSet
.addAll(issTmp2
);
1062 osbSet
.addAll(issTmp
);
1066 for( OriginalSourceBase
<?
> osb
:osbSet
) {
1067 if(osb
.getCitationMicroReference() !=null && !osb
.getCitationMicroReference().isEmpty()) {
1069 sourceMap
.put(osb
.getCitation().getTitleCache()+ "---"+osb
.getCitationMicroReference(),osb
);
1070 }catch(NullPointerException e
){logger
.warn("null pointer problem (no ref?) with "+osb
);}
1073 sourceMap
.put(osb
.getCitation().getTitleCache(),osb
);
1074 }catch(NullPointerException e
){logger
.warn("null pointer problem (no ref?) with "+osb
);}
1078 List
<OriginalSourceBase
<?
>> sources
=null;
1079 if(!associationSourcesSet
) {
1080 sources
= sui
.askForSource(sourceMap
, "descriptive element (association) ",taxon
.toString(),
1081 getReferenceService(),dataHolder
.docSources
);
1082 associationRefs
=sources
;
1083 associationSourcesSet
=true;
1086 sources
=associationRefs
;
1088 if(sources
!=null) {
1089 for (OriginalSourceBase
<?
> source
: sources
) {
1091 if(source
.isInstanceOf(DescriptionElementSource
.class)){
1093 if(sourceNotLinkedToElement(indAssociation
,source
)) {
1094 indAssociation
.addSource((DescriptionElementSource
)source
.clone());
1096 } catch (CloneNotSupportedException e
) {
1097 logger
.warn("clone forbidden?");
1100 if(sourceNotLinkedToElement(indAssociation
,source
)) {
1101 indAssociation
.addSource(OriginalSourceType
.Import
,null, null, source
.getCitation(),source
.getCitationMicroReference());
1104 if(sourceNotLinkedToElement(derivedUnitBase
, source
)) {
1105 derivedUnitBase
.addSource((IdentifiableSource
) source
.clone());
1107 } catch (CloneNotSupportedException e
) {
1108 // TODO Auto-generated catch block
1109 e
.printStackTrace();
1117 if(sourceNotLinkedToElement(indAssociation
,ref
,null)) {
1118 indAssociation
.addSource(OriginalSourceType
.Import
,null, null, ref
, null);
1120 if(sourceNotLinkedToElement(derivedUnitBase
, ref
,null)) {
1121 derivedUnitBase
.addSource(OriginalSourceType
.Import
,null, null, ref
, null);
1123 for (Reference
<?
> citation
: determinationEvent
.getReferences()) {
1124 if(sourceNotLinkedToElement(indAssociation
,citation
,null))
1126 indAssociation
.addSource(DescriptionElementSource
.NewInstance(OriginalSourceType
.Import
, null, null, citation
, null));
1128 if(sourceNotLinkedToElement(derivedUnitBase
, ref
,null)) {
1129 derivedUnitBase
.addSource(OriginalSourceType
.Import
,null, null, ref
, null);
1134 taxonDescription
.addElement(indAssociation
);
1136 save(taxonDescription
, state
);
1143 * @param derivedUnitBase2
1148 private boolean sourceNotLinkedToElement(DerivedUnit derivedUnitBase2
, Reference
<?
> b
, String d
) {
1149 Set
<IdentifiableSource
> linkedSources
= derivedUnitBase2
.getSources();
1150 for (IdentifiableSource is
:linkedSources
){
1151 Reference a
= is
.getCitation();
1152 String c
= is
.getCitationMicroReference();
1154 boolean refMatch
=false;
1155 boolean microMatch
=false;
1158 if (a
==null && b
==null) {
1161 if (a
!=null && b
!=null) {
1162 if (a
.getTitleCache().equalsIgnoreCase(b
.getTitleCache())) {
1166 }catch(Exception e
){}
1170 if (c
==null && d
==null) {
1173 if(c
!=null && d
!=null) {
1174 if(c
.equalsIgnoreCase(d
)) {
1179 catch(Exception e
){}
1181 if (microMatch
&& refMatch
) {
1191 * @param derivedUnitBase2
1195 private boolean sourceNotLinkedToElement(DerivedUnit derivedUnitBase2
, OriginalSourceBase
<?
> source
) {
1196 Set
<IdentifiableSource
> linkedSources
= derivedUnitBase2
.getSources();
1197 for (IdentifiableSource is
:linkedSources
){
1198 Reference a
= is
.getCitation();
1199 Reference b
= source
.getCitation();
1200 String c
= is
.getCitationMicroReference();
1201 String d
= source
.getCitationMicroReference();
1203 boolean refMatch
=false;
1204 boolean microMatch
=false;
1207 if (a
==null && b
==null) {
1210 if (a
!=null && b
!=null) {
1211 if (a
.getTitleCache().equalsIgnoreCase(b
.getTitleCache())) {
1215 }catch(Exception e
){}
1219 if (c
==null && d
==null) {
1222 if(c
!=null && d
!=null) {
1223 if(c
.equalsIgnoreCase(d
)) {
1228 catch(Exception e
){}
1230 if (microMatch
&& refMatch
) {
1240 * @param indAssociation
1245 private boolean sourceNotLinkedToElement(IndividualsAssociation indAssociation
, Reference
<?
> a
, String d
) {
1246 Set
<DescriptionElementSource
> linkedSources
= indAssociation
.getSources();
1247 for (DescriptionElementSource is
:linkedSources
){
1248 Reference b
= is
.getCitation();
1249 String c
= is
.getCitationMicroReference();
1251 boolean refMatch
=false;
1252 boolean microMatch
=false;
1255 if (a
==null && b
==null) {
1258 if (a
!=null && b
!=null) {
1259 if (a
.getTitleCache().equalsIgnoreCase(b
.getTitleCache())) {
1263 }catch(Exception e
){}
1267 if (c
==null && d
==null) {
1270 if(c
!=null && d
!=null) {
1271 if(c
.equalsIgnoreCase(d
)) {
1276 catch(Exception e
){}
1278 if (microMatch
&& refMatch
) {
1286 * @param taxonDescription
1291 private boolean sourceNotLinkedToElement(TaxonDescription taxonDescription
, Reference
<?
> a
, String d
) {
1292 Set
<IdentifiableSource
> linkedSources
= taxonDescription
.getSources();
1293 for (IdentifiableSource is
:linkedSources
){
1294 Reference b
= is
.getCitation();
1295 String c
= is
.getCitationMicroReference();
1297 boolean refMatch
=false;
1298 boolean microMatch
=false;
1301 if (a
==null && b
==null) {
1304 if (a
!=null && b
!=null) {
1305 if (a
.getTitleCache().equalsIgnoreCase(b
.getTitleCache())) {
1309 }catch(Exception e
){}
1313 if (c
==null && d
==null) {
1316 if(c
!=null && d
!=null) {
1317 if(c
.equalsIgnoreCase(d
)) {
1322 catch(Exception e
){}
1324 if (microMatch
&& refMatch
) {
1332 * @param indAssociation
1336 private boolean sourceNotLinkedToElement(IndividualsAssociation indAssociation
, OriginalSourceBase
<?
> source
) {
1337 Set
<DescriptionElementSource
> linkedSources
= indAssociation
.getSources();
1338 for (DescriptionElementSource is
:linkedSources
){
1339 Reference a
= is
.getCitation();
1340 Reference b
= source
.getCitation();
1341 String c
= is
.getCitationMicroReference();
1342 String d
= source
.getCitationMicroReference();
1344 boolean refMatch
=false;
1345 boolean microMatch
=false;
1348 if (a
==null && b
==null) {
1351 if (a
!=null && b
!=null) {
1352 if (a
.getTitleCache().equalsIgnoreCase(b
.getTitleCache())) {
1356 }catch(Exception e
){}
1360 if (c
==null && d
==null) {
1363 if(c
!=null && d
!=null) {
1364 if(c
.equalsIgnoreCase(d
)) {
1369 catch(Exception e
){}
1371 if (microMatch
&& refMatch
) {
1379 * @param taxonDescription
1383 private boolean sourceNotLinkedToElement(TaxonDescription taxonDescription
, OriginalSourceBase
<?
> sour
) {
1384 Set
<IdentifiableSource
> linkedSources
= taxonDescription
.getSources();
1385 for (IdentifiableSource is
:linkedSources
){
1386 Reference a
= is
.getCitation();
1387 Reference b
= sour
.getCitation();
1388 String c
= is
.getCitationMicroReference();
1389 String d
= sour
.getCitationMicroReference();
1391 boolean refMatch
=false;
1392 boolean microMatch
=false;
1395 if (a
==null && b
==null) {
1398 if (a
!=null && b
!=null) {
1399 if (a
.getTitleCache().equalsIgnoreCase(b
.getTitleCache())) {
1403 }catch(Exception e
){}
1407 if (c
==null && d
==null) {
1410 if(c
!=null && d
!=null) {
1411 if(c
.equalsIgnoreCase(d
)) {
1416 catch(Exception e
){}
1418 if (microMatch
&& refMatch
) {
1428 * look for the Feature object (FieldObs, Specimen,...)
1429 * @param unit : a specimen or obersvation base
1430 * @return the corresponding Feature
1432 private Feature
makeFeature(SpecimenOrObservationBase
<?
> unit
) {
1433 SpecimenOrObservationType type
= unit
.getRecordBasis();
1437 if (type
.isFeatureObservation()){
1438 return Feature
.OBSERVATION();
1439 }else if (type
.isFeatureSpecimen()){
1440 return Feature
.SPECIMEN();
1441 }else if (type
== SpecimenOrObservationType
.DerivedUnit
){
1442 return Feature
.OBSERVATION();
1443 // return getFeature("Specimen or observation");
1445 String message
= "Unhandled record basis '%s' for defining individuals association feature type. Use default.";
1446 logger
.warn(String
.format(message
, type
.getMessage()));
1447 return Feature
.OBSERVATION();
1448 // return getFeature("Specimen or observation");
1453 private Feature
getFeature(String featureName
, Abcd206ImportState state
){
1454 List
<Feature
> features
= getTermService().list(Feature
.class, null,null,null,null);
1455 Feature currentFeature
=null;
1456 for (Feature feature
: features
){
1457 String tmpF
= feature
.getTitleCache();
1458 if (tmpF
.equalsIgnoreCase(featureName
)) {
1459 currentFeature
=feature
;
1462 if (currentFeature
== null) {
1463 currentFeature
=Feature
.NewInstance(featureName
, featureName
, featureName
);
1464 save(currentFeature
, state
);
1466 return currentFeature
;
1470 * getTaxon : search for an existing taxon in the database
1471 * @param state : the ABCD import state
1472 * @param scientificName : the name (string)
1473 * @param i : the current unit position in the abcd file
1474 * @param rank : the rank for the taxon
1477 @SuppressWarnings("rawtypes")
1478 private Taxon
getTaxon(Abcd206ImportState state
, String scientificName
, int i
, Rank rank
) {
1479 // System.out.println("GETTAXON "+scientificName);
1480 Abcd206ImportConfigurator config
= state
.getConfig();
1482 NonViralName
<?
> taxonName
= null;
1484 SpecimenUserInteraction sui
= state
.getConfig().getSpecimenUserInteraction();
1486 // System.out.println("config.isReuseExistingTaxaWhenPossible() :"+config.isReuseExistingTaxaWhenPossible());
1487 if (config
.isReuseExistingTaxaWhenPossible()){
1488 List
<TaxonBase
> c
= null;
1490 List
<TaxonBase
> taxonbaseList
= getTaxonService().listByTitle(Taxon
.class, scientificName
+" sec", MatchMode
.BEGINNING
, null, null, null, null, null);
1491 if (taxonbaseList
.size()>0){
1492 if(config
.isInteractWithUser() && config
.isAllowReuseOtherClassifications()){
1493 taxon
= sui
.askWhereToFixData(scientificName
,taxonbaseList
, classification
);
1495 taxon
= sui
.lookForTaxaIntoCurrentClassification(taxonbaseList
, classification
);
1499 c
= getTaxonService().searchTaxaByName(scientificName
, ref
);
1500 if(config
.isInteractWithUser() && config
.isAllowReuseOtherClassifications()){
1501 taxon
= sui
.askWhereToFixData(scientificName
,c
, classification
);
1504 taxon
= sui
.lookForTaxaIntoCurrentClassification(c
, classification
);
1507 } catch (Exception e
) {
1508 logger
.info("Searchtaxabyname failed" + e
);
1512 if (!config
.isReuseExistingTaxaWhenPossible() || taxon
== null){
1513 // System.out.println("create new taxonName instance "+i+", "+config.isParseNameAutomatically());
1514 if (config
.isParseNameAutomatically()){
1515 taxonName
= parseScientificName(scientificName
);
1518 if (i
>=0 && (dataHolder
.atomisedIdentificationList
!= null || dataHolder
.atomisedIdentificationList
.size() > 0)) {
1519 taxonName
= setTaxonNameByType(dataHolder
.atomisedIdentificationList
.get(i
), scientificName
);
1524 // if (taxonName != null) {
1525 // System.out.println(taxonName.getTitleCache());
1527 // System.out.println("taxonname: "+taxonName);
1529 if(taxonName
== null){
1530 taxonName
= NonViralName
.NewInstance(rank
);
1531 taxonName
.setFullTitleCache(scientificName
,true);
1532 taxonName
.setTitleCache(scientificName
, true);
1534 // System.out.println("ADD NEW TAXON *"+taxonName.getRank()+"*"+taxonName.getTitleCache());
1535 if (rank
!= null && (taxonName
.getRank() ==null || taxonName
.getRank().toString().trim().isEmpty())) {
1536 taxonName
.setRank(rank
);
1538 save(taxonName
, state
);
1539 taxon
= Taxon
.NewInstance(taxonName
, ref
); //sec set null
1546 * HandleIdentifications : get the scientific names present in the ABCD
1547 * document and store link them with the observation/specimen data
1548 * @param state: the current ABCD import state
1549 * @param derivedUnitFacade : the current derivedunitfacade
1551 private void handleIdentifications(Abcd206ImportState state
, DerivedUnitFacade derivedUnitFacade
) {
1552 Abcd206ImportConfigurator config
= state
.getConfig();
1556 String scientificName
= "";
1557 boolean preferredFlag
= false;
1559 if (dataHolder
.nomenclatureCode
== ""){
1560 dataHolder
.nomenclatureCode
= config
.getNomenclaturalCode().toString();
1563 for (int i
= 0; i
< dataHolder
.identificationList
.size(); i
++) {
1564 Identification identification
= dataHolder
.identificationList
.get(i
);
1565 scientificName
= identification
.getScientificName().replaceAll(" et ", " & ");
1567 String preferred
= identification
.getPreferred();
1568 if (preferred
.equals("1") || preferred
.toLowerCase().indexOf("true") != -1 || dataHolder
.identificationList
.size()==1) {
1569 preferredFlag
= true;
1572 preferredFlag
= false;
1575 if (identification
.getCode().indexOf(':') != -1) {
1576 dataHolder
.nomenclatureCode
= identification
.getCode().split(COLON
)[1];
1579 dataHolder
.nomenclatureCode
= identification
.getCode();
1581 taxon
= getTaxon(state
, scientificName
, i
,null);
1582 addTaxonNode(taxon
, state
,preferredFlag
);
1583 linkDeterminationEvent(state
, taxon
, preferredFlag
, derivedUnitFacade
);
1588 * @param taxon : a taxon to add as a node
1589 * @param state : the ABCD import state
1591 private void addTaxonNode(Taxon taxon
, Abcd206ImportState state
, boolean preferredFlag
) {
1592 logger
.info("link taxon to a taxonNode "+taxon
.getTitleCache());
1593 boolean exist
= false;
1594 for (TaxonNode p
: classification
.getAllNodes()){
1596 if(p
.getTaxon().equals(taxon
)) {
1601 logger
.warn("TaxonNode doesn't seem to have a taxon");
1605 addParentTaxon(taxon
, state
, preferredFlag
);
1609 private boolean hasTaxonNodeInClassification(Taxon taxon
){
1610 for (TaxonNode node
: taxon
.getTaxonNodes()){
1611 if(node
.getClassification().equals(classification
)){
1619 * Add the hierarchy for a Taxon(add higher taxa)
1620 * @param taxon: a taxon to add as a node
1621 * @param state: the ABCD import state
1623 private void addParentTaxon(Taxon taxon
, Abcd206ImportState state
, boolean preferredFlag
){
1624 NonViralName
<?
> nvname
= CdmBase
.deproxy(taxon
.getName(), NonViralName
.class);
1625 Rank rank
= nvname
.getRank();
1627 Taxon subgenus
=null;
1628 Taxon species
= null;
1629 Taxon subspecies
= null;
1630 Taxon parent
= null;
1631 if (rank
.isLower(Rank
.GENUS() )){
1632 String prefix
= nvname
.getGenusOrUninomial();
1633 genus
= getTaxon(state
, prefix
, -1, Rank
.GENUS());
1634 if (preferredFlag
) {
1635 parent
= saveOrUpdateClassification(null, genus
, state
);
1639 if (rank
.isLower(Rank
.SUBGENUS())){
1640 String prefix
= nvname
.getGenusOrUninomial();
1641 String name
= nvname
.getInfraGenericEpithet();
1643 subgenus
= getTaxon(state
, prefix
+" "+name
, -1, Rank
.SUBGENUS());
1644 if (preferredFlag
) {
1645 parent
= saveOrUpdateClassification(genus
, subgenus
, state
);
1648 if (rank
.isLower(Rank
.SPECIES())){
1649 if (subgenus
!=null){
1650 String prefix
= nvname
.getGenusOrUninomial();
1651 String name
= nvname
.getInfraGenericEpithet();
1652 String spe
= nvname
.getSpecificEpithet();
1654 species
= getTaxon(state
, prefix
+" "+name
+" "+spe
, -1, Rank
.SPECIES());
1655 if (preferredFlag
) {
1656 parent
= saveOrUpdateClassification(subgenus
, species
, state
);
1661 String prefix
= nvname
.getGenusOrUninomial();
1662 String name
= nvname
.getSpecificEpithet();
1664 species
= getTaxon(state
, prefix
+" "+name
, -1, Rank
.SPECIES());
1665 if (preferredFlag
) {
1666 parent
= saveOrUpdateClassification(genus
, species
, state
);
1671 if (rank
.isInfraSpecific()){
1672 subspecies
= getTaxon(state
, nvname
.getFullTitleCache(), -1, Rank
.SUBSPECIES());
1673 if (preferredFlag
) {
1674 parent
= saveOrUpdateClassification(species
, subspecies
, state
);
1677 if (preferredFlag
) {
1678 saveOrUpdateClassification(parent
, taxon
, state
);
1683 * Link a parent to a child and save it in the current classification
1684 * @param parent: the higher Taxon
1685 * @param child : the lower (or current) Taxon
1686 * return the Taxon from the new created Node
1689 private Taxon
saveOrUpdateClassification(Taxon parent
, Taxon child
, Abcd206ImportState state
) {
1690 TaxonNode node
=null;
1691 if (parent
!= null) {
1692 parent
= (Taxon
) getTaxonService().find(parent
.getUuid());
1693 child
= (Taxon
) getTaxonService().find(child
.getUuid());
1694 //here we do not have to check if the taxon nodes already exists
1695 //this is done by classification.addParentChild()
1696 node
= classification
.addParentChild(parent
, child
, ref
, "");
1699 child
= (Taxon
) getTaxonService().find(child
.getUuid());
1700 //do not add child node if it already exists
1701 if(hasTaxonNodeInClassification(child
)){
1704 node
=classification
.addChildTaxon(child
, ref
, null);
1706 save(classification
, state
);
1707 return node
.getTaxon();
1711 * Parse automatically the scientific name
1712 * @param scientificName: the scientific name to parse
1713 * @return a parsed name
1715 private NonViralName
<?
> parseScientificName(String scientificName
) {
1716 NonViralNameParserImpl nvnpi
= NonViralNameParserImpl
.NewInstance();
1717 NonViralName
<?
> taxonName
= null;
1718 boolean problem
= false;
1721 logger
.info("parseScientificName " + dataHolder
.nomenclatureCode
.toString());
1724 if (dataHolder
.nomenclatureCode
.toString().equals("Zoological") || dataHolder
.nomenclatureCode
.toString().contains("ICZN")) {
1725 taxonName
= nvnpi
.parseFullName(scientificName
, NomenclaturalCode
.ICZN
, null);
1726 if (taxonName
.hasProblem()) {
1730 if (dataHolder
.nomenclatureCode
.toString().equals("Botanical") || dataHolder
.nomenclatureCode
.toString().contains("ICBN")) {
1731 taxonName
= nvnpi
.parseFullName(scientificName
, NomenclaturalCode
.ICNAFP
, null);
1732 if (taxonName
.hasProblem()) {
1736 if (dataHolder
.nomenclatureCode
.toString().equals("Bacterial") || dataHolder
.nomenclatureCode
.toString().contains("ICBN")) {
1737 taxonName
= nvnpi
.parseFullName(scientificName
, NomenclaturalCode
.ICNB
, null);
1738 if (taxonName
.hasProblem()) {
1742 if (dataHolder
.nomenclatureCode
.toString().equals("Cultivar") || dataHolder
.nomenclatureCode
.toString().contains("ICNCP")) {
1743 taxonName
= nvnpi
.parseFullName(scientificName
, NomenclaturalCode
.ICNCP
, null);
1744 if (taxonName
.hasProblem()) {
1749 logger
.info("Parsing with problem in parseScientificName " + scientificName
);
1757 * Create the name without automatic parsing, either because it failed, or because the user deactivated it.
1758 * The name is built upon the ABCD fields
1759 * @param atomisedMap : the ABCD atomised fields
1760 * @param fullName : the full scientific name
1761 * @return the corresponding Botanical or Zoological or... name
1763 private NonViralName
<?
> setTaxonNameByType(
1764 HashMap
<String
, String
> atomisedMap
, String fullName
) {
1765 boolean problem
= false;
1767 logger
.info("settaxonnamebytype " + dataHolder
.nomenclatureCode
.toString());
1770 if (dataHolder
.nomenclatureCode
.equals("Zoological")) {
1771 NonViralName
<ZoologicalName
> taxonName
= ZoologicalName
.NewInstance(null);
1772 taxonName
.setFullTitleCache(fullName
, true);
1773 taxonName
.setGenusOrUninomial(NB(getFromMap(atomisedMap
, "Genus")));
1774 taxonName
.setInfraGenericEpithet(NB(getFromMap(atomisedMap
, "SubGenus")));
1775 taxonName
.setSpecificEpithet(NB(getFromMap(atomisedMap
,"SpeciesEpithet")));
1776 taxonName
.setInfraSpecificEpithet(NB(getFromMap(atomisedMap
,"SubspeciesEpithet")));
1778 if (taxonName
.getGenusOrUninomial() != null){
1779 taxonName
.setRank(Rank
.GENUS());
1782 if (taxonName
.getInfraGenericEpithet() != null){
1783 taxonName
.setRank(Rank
.SUBGENUS());
1786 if (taxonName
.getSpecificEpithet() != null){
1787 taxonName
.setRank(Rank
.SPECIES());
1790 if (taxonName
.getInfraSpecificEpithet() != null){
1791 taxonName
.setRank(Rank
.SUBSPECIES());
1795 if (getFromMap(atomisedMap
, "AuthorTeamParenthesis") != null) {
1796 team
= Team
.NewInstance();
1797 team
.setTitleCache(getFromMap(atomisedMap
, "AuthorTeamParenthesis"), true);
1800 if (getFromMap(atomisedMap
, "AuthorTeamAndYear") != null) {
1801 team
= Team
.NewInstance();
1802 team
.setTitleCache(getFromMap(atomisedMap
, "AuthorTeamAndYear"), true);
1806 taxonName
.setBasionymAuthorTeam(team
);
1809 if (getFromMap(atomisedMap
, "AuthorTeamParenthesis") != null) {
1810 taxonName
.setAuthorshipCache(getFromMap(atomisedMap
, "AuthorTeamParenthesis"));
1812 else if (getFromMap(atomisedMap
, "AuthorTeamAndYear") != null) {
1813 taxonName
.setAuthorshipCache(getFromMap(atomisedMap
, "AuthorTeamAndYear"));
1816 if (getFromMap(atomisedMap
, "CombinationAuthorTeamAndYear") != null) {
1817 team
= Team
.NewInstance();
1818 team
.setTitleCache(getFromMap(atomisedMap
, "CombinationAuthorTeamAndYear"), true);
1819 taxonName
.setCombinationAuthorTeam(team
);
1821 if (taxonName
.hasProblem()) {
1822 logger
.info("pb ICZN");
1829 else if (dataHolder
.nomenclatureCode
.equals("Botanical")) {
1830 BotanicalName taxonName
= (BotanicalName
) parseScientificName(fullName
);
1831 if (taxonName
!= null){
1835 taxonName
= BotanicalName
.NewInstance(null);
1837 taxonName
.setFullTitleCache(fullName
, true);
1838 taxonName
.setGenusOrUninomial(NB(getFromMap(atomisedMap
, "Genus")));
1839 taxonName
.setInfraGenericEpithet(NB(getFromMap(atomisedMap
, "FirstEpithet")));
1840 taxonName
.setInfraSpecificEpithet(NB(getFromMap(atomisedMap
, "InfraSpeEpithet")));
1842 taxonName
.setRank(Rank
.getRankByName(getFromMap(atomisedMap
, "Rank")));
1843 } catch (Exception e
) {
1844 if (taxonName
.getGenusOrUninomial() != null){
1845 taxonName
.setRank(Rank
.GENUS());
1847 else if (taxonName
.getInfraGenericEpithet() != null){
1848 taxonName
.setRank(Rank
.SUBGENUS());
1850 else if (taxonName
.getSpecificEpithet() != null){
1851 taxonName
.setRank(Rank
.SPECIES());
1853 else if (taxonName
.getInfraSpecificEpithet() != null){
1854 taxonName
.setRank(Rank
.SUBSPECIES());
1858 if (getFromMap(atomisedMap
, "AuthorTeamParenthesis") != null) {
1859 team
= Team
.NewInstance();
1860 team
.setTitleCache(getFromMap(atomisedMap
, "AuthorTeamParenthesis"), true);
1861 taxonName
.setBasionymAuthorTeam(team
);
1863 if (getFromMap(atomisedMap
, "AuthorTeam") != null) {
1864 team
= Team
.NewInstance();
1865 team
.setTitleCache(getFromMap(atomisedMap
, "AuthorTeam"), true);
1866 taxonName
.setCombinationAuthorTeam(team
);
1869 if (getFromMap(atomisedMap
, "AuthorTeamParenthesis") != null) {
1870 taxonName
.setAuthorshipCache(getFromMap(atomisedMap
, "AuthorTeamParenthesis"));
1872 else if (getFromMap(atomisedMap
, "AuthorTeam") != null) {
1873 taxonName
.setAuthorshipCache(getFromMap(atomisedMap
, "AuthorTeam"));
1876 if (getFromMap(atomisedMap
, "CombinationAuthorTeamAndYear") != null) {
1877 team
= Team
.NewInstance();
1878 team
.setTitleCache(getFromMap(atomisedMap
, "CombinationAuthorTeamAndYear"), true);
1879 taxonName
.setCombinationAuthorTeam(team
);
1881 if (taxonName
.hasProblem()) {
1882 logger
.info("pb ICBN");
1889 else if (dataHolder
.nomenclatureCode
.equals("Bacterial")) {
1890 NonViralName
<BacterialName
> taxonName
= BacterialName
.NewInstance(null);
1891 taxonName
.setFullTitleCache(fullName
, true);
1892 taxonName
.setGenusOrUninomial(getFromMap(atomisedMap
, "Genus"));
1893 taxonName
.setInfraGenericEpithet(NB(getFromMap(atomisedMap
, "SubGenus")));
1894 taxonName
.setSpecificEpithet(NB(getFromMap(atomisedMap
, "Species")));
1895 taxonName
.setInfraSpecificEpithet(NB(getFromMap(atomisedMap
, "SubspeciesEpithet")));
1897 if (taxonName
.getGenusOrUninomial() != null){
1898 taxonName
.setRank(Rank
.GENUS());
1900 else if (taxonName
.getInfraGenericEpithet() != null){
1901 taxonName
.setRank(Rank
.SUBGENUS());
1903 else if (taxonName
.getSpecificEpithet() != null){
1904 taxonName
.setRank(Rank
.SPECIES());
1906 else if (taxonName
.getInfraSpecificEpithet() != null){
1907 taxonName
.setRank(Rank
.SUBSPECIES());
1910 if (getFromMap(atomisedMap
, "AuthorTeamAndYear") != null) {
1911 Team team
= Team
.NewInstance();
1912 team
.setTitleCache(getFromMap(atomisedMap
, "AuthorTeamAndYear"), true);
1913 taxonName
.setCombinationAuthorTeam(team
);
1915 if (getFromMap(atomisedMap
, "ParentheticalAuthorTeamAndYear") != null) {
1916 Team team
= Team
.NewInstance();
1917 team
.setTitleCache(getFromMap(atomisedMap
, "ParentheticalAuthorTeamAndYear"), true);
1918 taxonName
.setBasionymAuthorTeam(team
);
1920 if (taxonName
.hasProblem()) {
1921 logger
.info("pb ICNB");
1928 else if (dataHolder
.nomenclatureCode
.equals("Cultivar")) {
1929 CultivarPlantName taxonName
= CultivarPlantName
.NewInstance(null);
1931 if (taxonName
.hasProblem()) {
1932 logger
.info("pb ICNCP");
1942 logger
.info("Problem im setTaxonNameByType ");
1943 NonViralName
<?
> taxonName
= NonViralName
.NewInstance(null);
1944 taxonName
.setFullTitleCache(fullName
, true);
1947 NonViralName
<?
> tn
= NonViralName
.NewInstance(null);
1953 * Get a formated string from a hashmap
1954 * @param atomisedMap
1958 private String
getFromMap(HashMap
<String
, String
> atomisedMap
, String key
) {
1959 String value
= null;
1960 if (atomisedMap
.containsKey(key
)) {
1961 value
= atomisedMap
.get(key
);
1965 if (value
!= null && key
.matches(".*Year.*")) {
1966 value
= value
.trim();
1967 if (value
.matches("[a-z A-Z ]*[0-9]{4}$")) {
1968 String tmp
= value
.split("[0-9]{4}$")[0];
1969 int year
= Integer
.parseInt(value
.split(tmp
)[1]);
1982 catch (Exception e
) {
1988 // private void compareABCDtoCDM(URI urlFileName, List<String> knownElts, Abcd206XMLFieldGetter abcdFieldGetter) {
1990 // DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
1991 // DocumentBuilder constructeur = factory.newDocumentBuilder();
1992 // URL url = urlFileName.toURL();
1993 // Object o = url.getContent();
1994 // InputStream is = (InputStream) o;
1995 // Document document = constructeur.parse(is);
1996 // Element root = document.getDocumentElement();
1997 // abcdFieldGetter.traverse(root);
1999 // catch (ParserConfigurationException e){
2000 // e.printStackTrace();
2002 // catch (SAXException e) {
2003 // e.printStackTrace();
2005 // catch (IOException e) {
2006 // e.printStackTrace();
2008 // Set<String> elts = dataHolder.allABCDelements.keySet();
2009 // Iterator<String> it = elts.iterator();
2011 // while (it.hasNext()) {
2013 // if (knownElts.indexOf(elt) == -1) {
2015 // logger.info("Unmerged ABCD element: " + elt + " - "+ dataHolder.allABCDelements.get(elt));
2022 * Load the list of names from the ABCD file and save them
2023 * @param state : the current ABCD import state
2024 * @param unitsList : the unit list from the ABCD file
2025 * @param abcdFieldGetter : the ABCD parser
2027 private void prepareCollectors(Abcd206ImportState state
, NodeList unitsList
, Abcd206XMLFieldGetter abcdFieldGetter
) {
2028 List
<String
> collectors
= new ArrayList
<String
>();
2029 List
<String
> teams
= new ArrayList
<String
>();
2030 List
<List
<String
>> collectorinteams
= new ArrayList
<List
<String
>>();
2032 for (int i
= 0; i
< unitsList
.getLength(); i
++) {
2033 this.getCollectorsFromXML((Element
) unitsList
.item(i
), abcdFieldGetter
);
2034 for (String agent
: dataHolder
.gatheringAgentList
) {
2035 collectors
.add(agent
);
2037 List
<String
> tmpTeam
= new ArrayList
<String
>(new HashSet
<String
>(dataHolder
.gatheringTeamList
));
2038 if(!tmpTeam
.isEmpty()) {
2039 teams
.add(StringUtils
.join(tmpTeam
.toArray()," & "));
2041 for (String agent
:tmpTeam
) {
2042 collectors
.add(agent
);
2046 List
<String
> collectorsU
= new ArrayList
<String
>(new HashSet
<String
>(collectors
));
2047 List
<String
> teamsU
= new ArrayList
<String
>(new HashSet
<String
>(teams
));
2050 //existing teams in DB
2051 Map
<String
,Team
> titleCacheTeam
= new HashMap
<String
, Team
>();
2052 List
<UuidAndTitleCache
<Team
>> hiberTeam
= getAgentService().getTeamUuidAndTitleCache();
2054 Set
<UUID
> uuids
= new HashSet
<UUID
>();
2055 for (UuidAndTitleCache
<Team
> hibernateT
:hiberTeam
){
2056 uuids
.add(hibernateT
.getUuid());
2058 if (!uuids
.isEmpty()){
2059 List
<AgentBase
> existingTeams
= getAgentService().find(uuids
);
2060 for (AgentBase
<?
> existingP
:existingTeams
){
2061 titleCacheTeam
.put(existingP
.getTitleCache(),CdmBase
.deproxy(existingP
,Team
.class));
2066 Map
<String
,UUID
> teamMap
= new HashMap
<String
, UUID
>();
2067 for (UuidAndTitleCache
<Team
> uuidt
:hiberTeam
){
2068 teamMap
.put(uuidt
.getTitleCache(), uuidt
.getUuid());
2071 //existing persons in DB
2072 List
<UuidAndTitleCache
<Person
>> hiberPersons
= getAgentService().getPersonUuidAndTitleCache();
2073 Map
<String
,Person
> titleCachePerson
= new HashMap
<String
, Person
>();
2074 uuids
= new HashSet
<UUID
>();
2075 for (UuidAndTitleCache
<Person
> hibernateP
:hiberPersons
){
2076 uuids
.add(hibernateP
.getUuid());
2079 if (!uuids
.isEmpty()){
2080 List
<AgentBase
> existingPersons
= getAgentService().find(uuids
);
2081 for (AgentBase
<?
> existingP
:existingPersons
){
2082 titleCachePerson
.put(existingP
.getTitleCache(),CdmBase
.deproxy(existingP
,Person
.class));
2086 Map
<String
,UUID
> personMap
= new HashMap
<String
, UUID
>();
2087 for (UuidAndTitleCache
<Person
> person
:hiberPersons
){
2088 personMap
.put(person
.getTitleCache(), person
.getUuid());
2091 java
.util
.Collection
<Person
> personToadd
= new ArrayList
<Person
>();
2092 java
.util
.Collection
<Team
> teamToAdd
= new ArrayList
<Team
>();
2094 for (String collector
:collectorsU
){
2095 Person p
= Person
.NewInstance();
2096 p
.setTitleCache(collector
,true);
2097 if (!personMap
.containsKey(p
.getTitleCache())){
2101 for (String team
:teamsU
){
2102 Team p
= Team
.NewInstance();
2103 p
.setTitleCache(team
,true);
2104 if (!teamMap
.containsKey(p
.getTitleCache())){
2109 if(!personToadd
.isEmpty()){
2110 for (Person agent
: personToadd
){
2112 titleCachePerson
.put(agent
.getTitleCache(),CdmBase
.deproxy(agent
, Person
.class) );
2117 Map
<String
,Integer
>teamdone
= new HashMap
<String
, Integer
>();
2118 for (List
<String
> collteam
: collectorinteams
){
2119 if (!teamdone
.containsKey(StringUtils
.join(collteam
.toArray(),"-"))){
2120 Team team
= new Team();
2122 for (String collector
:collteam
){
2123 ptmp
= Person
.NewInstance();
2124 ptmp
.setTitleCache(collector
,true);
2125 Person p2
= titleCachePerson
.get(ptmp
.getTitleCache());
2126 team
.addTeamMember(p2
);
2130 teamToAdd
.add(team
);
2132 teamdone
.put(StringUtils
.join(collteam
.toArray(),"-"),0);
2136 if(!teamToAdd
.isEmpty()){
2137 for (Team agent
: teamToAdd
){
2139 titleCacheTeam
.put(agent
.getTitleCache(), CdmBase
.deproxy( agent
,Team
.class) );
2143 state
.getConfig().setTeams(titleCacheTeam
);
2144 state
.getConfig().setPersons(titleCachePerson
);
2148 protected boolean isIgnore(Abcd206ImportState state
) {