package eu.dnetlib.uoamonitorservice.service; import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException; import eu.dnetlib.uoamonitorservice.dao.SectionDAO; import eu.dnetlib.uoamonitorservice.dao.SubCategoryDAO; import eu.dnetlib.uoamonitorservice.dto.SectionFull; import eu.dnetlib.uoamonitorservice.entities.Category; import eu.dnetlib.uoamonitorservice.entities.Indicator; import eu.dnetlib.uoamonitorservice.entities.Section; import eu.dnetlib.uoamonitorservice.entities.SubCategory; import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; import eu.dnetlib.uoamonitorservice.primitives.Visibility; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; import java.util.stream.Collectors; @Service public class SectionService { private final SubCategoryDAO subCategoryDAO; private final SectionDAO dao; private final IndicatorService indicatorService; private final CommonService commonService; @Autowired public SectionService(SubCategoryDAO subCategoryDAO, SectionDAO dao, IndicatorService indicatorService, CommonService commonService) { this.subCategoryDAO = subCategoryDAO; this.dao = dao; this.indicatorService = indicatorService; this.commonService = commonService; } public Section findByPath(SubCategory subCategory, String sectionId) { if (!subCategory.getNumbers().contains(sectionId) || !subCategory.getCharts().contains(sectionId)) { throw new PathNotValidException("Section with id: " + sectionId + " not found in SubCategory: " + subCategory.getId()); } return this.dao.findById(sectionId).orElseThrow(() -> new EntityNotFoundException("Section with id: " + sectionId + " not found")); } public Section find(String id) { return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("Section with id: " + id + " not found")); } public SectionFull getFullSection(String type, String alias, String id) { Section section = this.find(id); return new SectionFull(section, section.getIndicators().stream() .map(indicatorId -> this.indicatorService.getIndicator(type, alias, indicatorId)) .collect(Collectors.toList())); } public List getIndicators(String id) { Section section = this.find(id); return section.getIndicators().stream().map(this.indicatorService::find).collect(Collectors.toList()); } public List getIndicatorsId(String id) { return this.getIndicators(id).stream().map(Indicator::getId).collect(Collectors.toList()); } public SectionFull buildSection(SectionFull sectionFull) { sectionFull.setIndicators(sectionFull.getIndicators().stream().map(this.indicatorService::save).collect(Collectors.toList())); Section section = this.save(new Section(sectionFull)); return new SectionFull(section, sectionFull.getIndicators()); } public Section save(Section section) { section.getIndicators().forEach(this.indicatorService::find); return this.dao.save(section); } public void delete(String type, Section section, boolean remove) { if(this.commonService.hasDeleteAuthority(type)) { this.dao.findByDefaultId(section.getId()).forEach(child -> { this.delete(type, child.getId(), remove); }); section.getIndicators().forEach(indicatorId -> { this.indicatorService.delete(type, indicatorId, false); }); if (remove) { this.removeSection(section.getId()); } this.dao.delete(section); } else { throw new ForbiddenException("Delete section: You are not authorized to delete section with id: " + section.getId()); } } public void delete(String type, String id, boolean remove) { Section section = this.find(id); this.delete(type, section, remove); } public void removeSection(String id) { this.subCategoryDAO.findByNumbersContaining(id).forEach(subCategory -> { subCategory.getNumbers().remove(id); this.subCategoryDAO.save(subCategory); }); this.subCategoryDAO.findByChartsContaining(id).forEach(subCategory -> { subCategory.getCharts().remove(id); this.subCategoryDAO.save(subCategory); }); } public SectionFull changeVisibility(String type, String alias, SectionFull section, Visibility visibility) { if(this.commonService.hasEditAuthority(type, alias)) { section.setIndicators(section.getIndicators().stream() .map(indicator -> this.indicatorService.changeVisibility(type, alias, indicator, visibility)) .collect(Collectors.toList())); section.update(this.save(new Section(section))); return section; } else { throw new ForbiddenException("Change section visibility: You are not authorized to update section with id: " + section.getId()); } } public SectionFull changeVisibility(String type, String alias, String id, Visibility visibility) { SectionFull sectionFull = this.getFullSection(type, alias, id); return this.changeVisibility(type, alias, sectionFull, visibility); } }