3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
11 package eu
.etaxonomy
.cdm
.api
.service
;
13 import java
.io
.IOException
;
15 import java
.net
.URISyntaxException
;
16 import java
.util
.ArrayList
;
17 import java
.util
.Arrays
;
18 import java
.util
.Collection
;
19 import java
.util
.Collections
;
20 import java
.util
.HashMap
;
21 import java
.util
.HashSet
;
22 import java
.util
.List
;
24 import java
.util
.Map
.Entry
;
26 import java
.util
.UUID
;
28 import org
.apache
.log4j
.Logger
;
29 import org
.apache
.lucene
.index
.CorruptIndexException
;
30 import org
.apache
.lucene
.queryparser
.classic
.ParseException
;
31 import org
.apache
.lucene
.search
.BooleanClause
.Occur
;
32 import org
.apache
.lucene
.search
.BooleanQuery
.Builder
;
33 import org
.apache
.lucene
.search
.SortField
;
34 import org
.apache
.lucene
.search
.grouping
.TopGroups
;
35 import org
.apache
.lucene
.util
.BytesRef
;
36 import org
.hibernate
.TransientObjectException
;
37 import org
.hibernate
.search
.spatial
.impl
.Rectangle
;
38 import org
.joda
.time
.Partial
;
39 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
40 import org
.springframework
.dao
.DataRetrievalFailureException
;
41 import org
.springframework
.stereotype
.Service
;
42 import org
.springframework
.transaction
.annotation
.Transactional
;
44 import eu
.etaxonomy
.cdm
.api
.facade
.DerivedUnitFacade
;
45 import eu
.etaxonomy
.cdm
.api
.facade
.DerivedUnitFacadeConfigurator
;
46 import eu
.etaxonomy
.cdm
.api
.facade
.DerivedUnitFacadeNotSupportedException
;
47 import eu
.etaxonomy
.cdm
.api
.service
.UpdateResult
.Status
;
48 import eu
.etaxonomy
.cdm
.api
.service
.config
.DeleteConfiguratorBase
;
49 import eu
.etaxonomy
.cdm
.api
.service
.config
.FindOccurrencesConfigurator
;
50 import eu
.etaxonomy
.cdm
.api
.service
.config
.FindOccurrencesConfigurator
.AssignmentStatus
;
51 import eu
.etaxonomy
.cdm
.api
.service
.config
.IIdentifiableEntityServiceConfigurator
;
52 import eu
.etaxonomy
.cdm
.api
.service
.config
.SpecimenDeleteConfigurator
;
53 import eu
.etaxonomy
.cdm
.api
.service
.dto
.DerivateDTO
;
54 import eu
.etaxonomy
.cdm
.api
.service
.dto
.DerivateDataDTO
;
55 import eu
.etaxonomy
.cdm
.api
.service
.dto
.DerivateDataDTO
.ContigFile
;
56 import eu
.etaxonomy
.cdm
.api
.service
.dto
.DerivateDataDTO
.MolecularData
;
57 import eu
.etaxonomy
.cdm
.api
.service
.dto
.FieldUnitDTO
;
58 import eu
.etaxonomy
.cdm
.api
.service
.dto
.PreservedSpecimenDTO
;
59 import eu
.etaxonomy
.cdm
.api
.service
.exception
.ReferencedObjectUndeletableException
;
60 import eu
.etaxonomy
.cdm
.api
.service
.molecular
.ISequenceService
;
61 import eu
.etaxonomy
.cdm
.api
.service
.pager
.Pager
;
62 import eu
.etaxonomy
.cdm
.api
.service
.pager
.impl
.DefaultPagerImpl
;
63 import eu
.etaxonomy
.cdm
.api
.service
.search
.ILuceneIndexToolProvider
;
64 import eu
.etaxonomy
.cdm
.api
.service
.search
.ISearchResultBuilder
;
65 import eu
.etaxonomy
.cdm
.api
.service
.search
.LuceneSearch
;
66 import eu
.etaxonomy
.cdm
.api
.service
.search
.QueryFactory
;
67 import eu
.etaxonomy
.cdm
.api
.service
.search
.SearchResult
;
68 import eu
.etaxonomy
.cdm
.api
.service
.search
.SearchResultBuilder
;
69 import eu
.etaxonomy
.cdm
.api
.service
.util
.TaxonRelationshipEdge
;
70 import eu
.etaxonomy
.cdm
.common
.monitor
.IProgressMonitor
;
71 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
72 import eu
.etaxonomy
.cdm
.model
.CdmBaseType
;
73 import eu
.etaxonomy
.cdm
.model
.agent
.AgentBase
;
74 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
75 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTerm
;
76 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTermBase
;
77 import eu
.etaxonomy
.cdm
.model
.common
.ICdmBase
;
78 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
79 import eu
.etaxonomy
.cdm
.model
.description
.CategoricalData
;
80 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
81 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
82 import eu
.etaxonomy
.cdm
.model
.description
.IndividualsAssociation
;
83 import eu
.etaxonomy
.cdm
.model
.description
.QuantitativeData
;
84 import eu
.etaxonomy
.cdm
.model
.description
.SpecimenDescription
;
85 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
86 import eu
.etaxonomy
.cdm
.model
.location
.Country
;
87 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
88 import eu
.etaxonomy
.cdm
.model
.media
.Media
;
89 import eu
.etaxonomy
.cdm
.model
.media
.MediaRepresentation
;
90 import eu
.etaxonomy
.cdm
.model
.media
.MediaRepresentationPart
;
91 import eu
.etaxonomy
.cdm
.model
.media
.MediaUtils
;
92 import eu
.etaxonomy
.cdm
.model
.molecular
.AmplificationResult
;
93 import eu
.etaxonomy
.cdm
.model
.molecular
.DnaSample
;
94 import eu
.etaxonomy
.cdm
.model
.molecular
.Sequence
;
95 import eu
.etaxonomy
.cdm
.model
.molecular
.SingleRead
;
96 import eu
.etaxonomy
.cdm
.model
.name
.SpecimenTypeDesignation
;
97 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
98 import eu
.etaxonomy
.cdm
.model
.name
.TypeDesignationStatusBase
;
99 import eu
.etaxonomy
.cdm
.model
.occurrence
.DerivationEvent
;
100 import eu
.etaxonomy
.cdm
.model
.occurrence
.DerivedUnit
;
101 import eu
.etaxonomy
.cdm
.model
.occurrence
.DeterminationEvent
;
102 import eu
.etaxonomy
.cdm
.model
.occurrence
.FieldUnit
;
103 import eu
.etaxonomy
.cdm
.model
.occurrence
.GatheringEvent
;
104 import eu
.etaxonomy
.cdm
.model
.occurrence
.MediaSpecimen
;
105 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
106 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationType
;
107 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
108 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
109 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.IDefinedTermDao
;
110 import eu
.etaxonomy
.cdm
.persistence
.dao
.initializer
.AbstractBeanInitializer
;
111 import eu
.etaxonomy
.cdm
.persistence
.dao
.molecular
.ISingleReadDao
;
112 import eu
.etaxonomy
.cdm
.persistence
.dao
.occurrence
.IOccurrenceDao
;
113 import eu
.etaxonomy
.cdm
.persistence
.dto
.UuidAndTitleCache
;
114 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
;
115 import eu
.etaxonomy
.cdm
.strategy
.cache
.common
.IIdentifiableEntityCacheStrategy
;
118 * @author a.babadshanjan
119 * @created 01.09.2008
122 @Transactional(readOnly
= true)
123 public class OccurrenceServiceImpl
extends IdentifiableServiceBase
<SpecimenOrObservationBase
, IOccurrenceDao
> implements IOccurrenceService
{
125 static private final Logger logger
= Logger
.getLogger(OccurrenceServiceImpl
.class);
128 private IDefinedTermDao definedTermDao
;
131 private IDescriptionService descriptionService
;
134 private INameService nameService
;
137 private ITaxonService taxonService
;
140 private ISequenceService sequenceService
;
143 private ISingleReadDao singleReadDao
;
146 private AbstractBeanInitializer beanInitializer
;
149 private ILuceneIndexToolProvider luceneIndexToolProvider
;
151 private static final String SEPARATOR_STRING
= ", ";
153 public OccurrenceServiceImpl() {
154 logger
.debug("Load OccurrenceService Bean");
159 @Transactional(readOnly
= false)
160 public void updateTitleCache(Class
<?
extends SpecimenOrObservationBase
> clazz
, Integer stepSize
, IIdentifiableEntityCacheStrategy
<SpecimenOrObservationBase
> cacheStrategy
, IProgressMonitor monitor
) {
162 clazz
= SpecimenOrObservationBase
.class;
164 super.updateTitleCacheImpl(clazz
, stepSize
, cacheStrategy
, monitor
);
168 * FIXME Candidate for harmonization
169 * move to termService
172 public Country
getCountryByIso(String iso639
) {
173 return this.definedTermDao
.getCountryByIso(iso639
);
178 * FIXME Candidate for harmonization
179 * move to termService
182 public List
<Country
> getCountryByName(String name
) {
183 List
<?
extends DefinedTermBase
> terms
= this.definedTermDao
.findByTitle(Country
.class, name
, null, null, null, null, null, null);
184 List
<Country
> countries
= new ArrayList
<Country
>();
185 for (int i
= 0; i
< terms
.size(); i
++) {
186 countries
.add((Country
) terms
.get(i
));
193 protected void setDao(IOccurrenceDao dao
) {
198 public Pager
<DerivationEvent
> getDerivationEvents(SpecimenOrObservationBase occurence
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
199 Integer numberOfResults
= dao
.countDerivationEvents(occurence
);
201 List
<DerivationEvent
> results
= new ArrayList
<DerivationEvent
>();
202 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
203 results
= dao
.getDerivationEvents(occurence
, pageSize
, pageNumber
, propertyPaths
);
206 return new DefaultPagerImpl
<DerivationEvent
>(pageNumber
, numberOfResults
, pageSize
, results
);
210 public int countDeterminations(SpecimenOrObservationBase occurence
, TaxonBase taxonbase
) {
211 return dao
.countDeterminations(occurence
, taxonbase
);
215 public Pager
<DeterminationEvent
> getDeterminations(SpecimenOrObservationBase occurrence
, TaxonBase taxonBase
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
216 Integer numberOfResults
= dao
.countDeterminations(occurrence
, taxonBase
);
218 List
<DeterminationEvent
> results
= new ArrayList
<DeterminationEvent
>();
219 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
220 results
= dao
.getDeterminations(occurrence
, taxonBase
, pageSize
, pageNumber
, propertyPaths
);
223 return new DefaultPagerImpl
<DeterminationEvent
>(pageNumber
, numberOfResults
, pageSize
, results
);
227 public Pager
<Media
> getMedia(SpecimenOrObservationBase occurence
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
228 Integer numberOfResults
= dao
.countMedia(occurence
);
230 List
<Media
> results
= new ArrayList
<Media
>();
231 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
232 results
= dao
.getMedia(occurence
, pageSize
, pageNumber
, propertyPaths
);
235 return new DefaultPagerImpl
<Media
>(pageNumber
, numberOfResults
, pageSize
, results
);
239 public Pager
<Media
> getMediainHierarchy(SpecimenOrObservationBase rootOccurence
, Integer pageSize
,
240 Integer pageNumber
, List
<String
> propertyPaths
) {
241 List
<Media
> media
= new ArrayList
<Media
>();
243 if(rootOccurence
.isInstanceOf(MediaSpecimen
.class)){
244 MediaSpecimen mediaSpecimen
= HibernateProxyHelper
.deproxy(rootOccurence
, MediaSpecimen
.class);
245 media
.add(mediaSpecimen
.getMediaSpecimen());
247 // pherograms & gelPhotos
248 if (rootOccurence
.isInstanceOf(DnaSample
.class)) {
249 DnaSample dnaSample
= CdmBase
.deproxy(rootOccurence
, DnaSample
.class);
250 Set
<Sequence
> sequences
= dnaSample
.getSequences();
251 //we do show only those gelPhotos which lead to a consensus sequence
252 for (Sequence sequence
: sequences
) {
253 Set
<Media
> dnaRelatedMedia
= new HashSet
<Media
>();
254 for (SingleRead singleRead
: sequence
.getSingleReads()){
255 AmplificationResult amplification
= singleRead
.getAmplificationResult();
256 dnaRelatedMedia
.add(amplification
.getGelPhoto());
257 dnaRelatedMedia
.add(singleRead
.getPherogram());
258 dnaRelatedMedia
.remove(null);
260 media
.addAll(dnaRelatedMedia
);
263 if(rootOccurence
.isInstanceOf(DerivedUnit
.class)){
264 DerivedUnit derivedUnit
= HibernateProxyHelper
.deproxy(rootOccurence
, DerivedUnit
.class);
265 for (DerivationEvent derivationEvent
: derivedUnit
.getDerivationEvents()) {
266 for (DerivedUnit childDerivative
: derivationEvent
.getDerivatives()) {
267 media
.addAll(getMediainHierarchy(childDerivative
, pageSize
, pageNumber
, propertyPaths
).getRecords());
271 return new DefaultPagerImpl
<Media
>(pageNumber
, media
.size(), pageSize
, media
);
275 public Pager
<SpecimenOrObservationBase
> list(Class
<?
extends SpecimenOrObservationBase
> type
, TaxonNameBase determinedAs
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
276 Integer numberOfResults
= dao
.count(type
, determinedAs
);
277 List
<SpecimenOrObservationBase
> results
= new ArrayList
<SpecimenOrObservationBase
>();
278 pageNumber
= pageNumber
== null ?
0 : pageNumber
;
279 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
280 Integer start
= pageSize
== null ?
0 : pageSize
* pageNumber
;
281 results
= dao
.list(type
, determinedAs
, pageSize
, start
, orderHints
, propertyPaths
);
283 return new DefaultPagerImpl
<SpecimenOrObservationBase
>(pageNumber
, numberOfResults
, pageSize
, results
);
287 public Pager
<SpecimenOrObservationBase
> list(Class
<?
extends SpecimenOrObservationBase
> type
, TaxonBase determinedAs
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
288 Integer numberOfResults
= dao
.count(type
, determinedAs
);
289 List
<SpecimenOrObservationBase
> results
= new ArrayList
<SpecimenOrObservationBase
>();
290 pageNumber
= pageNumber
== null ?
0 : pageNumber
;
291 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
292 Integer start
= pageSize
== null ?
0 : pageSize
* pageNumber
;
293 results
= dao
.list(type
, determinedAs
, pageSize
, start
, orderHints
, propertyPaths
);
295 return new DefaultPagerImpl
<SpecimenOrObservationBase
>(pageNumber
, numberOfResults
, pageSize
, results
);
299 public List
<UuidAndTitleCache
<DerivedUnit
>> getDerivedUnitUuidAndTitleCache() {
300 return dao
.getDerivedUnitUuidAndTitleCache();
304 public List
<UuidAndTitleCache
<FieldUnit
>> getFieldUnitUuidAndTitleCache() {
305 return dao
.getFieldUnitUuidAndTitleCache();
309 public DerivedUnitFacade
getDerivedUnitFacade(DerivedUnit derivedUnit
, List
<String
> propertyPaths
) throws DerivedUnitFacadeNotSupportedException
{
310 derivedUnit
= (DerivedUnit
) dao
.load(derivedUnit
.getUuid(), null);
311 DerivedUnitFacadeConfigurator config
= DerivedUnitFacadeConfigurator
.NewInstance();
312 config
.setThrowExceptionForNonSpecimenPreservationMethodRequest(false);
313 DerivedUnitFacade derivedUnitFacade
= DerivedUnitFacade
.NewInstance(derivedUnit
, config
);
314 beanInitializer
.initialize(derivedUnitFacade
, propertyPaths
);
315 return derivedUnitFacade
;
319 public List
<DerivedUnitFacade
> listDerivedUnitFacades(
320 DescriptionBase description
, List
<String
> propertyPaths
) {
322 List
<DerivedUnitFacade
> derivedUnitFacadeList
= new ArrayList
<DerivedUnitFacade
>();
323 IndividualsAssociation tempIndividualsAssociation
;
324 SpecimenOrObservationBase tempSpecimenOrObservationBase
;
325 List
<IndividualsAssociation
> elements
= descriptionService
.listDescriptionElements(description
, null, IndividualsAssociation
.class, null, 0, Arrays
.asList(new String
[]{"associatedSpecimenOrObservation"}));
326 for (IndividualsAssociation element
: elements
) {
327 tempIndividualsAssociation
= HibernateProxyHelper
.deproxy(element
, IndividualsAssociation
.class);
328 if (tempIndividualsAssociation
.getAssociatedSpecimenOrObservation() != null) {
329 tempSpecimenOrObservationBase
= HibernateProxyHelper
.deproxy(tempIndividualsAssociation
.getAssociatedSpecimenOrObservation(), SpecimenOrObservationBase
.class);
330 if (tempSpecimenOrObservationBase
.isInstanceOf(DerivedUnit
.class)) {
332 derivedUnitFacadeList
.add(DerivedUnitFacade
.NewInstance(HibernateProxyHelper
.deproxy(tempSpecimenOrObservationBase
, DerivedUnit
.class)));
333 } catch (DerivedUnitFacadeNotSupportedException e
) {
334 logger
.warn(tempIndividualsAssociation
.getAssociatedSpecimenOrObservation().getTitleCache() + " : " + e
.getMessage());
340 beanInitializer
.initializeAll(derivedUnitFacadeList
, propertyPaths
);
342 return derivedUnitFacadeList
;
347 public <T
extends SpecimenOrObservationBase
> List
<T
> listByAssociatedTaxon(Class
<T
> type
, Set
<TaxonRelationshipEdge
> includeRelationships
,
348 Taxon associatedTaxon
, Integer maxDepth
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
350 return pageByAssociatedTaxon(type
, includeRelationships
, associatedTaxon
, maxDepth
, pageSize
, pageNumber
, orderHints
, propertyPaths
).getRecords();
354 public Collection
<SpecimenOrObservationBase
> listFieldUnitsByAssociatedTaxon(Taxon associatedTaxon
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
355 return pageFieldUnitsByAssociatedTaxon(null, associatedTaxon
, null, null, null, null, propertyPaths
).getRecords();
359 public Pager
<SpecimenOrObservationBase
> pageFieldUnitsByAssociatedTaxon(Set
<TaxonRelationshipEdge
> includeRelationships
,
360 Taxon associatedTaxon
, Integer maxDepth
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
,
361 List
<String
> propertyPaths
) {
363 if (!getSession().contains(associatedTaxon
)) {
364 associatedTaxon
= (Taxon
) taxonService
.load(associatedTaxon
.getUuid());
367 // gather the IDs of all relevant field units
368 Set
<Integer
> fieldUnitIds
= new HashSet
<Integer
>();
369 List
<SpecimenOrObservationBase
> records
= listByAssociatedTaxon(null, includeRelationships
, associatedTaxon
, maxDepth
, null, null, orderHints
, propertyPaths
);
370 for (SpecimenOrObservationBase
<?
> specimen
: records
) {
371 for (FieldUnit fieldUnit
: getFieldUnits(specimen
.getUuid())) {
372 fieldUnitIds
.add(fieldUnit
.getId());
375 //dao.listByIds() does the paging of the field units. Passing the field units directly to the Pager would not work
376 List
<SpecimenOrObservationBase
> fieldUnits
= dao
.listByIds(fieldUnitIds
, pageSize
, pageNumber
, orderHints
, propertyPaths
);
377 return new DefaultPagerImpl
<SpecimenOrObservationBase
>(pageNumber
, fieldUnitIds
.size(), pageSize
, fieldUnits
);
381 public FieldUnitDTO
assembleFieldUnitDTO(FieldUnit fieldUnit
, UUID associatedTaxonUuid
) {
383 if (!getSession().contains(fieldUnit
)) {
384 fieldUnit
= (FieldUnit
) load(fieldUnit
.getUuid());
386 TaxonBase associatedTaxon
= taxonService
.load(associatedTaxonUuid
);
388 FieldUnitDTO fieldUnitDTO
= new FieldUnitDTO();
390 if (fieldUnit
.getGatheringEvent() != null) {
391 GatheringEvent gatheringEvent
= fieldUnit
.getGatheringEvent();
393 NamedArea country
= gatheringEvent
.getCountry();
394 fieldUnitDTO
.setCountry(country
!= null ? country
.getDescription() : null);
396 AgentBase collector
= gatheringEvent
.getCollector();
397 String fieldNumber
= fieldUnit
.getFieldNumber();
398 String collectionString
= "";
399 if (collector
!= null || fieldNumber
!= null) {
400 collectionString
+= collector
!= null ? collector
: "";
401 if (!collectionString
.isEmpty()) {
402 collectionString
+= " ";
404 collectionString
+= (fieldNumber
!= null ? fieldNumber
: "");
405 collectionString
.trim();
407 fieldUnitDTO
.setCollection(collectionString
);
409 Partial gatheringDate
= gatheringEvent
.getGatheringDate();
410 String dateString
= null;
411 if (gatheringDate
!= null) {
412 gatheringDate
.toString();
414 else if(gatheringEvent
.getTimeperiod()!=null && gatheringEvent
.getTimeperiod().getFreeText()!=null){
415 dateString
= gatheringEvent
.getTimeperiod().getFreeText();
417 fieldUnitDTO
.setDate(dateString
);
421 fieldUnitDTO
.setTaxonName(associatedTaxon
.getName().getTitleCache());
424 Map
<eu
.etaxonomy
.cdm
.model
.occurrence
.Collection
, Integer
> collectionToCountMap
= new HashMap
<eu
.etaxonomy
.cdm
.model
.occurrence
.Collection
, Integer
>();
425 // List of accession numbers for citation
426 List
<String
> preservedSpecimenAccessionNumbers
= new ArrayList
<String
>();
428 // assemble preserved specimen DTOs
429 Set
<DerivationEvent
> derivationEvents
= fieldUnit
.getDerivationEvents();
430 for (DerivationEvent derivationEvent
: derivationEvents
) {
431 Set
<DerivedUnit
> derivatives
= derivationEvent
.getDerivatives();
432 for (DerivedUnit derivedUnit
: derivatives
) {
433 if(!derivedUnit
.isPublish()){
436 // collect accession numbers for citation
437 String mostSignificantIdentifier
= getMostSignificantIdentifier(derivedUnit
);
438 if (mostSignificantIdentifier
!= null) {
439 preservedSpecimenAccessionNumbers
.add(mostSignificantIdentifier
);
441 // collect collections for herbaria column
442 if (derivedUnit
.getCollection() != null) {
443 Integer herbariumCount
= collectionToCountMap
.get(derivedUnit
.getCollection());
444 if (herbariumCount
== null) {
447 collectionToCountMap
.put(derivedUnit
.getCollection(), herbariumCount
+ 1);
449 if (derivedUnit
.getRecordBasis().equals(SpecimenOrObservationType
.PreservedSpecimen
)) {
450 PreservedSpecimenDTO preservedSpecimenDTO
= assemblePreservedSpecimenDTO(derivedUnit
, fieldUnitDTO
);
451 fieldUnitDTO
.addPreservedSpecimenDTO(preservedSpecimenDTO
);
452 fieldUnitDTO
.setHasCharacterData(fieldUnitDTO
.isHasCharacterData() || preservedSpecimenDTO
.isHasCharacterData());
453 fieldUnitDTO
.setHasDetailImage(fieldUnitDTO
.isHasDetailImage() || preservedSpecimenDTO
.isHasDetailImage());
454 fieldUnitDTO
.setHasDna(fieldUnitDTO
.isHasDna() || preservedSpecimenDTO
.isHasDna());
455 fieldUnitDTO
.setHasSpecimenScan(fieldUnitDTO
.isHasSpecimenScan() || preservedSpecimenDTO
.isHasSpecimenScan());
459 // assemble derivate data DTO
460 assembleDerivateDataDTO(fieldUnitDTO
, fieldUnit
);
463 String citation
= fieldUnit
.getTitleCache();
464 if (!preservedSpecimenAccessionNumbers
.isEmpty()) {
466 for (String accessionNumber
: preservedSpecimenAccessionNumbers
) {
467 if (!accessionNumber
.isEmpty()) {
468 citation
+= accessionNumber
+ SEPARATOR_STRING
;
471 citation
= removeTail(citation
, SEPARATOR_STRING
);
474 fieldUnitDTO
.setCitation(citation
);
476 // assemble herbaria string
477 String herbariaString
= "";
478 for (Entry
<eu
.etaxonomy
.cdm
.model
.occurrence
.Collection
, Integer
> e
: collectionToCountMap
.entrySet()) {
479 eu
.etaxonomy
.cdm
.model
.occurrence
.Collection collection
= e
.getKey();
480 if (collection
.getCode() != null) {
481 herbariaString
+= collection
.getCode();
483 if (e
.getValue() > 1) {
484 herbariaString
+= "(" + e
.getValue() + ")";
486 herbariaString
+= SEPARATOR_STRING
;
488 herbariaString
= removeTail(herbariaString
, SEPARATOR_STRING
);
489 fieldUnitDTO
.setHerbarium(herbariaString
);
495 public PreservedSpecimenDTO
assemblePreservedSpecimenDTO(DerivedUnit derivedUnit
) {
496 return assemblePreservedSpecimenDTO(derivedUnit
, null);
500 public String
getMostSignificantIdentifier(DerivedUnit derivedUnit
) {
501 if (derivedUnit
.getAccessionNumber() != null && !derivedUnit
.getAccessionNumber().isEmpty()) {
502 return derivedUnit
.getAccessionNumber();
504 else if(derivedUnit
.getBarcode()!=null && !derivedUnit
.getBarcode().isEmpty()){
505 return derivedUnit
.getBarcode();
507 else if(derivedUnit
.getCatalogNumber()!=null && !derivedUnit
.getCatalogNumber().isEmpty()){
508 return derivedUnit
.getCatalogNumber();
513 public PreservedSpecimenDTO
assemblePreservedSpecimenDTO(DerivedUnit derivedUnit
, FieldUnitDTO fieldUnitDTO
) {
514 if (!getSession().contains(derivedUnit
)) {
515 derivedUnit
= (DerivedUnit
) load(derivedUnit
.getUuid());
517 PreservedSpecimenDTO preservedSpecimenDTO
= new PreservedSpecimenDTO();
519 // check identifiers in priority order accNo>barCode>catalogNumber
520 if (derivedUnit
.getAccessionNumber() != null && !derivedUnit
.getAccessionNumber().isEmpty()) {
521 preservedSpecimenDTO
.setAccessionNumber(derivedUnit
.getAccessionNumber());
523 else if(derivedUnit
.getBarcode()!=null && !derivedUnit
.getBarcode().isEmpty()){
524 preservedSpecimenDTO
.setAccessionNumber(derivedUnit
.getBarcode());
526 else if(derivedUnit
.getCatalogNumber()!=null && !derivedUnit
.getCatalogNumber().isEmpty()){
527 preservedSpecimenDTO
.setAccessionNumber(derivedUnit
.getCatalogNumber());
529 preservedSpecimenDTO
.setUuid(derivedUnit
.getUuid().toString());
532 Collection
<FieldUnit
> fieldUnits
= getFieldUnits(derivedUnit
);
533 if (fieldUnits
.size() == 1) {
534 preservedSpecimenDTO
.setCitation(fieldUnits
.iterator().next().getTitleCache());
537 preservedSpecimenDTO
.setCitation("No Citation available. This specimen either has no or multiple field units.");
540 // character state data
541 Collection
<DescriptionElementBase
> characterDataForSpecimen
= getCharacterDataForSpecimen(derivedUnit
);
542 if (!characterDataForSpecimen
.isEmpty()) {
543 if (fieldUnitDTO
!= null) {
544 fieldUnitDTO
.setHasCharacterData(true);
547 for (DescriptionElementBase descriptionElementBase
: characterDataForSpecimen
) {
548 String character
= descriptionElementBase
.getFeature().getLabel();
549 ArrayList
<Language
> languages
= new ArrayList
<Language
>(Collections
.singleton(Language
.DEFAULT()));
550 if (descriptionElementBase
instanceof QuantitativeData
) {
551 QuantitativeData quantitativeData
= (QuantitativeData
) descriptionElementBase
;
552 DefaultQuantitativeDescriptionBuilder builder
= new DefaultQuantitativeDescriptionBuilder();
553 String state
= builder
.build(quantitativeData
, languages
).getText(Language
.DEFAULT());
554 preservedSpecimenDTO
.addCharacterData(character
, state
);
556 else if(descriptionElementBase
instanceof CategoricalData
){
557 CategoricalData categoricalData
= (CategoricalData
) descriptionElementBase
;
558 DefaultCategoricalDescriptionBuilder builder
= new DefaultCategoricalDescriptionBuilder();
559 String state
= builder
.build(categoricalData
, languages
).getText(Language
.DEFAULT());
560 preservedSpecimenDTO
.addCharacterData(character
, state
);
563 // check type designations
564 Collection
<SpecimenTypeDesignation
> specimenTypeDesignations
= listTypeDesignations(derivedUnit
, null, null, null, null);
565 for (SpecimenTypeDesignation specimenTypeDesignation
: specimenTypeDesignations
) {
566 if (fieldUnitDTO
!= null) {
567 fieldUnitDTO
.setHasType(true);
569 TypeDesignationStatusBase
<?
> typeStatus
= specimenTypeDesignation
.getTypeStatus();
570 if (typeStatus
!= null) {
571 List
<String
> typedTaxaNames
= new ArrayList
<String
>();
572 String label
= typeStatus
.getLabel();
573 Set
<TaxonNameBase
> typifiedNames
= specimenTypeDesignation
.getTypifiedNames();
574 for (TaxonNameBase taxonNameBase
: typifiedNames
) {
575 typedTaxaNames
.add(taxonNameBase
.getFullTitleCache());
577 preservedSpecimenDTO
.addTypes(label
, typedTaxaNames
);
581 // individuals associations
582 Collection
<IndividualsAssociation
> individualsAssociations
= listIndividualsAssociations(derivedUnit
, null, null, null, null);
583 for (IndividualsAssociation individualsAssociation
: individualsAssociations
) {
584 if (individualsAssociation
.getInDescription() != null) {
585 if (individualsAssociation
.getInDescription().isInstanceOf(TaxonDescription
.class)) {
586 TaxonDescription taxonDescription
= HibernateProxyHelper
.deproxy(individualsAssociation
.getInDescription(), TaxonDescription
.class);
587 Taxon taxon
= taxonDescription
.getTaxon();
588 if (taxon
!= null && taxon
.getName() != null) {
589 preservedSpecimenDTO
.addAssociatedTaxon(taxon
.getName().getTitleCache());
594 // assemble sub derivates
595 preservedSpecimenDTO
.setDerivateDataDTO(assembleDerivateDataDTO(preservedSpecimenDTO
, derivedUnit
));
596 return preservedSpecimenDTO
;
599 private DerivateDataDTO
assembleDerivateDataDTO(DerivateDTO derivateDTO
, SpecimenOrObservationBase
<?
> specimenOrObservation
) {
600 DerivateDataDTO derivateDataDTO
= new DerivateDataDTO();
601 Collection
<DerivedUnit
> childDerivates
= getDerivedUnitsFor(specimenOrObservation
);
602 for (DerivedUnit childDerivate
: childDerivates
) {
603 // assemble molecular data
604 //pattern: DNAMarker [contig1, primer1_1, primer1_2, ...][contig2, primer2_1, ...]...
605 if (childDerivate
.isInstanceOf(DnaSample
.class)) {
606 if (childDerivate
.getRecordBasis() == SpecimenOrObservationType
.TissueSample
) {
607 // TODO implement TissueSample assembly for web service
609 if (childDerivate
.getRecordBasis() == SpecimenOrObservationType
.DnaSample
) {
611 DnaSample dna
= HibernateProxyHelper
.deproxy(childDerivate
, DnaSample
.class);
612 if (!dna
.getSequences().isEmpty()) {
613 derivateDTO
.setHasDna(true);
615 for (Sequence sequence
: dna
.getSequences()) {
618 boldUri
= sequence
.getBoldUri();
619 } catch (URISyntaxException e1
) {
620 logger
.error("Could not create BOLD URI", e1
);
622 final DefinedTerm dnaMarker
= sequence
.getDnaMarker();
623 MolecularData molecularData
= derivateDataDTO
.addProviderLink(boldUri
!= null ? boldUri
: null, dnaMarker
!= null ? dnaMarker
.getLabel() : "[no marker]");
626 ContigFile contigFile
= null;
627 if (sequence
.getContigFile() != null) {
628 MediaRepresentationPart contigMediaRepresentationPart
= MediaUtils
.getFirstMediaRepresentationPart(sequence
.getContigFile());
629 if (contigMediaRepresentationPart
!= null) {
630 contigFile
= molecularData
.addContigFile(contigMediaRepresentationPart
.getUri(), "contig");
633 if(contigFile
==null){
634 contigFile
= molecularData
.addContigFile(null, "[no contig]");
637 if (sequence
.getSingleReads() != null) {
639 for (SingleRead singleRead
: sequence
.getSingleReads()) {
640 MediaRepresentationPart pherogramMediaRepresentationPart
= MediaUtils
.getFirstMediaRepresentationPart(singleRead
.getPherogram());
641 if (pherogramMediaRepresentationPart
!= null) {
642 contigFile
.addPrimerLink(pherogramMediaRepresentationPart
.getUri(), "read"+readCount
++);
649 // assemble media data
650 else if (childDerivate
.isInstanceOf(MediaSpecimen
.class)) {
651 MediaSpecimen media
= HibernateProxyHelper
.deproxy(childDerivate
, MediaSpecimen
.class);
653 String mediaUriString
= getMediaUriString(media
);
654 if (media
.getKindOfUnit() != null) {
656 if (media
.getKindOfUnit().getUuid().equals(UUID
.fromString("acda15be-c0e2-4ea8-8783-b9b0c4ad7f03"))) {
657 derivateDataDTO
.addSpecimenScanUuid(media
.getMediaSpecimen().getUuid());
658 derivateDTO
.setHasSpecimenScan(true);
659 String imageLinkText
= "scan";
660 if (derivateDTO
instanceof PreservedSpecimenDTO
&& ((PreservedSpecimenDTO
) derivateDTO
).getAccessionNumber() != null) {
661 imageLinkText
= ((PreservedSpecimenDTO
) derivateDTO
).getAccessionNumber();
663 derivateDataDTO
.addSpecimenScan(mediaUriString
== null ?
"" : mediaUriString
, imageLinkText
);
666 else if (media
.getKindOfUnit().getUuid().equals(UUID
.fromString("31eb8d02-bf5d-437c-bcc6-87a626445f34"))) {
667 derivateDataDTO
.addDetailImageUuid(media
.getMediaSpecimen().getUuid());
668 derivateDTO
.setHasDetailImage(true);
670 if (media
.getMediaSpecimen() != null && media
.getMediaSpecimen().getTitle() != null) {
671 motif
= media
.getMediaSpecimen().getTitle().getText();
673 derivateDataDTO
.addDetailImage(mediaUriString
== null ?
"" : mediaUriString
, motif
!= null ? motif
: "[no motif]");
678 return derivateDataDTO
;
681 private String
removeTail(String string
, final String tail
) {
682 if (string
.endsWith(tail
)) {
683 string
= string
.substring(0, string
.length() - tail
.length());
688 private String
getMediaUriString(MediaSpecimen mediaSpecimen
) {
689 String mediaUri
= null;
690 Collection
<MediaRepresentation
> mediaRepresentations
= mediaSpecimen
.getMediaSpecimen().getRepresentations();
691 if (mediaRepresentations
!= null && !mediaRepresentations
.isEmpty()) {
692 Collection
<MediaRepresentationPart
> mediaRepresentationParts
= mediaRepresentations
.iterator().next().getParts();
693 if (mediaRepresentationParts
!= null && !mediaRepresentationParts
.isEmpty()) {
694 MediaRepresentationPart part
= mediaRepresentationParts
.iterator().next();
695 if (part
.getUri() != null) {
696 mediaUri
= part
.getUri().toASCIIString();
703 private Collection
<DerivedUnit
> getDerivedUnitsFor(SpecimenOrObservationBase
<?
> specimen
) {
704 Collection
<DerivedUnit
> derivedUnits
= new ArrayList
<DerivedUnit
>();
705 for (DerivationEvent derivationEvent
: specimen
.getDerivationEvents()) {
706 for (DerivedUnit derivative
: derivationEvent
.getDerivatives()) {
707 derivedUnits
.add(derivative
);
708 derivedUnits
.addAll(getDerivedUnitsFor(derivative
));
715 @SuppressWarnings("unchecked")
717 public <T
extends SpecimenOrObservationBase
> Pager
<T
> pageByAssociatedTaxon(Class
<T
> type
, Set
<TaxonRelationshipEdge
> includeRelationships
,
718 Taxon associatedTaxon
, Integer maxDepth
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
720 Set
<Taxon
> taxa
= new HashSet
<Taxon
>();
721 Set
<Integer
> occurrenceIds
= new HashSet
<Integer
>();
722 List
<T
> occurrences
= new ArrayList
<T
>();
724 // Integer limit = PagerUtils.limitFor(pageSize);
725 // Integer start = PagerUtils.startFor(pageSize, pageNumber);
727 if (!getSession().contains(associatedTaxon
)) {
728 associatedTaxon
= (Taxon
) taxonService
.load(associatedTaxon
.getUuid());
731 if (includeRelationships
!= null) {
732 taxa
= taxonService
.listRelatedTaxa(associatedTaxon
, includeRelationships
, maxDepth
, null, null, propertyPaths
);
735 taxa
.add(associatedTaxon
);
737 for (Taxon taxon
: taxa
) {
738 List
<T
> perTaxonOccurrences
= dao
.listByAssociatedTaxon(type
, taxon
, null, null, orderHints
, propertyPaths
);
739 for (SpecimenOrObservationBase o
: perTaxonOccurrences
) {
740 occurrenceIds
.add(o
.getId());
743 occurrences
= (List
<T
>) dao
.listByIds(occurrenceIds
, pageSize
, pageNumber
, orderHints
, propertyPaths
);
745 return new DefaultPagerImpl
<T
>(pageNumber
, occurrenceIds
.size(), pageSize
, occurrences
);
750 public <T
extends SpecimenOrObservationBase
> Pager
<T
> pageByAssociatedTaxon(Class
<T
> type
, Set
<TaxonRelationshipEdge
> includeRelationships
,
751 String taxonUUID
, Integer maxDepth
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
753 UUID uuid
= UUID
.fromString(taxonUUID
);
754 Taxon tax
= (Taxon
) taxonService
.load(uuid
);
755 // TODO REMOVE NULL STATEMENT
757 return pageByAssociatedTaxon(type
, includeRelationships
, tax
, maxDepth
, pageSize
, pageNumber
, orderHints
, propertyPaths
);
762 public Pager
<SearchResult
<SpecimenOrObservationBase
>> findByFullText(
763 Class
<?
extends SpecimenOrObservationBase
> clazz
, String queryString
, Rectangle boundingBox
, List
<Language
> languages
,
764 boolean highlightFragments
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
,
765 List
<String
> propertyPaths
) throws CorruptIndexException
, IOException
, ParseException
{
767 LuceneSearch luceneSearch
= prepareByFullTextSearch(clazz
, queryString
, boundingBox
, languages
, highlightFragments
);
769 // --- execute search
770 TopGroups
<BytesRef
> topDocsResultSet
= luceneSearch
.executeSearch(pageSize
, pageNumber
);
772 Map
<CdmBaseType
, String
> idFieldMap
= new HashMap
<CdmBaseType
, String
>();
773 idFieldMap
.put(CdmBaseType
.SPECIMEN_OR_OBSERVATIONBASE
, "id");
775 // --- initialize taxa, highlight matches ....
776 ISearchResultBuilder searchResultBuilder
= new SearchResultBuilder(luceneSearch
, luceneSearch
.getQuery());
777 @SuppressWarnings("rawtypes")
778 List
<SearchResult
<SpecimenOrObservationBase
>> searchResults
= searchResultBuilder
.createResultSet(
779 topDocsResultSet
, luceneSearch
.getHighlightFields(), dao
, idFieldMap
, propertyPaths
);
781 int totalHits
= topDocsResultSet
!= null ? topDocsResultSet
.totalGroupCount
: 0;
783 return new DefaultPagerImpl
<SearchResult
<SpecimenOrObservationBase
>>(pageNumber
, totalHits
, pageSize
,
788 private LuceneSearch
prepareByFullTextSearch(Class
<?
extends SpecimenOrObservationBase
> clazz
, String queryString
, Rectangle bbox
,
789 List
<Language
> languages
, boolean highlightFragments
) {
791 Builder finalQueryBuilder
= new Builder();
792 Builder textQueryBuilder
= new Builder();
794 LuceneSearch luceneSearch
= new LuceneSearch(luceneIndexToolProvider
, FieldUnit
.class);
795 QueryFactory queryFactory
= luceneIndexToolProvider
.newQueryFactoryFor(FieldUnit
.class);
798 luceneSearch
.setCdmTypRestriction(clazz
);
799 if (queryString
!= null) {
800 textQueryBuilder
.add(queryFactory
.newTermQuery("titleCache", queryString
), Occur
.SHOULD
);
801 finalQueryBuilder
.add(textQueryBuilder
.build(), Occur
.MUST
);
806 finalQueryBuilder
.add(QueryFactory
.buildSpatialQueryByRange(bbox
, "gatheringEvent.exactLocation.point"), Occur
.MUST
);
809 luceneSearch
.setQuery(finalQueryBuilder
.build());
812 SortField
[] sortFields
= new SortField
[] { SortField
.FIELD_SCORE
, new SortField("titleCache__sort", SortField
.Type
.STRING
, false) };
813 luceneSearch
.setSortFields(sortFields
);
815 if (highlightFragments
) {
816 luceneSearch
.setHighlightFields(queryFactory
.getTextFieldNamesAsArray());
823 public Collection
<FieldUnit
> getFieldUnits(UUID derivedUnitUuid
) {
824 //It will search recursively over all {@link DerivationEvent}s and get the "originals" ({@link SpecimenOrObservationBase})
825 //from which this DerivedUnit was derived until all FieldUnits are found.
827 // FIXME: use HQL queries to increase performance
828 SpecimenOrObservationBase
<?
> specimen
= load(derivedUnitUuid
);
829 // specimen = HibernateProxyHelper.deproxy(specimen, SpecimenOrObservationBase.class);
830 Collection
<FieldUnit
> fieldUnits
= new ArrayList
<FieldUnit
>();
832 if (specimen
.isInstanceOf(FieldUnit
.class)) {
833 fieldUnits
.add(HibernateProxyHelper
.deproxy(specimen
, FieldUnit
.class));
835 else if(specimen
.isInstanceOf(DerivedUnit
.class)){
836 fieldUnits
.addAll(getFieldUnits(HibernateProxyHelper
.deproxy(specimen
, DerivedUnit
.class)));
841 private Collection
<FieldUnit
> getFieldUnits(DerivedUnit derivedUnit
) {
842 Collection
<FieldUnit
> fieldUnits
= new HashSet
<FieldUnit
>();
843 Set
<SpecimenOrObservationBase
> originals
= derivedUnit
.getOriginals();
844 if (originals
!= null && !originals
.isEmpty()) {
845 for (SpecimenOrObservationBase
<?
> original
: originals
) {
846 if (original
.isInstanceOf(FieldUnit
.class)) {
847 fieldUnits
.add(HibernateProxyHelper
.deproxy(original
, FieldUnit
.class));
849 else if(original
.isInstanceOf(DerivedUnit
.class)){
850 fieldUnits
.addAll(getFieldUnits(HibernateProxyHelper
.deproxy(original
, DerivedUnit
.class)));
858 @Transactional(readOnly
= false)
859 public UpdateResult
moveSequence(DnaSample from
, DnaSample to
, Sequence sequence
) {
860 return moveSequence(from
.getUuid(), to
.getUuid(), sequence
.getUuid());
864 @Transactional(readOnly
= false)
865 public UpdateResult
moveSequence(UUID fromUuid
, UUID toUuid
, UUID sequenceUuid
) {
866 // reload specimens to avoid session conflicts
867 DnaSample from
= (DnaSample
) load(fromUuid
);
868 DnaSample to
= (DnaSample
) load(toUuid
);
869 Sequence sequence
= sequenceService
.load(sequenceUuid
);
871 if (from
== null || to
== null || sequence
== null) {
872 throw new TransientObjectException("One of the CDM entities has not been saved to the data base yet. Moving only works for persisted/saved CDM entities.\n" +
873 "Operation was move "+sequence
+ " from "+from
+" to "+to
);
875 UpdateResult result
= new UpdateResult();
876 from
.removeSequence(sequence
);
878 to
.addSequence(sequence
);
880 result
.setStatus(Status
.OK
);
881 result
.addUpdatedObject(from
);
882 result
.addUpdatedObject(to
);
887 @Transactional(readOnly
= false)
888 public boolean moveDerivate(SpecimenOrObservationBase
<?
> from
, SpecimenOrObservationBase
<?
> to
, DerivedUnit derivate
) {
889 return moveDerivate(from
!=null?from
.getUuid():null, to
.getUuid(), derivate
.getUuid()).isOk();
893 @Transactional(readOnly
= false)
894 public UpdateResult
moveDerivate(UUID specimenFromUuid
, UUID specimenToUuid
, UUID derivateUuid
) {
895 // reload specimens to avoid session conflicts
896 SpecimenOrObservationBase
<?
> from
= null;
897 if(specimenFromUuid
!=null){
898 from
= load(specimenFromUuid
);
900 SpecimenOrObservationBase
<?
> to
= load(specimenToUuid
);
901 DerivedUnit derivate
= (DerivedUnit
) load(derivateUuid
);
903 if ((specimenFromUuid
!=null && from
== null) || to
== null || derivate
== null) {
904 throw new TransientObjectException("One of the CDM entities has not been saved to the data base yet. Moving only works for persisted/saved CDM entities.\n" +
905 "Operation was move "+derivate
+ " from "+from
+" to "+to
);
907 UpdateResult result
= new UpdateResult();
908 SpecimenOrObservationType derivateType
= derivate
.getRecordBasis();
909 SpecimenOrObservationType toType
= to
.getRecordBasis();
910 // check if type is a sub derivate type
911 if(toType
==SpecimenOrObservationType
.FieldUnit
//moving to FieldUnit always works
912 || derivateType
==SpecimenOrObservationType
.Media
//moving media always works
913 || (derivateType
.isKindOf(toType
) && toType
!=derivateType
)){ //moving only to parent derivate type
915 // remove derivation event from parent specimen of dragged object
916 DerivationEvent eventToRemove
= null;
917 for (DerivationEvent event
: from
.getDerivationEvents()) {
918 if (event
.getDerivatives().contains(derivate
)) {
919 eventToRemove
= event
;
923 from
.removeDerivationEvent(eventToRemove
);
924 if(eventToRemove
!=null){
925 // add new derivation event to target and copy the event parameters of the old one
926 DerivationEvent derivedFromNewOriginalEvent
= DerivationEvent
.NewSimpleInstance(to
, derivate
, null);
927 derivedFromNewOriginalEvent
.setActor(eventToRemove
.getActor());
928 derivedFromNewOriginalEvent
.setDescription(eventToRemove
.getDescription());
929 derivedFromNewOriginalEvent
.setInstitution(eventToRemove
.getInstitution());
930 derivedFromNewOriginalEvent
.setTimeperiod(eventToRemove
.getTimeperiod());
931 derivedFromNewOriginalEvent
.setType(eventToRemove
.getType());
932 to
.addDerivationEvent(derivedFromNewOriginalEvent
);
933 derivate
.setDerivedFrom(derivedFromNewOriginalEvent
);
937 //derivative had no parent before so we use empty derivation event
938 DerivationEvent derivedFromNewOriginalEvent
= DerivationEvent
.NewSimpleInstance(to
, derivate
, null);
939 to
.addDerivationEvent(derivedFromNewOriginalEvent
);
940 derivate
.setDerivedFrom(derivedFromNewOriginalEvent
);
947 result
.setStatus(Status
.OK
);
948 result
.addUpdatedObject(from
);
949 result
.addUpdatedObject(to
);
951 result
.setStatus(Status
.ERROR
);
957 public Collection
<ICdmBase
> getNonCascadedAssociatedElements(SpecimenOrObservationBase
<?
> specimen
) {
958 // potential fields that are not persisted cascadingly
971 --CollectingAreas TERM
979 -storedUnder CDM TaxonNameBase
982 Collection
<ICdmBase
> nonCascadedCdmEntities
= new HashSet
<ICdmBase
>();
984 //Choose the correct entry point to traverse the graph (FieldUnit or DerivedUnit)
987 if (specimen
.isInstanceOf(FieldUnit
.class)) {
988 nonCascadedCdmEntities
.addAll(getFieldUnitNonCascadedAssociatedElements(HibernateProxyHelper
.deproxy(specimen
, FieldUnit
.class)));
991 else if (specimen
.isInstanceOf(DerivedUnit
.class)) {
992 DerivedUnit derivedUnit
= HibernateProxyHelper
.deproxy(specimen
, DerivedUnit
.class);
993 if (derivedUnit
.getDerivedFrom() != null) {
994 Collection
<FieldUnit
> fieldUnits
= getFieldUnits(derivedUnit
);
995 for (FieldUnit fieldUnit
: fieldUnits
) {
996 nonCascadedCdmEntities
.addAll(getFieldUnitNonCascadedAssociatedElements(fieldUnit
));
1000 return nonCascadedCdmEntities
;
1003 private Collection
<ICdmBase
> getFieldUnitNonCascadedAssociatedElements(FieldUnit fieldUnit
) {
1004 // get non cascaded element on SpecimenOrObservationBase level
1005 Collection
<ICdmBase
> nonCascadedCdmEntities
= getSpecimenOrObservationNonCascadedAssociatedElements(fieldUnit
);
1007 // get FieldUnit specific elements
1008 GatheringEvent gatheringEvent
= fieldUnit
.getGatheringEvent();
1009 if (gatheringEvent
!= null) {
1011 if (gatheringEvent
.getCountry() != null) {
1012 nonCascadedCdmEntities
.add(gatheringEvent
.getCountry());
1015 for (NamedArea namedArea
: gatheringEvent
.getCollectingAreas()) {
1016 nonCascadedCdmEntities
.add(namedArea
);
1019 for (DerivationEvent derivationEvent
: fieldUnit
.getDerivationEvents()) {
1020 for (DerivedUnit derivedUnit
: derivationEvent
.getDerivatives()) {
1021 nonCascadedCdmEntities
.addAll(getDerivedUnitNonCascadedAssociatedElements(derivedUnit
));
1024 return nonCascadedCdmEntities
;
1027 private Collection
<ICdmBase
> getDerivedUnitNonCascadedAssociatedElements(DerivedUnit derivedUnit
) {
1028 // get non cascaded element on SpecimenOrObservationBase level
1029 Collection
<ICdmBase
> nonCascadedCdmEntities
= getSpecimenOrObservationNonCascadedAssociatedElements(derivedUnit
);
1031 // get DerivedUnit specific elements
1032 if (derivedUnit
.getCollection() != null && derivedUnit
.getCollection().getInstitute() != null) {
1033 for (DefinedTerm type
: derivedUnit
.getCollection().getInstitute().getTypes()) {
1034 nonCascadedCdmEntities
.add(type
);
1037 if (derivedUnit
.getPreservation() != null && derivedUnit
.getPreservation().getMedium() != null) {
1038 nonCascadedCdmEntities
.add(derivedUnit
.getPreservation().getMedium());
1040 if (derivedUnit
.getStoredUnder() != null) {
1041 nonCascadedCdmEntities
.add(derivedUnit
.getStoredUnder());
1043 return nonCascadedCdmEntities
;
1046 private Collection
<ICdmBase
> getSpecimenOrObservationNonCascadedAssociatedElements(
1047 SpecimenOrObservationBase
<?
> specimen
) {
1048 Collection
<ICdmBase
> nonCascadedCdmEntities
= new HashSet
<ICdmBase
>();
1049 // scan SpecimenOrObservationBase
1050 for (DeterminationEvent determinationEvent
: specimen
.getDeterminations()) {
1052 if (determinationEvent
.getModifier() != null) {
1053 nonCascadedCdmEntities
.add(determinationEvent
.getModifier());
1057 if (specimen
.getKindOfUnit() != null) {
1058 nonCascadedCdmEntities
.add(specimen
.getKindOfUnit());
1061 if (specimen
.getLifeStage() != null) {
1062 nonCascadedCdmEntities
.add(specimen
.getLifeStage());
1065 if (specimen
.getSex() != null) {
1066 nonCascadedCdmEntities
.add(specimen
.getSex());
1068 return nonCascadedCdmEntities
;
1072 public DeleteResult
isDeletable(SpecimenOrObservationBase specimen
, DeleteConfiguratorBase config
) {
1073 DeleteResult deleteResult
= new DeleteResult();
1074 SpecimenDeleteConfigurator specimenDeleteConfigurator
= (SpecimenDeleteConfigurator
) config
;
1076 // check elements found by super method
1077 Set
<CdmBase
> relatedObjects
= super.isDeletable(specimen
, config
).getRelatedObjects();
1078 for (CdmBase cdmBase
: relatedObjects
) {
1079 // check for type designation
1080 if (cdmBase
.isInstanceOf(SpecimenTypeDesignation
.class) && !specimenDeleteConfigurator
.isDeleteFromTypeDesignation()) {
1081 deleteResult
.setAbort();
1082 deleteResult
.addException(new ReferencedObjectUndeletableException("Specimen is a type specimen."));
1083 deleteResult
.addRelatedObject(cdmBase
);
1086 // check for IndividualsAssociations
1087 else if (cdmBase
.isInstanceOf(IndividualsAssociation
.class) && !specimenDeleteConfigurator
.isDeleteFromIndividualsAssociation()) {
1088 deleteResult
.setAbort();
1089 deleteResult
.addException(new ReferencedObjectUndeletableException("Specimen is still associated via IndividualsAssociations"));
1090 deleteResult
.addRelatedObject(cdmBase
);
1093 // check for specimen/taxon description
1094 else if((cdmBase
.isInstanceOf(SpecimenDescription
.class) || cdmBase
.isInstanceOf(TaxonDescription
.class))
1095 && !specimenDeleteConfigurator
.isDeleteFromDescription()){
1096 deleteResult
.setAbort();
1097 deleteResult
.addException(new ReferencedObjectUndeletableException("Specimen is still used in a Description."));
1098 deleteResult
.addRelatedObject(cdmBase
);
1101 // check for children and parents (derivation events)
1102 else if (cdmBase
.isInstanceOf(DerivationEvent
.class)) {
1103 DerivationEvent derivationEvent
= HibernateProxyHelper
.deproxy(cdmBase
, DerivationEvent
.class);
1104 // check if derivation event is empty
1105 if (!derivationEvent
.getDerivatives().isEmpty() && derivationEvent
.getOriginals().contains(specimen
)) {
1106 // if derivationEvent is the childEvent and contains derivations
1107 // if (derivationEvent.getDerivatives().contains(specimen)) {
1108 // //if it is the parent event the specimen is still deletable
1111 if(!specimenDeleteConfigurator
.isDeleteChildren()){
1112 //if children should not be deleted then it is undeletable
1113 deleteResult
.setAbort();
1114 deleteResult
.addException(new ReferencedObjectUndeletableException("Derivative still has child derivatives."));
1115 deleteResult
.addRelatedObject(cdmBase
);
1119 // check all children if they can be deleted
1120 Set
<DerivedUnit
> derivatives
= derivationEvent
.getDerivatives();
1121 DeleteResult childResult
= new DeleteResult();
1122 for (DerivedUnit derivedUnit
: derivatives
) {
1123 childResult
.includeResult(isDeletable(derivedUnit
, specimenDeleteConfigurator
));
1125 if (!childResult
.isOk()) {
1126 deleteResult
.setAbort();
1127 deleteResult
.includeResult(childResult
);
1128 deleteResult
.addRelatedObject(cdmBase
);
1134 // check for amplification
1135 else if (cdmBase
.isInstanceOf(AmplificationResult
.class) && !specimenDeleteConfigurator
.isDeleteMolecularData()) {
1136 deleteResult
.setAbort();
1137 deleteResult
.addException(new ReferencedObjectUndeletableException("DnaSample is used in amplification results."));
1138 deleteResult
.addRelatedObject(cdmBase
);
1141 // check for sequence
1142 else if (cdmBase
.isInstanceOf(Sequence
.class) && !specimenDeleteConfigurator
.isDeleteMolecularData()) {
1143 deleteResult
.setAbort();
1144 deleteResult
.addException(new ReferencedObjectUndeletableException("DnaSample is used in sequences."));
1145 deleteResult
.addRelatedObject(cdmBase
);
1149 if (deleteResult
.isOk()) {
1150 //add all related object if deletion is OK so they can be handled by the delete() method
1151 deleteResult
.addRelatedObjects(relatedObjects
);
1153 return deleteResult
;
1160 public DeleteResult
delete(UUID specimenUuid
, SpecimenDeleteConfigurator config
) {
1161 return delete(load(specimenUuid
), config
);
1165 public DeleteResult
delete(SpecimenOrObservationBase
<?
> specimen
, SpecimenDeleteConfigurator config
) {
1166 specimen
= HibernateProxyHelper
.deproxy(specimen
, SpecimenOrObservationBase
.class);
1168 if (config
.isDeleteChildren()) {
1169 Set
<DerivationEvent
> derivationEvents
= specimen
.getDerivationEvents();
1170 //clone to avoid concurrent modification
1171 //can happen if the child is deleted and deleted its own derivedFrom event
1172 Set
<DerivationEvent
> derivationEventsClone
= new HashSet
<DerivationEvent
>(derivationEvents
);
1173 for (DerivationEvent derivationEvent
: derivationEventsClone
) {
1174 Set
<DerivedUnit
> derivatives
= derivationEvent
.getDerivatives();
1175 for (DerivedUnit derivedUnit
: derivatives
) {
1176 delete(derivedUnit
, config
);
1181 DeleteResult deleteResult
= isDeletable(specimen
, config
);
1182 if (!deleteResult
.isOk()) {
1183 return deleteResult
;
1186 // check related objects
1187 Set
<CdmBase
> relatedObjects
= deleteResult
.getRelatedObjects();
1189 for (CdmBase relatedObject
: relatedObjects
) {
1190 // check for TypeDesignations
1191 if (relatedObject
.isInstanceOf(SpecimenTypeDesignation
.class)) {
1192 SpecimenTypeDesignation designation
= HibernateProxyHelper
.deproxy(relatedObject
, SpecimenTypeDesignation
.class);
1193 designation
.setTypeSpecimen(null);
1194 List
<TaxonNameBase
> typifiedNames
= new ArrayList
<TaxonNameBase
>();
1195 typifiedNames
.addAll(designation
.getTypifiedNames());
1196 for (TaxonNameBase taxonNameBase
: typifiedNames
) {
1197 taxonNameBase
.removeTypeDesignation(designation
);
1200 // delete IndividualsAssociation
1201 if (relatedObject
.isInstanceOf(IndividualsAssociation
.class)) {
1202 IndividualsAssociation association
= HibernateProxyHelper
.deproxy(relatedObject
, IndividualsAssociation
.class);
1203 association
.setAssociatedSpecimenOrObservation(null);
1204 association
.getInDescription().removeElement(association
);
1206 // check for taxon description
1207 if (relatedObject
.isInstanceOf(TaxonDescription
.class)) {
1208 TaxonDescription taxonDescription
= HibernateProxyHelper
.deproxy(relatedObject
, TaxonDescription
.class);
1209 taxonDescription
.setDescribedSpecimenOrObservation(null);
1211 // check for specimen description
1212 if (relatedObject
.isInstanceOf(SpecimenDescription
.class)) {
1213 SpecimenDescription specimenDescription
= HibernateProxyHelper
.deproxy(relatedObject
, SpecimenDescription
.class);
1214 // check if specimen is "described" specimen
1215 if (specimenDescription
.getDescribedSpecimenOrObservation().equals(specimen
)) {
1216 specimenDescription
.setDescribedSpecimenOrObservation(null);
1218 // check if description is a description of the given specimen
1219 if (specimen
.getDescriptions().contains(specimenDescription
)) {
1220 specimen
.removeDescription(specimenDescription
);
1222 DeleteResult descriptionDelete
= descriptionService
.isDeletable(specimenDescription
, null);
1223 if (descriptionDelete
.isOk()){
1224 descriptionService
.delete(specimenDescription
);
1227 // check for amplification
1228 if (relatedObject
.isInstanceOf(AmplificationResult
.class)) {
1229 AmplificationResult amplificationResult
= HibernateProxyHelper
.deproxy(relatedObject
, AmplificationResult
.class);
1230 amplificationResult
.getDnaSample().removeAmplificationResult(amplificationResult
);
1232 // check for sequence
1233 if (relatedObject
.isInstanceOf(Sequence
.class)) {
1234 Sequence sequence
= HibernateProxyHelper
.deproxy(relatedObject
, Sequence
.class);
1235 sequence
.getDnaSample().removeSequence(sequence
);
1237 // check for children and parents (derivation events)
1238 if (relatedObject
.isInstanceOf(DerivationEvent
.class)) {
1239 DerivationEvent derivationEvent
= HibernateProxyHelper
.deproxy(relatedObject
, DerivationEvent
.class);
1240 // parent derivation event (derivedFrom)
1241 if (derivationEvent
.getDerivatives().contains(specimen
) && specimen
.isInstanceOf(DerivedUnit
.class)) {
1242 derivationEvent
.removeDerivative(HibernateProxyHelper
.deproxy(specimen
, DerivedUnit
.class));
1243 if (derivationEvent
.getDerivatives().isEmpty()) {
1244 Set
<SpecimenOrObservationBase
> originals
= derivationEvent
.getOriginals();
1245 for (SpecimenOrObservationBase specimenOrObservationBase
: originals
) {
1246 specimenOrObservationBase
.removeDerivationEvent(derivationEvent
);
1247 deleteResult
.addUpdatedObject(specimenOrObservationBase
);
1252 //child derivation events should not occur since we delete the hierarchy from bottom to top
1257 deleteResult
.includeResult(delete(specimen
));
1258 return deleteResult
;
1262 public Collection
<IndividualsAssociation
> listIndividualsAssociations(SpecimenOrObservationBase
<?
> specimen
, Integer limit
, Integer start
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
1263 return dao
.listIndividualsAssociations(specimen
, null, null, null, null);
1267 public Collection
<TaxonBase
<?
>> listAssociatedTaxa(SpecimenOrObservationBase
<?
> specimen
, Integer limit
, Integer start
,
1268 List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
1269 Collection
<TaxonBase
<?
>> associatedTaxa
= new HashSet
<TaxonBase
<?
>>();
1271 //individuals associations
1272 associatedTaxa
.addAll(listIndividualsAssociationTaxa(specimen
, limit
, start
, orderHints
, propertyPaths
));
1274 if(specimen
.isInstanceOf(DerivedUnit
.class)){
1275 associatedTaxa
.addAll(listTypeDesignationTaxa(HibernateProxyHelper
.deproxy(specimen
, DerivedUnit
.class), limit
, start
, orderHints
, propertyPaths
));
1278 associatedTaxa
.addAll(listDeterminedTaxa(specimen
, limit
, start
, orderHints
, propertyPaths
));
1280 return associatedTaxa
;
1285 public Collection
<TaxonBase
<?
>> listDeterminedTaxa(SpecimenOrObservationBase
<?
> specimen
, Integer limit
, Integer start
,
1286 List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
1287 Collection
<TaxonBase
<?
>> associatedTaxa
= new HashSet
<TaxonBase
<?
>>();
1288 for (DeterminationEvent determinationEvent
: listDeterminationEvents(specimen
, limit
, start
, orderHints
, propertyPaths
)) {
1289 if(determinationEvent
.getIdentifiedUnit().equals(specimen
)){
1290 if(determinationEvent
.getTaxon()!=null){
1291 associatedTaxa
.add(determinationEvent
.getTaxon());
1293 if(determinationEvent
.getTaxonName()!=null){
1294 associatedTaxa
.addAll(determinationEvent
.getTaxonName().getTaxonBases());
1298 return associatedTaxa
;
1302 public Collection
<TaxonBase
<?
>> listTypeDesignationTaxa(DerivedUnit specimen
, Integer limit
, Integer start
,
1303 List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
1304 Collection
<TaxonBase
<?
>> associatedTaxa
= new HashSet
<TaxonBase
<?
>>();
1305 for (SpecimenTypeDesignation typeDesignation
: listTypeDesignations(specimen
, limit
, start
, orderHints
, propertyPaths
)) {
1306 if(typeDesignation
.getTypeSpecimen().equals(specimen
)){
1307 Set
<TaxonNameBase
> typifiedNames
= typeDesignation
.getTypifiedNames();
1308 for (TaxonNameBase taxonNameBase
: typifiedNames
) {
1309 associatedTaxa
.addAll(taxonNameBase
.getTaxa());
1313 return associatedTaxa
;
1317 public Collection
<TaxonBase
<?
>> listIndividualsAssociationTaxa(SpecimenOrObservationBase
<?
> specimen
, Integer limit
, Integer start
,
1318 List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
1319 Collection
<TaxonBase
<?
>> associatedTaxa
= new HashSet
<TaxonBase
<?
>>();
1320 for (IndividualsAssociation individualsAssociation
: listIndividualsAssociations(specimen
, limit
, start
, orderHints
, propertyPaths
)) {
1321 if(individualsAssociation
.getInDescription().isInstanceOf(TaxonDescription
.class)){
1322 TaxonDescription taxonDescription
= HibernateProxyHelper
.deproxy(individualsAssociation
.getInDescription(), TaxonDescription
.class);
1323 if(taxonDescription
.getTaxon()!=null){
1324 associatedTaxa
.add(taxonDescription
.getTaxon());
1328 return associatedTaxa
;
1332 public Collection
<DeterminationEvent
> listDeterminationEvents(SpecimenOrObservationBase
<?
> specimen
,
1333 Integer limit
, Integer start
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
1334 return dao
.listDeterminationEvents(specimen
, limit
, start
, orderHints
, propertyPaths
);
1338 public Map
<DerivedUnit
, Collection
<SpecimenTypeDesignation
>> listTypeDesignations(
1339 Collection
<DerivedUnit
> specimens
, Integer limit
, Integer start
,
1340 List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
1341 Map
<DerivedUnit
, Collection
<SpecimenTypeDesignation
>> typeDesignationMap
= new HashMap
<DerivedUnit
, Collection
<SpecimenTypeDesignation
>>();
1342 for (DerivedUnit specimen
: specimens
) {
1343 Collection
<SpecimenTypeDesignation
> typeDesignations
= listTypeDesignations(specimen
, limit
, start
, orderHints
, propertyPaths
);
1344 typeDesignationMap
.put(specimen
, typeDesignations
);
1346 return typeDesignationMap
;
1350 public Collection
<SpecimenTypeDesignation
> listTypeDesignations(DerivedUnit specimen
,
1351 Integer limit
, Integer start
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
1352 return dao
.listTypeDesignations(specimen
, limit
, start
, orderHints
, propertyPaths
);
1356 public Collection
<DescriptionBase
<?
>> listDescriptionsWithDescriptionSpecimen(
1357 SpecimenOrObservationBase
<?
> specimen
, Integer limit
, Integer start
, List
<OrderHint
> orderHints
,
1358 List
<String
> propertyPaths
) {
1359 return dao
.listDescriptionsWithDescriptionSpecimen(specimen
, limit
, start
, orderHints
, propertyPaths
);
1363 @Deprecated //this is not a service layer task so it may be removed in future versions
1364 public Collection
<DescriptionElementBase
> getCharacterDataForSpecimen(SpecimenOrObservationBase
<?
> specimen
) {
1365 if (specimen
!= null) {
1366 return specimen
.characterData();
1368 return new ArrayList
<DescriptionElementBase
>();
1373 public Collection
<DescriptionElementBase
> getCharacterDataForSpecimen(UUID specimenUuid
) {
1374 SpecimenOrObservationBase
<?
> specimen
= load(specimenUuid
);
1375 if (specimen
!= null) {
1376 return getCharacterDataForSpecimen(specimen
);
1379 throw new DataRetrievalFailureException("Specimen with the given uuid not found in the data base");
1385 public Pager
<SpecimenOrObservationBase
> findByTitle(
1386 IIdentifiableEntityServiceConfigurator
<SpecimenOrObservationBase
> config
) {
1387 if (config
instanceof FindOccurrencesConfigurator
) {
1388 FindOccurrencesConfigurator occurrenceConfig
= (FindOccurrencesConfigurator
) config
;
1389 List
<SpecimenOrObservationBase
> occurrences
= new ArrayList
<SpecimenOrObservationBase
>();
1391 if(occurrenceConfig
.getAssociatedTaxonUuid()!=null){
1392 TaxonBase taxonBase
= taxonService
.load(occurrenceConfig
.getAssociatedTaxonUuid());
1393 if(taxonBase
.isInstanceOf(Taxon
.class)){
1394 taxon
= HibernateProxyHelper
.deproxy(taxonBase
, Taxon
.class);
1397 TaxonNameBase taxonName
= null;
1398 if(occurrenceConfig
.getAssociatedTaxonNameUuid()!=null){
1399 taxonName
= nameService
.load(occurrenceConfig
.getAssociatedTaxonNameUuid());
1401 occurrences
.addAll(dao
.findOccurrences(occurrenceConfig
.getClazz(),
1402 occurrenceConfig
.getTitleSearchString(), occurrenceConfig
.getSignificantIdentifier(),
1403 occurrenceConfig
.getSpecimenType(), taxon
, taxonName
, occurrenceConfig
.getMatchMode(), null, null,
1404 occurrenceConfig
.getOrderHints(), occurrenceConfig
.getPropertyPaths()));
1405 //filter out (un-)assigned specimens
1406 if(taxon
==null && taxonName
==null){
1407 AssignmentStatus assignmentStatus
= occurrenceConfig
.getAssignmentStatus();
1408 List
<SpecimenOrObservationBase
<?
>> specimenWithAssociations
= new ArrayList
<SpecimenOrObservationBase
<?
>>();
1409 if(!assignmentStatus
.equals(AssignmentStatus
.ALL_SPECIMENS
)){
1410 for (SpecimenOrObservationBase specimenOrObservationBase
: occurrences
) {
1411 Collection
<TaxonBase
<?
>> associatedTaxa
= listAssociatedTaxa(specimenOrObservationBase
, null, null, null, null);
1412 if(!associatedTaxa
.isEmpty()){
1413 specimenWithAssociations
.add(specimenOrObservationBase
);
1417 if(assignmentStatus
.equals(AssignmentStatus
.UNASSIGNED_SPECIMENS
)){
1418 occurrences
.removeAll(specimenWithAssociations
);
1420 if(assignmentStatus
.equals(AssignmentStatus
.ASSIGNED_SPECIMENS
)){
1421 occurrences
= new ArrayList
<SpecimenOrObservationBase
>(specimenWithAssociations
);
1424 // indirectly associated specimens
1425 List
<SpecimenOrObservationBase
> indirectlyAssociatedOccurrences
= new ArrayList
<SpecimenOrObservationBase
>(occurrences
);
1426 if(occurrenceConfig
.isRetrieveIndirectlyAssociatedSpecimens()){
1427 for (SpecimenOrObservationBase specimen
: occurrences
) {
1428 List
<SpecimenOrObservationBase
<?
>> allHierarchyDerivates
= getAllHierarchyDerivatives(specimen
);
1429 for (SpecimenOrObservationBase
<?
> specimenOrObservationBase
: allHierarchyDerivates
) {
1430 if(!occurrences
.contains(specimenOrObservationBase
)){
1431 indirectlyAssociatedOccurrences
.add(specimenOrObservationBase
);
1435 occurrences
= indirectlyAssociatedOccurrences
;
1438 return new DefaultPagerImpl
<SpecimenOrObservationBase
>(config
.getPageNumber(), occurrences
.size(), config
.getPageSize(), occurrences
);
1440 return super.findByTitle(config
);
1444 public List
<SpecimenOrObservationBase
<?
>> getAllHierarchyDerivatives(SpecimenOrObservationBase
<?
> specimen
){
1445 List
<SpecimenOrObservationBase
<?
>> allHierarchyDerivatives
= new ArrayList
<SpecimenOrObservationBase
<?
>>();
1446 Collection
<FieldUnit
> fieldUnits
= getFieldUnits(specimen
.getUuid());
1447 if(fieldUnits
.isEmpty()){
1448 allHierarchyDerivatives
.add(specimen
);
1449 allHierarchyDerivatives
.addAll(getAllChildDerivatives(specimen
));
1452 for (FieldUnit fieldUnit
: fieldUnits
) {
1453 allHierarchyDerivatives
.add(fieldUnit
);
1454 allHierarchyDerivatives
.addAll(getAllChildDerivatives(fieldUnit
));
1457 return allHierarchyDerivatives
;
1461 public List
<DerivedUnit
> getAllChildDerivatives(UUID specimenUuid
){
1462 return getAllChildDerivatives(load(specimenUuid
));
1466 public List
<DerivedUnit
> getAllChildDerivatives(SpecimenOrObservationBase
<?
> specimen
){
1467 List
<DerivedUnit
> childDerivate
= new ArrayList
<DerivedUnit
>();
1468 Set
<DerivationEvent
> derivationEvents
= specimen
.getDerivationEvents();
1469 for (DerivationEvent derivationEvent
: derivationEvents
) {
1470 Set
<DerivedUnit
> derivatives
= derivationEvent
.getDerivatives();
1471 for (DerivedUnit derivedUnit
: derivatives
) {
1472 childDerivate
.add(derivedUnit
);
1473 childDerivate
.addAll(getAllChildDerivatives(derivedUnit
.getUuid()));
1476 return childDerivate
;
1480 public int countOccurrences(IIdentifiableEntityServiceConfigurator
<SpecimenOrObservationBase
> config
){
1481 return findByTitle(config
).getRecords().size();