8a9426d2e68942d3c4b8b867f6bdb6400d72c03e
[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
10 package eu.etaxonomy.cdm.api.service;
11
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.List;
15 import java.util.UUID;
16
17 import org.springframework.beans.factory.annotation.Autowired;
18 import org.springframework.stereotype.Service;
19 import org.springframework.transaction.annotation.Transactional;
20
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.DefinedTermBase;
25 import eu.etaxonomy.cdm.model.common.Language;
26 import eu.etaxonomy.cdm.model.common.TermType;
27 import eu.etaxonomy.cdm.model.common.TermVocabulary;
28 import eu.etaxonomy.cdm.persistence.dao.common.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;
34
35 @Service
36 @Transactional(readOnly = true)
37 public class VocabularyServiceImpl extends IdentifiableServiceBase<TermVocabulary,ITermVocabularyDao> implements IVocabularyService {
38
39 @Autowired
40 private ITermService termService;
41
42 @Override
43 @Autowired
44 protected void setDao(ITermVocabularyDao dao) {
45 this.dao = dao;
46 }
47
48
49 @Override
50 @Transactional(readOnly = false)
51 public void updateCaches(Class<? extends TermVocabulary> clazz, Integer stepSize, IIdentifiableEntityCacheStrategy<TermVocabulary> cacheStrategy, IProgressMonitor monitor) {
52 if (clazz == null){
53 clazz = TermVocabulary.class;
54 }
55 super.updateCachesImpl(clazz, stepSize, cacheStrategy, monitor);
56 }
57
58
59 @Override
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);
63 }
64
65 @Override
66 public <T extends DefinedTermBase> List<TermVocabulary<T>> findByTermType(TermType termType, List<String> propertyPaths) {
67 return dao.findByTermType(termType, propertyPaths);
68 }
69 /**
70 * (non-Javadoc)
71 * @see eu.etaxonomy.cdm.api.service.ITermService#getLanguageVocabulary()
72 * FIXME candidate for harmonization
73 * is this the same as getVocabulary(VocabularyEnum.Language)
74 */
75 @Override
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;
81 }
82
83 @Override
84 public Pager<DefinedTermBase> getTerms(TermVocabulary vocabulary, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
85 long numberOfResults = dao.countTerms(vocabulary);
86
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);
90 }
91
92 return new DefaultPagerImpl<>(pageNumber, numberOfResults, pageSize, results);
93 }
94
95
96 @Override
97 public Collection<TermDto> getTopLevelTerms(UUID vocabularyUuid) {
98 return dao.getTopLevelTerms(vocabularyUuid);
99 }
100
101 @Override
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);
107 }
108 return topLevelTerms;
109 }
110
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);
117 });
118 }
119
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);
126 });
127 }
128
129 @Override
130 public List<TermVocabularyDto> findVocabularyDtoByTermType(TermType termType) {
131 return dao.findVocabularyDtoByTermType(termType);
132 }
133
134 @Transactional(readOnly = false)
135 @Override
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);
143 }
144
145 @Override
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);
149 }
150 }