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
.model
.common
.CdmClass
;
26 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
27 import eu
.etaxonomy
.cdm
.model
.term
.DefinedTermBase
;
28 import eu
.etaxonomy
.cdm
.model
.term
.TermType
;
29 import eu
.etaxonomy
.cdm
.model
.term
.TermVocabulary
;
30 import eu
.etaxonomy
.cdm
.persistence
.dao
.term
.ITermVocabularyDao
;
31 import eu
.etaxonomy
.cdm
.persistence
.dto
.TermDto
;
32 import eu
.etaxonomy
.cdm
.persistence
.dto
.TermVocabularyDto
;
33 import eu
.etaxonomy
.cdm
.persistence
.dto
.UuidAndTitleCache
;
34 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
;
35 import eu
.etaxonomy
.cdm
.strategy
.cache
.common
.IIdentifiableEntityCacheStrategy
;
38 @Transactional(readOnly
= true)
39 public class VocabularyServiceImpl
extends IdentifiableServiceBase
<TermVocabulary
,ITermVocabularyDao
> implements IVocabularyService
{
42 private ITermService termService
;
46 protected void setDao(ITermVocabularyDao dao
) {
51 @Transactional(readOnly
= false)
52 public UpdateResult
updateCaches(Class
<?
extends TermVocabulary
> clazz
, Integer stepSize
, IIdentifiableEntityCacheStrategy
<TermVocabulary
> cacheStrategy
, IProgressMonitor monitor
) {
54 clazz
= TermVocabulary
.class;
56 return super.updateCachesImpl(clazz
, stepSize
, cacheStrategy
, monitor
);
60 public List
<TermVocabulary
> listByTermType(TermType termType
, boolean includeSubTypes
,
61 Integer limit
, Integer start
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
62 return dao
.listByTermType(termType
, includeSubTypes
, limit
, start
, orderHints
, propertyPaths
);
66 public <T
extends DefinedTermBase
> List
<TermVocabulary
<T
>> findByTermType(TermType termType
, List
<String
> propertyPaths
) {
67 return dao
.findByTermType(termType
, propertyPaths
);
71 * @see eu.etaxonomy.cdm.api.service.ITermService#getLanguageVocabulary()
72 * FIXME candidate for harmonization
73 * is this the same as getVocabulary(VocabularyEnum.Language)
76 public TermVocabulary
<Language
> getLanguageVocabulary() {
77 String uuidString
= "45ac7043-7f5e-4f37-92f2-3874aaaef2de";
78 UUID uuid
= UUID
.fromString(uuidString
);
79 TermVocabulary
<Language
> languageVocabulary
= dao
.findByUuid(uuid
);
80 return languageVocabulary
;
84 public Pager
<DefinedTermBase
> getTerms(TermVocabulary vocabulary
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
85 long numberOfResults
= dao
.countTerms(vocabulary
);
87 List
<DefinedTermBase
> results
= new ArrayList
<>();
88 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
89 results
= dao
.getTerms(vocabulary
, pageSize
, pageNumber
,orderHints
,propertyPaths
);
92 return new DefaultPagerImpl
<>(pageNumber
, numberOfResults
, pageSize
, results
);
97 public Collection
<TermDto
> getTopLevelTerms(UUID vocabularyUuid
) {
98 return dao
.getTopLevelTerms(vocabularyUuid
);
102 public Collection
<TermDto
> getTerms(UUID vocabularyUuid
) {
103 return dao
.getTerms(vocabularyUuid
);
107 public Collection
<TermDto
> getTerms(List
<UUID
> vocabularyUuids
) {
108 return dao
.getTerms(vocabularyUuids
);
112 public Collection
<TermDto
> getNamedAreaTerms(List
<UUID
> vocabularyUuids
) {
113 return dao
.getNamedAreaTerms(vocabularyUuids
);
117 public List
<TermDto
> getCompleteTermHierarchy(TermVocabularyDto vocabularyDto
) {
118 List
<TermDto
> topLevelTerms
= dao
.getTopLevelTerms(vocabularyDto
.getUuid(), vocabularyDto
.getTermType());
119 for (TermDto termDto
: topLevelTerms
) {
120 termDto
.setVocabularyDto(vocabularyDto
);
121 initializeIncludes(termDto
);
122 initializeGeneralizationOf(termDto
);
125 return topLevelTerms
;
128 private void initializeGeneralizationOf(TermDto parentTerm
){
129 Collection
<TermDto
> generalizationOf
= termService
.getKindOfsAsDto(parentTerm
);
130 parentTerm
.setGeneralizationOf(generalizationOf
);
131 generalizationOf
.forEach(generalization
->{
132 generalization
.setVocabularyDto(parentTerm
.getVocabularyDto());
133 generalization
.setKindOfDto(parentTerm
);
134 initializeGeneralizationOf(generalization
);
138 private void initializeIncludes(TermDto parentTerm
){
139 Collection
<TermDto
> includes
= termService
.getIncludesAsDto(parentTerm
);
140 parentTerm
.setIncludes(includes
);
141 includes
.forEach(include
->{
142 include
.setVocabularyDto(parentTerm
.getVocabularyDto());
143 initializeIncludes(include
);
144 include
.setPartOfDto(parentTerm
);
149 public List
<TermVocabularyDto
> findVocabularyDtoByTermType(TermType termType
) {
150 return findVocabularyDtoByTermTypes(Collections
.singleton(termType
), true);
154 public List
<TermVocabularyDto
> findVocabularyDtoByTermTypeAndPattern(String pattern
, TermType termType
) {
155 return dao
.findVocabularyDtoByTermTypes(Collections
.singleton(termType
), pattern
, true);
159 public List
<TermVocabularyDto
> findVocabularyDtoByTermTypes(Set
<TermType
> termTypes
) {
160 return findVocabularyDtoByTermTypes(termTypes
, true);
164 public List
<TermVocabularyDto
> findVocabularyDtoByTermType(TermType termType
, boolean includeSubtypes
) {
165 return findVocabularyDtoByTermTypes(Collections
.singleton(termType
), includeSubtypes
);
169 public List
<TermVocabularyDto
> findVocabularyDtoByTermTypes(Set
<TermType
> termTypes
, boolean includeSubtypes
) {
170 return dao
.findVocabularyDtoByTermTypes(termTypes
, includeSubtypes
);
174 public List
<TermVocabularyDto
> findFeatureVocabularyDtoByTermTypes(Set
<CdmClass
> availableFor
) {
175 return dao
.findVocabularyDtoByAvailableFor(availableFor
);
179 public TermVocabularyDto
findVocabularyDtoByVocabularyUuid(UUID vocUuid
) {
180 return dao
.findVocabularyDtoByUuid(vocUuid
);
183 @Transactional(readOnly
= false)
185 public TermDto
addNewTerm(TermType termType
, UUID vocabularyUUID
) {
186 DefinedTermBase
<?
> term
= termType
.getEmptyDefinedTermBase();
187 termService
.save(term
);
188 TermVocabulary vocabulary
= dao
.load(vocabularyUUID
);
189 vocabulary
.addTerm(term
);
190 dao
.saveOrUpdate(vocabulary
);
191 return TermDto
.fromTerm(term
, true);
195 public <S
extends TermVocabulary
> List
<UuidAndTitleCache
<S
>> getUuidAndTitleCache(Class
<S
> clazz
, TermType termType
,
196 Integer limit
, String pattern
) {
197 return dao
.getUuidAndTitleCache(clazz
, termType
, limit
, pattern
);
201 public List
<TermVocabularyDto
> findVocabularyDtoByVocabularyUuids(List
<UUID
> vocUuids
) {
202 return dao
.findVocabularyDtoByUuids(vocUuids
);