cleanup
[cdmlib.git] / cdmlib-services / src / main / java / eu / etaxonomy / cdm / api / service / VocabularyServiceImpl.java
index 2ac06a597d2fdb958a600d7a3eb049b091b97f98..4e077885745c61ab5924d5b7f47fa2999f6e27ad 100644 (file)
-/**\r
-* Copyright (C) 2009 EDIT\r
-* European Distributed Institute of Taxonomy\r
-* http://www.e-taxonomy.eu\r
-*\r
-* The contents of this file are subject to the Mozilla Public License Version 1.1\r
-* See LICENSE.TXT at the top of this package for the full license terms.\r
-*/ \r
-\r
-package eu.etaxonomy.cdm.api.service;\r
-\r
-import java.util.ArrayList;\r
-import java.util.List;\r
-import java.util.Set;\r
-import java.util.UUID;\r
-\r
-import org.springframework.beans.factory.annotation.Autowired;\r
-import org.springframework.stereotype.Service;\r
-import org.springframework.transaction.annotation.Transactional;\r
-\r
-import eu.etaxonomy.cdm.api.service.pager.Pager;\r
-import eu.etaxonomy.cdm.api.service.pager.impl.DefaultPagerImpl;\r
-import eu.etaxonomy.cdm.model.agent.Address;\r
-import eu.etaxonomy.cdm.model.common.DefinedTermBase;\r
-import eu.etaxonomy.cdm.model.common.Language;\r
-import eu.etaxonomy.cdm.model.common.TermVocabulary;\r
-import eu.etaxonomy.cdm.model.common.VocabularyEnum;\r
-import eu.etaxonomy.cdm.persistence.dao.common.ITermVocabularyDao;\r
-import eu.etaxonomy.cdm.persistence.query.OrderHint;\r
-\r
-@Service\r
-@Transactional(readOnly = true)\r
-public class VocabularyServiceImpl extends IdentifiableServiceBase<TermVocabulary,ITermVocabularyDao>  implements IVocabularyService {\r
-\r
-       @Autowired\r
-       protected void setDao(ITermVocabularyDao dao) {\r
-               this.dao = dao;\r
-       }\r
-\r
-       public void generateTitleCache() {\r
-               // TODO Auto-generated method stub\r
-       }\r
-       \r
-       public TermVocabulary<DefinedTermBase> getVocabulary(VocabularyEnum vocabularyType){\r
-               return dao.findByUuid(vocabularyType.getUuid());\r
-       }\r
-       \r
-       /**\r
-        *  (non-Javadoc)\r
-        * @see eu.etaxonomy.cdm.api.service.ITermService#listVocabularies(java.lang.Class)\r
-        * FIXME candidate for harmonization\r
-        * vocabularyService.list\r
-        */\r
-       public Set<TermVocabulary> listVocabularies(Class termClass) {\r
-               logger.error("Method not implemented yet");\r
-               return null;\r
-       }       \r
-       \r
-       /** \r
-        * (non-Javadoc)\r
-        * @see eu.etaxonomy.cdm.api.service.ITermService#getLanguageVocabulary()\r
-        * FIXME candidate for harmonization\r
-        * is this the same as getVocabulary(VocabularyEnum.Language)\r
-        */\r
-       public TermVocabulary<Language> getLanguageVocabulary() {\r
-               String uuidString = "45ac7043-7f5e-4f37-92f2-3874aaaef2de";\r
-               UUID uuid = UUID.fromString(uuidString);\r
-               TermVocabulary<Language> languageVocabulary = (TermVocabulary)dao.findByUuid(uuid);\r
-               return languageVocabulary;\r
-       }\r
-\r
-       public Pager<DefinedTermBase> getTerms(TermVocabulary vocabulary, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints,     List<String> propertyPaths) {\r
-        Integer numberOfResults = dao.countTerms(vocabulary);\r
-               \r
-               List<DefinedTermBase> results = new ArrayList<DefinedTermBase>();\r
-               if(numberOfResults > 0) { // no point checking again\r
-                       results = dao.getTerms(vocabulary, pageSize, pageNumber,orderHints,propertyPaths); \r
-               }\r
-                       \r
-               return new DefaultPagerImpl<DefinedTermBase>(pageNumber, numberOfResults, pageSize, results);\r
-       }\r
-\r
-}\r
+/**
+* Copyright (C) 2009 EDIT
+* European Distributed Institute of Taxonomy
+* http://www.e-taxonomy.eu
+*
+* 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;
+import org.springframework.stereotype.Service;
+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.CdmClass;
+import eu.etaxonomy.cdm.model.common.Language;
+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;
+
+@Service
+@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 UpdateResult updateCaches(Class<? extends TermVocabulary> clazz, Integer stepSize, IIdentifiableEntityCacheStrategy<TermVocabulary> cacheStrategy, IProgressMonitor monitor) {
+               if (clazz == null){
+                       clazz = TermVocabulary.class;
+               }
+               return super.updateCachesImpl(clazz, stepSize, cacheStrategy, monitor);
+       }
+
+    @Override
+    public List<TermVocabulary> listByTermType(TermType termType, boolean includeSubTypes,
+            Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
+        return dao.listByTermType(termType, includeSubTypes, limit, start, orderHints, propertyPaths);
+    }
+
+    @Override
+       public <T extends DefinedTermBase> List<TermVocabulary<T>> findByTermType(TermType termType, List<String> propertyPaths) {
+               return dao.findByTermType(termType, propertyPaths);
+       }
+       /**
+        * (non-Javadoc)
+        * @see eu.etaxonomy.cdm.api.service.ITermService#getLanguageVocabulary()
+        * FIXME candidate for harmonization
+        * is this the same as getVocabulary(VocabularyEnum.Language)
+        */
+       @Override
+       public TermVocabulary<Language> getLanguageVocabulary() {
+               String uuidString = "45ac7043-7f5e-4f37-92f2-3874aaaef2de";
+               UUID uuid = UUID.fromString(uuidString);
+               TermVocabulary<Language> languageVocabulary = dao.findByUuid(uuid);
+               return languageVocabulary;
+       }
+
+       @Override
+       public Pager<DefinedTermBase> getTerms(TermVocabulary vocabulary, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints,     List<String> propertyPaths) {
+        long numberOfResults = dao.countTerms(vocabulary);
+
+               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<>(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);
+    }
+}