package eu.dnetlib.uoamonitorservice.service; import eu.dnetlib.uoamonitorservice.dao.SubCategoryDAO; import eu.dnetlib.uoamonitorservice.dto.SubCategoryFull; import eu.dnetlib.uoamonitorservice.dto.TopicFull; import eu.dnetlib.uoamonitorservice.entities.Category; import eu.dnetlib.uoamonitorservice.entities.SubCategory; 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.stereotype.Service; import java.util.stream.Collectors; @Service public class SubCategoryService { private final SubCategoryDAO dao; private final CommonService commonService; private final SectionService sectionService; @Autowired public SubCategoryService( CommonService commonService, SubCategoryDAO dao, SectionService sectionService) { this.commonService = commonService; this.dao = dao; this.sectionService = sectionService; } public SubCategory findByPath(Category category, String subcategoryId, String message) { if (!category.getSubCategories().contains(subcategoryId)) { throw new PathNotValidException(message + ": SubCategory with id: " + subcategoryId + " not found in Category: " + category.getId()); } return this.dao.findById(subcategoryId).orElseThrow(() -> new EntityNotFoundException(message + ": SubCategory with id: " + subcategoryId + " not found")); } public SubCategoryFull getFullSubCategory(String type, String alias, String subCategoryId) { SubCategory subCategory = this.find(subCategoryId); if(commonService.hasVisibilityAuthority(type, alias, subCategory)) { return new SubCategoryFull(subCategory, subCategory.getNumbers().stream() .map(sectionId -> this.sectionService.getFullSection(type, alias, sectionId)) .collect(Collectors.toList()), subCategory.getCharts().stream() .map(sectionId -> this.sectionService.getFullSection(type, alias, sectionId)) .collect(Collectors.toList())); } else { return null; } } public SubCategory find(String id) { return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("SubCategory with id: " + id + " not found")); } }