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.Indicator; import eu.dnetlib.uoamonitorservice.entities.Section; import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; 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 SectionFull changeVisibilityTree(String sectionId, Visibility visibility) { Section section = this.find(sectionId); SectionFull sectionFull = new SectionFull(section, section.getIndicators().stream().map(this.indicatorService::find).collect(Collectors.toList())); sectionFull.setIndicators(sectionFull.getIndicators().stream().map(indicator -> { indicator.setVisibility(visibility); return indicatorService.save(indicator); }).collect(Collectors.toList())); return sectionFull; } public SectionFull saveSection(SectionFull sectionFull) { Section section = new Section(sectionFull); if(section.getId() != null) { section.setIndicators(this.getIndicatorsId(section.getId())); } return sectionFull; } 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, String id, Visibility visibility) { if(this.commonService.hasEditAuthority(type, alias)) { SectionFull sectionFull = this.getFullSection(type, alias, id); sectionFull.setIndicators(sectionFull.getIndicators().stream() .map(category -> this.indicatorService.changeVisibility(type, alias, category.getId(), visibility)) .collect(Collectors.toList())); Section section = this.save(new Section(sectionFull)); sectionFull.update(section); return sectionFull; } else { throw new ForbiddenException("Change section visibility: You are not authorized to update section with id: " + id); } } }