package eu.dnetlib.uoamonitorservice.service; import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException; import eu.dnetlib.uoamonitorservice.dao.CategoryDAO; import eu.dnetlib.uoamonitorservice.dao.SubCategoryDAO; import eu.dnetlib.uoamonitorservice.dto.CategoryFull; 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.generics.Common; import eu.dnetlib.uoamonitorservice.generics.SectionGeneric; import eu.dnetlib.uoamonitorservice.generics.SubCategoryGeneric; import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; import eu.dnetlib.uoamonitorservice.primitives.IndicatorType; import eu.dnetlib.uoamonitorservice.primitives.Visibility; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.stream.Collectors; @Service public class SubCategoryService { private final CategoryDAO categoryDAO; private final SubCategoryDAO dao; private final CommonService commonService; private final SectionService sectionService; @Autowired public SubCategoryService(CategoryDAO categoryDAO, SubCategoryDAO dao,CommonService commonService, SectionService sectionService) { this.categoryDAO = categoryDAO; this.dao = dao; this.commonService = commonService; 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")); } public SubCategoryFull buildSubcategory(SubCategoryFull subCategoryFull) { subCategoryFull.setNumbers(subCategoryFull.getNumbers().stream().map(this.sectionService::buildSection).collect(Collectors.toList())); subCategoryFull.setCharts(subCategoryFull.getCharts().stream().map(this.sectionService::buildSection).collect(Collectors.toList())); subCategoryFull.update(this.save(new SubCategory(subCategoryFull))); return subCategoryFull; } public SubCategory save(SubCategory subCategory) { subCategory.getNumbers().forEach(this.sectionService::find); subCategory.getCharts().forEach(this.sectionService::find); return this.dao.save(subCategory); } public void delete(String type, SubCategory subCategory, boolean remove ) { if(this.commonService.hasDeleteAuthority(type)) { this.dao.findByDefaultId(subCategory.getId()).forEach(child -> { this.delete(type, child.getId(), remove); }); subCategory.getNumbers().forEach(sectionId -> { this.sectionService.delete(type, sectionId, false); }); subCategory.getCharts().forEach(sectionId -> { this.sectionService.delete(type, sectionId, false); }); if (remove) { this.removeSubCategory(subCategory.getId()); } this.dao.delete(subCategory); } else { throw new ForbiddenException("Delete subCategory: You are not authorized to delete subCategory with id: " + subCategory.getId()); } } public void delete(String type, String id, boolean remove) { SubCategory subCategory = this.find(id); this.delete(type, subCategory, remove); } public void removeSubCategory(String id) { this.categoryDAO.findBySubCategoriesContaining(id).forEach(category -> { category.getSubCategories().remove(id); this.categoryDAO.save(category); }); } public SubCategoryFull changeVisibility(String type, String alias, String id, Visibility visibility, Boolean propagate) { if(this.commonService.hasEditAuthority(type, alias)) { SubCategoryFull subCategoryFull = this.getFullSubCategory(type, alias, id); subCategoryFull.setVisibility(visibility); if(propagate) { subCategoryFull.setNumbers(subCategoryFull.getNumbers().stream() .map(category -> this.sectionService.changeVisibility(type, alias, category.getId(), visibility)) .collect(Collectors.toList())); subCategoryFull.setCharts(subCategoryFull.getCharts().stream() .map(category -> this.sectionService.changeVisibility(type, alias, category.getId(), visibility)) .collect(Collectors.toList())); } SubCategory subCategory = this.save(new SubCategory(subCategoryFull)); subCategoryFull.update(subCategory); return subCategoryFull; } else { throw new ForbiddenException("Change subCategory visibility: You are not authorized to update subCategory with id: " + id); } } }