Project

General

Profile

Download (7.62 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 Collection<TermDto> getCompleteTermHierarchy(TermVocabularyDto vocabularyDto) {
121
        Collection<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
        return topLevelTerms;
128
    }
129

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

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

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

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

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

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

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

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

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

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

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

    
202

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