package eu.dnetlib.uoamonitorservice.service; import eu.dnetlib.uoamonitorservice.dao.CategoryDAO; import eu.dnetlib.uoamonitorservice.dto.CategoryFull; import eu.dnetlib.uoamonitorservice.dto.TopicFull; import eu.dnetlib.uoamonitorservice.entities.Category; import eu.dnetlib.uoamonitorservice.entities.Topic; import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.method.P; import org.springframework.stereotype.Service; import java.util.stream.Collectors; @Service public class CategoryService { private final CategoryDAO dao; private final SubCategoryService subCategoryService; private final CommonService commonService; @Autowired public CategoryService(SubCategoryService subCategoryService, CommonService commonService, CategoryDAO dao) { this.subCategoryService = subCategoryService; this.commonService = commonService; this.dao = dao; } public Category findByPath(Topic topic, String categoryId, String message) { if (!topic.getCategories().contains(categoryId)) { throw new PathNotValidException(message + ": Category with id: " + categoryId + " not found in Topic: " + topic.getId()); } return this.dao.findById(categoryId).orElseThrow(() -> new EntityNotFoundException(message + ": Category with id: " + categoryId + " not found")); } public CategoryFull getFullCategory(String type, String alias, String categoryId) { Category category = this.find(categoryId); if(commonService.hasVisibilityAuthority(type, alias, category)) { return new CategoryFull(category, category.getSubCategories().stream() .map(subCategoryId -> this.subCategoryService.getFullSubCategory(type, alias, subCategoryId)) .collect(Collectors.toList())); } else { return null; } } public Category find(String id) { return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("Category with id: " + id + " not found")); } }