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.TermType;
|
30
|
import eu.etaxonomy.cdm.model.term.TermVocabulary;
|
31
|
import eu.etaxonomy.cdm.persistence.dao.description.IVocabularyFilterDao;
|
32
|
import eu.etaxonomy.cdm.persistence.dao.term.ITermVocabularyDao;
|
33
|
import eu.etaxonomy.cdm.persistence.dto.TermDto;
|
34
|
import eu.etaxonomy.cdm.persistence.dto.TermVocabularyDto;
|
35
|
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
|
36
|
import eu.etaxonomy.cdm.persistence.query.OrderHint;
|
37
|
import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
|
38
|
|
39
|
@Service
|
40
|
@Transactional(readOnly = true)
|
41
|
public class VocabularyServiceImpl extends IdentifiableServiceBase<TermVocabulary,ITermVocabularyDao> implements IVocabularyService {
|
42
|
|
43
|
@Autowired
|
44
|
private ITermService termService;
|
45
|
|
46
|
@Autowired
|
47
|
private IVocabularyFilterDao vocabularyFilterDao;
|
48
|
|
49
|
@Override
|
50
|
@Autowired
|
51
|
protected void setDao(ITermVocabularyDao dao) {
|
52
|
this.dao = dao;
|
53
|
}
|
54
|
|
55
|
@Override
|
56
|
@Transactional(readOnly = false)
|
57
|
public UpdateResult updateCaches(Class<? extends TermVocabulary> clazz, Integer stepSize, IIdentifiableEntityCacheStrategy<TermVocabulary> cacheStrategy, IProgressMonitor monitor) {
|
58
|
if (clazz == null){
|
59
|
clazz = TermVocabulary.class;
|
60
|
}
|
61
|
return super.updateCachesImpl(clazz, stepSize, cacheStrategy, monitor);
|
62
|
}
|
63
|
|
64
|
@Override
|
65
|
public List<TermVocabulary> listByTermType(TermType termType, boolean includeSubTypes,
|
66
|
Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
|
67
|
return dao.listByTermType(termType, includeSubTypes, limit, start, orderHints, propertyPaths);
|
68
|
}
|
69
|
|
70
|
@Override
|
71
|
public <T extends DefinedTermBase> List<TermVocabulary<T>> findByTermType(TermType termType, List<String> propertyPaths) {
|
72
|
return dao.findByTermType(termType, propertyPaths);
|
73
|
}
|
74
|
/**
|
75
|
* (non-Javadoc)
|
76
|
* @see eu.etaxonomy.cdm.api.service.ITermService#getLanguageVocabulary()
|
77
|
* FIXME candidate for harmonization
|
78
|
* is this the same as getVocabulary(VocabularyEnum.Language)
|
79
|
*/
|
80
|
@Override
|
81
|
public TermVocabulary<Language> getLanguageVocabulary() {
|
82
|
String uuidString = "45ac7043-7f5e-4f37-92f2-3874aaaef2de";
|
83
|
UUID uuid = UUID.fromString(uuidString);
|
84
|
TermVocabulary<Language> languageVocabulary = dao.findByUuid(uuid);
|
85
|
return languageVocabulary;
|
86
|
}
|
87
|
|
88
|
@Override
|
89
|
public Pager<DefinedTermBase> getTerms(TermVocabulary vocabulary, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
|
90
|
long numberOfResults = dao.countTerms(vocabulary);
|
91
|
|
92
|
List<DefinedTermBase> results = new ArrayList<>();
|
93
|
if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
|
94
|
results = dao.getTerms(vocabulary, pageSize, pageNumber,orderHints,propertyPaths);
|
95
|
}
|
96
|
|
97
|
return new DefaultPagerImpl<>(pageNumber, numberOfResults, pageSize, results);
|
98
|
}
|
99
|
|
100
|
|
101
|
@Override
|
102
|
public Collection<TermDto> getTopLevelTerms(UUID vocabularyUuid) {
|
103
|
return dao.getTopLevelTerms(vocabularyUuid);
|
104
|
}
|
105
|
|
106
|
@Override
|
107
|
public Collection<TermDto> getTerms(UUID vocabularyUuid) {
|
108
|
return dao.getTerms(vocabularyUuid);
|
109
|
}
|
110
|
|
111
|
@Override
|
112
|
public Collection<TermDto> getTerms(List<UUID> vocabularyUuids) {
|
113
|
return dao.getTerms(vocabularyUuids);
|
114
|
}
|
115
|
|
116
|
@Override
|
117
|
public Collection<TermDto> getNamedAreaTerms(List<UUID> vocabularyUuids) {
|
118
|
return dao.getNamedAreaTerms(vocabularyUuids);
|
119
|
}
|
120
|
|
121
|
@Override
|
122
|
public List<TermDto> getCompleteTermHierarchy(TermVocabularyDto vocabularyDto) {
|
123
|
List<TermDto> topLevelTerms = dao.getTopLevelTerms(vocabularyDto.getUuid(), vocabularyDto.getTermType());
|
124
|
for (TermDto termDto : topLevelTerms) {
|
125
|
termDto.setVocabularyDto(vocabularyDto);
|
126
|
initializeIncludes(termDto);
|
127
|
initializeGeneralizationOf(termDto);
|
128
|
}
|
129
|
|
130
|
return topLevelTerms;
|
131
|
}
|
132
|
|
133
|
private void initializeGeneralizationOf(TermDto parentTerm){
|
134
|
Collection<TermDto> generalizationOf = termService.getKindOfsAsDto(parentTerm);
|
135
|
parentTerm.setGeneralizationOf(generalizationOf);
|
136
|
generalizationOf.forEach(generalization->{
|
137
|
generalization.setVocabularyDto(parentTerm.getVocabularyDto());
|
138
|
generalization.setKindOfDto(parentTerm);
|
139
|
initializeGeneralizationOf(generalization);
|
140
|
});
|
141
|
}
|
142
|
|
143
|
private void initializeIncludes(TermDto parentTerm){
|
144
|
Collection<TermDto> includes = termService.getIncludesAsDto(parentTerm);
|
145
|
parentTerm.setIncludes(includes);
|
146
|
includes.forEach(include->{
|
147
|
include.setVocabularyDto(parentTerm.getVocabularyDto());
|
148
|
initializeIncludes(include);
|
149
|
include.setPartOfDto(parentTerm);
|
150
|
});
|
151
|
}
|
152
|
|
153
|
@Override
|
154
|
public List<TermVocabularyDto> findVocabularyDtoByTermType(TermType termType) {
|
155
|
return findVocabularyDtoByTermTypes(Collections.singleton(termType), true);
|
156
|
}
|
157
|
|
158
|
@Override
|
159
|
public List<TermVocabularyDto> findVocabularyDtoByTermTypeAndPattern(String pattern, TermType termType) {
|
160
|
return dao.findVocabularyDtoByTermTypes(Collections.singleton(termType), pattern, true);
|
161
|
}
|
162
|
|
163
|
@Override
|
164
|
public List<TermVocabularyDto> findVocabularyDtoByTermTypes(Set<TermType> termTypes) {
|
165
|
return findVocabularyDtoByTermTypes(termTypes, true);
|
166
|
}
|
167
|
|
168
|
@Override
|
169
|
public List<TermVocabularyDto> findVocabularyDtoByTermType(TermType termType, boolean includeSubtypes) {
|
170
|
return findVocabularyDtoByTermTypes(Collections.singleton(termType), includeSubtypes);
|
171
|
}
|
172
|
|
173
|
@Override
|
174
|
public List<TermVocabularyDto> findVocabularyDtoByTermTypes(Set<TermType> termTypes, boolean includeSubtypes) {
|
175
|
return dao.findVocabularyDtoByTermTypes(termTypes, includeSubtypes);
|
176
|
}
|
177
|
|
178
|
@Override
|
179
|
public List<TermVocabularyDto> findFeatureVocabularyDtoByTermTypes(Set<CdmClass> availableFor) {
|
180
|
return dao.findVocabularyDtoByAvailableFor(availableFor);
|
181
|
}
|
182
|
|
183
|
@Override
|
184
|
public TermVocabularyDto findVocabularyDtoByVocabularyUuid(UUID vocUuid) {
|
185
|
return dao.findVocabularyDtoByUuid(vocUuid);
|
186
|
}
|
187
|
|
188
|
@Transactional(readOnly = false)
|
189
|
@Override
|
190
|
public TermDto addNewTerm(TermType termType, UUID vocabularyUUID, Language lang) {
|
191
|
DefinedTermBase<?> term = termType.getEmptyDefinedTermBase(lang);
|
192
|
termService.save(term);
|
193
|
TermVocabulary vocabulary = dao.load(vocabularyUUID);
|
194
|
vocabulary.addTerm(term);
|
195
|
dao.saveOrUpdate(vocabulary);
|
196
|
return TermDto.fromTerm(term, true);
|
197
|
}
|
198
|
|
199
|
@Override
|
200
|
public <S extends TermVocabulary> List<UuidAndTitleCache<S>> getUuidAndTitleCache(Class<S> clazz, TermType termType,
|
201
|
Integer limit, String pattern) {
|
202
|
return dao.getUuidAndTitleCache(clazz, termType, limit, pattern);
|
203
|
}
|
204
|
|
205
|
@Override
|
206
|
public List<TermVocabularyDto> findVocabularyDtoByVocabularyUuids(List<UUID> vocUuids) {
|
207
|
return dao.findVocabularyDtoByUuids(vocUuids);
|
208
|
}
|
209
|
|
210
|
@Override
|
211
|
public List<UUID> uuidList(VocabularyFilter filter){
|
212
|
return vocabularyFilterDao.listUuids(filter);
|
213
|
}
|
214
|
|
215
|
@Override
|
216
|
public List<Integer> idList(VocabularyFilter filter){
|
217
|
return vocabularyFilterDao.idList(filter);
|
218
|
}
|
219
|
|
220
|
@Override
|
221
|
public long count(VocabularyFilter filter) {
|
222
|
return vocabularyFilterDao.count(filter);
|
223
|
}
|
224
|
}
|