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
.util
.ArrayList
;
14 import java
.util
.Collection
;
15 import java
.util
.HashSet
;
16 import java
.util
.List
;
19 import java
.util
.UUID
;
21 import org
.apache
.commons
.lang
.StringUtils
;
22 import org
.apache
.log4j
.Logger
;
23 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
24 import org
.springframework
.stereotype
.Service
;
25 import org
.springframework
.transaction
.annotation
.Transactional
;
27 import eu
.etaxonomy
.cdm
.api
.service
.pager
.Pager
;
28 import eu
.etaxonomy
.cdm
.api
.service
.pager
.impl
.AbstractPagerImpl
;
29 import eu
.etaxonomy
.cdm
.api
.service
.pager
.impl
.DefaultPagerImpl
;
30 import eu
.etaxonomy
.cdm
.api
.utility
.DescriptionUtility
;
31 import eu
.etaxonomy
.cdm
.common
.monitor
.IProgressMonitor
;
32 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
33 import eu
.etaxonomy
.cdm
.model
.common
.Annotation
;
34 import eu
.etaxonomy
.cdm
.model
.common
.AnnotationType
;
35 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTerm
;
36 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
37 import eu
.etaxonomy
.cdm
.model
.common
.Marker
;
38 import eu
.etaxonomy
.cdm
.model
.common
.MarkerType
;
39 import eu
.etaxonomy
.cdm
.model
.common
.TermVocabulary
;
40 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
41 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
42 import eu
.etaxonomy
.cdm
.model
.description
.Distribution
;
43 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
44 import eu
.etaxonomy
.cdm
.model
.description
.FeatureTree
;
45 import eu
.etaxonomy
.cdm
.model
.description
.PresenceAbsenceTerm
;
46 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
47 import eu
.etaxonomy
.cdm
.model
.description
.TaxonNameDescription
;
48 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
49 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
50 import eu
.etaxonomy
.cdm
.model
.location
.NamedAreaLevel
;
51 import eu
.etaxonomy
.cdm
.model
.media
.Media
;
52 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
53 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
54 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.IDefinedTermDao
;
55 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.ITermVocabularyDao
;
56 import eu
.etaxonomy
.cdm
.persistence
.dao
.description
.IDescriptionDao
;
57 import eu
.etaxonomy
.cdm
.persistence
.dao
.description
.IDescriptionElementDao
;
58 import eu
.etaxonomy
.cdm
.persistence
.dao
.description
.IFeatureDao
;
59 import eu
.etaxonomy
.cdm
.persistence
.dao
.description
.IFeatureNodeDao
;
60 import eu
.etaxonomy
.cdm
.persistence
.dao
.description
.IFeatureTreeDao
;
61 import eu
.etaxonomy
.cdm
.persistence
.dao
.description
.IStatisticalMeasurementValueDao
;
62 import eu
.etaxonomy
.cdm
.persistence
.dao
.taxon
.ITaxonDao
;
63 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
;
64 import eu
.etaxonomy
.cdm
.strategy
.cache
.common
.IIdentifiableEntityCacheStrategy
;
72 * @author a.kohlbecker
77 @Transactional(readOnly
= true)
78 public class DescriptionServiceImpl
extends IdentifiableServiceBase
<DescriptionBase
,IDescriptionDao
> implements IDescriptionService
{
80 private static final Logger logger
= Logger
.getLogger(DescriptionServiceImpl
.class);
82 protected IDescriptionElementDao descriptionElementDao
;
83 protected IFeatureTreeDao featureTreeDao
;
84 protected IFeatureNodeDao featureNodeDao
;
85 protected IFeatureDao featureDao
;
86 protected ITermVocabularyDao vocabularyDao
;
87 protected IDefinedTermDao definedTermDao
;
88 protected IStatisticalMeasurementValueDao statisticalMeasurementValueDao
;
89 protected ITaxonDao taxonDao
;
91 //TODO change to Interface
92 private NaturalLanguageGenerator naturalLanguageGenerator
;
95 protected void setFeatureTreeDao(IFeatureTreeDao featureTreeDao
) {
96 this.featureTreeDao
= featureTreeDao
;
100 protected void setFeatureNodeDao(IFeatureNodeDao featureNodeDao
) {
101 this.featureNodeDao
= featureNodeDao
;
105 protected void setFeatureDao(IFeatureDao featureDao
) {
106 this.featureDao
= featureDao
;
110 protected void setVocabularyDao(ITermVocabularyDao vocabularyDao
) {
111 this.vocabularyDao
= vocabularyDao
;
115 protected void setDefinedTermDao(IDefinedTermDao definedTermDao
) {
116 this.definedTermDao
= definedTermDao
;
120 protected void statisticalMeasurementValueDao(IStatisticalMeasurementValueDao statisticalMeasurementValueDao
) {
121 this.statisticalMeasurementValueDao
= statisticalMeasurementValueDao
;
125 protected void setDescriptionElementDao(IDescriptionElementDao descriptionElementDao
) {
126 this.descriptionElementDao
= descriptionElementDao
;
130 protected void setNaturalLanguageGenerator(NaturalLanguageGenerator naturalLanguageGenerator
) {
131 this.naturalLanguageGenerator
= naturalLanguageGenerator
;
135 protected void setTaxonDao(ITaxonDao taxonDao
) {
136 this.taxonDao
= taxonDao
;
142 public DescriptionServiceImpl() {
143 logger
.debug("Load DescriptionService Bean");
149 * @see eu.etaxonomy.cdm.api.service.IIdentifiableEntityService#updateTitleCache(java.lang.Integer, eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy)
152 @Transactional(readOnly
= false)
153 public void updateTitleCache(Class
<?
extends DescriptionBase
> clazz
, Integer stepSize
, IIdentifiableEntityCacheStrategy
<DescriptionBase
> cacheStrategy
, IProgressMonitor monitor
) {
155 clazz
= DescriptionBase
.class;
157 super.updateTitleCacheImpl(clazz
, stepSize
, cacheStrategy
, monitor
);
162 public TermVocabulary
<Feature
> getDefaultFeatureVocabulary(){
163 String uuidFeature
= "b187d555-f06f-4d65-9e53-da7c93f8eaa8";
164 UUID featureUuid
= UUID
.fromString(uuidFeature
);
165 return vocabularyDao
.findByUuid(featureUuid
);
170 protected void setDao(IDescriptionDao dao
) {
175 public int count(Class
<?
extends DescriptionBase
> type
, Boolean hasImages
, Boolean hasText
,Set
<Feature
> feature
) {
176 return dao
.countDescriptions(type
, hasImages
, hasText
, feature
);
180 public Pager
<DescriptionElementBase
> pageDescriptionElements(DescriptionBase description
, Class
<?
extends DescriptionBase
> descriptionType
,
181 Set
<Feature
> features
, Class
<?
extends DescriptionElementBase
> type
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
183 List
<DescriptionElementBase
> results
= listDescriptionElements(description
, descriptionType
, features
, type
, pageSize
, pageNumber
, propertyPaths
);
184 return new DefaultPagerImpl
<DescriptionElementBase
>(pageNumber
, results
.size(), pageSize
, results
);
188 * @see eu.etaxonomy.cdm.api.service.IDescriptionService#getDescriptionElements(eu.etaxonomy.cdm.model.description.DescriptionBase, java.util.Set, java.lang.Class, java.lang.Integer, java.lang.Integer, java.util.List)
192 public Pager
<DescriptionElementBase
> getDescriptionElements(DescriptionBase description
,
193 Set
<Feature
> features
, Class
<?
extends DescriptionElementBase
> type
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
194 return pageDescriptionElements(description
, null, features
, type
, pageSize
, pageNumber
, propertyPaths
);
198 public List
<DescriptionElementBase
> listDescriptionElements(DescriptionBase description
, Class
<?
extends DescriptionBase
> descriptionType
,
199 Set
<Feature
> features
, Class
<?
extends DescriptionElementBase
> type
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
201 Integer numberOfResults
= dao
.countDescriptionElements(description
, descriptionType
, features
, type
);
202 List
<DescriptionElementBase
> results
= new ArrayList
<DescriptionElementBase
>();
203 if(AbstractPagerImpl
.hasResultsInRange(numberOfResults
.longValue(), pageNumber
, pageSize
)) {
204 results
= dao
.getDescriptionElements(description
, descriptionType
, features
, type
, pageSize
, pageNumber
, propertyPaths
);
210 * @see eu.etaxonomy.cdm.api.service.IDescriptionService#listDescriptionElements(eu.etaxonomy.cdm.model.description.DescriptionBase, java.util.Set, java.lang.Class, java.lang.Integer, java.lang.Integer, java.util.List)
214 public List
<DescriptionElementBase
> listDescriptionElements(DescriptionBase description
,
215 Set
<Feature
> features
, Class
<?
extends DescriptionElementBase
> type
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
217 return listDescriptionElements(description
, null, features
, type
, pageSize
, pageNumber
, propertyPaths
);
221 public Pager
<Annotation
> getDescriptionElementAnnotations(DescriptionElementBase annotatedObj
, MarkerType status
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
){
222 Integer numberOfResults
= descriptionElementDao
.countAnnotations(annotatedObj
, status
);
224 List
<Annotation
> results
= new ArrayList
<Annotation
>();
225 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
226 results
= descriptionElementDao
.getAnnotations(annotatedObj
, status
, pageSize
, pageNumber
, orderHints
, propertyPaths
);
229 return new DefaultPagerImpl
<Annotation
>(pageNumber
, numberOfResults
, pageSize
, results
);
235 public Pager
<Media
> getMedia(DescriptionElementBase descriptionElement
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
236 Integer numberOfResults
= descriptionElementDao
.countMedia(descriptionElement
);
238 List
<Media
> results
= new ArrayList
<Media
>();
239 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
240 results
= descriptionElementDao
.getMedia(descriptionElement
, pageSize
, pageNumber
, propertyPaths
);
243 return new DefaultPagerImpl
<Media
>(pageNumber
, numberOfResults
, pageSize
, results
);
247 public Pager
<TaxonDescription
> pageTaxonDescriptions(Taxon taxon
, Set
<DefinedTerm
> scopes
, Set
<NamedArea
> geographicalScope
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
248 Set
<MarkerType
> markerTypes
= null;
249 return pageTaxonDescriptions(taxon
, scopes
, geographicalScope
, markerTypes
, pageSize
, pageNumber
, propertyPaths
);
253 public List
<TaxonDescription
> listTaxonDescriptions(Taxon taxon
, Set
<DefinedTerm
> scopes
, Set
<NamedArea
> geographicalScope
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
254 Set
<MarkerType
> markerTypes
= null;
255 return listTaxonDescriptions(taxon
, scopes
, geographicalScope
, markerTypes
, pageSize
, pageNumber
, propertyPaths
);
260 * @see eu.etaxonomy.cdm.api.service.IDescriptionService#pageMarkedTaxonDescriptions(eu.etaxonomy.cdm.model.taxon.Taxon, java.util.Set, java.util.Set, java.util.Set, java.lang.Integer, java.lang.Integer, java.util.List)
263 public Pager
<TaxonDescription
> pageTaxonDescriptions(Taxon taxon
, Set
<DefinedTerm
> scopes
, Set
<NamedArea
> geographicalScope
, Set
<MarkerType
> markerTypes
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
264 Integer numberOfResults
= dao
.countTaxonDescriptions(taxon
, scopes
, geographicalScope
, markerTypes
);
266 List
<TaxonDescription
> results
= new ArrayList
<TaxonDescription
>();
267 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
268 results
= dao
.listTaxonDescriptions(taxon
, scopes
, geographicalScope
, markerTypes
, pageSize
, pageNumber
, propertyPaths
);
271 return new DefaultPagerImpl
<TaxonDescription
>(pageNumber
, numberOfResults
, pageSize
, results
);
275 public List
<TaxonDescription
> listTaxonDescriptions(Taxon taxon
, Set
<DefinedTerm
> scopes
, Set
<NamedArea
> geographicalScope
, Set
<MarkerType
> markerTypes
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
276 List
<TaxonDescription
> results
= dao
.listTaxonDescriptions(taxon
, scopes
, geographicalScope
, markerTypes
, pageSize
, pageNumber
, propertyPaths
);
282 * @see eu.etaxonomy.cdm.api.service.IDescriptionService#listTaxonDescriptionMedia(UUID, boolean, Set, Integer, Integer, List)
285 public List
<Media
> listTaxonDescriptionMedia(UUID taxonUuid
, boolean limitToGalleries
, Set
<MarkerType
> markerTypes
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
){
286 return this.dao
.listTaxonDescriptionMedia(taxonUuid
, limitToGalleries
, markerTypes
, pageSize
, pageNumber
, propertyPaths
);
290 * @see IDescriptionService#countTaxonDescriptionMedia(UUID, boolean, Set)
293 public int countTaxonDescriptionMedia(UUID taxonUuid
, boolean limitToGalleries
, Set
<MarkerType
> markerTypes
){
294 return this.dao
.countTaxonDescriptionMedia(taxonUuid
, limitToGalleries
, markerTypes
);
299 * @see eu.etaxonomy.cdm.api.service.IDescriptionService#getOrderedDistributions(java.util.Set, boolean, boolean, java.util.Set, java.util.List)
303 public DistributionTree
getOrderedDistributions(
304 Set
<TaxonDescription
> taxonDescriptions
,
305 boolean subAreaPreference
,
306 boolean statusOrderPreference
,
307 Set
<MarkerType
> hideMarkedAreas
,
308 Set
<NamedAreaLevel
> omitLevels
, List
<String
> propertyPaths
){
310 List
<Distribution
> distList
= new ArrayList
<Distribution
>();
312 List
<UUID
> uuids
= new ArrayList
<UUID
>();
313 for (TaxonDescription taxonDescription
: taxonDescriptions
) {
314 if (! taxonDescription
.isImageGallery()){ //image galleries should not have descriptions, but better filter fully on DTYPE of description element
315 uuids
.add(taxonDescription
.getUuid());
319 List
<DescriptionBase
> desclist
= dao
.list(uuids
, null, null, null, propertyPaths
);
320 for (DescriptionBase desc
: desclist
) {
321 if (desc
.isInstanceOf(TaxonDescription
.class)){
322 Set
<DescriptionElementBase
> elements
= desc
.getElements();
323 for (DescriptionElementBase element
: elements
) {
324 if (element
.isInstanceOf(Distribution
.class)) {
325 Distribution distribution
= (Distribution
) element
;
326 if(distribution
.getArea() != null){
327 distList
.add(distribution
);
335 // for (TaxonDescription taxonDescription : taxonDescriptions) {
336 // if (logger.isDebugEnabled()){ logger.debug("load taxon description " + taxonDescription.getUuid());}
337 // //TODO why not loading all description via .list ? This may improve performance
338 // taxonDescription = (TaxonDescription) dao.load(taxonDescription.getUuid(), propertyPaths);
339 // Set<DescriptionElementBase> elements = taxonDescription.getElements();
340 // for (DescriptionElementBase element : elements) {
341 // if (element.isInstanceOf(Distribution.class)) {
342 // Distribution distribution = (Distribution) element;
343 // if(distribution.getArea() != null){
344 // distList.add(distribution);
350 if (logger
.isDebugEnabled()){logger
.debug("filter tree for " + distList
.size() + " distributions ...");}
352 // filter distributions
353 Collection
<Distribution
> filteredDistributions
= DescriptionUtility
.filterDistributions(distList
, subAreaPreference
, statusOrderPreference
, hideMarkedAreas
);
355 distList
.addAll(filteredDistributions
);
357 return DescriptionUtility
.orderDistributions(omitLevels
, distList
);
362 public Pager
<TaxonNameDescription
> getTaxonNameDescriptions(TaxonNameBase name
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
363 Integer numberOfResults
= dao
.countTaxonNameDescriptions(name
);
365 List
<TaxonNameDescription
> results
= new ArrayList
<TaxonNameDescription
>();
366 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
367 results
= dao
.getTaxonNameDescriptions(name
, pageSize
, pageNumber
,propertyPaths
);
370 return new DefaultPagerImpl
<TaxonNameDescription
>(pageNumber
, numberOfResults
, pageSize
, results
);
375 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
) {
376 Integer numberOfResults
= dao
.countDescriptions(type
, hasImages
, hasText
, feature
);
378 List
<DescriptionBase
> results
= new ArrayList
<DescriptionBase
>();
379 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
380 results
= dao
.listDescriptions(type
, hasImages
, hasText
, feature
, pageSize
, pageNumber
,orderHints
,propertyPaths
);
383 return new DefaultPagerImpl
<DescriptionBase
>(pageNumber
, numberOfResults
, pageSize
, results
);
387 * FIXME Candidate for harmonization
388 * Rename: searchByDistribution
391 public Pager
<TaxonDescription
> searchDescriptionByDistribution(Set
<NamedArea
> namedAreas
, PresenceAbsenceTerm presence
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
392 Integer numberOfResults
= dao
.countDescriptionByDistribution(namedAreas
, presence
);
394 List
<TaxonDescription
> results
= new ArrayList
<TaxonDescription
>();
395 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
396 results
= dao
.searchDescriptionByDistribution(namedAreas
, presence
, pageSize
, pageNumber
,orderHints
,propertyPaths
);
399 return new DefaultPagerImpl
<TaxonDescription
>(pageNumber
, numberOfResults
, pageSize
, results
);
403 * FIXME Candidate for harmonization
404 * move: descriptionElementService.search
407 public Pager
<DescriptionElementBase
> searchElements(Class
<?
extends DescriptionElementBase
> clazz
, String queryString
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
408 Integer numberOfResults
= descriptionElementDao
.count(clazz
, queryString
);
410 List
<DescriptionElementBase
> results
= new ArrayList
<DescriptionElementBase
>();
411 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
412 results
= descriptionElementDao
.search(clazz
, queryString
, pageSize
, pageNumber
, orderHints
, propertyPaths
);
415 return new DefaultPagerImpl
<DescriptionElementBase
>(pageNumber
, numberOfResults
, pageSize
, results
);
419 * FIXME Candidate for harmonization
420 * descriptionElementService.find
423 public DescriptionElementBase
getDescriptionElementByUuid(UUID uuid
) {
424 return descriptionElementDao
.findByUuid(uuid
);
428 * FIXME Candidate for harmonization
429 * descriptionElementService.load
432 public DescriptionElementBase
loadDescriptionElement(UUID uuid
, List
<String
> propertyPaths
) {
433 return descriptionElementDao
.load(uuid
, propertyPaths
);
437 * FIXME Candidate for harmonization
438 * descriptionElementService.save
441 @Transactional(readOnly
= false)
442 public UUID
saveDescriptionElement(DescriptionElementBase descriptionElement
) {
443 return descriptionElementDao
.save(descriptionElement
);
447 * FIXME Candidate for harmonization
448 * descriptionElementService.save
451 @Transactional(readOnly
= false)
452 public Map
<UUID
, DescriptionElementBase
> saveDescriptionElement(Collection
<DescriptionElementBase
> descriptionElements
) {
453 return descriptionElementDao
.saveAll(descriptionElements
);
457 * FIXME Candidate for harmonization
458 * descriptionElementService.delete
461 public UUID
deleteDescriptionElement(DescriptionElementBase descriptionElement
) {
462 return descriptionElementDao
.delete(descriptionElement
);
467 * @see eu.etaxonomy.cdm.api.service.IDescriptionService#deleteDescriptionElement(java.util.UUID)
470 public UUID
deleteDescriptionElement(UUID descriptionElementUuid
) {
471 return deleteDescriptionElement(descriptionElementDao
.load(descriptionElementUuid
));
475 @Transactional(readOnly
= false)
476 public DeleteResult
deleteDescription(DescriptionBase description
) {
477 DeleteResult deleteResult
= new DeleteResult();
479 if (description
instanceof TaxonDescription
){
480 TaxonDescription taxDescription
= HibernateProxyHelper
.deproxy(description
, TaxonDescription
.class);
481 Taxon tax
= taxDescription
.getTaxon();
482 tax
.removeDescription(taxDescription
, true);
484 deleteResult
.addUpdatedObject(tax
);
485 deleteResult
.setCdmEntity(tax
);
493 * @see eu.etaxonomy.cdm.api.service.IDescriptionService#deleteDescription(java.util.UUID)
496 @Transactional(readOnly
= false)
497 public DeleteResult
deleteDescription(UUID descriptionUuid
) {
498 return deleteDescription(dao
.load(descriptionUuid
));
503 public TermVocabulary
<Feature
> getFeatureVocabulary(UUID uuid
) {
504 return vocabularyDao
.findByUuid(uuid
);
509 public <T
extends DescriptionElementBase
> List
<T
> getDescriptionElementsForTaxon(
510 Taxon taxon
, Set
<Feature
> features
,
511 Class
<T
> type
, Integer pageSize
,
512 Integer pageNumber
, List
<String
> propertyPaths
) {
513 return listDescriptionElementsForTaxon(taxon
, features
, type
, pageSize
, pageNumber
, propertyPaths
);
517 public <T
extends DescriptionElementBase
> List
<T
> listDescriptionElementsForTaxon(
518 Taxon taxon
, Set
<Feature
> features
,
519 Class
<T
> type
, Integer pageSize
,
520 Integer pageNumber
, List
<String
> propertyPaths
) {
521 return dao
.getDescriptionElementForTaxon(taxon
.getUuid(), features
, type
, pageSize
, pageNumber
, propertyPaths
);
525 public <T
extends DescriptionElementBase
> Pager
<T
> pageDescriptionElementsForTaxon(
526 Taxon taxon
, Set
<Feature
> features
,
527 Class
<T
> type
, Integer pageSize
,
528 Integer pageNumber
, List
<String
> propertyPaths
) {
529 if (logger
.isDebugEnabled()){logger
.debug(" get count ...");}
530 Long count
= dao
.countDescriptionElementForTaxon(taxon
.getUuid(), features
, type
);
531 List
<T
> descriptionElements
;
532 if(AbstractPagerImpl
.hasResultsInRange(count
, pageNumber
, pageSize
)){ // no point checking again
533 if (logger
.isDebugEnabled()){logger
.debug(" get list ...");}
534 descriptionElements
= listDescriptionElementsForTaxon(taxon
, features
, type
, pageSize
, pageNumber
, propertyPaths
);
536 descriptionElements
= new ArrayList
<T
>(0);
538 if (logger
.isDebugEnabled()){logger
.debug(" service - DONE ...");}
539 return new DefaultPagerImpl
<T
>(pageNumber
, count
.intValue(), pageSize
, descriptionElements
);
544 * @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)
547 public String
generateNaturalLanguageDescription(FeatureTree featureTree
,
548 TaxonDescription description
, List
<Language
> preferredLanguages
, String separator
) {
550 Language lang
= preferredLanguages
.size() > 0 ? preferredLanguages
.get(0) : Language
.DEFAULT();
552 description
= (TaxonDescription
)load(description
.getUuid());
553 featureTree
= featureTreeDao
.load(featureTree
.getUuid());
555 StringBuilder naturalLanguageDescription
= new StringBuilder();
557 MarkerType useMarkerType
= (MarkerType
) definedTermDao
.load(UUID
.fromString("2e6e42d9-e92a-41f4-899b-03c0ac64f039"));
558 boolean isUseDescription
= false;
559 if(!description
.getMarkers().isEmpty()) {
560 for (Marker marker
: description
.getMarkers()) {
561 MarkerType markerType
= marker
.getMarkerType();
562 if (markerType
.equals(useMarkerType
)) {
563 isUseDescription
= true;
569 if(description
.hasStructuredData() && !isUseDescription
){
572 String lastCategory
= null;
573 String categorySeparator
= ". ";
575 List
<TextData
> textDataList
;
576 TextData naturalLanguageDescriptionText
= null;
578 boolean useMicroFormatQuantitativeDescriptionBuilder
= false;
580 if(useMicroFormatQuantitativeDescriptionBuilder
){
582 MicroFormatQuantitativeDescriptionBuilder micro
= new MicroFormatQuantitativeDescriptionBuilder();
583 naturalLanguageGenerator
.setQuantitativeDescriptionBuilder(micro
);
584 naturalLanguageDescriptionText
= naturalLanguageGenerator
.generateSingleTextData(featureTree
, (description
), lang
);
588 naturalLanguageDescriptionText
= naturalLanguageGenerator
.generateSingleTextData(
594 return naturalLanguageDescriptionText
.getText(lang
);
597 // boolean doItBetter = false;
599 // for (TextData textData : textDataList.toArray(new TextData[textDataList.size()])){
600 // if(textData.getMultilanguageText().size() > 0){
602 // if (!textData.getFeature().equals(Feature.UNKNOWN())) {
603 // String featureLabel = textData.getFeature().getLabel(lang);
608 // * The code lines below are desinged to handle
609 // * a special case where as the feature label contains
610 // * hierarchical information on the features. This code
611 // * exist only as a base for discussion, and is not
612 // * intendet to be used in production.
614 // featureLabel = StringUtils.remove(featureLabel, '>');
616 // String[] labelTokens = StringUtils.split(featureLabel, '<');
617 // if(labelTokens[0].equals(lastCategory) && labelTokens.length > 1){
618 // if(naturalLanguageDescription.length() > 0){
619 // naturalLanguageDescription.append(separator);
621 // naturalLanguageDescription.append(labelTokens[1]);
623 // if(naturalLanguageDescription.length() > 0){
624 // naturalLanguageDescription.append(categorySeparator);
626 // naturalLanguageDescription.append(StringUtils.join(labelTokens));
628 // lastCategory = labelTokens[0];
629 // // end of demo code
631 // if(naturalLanguageDescription.length() > 0){
632 // naturalLanguageDescription.append(separator);
634 // naturalLanguageDescription.append(textData.getFeature().getLabel(lang));
637 // if(naturalLanguageDescription.length() > 0){
638 // naturalLanguageDescription.append(separator);
641 // String text = textData.getMultilanguageText().values().iterator().next().getText();
642 // naturalLanguageDescription.append(text);
648 else if (isUseDescription
) {
649 //AT: Left Blank in case we need to generate a Natural language text string.
651 return naturalLanguageDescription
.toString();
655 * @see eu.etaxonomy.cdm.api.service.IDescriptionService#hasStructuredData(eu.etaxonomy.cdm.model.description.DescriptionBase)
658 public boolean hasStructuredData(DescriptionBase
<?
> description
) {
659 return load(description
.getUuid()).hasStructuredData();
664 * @see eu.etaxonomy.cdm.api.service.IDescriptionService#moveDescriptionElementsToDescription(java.util.Collection, eu.etaxonomy.cdm.model.description.DescriptionBase, boolean)
667 public void moveDescriptionElementsToDescription(Collection
<DescriptionElementBase
> descriptionElements
,
668 DescriptionBase targetDescription
, boolean isCopy
) {
670 if (descriptionElements
.isEmpty() ){
674 if (! isCopy
&& descriptionElements
== descriptionElements
.iterator().next().getInDescription().getElements()){
675 //if the descriptionElements collection is the elements set of a description, put it in a separate set before to avoid concurrent modification exceptions
676 descriptionElements
= new HashSet
<DescriptionElementBase
>(descriptionElements
);
677 // descriptionElementsTmp.addAll(descriptionElements);
678 // descriptionElements = descriptionElementsTmp;
680 for (DescriptionElementBase element
: descriptionElements
){
681 DescriptionBase description
= element
.getInDescription();
683 DescriptionElementBase newElement
= (DescriptionElementBase
)element
.clone();
684 targetDescription
.addElement(newElement
);
685 } catch (CloneNotSupportedException e
) {
686 new RuntimeException ("Clone not yet implemented for class " + element
.getClass().getName(), e
);
689 description
.removeElement(element
);
696 public Pager
<NamedArea
> pageNamedAreasInUse(Integer pageSize
,
697 Integer pageNumber
, List
<String
> propertyPaths
){
699 List
<NamedArea
> results
= dao
.listNamedAreasInUse(pageSize
, pageNumber
, propertyPaths
);
700 return new DefaultPagerImpl
<NamedArea
>(pageNumber
, results
.size(), pageSize
, results
);
705 @Transactional(readOnly
= false)
706 public UpdateResult
moveTaxonDescriptions(Taxon sourceTaxon
, Taxon targetTaxon
) {
707 List
<TaxonDescription
> descriptions
= new ArrayList(sourceTaxon
.getDescriptions());
708 UpdateResult result
= new UpdateResult();
709 result
.addUpdatedObject(sourceTaxon
);
710 result
.addUpdatedObject(targetTaxon
);
711 for(TaxonDescription description
: descriptions
){
713 String moveMessage
= String
.format("Description moved from %s", sourceTaxon
);
714 if(description
.isProtectedTitleCache()){
715 String separator
= "";
716 if(!StringUtils
.isBlank(description
.getTitleCache())){
719 description
.setTitleCache(description
.getTitleCache() + separator
+ moveMessage
, true);
721 Annotation annotation
= Annotation
.NewInstance(moveMessage
, Language
.getDefaultLanguage());
722 annotation
.setAnnotationType(AnnotationType
.TECHNICAL());
723 description
.addAnnotation(annotation
);
724 targetTaxon
.addDescription(description
);
730 @Transactional(readOnly
= false)
731 public UpdateResult
moveTaxonDescriptions(UUID sourceTaxonUuid
, UUID targetTaxonUuid
) {
732 Taxon sourceTaxon
= HibernateProxyHelper
.deproxy(taxonDao
.load(sourceTaxonUuid
), Taxon
.class);
733 Taxon targetTaxon
= HibernateProxyHelper
.deproxy(taxonDao
.load(targetTaxonUuid
), Taxon
.class);
734 return moveTaxonDescriptions(sourceTaxon
, targetTaxon
);