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.
10 package eu
.etaxonomy
.cdm
.api
.service
;
12 import java
.util
.ArrayList
;
13 import java
.util
.Collection
;
14 import java
.util
.Collections
;
15 import java
.util
.List
;
17 import java
.util
.UUID
;
19 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
20 import org
.springframework
.stereotype
.Service
;
21 import org
.springframework
.transaction
.annotation
.Transactional
;
23 import eu
.etaxonomy
.cdm
.api
.service
.pager
.Pager
;
24 import eu
.etaxonomy
.cdm
.api
.service
.pager
.impl
.DefaultPagerImpl
;
25 import eu
.etaxonomy
.cdm
.common
.monitor
.IProgressMonitor
;
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
) {
52 @Transactional(readOnly
= false)
53 public UpdateResult
updateCaches(Class
<?
extends TermVocabulary
> clazz
, Integer stepSize
, IIdentifiableEntityCacheStrategy
<TermVocabulary
> cacheStrategy
, IProgressMonitor monitor
) {
55 clazz
= TermVocabulary
.class;
57 return super.updateCachesImpl(clazz
, stepSize
, cacheStrategy
, monitor
);
62 public List
<TermVocabulary
> listByTermType(TermType termType
, boolean includeSubTypes
,
63 Integer limit
, Integer start
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
64 return dao
.listByTermType(termType
, includeSubTypes
, limit
, start
, orderHints
, propertyPaths
);
68 public <T
extends DefinedTermBase
> List
<TermVocabulary
<T
>> findByTermType(TermType termType
, List
<String
> propertyPaths
) {
69 return dao
.findByTermType(termType
, propertyPaths
);
73 * @see eu.etaxonomy.cdm.api.service.ITermService#getLanguageVocabulary()
74 * FIXME candidate for harmonization
75 * is this the same as getVocabulary(VocabularyEnum.Language)
78 public TermVocabulary
<Language
> getLanguageVocabulary() {
79 String uuidString
= "45ac7043-7f5e-4f37-92f2-3874aaaef2de";
80 UUID uuid
= UUID
.fromString(uuidString
);
81 TermVocabulary
<Language
> languageVocabulary
= dao
.findByUuid(uuid
);
82 return languageVocabulary
;
86 public Pager
<DefinedTermBase
> getTerms(TermVocabulary vocabulary
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
87 long numberOfResults
= dao
.countTerms(vocabulary
);
89 List
<DefinedTermBase
> results
= new ArrayList
<>();
90 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
91 results
= dao
.getTerms(vocabulary
, pageSize
, pageNumber
,orderHints
,propertyPaths
);
94 return new DefaultPagerImpl
<>(pageNumber
, numberOfResults
, pageSize
, results
);
99 public Collection
<TermDto
> getTopLevelTerms(UUID vocabularyUuid
) {
100 return dao
.getTopLevelTerms(vocabularyUuid
);
104 public Collection
<TermDto
> getTerms(UUID vocabularyUuid
) {
105 return dao
.getTerms(vocabularyUuid
);
109 public Collection
<TermDto
> getTerms(List
<UUID
> vocabularyUuids
) {
110 return dao
.getTerms(vocabularyUuids
);
114 public Collection
<TermDto
> getCompleteTermHierarchy(TermVocabularyDto vocabularyDto
) {
115 Collection
<TermDto
> topLevelTerms
= dao
.getTopLevelTerms(vocabularyDto
.getUuid());
116 for (TermDto termDto
: topLevelTerms
) {
117 termDto
.setVocabularyDto(vocabularyDto
);
118 initializeIncludes(termDto
);
119 initializeGeneralizationOf(termDto
);
121 return topLevelTerms
;
124 private void initializeGeneralizationOf(TermDto parentTerm
){
125 Collection
<TermDto
> generalizationOf
= termService
.getKindOfsAsDto(parentTerm
);
126 parentTerm
.setGeneralizationOf(generalizationOf
);
127 generalizationOf
.forEach(generalization
->{
128 generalization
.setVocabularyDto(parentTerm
.getVocabularyDto());
129 generalization
.setKindOfDto(parentTerm
);
130 initializeGeneralizationOf(generalization
);
134 private void initializeIncludes(TermDto parentTerm
){
135 Collection
<TermDto
> includes
= termService
.getIncludesAsDto(parentTerm
);
136 parentTerm
.setIncludes(includes
);
137 includes
.forEach(include
->{
138 include
.setVocabularyDto(parentTerm
.getVocabularyDto());
139 initializeIncludes(include
);
140 include
.setPartOfDto(parentTerm
);
145 public List
<TermVocabularyDto
> findVocabularyDtoByTermType(TermType termType
) {
146 return findVocabularyDtoByTermTypes(Collections
.singleton(termType
));
150 public List
<TermVocabularyDto
> findVocabularyDtoByTermTypes(Set
<TermType
> termTypes
) {
151 return dao
.findVocabularyDtoByTermTypes(termTypes
);
155 public TermVocabularyDto
findVocabularyDtoByVocabularyUuid(UUID vocUuid
) {
156 return dao
.findVocabularyDtoByUuid(vocUuid
);
159 @Transactional(readOnly
= false)
161 public TermDto
addNewTerm(TermType termType
, UUID vocabularyUUID
) {
162 DefinedTermBase term
= termType
.getEmptyDefinedTermBase();
163 termService
.save(term
);
164 TermVocabulary vocabulary
= dao
.load(vocabularyUUID
);
165 vocabulary
.addTerm(term
);
166 dao
.saveOrUpdate(vocabulary
);
167 return TermDto
.fromTerm(term
, true);
171 public <S
extends TermVocabulary
> List
<UuidAndTitleCache
<S
>> getUuidAndTitleCache(Class
<S
> clazz
, TermType termType
,
172 Integer limit
, String pattern
) {
173 return dao
.getUuidAndTitleCache(clazz
, termType
, limit
, pattern
);