ref #10284 adjust service API to allow retrieving terms of a term collection
[cdmlib.git] / cdmlib-services / src / main / java / eu / etaxonomy / cdm / api / service / VocabularyServiceImpl.java
1 /**
2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9 package eu.etaxonomy.cdm.api.service;
10
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.Collections;
14 import java.util.List;
15 import java.util.Set;
16 import java.util.UUID;
17
18 import org.springframework.beans.factory.annotation.Autowired;
19 import org.springframework.stereotype.Service;
20 import org.springframework.transaction.annotation.Transactional;
21
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;
40
41 @Service
42 @Transactional(readOnly = true)
43 public class VocabularyServiceImpl
44 extends IdentifiableServiceBase<TermVocabulary,ITermVocabularyDao>
45 implements IVocabularyService {
46
47 @Autowired
48 private ITermService termService;
49
50 @Autowired
51 private IVocabularyFilterDao vocabularyFilterDao;
52
53 @Autowired
54 private ITermCollectionDao termCollectionDao;
55
56
57 @Override
58 @Autowired
59 protected void setDao(ITermVocabularyDao dao) {
60 this.dao = dao;
61 }
62
63 @Override
64 @Transactional(readOnly = false)
65 public UpdateResult updateCaches(Class<? extends TermVocabulary> clazz, Integer stepSize, IIdentifiableEntityCacheStrategy<TermVocabulary> cacheStrategy, IProgressMonitor monitor) {
66 if (clazz == null){
67 clazz = TermVocabulary.class;
68 }
69 return super.updateCachesImpl(clazz, stepSize, cacheStrategy, monitor);
70 }
71
72 @Override
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);
76 }
77
78 @Override
79 public <T extends DefinedTermBase> List<TermVocabulary<T>> findByTermType(TermType termType, List<String> propertyPaths) {
80 return dao.findByTermType(termType, propertyPaths);
81 }
82
83 @Override
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);
87 }else {
88 List<TermVocabulary<T>> result = new ArrayList<>();
89 for (TermType termType : termTypes) {
90 result.addAll(dao.findByTermType(termType, propertyPaths));
91 }
92 return result;
93 }
94 }
95
96 /**
97 * (non-Javadoc)
98 * @see eu.etaxonomy.cdm.api.service.ITermService#getLanguageVocabulary()
99 * FIXME candidate for harmonization
100 * is this the same as getVocabulary(VocabularyEnum.Language)
101 */
102 @Override
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;
108 }
109
110 @Override
111 public Pager<DefinedTermBase> getTerms(TermVocabulary vocabulary, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
112 long numberOfResults = dao.countTerms(vocabulary);
113
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);
117 }
118
119 return new DefaultPagerImpl<>(pageNumber, numberOfResults, pageSize, results);
120 }
121
122
123 @Override
124 public Collection<TermDto> getTopLevelTerms(UUID vocabularyUuid) {
125 return dao.getTopLevelTerms(vocabularyUuid);
126 }
127
128 @Override
129 public Collection<TermDto> getTerms(UUID vocabularyUuid) {
130 return dao.getTerms(vocabularyUuid);
131 }
132
133 @Override
134 public Collection<TermDto> getTerms(List<UUID> vocabularyUuids) {
135 return dao.getTerms(vocabularyUuids);
136 }
137
138 @Override
139 public Collection<TermDto> getNamedAreaTerms(List<UUID> vocabularyUuids) {
140 return dao.getNamedAreaTerms(vocabularyUuids);
141 }
142
143 @Override
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);
150 }
151
152 return topLevelTerms;
153 }
154
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);
162 });
163 }
164
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);
172 });
173 }
174
175 @Override
176 public List<TermVocabularyDto> findVocabularyDtoByTermType(TermType termType) {
177 return findVocabularyDtoByTermTypes(Collections.singleton(termType), true);
178 }
179
180 @Override
181 public List<TermVocabularyDto> findVocabularyDtoByTermTypeAndPattern(String pattern, TermType termType) {
182 return dao.findVocabularyDtoByTermTypes(Collections.singleton(termType), pattern, true);
183 }
184
185 @Override
186 public List<TermVocabularyDto> findVocabularyDtoByTermTypes(Set<TermType> termTypes) {
187 return findVocabularyDtoByTermTypes(termTypes, true);
188 }
189
190 @Override
191 public List<TermVocabularyDto> findVocabularyDtoByTermType(TermType termType, boolean includeSubtypes) {
192 return findVocabularyDtoByTermTypes(Collections.singleton(termType), includeSubtypes);
193 }
194
195 @Override
196 public List<TermVocabularyDto> findVocabularyDtoByTermTypes(Set<TermType> termTypes, boolean includeSubtypes) {
197 return dao.findVocabularyDtoByTermTypes(termTypes, includeSubtypes);
198 }
199
200 @Override
201 public List<TermVocabularyDto> findFeatureVocabularyDtoByTermTypes(Set<CdmClass> availableFor) {
202 return dao.findVocabularyDtoByAvailableFor(availableFor);
203 }
204
205 @Override
206 public TermVocabularyDto findVocabularyDtoByVocabularyUuid(UUID vocUuid) {
207 return dao.findVocabularyDtoByUuid(vocUuid);
208 }
209
210 @Transactional(readOnly = false)
211 @Override
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);
219 }
220
221 @Override
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);
225 }
226
227 @Override
228 public List<TermVocabularyDto> findVocabularyDtoByVocabularyUuids(List<UUID> vocUuids) {
229 return dao.findVocabularyDtoByUuids(vocUuids);
230 }
231
232 @Override
233 public List<UUID> uuidList(VocabularyFilter filter){
234 return vocabularyFilterDao.listUuids(filter);
235 }
236
237 @Override
238 public List<Integer> idList(VocabularyFilter filter){
239 return vocabularyFilterDao.idList(filter);
240 }
241
242 @Override
243 public long count(VocabularyFilter filter) {
244 return vocabularyFilterDao.count(filter);
245 }
246
247 @Override
248 public TermCollection<?,?> findCollection(UUID termCollectionUuid){
249 return termCollectionDao.findByUuid(termCollectionUuid);
250 }
251 }