Project

General

Profile

Download (7.61 KB) Statistics
| Branch: | Tag: | Revision:
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

    
10
package eu.etaxonomy.cdm.api.service;
11

    
12
import java.util.ArrayList;
13
import java.util.Collection;
14
import java.util.Collections;
15
import java.util.List;
16
import java.util.Set;
17
import java.util.UUID;
18

    
19
import org.springframework.beans.factory.annotation.Autowired;
20
import org.springframework.stereotype.Service;
21
import org.springframework.transaction.annotation.Transactional;
22

    
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.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.term.ITermVocabularyDao;
32
import eu.etaxonomy.cdm.persistence.dto.TermDto;
33
import eu.etaxonomy.cdm.persistence.dto.TermVocabularyDto;
34
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
35
import eu.etaxonomy.cdm.persistence.query.OrderHint;
36
import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
37

    
38
@Service
39
@Transactional(readOnly = true)
40
public class VocabularyServiceImpl extends IdentifiableServiceBase<TermVocabulary,ITermVocabularyDao>  implements IVocabularyService {
41

    
42
    @Autowired
43
    private ITermService termService;
44

    
45
	@Override
46
    @Autowired
47
	protected void setDao(ITermVocabularyDao dao) {
48
		this.dao = dao;
49
	}
50

    
51

    
52
	@Override
53
	@Transactional(readOnly = false)
54
    public UpdateResult updateCaches(Class<? extends TermVocabulary> clazz, Integer stepSize, IIdentifiableEntityCacheStrategy<TermVocabulary> cacheStrategy, IProgressMonitor monitor) {
55
		if (clazz == null){
56
			clazz = TermVocabulary.class;
57
		}
58
		return super.updateCachesImpl(clazz, stepSize, cacheStrategy, monitor);
59
	}
60

    
61

    
62
    @Override
63
    public List<TermVocabulary> listByTermType(TermType termType, boolean includeSubTypes,
64
            Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
65
        return dao.listByTermType(termType, includeSubTypes, limit, start, orderHints, propertyPaths);
66
    }
67

    
68
    @Override
69
	public <T extends DefinedTermBase> List<TermVocabulary<T>> findByTermType(TermType termType, List<String> propertyPaths) {
70
		return dao.findByTermType(termType, propertyPaths);
71
	}
72
	/**
73
	 * (non-Javadoc)
74
	 * @see eu.etaxonomy.cdm.api.service.ITermService#getLanguageVocabulary()
75
	 * FIXME candidate for harmonization
76
	 * is this the same as getVocabulary(VocabularyEnum.Language)
77
	 */
78
	@Override
79
	public TermVocabulary<Language> getLanguageVocabulary() {
80
		String uuidString = "45ac7043-7f5e-4f37-92f2-3874aaaef2de";
81
		UUID uuid = UUID.fromString(uuidString);
82
		TermVocabulary<Language> languageVocabulary = dao.findByUuid(uuid);
83
		return languageVocabulary;
84
	}
85

    
86
	@Override
87
	public Pager<DefinedTermBase> getTerms(TermVocabulary vocabulary, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints,	List<String> propertyPaths) {
88
        long numberOfResults = dao.countTerms(vocabulary);
89

    
90
		List<DefinedTermBase> results = new ArrayList<>();
91
		if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
92
			results = dao.getTerms(vocabulary, pageSize, pageNumber,orderHints,propertyPaths);
93
		}
94

    
95
		return new DefaultPagerImpl<>(pageNumber, numberOfResults, pageSize, results);
96
	}
97

    
98

    
99
    @Override
100
    public Collection<TermDto> getTopLevelTerms(UUID vocabularyUuid) {
101
        return dao.getTopLevelTerms(vocabularyUuid);
102
    }
103

    
104
    @Override
105
    public Collection<TermDto> getTerms(UUID vocabularyUuid) {
106
        return dao.getTerms(vocabularyUuid);
107
    }
108

    
109
    @Override
110
    public Collection<TermDto> getTerms(List<UUID> vocabularyUuids) {
111
        return dao.getTerms(vocabularyUuids);
112
    }
113

    
114
    @Override
115
    public Collection<TermDto> getNamedAreaTerms(List<UUID> vocabularyUuids) {
116
        return dao.getNamedAreaTerms(vocabularyUuids);
117
    }
