cleanup
[cdmlib.git] / cdmlib-services / src / main / java / eu / etaxonomy / cdm / api / service / VocabularyServiceImpl.java
index 5f745d37fe829d86278f260782eb9a54bea2da78..4e077885745c61ab5924d5b7f47fa2999f6e27ad 100644 (file)
@@ -6,11 +6,13 @@
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * See LICENSE.TXT at the top of this package for the full license terms.
 */
-
 package eu.etaxonomy.cdm.api.service;
 
 import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
 import java.util.List;
+import java.util.Set;
 import java.util.UUID;
 
 import org.springframework.beans.factory.annotation.Autowired;
@@ -20,11 +22,15 @@ import org.springframework.transaction.annotation.Transactional;
 import eu.etaxonomy.cdm.api.service.pager.Pager;
 import eu.etaxonomy.cdm.api.service.pager.impl.DefaultPagerImpl;
 import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
-import eu.etaxonomy.cdm.model.common.DefinedTermBase;
+import eu.etaxonomy.cdm.model.common.CdmClass;
 import eu.etaxonomy.cdm.model.common.Language;
-import eu.etaxonomy.cdm.model.common.TermType;
-import eu.etaxonomy.cdm.model.common.TermVocabulary;
-import eu.etaxonomy.cdm.persistence.dao.common.ITermVocabularyDao;
+import eu.etaxonomy.cdm.model.term.DefinedTermBase;
+import eu.etaxonomy.cdm.model.term.TermType;
+import eu.etaxonomy.cdm.model.term.TermVocabulary;
+import eu.etaxonomy.cdm.persistence.dao.term.ITermVocabularyDao;
+import eu.etaxonomy.cdm.persistence.dto.TermDto;
+import eu.etaxonomy.cdm.persistence.dto.TermVocabularyDto;
+import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
 import eu.etaxonomy.cdm.persistence.query.OrderHint;
 import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
 
