/* package eu.dnetlib.uoamonitorservice.controllers; import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils; import eu.dnetlib.uoamonitorservice.dao.*; import eu.dnetlib.uoamonitorservice.dto.SectionFull; import eu.dnetlib.uoamonitorservice.entities.*; import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException; import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; import eu.dnetlib.uoamonitorservice.primitives.Visibility; import eu.dnetlib.uoamonitorservice.service.SectionService; import eu.dnetlib.uoamonitorservice.service.SubCategoryService; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; import java.util.ArrayList; import java.util.Date; import java.util.List; @RestController @CrossOrigin(origins = "*") public class SectionController { private final Logger log = LogManager.getLogger(this.getClass()); private SectionService service; @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/save/{index}", method = RequestMethod.POST) public SectionFull saveSection(@PathVariable("stakeholderId") String stakeholderId, @PathVariable("topicId") String topicId, @PathVariable("categoryId") String categoryId, @PathVariable("subcategoryId") String subcategoryId, @PathVariable("index") String index, @RequestBody SectionFull sectionFull) { log.debug("save section"); log.debug("Name: "+sectionFull.getTitle() + " - Id: "+sectionFull.getId() + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId); SubCategory subCategory = this.subCategoryService.checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId, "Save Section"); Section section = new Section(sectionFull); Date date = new Date(); section.setUpdateDate(date); sectionFull.setUpdateDate(date); List indicators = new ArrayList<>(); Section oldSection = null; if(sectionFull.getId() != null) { oldSection = sectionDAO.findById(sectionFull.getId()); if(oldSection == null) { // EXCEPTION - Section not found throw new EntityNotFoundException("save section: Section with id: " + sectionFull.getId() + " not found"); } for(String indicatorId : oldSection.getIndicators()) { Indicator indicator = indicatorDAO.findById(indicatorId); if (indicator == null) { // EXCEPTION - Indicator not found throw new EntityNotFoundException("Save section: Indicator with id: "+indicatorId+" not found (indicator exists in section: "+section.getId()+")"); } indicators.add(indicator.getId()); } } else { // section does not exist in DB section.setCreationDate(date); sectionFull.setCreationDate(date); for(Indicator indicator : sectionFull.getIndicators()) { indicators.add(indicator.getId()); } } String sectionId = sectionFull.getId(); section.setIndicators(indicators); Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); // this section belongs in default profile and it is new or it is updated if(stakeholder.getDefaultId() == null) { if(sectionId == null) { sectionDAO.save(section); onSaveDefaultSection(section, topicId, categoryId, subcategoryId, stakeholder); } else { onUpdateDefaultSection(section, stakeholder, oldSection); sectionDAO.save(section); } } else { sectionDAO.save(section); } List sections = null; if(sectionFull.getType().equals("chart")) { sections = subCategory.getCharts(); } else if(sectionFull.getType().equals("number")) { sections = subCategory.getNumbers(); } int existing_index = sections.indexOf(section.getId()); if (existing_index == -1) { if(Integer.parseInt(index) != -1) { sections.add(Integer.parseInt(index), section.getId()); } else { sections.add(section.getId()); } subCategoryDAO.save(subCategory); log.debug("Section saved!"); sectionFull.setId(section.getId()); } return sectionFull; } public void onSaveDefaultSection(Section section, String defaultTopicId, String defaultCategoryId, String defaultSubcategoryId, Stakeholder defaultStakeholder) { log.debug("On save default section"); // new section in default profile - add it on profiles of the same type List subCategories = subCategoryDAO.findByDefaultId(defaultSubcategoryId); for (SubCategory subCategory : subCategories) { Category parentCategory = categoryDAO.findBySubCategoriesContaining(subCategory.getId()); Topic parentTopic = topicDAO.findByCategoriesContaining(parentCategory.getId()); Stakeholder parentStakeholder = stakeholderDAO.findByTopicsContaining(parentTopic.getId()); Section sectionNew = new Section(); sectionNew.copyFromDefault(section); sectionNew.setStakeholderAlias(parentStakeholder.getAlias()); sectionDAO.save(sectionNew); List sections = null; if (section.getType().equals("chart")) { sections = subCategory.getCharts(); } else if (section.getType().equals("number")) { sections = subCategory.getNumbers(); } sections.add(sectionNew.getId()); subCategoryDAO.save(subCategory); } } public void onUpdateDefaultSection(Section section, Stakeholder stakeholder, Section oldSection) { log.debug("On update default section"); } private SubCategory checkForExceptions(String stakeholderId, String topicId, String categoryId, String subcategoryId) { Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId).orElseThrow(() -> new EntityNotFoundException("Save indicator: Stakeholder with id: " + stakeholderId + " not found")); if(!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { // EXCEPTION - Access denied throw new ForbiddenException("CheckForExceptions Section: You are not authorized to update stakeholder with id: "+stakeholderId); } Topic topic = topicDAO.findById(topicId).orElseThrow(() -> new EntityNotFoundException("Save indicator: Topic with id: "+topicId+" not found")); if(!stakeholder.getTopics().contains(topicId)) { // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias(); throw new PathNotValidException("Save indicator: Topic with id: " + topicId + " not found in Stakeholder: " + stakeholderId); } Category category = categoryDAO.findById(categoryId); if(category == null) { // EXCEPTION - Category not found throw new EntityNotFoundException("Save indicator: Category with id: "+categoryId+" not found"); } if(!topic.getCategories().contains(categoryId)) { // EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); throw new PathNotValidException("Save indicator: Category with id: "+categoryId+" not found in Topic: "+topicId); } SubCategory subcategory = subCategoryDAO.findById(subcategoryId); if(subcategory == null) { // EXCEPTION - SubCategory not found throw new EntityNotFoundException("Save indicator: SubCategory with id: "+subcategoryId+" not found"); } if (!category.getSubCategories().contains(subcategoryId)) { // EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); throw new PathNotValidException("Save indicator: SubCategory with id: "+subcategoryId+" not found in Category: "+categoryId); } return subcategory; } public void deleteTree(SubCategory subCategory) { List sections = subCategory.getCharts(); for(String sectionId : sections) { Section section = sectionDAO.findById(sectionId); if (section == null) { // EXCEPTION - Section not found throw new EntityNotFoundException("Section delete tree: Chart Section with id: "+sectionId+" not found (section exists in subCategory: "+subCategory.getId()+")"); } indicatorController.deleteTree(section); sectionDAO.delete(sectionId); } sections = subCategory.getNumbers(); for(String sectionId : sections) { Section section = sectionDAO.findById(sectionId); if (section == null) { // EXCEPTION - Section not found throw new EntityNotFoundException("Section delete tree: Number Section with id: "+sectionId+" not found (section exists in subCategory: "+subCategory.getId()+")"); } indicatorController.deleteTree(section); sectionDAO.delete(sectionId); } } public void disConnectTree(SubCategory subCategory) { List sections = subCategory.getCharts(); for(String sectionId : sections) { Section section = sectionDAO.findById(sectionId); if (section == null) { // EXCEPTION - Section not found throw new EntityNotFoundException("Section disconnect tree: Chart Section with id: "+sectionId+" not found (section exists in subCategory: "+subCategory.getId()+")"); } indicatorController.disConnectTree(section); section.setDefaultId(null); sectionDAO.save(section); } sections = subCategory.getNumbers(); for(String sectionId : sections) { Section section = sectionDAO.findById(sectionId); if (section == null) { // EXCEPTION - Section not found throw new EntityNotFoundException("Section disconnect tree: Number Section with id: "+sectionId+" not found (section exists in subCategory: "+subCategory.getId()+")"); } indicatorController.disConnectTree(section); section.setDefaultId(null); sectionDAO.save(section); } } } */