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
.List
;
15 import java
.util
.UUID
;
17 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
18 import org
.springframework
.stereotype
.Service
;
19 import org
.springframework
.transaction
.annotation
.Transactional
;
21 import eu
.etaxonomy
.cdm
.api
.service
.pager
.Pager
;
22 import eu
.etaxonomy
.cdm
.api
.service
.pager
.impl
.DefaultPagerImpl
;
23 import eu
.etaxonomy
.cdm
.common
.monitor
.IProgressMonitor
;
24 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
25 import eu
.etaxonomy
.cdm
.model
.term
.DefinedTermBase
;
26 import eu
.etaxonomy
.cdm
.model
.term
.TermType
;
27 import eu
.etaxonomy
.cdm
.model
.term
.TermVocabulary
;
28 import eu
.etaxonomy
.cdm
.persistence
.dao
.term
.ITermVocabularyDao
;
29 import eu
.etaxonomy
.cdm
.persistence
.dto
.TermDto
;
30 import eu
.etaxonomy
.cdm
.persistence
.dto
.TermVocabularyDto
;
31 import eu
.etaxonomy
.cdm
.persistence
.dto
.UuidAndTitleCache
;
32 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
;
33 import eu
.etaxonomy
.cdm
.strategy
.cache
.common
.IIdentifiableEntityCacheStrategy
;
36 @Transactional(readOnly
= true)
37 public class VocabularyServiceImpl
extends IdentifiableServiceBase
<TermVocabulary
,ITermVocabularyDao
> implements IVocabularyService
{
40 private ITermService termService
;
44 protected void setDao(ITermVocabularyDao dao
) {
50 @Transactional(readOnly
= false)
51 public UpdateResult
updateCaches(Class
<?
extends TermVocabulary
> clazz
, Integer stepSize
, IIdentifiableEntityCacheStrategy
<TermVocabulary
> cacheStrategy
, IProgressMonitor monitor
) {
53 clazz
= TermVocabulary
.class;
55 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
> getCompleteTermHierarchy(UUID vocabularyUuid
) {
103 Collection
<TermDto
> topLevelTerms
= dao
.getTopLevelTerms(vocabularyUuid
);
104 for (TermDto termDto
: topLevelTerms
) {
105 initializeIncludes(termDto
);
106 initializeGeneralizationOf(termDto
);
108 return topLevelTerms
;
111 private void initializeGeneralizationOf(TermDto parentTerm
){
112 Collection
<TermDto
> generalizationOf
= termService
.getKindOfsAsDto(parentTerm
);
113 parentTerm
.setGeneralizationOf(generalizationOf
);
114 generalizationOf
.forEach(generalization
->{
115 generalization
.setKindOfDto(parentTerm
);
116 initializeGeneralizationOf(generalization
);
120 private void initializeIncludes(TermDto parentTerm
){
121 Collection
<TermDto
> includes
= termService
.getIncludesAsDto(parentTerm
);
122 parentTerm
.setIncludes(includes
);
123 includes
.forEach(include
->{
124 initializeIncludes(include
);
125 include
.setPartOfDto(parentTerm
);
130 public List
<TermVocabularyDto
> findVocabularyDtoByTermType(TermType termType
) {
131 return dao
.findVocabularyDtoByTermType(termType
);
134 @Transactional(readOnly
= false)
136 public TermDto
addNewTerm(TermType termType
, UUID vocabularyUUID
) {
137 DefinedTermBase term
= termType
.getEmptyDefinedTermBase();
138 termService
.save(term
);
139 TermVocabulary vocabulary
= dao
.load(vocabularyUUID
);
140 vocabulary
.addTerm(term
);
141 dao
.saveOrUpdate(vocabulary
);
142 return TermDto
.fromTerm(term
, true);
146 public <S
extends TermVocabulary
> List
<UuidAndTitleCache
<S
>> getUuidAndTitleCache(Class
<S
> clazz
, TermType termType
,
147 Integer limit
, String pattern
) {
148 return dao
.getUuidAndTitleCache(clazz
, termType
, limit
, pattern
);