@@ -32,34 +38,22 @@ import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
 @Transactional(readOnly = true)
 public class VocabularyServiceImpl extends IdentifiableServiceBase<TermVocabulary,ITermVocabularyDao>  implements IVocabularyService {
 
+    @Autowired
+    private ITermService termService;
+
        @Override
     @Autowired
        protected void setDao(ITermVocabularyDao dao) {
                this.dao = dao;
        }
 
-
        @Override
        @Transactional(readOnly = false)
-    public void updateTitleCache(Class<? extends TermVocabulary> clazz, Integer stepSize, IIdentifiableEntityCacheStrategy<TermVocabulary> cacheStrategy, IProgressMonitor monitor) {
+    public UpdateResult updateCaches(Class<? extends TermVocabulary> clazz, Integer stepSize, IIdentifiableEntityCacheStrategy<TermVocabulary> cacheStrategy, IProgressMonitor monitor) {
                if (clazz == null){
                        clazz = TermVocabulary.class;
                }
-               super.updateTitleCacheImpl(clazz, stepSize, cacheStrategy, monitor);
-       }
-
-       @Override
-    @Deprecated
-    public <TERM extends DefinedTermBase> List<TermVocabulary<TERM>> listByTermClass(Class<TERM> clazz, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
-               boolean includeSubclasses = false;
-               boolean includeEmptyVocs = false;
-               return (List)listByTermClass(clazz, includeSubclasses, includeEmptyVocs, limit, start, orderHints, propertyPaths);
-       }
-
-       @Override
-       @Deprecated
-       public <TERM extends DefinedTermBase> List<TermVocabulary<? extends TERM>> listByTermClass(Class<TERM> clazz, boolean includeSubclasses, boolean includeEmptyVocs, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
-               return dao.listByTermClass(clazz, includeSubclasses, includeEmptyVocs,limit, start, orderHints, propertyPaths);
+               return super.updateCachesImpl(clazz, stepSize, cacheStrategy, monitor);
        }
 
     @Override
@@ -69,8 +63,8 @@ public class VocabularyServiceImpl extends IdentifiableServiceBase<TermVocabular
     }
 
     @Override
-       public <T extends DefinedTermBase> List<TermVocabulary<T>> findByTermType(TermType termType) {
-               return dao.findByTermType(termType);
+       public <T extends DefinedTermBase> List<TermVocabulary<T>> findByTermType(TermType termType, List<String> propertyPaths) {
+               return dao.findByTermType(termType, propertyPaths);
        }
        /**
         * (non-Javadoc)
@@ -88,14 +82,123 @@ public class VocabularyServiceImpl extends IdentifiableServiceBase<TermVocabular
 
        @Override
        public Pager<DefinedTermBase> getTerms(TermVocabulary vocabulary, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints,     List<String> propertyPaths) {
-        Integer numberOfResults = dao.countTerms(vocabulary);
+        long numberOfResults = dao.countTerms(vocabulary);
 
-               List<DefinedTermBase> results = new ArrayList<DefinedTermBase>();
+               List<DefinedTermBase> results = new ArrayList<>();
                if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
                        results = dao.getTerms(vocabulary, pageSize, pageNumber,orderHints,propertyPaths);
                }
 
-               return new DefaultPagerImpl<DefinedTermBase>(pageNumber, numberOfResults, pageSize, results);
+               return new DefaultPagerImpl<>(pageNumber, numberOfResults, pageSize, results);
        }
 
+
+    @Override
+    public Collection<TermDto> getTopLevelTerms(UUID vocabularyUuid) {
+        return dao.getTopLevelTerms(vocabularyUuid);
+    }
+
+    @Override
+    public Collection<TermDto> getTerms(UUID vocabularyUuid) {
+        return dao.getTerms(vocabularyUuid);
+    }
+
+    @Override
+    public Collection<TermDto> getTerms(List<UUID> vocabularyUuids) {
+        return dao.getTerms(vocabularyUuids);
+    }
+
+    @Override
+    public Collection<TermDto> getNamedAreaTerms(List<UUID> vocabularyUuids) {
+        return dao.getNamedAreaTerms(vocabularyUuids);
+    }
+
+    @Override
+    public List<TermDto> getCompleteTermHierarchy(TermVocabularyDto vocabularyDto) {
+        List<TermDto> topLevelTerms = dao.getTopLevelTerms(vocabularyDto.getUuid(), vocabularyDto.getTermType());
+        for (TermDto termDto : topLevelTerms) {
+            termDto.setVocabularyDto(vocabularyDto);
+            initializeIncludes(termDto);
+            initializeGeneralizationOf(termDto);
+        }
+
+        return topLevelTerms;
+    }
+
+    private void initializeGeneralizationOf(TermDto parentTerm){
+        Collection<TermDto> generalizationOf = termService.getKindOfsAsDto(parentTerm);
+        parentTerm.setGeneralizationOf(generalizationOf);
+        generalizationOf.forEach(generalization->{
+            generalization.setVocabularyDto(parentTerm.getVocabularyDto());
+            generalization.setKindOfDto(parentTerm);
+            initializeGeneralizationOf(generalization);
+        });
+    }
+
+    private void initializeIncludes(TermDto parentTerm){
+        Collection<TermDto> includes = termService.getIncludesAsDto(parentTerm);
+        parentTerm.setIncludes(includes);
+        includes.forEach(include->{
+            include.setVocabularyDto(parentTerm.getVocabularyDto());
+            initializeIncludes(include);
+            include.setPartOfDto(parentTerm);
+        });
+    }
+
+    @Override
+    public List<TermVocabularyDto> findVocabularyDtoByTermType(TermType termType) {
+        return findVocabularyDtoByTermTypes(Collections.singleton(termType), true);
+    }
+
+    @Override
+    public List<TermVocabularyDto> findVocabularyDtoByTermTypeAndPattern(String pattern, TermType termType) {
+        return dao.findVocabularyDtoByTermTypes(Collections.singleton(termType), pattern, true);
+    }
+
+    @Override
+    public List<TermVocabularyDto> findVocabularyDtoByTermTypes(Set<TermType> termTypes) {
+        return findVocabularyDtoByTermTypes(termTypes, true);
+    }
+
+    @Override
+    public List<TermVocabularyDto> findVocabularyDtoByTermType(TermType termType, boolean includeSubtypes) {
+        return findVocabularyDtoByTermTypes(Collections.singleton(termType), includeSubtypes);
+    }
+
+    @Override
+    public List<TermVocabularyDto> findVocabularyDtoByTermTypes(Set<TermType> termTypes, boolean includeSubtypes) {
+        return dao.findVocabularyDtoByTermTypes(termTypes, includeSubtypes);
+    }
+
+    @Override
+    public List<TermVocabularyDto> findFeatureVocabularyDtoByTermTypes(Set<CdmClass> availableFor) {
+        return dao.findVocabularyDtoByAvailableFor(availableFor);
+    }
+
+    @Override
+    public TermVocabularyDto findVocabularyDtoByVocabularyUuid(UUID vocUuid) {
+        return dao.findVocabularyDtoByUuid(vocUuid);
+    }
+
+    @Transactional(readOnly = false)
+    @Override
+    public TermDto addNewTerm(TermType termType, UUID vocabularyUUID) {
+        DefinedTermBase<?> term = termType.getEmptyDefinedTermBase();
+        termService.save(term);
+        TermVocabulary vocabulary = dao.load(vocabularyUUID);
+        vocabulary.addTerm(term);
+        dao.saveOrUpdate(vocabulary);
+        return TermDto.fromTerm(term, true);
+    }
+
+    @Override
+    public <S extends TermVocabulary> List<UuidAndTitleCache<S>> getUuidAndTitleCache(Class<S> clazz, TermType termType,
+            Integer limit, String pattern) {
+        return dao.getUuidAndTitleCache(clazz, termType, limit, pattern);
+    }
+
+    @Override
+    public List<TermVocabularyDto> findVocabularyDtoByVocabularyUuids(List<UUID> vocUuids) {
+        return dao.findVocabularyDtoByUuids(vocUuids);
+    }
 }