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
.Collection
;
14 import java
.util
.HashSet
;
15 import java
.util
.List
;
18 import java
.util
.UUID
;
20 import org
.apache
.commons
.lang
.StringUtils
;
21 import org
.apache
.log4j
.Logger
;
22 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
23 import org
.springframework
.stereotype
.Service
;
24 import org
.springframework
.transaction
.annotation
.Transactional
;
26 import eu
.etaxonomy
.cdm
.api
.service
.pager
.Pager
;
27 import eu
.etaxonomy
.cdm
.api
.service
.pager
.impl
.AbstractPagerImpl
;
28 import eu
.etaxonomy
.cdm
.api
.service
.pager
.impl
.DefaultPagerImpl
;
29 import eu
.etaxonomy
.cdm
.api
.utility
.DescriptionUtility
;
30 import eu
.etaxonomy
.cdm
.common
.monitor
.IProgressMonitor
;
31 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
32 import eu
.etaxonomy
.cdm
.model
.common
.Annotation
;
33 import eu
.etaxonomy
.cdm
.model
.common
.AnnotationType
;
34 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
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
.dto
.TermDto
;
64 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
;
65 import eu
.etaxonomy
.cdm
.strategy
.cache
.common
.IIdentifiableEntityCacheStrategy
;
73 * @author a.kohlbecker
78 @Transactional(readOnly
= true)
79 public class DescriptionServiceImpl
extends IdentifiableServiceBase
<DescriptionBase
,IDescriptionDao
> implements IDescriptionService
{
81 private static final Logger logger
= Logger
.getLogger(DescriptionServiceImpl
.class);
83 protected IDescriptionElementDao descriptionElementDao
;
84 protected IFeatureTreeDao featureTreeDao
;
85 protected IFeatureNodeDao featureNodeDao
;
86 protected IFeatureDao featureDao
;
87 protected ITermVocabularyDao vocabularyDao
;
88 protected IDefinedTermDao definedTermDao
;
89 protected IStatisticalMeasurementValueDao statisticalMeasurementValueDao
;
90 protected ITaxonDao taxonDao
;
92 //TODO change to Interface
93 private NaturalLanguageGenerator naturalLanguageGenerator
;
96 protected void setFeatureTreeDao(IFeatureTreeDao featureTreeDao
) {
97 this.featureTreeDao
= featureTreeDao
;
101 protected void setFeatureNodeDao(IFeatureNodeDao featureNodeDao
) {
102 this.featureNodeDao
= featureNodeDao
;
106 protected void setFeatureDao(IFeatureDao featureDao
) {
107 this.featureDao
= featureDao
;
111 protected void setVocabularyDao(ITermVocabularyDao vocabularyDao
) {
112 this.vocabularyDao
= vocabularyDao
;
116 protected void setDefinedTermDao(IDefinedTermDao definedTermDao
) {
117 this.definedTermDao
= definedTermDao
;
121 protected void statisticalMeasurementValueDao(IStatisticalMeasurementValueDao statisticalMeasurementValueDao
) {
122 this.statisticalMeasurementValueDao
= statisticalMeasurementValueDao
;
126 protected void setDescriptionElementDao(IDescriptionElementDao descriptionElementDao
) {
127 this.descriptionElementDao
= descriptionElementDao
;
131 protected void setNaturalLanguageGenerator(NaturalLanguageGenerator naturalLanguageGenerator
) {
132 this.naturalLanguageGenerator
= naturalLanguageGenerator
;
136 protected void setTaxonDao(ITaxonDao taxonDao
) {
137 this.taxonDao
= taxonDao
;
143 public DescriptionServiceImpl() {
144 logger
.debug("Load DescriptionService Bean");
149 @Transactional(readOnly
= false)
150 public void updateTitleCache(Class
<?
extends DescriptionBase
> clazz
, Integer stepSize
, IIdentifiableEntityCacheStrategy
<DescriptionBase
> cacheStrategy
, IProgressMonitor monitor
) {
152 clazz
= DescriptionBase
.class;
154 super.updateTitleCacheImpl(clazz
, stepSize
, cacheStrategy
, monitor
);
159 public TermVocabulary
<Feature
> getDefaultFeatureVocabulary(){
160 String uuidFeature
= "b187d555-f06f-4d65-9e53-da7c93f8eaa8";
161 UUID featureUuid
= UUID
.fromString(uuidFeature
);
162 return vocabularyDao
.findByUuid(featureUuid
);
167 protected void setDao(IDescriptionDao dao
) {
172 public int count(Class
<?
extends DescriptionBase
> type
, Boolean hasImages
, Boolean hasText
,Set
<Feature
> feature
) {
173 return dao
.countDescriptions(type
, hasImages
, hasText
, feature
);
177 public <T
extends DescriptionElementBase
> Pager
<T
> pageDescriptionElements(DescriptionBase description
, Class
<?
extends DescriptionBase
> descriptionType
,
178 Set
<Feature
> features
, Class
<T
> type
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
180 List
<T
> results
= listDescriptionElements(description
, descriptionType
, features
, type
, pageSize
, pageNumber
, propertyPaths
);
181 return new DefaultPagerImpl
<T
>(pageNumber
, results
.size(), pageSize
, results
);
186 public <T
extends DescriptionElementBase
> Pager
<T
> getDescriptionElements(DescriptionBase description
,
187 Set
<Feature
> features
, Class
<T
> type
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
188 return pageDescriptionElements(description
, null, features
, type
, pageSize
, pageNumber
, propertyPaths
);
194 public <T
extends DescriptionElementBase
> List
<T
> listDescriptionElements(DescriptionBase description
,
195 Class
<?
extends DescriptionBase
> descriptionType
, Set
<Feature
> features
, Class
<T
> type
, Integer pageSize
, Integer pageNumber
,
196 List
<String
> propertyPaths
) {
198 Integer numberOfResults
= dao
.countDescriptionElements(description
, descriptionType
, features
, type
);
199 List
<T
> results
= new ArrayList
<T
>();
200 if(AbstractPagerImpl
.hasResultsInRange(numberOfResults
.longValue(), pageNumber
, pageSize
)) {
201 results
= dao
.getDescriptionElements(description
, descriptionType
, features
, type
, pageSize
, pageNumber
, propertyPaths
);
210 public <T
extends DescriptionElementBase
> List
<T
> listDescriptionElements(DescriptionBase description
,
211 Set
<Feature
> features
, Class
<T
> type
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
213 return listDescriptionElements(description
, null, features
, type
, pageSize
, pageNumber
, propertyPaths
);
217 public Pager
<Annotation
> getDescriptionElementAnnotations(DescriptionElementBase annotatedObj
, MarkerType status
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
){
218 Integer numberOfResults
= descriptionElementDao
.countAnnotations(annotatedObj
, status
);
220 List
<Annotation
> results
= new ArrayList
<Annotation
>();
221 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
222 results
= descriptionElementDao
.getAnnotations(annotatedObj
, status
, pageSize
, pageNumber
, orderHints
, propertyPaths
);
225 return new DefaultPagerImpl
<Annotation
>(pageNumber
, numberOfResults
, pageSize
, results
);
230 public Pager
<Media
> getMedia(DescriptionElementBase descriptionElement
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
231 Integer numberOfResults
= descriptionElementDao
.countMedia(descriptionElement
);
233 List
<Media
> results
= new ArrayList
<Media
>();
234 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
235 results
= descriptionElementDao
.getMedia(descriptionElement
, pageSize
, pageNumber
, propertyPaths
);
238 return new DefaultPagerImpl
<Media
>(pageNumber
, numberOfResults
, pageSize
, results
);
242 public Pager
<TaxonDescription
> pageTaxonDescriptions(Taxon taxon
, Set
<DefinedTerm
> scopes
, Set
<NamedArea
> geographicalScope
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
243 Set
<MarkerType
> markerTypes
= null;
244 return pageTaxonDescriptions(taxon
, scopes
, geographicalScope
, markerTypes
, pageSize
, pageNumber
, propertyPaths
);
248 public List
<TaxonDescription
> listTaxonDescriptions(Taxon taxon
, Set
<DefinedTerm
> scopes
, Set
<NamedArea
> geographicalScope
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
249 Set
<MarkerType
> markerTypes
= null;
250 return listTaxonDescriptions(taxon
, scopes
, geographicalScope
, markerTypes
, pageSize
, pageNumber
, propertyPaths
);
254 public Pager
<TaxonDescription
> pageTaxonDescriptions(Taxon taxon
, Set
<DefinedTerm
> scopes
, Set
<NamedArea
> geographicalScope
, Set
<MarkerType
> markerTypes
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
255 Integer numberOfResults
= dao
.countTaxonDescriptions(taxon
, scopes
, geographicalScope
, markerTypes
);
257 List
<TaxonDescription
> results
= new ArrayList
<TaxonDescription
>();
258 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
259 results
= dao
.listTaxonDescriptions(taxon
, scopes
, geographicalScope
, markerTypes
, pageSize
, pageNumber
, propertyPaths
);
262 return new DefaultPagerImpl
<TaxonDescription
>(pageNumber
, numberOfResults
, pageSize
, results
);
266 public List
<TaxonDescription
> listTaxonDescriptions(Taxon taxon
, Set
<DefinedTerm
> scopes
, Set
<NamedArea
> geographicalScope
, Set
<MarkerType
> markerTypes
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
267 List
<TaxonDescription
> results
= dao
.listTaxonDescriptions(taxon
, scopes
, geographicalScope
, markerTypes
, pageSize
, pageNumber
, propertyPaths
);
273 public List
<Media
> listTaxonDescriptionMedia(UUID taxonUuid
, boolean limitToGalleries
, Set
<MarkerType
> markerTypes
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
){
274 return this.dao
.listTaxonDescriptionMedia(taxonUuid
, limitToGalleries
, markerTypes
, pageSize
, pageNumber
, propertyPaths
);
278 public int countTaxonDescriptionMedia(UUID taxonUuid
, boolean limitToGalleries
, Set
<MarkerType
> markerTypes
){
279 return this.dao
.countTaxonDescriptionMedia(taxonUuid
, limitToGalleries
, markerTypes
);
284 public DistributionTree
getOrderedDistributions(
285 Set
<TaxonDescription
> taxonDescriptions
,
286 boolean subAreaPreference
,
287 boolean statusOrderPreference
,
288 Set
<MarkerType
> hiddenAreaMarkerTypes
,
289 Set
<NamedAreaLevel
> omitLevels
, List
<String
> propertyPaths
){
291 List
<Distribution
> distList
= new ArrayList
<Distribution
>();
293 List
<UUID
> uuids
= new ArrayList
<UUID
>();
294 for (TaxonDescription taxonDescription
: taxonDescriptions
) {
295 if (! taxonDescription
.isImageGallery()){ //image galleries should not have descriptions, but better filter fully on DTYPE of description element
296 uuids
.add(taxonDescription
.getUuid());
300 List
<DescriptionBase
> desclist
= dao
.list(uuids
, null, null, null, propertyPaths
);
301 for (DescriptionBase desc
: desclist
) {
302 if (desc
.isInstanceOf(TaxonDescription
.class)){
303 Set
<DescriptionElementBase
> elements
= desc
.getElements();
304 for (DescriptionElementBase element
: elements
) {
305 if (element
.isInstanceOf(Distribution
.class)) {
306 Distribution distribution
= (Distribution
) element
;
307 if(distribution
.getArea() != null){
308 distList
.add(distribution
);
316 // for (TaxonDescription taxonDescription : taxonDescriptions) {
317 // if (logger.isDebugEnabled()){ logger.debug("load taxon description " + taxonDescription.getUuid());}
318 // //TODO why not loading all description via .list ? This may improve performance
319 // taxonDescription = (TaxonDescription) dao.load(taxonDescription.getUuid(), propertyPaths);
320 // Set<DescriptionElementBase> elements = taxonDescription.getElements();
321 // for (DescriptionElementBase element : elements) {
322 // if (element.isInstanceOf(Distribution.class)) {
323 // Distribution distribution = (Distribution) element;
324 // if(distribution.getArea() != null){
325 // distList.add(distribution);
331 if (logger
.isDebugEnabled()){logger
.debug("filter tree for " + distList
.size() + " distributions ...");}
333 // filter distributions
334 Collection
<Distribution
> filteredDistributions
= DescriptionUtility
.filterDistributions(distList
, hiddenAreaMarkerTypes
, true, statusOrderPreference
, false);
336 distList
.addAll(filteredDistributions
);
338 return DescriptionUtility
.orderDistributions(definedTermDao
, omitLevels
, distList
, hiddenAreaMarkerTypes
, null);
343 public Pager
<TaxonNameDescription
> getTaxonNameDescriptions(TaxonNameBase name
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
344 Integer numberOfResults
= dao
.countTaxonNameDescriptions(name
);
346 List
<TaxonNameDescription
> results
= new ArrayList
<TaxonNameDescription
>();
347 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
348 results
= dao
.getTaxonNameDescriptions(name
, pageSize
, pageNumber
,propertyPaths
);
351 return new DefaultPagerImpl
<TaxonNameDescription
>(pageNumber
, numberOfResults
, pageSize
, results
);
356 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
) {
357 Integer numberOfResults
= dao
.countDescriptions(type
, hasImages
, hasText
, feature
);
359 List
<DescriptionBase
> results
= new ArrayList
<DescriptionBase
>();
360 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
361 results
= dao
.listDescriptions(type
, hasImages
, hasText
, feature
, pageSize
, pageNumber
,orderHints
,propertyPaths
);
364 return new DefaultPagerImpl
<DescriptionBase
>(pageNumber
, numberOfResults
, pageSize
, results
);
368 * FIXME Candidate for harmonization
369 * Rename: searchByDistribution
372 public Pager
<TaxonDescription
> searchDescriptionByDistribution(Set
<NamedArea
> namedAreas
, PresenceAbsenceTerm presence
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
373 Integer numberOfResults
= dao
.countDescriptionByDistribution(namedAreas
, presence
);
375 List
<TaxonDescription
> results
= new ArrayList
<TaxonDescription
>();
376 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
377 results
= dao
.searchDescriptionByDistribution(namedAreas
, presence
, pageSize
, pageNumber
,orderHints
,propertyPaths
);
380 return new DefaultPagerImpl
<TaxonDescription
>(pageNumber
, numberOfResults
, pageSize
, results
);
384 * FIXME Candidate for harmonization
385 * move: descriptionElementService.search
388 public Pager
<DescriptionElementBase
> searchElements(Class
<?
extends DescriptionElementBase
> clazz
, String queryString
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
389 Integer numberOfResults
= descriptionElementDao
.count(clazz
, queryString
);
391 List
<DescriptionElementBase
> results
= new ArrayList
<DescriptionElementBase
>();
392 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
393 results
= descriptionElementDao
.search(clazz
, queryString
, pageSize
, pageNumber
, orderHints
, propertyPaths
);
396 return new DefaultPagerImpl
<DescriptionElementBase
>(pageNumber
, numberOfResults
, pageSize
, results
);
400 * FIXME Candidate for harmonization
401 * descriptionElementService.find
404 public DescriptionElementBase
getDescriptionElementByUuid(UUID uuid
) {
405 return descriptionElementDao
.findByUuid(uuid
);
409 * FIXME Candidate for harmonization
410 * descriptionElementService.load
413 public DescriptionElementBase
loadDescriptionElement(UUID uuid
, List
<String
> propertyPaths
) {
414 return descriptionElementDao
.load(uuid
, propertyPaths
);
418 * FIXME Candidate for harmonization
419 * descriptionElementService.save
422 @Transactional(readOnly
= false)
423 public UUID
saveDescriptionElement(DescriptionElementBase descriptionElement
) {
424 return descriptionElementDao
.save(descriptionElement
).getUuid();
428 * FIXME Candidate for harmonization
429 * descriptionElementService.save
432 @Transactional(readOnly
= false)
433 public Map
<UUID
, DescriptionElementBase
> saveDescriptionElement(Collection
<DescriptionElementBase
> descriptionElements
) {
434 return descriptionElementDao
.saveAll(descriptionElements
);
438 * FIXME Candidate for harmonization
439 * descriptionElementService.delete
442 public UUID
deleteDescriptionElement(DescriptionElementBase descriptionElement
) {
443 return descriptionElementDao
.delete(descriptionElement
);
448 * @see eu.etaxonomy.cdm.api.service.IDescriptionService#deleteDescriptionElement(java.util.UUID)
451 public UUID
deleteDescriptionElement(UUID descriptionElementUuid
) {
452 return deleteDescriptionElement(descriptionElementDao
.load(descriptionElementUuid
));
456 @Transactional(readOnly
= false)
457 public DeleteResult
deleteDescription(DescriptionBase description
) {
458 DeleteResult deleteResult
= new DeleteResult();
460 if (description
instanceof TaxonDescription
){
461 TaxonDescription taxDescription
= HibernateProxyHelper
.deproxy(description
, TaxonDescription
.class);
462 Taxon tax
= taxDescription
.getTaxon();
463 tax
.removeDescription(taxDescription
, true);
464 dao
.delete(description
);
466 deleteResult
.addUpdatedObject(tax
);
467 deleteResult
.setCdmEntity(tax
);
476 * @see eu.etaxonomy.cdm.api.service.IDescriptionService#deleteDescription(java.util.UUID)
479 @Transactional(readOnly
= false)
480 public DeleteResult
deleteDescription(UUID descriptionUuid
) {
481 return deleteDescription(dao
.load(descriptionUuid
));
486 public TermVocabulary
<Feature
> getFeatureVocabulary(UUID uuid
) {
487 return vocabularyDao
.findByUuid(uuid
);
492 public <T
extends DescriptionElementBase
> List
<T
> getDescriptionElementsForTaxon(
493 Taxon taxon
, Set
<Feature
> features
,
494 Class
<T
> type
, Integer pageSize
,
495 Integer pageNumber
, List
<String
> propertyPaths
) {
496 return listDescriptionElementsForTaxon(taxon
, features
, type
, pageSize
, pageNumber
, propertyPaths
);
500 public <T
extends DescriptionElementBase
> List
<T
> listDescriptionElementsForTaxon(
501 Taxon taxon
, Set
<Feature
> features
,
502 Class
<T
> type
, Integer pageSize
,
503 Integer pageNumber
, List
<String
> propertyPaths
) {
504 return dao
.getDescriptionElementForTaxon(taxon
.getUuid(), features
, type
, pageSize
, pageNumber
, propertyPaths
);
508 public <T
extends DescriptionElementBase
> Pager
<T
> pageDescriptionElementsForTaxon(
509 Taxon taxon
, Set
<Feature
> features
,
510 Class
<T
> type
, Integer pageSize
,
511 Integer pageNumber
, List
<String
> propertyPaths
) {
512 if (logger
.isDebugEnabled()){logger
.debug(" get count ...");}
513 Long count
= dao
.countDescriptionElementForTaxon(taxon
.getUuid(), features
, type
);
514 List
<T
> descriptionElements
;
515 if(AbstractPagerImpl
.hasResultsInRange(count
, pageNumber
, pageSize
)){ // no point checking again
516 if (logger
.isDebugEnabled()){logger
.debug(" get list ...");}
517 descriptionElements
= listDescriptionElementsForTaxon(taxon
, features
, type
, pageSize
, pageNumber
, propertyPaths
);
519 descriptionElements
= new ArrayList
<T
>(0);
521 if (logger
.isDebugEnabled()){logger
.debug(" service - DONE ...");}
522 return new DefaultPagerImpl
<T
>(pageNumber
, count
.intValue(), pageSize
, descriptionElements
);
527 * @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)
530 public String
generateNaturalLanguageDescription(FeatureTree featureTree
,
531 TaxonDescription description
, List
<Language
> preferredLanguages
, String separator
) {
533 Language lang
= preferredLanguages
.size() > 0 ? preferredLanguages
.get(0) : Language
.DEFAULT();
535 description
= (TaxonDescription
)load(description
.getUuid());
536 featureTree
= featureTreeDao
.load(featureTree
.getUuid());
538 StringBuilder naturalLanguageDescription
= new StringBuilder();
540 MarkerType useMarkerType
= (MarkerType
) definedTermDao
.load(UUID
.fromString("2e6e42d9-e92a-41f4-899b-03c0ac64f039"));
541 boolean isUseDescription
= false;
542 if(!description
.getMarkers().isEmpty()) {
543 for (Marker marker
: description
.getMarkers()) {
544 MarkerType markerType
= marker
.getMarkerType();
545 if (markerType
.equals(useMarkerType
)) {
546 isUseDescription
= true;
552 if(description
.hasStructuredData() && !isUseDescription
){
555 String lastCategory
= null;
556 String categorySeparator
= ". ";
558 List
<TextData
> textDataList
;
559 TextData naturalLanguageDescriptionText
= null;
561 boolean useMicroFormatQuantitativeDescriptionBuilder
= false;
563 if(useMicroFormatQuantitativeDescriptionBuilder
){
565 MicroFormatQuantitativeDescriptionBuilder micro
= new MicroFormatQuantitativeDescriptionBuilder();
566 naturalLanguageGenerator
.setQuantitativeDescriptionBuilder(micro
);
567 naturalLanguageDescriptionText
= naturalLanguageGenerator
.generateSingleTextData(featureTree
, (description
), lang
);
571 naturalLanguageDescriptionText
= naturalLanguageGenerator
.generateSingleTextData(
577 return naturalLanguageDescriptionText
.getText(lang
);
580 // boolean doItBetter = false;
582 // for (TextData textData : textDataList.toArray(new TextData[textDataList.size()])){
583 // if(textData.getMultilanguageText().size() > 0){
585 // if (!textData.getFeature().equals(Feature.UNKNOWN())) {
586 // String featureLabel = textData.getFeature().getLabel(lang);
591 // * The code lines below are desinged to handle
592 // * a special case where as the feature label contains
593 // * hierarchical information on the features. This code
594 // * exist only as a base for discussion, and is not
595 // * intendet to be used in production.
597 // featureLabel = StringUtils.remove(featureLabel, '>');
599 // String[] labelTokens = StringUtils.split(featureLabel, '<');
600 // if(labelTokens[0].equals(lastCategory) && labelTokens.length > 1){
601 // if(naturalLanguageDescription.length() > 0){
602 // naturalLanguageDescription.append(separator);
604 // naturalLanguageDescription.append(labelTokens[1]);
606 // if(naturalLanguageDescription.length() > 0){
607 // naturalLanguageDescription.append(categorySeparator);
609 // naturalLanguageDescription.append(StringUtils.join(labelTokens));
611 // lastCategory = labelTokens[0];
612 // // end of demo code
614 // if(naturalLanguageDescription.length() > 0){
615 // naturalLanguageDescription.append(separator);
617 // naturalLanguageDescription.append(textData.getFeature().getLabel(lang));
620 // if(naturalLanguageDescription.length() > 0){
621 // naturalLanguageDescription.append(separator);
624 // String text = textData.getMultilanguageText().values().iterator().next().getText();
625 // naturalLanguageDescription.append(text);
631 else if (isUseDescription
) {
632 //AT: Left Blank in case we need to generate a Natural language text string.
634 return naturalLanguageDescription
.toString();
639 public boolean hasStructuredData(DescriptionBase
<?
> description
) {
640 return load(description
.getUuid()).hasStructuredData();
645 @Transactional(readOnly
= false)
646 public UpdateResult
moveDescriptionElementsToDescription(
647 Collection
<DescriptionElementBase
> descriptionElements
,
648 DescriptionBase targetDescription
,
651 UpdateResult result
= new UpdateResult();
652 if (descriptionElements
.isEmpty() ){
656 if (! isCopy
&& descriptionElements
== descriptionElements
.iterator().next().getInDescription().getElements()){
657 //if the descriptionElements collection is the elements set of a description, put it in a separate set before to avoid concurrent modification exceptions
658 descriptionElements
= new HashSet
<DescriptionElementBase
>(descriptionElements
);
659 // descriptionElementsTmp.addAll(descriptionElements);
660 // descriptionElements = descriptionElementsTmp;
662 for (DescriptionElementBase element
: descriptionElements
){
663 DescriptionBase
<?
> description
= element
.getInDescription();
665 DescriptionElementBase newElement
= (DescriptionElementBase
)element
.clone();
666 targetDescription
.addElement(newElement
);
667 } catch (CloneNotSupportedException e
) {
668 throw new RuntimeException ("Clone not yet implemented for class " + element
.getClass().getName(), e
);
671 description
.removeElement(element
);
672 if (description
.getElements().isEmpty()){
673 if (description
instanceof TaxonDescription
){
674 TaxonDescription taxDescription
= HibernateProxyHelper
.deproxy(description
, TaxonDescription
.class);
675 if (taxDescription
.getTaxon() != null){
676 taxDescription
.getTaxon().removeDescription((TaxonDescription
)description
);
679 dao
.delete(description
);
681 dao
.saveOrUpdate(description
);
682 result
.addUpdatedObject(description
);
688 dao
.saveOrUpdate(targetDescription
);
689 result
.addUpdatedObject(targetDescription
);
690 if (targetDescription
instanceof TaxonDescription
){
691 result
.addUpdatedObject(((TaxonDescription
)targetDescription
).getTaxon());
697 @Transactional(readOnly
= false)
698 public UpdateResult
moveDescriptionElementsToDescription(
699 Set
<UUID
> descriptionElementUUIDs
,
700 UUID targetDescriptionUuid
,
702 Set
<DescriptionElementBase
> descriptionElements
= new HashSet
<DescriptionElementBase
>();
703 for(UUID deUuid
: descriptionElementUUIDs
) {
704 descriptionElements
.add(descriptionElementDao
.load(deUuid
));
706 DescriptionBase targetDescription
= dao
.load(targetDescriptionUuid
);
708 return moveDescriptionElementsToDescription(descriptionElements
, targetDescription
, isCopy
);
712 @Transactional(readOnly
= false)
713 public UpdateResult
moveDescriptionElementsToDescription(
714 Set
<UUID
> descriptionElementUUIDs
,
715 UUID targetTaxonUuid
,
718 Taxon targetTaxon
= CdmBase
.deproxy(taxonDao
.load(targetTaxonUuid
), Taxon
.class);
719 DescriptionBase targetDescription
= TaxonDescription
.NewInstance(targetTaxon
);
720 targetDescription
.setTitleCache(moveMessage
, true);
721 Annotation annotation
= Annotation
.NewInstance(moveMessage
, Language
.getDefaultLanguage());
722 annotation
.setAnnotationType(AnnotationType
.TECHNICAL());
723 targetDescription
.addAnnotation(annotation
);
725 targetDescription
= dao
.save(targetDescription
);
726 Set
<DescriptionElementBase
> descriptionElements
= new HashSet
<DescriptionElementBase
>();
727 for(UUID deUuid
: descriptionElementUUIDs
) {
728 descriptionElements
.add(descriptionElementDao
.load(deUuid
));
731 return moveDescriptionElementsToDescription(descriptionElements
, targetDescription
, isCopy
);
735 public Pager
<TermDto
> pageNamedAreasInUse(boolean includeAllParents
, Integer pageSize
,
737 List
<TermDto
> results
= dao
.listNamedAreasInUse(includeAllParents
, null, null);
738 int startIndex
= pageNumber
* pageSize
;
739 int toIndex
= Math
.min(startIndex
+ pageSize
, results
.size());
740 List
<TermDto
> page
= results
.subList(startIndex
, toIndex
);
741 return new DefaultPagerImpl
<TermDto
>(pageNumber
, results
.size(), pageSize
, page
);
746 @Transactional(readOnly
= false)
747 public UpdateResult
moveTaxonDescriptions(Taxon sourceTaxon
, Taxon targetTaxon
) {
748 List
<TaxonDescription
> descriptions
= new ArrayList(sourceTaxon
.getDescriptions());
749 UpdateResult result
= new UpdateResult();
750 result
.addUpdatedObject(sourceTaxon
);
751 result
.addUpdatedObject(targetTaxon
);
752 for(TaxonDescription description
: descriptions
){
754 String moveMessage
= String
.format("Description moved from %s", sourceTaxon
);
755 if(description
.isProtectedTitleCache()){
756 String separator
= "";
757 if(!StringUtils
.isBlank(description
.getTitleCache())){
760 description
.setTitleCache(description
.getTitleCache() + separator
+ moveMessage
, true);
762 Annotation annotation
= Annotation
.NewInstance(moveMessage
, Language
.getDefaultLanguage());
763 annotation
.setAnnotationType(AnnotationType
.TECHNICAL());
764 description
.addAnnotation(annotation
);
765 targetTaxon
.addDescription(description
);
771 @Transactional(readOnly
= false)
772 public UpdateResult
moveTaxonDescriptions(UUID sourceTaxonUuid
, UUID targetTaxonUuid
) {
773 Taxon sourceTaxon
= HibernateProxyHelper
.deproxy(taxonDao
.load(sourceTaxonUuid
), Taxon
.class);
774 Taxon targetTaxon
= HibernateProxyHelper
.deproxy(taxonDao
.load(targetTaxonUuid
), Taxon
.class);
775 return moveTaxonDescriptions(sourceTaxon
, targetTaxon
);
780 @Transactional(readOnly
= false)
781 public UpdateResult
moveTaxonDescription(UUID descriptionUuid
, UUID targetTaxonUuid
){
782 UpdateResult result
= new UpdateResult();
783 TaxonDescription description
= HibernateProxyHelper
.deproxy(dao
.load(descriptionUuid
), TaxonDescription
.class);
785 Taxon sourceTaxon
= description
.getTaxon();
786 String moveMessage
= String
.format("Description moved from %s", sourceTaxon
);
787 if(description
.isProtectedTitleCache()){
788 String separator
= "";
789 if(!StringUtils
.isBlank(description
.getTitleCache())){
792 description
.setTitleCache(description
.getTitleCache() + separator
+ moveMessage
, true);
794 Annotation annotation
= Annotation
.NewInstance(moveMessage
, Language
.getDefaultLanguage());
795 annotation
.setAnnotationType(AnnotationType
.TECHNICAL());
796 description
.addAnnotation(annotation
);
797 Taxon targetTaxon
= HibernateProxyHelper
.deproxy(taxonDao
.load(targetTaxonUuid
), Taxon
.class);
798 targetTaxon
.addDescription(description
);
799 result
.addUpdatedObject(targetTaxon
);
800 result
.addUpdatedObject(sourceTaxon
);
801 // dao.merge(description);