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
.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
.model
.common
.Annotation
;
32 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTerm
;
33 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
34 import eu
.etaxonomy
.cdm
.model
.common
.Marker
;
35 import eu
.etaxonomy
.cdm
.model
.common
.MarkerType
;
36 import eu
.etaxonomy
.cdm
.model
.common
.TermVocabulary
;
37 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
38 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
39 import eu
.etaxonomy
.cdm
.model
.description
.Distribution
;
40 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
41 import eu
.etaxonomy
.cdm
.model
.description
.FeatureTree
;
42 import eu
.etaxonomy
.cdm
.model
.description
.PresenceAbsenceTermBase
;
43 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
44 import eu
.etaxonomy
.cdm
.model
.description
.TaxonNameDescription
;
45 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
46 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
47 import eu
.etaxonomy
.cdm
.model
.location
.NamedAreaLevel
;
48 import eu
.etaxonomy
.cdm
.model
.media
.Media
;
49 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
50 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
51 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.IDefinedTermDao
;
52 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.ITermVocabularyDao
;
53 import eu
.etaxonomy
.cdm
.persistence
.dao
.description
.IDescriptionDao
;
54 import eu
.etaxonomy
.cdm
.persistence
.dao
.description
.IDescriptionElementDao
;
55 import eu
.etaxonomy
.cdm
.persistence
.dao
.description
.IFeatureDao
;
56 import eu
.etaxonomy
.cdm
.persistence
.dao
.description
.IFeatureNodeDao
;
57 import eu
.etaxonomy
.cdm
.persistence
.dao
.description
.IFeatureTreeDao
;
58 import eu
.etaxonomy
.cdm
.persistence
.dao
.description
.IStatisticalMeasurementValueDao
;
59 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
;
60 import eu
.etaxonomy
.cdm
.strategy
.cache
.common
.IIdentifiableEntityCacheStrategy
;
68 @Transactional(readOnly
= true)
69 public class DescriptionServiceImpl
extends IdentifiableServiceBase
<DescriptionBase
,IDescriptionDao
> implements IDescriptionService
{
71 private static final Logger logger
= Logger
.getLogger(DescriptionServiceImpl
.class);
73 protected IDescriptionElementDao descriptionElementDao
;
74 protected IFeatureTreeDao featureTreeDao
;
75 protected IFeatureNodeDao featureNodeDao
;
76 protected IFeatureDao featureDao
;
77 protected ITermVocabularyDao vocabularyDao
;
78 protected IDefinedTermDao definedTermDao
;
79 protected IStatisticalMeasurementValueDao statisticalMeasurementValueDao
;
80 //TODO change to Interface
81 private NaturalLanguageGenerator naturalLanguageGenerator
;
84 protected void setFeatureTreeDao(IFeatureTreeDao featureTreeDao
) {
85 this.featureTreeDao
= featureTreeDao
;
89 protected void setFeatureNodeDao(IFeatureNodeDao featureNodeDao
) {
90 this.featureNodeDao
= featureNodeDao
;
94 protected void setFeatureDao(IFeatureDao featureDao
) {
95 this.featureDao
= featureDao
;
99 protected void setVocabularyDao(ITermVocabularyDao vocabularyDao
) {
100 this.vocabularyDao
= vocabularyDao
;
104 protected void setDefinedTermDao(IDefinedTermDao definedTermDao
) {
105 this.definedTermDao
= definedTermDao
;
109 protected void statisticalMeasurementValueDao(IStatisticalMeasurementValueDao statisticalMeasurementValueDao
) {
110 this.statisticalMeasurementValueDao
= statisticalMeasurementValueDao
;
114 protected void setDescriptionElementDao(IDescriptionElementDao descriptionElementDao
) {
115 this.descriptionElementDao
= descriptionElementDao
;
119 protected void setNaturalLanguageGenerator(NaturalLanguageGenerator naturalLanguageGenerator
) {
120 this.naturalLanguageGenerator
= naturalLanguageGenerator
;
126 public DescriptionServiceImpl() {
127 logger
.debug("Load DescriptionService Bean");
133 * @see eu.etaxonomy.cdm.api.service.IIdentifiableEntityService#updateTitleCache(java.lang.Integer, eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy)
136 @Transactional(readOnly
= false)
137 public void updateTitleCache(Class
<?
extends DescriptionBase
> clazz
, Integer stepSize
, IIdentifiableEntityCacheStrategy
<DescriptionBase
> cacheStrategy
, IProgressMonitor monitor
) {
139 clazz
= DescriptionBase
.class;
141 super.updateTitleCacheImpl(clazz
, stepSize
, cacheStrategy
, monitor
);
146 public TermVocabulary
<Feature
> getDefaultFeatureVocabulary(){
147 String uuidFeature
= "b187d555-f06f-4d65-9e53-da7c93f8eaa8";
148 UUID featureUuid
= UUID
.fromString(uuidFeature
);
149 return vocabularyDao
.findByUuid(featureUuid
);
154 protected void setDao(IDescriptionDao dao
) {
159 public int count(Class
<?
extends DescriptionBase
> type
, Boolean hasImages
, Boolean hasText
,Set
<Feature
> feature
) {
160 return dao
.countDescriptions(type
, hasImages
, hasText
, feature
);
164 public Pager
<DescriptionElementBase
> pageDescriptionElements(DescriptionBase description
, Class
<?
extends DescriptionBase
> descriptionType
,
165 Set
<Feature
> features
, Class
<?
extends DescriptionElementBase
> type
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
167 List
<DescriptionElementBase
> results
= listDescriptionElements(description
, descriptionType
, features
, type
, pageSize
, pageNumber
, propertyPaths
);
168 return new DefaultPagerImpl
<DescriptionElementBase
>(pageNumber
, results
.size(), pageSize
, results
);
172 * @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)
176 public Pager
<DescriptionElementBase
> getDescriptionElements(DescriptionBase description
,
177 Set
<Feature
> features
, Class
<?
extends DescriptionElementBase
> type
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
178 return pageDescriptionElements(description
, null, features
, type
, pageSize
, pageNumber
, propertyPaths
);
182 public List
<DescriptionElementBase
> listDescriptionElements(DescriptionBase description
, Class
<?
extends DescriptionBase
> descriptionType
,
183 Set
<Feature
> features
, Class
<?
extends DescriptionElementBase
> type
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
185 Integer numberOfResults
= dao
.countDescriptionElements(description
, descriptionType
, features
, type
);
186 List
<DescriptionElementBase
> results
= new ArrayList
<DescriptionElementBase
>();
187 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
188 results
= dao
.getDescriptionElements(description
, descriptionType
, features
, type
, pageSize
, pageNumber
, propertyPaths
);
194 * @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)
198 public List
<DescriptionElementBase
> listDescriptionElements(DescriptionBase description
,
199 Set
<Feature
> features
, Class
<?
extends DescriptionElementBase
> type
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
201 return listDescriptionElements(description
, null, features
, type
, pageSize
, pageNumber
, propertyPaths
);
205 public Pager
<Annotation
> getDescriptionElementAnnotations(DescriptionElementBase annotatedObj
, MarkerType status
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
){
206 Integer numberOfResults
= descriptionElementDao
.countAnnotations(annotatedObj
, status
);
208 List
<Annotation
> results
= new ArrayList
<Annotation
>();
209 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
210 results
= descriptionElementDao
.getAnnotations(annotatedObj
, status
, pageSize
, pageNumber
, orderHints
, propertyPaths
);
213 return new DefaultPagerImpl
<Annotation
>(pageNumber
, numberOfResults
, pageSize
, results
);
219 public Pager
<Media
> getMedia(DescriptionElementBase descriptionElement
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
220 Integer numberOfResults
= descriptionElementDao
.countMedia(descriptionElement
);
222 List
<Media
> results
= new ArrayList
<Media
>();
223 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
224 results
= descriptionElementDao
.getMedia(descriptionElement
, pageSize
, pageNumber
, propertyPaths
);
227 return new DefaultPagerImpl
<Media
>(pageNumber
, numberOfResults
, pageSize
, results
);
231 public Pager
<TaxonDescription
> pageTaxonDescriptions(Taxon taxon
, Set
<DefinedTerm
> scopes
, Set
<NamedArea
> geographicalScope
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
232 Set
<MarkerType
> markerTypes
= null;
233 return pageTaxonDescriptions(taxon
, scopes
, geographicalScope
, markerTypes
, pageSize
, pageNumber
, propertyPaths
);
237 public List
<TaxonDescription
> listTaxonDescriptions(Taxon taxon
, Set
<DefinedTerm
> scopes
, Set
<NamedArea
> geographicalScope
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
238 Set
<MarkerType
> markerTypes
= null;
239 return listTaxonDescriptions(taxon
, scopes
, geographicalScope
, markerTypes
, pageSize
, pageNumber
, propertyPaths
);
244 * @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)
247 public Pager
<TaxonDescription
> pageTaxonDescriptions(Taxon taxon
, Set
<DefinedTerm
> scopes
, Set
<NamedArea
> geographicalScope
, Set
<MarkerType
> markerTypes
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
248 Integer numberOfResults
= dao
.countTaxonDescriptions(taxon
, scopes
, geographicalScope
, markerTypes
);
250 List
<TaxonDescription
> results
= new ArrayList
<TaxonDescription
>();
251 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
252 results
= dao
.listTaxonDescriptions(taxon
, scopes
, geographicalScope
, markerTypes
, pageSize
, pageNumber
, propertyPaths
);
255 return new DefaultPagerImpl
<TaxonDescription
>(pageNumber
, numberOfResults
, pageSize
, results
);
259 public List
<TaxonDescription
> listTaxonDescriptions(Taxon taxon
, Set
<DefinedTerm
> scopes
, Set
<NamedArea
> geographicalScope
, Set
<MarkerType
> markerTypes
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
260 List
<TaxonDescription
> results
= dao
.listTaxonDescriptions(taxon
, scopes
, geographicalScope
, markerTypes
, pageSize
, pageNumber
, propertyPaths
);
266 * @see eu.etaxonomy.cdm.api.service.IDescriptionService#listTaxonDescriptionMedia(UUID, boolean, Set, Integer, Integer, List)
269 public List
<Media
> listTaxonDescriptionMedia(UUID taxonUuid
, boolean limitToGalleries
, Set
<MarkerType
> markerTypes
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
){
270 return this.dao
.listTaxonDescriptionMedia(taxonUuid
, limitToGalleries
, markerTypes
, pageSize
, pageNumber
, propertyPaths
);
274 * @see IDescriptionService#countTaxonDescriptionMedia(UUID, boolean, Set)
277 public int countTaxonDescriptionMedia(UUID taxonUuid
, boolean limitToGalleries
, Set
<MarkerType
> markerTypes
){
278 return this.dao
.countTaxonDescriptionMedia(taxonUuid
, limitToGalleries
, markerTypes
);
285 public DistributionTree
getOrderedDistributions(
286 Set
<TaxonDescription
> taxonDescriptions
,
287 Set
<NamedAreaLevel
> omitLevels
,
288 List
<String
> propertyPaths
){
290 DistributionTree tree
= new DistributionTree();
291 List
<Distribution
> distList
= new ArrayList
<Distribution
>();
292 if (logger
.isDebugEnabled()){logger
.debug("create tree ...");}
294 List
<UUID
> uuids
= new ArrayList
<UUID
>();
295 for (TaxonDescription taxonDescription
: taxonDescriptions
) {
296 if (! taxonDescription
.isImageGallery()){ //image galleries should not have descriptions, but better filter fully on DTYPE of description element
297 uuids
.add(taxonDescription
.getUuid());
301 List
<DescriptionBase
> desclist
= dao
.list(uuids
, null, null, null, propertyPaths
);
302 for (DescriptionBase desc
: desclist
) {
303 if (desc
.isInstanceOf(TaxonDescription
.class)){
304 Set
<DescriptionElementBase
> elements
= desc
.getElements();
305 for (DescriptionElementBase element
: elements
) {
306 if (element
.isInstanceOf(Distribution
.class)) {
307 Distribution distribution
= (Distribution
) element
;
308 if(distribution
.getArea() != null){
309 distList
.add(distribution
);
317 // for (TaxonDescription taxonDescription : taxonDescriptions) {
318 // if (logger.isDebugEnabled()){ logger.debug("load taxon description " + taxonDescription.getUuid());}
319 // //TODO why not loading all description via .list ? This may improve performance
320 // taxonDescription = (TaxonDescription) dao.load(taxonDescription.getUuid(), propertyPaths);
321 // Set<DescriptionElementBase> elements = taxonDescription.getElements();
322 // for (DescriptionElementBase element : elements) {
323 // if (element.isInstanceOf(Distribution.class)) {
324 // Distribution distribution = (Distribution) element;
325 // if(distribution.getArea() != null){
326 // distList.add(distribution);
332 if (logger
.isDebugEnabled()){logger
.debug("filter tree for " + distList
.size() + " distributions ...");}
334 // filter distributions
335 Collection
<Distribution
> filteredDistributions
= DescriptionUtility
.filterDistributions(distList
);
337 distList
.addAll(filteredDistributions
);
339 if (logger
.isDebugEnabled()){logger
.debug("order tree ...");}
342 tree
.orderAsTree(distList
, omitLevels
);
343 tree
.recursiveSortChildrenByLabel(); // FIXME respect current locale for sorting
344 if (logger
.isDebugEnabled()){logger
.debug("create tree - DONE");}
349 public Pager
<TaxonNameDescription
> getTaxonNameDescriptions(TaxonNameBase name
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
350 Integer numberOfResults
= dao
.countTaxonNameDescriptions(name
);
352 List
<TaxonNameDescription
> results
= new ArrayList
<TaxonNameDescription
>();
353 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
354 results
= dao
.getTaxonNameDescriptions(name
, pageSize
, pageNumber
,propertyPaths
);
357 return new DefaultPagerImpl
<TaxonNameDescription
>(pageNumber
, numberOfResults
, pageSize
, results
);
362 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
) {
363 Integer numberOfResults
= dao
.countDescriptions(type
, hasImages
, hasText
, feature
);
365 List
<DescriptionBase
> results
= new ArrayList
<DescriptionBase
>();
366 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
367 results
= dao
.listDescriptions(type
, hasImages
, hasText
, feature
, pageSize
, pageNumber
,orderHints
,propertyPaths
);
370 return new DefaultPagerImpl
<DescriptionBase
>(pageNumber
, numberOfResults
, pageSize
, results
);
374 * FIXME Candidate for harmonization
375 * Rename: searchByDistribution
378 public Pager
<TaxonDescription
> searchDescriptionByDistribution(Set
<NamedArea
> namedAreas
, PresenceAbsenceTermBase presence
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
379 Integer numberOfResults
= dao
.countDescriptionByDistribution(namedAreas
, presence
);
381 List
<TaxonDescription
> results
= new ArrayList
<TaxonDescription
>();
382 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
383 results
= dao
.searchDescriptionByDistribution(namedAreas
, presence
, pageSize
, pageNumber
,orderHints
,propertyPaths
);
386 return new DefaultPagerImpl
<TaxonDescription
>(pageNumber
, numberOfResults
, pageSize
, results
);
390 * FIXME Candidate for harmonization
391 * move: descriptionElementService.search
394 public Pager
<DescriptionElementBase
> searchElements(Class
<?
extends DescriptionElementBase
> clazz
, String queryString
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
395 Integer numberOfResults
= descriptionElementDao
.count(clazz
, queryString
);
397 List
<DescriptionElementBase
> results
= new ArrayList
<DescriptionElementBase
>();
398 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
399 results
= descriptionElementDao
.search(clazz
, queryString
, pageSize
, pageNumber
, orderHints
, propertyPaths
);
402 return new DefaultPagerImpl
<DescriptionElementBase
>(pageNumber
, numberOfResults
, pageSize
, results
);
406 * FIXME Candidate for harmonization
407 * descriptionElementService.find
410 public DescriptionElementBase
getDescriptionElementByUuid(UUID uuid
) {
411 return descriptionElementDao
.findByUuid(uuid
);
415 * FIXME Candidate for harmonization
416 * descriptionElementService.load
419 public DescriptionElementBase
loadDescriptionElement(UUID uuid
, List
<String
> propertyPaths
) {
420 return descriptionElementDao
.load(uuid
, propertyPaths
);
424 * FIXME Candidate for harmonization
425 * descriptionElementService.save
428 @Transactional(readOnly
= false)
429 public UUID
saveDescriptionElement(DescriptionElementBase descriptionElement
) {
430 return descriptionElementDao
.save(descriptionElement
);
434 * FIXME Candidate for harmonization
435 * descriptionElementService.save
438 @Transactional(readOnly
= false)
439 public Map
<UUID
, DescriptionElementBase
> saveDescriptionElement(Collection
<DescriptionElementBase
> descriptionElements
) {
440 return descriptionElementDao
.saveAll(descriptionElements
);
444 * FIXME Candidate for harmonization
445 * descriptionElementService.delete
448 public UUID
deleteDescriptionElement(DescriptionElementBase descriptionElement
) {
449 return descriptionElementDao
.delete(descriptionElement
);
453 public TermVocabulary
<Feature
> getFeatureVocabulary(UUID uuid
) {
454 return vocabularyDao
.findByUuid(uuid
);
459 public <T
extends DescriptionElementBase
> List
<T
> getDescriptionElementsForTaxon(
460 Taxon taxon
, Set
<Feature
> features
,
461 Class
<T
> type
, Integer pageSize
,
462 Integer pageNumber
, List
<String
> propertyPaths
) {
463 return listDescriptionElementsForTaxon(taxon
, features
, type
, pageSize
, pageNumber
, propertyPaths
);
467 public <T
extends DescriptionElementBase
> List
<T
> listDescriptionElementsForTaxon(
468 Taxon taxon
, Set
<Feature
> features
,
469 Class
<T
> type
, Integer pageSize
,
470 Integer pageNumber
, List
<String
> propertyPaths
) {
471 return dao
.getDescriptionElementForTaxon(taxon
, features
, type
, pageSize
, pageNumber
, propertyPaths
);
475 public <T
extends DescriptionElementBase
> Pager
<T
> pageDescriptionElementsForTaxon(
476 Taxon taxon
, Set
<Feature
> features
,
477 Class
<T
> type
, Integer pageSize
,
478 Integer pageNumber
, List
<String
> propertyPaths
) {
479 if (logger
.isDebugEnabled()){logger
.debug(" get count ...");}
480 Long count
= dao
.countDescriptionElementForTaxon(taxon
, features
, type
);
481 List
<T
> descriptionElements
;
482 if(AbstractPagerImpl
.hasResultsInRange(count
, pageNumber
, pageSize
)){ // no point checking again
483 if (logger
.isDebugEnabled()){logger
.debug(" get list ...");}
484 descriptionElements
= listDescriptionElementsForTaxon(taxon
, features
, type
, pageSize
, pageNumber
, propertyPaths
);
486 descriptionElements
= new ArrayList
<T
>(0);
488 if (logger
.isDebugEnabled()){logger
.debug(" service - DONE ...");}
489 return new DefaultPagerImpl
<T
>(pageNumber
, count
.intValue(), pageSize
, descriptionElements
);
494 * @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)
497 public String
generateNaturalLanguageDescription(FeatureTree featureTree
,
498 TaxonDescription description
, List
<Language
> preferredLanguages
, String separator
) {
500 Language lang
= preferredLanguages
.size() > 0 ? preferredLanguages
.get(0) : Language
.DEFAULT();
502 description
= (TaxonDescription
)load(description
.getUuid());
503 featureTree
= featureTreeDao
.load(featureTree
.getUuid());
505 StringBuilder naturalLanguageDescription
= new StringBuilder();
507 MarkerType useMarkerType
= (MarkerType
) definedTermDao
.load(UUID
.fromString("2e6e42d9-e92a-41f4-899b-03c0ac64f039"));
508 boolean isUseDescription
= false;
509 if(!description
.getMarkers().isEmpty()) {
510 for (Marker marker
: description
.getMarkers()) {
511 MarkerType markerType
= marker
.getMarkerType();
512 if (markerType
.equals(useMarkerType
)) {
513 isUseDescription
= true;
519 if(description
.hasStructuredData() && !isUseDescription
){
522 String lastCategory
= null;
523 String categorySeparator
= ". ";
525 List
<TextData
> textDataList
;
526 TextData naturalLanguageDescriptionText
= null;
528 boolean useMicroFormatQuantitativeDescriptionBuilder
= false;
530 if(useMicroFormatQuantitativeDescriptionBuilder
){
532 MicroFormatQuantitativeDescriptionBuilder micro
= new MicroFormatQuantitativeDescriptionBuilder();
533 naturalLanguageGenerator
.setQuantitativeDescriptionBuilder(micro
);
534 naturalLanguageDescriptionText
= naturalLanguageGenerator
.generateSingleTextData(featureTree
, (description
), lang
);
538 naturalLanguageDescriptionText
= naturalLanguageGenerator
.generateSingleTextData(
544 return naturalLanguageDescriptionText
.getText(lang
);
547 // boolean doItBetter = false;
549 // for (TextData textData : textDataList.toArray(new TextData[textDataList.size()])){
550 // if(textData.getMultilanguageText().size() > 0){
552 // if (!textData.getFeature().equals(Feature.UNKNOWN())) {
553 // String featureLabel = textData.getFeature().getLabel(lang);
558 // * The code lines below are desinged to handle
559 // * a special case where as the feature label contains
560 // * hierarchical information on the features. This code
561 // * exist only as a base for discussion, and is not
562 // * intendet to be used in production.
564 // featureLabel = StringUtils.remove(featureLabel, '>');
566 // String[] labelTokens = StringUtils.split(featureLabel, '<');
567 // if(labelTokens[0].equals(lastCategory) && labelTokens.length > 1){
568 // if(naturalLanguageDescription.length() > 0){
569 // naturalLanguageDescription.append(separator);
571 // naturalLanguageDescription.append(labelTokens[1]);
573 // if(naturalLanguageDescription.length() > 0){
574 // naturalLanguageDescription.append(categorySeparator);
576 // naturalLanguageDescription.append(StringUtils.join(labelTokens));
578 // lastCategory = labelTokens[0];
579 // // end of demo code
581 // if(naturalLanguageDescription.length() > 0){
582 // naturalLanguageDescription.append(separator);
584 // naturalLanguageDescription.append(textData.getFeature().getLabel(lang));
587 // if(naturalLanguageDescription.length() > 0){
588 // naturalLanguageDescription.append(separator);
591 // String text = textData.getMultilanguageText().values().iterator().next().getText();
592 // naturalLanguageDescription.append(text);
598 else if (isUseDescription
) {
599 //AT: Left Blank in case we need to generate a Natural language text string.
601 return naturalLanguageDescription
.toString();
605 * @see eu.etaxonomy.cdm.api.service.IDescriptionService#hasStructuredData(eu.etaxonomy.cdm.model.description.DescriptionBase)
608 public boolean hasStructuredData(DescriptionBase
<?
> description
) {
609 return load(description
.getUuid()).hasStructuredData();
614 * @see eu.etaxonomy.cdm.api.service.IDescriptionService#moveDescriptionElementsToDescription(java.util.Collection, eu.etaxonomy.cdm.model.description.DescriptionBase, boolean)
617 public void moveDescriptionElementsToDescription(Collection
<DescriptionElementBase
> descriptionElements
,
618 DescriptionBase targetDescription
, boolean isCopy
) {
620 if (descriptionElements
.isEmpty() ){
624 if (! isCopy
&& descriptionElements
== descriptionElements
.iterator().next().getInDescription().getElements()){
625 //if the descriptionElements collection is the elements set of a description, put it in a separate set before to avoid concurrent modification exceptions
626 descriptionElements
= new HashSet
<DescriptionElementBase
>(descriptionElements
);
627 // descriptionElementsTmp.addAll(descriptionElements);
628 // descriptionElements = descriptionElementsTmp;
630 for (DescriptionElementBase element
: descriptionElements
){
631 DescriptionBase description
= element
.getInDescription();
633 DescriptionElementBase newElement
= (DescriptionElementBase
)element
.clone();
634 targetDescription
.addElement(newElement
);
635 } catch (CloneNotSupportedException e
) {
636 new RuntimeException ("Clone not yet implemented for class " + element
.getClass().getName(), e
);
639 description
.removeElement(element
);
646 public Pager
<NamedArea
> pageNamedAreasInUse(Integer pageSize
,
647 Integer pageNumber
, List
<String
> propertyPaths
){
649 List
<NamedArea
> results
= dao
.listNamedAreasInUse(pageSize
, pageNumber
, propertyPaths
);
650 return new DefaultPagerImpl
<NamedArea
>(pageNumber
, results
.size(), pageSize
, results
);