118

    
119
    @Override
120
    public List<TermDto> getCompleteTermHierarchy(TermVocabularyDto vocabularyDto) {
121
        List<TermDto> topLevelTerms = dao.getTopLevelTerms(vocabularyDto.getUuid(), vocabularyDto.getTermType());
122
        for (TermDto termDto : topLevelTerms) {
123
            termDto.setVocabularyDto(vocabularyDto);
124
            initializeIncludes(termDto);
125
            initializeGeneralizationOf(termDto);
126
        }
127

    
128
        return topLevelTerms;
129
    }
130

    
131
    private void initializeGeneralizationOf(TermDto parentTerm){
132
        Collection<TermDto> generalizationOf = termService.getKindOfsAsDto(parentTerm);
133
        parentTerm.setGeneralizationOf(generalizationOf);
134
        generalizationOf.forEach(generalization->{
135
            generalization.setVocabularyDto(parentTerm.getVocabularyDto());
136
            generalization.setKindOfDto(parentTerm);
137
            initializeGeneralizationOf(generalization);
138
        });
139
    }
140

    
141
    private void initializeIncludes(TermDto parentTerm){
142
        Collection<TermDto> includes = termService.getIncludesAsDto(parentTerm);
143
        parentTerm.setIncludes(includes);
144
        includes.forEach(include->{
145
            include.setVocabularyDto(parentTerm.getVocabularyDto());
146
            initializeIncludes(include);
147
            include.setPartOfDto(parentTerm);
148
        });
149
    }
150

    
151
    @Override
152
    public List<TermVocabularyDto> findVocabularyDtoByTermType(TermType termType) {
153
        return findVocabularyDtoByTermTypes(Collections.singleton(termType), true);
154
    }
155

    
156
    @Override
157
    public List<TermVocabularyDto> findVocabularyDtoByTermTypeAndPattern(String pattern, TermType termType) {
158
        return dao.findVocabularyDtoByTermTypes(Collections.singleton(termType), pattern, true);
159
    }
160

    
161
    @Override
162
    public List<TermVocabularyDto> findVocabularyDtoByTermTypes(Set<TermType> termTypes) {
163
        return findVocabularyDtoByTermTypes(termTypes, true);
164
    }
165

    
166
    @Override
167
    public List<TermVocabularyDto> findVocabularyDtoByTermType(TermType termType, boolean includeSubtypes) {
168
        return findVocabularyDtoByTermTypes(Collections.singleton(termType), includeSubtypes);
169
    }
170

    
171
    @Override
172
    public List<TermVocabularyDto> findVocabularyDtoByTermTypes(Set<TermType> termTypes, boolean includeSubtypes) {
173
        return dao.findVocabularyDtoByTermTypes(termTypes, includeSubtypes);
174
    }
175

    
176
    @Override
177
    public List<TermVocabularyDto> findFeatureVocabularyDtoByTermTypes(Set<CdmClass> availableFor) {
178
        return dao.findVocabularyDtoByAvailableFor(availableFor);
179
    }
180

    
181
    @Override
182
    public TermVocabularyDto findVocabularyDtoByVocabularyUuid(UUID vocUuid) {
183
        return dao.findVocabularyDtoByUuid(vocUuid);
184
    }
185

    
186
    @Transactional(readOnly = false)
187
    @Override
188
    public TermDto addNewTerm(TermType termType, UUID vocabularyUUID) {
189
        DefinedTermBase term = termType.getEmptyDefinedTermBase();
190
        termService.save(term);
191
        TermVocabulary vocabulary = dao.load(vocabularyUUID);
192
        vocabulary.addTerm(term);
193
        dao.saveOrUpdate(vocabulary);
194
        return TermDto.fromTerm(term, true);
195
    }
196

    
197
    @Override
198
    public <S extends TermVocabulary> List<UuidAndTitleCache<S>> getUuidAndTitleCache(Class<S> clazz, TermType termType,
199
            Integer limit, String pattern) {
200
        return dao.getUuidAndTitleCache(clazz, termType, limit, pattern);
201
    }
202

    
203

    
204
    @Override
205
    public List<TermVocabularyDto> findVocabularyDtoByVocabularyUuids(List<UUID> vocUuids) {
206
        return dao.findVocabularyDtoByUuids(vocUuids);
207
    }
208
}
(97-97/97)