2 * Copyright (C) 2009 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.
9 package eu
.etaxonomy
.cdm
.api
.service
;
11 import java
.util
.ArrayList
;
12 import java
.util
.Collection
;
13 import java
.util
.Collections
;
14 import java
.util
.List
;
16 import java
.util
.UUID
;
18 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
19 import org
.springframework
.stereotype
.Service
;
20 import org
.springframework
.transaction
.annotation
.Transactional
;
22 import eu
.etaxonomy
.cdm
.api
.service
.pager
.Pager
;
23 import eu
.etaxonomy
.cdm
.api
.service
.pager
.impl
.DefaultPagerImpl
;
24 import eu
.etaxonomy
.cdm
.common
.monitor
.IProgressMonitor
;
25 import eu
.etaxonomy
.cdm
.filter
.VocabularyFilter
;
26 import eu
.etaxonomy
.cdm
.model
.common
.CdmClass
;
27 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
28 import eu
.etaxonomy
.cdm
.model
.term
.DefinedTermBase
;
29 import eu
.etaxonomy
.cdm
.model
.term
.TermCollection
;
30 import eu
.etaxonomy
.cdm
.model
.term
.TermType
;
31 import eu
.etaxonomy
.cdm
.model
.term
.TermVocabulary
;
32 import eu
.etaxonomy
.cdm
.persistence
.dao
.description
.IVocabularyFilterDao
;
33 import eu
.etaxonomy
.cdm
.persistence
.dao
.term
.ITermCollectionDao
;
34 import eu
.etaxonomy
.cdm
.persistence
.dao
.term
.ITermVocabularyDao
;
35 import eu
.etaxonomy
.cdm
.persistence
.dto
.TermDto
;
36 import eu
.etaxonomy
.cdm
.persistence
.dto
.TermVocabularyDto
;
37 import eu
.etaxonomy
.cdm
.persistence
.dto
.UuidAndTitleCache
;
38 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
;
39 import eu
.etaxonomy
.cdm
.strategy
.cache
.common
.IIdentifiableEntityCacheStrategy
;
42 @Transactional(readOnly
= true)
43 public class VocabularyServiceImpl
44 extends IdentifiableServiceBase
<TermVocabulary
,ITermVocabularyDao
>
45 implements IVocabularyService
{
48 private ITermService termService
;
51 private IVocabularyFilterDao vocabularyFilterDao
;
54 private ITermCollectionDao termCollectionDao
;
59 protected void setDao(ITermVocabularyDao dao
) {
64 @Transactional(readOnly
= false)
65 public UpdateResult
updateCaches(Class
<?
extends TermVocabulary
> clazz
, Integer stepSize
, IIdentifiableEntityCacheStrategy
<TermVocabulary
> cacheStrategy
, IProgressMonitor monitor
) {
67 clazz
= TermVocabulary
.class;
69 return super.updateCachesImpl(clazz
, stepSize
, cacheStrategy
, monitor
);
73 public List
<TermVocabulary
> listByTermType(TermType termType
, boolean includeSubTypes
,
74 Integer limit
, Integer start
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
75 return dao
.listByTermType(termType
, includeSubTypes
, limit
, start
, orderHints
, propertyPaths
);
79 public <T
extends DefinedTermBase
> List
<TermVocabulary
<T
>> findByTermType(TermType termType
, List
<String
> propertyPaths
) {
80 return dao
.findByTermType(termType
, propertyPaths
);
84 public <T
extends DefinedTermBase
> List
<TermVocabulary
<T
>> findByTermType(Set
<TermType
> termTypes
, List
<String
> propertyPaths
){
85 if (termTypes
.size() == 1) {
86 return findByTermType(termTypes
.iterator().next(), propertyPaths
);
88 List
<TermVocabulary
<T
>> result
= new ArrayList
<>();
89 for (TermType termType
: termTypes
) {
90 result
.addAll(dao
.findByTermType(termType
, propertyPaths
));
98 * @see eu.etaxonomy.cdm.api.service.ITermService#getLanguageVocabulary()
99 * FIXME candidate for harmonization
100 * is this the same as getVocabulary(VocabularyEnum.Language)
103 public TermVocabulary
<Language
> getLanguageVocabulary() {
104 String uuidString
= "45ac7043-7f5e-4f37-92f2-3874aaaef2de";
105 UUID uuid
= UUID
.fromString(uuidString
);
106 TermVocabulary
<Language
> languageVocabulary
= dao
.findByUuid(uuid
);
107 return languageVocabulary
;
111 public Pager
<DefinedTermBase
> getTerms(TermVocabulary vocabulary
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
112 long numberOfResults
= dao
.countTerms(vocabulary
);
114 List
<DefinedTermBase
> results
= new ArrayList
<>();
115 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
116 results
= dao
.getTerms(vocabulary
, pageSize
, pageNumber
,orderHints
,propertyPaths
);
119 return new DefaultPagerImpl
<>(pageNumber
, numberOfResults
, pageSize
, results
);
124 public Collection
<TermDto
> getTopLevelTerms(UUID vocabularyUuid
) {
125 return dao
.getTopLevelTerms(vocabularyUuid
);
129 public Collection
<TermDto
> getTerms(UUID vocabularyUuid
) {
130 return dao
.getTerms(vocabularyUuid
);
134 public Collection
<TermDto
> getTerms(List
<UUID
> vocabularyUuids
) {
135 return dao
.getTerms(vocabularyUuids
);
139 public Collection
<TermDto
> getNamedAreaTerms(List
<UUID
> vocabularyUuids
) {
140 return dao
.getNamedAreaTerms(vocabularyUuids
);
144 public List
<TermDto
> getCompleteTermHierarchy(TermVocabularyDto vocabularyDto
) {
145 List
<TermDto
> topLevelTerms
= dao
.getTopLevelTerms(vocabularyDto
.getUuid(), vocabularyDto
.getTermType());
146 for (TermDto termDto
: topLevelTerms
) {
147 termDto
.setVocabularyDto(vocabularyDto
);
148 initializeIncludes(termDto
);
149 initializeGeneralizationOf(termDto
);
152 return topLevelTerms
;
155 private void initializeGeneralizationOf(TermDto parentTerm
){
156 Collection
<TermDto
> generalizationOf
= termService
.getKindOfsAsDto(parentTerm
);
157 parentTerm
.setGeneralizationOf(generalizationOf
);
158 generalizationOf
.forEach(generalization
->{
159 generalization
.setVocabularyDto(parentTerm
.getVocabularyDto());
160 generalization
.setKindOfDto(parentTerm
);
161 initializeGeneralizationOf(generalization
);
165 private void initializeIncludes(TermDto parentTerm
){
166 Collection
<TermDto
> includes
= termService
.getIncludesAsDto(parentTerm
);
167 parentTerm
.setIncludes(includes
);
168 includes
.forEach(include
->{
169 include
.setVocabularyDto(parentTerm
.getVocabularyDto());
170 initializeIncludes(include
);
171 include
.setPartOfDto(parentTerm
);
176 public List
<TermVocabularyDto
> findVocabularyDtoByTermType(TermType termType
) {
177 return findVocabularyDtoByTermTypes(Collections
.singleton(termType
), true);
181 public List
<TermVocabularyDto
> findVocabularyDtoByTermTypeAndPattern(String pattern
, TermType termType
) {
182 return dao
.findVocabularyDtoByTermTypes(Collections
.singleton(termType
), pattern
, true);
186 public List
<TermVocabularyDto
> findVocabularyDtoByTermTypes(Set
<TermType
> termTypes
) {
187 return findVocabularyDtoByTermTypes(termTypes
, true);
191 public List
<TermVocabularyDto
> findVocabularyDtoByTermType(TermType termType
, boolean includeSubtypes
) {
192 return findVocabularyDtoByTermTypes(Collections
.singleton(termType
), includeSubtypes
);
196 public List
<TermVocabularyDto
> findVocabularyDtoByTermTypes(Set
<TermType
> termTypes
, boolean includeSubtypes
) {
197 return dao
.findVocabularyDtoByTermTypes(termTypes
, includeSubtypes
);
201 public List
<TermVocabularyDto
> findFeatureVocabularyDtoByTermTypes(Set
<CdmClass
> availableFor
) {
202 return dao
.findVocabularyDtoByAvailableFor(availableFor
);
206 public TermVocabularyDto
findVocabularyDtoByVocabularyUuid(UUID vocUuid
) {
207 return dao
.findVocabularyDtoByUuid(vocUuid
);
210 @Transactional(readOnly
= false)
212 public TermDto
addNewTerm(TermType termType
, UUID vocabularyUUID
, Language lang
) {
213 DefinedTermBase
<?
> term
= termType
.getEmptyDefinedTermBase(lang
);
214 termService
.save(term
);
215 TermVocabulary vocabulary
= dao
.load(vocabularyUUID
);
216 vocabulary
.addTerm(term
);
217 dao
.saveOrUpdate(vocabulary
);
218 return TermDto
.fromTerm(term
, true);
222 public <S
extends TermVocabulary
> List
<UuidAndTitleCache
<S
>> getUuidAndTitleCache(Class
<S
> clazz
, TermType termType
,
223 Integer limit
, String pattern
) {
224 return dao
.getUuidAndTitleCache(clazz
, termType
, limit
, pattern
);
228 public List
<TermVocabularyDto
> findVocabularyDtoByVocabularyUuids(List
<UUID
> vocUuids
) {
229 return dao
.findVocabularyDtoByUuids(vocUuids
);
233 public List
<UUID
> uuidList(VocabularyFilter filter
){
234 return vocabularyFilterDao
.listUuids(filter
);
238 public List
<Integer
> idList(VocabularyFilter filter
){
239 return vocabularyFilterDao
.idList(filter
);
243 public long count(VocabularyFilter filter
) {
244 return vocabularyFilterDao
.count(filter
);
248 public TermCollection
<?
,?
> findCollection(UUID termCollectionUuid
){
249 return termCollectionDao
.findByUuid(termCollectionUuid
);