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
.api
.service
;
12 import java
.util
.ArrayList
;
13 import java
.util
.Arrays
;
14 import java
.util
.Collection
;
15 import java
.util
.HashSet
;
16 import java
.util
.Iterator
;
17 import java
.util
.List
;
20 import java
.util
.UUID
;
22 import org
.apache
.commons
.lang
.StringUtils
;
23 import org
.apache
.log4j
.Logger
;
24 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
25 import org
.springframework
.stereotype
.Service
;
26 import org
.springframework
.transaction
.annotation
.Transactional
;
28 import eu
.etaxonomy
.cdm
.api
.service
.pager
.Pager
;
29 import eu
.etaxonomy
.cdm
.api
.service
.pager
.impl
.AbstractPagerImpl
;
30 import eu
.etaxonomy
.cdm
.api
.service
.pager
.impl
.DefaultPagerImpl
;
31 import eu
.etaxonomy
.cdm
.api
.utility
.DescriptionUtility
;
32 import eu
.etaxonomy
.cdm
.common
.monitor
.IProgressMonitor
;
33 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
34 import eu
.etaxonomy
.cdm
.model
.common
.Annotation
;
35 import eu
.etaxonomy
.cdm
.model
.common
.AnnotationType
;
36 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
37 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTerm
;
38 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
39 import eu
.etaxonomy
.cdm
.model
.common
.Marker
;
40 import eu
.etaxonomy
.cdm
.model
.common
.MarkerType
;
41 import eu
.etaxonomy
.cdm
.model
.common
.TermVocabulary
;
42 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
43 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
44 import eu
.etaxonomy
.cdm
.model
.description
.DescriptiveDataSet
;
45 import eu
.etaxonomy
.cdm
.model
.description
.Distribution
;
46 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
47 import eu
.etaxonomy
.cdm
.model
.description
.FeatureTree
;
48 import eu
.etaxonomy
.cdm
.model
.description
.PresenceAbsenceTerm
;
49 import eu
.etaxonomy
.cdm
.model
.description
.SpecimenDescription
;
50 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
51 import eu
.etaxonomy
.cdm
.model
.description
.TaxonNameDescription
;
52 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
53 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
54 import eu
.etaxonomy
.cdm
.model
.location
.NamedAreaLevel
;
55 import eu
.etaxonomy
.cdm
.model
.media
.Media
;
56 import eu
.etaxonomy
.cdm
.model
.name
.TaxonName
;
57 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
58 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
59 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.IDefinedTermDao
;
60 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.ITermVocabularyDao
;
61 import eu
.etaxonomy
.cdm
.persistence
.dao
.description
.IDescriptionDao
;
62 import eu
.etaxonomy
.cdm
.persistence
.dao
.description
.IDescriptionElementDao
;
63 import eu
.etaxonomy
.cdm
.persistence
.dao
.description
.IDescriptiveDataSetDao
;
64 import eu
.etaxonomy
.cdm
.persistence
.dao
.description
.IFeatureDao
;
65 import eu
.etaxonomy
.cdm
.persistence
.dao
.description
.IFeatureNodeDao
;
66 import eu
.etaxonomy
.cdm
.persistence
.dao
.description
.IFeatureTreeDao
;
67 import eu
.etaxonomy
.cdm
.persistence
.dao
.description
.IStatisticalMeasurementValueDao
;
68 import eu
.etaxonomy
.cdm
.persistence
.dao
.taxon
.ITaxonDao
;
69 import eu
.etaxonomy
.cdm
.persistence
.dao
.taxon
.ITaxonNodeDao
;
70 import eu
.etaxonomy
.cdm
.persistence
.dto
.TermDto
;
71 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
;
72 import eu
.etaxonomy
.cdm
.strategy
.cache
.common
.IIdentifiableEntityCacheStrategy
;
76 * @author a.kohlbecker
82 @Transactional(readOnly
= true)
83 public class DescriptionServiceImpl
84 extends IdentifiableServiceBase
<DescriptionBase
,IDescriptionDao
>
85 implements IDescriptionService
{
87 private static final Logger logger
= Logger
.getLogger(DescriptionServiceImpl
.class);
89 protected IDescriptionElementDao descriptionElementDao
;
90 protected IFeatureTreeDao featureTreeDao
;
91 protected IDescriptiveDataSetDao descriptiveDataSetDao
;
92 protected IFeatureNodeDao featureNodeDao
;
93 protected IFeatureDao featureDao
;
94 protected ITermVocabularyDao vocabularyDao
;
95 protected IDefinedTermDao definedTermDao
;
96 protected IStatisticalMeasurementValueDao statisticalMeasurementValueDao
;
97 protected ITaxonDao taxonDao
;
98 protected ITaxonNodeDao taxonNodeDao
;
99 protected IDescriptiveDataSetDao dataSetDao
;
102 private IProgressMonitorService progressMonitorService
;
104 //TODO change to Interface
105 private NaturalLanguageGenerator naturalLanguageGenerator
;
108 protected void setFeatureTreeDao(IFeatureTreeDao featureTreeDao
) {
109 this.featureTreeDao
= featureTreeDao
;
113 protected void setDescriptiveDataSetDao(IDescriptiveDataSetDao descriptiveDataSetDao
) {
114 this.descriptiveDataSetDao
= descriptiveDataSetDao
;
118 protected void setFeatureNodeDao(IFeatureNodeDao featureNodeDao
) {
119 this.featureNodeDao
= featureNodeDao
;
123 protected void setFeatureDao(IFeatureDao featureDao
) {
124 this.featureDao
= featureDao
;
128 protected void setVocabularyDao(ITermVocabularyDao vocabularyDao
) {
129 this.vocabularyDao
= vocabularyDao
;
133 protected void setDefinedTermDao(IDefinedTermDao definedTermDao
) {
134 this.definedTermDao
= definedTermDao
;
138 protected void statisticalMeasurementValueDao(IStatisticalMeasurementValueDao statisticalMeasurementValueDao
) {
139 this.statisticalMeasurementValueDao
= statisticalMeasurementValueDao
;
143 protected void setDescriptionElementDao(IDescriptionElementDao descriptionElementDao
) {
144 this.descriptionElementDao
= descriptionElementDao
;
148 protected void setNaturalLanguageGenerator(NaturalLanguageGenerator naturalLanguageGenerator
) {
149 this.naturalLanguageGenerator
= naturalLanguageGenerator
;
153 protected void setTaxonDao(ITaxonDao taxonDao
) {
154 this.taxonDao
= taxonDao
;
158 protected void setTaxonNodeDao(ITaxonNodeDao taxonNodeDao
) {
159 this.taxonNodeDao
= taxonNodeDao
;
163 protected void setDataSetDao(IDescriptiveDataSetDao dataSetDao
) {
164 this.dataSetDao
= dataSetDao
;
170 public DescriptionServiceImpl() {
171 logger
.debug("Load DescriptionService Bean");
176 @Transactional(readOnly
= false)
177 public void updateTitleCache(Class
<?
extends DescriptionBase
> clazz
, Integer stepSize
, IIdentifiableEntityCacheStrategy
<DescriptionBase
> cacheStrategy
, IProgressMonitor monitor
) {
179 clazz
= DescriptionBase
.class;
181 super.updateTitleCacheImpl(clazz
, stepSize
, cacheStrategy
, monitor
);
186 public TermVocabulary
<Feature
> getDefaultFeatureVocabulary(){
187 String uuidFeature
= "b187d555-f06f-4d65-9e53-da7c93f8eaa8";
188 UUID featureUuid
= UUID
.fromString(uuidFeature
);
189 return vocabularyDao
.findByUuid(featureUuid
);
194 protected void setDao(IDescriptionDao dao
) {
199 public long count(Class
<?
extends DescriptionBase
> type
, Boolean hasImages
, Boolean hasText
,Set
<Feature
> feature
) {
200 return dao
.countDescriptions(type
, hasImages
, hasText
, feature
);
204 public <T
extends DescriptionElementBase
> Pager
<T
> pageDescriptionElements(DescriptionBase description
, Class
<?
extends DescriptionBase
> descriptionType
,
205 Set
<Feature
> features
, Class
<T
> type
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
207 List
<T
> results
= listDescriptionElements(description
, descriptionType
, features
, type
, pageSize
, pageNumber
, propertyPaths
);
208 return new DefaultPagerImpl
<>(pageNumber
, results
.size(), pageSize
, results
);
213 public <T
extends DescriptionElementBase
> Pager
<T
> getDescriptionElements(DescriptionBase description
,
214 Set
<Feature
> features
, Class
<T
> type
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
215 return pageDescriptionElements(description
, null, features
, type
, pageSize
, pageNumber
, propertyPaths
);
221 public <T
extends DescriptionElementBase
> List
<T
> listDescriptionElements(DescriptionBase description
,
222 Class
<?
extends DescriptionBase
> descriptionType
, Set
<Feature
> features
, Class
<T
> type
, Integer pageSize
, Integer pageNumber
,
223 List
<String
> propertyPaths
) {
225 long numberOfResults
= dao
.countDescriptionElements(description
, descriptionType
, features
, type
);
226 List
<T
> results
= new ArrayList
<T
>();
227 if(AbstractPagerImpl
.hasResultsInRange(numberOfResults
, pageNumber
, pageSize
)) {
228 results
= dao
.getDescriptionElements(description
, descriptionType
, features
, type
, pageSize
, pageNumber
, propertyPaths
);
237 public <T
extends DescriptionElementBase
> List
<T
> listDescriptionElements(DescriptionBase description
,
238 Set
<Feature
> features
, Class
<T
> type
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
240 return listDescriptionElements(description
, null, features
, type
, pageSize
, pageNumber
, propertyPaths
);
244 public Pager
<Annotation
> getDescriptionElementAnnotations(DescriptionElementBase annotatedObj
, MarkerType status
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
){
245 long numberOfResults
= descriptionElementDao
.countAnnotations(annotatedObj
, status
);
247 List
<Annotation
> results
= new ArrayList
<>();
248 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
249 results
= descriptionElementDao
.getAnnotations(annotatedObj
, status
, pageSize
, pageNumber
, orderHints
, propertyPaths
);
252 return new DefaultPagerImpl
<>(pageNumber
, numberOfResults
, pageSize
, results
);
257 public Pager
<Media
> getMedia(DescriptionElementBase descriptionElement
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
258 Integer numberOfResults
= descriptionElementDao
.countMedia(descriptionElement
);
260 List
<Media
> results
= new ArrayList
<Media
>();
261 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
262 results
= descriptionElementDao
.getMedia(descriptionElement
, pageSize
, pageNumber
, propertyPaths
);
265 return new DefaultPagerImpl
<Media
>(pageNumber
, numberOfResults
, pageSize
, results
);
269 public Pager
<TaxonDescription
> pageTaxonDescriptions(Taxon taxon
, Set
<DefinedTerm
> scopes
, Set
<NamedArea
> geographicalScope
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
270 Set
<MarkerType
> markerTypes
= null;
271 return pageTaxonDescriptions(taxon
, scopes
, geographicalScope
, markerTypes
, pageSize
, pageNumber
, propertyPaths
);
275 public List
<TaxonDescription
> listTaxonDescriptions(Taxon taxon
, Set
<DefinedTerm
> scopes
, Set
<NamedArea
> geographicalScope
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
276 Set
<MarkerType
> markerTypes
= null;
277 return listTaxonDescriptions(taxon
, scopes
, geographicalScope
, markerTypes
, pageSize
, pageNumber
, propertyPaths
);
281 public Pager
<TaxonDescription
> pageTaxonDescriptions(Taxon taxon
, Set
<DefinedTerm
> scopes
, Set
<NamedArea
> geographicalScope
, Set
<MarkerType
> markerTypes
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
282 long numberOfResults
= dao
.countTaxonDescriptions(taxon
, scopes
, geographicalScope
, markerTypes
);
284 List
<TaxonDescription
> results
= new ArrayList
<>();
285 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
286 results
= dao
.listTaxonDescriptions(taxon
, scopes
, geographicalScope
, markerTypes
, pageSize
, pageNumber
, propertyPaths
);
289 return new DefaultPagerImpl
<>(pageNumber
, numberOfResults
, pageSize
, results
);
293 public List
<TaxonDescription
> listTaxonDescriptions(Taxon taxon
, Set
<DefinedTerm
> scopes
, Set
<NamedArea
> geographicalScope
, Set
<MarkerType
> markerTypes
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
294 List
<TaxonDescription
> results
= dao
.listTaxonDescriptions(taxon
, scopes
, geographicalScope
, markerTypes
, pageSize
, pageNumber
, propertyPaths
);
300 public List
<Media
> listTaxonDescriptionMedia(UUID taxonUuid
, boolean limitToGalleries
, Set
<MarkerType
> markerTypes
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
){
301 return this.dao
.listTaxonDescriptionMedia(taxonUuid
, limitToGalleries
, markerTypes
, pageSize
, pageNumber
, propertyPaths
);
305 public int countTaxonDescriptionMedia(UUID taxonUuid
, boolean limitToGalleries
, Set
<MarkerType
> markerTypes
){
306 return this.dao
.countTaxonDescriptionMedia(taxonUuid
, limitToGalleries
, markerTypes
);
311 public DistributionTree
getOrderedDistributions(
312 Set
<TaxonDescription
> taxonDescriptions
,
313 boolean subAreaPreference
,
314 boolean statusOrderPreference
,
315 Set
<MarkerType
> hiddenAreaMarkerTypes
,
316 Set
<NamedAreaLevel
> omitLevels
, List
<String
> propertyPaths
){
318 List
<Distribution
> distList
= new ArrayList
<Distribution
>();
320 List
<UUID
> uuids
= new ArrayList
<UUID
>();
321 for (TaxonDescription taxonDescription
: taxonDescriptions
) {
322 if (! taxonDescription
.isImageGallery()){ //image galleries should not have descriptions, but better filter fully on DTYPE of description element
323 uuids
.add(taxonDescription
.getUuid());
327 List
<DescriptionBase
> desclist
= dao
.list(uuids
, null, null, null, propertyPaths
);
328 for (DescriptionBase desc
: desclist
) {
329 if (desc
.isInstanceOf(TaxonDescription
.class)){
330 Set
<DescriptionElementBase
> elements
= desc
.getElements();
331 for (DescriptionElementBase element
: elements
) {
332 if (element
.isInstanceOf(Distribution
.class)) {
333 Distribution distribution
= (Distribution
) element
;
334 if(distribution
.getArea() != null){
335 distList
.add(distribution
);
343 // for (TaxonDescription taxonDescription : taxonDescriptions) {
344 // if (logger.isDebugEnabled()){ logger.debug("load taxon description " + taxonDescription.getUuid());}
345 // //TODO why not loading all description via .list ? This may improve performance
346 // taxonDescription = (TaxonDescription) dao.load(taxonDescription.getUuid(), propertyPaths);
347 // Set<DescriptionElementBase> elements = taxonDescription.getElements();
348 // for (DescriptionElementBase element : elements) {
349 // if (element.isInstanceOf(Distribution.class)) {
350 // Distribution distribution = (Distribution) element;
351 // if(distribution.getArea() != null){
352 // distList.add(distribution);
358 if (logger
.isDebugEnabled()){logger
.debug("filter tree for " + distList
.size() + " distributions ...");}
360 // filter distributions
361 Collection
<Distribution
> filteredDistributions
= DescriptionUtility
.filterDistributions(distList
, hiddenAreaMarkerTypes
, true, statusOrderPreference
, false);
363 distList
.addAll(filteredDistributions
);
365 return DescriptionUtility
.orderDistributions(definedTermDao
, omitLevels
, distList
, hiddenAreaMarkerTypes
, null);
370 public Pager
<TaxonNameDescription
> getTaxonNameDescriptions(TaxonName name
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
371 long numberOfResults
= dao
.countTaxonNameDescriptions(name
);
373 List
<TaxonNameDescription
> results
= new ArrayList
<>();
374 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
375 results
= dao
.getTaxonNameDescriptions(name
, pageSize
, pageNumber
,propertyPaths
);
378 return new DefaultPagerImpl
<>(pageNumber
, numberOfResults
, pageSize
, results
);
383 public Pager
<DescriptionBase
> page(Class
<?
extends DescriptionBase
> type
, Boolean hasImages
, Boolean hasText
, Set
<Feature
> feature
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
384 long numberOfResults
= dao
.countDescriptions(type
, hasImages
, hasText
, feature
);
386 @SuppressWarnings("rawtypes")
387 List
<DescriptionBase
> results
= new ArrayList
<>();
388 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
389 results
= dao
.listDescriptions(type
, hasImages
, hasText
, feature
, pageSize
, pageNumber
,orderHints
,propertyPaths
);
392 return new DefaultPagerImpl
<>(pageNumber
, numberOfResults
, pageSize
, results
);
396 * FIXME Candidate for harmonization
397 * Rename: searchByDistribution
400 public Pager
<TaxonDescription
> searchDescriptionByDistribution(Set
<NamedArea
> namedAreas
, PresenceAbsenceTerm presence
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
401 long numberOfResults
= dao
.countDescriptionByDistribution(namedAreas
, presence
);
403 List
<TaxonDescription
> results
= new ArrayList
<>();
404 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
405 results
= dao
.searchDescriptionByDistribution(namedAreas
, presence
, pageSize
, pageNumber
,orderHints
,propertyPaths
);
408 return new DefaultPagerImpl
<>(pageNumber
, numberOfResults
, pageSize
, results
);
412 * FIXME Candidate for harmonization
413 * move: descriptionElementService.search
416 // public Pager<T> searchElements(Class<? extends T> clazz, String queryString, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
417 public <S
extends DescriptionElementBase
> Pager
<S
> searchElements(Class
<S
> clazz
, String queryString
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
418 long numberOfResults
= descriptionElementDao
.count(clazz
, queryString
);
420 List
<S
> results
= new ArrayList
<>();
421 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
422 results
= (List
<S
>)descriptionElementDao
.search(clazz
, queryString
, pageSize
, pageNumber
, orderHints
, propertyPaths
);
425 return new DefaultPagerImpl
<>(pageNumber
, numberOfResults
, pageSize
, results
);
429 * FIXME Candidate for harmonization
430 * descriptionElementService.find
433 public DescriptionElementBase
getDescriptionElementByUuid(UUID uuid
) {
434 return descriptionElementDao
.findByUuid(uuid
);
438 * FIXME Candidate for harmonization
439 * descriptionElementService.load
442 public DescriptionElementBase
loadDescriptionElement(UUID uuid
, List
<String
> propertyPaths
) {
443 return descriptionElementDao
.load(uuid
, propertyPaths
);
447 * FIXME Candidate for harmonization
448 * descriptionElementService.save
451 @Transactional(readOnly
= false)
452 public UUID
saveDescriptionElement(DescriptionElementBase descriptionElement
) {
453 return descriptionElementDao
.save(descriptionElement
).getUuid();
457 * FIXME Candidate for harmonization
458 * descriptionElementService.save
461 @Transactional(readOnly
= false)
462 public Map
<UUID
, DescriptionElementBase
> saveDescriptionElement(Collection
<DescriptionElementBase
> descriptionElements
) {
463 return descriptionElementDao
.saveAll(descriptionElements
);
467 * FIXME Candidate for harmonization
468 * descriptionElementService.delete
471 public UUID
deleteDescriptionElement(DescriptionElementBase descriptionElement
) {
472 return descriptionElementDao
.delete(descriptionElement
);
477 * @see eu.etaxonomy.cdm.api.service.IDescriptionService#deleteDescriptionElement(java.util.UUID)
480 public UUID
deleteDescriptionElement(UUID descriptionElementUuid
) {
481 return deleteDescriptionElement(descriptionElementDao
.load(descriptionElementUuid
));
485 @Transactional(readOnly
= false)
486 public DeleteResult
deleteDescription(DescriptionBase description
) {
487 DeleteResult deleteResult
= new DeleteResult();
488 description
= load(description
.getId(), Arrays
.asList("descriptiveDataSets"));//avoid lazy init exception
490 if (description
instanceof TaxonDescription
){
491 TaxonDescription taxDescription
= HibernateProxyHelper
.deproxy(description
, TaxonDescription
.class);
492 Taxon tax
= taxDescription
.getTaxon();
493 tax
.removeDescription(taxDescription
, true);
494 deleteResult
.addUpdatedObject(tax
);
496 else if (HibernateProxyHelper
.isInstanceOf(description
, SpecimenDescription
.class)){
497 SpecimenDescription specimenDescription
= HibernateProxyHelper
.deproxy(description
, SpecimenDescription
.class);
498 SpecimenOrObservationBase specimen
= specimenDescription
.getDescribedSpecimenOrObservation();
499 specimen
.removeDescription(specimenDescription
);
500 deleteResult
.addUpdatedObject(specimen
);
503 Set
<DescriptiveDataSet
> descriptiveDataSets
= description
.getDescriptiveDataSets();
504 for (Iterator
<DescriptiveDataSet
> iterator
= descriptiveDataSets
.iterator(); iterator
.hasNext();) {
505 iterator
.next().removeDescription(description
);
508 dao
.delete(description
);
509 deleteResult
.addDeletedObject(description
);
510 deleteResult
.setCdmEntity(description
);
518 * @see eu.etaxonomy.cdm.api.service.IDescriptionService#deleteDescription(java.util.UUID)
521 @Transactional(readOnly
= false)
522 public DeleteResult
deleteDescription(UUID descriptionUuid
) {
523 return deleteDescription(dao
.load(descriptionUuid
));
528 public TermVocabulary
<Feature
> getFeatureVocabulary(UUID uuid
) {
529 return vocabularyDao
.findByUuid(uuid
);
534 public <T
extends DescriptionElementBase
> List
<T
> getDescriptionElementsForTaxon(
535 Taxon taxon
, Set
<Feature
> features
,
536 Class
<T
> type
, Integer pageSize
,
537 Integer pageNumber
, List
<String
> propertyPaths
) {
538 return listDescriptionElementsForTaxon(taxon
, features
, type
, pageSize
, pageNumber
, propertyPaths
);
542 public <T
extends DescriptionElementBase
> List
<T
> listDescriptionElementsForTaxon(
543 Taxon taxon
, Set
<Feature
> features
,
544 Class
<T
> type
, Integer pageSize
,
545 Integer pageNumber
, List
<String
> propertyPaths
) {
546 return dao
.getDescriptionElementForTaxon(taxon
.getUuid(), features
, type
, pageSize
, pageNumber
, propertyPaths
);
550 public <T
extends DescriptionElementBase
> Pager
<T
> pageDescriptionElementsForTaxon(
551 Taxon taxon
, Set
<Feature
> features
,
552 Class
<T
> type
, Integer pageSize
,
553 Integer pageNumber
, List
<String
> propertyPaths
) {
554 if (logger
.isDebugEnabled()){logger
.debug(" get count ...");}
555 Long count
= dao
.countDescriptionElementForTaxon(taxon
.getUuid(), features
, type
);
556 List
<T
> descriptionElements
;
557 if(AbstractPagerImpl
.hasResultsInRange(count
, pageNumber
, pageSize
)){ // no point checking again
558 if (logger
.isDebugEnabled()){logger
.debug(" get list ...");}
559 descriptionElements
= listDescriptionElementsForTaxon(taxon
, features
, type
, pageSize
, pageNumber
, propertyPaths
);
561 descriptionElements
= new ArrayList
<T
>(0);
563 if (logger
.isDebugEnabled()){logger
.debug(" service - DONE ...");}
564 return new DefaultPagerImpl
<T
>(pageNumber
, count
, pageSize
, descriptionElements
);
569 * @see eu.etaxonomy.cdm.api.service.IDescriptionService#generateNaturalLanguageDescription(eu.etaxonomy.cdm.model.description.FeatureTree, eu.etaxonomy.cdm.model.description.TaxonDescription, eu.etaxonomy.cdm.model.common.Language, java.util.List)
572 public String
generateNaturalLanguageDescription(FeatureTree featureTree
,
573 TaxonDescription description
, List
<Language
> preferredLanguages
, String separator
) {
575 Language lang
= preferredLanguages
.size() > 0 ? preferredLanguages
.get(0) : Language
.DEFAULT();
577 description
= (TaxonDescription
)load(description
.getUuid());
578 featureTree
= featureTreeDao
.load(featureTree
.getUuid());
580 StringBuilder naturalLanguageDescription
= new StringBuilder();
582 MarkerType useMarkerType
= (MarkerType
) definedTermDao
.load(UUID
.fromString("2e6e42d9-e92a-41f4-899b-03c0ac64f039"));
583 boolean isUseDescription
= false;
584 if(!description
.getMarkers().isEmpty()) {
585 for (Marker marker
: description
.getMarkers()) {
586 MarkerType markerType
= marker
.getMarkerType();
587 if (markerType
.equals(useMarkerType
)) {
588 isUseDescription
= true;
594 if(description
.hasStructuredData() && !isUseDescription
){
597 String lastCategory
= null;
598 String categorySeparator
= ". ";
600 List
<TextData
> textDataList
;
601 TextData naturalLanguageDescriptionText
= null;
603 boolean useMicroFormatQuantitativeDescriptionBuilder
= false;
605 if(useMicroFormatQuantitativeDescriptionBuilder
){
607 MicroFormatQuantitativeDescriptionBuilder micro
= new MicroFormatQuantitativeDescriptionBuilder();
608 naturalLanguageGenerator
.setQuantitativeDescriptionBuilder(micro
);
609 naturalLanguageDescriptionText
= naturalLanguageGenerator
.generateSingleTextData(featureTree
, (description
), lang
);
613 naturalLanguageDescriptionText
= naturalLanguageGenerator
.generateSingleTextData(
619 return naturalLanguageDescriptionText
.getText(lang
);
622 // boolean doItBetter = false;
624 // for (TextData textData : textDataList.toArray(new TextData[textDataList.size()])){
625 // if(textData.getMultilanguageText().size() > 0){
627 // if (!textData.getFeature().equals(Feature.UNKNOWN())) {
628 // String featureLabel = textData.getFeature().getLabel(lang);
633 // * The code lines below are desinged to handle
634 // * a special case where as the feature label contains
635 // * hierarchical information on the features. This code
636 // * exist only as a base for discussion, and is not
637 // * intendet to be used in production.
639 // featureLabel = StringUtils.remove(featureLabel, '>');
641 // String[] labelTokens = StringUtils.split(featureLabel, '<');
642 // if(labelTokens[0].equals(lastCategory) && labelTokens.length > 1){
643 // if(naturalLanguageDescription.length() > 0){
644 // naturalLanguageDescription.append(separator);
646 // naturalLanguageDescription.append(labelTokens[1]);
648 // if(naturalLanguageDescription.length() > 0){
649 // naturalLanguageDescription.append(categorySeparator);
651 // naturalLanguageDescription.append(StringUtils.join(labelTokens));
653 // lastCategory = labelTokens[0];
654 // // end of demo code
656 // if(naturalLanguageDescription.length() > 0){
657 // naturalLanguageDescription.append(separator);
659 // naturalLanguageDescription.append(textData.getFeature().getLabel(lang));
662 // if(naturalLanguageDescription.length() > 0){
663 // naturalLanguageDescription.append(separator);
666 // String text = textData.getMultilanguageText().values().iterator().next().getText();
667 // naturalLanguageDescription.append(text);
673 else if (isUseDescription
) {
674 //AT: Left Blank in case we need to generate a Natural language text string.
676 return naturalLanguageDescription
.toString();
681 public boolean hasStructuredData(DescriptionBase
<?
> description
) {
682 return load(description
.getUuid()).hasStructuredData();
687 @Transactional(readOnly
= false)
688 public UpdateResult
moveDescriptionElementsToDescription(
689 Collection
<DescriptionElementBase
> descriptionElements
,
690 DescriptionBase targetDescription
,
693 UpdateResult result
= new UpdateResult();
694 if (descriptionElements
.isEmpty() ){
698 if (! isCopy
&& descriptionElements
== descriptionElements
.iterator().next().getInDescription().getElements()){
699 //if the descriptionElements collection is the elements set of a description, put it in a separate set before to avoid concurrent modification exceptions
700 descriptionElements
= new HashSet
<DescriptionElementBase
>(descriptionElements
);
701 // descriptionElementsTmp.addAll(descriptionElements);
702 // descriptionElements = descriptionElementsTmp;
704 for (DescriptionElementBase element
: descriptionElements
){
705 DescriptionBase
<?
> description
= element
.getInDescription();
707 DescriptionElementBase newElement
= (DescriptionElementBase
)element
.clone();
708 targetDescription
.addElement(newElement
);
709 } catch (CloneNotSupportedException e
) {
710 throw new RuntimeException ("Clone not yet implemented for class " + element
.getClass().getName(), e
);
713 description
.removeElement(element
);
714 if (description
.getElements().isEmpty()){
715 if (description
instanceof TaxonDescription
){
716 TaxonDescription taxDescription
= HibernateProxyHelper
.deproxy(description
, TaxonDescription
.class);
717 if (taxDescription
.getTaxon() != null){
718 taxDescription
.getTaxon().removeDescription((TaxonDescription
)description
);
721 dao
.delete(description
);
724 dao
.saveOrUpdate(description
);
725 result
.addUpdatedObject(description
);
731 dao
.saveOrUpdate(targetDescription
);
732 result
.addUpdatedObject(targetDescription
);
733 if (targetDescription
instanceof TaxonDescription
){
734 result
.addUpdatedObject(((TaxonDescription
)targetDescription
).getTaxon());
740 @Transactional(readOnly
= false)
741 public UpdateResult
moveDescriptionElementsToDescription(
742 Set
<UUID
> descriptionElementUUIDs
,
743 UUID targetDescriptionUuid
,
745 Set
<DescriptionElementBase
> descriptionElements
= new HashSet
<DescriptionElementBase
>();
746 for(UUID deUuid
: descriptionElementUUIDs
) {
747 descriptionElements
.add(descriptionElementDao
.load(deUuid
));
749 DescriptionBase targetDescription
= dao
.load(targetDescriptionUuid
);
751 return moveDescriptionElementsToDescription(descriptionElements
, targetDescription
, isCopy
);
755 @Transactional(readOnly
= false)
756 public UpdateResult
moveDescriptionElementsToDescription(
757 Set
<UUID
> descriptionElementUUIDs
,
758 UUID targetTaxonUuid
,
761 Taxon targetTaxon
= CdmBase
.deproxy(taxonDao
.load(targetTaxonUuid
), Taxon
.class);
762 DescriptionBase targetDescription
= TaxonDescription
.NewInstance(targetTaxon
);
763 targetDescription
.setTitleCache(moveMessage
, true);
764 Annotation annotation
= Annotation
.NewInstance(moveMessage
, Language
.getDefaultLanguage());
765 annotation
.setAnnotationType(AnnotationType
.TECHNICAL());
766 targetDescription
.addAnnotation(annotation
);
768 targetDescription
= dao
.save(targetDescription
);
769 Set
<DescriptionElementBase
> descriptionElements
= new HashSet
<DescriptionElementBase
>();
770 for(UUID deUuid
: descriptionElementUUIDs
) {
771 descriptionElements
.add(descriptionElementDao
.load(deUuid
));
774 return moveDescriptionElementsToDescription(descriptionElements
, targetDescription
, isCopy
);
778 public Pager
<TermDto
> pageNamedAreasInUse(boolean includeAllParents
, Integer pageSize
,
780 List
<TermDto
> results
= dao
.listNamedAreasInUse(includeAllParents
, null, null);
781 int startIndex
= pageNumber
* pageSize
;
782 int toIndex
= Math
.min(startIndex
+ pageSize
, results
.size());
783 List
<TermDto
> page
= results
.subList(startIndex
, toIndex
);
784 return new DefaultPagerImpl
<TermDto
>(pageNumber
, results
.size(), pageSize
, page
);
789 @Transactional(readOnly
= false)
790 public UpdateResult
moveTaxonDescriptions(Taxon sourceTaxon
, Taxon targetTaxon
) {
791 List
<TaxonDescription
> descriptions
= new ArrayList(sourceTaxon
.getDescriptions());
792 UpdateResult result
= new UpdateResult();
793 result
.addUpdatedObject(sourceTaxon
);
794 result
.addUpdatedObject(targetTaxon
);
795 for(TaxonDescription description
: descriptions
){
797 String moveMessage
= String
.format("Description moved from %s", sourceTaxon
);
798 if(description
.isProtectedTitleCache()){
799 String separator
= "";
800 if(!StringUtils
.isBlank(description
.getTitleCache())){
803 description
.setTitleCache(description
.getTitleCache() + separator
+ moveMessage
, true);
805 Annotation annotation
= Annotation
.NewInstance(moveMessage
, Language
.getDefaultLanguage());
806 annotation
.setAnnotationType(AnnotationType
.TECHNICAL());
807 description
.addAnnotation(annotation
);
808 targetTaxon
.addDescription(description
);
814 @Transactional(readOnly
= false)
815 public UpdateResult
moveTaxonDescriptions(UUID sourceTaxonUuid
, UUID targetTaxonUuid
) {
816 Taxon sourceTaxon
= HibernateProxyHelper
.deproxy(taxonDao
.load(sourceTaxonUuid
), Taxon
.class);
817 Taxon targetTaxon
= HibernateProxyHelper
.deproxy(taxonDao
.load(targetTaxonUuid
), Taxon
.class);
818 return moveTaxonDescriptions(sourceTaxon
, targetTaxon
);
823 @Transactional(readOnly
= false)
824 public UpdateResult
moveTaxonDescription(UUID descriptionUuid
, UUID targetTaxonUuid
){
825 UpdateResult result
= new UpdateResult();
826 TaxonDescription description
= HibernateProxyHelper
.deproxy(dao
.load(descriptionUuid
), TaxonDescription
.class);
828 Taxon sourceTaxon
= description
.getTaxon();
829 String moveMessage
= String
.format("Description moved from %s", sourceTaxon
);
830 if(description
.isProtectedTitleCache()){
831 String separator
= "";
832 if(!StringUtils
.isBlank(description
.getTitleCache())){
835 description
.setTitleCache(description
.getTitleCache() + separator
+ moveMessage
, true);
837 Annotation annotation
= Annotation
.NewInstance(moveMessage
, Language
.getDefaultLanguage());
838 annotation
.setAnnotationType(AnnotationType
.TECHNICAL());
839 description
.addAnnotation(annotation
);
840 Taxon targetTaxon
= HibernateProxyHelper
.deproxy(taxonDao
.load(targetTaxonUuid
), Taxon
.class);
841 targetTaxon
.addDescription(description
);
842 result
.addUpdatedObject(targetTaxon
);
843 result
.addUpdatedObject(sourceTaxon
);
844 // dao.merge(description);