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
.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
.Propagation
;
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
.DefaultPagerImpl
;
29 import eu
.etaxonomy
.cdm
.model
.common
.Annotation
;
30 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
31 import eu
.etaxonomy
.cdm
.model
.common
.MarkerType
;
32 import eu
.etaxonomy
.cdm
.model
.common
.TermVocabulary
;
33 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
34 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
35 import eu
.etaxonomy
.cdm
.model
.description
.Distribution
;
36 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
37 import eu
.etaxonomy
.cdm
.model
.description
.FeatureTree
;
38 import eu
.etaxonomy
.cdm
.model
.description
.PresenceAbsenceTermBase
;
39 import eu
.etaxonomy
.cdm
.model
.description
.Scope
;
40 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
41 import eu
.etaxonomy
.cdm
.model
.description
.TaxonNameDescription
;
42 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
43 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
44 import eu
.etaxonomy
.cdm
.model
.location
.NamedAreaLevel
;
45 import eu
.etaxonomy
.cdm
.model
.media
.Media
;
46 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
47 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
48 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.ITermVocabularyDao
;
49 import eu
.etaxonomy
.cdm
.persistence
.dao
.description
.IDescriptionDao
;
50 import eu
.etaxonomy
.cdm
.persistence
.dao
.description
.IDescriptionElementDao
;
51 import eu
.etaxonomy
.cdm
.persistence
.dao
.description
.IFeatureDao
;
52 import eu
.etaxonomy
.cdm
.persistence
.dao
.description
.IFeatureNodeDao
;
53 import eu
.etaxonomy
.cdm
.persistence
.dao
.description
.IFeatureTreeDao
;
54 import eu
.etaxonomy
.cdm
.persistence
.dao
.description
.IStatisticalMeasurementValueDao
;
55 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
;
63 @Transactional(readOnly
= true, propagation
= Propagation
.SUPPORTS
)
64 public class DescriptionServiceImpl
extends IdentifiableServiceBase
<DescriptionBase
,IDescriptionDao
> implements IDescriptionService
{
66 private static final Logger logger
= Logger
.getLogger(DescriptionServiceImpl
.class);
68 protected IDescriptionElementDao descriptionElementDao
;
69 protected IFeatureTreeDao featureTreeDao
;
70 protected IFeatureNodeDao featureNodeDao
;
71 protected IFeatureDao featureDao
;
72 protected ITermVocabularyDao vocabularyDao
;
73 protected IStatisticalMeasurementValueDao statisticalMeasurementValueDao
;
74 private NaturalLanguageGenerator naturalLanguageGenerator
;
77 protected void setFeatureTreeDao(IFeatureTreeDao featureTreeDao
) {
78 this.featureTreeDao
= featureTreeDao
;
82 protected void setFeatureNodeDao(IFeatureNodeDao featureNodeDao
) {
83 this.featureNodeDao
= featureNodeDao
;
87 protected void setFeatureDao(IFeatureDao featureDao
) {
88 this.featureDao
= featureDao
;
92 protected void setVocabularyDao(ITermVocabularyDao vocabularyDao
) {
93 this.vocabularyDao
= vocabularyDao
;
97 protected void statisticalMeasurementValueDao(IStatisticalMeasurementValueDao statisticalMeasurementValueDao
) {
98 this.statisticalMeasurementValueDao
= statisticalMeasurementValueDao
;
102 protected void setDescriptionElementDao(IDescriptionElementDao descriptionElementDao
) {
103 this.descriptionElementDao
= descriptionElementDao
;
107 protected void setNaturalLanguageGenerator(NaturalLanguageGenerator naturalLanguageGenerator
) {
108 this.naturalLanguageGenerator
= naturalLanguageGenerator
;
114 public DescriptionServiceImpl() {
115 logger
.debug("Load DescriptionService Bean");
119 * @see eu.etaxonomy.cdm.api.service.IIdentifiableEntityService#updateTitleCache()
122 public void updateTitleCache() {
123 Class
<DescriptionBase
> clazz
= DescriptionBase
.class;
124 super.updateTitleCache(clazz
, null, null);
127 public TermVocabulary
<Feature
> getDefaultFeatureVocabulary(){
128 String uuidFeature
= "b187d555-f06f-4d65-9e53-da7c93f8eaa8";
129 UUID featureUuid
= UUID
.fromString(uuidFeature
);
130 return (TermVocabulary
)vocabularyDao
.findByUuid(featureUuid
);
134 protected void setDao(IDescriptionDao dao
) {
138 public int count(Class
<?
extends DescriptionBase
> type
, Boolean hasImages
, Boolean hasText
,Set
<Feature
> feature
) {
139 return dao
.countDescriptions(type
, hasImages
, hasText
, feature
);
143 * FIXME Candidate for harmonization
144 * rename -> getElements
146 public Pager
<DescriptionElementBase
> getDescriptionElements(DescriptionBase description
,
147 Set
<Feature
> features
, Class
<?
extends DescriptionElementBase
> type
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
149 List
<DescriptionElementBase
> results
= listDescriptionElements(description
, features
, type
, pageSize
, pageNumber
, propertyPaths
);
150 return new DefaultPagerImpl
<DescriptionElementBase
>(pageNumber
, results
.size(), pageSize
, results
);
153 public List
<DescriptionElementBase
> listDescriptionElements(DescriptionBase description
,
154 Set
<Feature
> features
, Class
<?
extends DescriptionElementBase
> type
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
155 Integer numberOfResults
= dao
.countDescriptionElements(description
, features
, type
);
157 List
<DescriptionElementBase
> results
= new ArrayList
<DescriptionElementBase
>();
158 if(numberOfResults
> 0) { // no point checking again
159 results
= dao
.getDescriptionElements(description
, features
, type
, pageSize
, pageNumber
, propertyPaths
);
164 public Pager
<Annotation
> getDescriptionElementAnnotations(DescriptionElementBase annotatedObj
, MarkerType status
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
){
165 Integer numberOfResults
= descriptionElementDao
.countAnnotations(annotatedObj
, status
);
167 List
<Annotation
> results
= new ArrayList
<Annotation
>();
168 if(numberOfResults
> 0) { // no point checking again
169 results
= descriptionElementDao
.getAnnotations(annotatedObj
, status
, pageSize
, pageNumber
, orderHints
, propertyPaths
);
172 return new DefaultPagerImpl
<Annotation
>(pageNumber
, numberOfResults
, pageSize
, results
);
177 public Pager
<Media
> getMedia(DescriptionElementBase descriptionElement
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
178 Integer numberOfResults
= descriptionElementDao
.countMedia(descriptionElement
);
180 List
<Media
> results
= new ArrayList
<Media
>();
181 if(numberOfResults
> 0) { // no point checking again
182 results
= descriptionElementDao
.getMedia(descriptionElement
, pageSize
, pageNumber
, propertyPaths
);
185 return new DefaultPagerImpl
<Media
>(pageNumber
, numberOfResults
, pageSize
, results
);
188 public Pager
<TaxonDescription
> getTaxonDescriptions(Taxon taxon
, Set
<Scope
> scopes
, Set
<NamedArea
> geographicalScope
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
189 Integer numberOfResults
= dao
.countTaxonDescriptions(taxon
, scopes
, geographicalScope
);
191 List
<TaxonDescription
> results
= new ArrayList
<TaxonDescription
>();
192 if(numberOfResults
> 0) { // no point checking again
193 results
= dao
.getTaxonDescriptions(taxon
, scopes
, geographicalScope
, pageSize
, pageNumber
, propertyPaths
);
196 return new DefaultPagerImpl
<TaxonDescription
>(pageNumber
, numberOfResults
, pageSize
, results
);
199 public List
<TaxonDescription
> listTaxonDescriptions(Taxon taxon
, Set
<Scope
> scopes
, Set
<NamedArea
> geographicalScope
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
200 List
<TaxonDescription
> results
= dao
.getTaxonDescriptions(taxon
, scopes
, geographicalScope
, pageSize
, pageNumber
, propertyPaths
);
204 public DistributionTree
getOrderedDistributions(
205 Set
<TaxonDescription
> taxonDescriptions
,
206 Set
<NamedAreaLevel
> omitLevels
,
207 List
<String
> propertyPaths
){
209 DistributionTree tree
= new DistributionTree();
210 List
<Distribution
> distList
= new ArrayList
<Distribution
>();
212 for (TaxonDescription taxonDescription
: taxonDescriptions
) {
213 taxonDescription
= (TaxonDescription
) dao
.load(taxonDescription
.getUuid(), propertyPaths
);
214 Set
<DescriptionElementBase
> elements
= taxonDescription
.getElements();
215 for (DescriptionElementBase element
: elements
) {
216 if (element
.isInstanceOf(Distribution
.class)) {
217 Distribution distribution
= (Distribution
) element
;
218 if(distribution
.getArea() != null){
219 distList
.add(distribution
);
226 tree
.merge(distList
, omitLevels
);
231 public Pager
<TaxonNameDescription
> getTaxonNameDescriptions(TaxonNameBase name
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
232 Integer numberOfResults
= dao
.countTaxonNameDescriptions(name
);
234 List
<TaxonNameDescription
> results
= new ArrayList
<TaxonNameDescription
>();
235 if(numberOfResults
> 0) { // no point checking again
236 results
= dao
.getTaxonNameDescriptions(name
, pageSize
, pageNumber
,propertyPaths
);
239 return new DefaultPagerImpl
<TaxonNameDescription
>(pageNumber
, numberOfResults
, pageSize
, results
);
243 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
) {
244 Integer numberOfResults
= dao
.countDescriptions(type
, hasImages
, hasText
, feature
);
246 List
<DescriptionBase
> results
= new ArrayList
<DescriptionBase
>();
247 if(numberOfResults
> 0) { // no point checking again
248 results
= dao
.listDescriptions(type
, hasImages
, hasText
, feature
, pageSize
, pageNumber
,orderHints
,propertyPaths
);
251 return new DefaultPagerImpl
<DescriptionBase
>(pageNumber
, numberOfResults
, pageSize
, results
);
255 * FIXME Candidate for harmonization
256 * Rename: searchByDistribution
258 public Pager
<TaxonDescription
> searchDescriptionByDistribution(Set
<NamedArea
> namedAreas
, PresenceAbsenceTermBase presence
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
259 Integer numberOfResults
= dao
.countDescriptionByDistribution(namedAreas
, presence
);
261 List
<TaxonDescription
> results
= new ArrayList
<TaxonDescription
>();
262 if(numberOfResults
> 0) { // no point checking again
263 results
= dao
.searchDescriptionByDistribution(namedAreas
, presence
, pageSize
, pageNumber
,orderHints
,propertyPaths
);
266 return new DefaultPagerImpl
<TaxonDescription
>(pageNumber
, numberOfResults
, pageSize
, results
);
270 * FIXME Candidate for harmonization
271 * move: descriptionElementService.search
273 public Pager
<DescriptionElementBase
> searchElements(Class
<?
extends DescriptionElementBase
> clazz
, String queryString
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
274 Integer numberOfResults
= descriptionElementDao
.count(clazz
,queryString
);
276 List
<DescriptionElementBase
> results
= new ArrayList
<DescriptionElementBase
>();
277 if(numberOfResults
> 0) { // no point checking again
278 results
= descriptionElementDao
.search(clazz
, queryString
, pageSize
, pageNumber
, orderHints
, propertyPaths
);
281 return new DefaultPagerImpl
<DescriptionElementBase
>(pageNumber
, numberOfResults
, pageSize
, results
);
285 * FIXME Candidate for harmonization
286 * descriptionElementService.find
288 public DescriptionElementBase
getDescriptionElementByUuid(UUID uuid
) {
289 return descriptionElementDao
.findByUuid(uuid
);
293 * FIXME Candidate for harmonization
294 * descriptionElementService.load
296 public DescriptionElementBase
loadDescriptionElement(UUID uuid
, List
<String
> propertyPaths
) {
297 return descriptionElementDao
.load(uuid
, propertyPaths
);
301 * FIXME Candidate for harmonization
302 * descriptionElementService.save
304 @Transactional(readOnly
= false)
305 public UUID
saveDescriptionElement(DescriptionElementBase descriptionElement
) {
306 return descriptionElementDao
.save(descriptionElement
);
310 * FIXME Candidate for harmonization
311 * descriptionElementService.save
313 @Transactional(readOnly
= false)
314 public Map
<UUID
, DescriptionElementBase
> saveDescriptionElement(Collection
<DescriptionElementBase
> descriptionElements
) {
315 return descriptionElementDao
.saveAll(descriptionElements
);
319 * FIXME Candidate for harmonization
320 * descriptionElementService.delete
322 public UUID
deleteDescriptionElement(DescriptionElementBase descriptionElement
) {
323 return descriptionElementDao
.delete(descriptionElement
);
326 public TermVocabulary
<Feature
> getFeatureVocabulary(UUID uuid
) {
327 return (TermVocabulary
)vocabularyDao
.findByUuid(uuid
);
330 public List
<DescriptionElementBase
> getDescriptionElementsForTaxon(
331 Taxon taxon
, Set
<Feature
> features
,
332 Class
<?
extends DescriptionElementBase
> type
, Integer pageSize
,
333 Integer pageNumber
, List
<String
> propertyPaths
) {
335 return (List
<DescriptionElementBase
>) dao
.getDescriptionElementForTaxon(taxon
, features
, type
, pageSize
, pageNumber
, propertyPaths
);
338 public List
<DescriptionElementBase
> getDescriptionElementsForTaxon(
339 Taxon taxon
, FeatureTree featureTree
,
340 Class
<?
extends DescriptionElementBase
> type
, Integer pageSize
,
341 Integer pageNumber
, List
<String
> propertyPaths
) {
343 return (List
<DescriptionElementBase
>) dao
.getDescriptionElementForTaxon(taxon
, featureTree
.getDistinctFeatures(), type
, pageSize
, pageNumber
, propertyPaths
);
347 * @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)
350 public String
generateNaturalLanguageDescription(FeatureTree featureTree
,
351 TaxonDescription description
, List
<Language
> preferredLanguages
, String separator
) {
353 Language lang
= preferredLanguages
.size() > 0 ? preferredLanguages
.get(0) : Language
.DEFAULT();
355 description
= (TaxonDescription
)load(description
.getUuid());
356 featureTree
= featureTreeDao
.load(featureTree
.getUuid());
358 StringBuilder naturalLanguageDescription
= new StringBuilder();
360 if(description
.hasStructuredData()){
363 String lastCategory
= null;
364 String categorySeparator
= ". ";
366 List
<TextData
> textDataList
;
367 String naturalLanguageDescriptionText
= null;
369 boolean useMicroFormatQuantitativeDescriptionBuilder
= false;
371 if(useMicroFormatQuantitativeDescriptionBuilder
){
373 MicroFormatQuantitativeDescriptionBuilder micro
= new MicroFormatQuantitativeDescriptionBuilder();
374 naturalLanguageGenerator
.setQuantitativeDescriptionBuilder(micro
);
375 //FXIME commented because it lead to compile errors -> Maxime please fix
376 logger
.error("naturalLanguageGenerator.generateStringNaturalLanguageDescription needs to be fixed");
377 // naturalLanguageDescriptionText = naturalLanguageGenerator.generateStringNaturalLanguageDescription(featureTree, ((TaxonDescription)description), lang);
381 //FXIME commented because it lead to compile errors -> Maxime please fix
382 logger
.error("naturalLanguageGenerator.generateStringNaturalLanguageDescription needs to be fixed");
383 // naturalLanguageDescriptionText = naturalLanguageGenerator.generateStringNaturalLanguageDescription(
385 // ((TaxonDescription)description),
389 return naturalLanguageDescriptionText
;
392 // boolean doItBetter = false;
394 // for (TextData textData : textDataList.toArray(new TextData[textDataList.size()])){
395 // if(textData.getMultilanguageText().size() > 0){
397 // if (!textData.getFeature().equals(Feature.UNKNOWN())) {
398 // String featureLabel = textData.getFeature().getLabel(lang);
403 // * The code lines below are desinged to handle
404 // * a special case where as the feature label contains
405 // * hierarchical information on the features. This code
406 // * exist only as a base for discussion, and is not
407 // * intendet to be used in production.
409 // featureLabel = StringUtils.remove(featureLabel, '>');
411 // String[] labelTokens = StringUtils.split(featureLabel, '<');
412 // if(labelTokens[0].equals(lastCategory) && labelTokens.length > 1){
413 // if(naturalLanguageDescription.length() > 0){
414 // naturalLanguageDescription.append(separator);
416 // naturalLanguageDescription.append(labelTokens[1]);
418 // if(naturalLanguageDescription.length() > 0){
419 // naturalLanguageDescription.append(categorySeparator);
421 // naturalLanguageDescription.append(StringUtils.join(labelTokens));
423 // lastCategory = labelTokens[0];
424 // // end of demo code
426 // if(naturalLanguageDescription.length() > 0){
427 // naturalLanguageDescription.append(separator);
429 // naturalLanguageDescription.append(textData.getFeature().getLabel(lang));
432 // if(naturalLanguageDescription.length() > 0){
433 // naturalLanguageDescription.append(separator);
436 // String text = textData.getMultilanguageText().values().iterator().next().getText();
437 // naturalLanguageDescription.append(text);
443 return naturalLanguageDescription
.toString();
447 * @see eu.etaxonomy.cdm.api.service.IDescriptionService#hasStructuredData(eu.etaxonomy.cdm.model.description.DescriptionBase)
450 public boolean hasStructuredData(DescriptionBase
<?
> description
) {
451 return load(description
.getUuid()).hasStructuredData();