Fix delete and change visibility methods for all entities.

This commit is contained in:
Konstantinos Triantafyllou 2024-03-14 19:01:28 +02:00
parent cb7780b84e
commit cf505bd214
11 changed files with 336 additions and 986 deletions

View File

@ -1,77 +1,36 @@
/*
package eu.dnetlib.uoamonitorservice.controllers;
import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException;
import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils;
import eu.dnetlib.uoamonitorservice.dao.CategoryDAO;
import eu.dnetlib.uoamonitorservice.dao.StakeholderDAO;
import eu.dnetlib.uoamonitorservice.dao.SubCategoryDAO;
import eu.dnetlib.uoamonitorservice.dao.TopicDAO;
import eu.dnetlib.uoamonitorservice.entities.*;
import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException;
import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException;
import eu.dnetlib.uoamonitorservice.dto.CategoryFull;
import eu.dnetlib.uoamonitorservice.entities.Category;
import eu.dnetlib.uoamonitorservice.entities.Stakeholder;
import eu.dnetlib.uoamonitorservice.entities.Topic;
import eu.dnetlib.uoamonitorservice.primitives.Visibility;
import eu.dnetlib.uoamonitorservice.service.CategoryService;
import eu.dnetlib.uoamonitorservice.service.StakeholderService;
import eu.dnetlib.uoamonitorservice.service.TopicService;
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.Iterator;
import java.util.List;
@RestController
@CrossOrigin(origins = "*")
public class CategoryController {
private final Logger log = LogManager.getLogger(this.getClass());
@Autowired
private RolesUtils rolesUtils;
private final StakeholderService stakeholderService;
private final TopicService topicService;
private final CategoryService categoryService;
@Autowired
private StakeholderDAO stakeholderDAO;
@Autowired
private TopicDAO topicDAO;
@Autowired
private CategoryDAO categoryDAO;
@Autowired
private SubCategoryDAO subCategoryDAO;
@Autowired
private SubCategoryController subCategoryController;
public Category<SubCategory> buildCategory(Category<SubCategory> categoryFull) {
Category<String> category = new Category<>(categoryFull);
List<String> subCategories = new ArrayList<>();
List<SubCategory> subCategoriesFull = new ArrayList<>();
for (SubCategory<Section<Indicator>> subCategory : categoryFull.getSubCategories()) {
SubCategory<Section<Indicator>> subcategoryFull = subCategoryController.buildSubCategory(subCategory);
subCategoriesFull.add(subcategoryFull);
subCategories.add(subcategoryFull.getId());
}
categoryFull.setSubCategories(subCategoriesFull);
category.setSubCategories(subCategories);
Date date = new Date();
category.setCreationDate(date);
category.setUpdateDate(date);
categoryFull.setCreationDate(date);
categoryFull.setUpdateDate(date);
categoryDAO.save(category);
categoryFull.setId(category.getId());
return categoryFull;
public CategoryController(StakeholderService stakeholderService, TopicService topicService, CategoryService categoryService) {
this.stakeholderService = stakeholderService;
this.topicService = topicService;
this.categoryService = categoryService;
}
@PreAuthorize("isAuthenticated()")
/*@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/save", method = RequestMethod.POST)
public Category<SubCategory> saveCategory(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@ -170,9 +129,9 @@ public class CategoryController {
throw new EntityNotFoundException("Save category: Stakeholder with id: " + stakeholderId + " not found");
}
return categoryFull;
}
}*/
public void onSaveDefaultCategory(Category<String> category, String topicId) {
/*public void onSaveDefaultCategory(Category<String> category, String topicId) {
log.debug("On save default category");
List<Topic> topics = topicDAO.findByDefaultId(topicId);
@ -190,9 +149,9 @@ public class CategoryController {
String subCategoryOverviewId = category.getSubCategories().get(0);
SubCategory subCategoryOverview = subCategoryDAO.findById(subCategoryOverviewId);
subCategoryController.onSaveDefaultSubCategory(subCategoryOverview, category.getId());
}
}*/
public void onUpdateDefaultCategory(Category category, Category oldCategory) {
/*public void onUpdateDefaultCategory(Category category, Category oldCategory) {
log.debug("On update default category");
List<Category> categories = categoryDAO.findByDefaultId(category.getId());
@ -218,7 +177,7 @@ public class CategoryController {
categoryBasedOnDefault.setUpdateDate(category.getUpdateDate());
categoryDAO.save(categoryBasedOnDefault);
}
}
}*/
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/delete", method = RequestMethod.DELETE)
@ -228,108 +187,14 @@ public class CategoryController {
@RequestParam(required = false) String children) {
log.debug("delete category");
log.debug("Id: " + categoryId + " - Stakeholder: " + stakeholderId + " - Topic: " + topicId);
Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId);
if (stakeholder != null) {
if (!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) {
// EXCEPTION - Access denied
throw new ForbiddenException("Delete category: You are not authorized to update stakeholder with id: " + stakeholderId);
}
Topic<String> topic = topicDAO.findById(topicId);
if (topic != null) {
if (stakeholder.getTopics().contains(topicId)) {
Category<String> category = categoryDAO.findById(categoryId);
if (category != null) {
if (category.getDefaultId() != null && !rolesUtils.hasCreateAndDeleteAuthority(stakeholder.getType())) {
// EXCEPTION - Access denied
throw new ForbiddenException("Delete category: You are not authorized to delete a default Category in stakeholder with id: " + stakeholderId);
}
List<String> categories = topic.getCategories();
int index = categories.indexOf(categoryId);
if (index != -1) {
// this category belongs in default profile
if (topic.getDefaultId() == null && children != null) {
onDeleteDefaultCategory(categoryId, topicId, children);
}
subCategoryController.deleteTree(category);
category.setSubCategories(null);
categories.remove(index);
topicDAO.save(topic);
categoryDAO.delete(categoryId);
log.debug("Category deleted!");
} else {
// EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
throw new PathNotValidException("Delete category: Category with id: " + categoryId + " not found in Topic: " + topicId);
}
} else {
// EXCEPTION - Category not found
throw new EntityNotFoundException("Delete category: Category with id: " + categoryId + " not found");
}
} else {
// EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
throw new PathNotValidException("Delete category: Topic with id: " + topicId + " not found in Stakeholder: " + stakeholderId);
}
} else {
// EXCEPTION - Topic not found
throw new EntityNotFoundException("Delete category: Topic with id: " + topicId + " not found");
}
} else {
// EXCEPTION - Stakeholder not found
throw new EntityNotFoundException("Delete category: Stakeholder with id: " + stakeholderId + " not found");
}
Stakeholder stakeholder = this.stakeholderService.findById(stakeholderId);
Topic topic = this.topicService.findByPath(stakeholder, topicId);
Category category = this.categoryService.findByPath(topic, categoryId);
this.categoryService.delete(stakeholder.getType(), category, true);
return true;
}
public boolean onDeleteDefaultCategory(String defaultCategoryId, String defaultTopicId, String children) {
if (children.equals("delete")) {
List<Topic> topics = topicDAO.findByDefaultId(defaultTopicId);
List<Category> categories = categoryDAO.findByDefaultId(defaultCategoryId);
for (Topic topic : topics) {
Iterator<Category> categoriesIterator = categories.iterator();
while (categoriesIterator.hasNext()) {
Category category = categoriesIterator.next();
String categoryId = category.getId();
if (topic.getCategories() != null && topic.getCategories().contains(categoryId)) {
categoriesIterator.remove();
topic.getCategories().remove(categoryId);
topicDAO.save(topic);
subCategoryController.deleteTree(category);
categoryDAO.delete(categoryId);
log.debug("Category with id: " + categoryId + " deleted!");
break;
}
}
}
} else if (children.equals("disconnect")) {
List<Category> categories = categoryDAO.findByDefaultId(defaultCategoryId);
for (Category category : categories) {
subCategoryController.disConnectTree(category);
category.setDefaultId(null);
categoryDAO.save(category);
log.debug("DefaultId for Category with id: " + category.getId() + " empty!");
}
}
return true;
}
@PreAuthorize("isAuthenticated()")
/*@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/reorder", method = RequestMethod.POST)
public List<Category> reorderCategories(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@ -361,131 +226,19 @@ public class CategoryController {
log.debug("Categories reordered!");
return categoriesFull;
}
}*/
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/change-visibility", method = RequestMethod.POST)
public Category changeCategoryVisibility(@PathVariable("stakeholderId") String stakeholderId,
public CategoryFull changeCategoryVisibility(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId,
@RequestParam("visibility") Visibility visibility, @RequestParam(required = false) Boolean propagate) {
log.debug("change category visibility: " + visibility + " - toggle propagate: " + ((propagate != null && propagate) ? "true" : "false"));
@RequestParam("visibility") Visibility visibility, @RequestParam(defaultValue = "false") Boolean propagate) {
log.debug("change category visibility: " + visibility + " - toggle propagate: " + propagate);
log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId);
Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId);
if (stakeholder != null) {
if (!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) {
// EXCEPTION - Access denied
throw new ForbiddenException("Toggle category: You are not authorized to update stakeholder with id: " + stakeholderId);
}
Topic<String> topic = topicDAO.findById(topicId);
if (topic != null) {
if (stakeholder.getTopics().contains(topicId)) {
if (topic.getCategories().contains(categoryId)) {
return changeVisibilityTree(categoryId, visibility, propagate);
} else {
// EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
throw new PathNotValidException("Toggle category: Category with id: " + categoryId + " not found in Topic: " + topicId);
}
} else {
// EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
throw new PathNotValidException("Toggle category: Topic with id: " + topicId + " not found in Stakeholder: " + stakeholderId);
}
} else {
// EXCEPTION - Topic not found
throw new EntityNotFoundException("Toggle category: Topic with id: " + topicId + " not found");
}
} else {
// EXCEPTION - Stakeholder not found
throw new EntityNotFoundException("Toggle category: Stakeholder with id: " + stakeholderId + " not found");
Stakeholder stakeholder = this.stakeholderService.findById(stakeholderId);
Topic topic = this.topicService.findByPath(stakeholder, topicId);
Category category = this.categoryService.findByPath(topic, categoryId);
return this.categoryService.changeVisibility(stakeholder.getType(), stakeholder.getAlias(), category, visibility, propagate);
}
}
public Category changeVisibilityTree(String categoryId, Visibility visibility, Boolean propagate) {
Category<String> category = categoryDAO.findById(categoryId);
if (category == null) {
// EXCEPTION - Category not found
throw new EntityNotFoundException("Change category visibility: Category with id: " + categoryId + " not found");
}
Category<SubCategory> categoryFull = new Category(category);
List<SubCategory> subCategoriesFull = new ArrayList<>();
if (propagate != null && propagate) {
for (String subCategoryId : category.getSubCategories()) {
subCategoriesFull.add(subCategoryController.changeVisibilityTree(subCategoryId, visibility, propagate));
}
}
category.setVisibility(visibility);
categoryDAO.save(category);
log.debug("Category toggled!");
categoryFull.setVisibility(visibility);
categoryFull.setSubCategories(subCategoriesFull);
return categoryFull;
}
private Topic checkForExceptions(String stakeholderId, String topicId) {
Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId);
if (stakeholder == null) {
// EXCEPTION - Stakeholder not found
throw new EntityNotFoundException("checkForExceptions category: Stakeholder with id: " + stakeholderId + " not found");
}
if (!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) {
// EXCEPTION - Access denied
throw new ForbiddenException("checkForExceptions category: You are not authorized to update stakeholder with id: " + stakeholderId);
}
Topic<String> topic = topicDAO.findById(topicId);
if (topic == null) {
// EXCEPTION - Topic not found
throw new EntityNotFoundException("checkForExceptions category: Topic with id: " + topicId + " not found");
}
if (!stakeholder.getTopics().contains(topicId)) {
// EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
throw new PathNotValidException("checkForExceptions category: Topic with id: " + topicId + " not found in Stakeholder: " + stakeholderId);
}
return topic;
}
public void deleteTree(Topic topic) {
List<String> categories = topic.getCategories();
for (String categoryId : categories) {
Category category = categoryDAO.findById(categoryId);
if (category == null) {
// EXCEPTION - Category not found
throw new EntityNotFoundException("Category delete tree: Category with id: " + categoryId + " not found (category exists in topic: " + topic.getId() + ")");
}
subCategoryController.deleteTree(category);
categoryDAO.delete(categoryId);
}
}
public void disConnectTree(Topic topic) {
List<String> categories = topic.getCategories();
for (String categoryId : categories) {
Category category = categoryDAO.findById(categoryId);
if (category == null) {
// EXCEPTION - Category not found
throw new EntityNotFoundException("Category disconnect tree: Category with id: " + categoryId + " not found (category exists in topic: " + topic.getId() + ")");
}
subCategoryController.disConnectTree(category);
category.setDefaultId(null);
categoryDAO.save(category);
}
}
}
*/

View File

@ -1,58 +1,39 @@
/*
package eu.dnetlib.uoamonitorservice.controllers;
import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException;
import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils;
import eu.dnetlib.uoamonitorservice.dao.*;
import eu.dnetlib.uoamonitorservice.entities.*;
import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException;
import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException;
import eu.dnetlib.uoamonitorservice.primitives.IndicatorPath;
import eu.dnetlib.uoamonitorservice.primitives.ReorderEvent;
import eu.dnetlib.uoamonitorservice.primitives.Visibility;
import eu.dnetlib.uoamonitorservice.service.*;
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.io.UnsupportedEncodingException;
import java.util.*;
@RestController
@CrossOrigin(origins = "*")
public class IndicatorController {
private final Logger log = LogManager.getLogger(this.getClass());
@Autowired
private RolesUtils rolesUtils;
private final StakeholderService stakeholderService;
private final TopicService topicService;
private final CategoryService categoryService;
private final SubCategoryService subCategoryService;
private final SectionService sectionService;
private final IndicatorService indicatorService;
@Autowired
private StakeholderDAO stakeholderDAO;
public IndicatorController(StakeholderService stakeholderService, TopicService topicService, CategoryService categoryService,
SubCategoryService subCategoryService, SectionService sectionService, IndicatorService indicatorService) {
this.stakeholderService = stakeholderService;
this.topicService = topicService;
this.categoryService = categoryService;
this.subCategoryService = subCategoryService;
this.sectionService = sectionService;
this.indicatorService = indicatorService;
}
@Autowired
private TopicDAO topicDAO;
@Autowired
private CategoryDAO categoryDAO;
@Autowired
private SubCategoryDAO subCategoryDAO;
@Autowired
private SectionDAO sectionDAO;
@Autowired
private IndicatorDAO indicatorDAO;
@Autowired
private SectionController sectionController;
@Autowired
private StakeholderController stakeholderController;
@PreAuthorize("isAuthenticated()")
/*@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/save-bulk", method = RequestMethod.POST)
public Stakeholder saveBulkIndicators(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@ -143,8 +124,8 @@ public class IndicatorController {
indicators.add(indicator.getId());
log.debug("Indicator saved!");
}
@PreAuthorize("isAuthenticated()")
*/
/* @PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/save", method = RequestMethod.POST)
public Indicator saveIndicator(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@ -462,7 +443,7 @@ public class IndicatorController {
indicatorDAO.save(indicatorBasedOnDefault);
}
}
*/
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{indicatorId}/delete", method = RequestMethod.DELETE)
public boolean deleteIndicator(@PathVariable("stakeholderId") String stakeholderId,
@ -474,79 +455,18 @@ public class IndicatorController {
@RequestParam(required = false) String children) {
log.debug("delete indicator");
log.debug("Id: " + indicatorId + " - Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId + " - Section: " + sectionId);
Indicator indicator = indicatorDAO.findById(indicatorId);
if (indicator != null) {
Section<String> section = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator.getType());
Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId);
if (indicator.getDefaultId() != null && !rolesUtils.hasCreateAndDeleteAuthority(stakeholder.getType())) {
// EXCEPTION - Access denied
throw new ForbiddenException("Delete indicator: You are not authorized to delete a default Indicator in stakeholder with id: " + stakeholderId);
}
List<String> indicators = section.getIndicators();
int index = indicators.indexOf(indicatorId);
if (index != -1) {
// this indicator belongs in default profile
if (section.getDefaultId() == null && children != null) {
onDeleteDefaultIndicator(indicatorId, sectionId, children);
}
indicators.remove(index);
sectionDAO.save(section);
indicatorDAO.delete(indicatorId);
log.debug("Indicator deleted!");
} else {
// EXCEPTION - Indicator not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias(); -> Section: section.getTitle();
throw new PathNotValidException("Delete indicator: Indicator with id: " + indicatorId + " not found in Sectiom: " + sectionId);
}
} else {
// EXCEPTION - Indicator not found
throw new EntityNotFoundException("Delete indicator: Indicator with id: " + indicatorId + " not found");
}
Stakeholder stakeholder = this.stakeholderService.findById(stakeholderId);
Topic topic = this.topicService.findByPath(stakeholder, topicId);
Category category = this.categoryService.findByPath(topic, categoryId);
SubCategory subCategory = this.subCategoryService.findByPath(category, subcategoryId);
Section section = this.sectionService.findByPath(subCategory, sectionId);
Indicator indicator = this.indicatorService.findByPath(section, indicatorId);
this.indicatorService.delete(stakeholder.getType(), indicator, true);
return true;
}
public boolean onDeleteDefaultIndicator(String defaultIndicatorId, String defaultSectionId, String children) {
if (children.equals("delete")) {
// 2nd way
List<Section> sections = sectionDAO.findByDefaultId(defaultSectionId);
List<Indicator> indicators = indicatorDAO.findByDefaultId(defaultIndicatorId);
for (Section section : sections) {
Iterator<Indicator> indicatorsIterator = indicators.iterator();
while (indicatorsIterator.hasNext()) {
String indicatorId = indicatorsIterator.next().getId();
if (section.getIndicators().contains(indicatorId)) {
indicatorsIterator.remove();
section.getIndicators().remove(indicatorId);
sectionDAO.save(section);
indicatorDAO.delete(indicatorId);
log.debug("Indicator with id: " + indicatorId + " deleted!");
break;
}
}
}
} else if (children.equals("disconnect")) {
List<Indicator> indicators = indicatorDAO.findByDefaultId(defaultIndicatorId);
for (Indicator indicator : indicators) {
indicator.setDefaultId(null);
indicatorDAO.save(indicator);
log.debug("DefaultId for Indicator with id: " + indicator.getId() + " empty!");
}
}
return true;
}
@PreAuthorize("isAuthenticated()")
/*@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{type}/reorder", method = RequestMethod.POST)
public List<Indicator> reorderIndicators(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@ -586,7 +506,7 @@ public class IndicatorController {
log.debug("Indicators reordered!");
return indicatorsFull;
}
}*/
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{indicatorId}/change-visibility", method = RequestMethod.POST)
@ -599,121 +519,12 @@ public class IndicatorController {
@RequestParam("visibility") Visibility visibility) {
log.debug("change indicator visibility: " + visibility);
log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId + " - Section: " + sectionId + " - Indicator: " + indicatorId);
Indicator indicator = indicatorDAO.findById(indicatorId);
if (indicator == null) {
// EXCEPTION - Indicator not found
throw new EntityNotFoundException("Change indicator visibility: Indicator with id: " + indicatorId + " not found");
}
Section<String> section = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator.getType());
List<String> indicators = section.getIndicators();
if (indicators.contains(indicatorId)) {
return changeVisibilityTree(indicatorId, indicator, visibility);
} else {
// EXCEPTION - Indicator not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subCategory.getAlias(); -> Section: section.getTitle();
throw new PathNotValidException("Toggle indicators: Indicator with id: " + indicatorId + " not found in Section: " + sectionId);
Stakeholder stakeholder = this.stakeholderService.findById(stakeholderId);
Topic topic = this.topicService.findByPath(stakeholder, topicId);
Category category = this.categoryService.findByPath(topic, categoryId);
SubCategory subCategory = this.subCategoryService.findByPath(category, subcategoryId);
Section section = this.sectionService.findByPath(subCategory, sectionId);
Indicator indicator = this.indicatorService.findByPath(section, indicatorId);
return this.indicatorService.changeVisibility(stakeholder.getType(), stakeholder.getAlias(), indicator, visibility);
}
}
public Indicator changeVisibilityTree(String indicatorId, Indicator indicator, Visibility visibility) {
if (indicator == null) {
indicator = indicatorDAO.findById(indicatorId);
if (indicator == null) {
// EXCEPTION - Indicator not found
throw new EntityNotFoundException("Change indicator visibility: Indicator with id: " + indicatorId + " not found");
}
}
indicator.setVisibility(visibility);
indicatorDAO.save(indicator);
log.debug("Indicator toggled!");
return indicator;
}
private Section checkForExceptions(String stakeholderId, String topicId, String categoryId, String subcategoryId, String sectionId, String indicatorType) {
Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId);
if (stakeholder == null) {
// EXCEPTION - Stakeholder not found
throw new EntityNotFoundException("Save indicator: Stakeholder with id: " + stakeholderId + " not found");
}
if (!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) {
// EXCEPTION - Access denied
throw new ForbiddenException("CheckForExceptions Indicator: You are not authorized to update stakeholder with id: " + stakeholderId);
}
Topic<String> topic = topicDAO.findById(topicId);
if (topic == null) {
// EXCEPTION - Topic not found
throw 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<String> 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<String> 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);
}
Section<String> section = sectionDAO.findById(sectionId);
if (section == null) {
// EXCEPTION - Section not found
throw new EntityNotFoundException("Save indicator: Section with id: " + sectionId + " not found");
}
if (indicatorType.equals("chart")) {
if (!subcategory.getCharts().contains(sectionId)) {
// EXCEPTION - Section not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias();
throw new PathNotValidException("Save indicator: SubCategory with id: " + subcategoryId + " not found in Category: " + categoryId);
}
} else if (indicatorType.equals("number")) {
if (!subcategory.getNumbers().contains(sectionId)) {
// EXCEPTION - Section not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias();
throw new PathNotValidException("Save indicator: SubCategory with id: " + subcategoryId + " not found in Category: " + categoryId);
}
}
return section;
}
public void deleteTree(Section section) {
List<String> indicators = section.getIndicators();
for (String indicatorId : indicators) {
indicatorDAO.delete(indicatorId);
}
}
public void disConnectTree(Section section) {
List<String> indicators = section.getIndicators();
for (String indicatorId : indicators) {
Indicator indicator = indicatorDAO.findById(indicatorId);
indicator.setDefaultId(null);
indicatorDAO.save(indicator);
}
}
}
*/

View File

@ -1,47 +1,48 @@
/*
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 eu.dnetlib.uoamonitorservice.service.*;
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;
private final StakeholderService stakeholderService;
private final TopicService topicService;
private final CategoryService categoryService;
private final SubCategoryService subCategoryService;
private final SectionService sectionService;
@PreAuthorize("isAuthenticated()")
@Autowired
public SectionController(StakeholderService stakeholderService, TopicService topicService, CategoryService categoryService,
SubCategoryService subCategoryService, SectionService sectionService) {
this.stakeholderService = stakeholderService;
this.topicService = topicService;
this.categoryService = categoryService;
this.subCategoryService = subCategoryService;
this.sectionService = sectionService;
}
/*@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/save/{index}", method = RequestMethod.POST)
public SectionFull saveSection(@PathVariable("stakeholderId") String stakeholderId,
public Section saveSection(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId,
@PathVariable("subcategoryId") String subcategoryId,
@PathVariable("index") String index,
@RequestBody SectionFull sectionFull) {
@RequestBody Section<Indicator> 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");
SubCategory<String> subCategory = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId);
Section section = new Section(sectionFull);
Section<String> section = new Section<>(sectionFull);
Date date = new Date();
section.setUpdateDate(date);
@ -78,7 +79,7 @@ public class SectionController {
section.setIndicators(indicators);
Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId);
Stakeholder<String> 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) {
@ -151,121 +152,94 @@ public class SectionController {
public void onUpdateDefaultSection(Section section, Stakeholder stakeholder, Section oldSection) {
log.debug("On update default section");
// section already exists - check if changed and update all sections based on it
boolean changed = false;
List<Section> sections = sectionDAO.findByDefaultId(section.getId());
for(Section sectionBasedOnDefault : sections) {
if(section.getTitle() != null && !section.getTitle().equals(sectionBasedOnDefault.getTitle())
&& (oldSection.getTitle() == null || oldSection.getTitle().equals(sectionBasedOnDefault.getTitle()))) {
sectionBasedOnDefault.setTitle(section.getTitle());
changed = true;
}
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);
if(!changed) {
// break;
continue;
}
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);
// sectionBasedOnDefault.setTitle(section.getTitle());
sectionBasedOnDefault.setUpdateDate(section.getUpdateDate());
sectionDAO.save(sectionBasedOnDefault);
}
}*/
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/delete", method = RequestMethod.DELETE)
public boolean deleteSection(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId,
@PathVariable("subcategoryId") String subcategoryId,
@PathVariable("sectionId") String sectionId,
@RequestParam(required = false) String children) {
log.debug("delete section");
log.debug("Id: " + sectionId + " - Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId);
Stakeholder stakeholder = this.stakeholderService.findById(stakeholderId);
Topic topic = this.topicService.findByPath(stakeholder, topicId);
Category category = this.categoryService.findByPath(topic, categoryId);
SubCategory subCategory = this.subCategoryService.findByPath(category, subcategoryId);
Section section = this.sectionService.findByPath(subCategory, sectionId);
this.sectionService.delete(stakeholder.getType(), section, true);
return true;
}
Category<String> category = categoryDAO.findById(categoryId);
if(category == null) {
// EXCEPTION - Category not found
throw new EntityNotFoundException("Save indicator: Category with id: "+categoryId+" not found");
/*@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{type}/reorder", method = RequestMethod.POST)
public List<Section> reorderSections(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId,
@PathVariable("subcategoryId") String subcategoryId,
@PathVariable("type") String type,
@RequestBody List<String> sections) {
log.debug("reorder sections of type: "+type);
log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId);
SubCategory<String> subCategory = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId);
if (type.equals("chart")) {
List<String> oldSections = subCategory.getCharts();
for (String sectionId : oldSections) {
if (!sections.contains(sectionId)) {
sections.add(sectionId);
}
}
subCategory.setCharts(sections);
} else if (type.equals("number")) {
List<String> oldSections = subCategory.getNumbers();
for (String sectionId : oldSections) {
if (!sections.contains(sectionId)) {
sections.add(sectionId);
}
}
subCategory.setNumbers(sections);
}
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<String> 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<String> sections = subCategory.getCharts();
List<Section> sectionsFull = new ArrayList<>();
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()+")");
throw new EntityNotFoundException("Reorder sections: Section with id: " + sectionId + " not found");
}
sectionsFull.add(section);
}
indicatorController.deleteTree(section);
subCategoryDAO.save(subCategory);
log.debug("Sections reordered!");
sectionDAO.delete(sectionId);
return sectionsFull;
}*/
}
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<String> 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);
}
}
public Section changeVisibilityTree(String sectionId, Visibility visibility) {
Section<String> section = sectionDAO.findById(sectionId);
if (section == null) {
// EXCEPTION - Section not found
throw new EntityNotFoundException("Change section visibility: Section with id: " + sectionId + " not found");
}
Section<Indicator> sectionFull = new Section(section);
List<Indicator> indicatorsFull = new ArrayList();
for (String indicatorId : section.getIndicators()) {
indicatorsFull.add(indicatorController.changeVisibilityTree(indicatorId, null, visibility));
}
sectionFull.setIndicators(indicatorsFull);
return sectionFull;
}
}
*/

View File

@ -1,93 +1,40 @@
/*
package eu.dnetlib.uoamonitorservice.controllers;
import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils;
import eu.dnetlib.uoamonitorservice.dao.*;
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.dto.SubCategoryFull;
import eu.dnetlib.uoamonitorservice.entities.Category;
import eu.dnetlib.uoamonitorservice.entities.Stakeholder;
import eu.dnetlib.uoamonitorservice.entities.SubCategory;
import eu.dnetlib.uoamonitorservice.entities.Topic;
import eu.dnetlib.uoamonitorservice.primitives.Visibility;
import eu.dnetlib.uoamonitorservice.service.CategoryService;
import eu.dnetlib.uoamonitorservice.service.StakeholderService;
import eu.dnetlib.uoamonitorservice.service.SubCategoryService;
import eu.dnetlib.uoamonitorservice.service.TopicService;
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.Iterator;
import java.util.List;
@RestController
@CrossOrigin(origins = "*")
public class SubCategoryController {
private final Logger log = LogManager.getLogger(this.getClass());
@Autowired
private RolesUtils rolesUtils;
private final StakeholderService stakeholderService;
private final TopicService topicService;
private final CategoryService categoryService;
private final SubCategoryService subCategoryService;
@Autowired
private StakeholderDAO stakeholderDAO;
@Autowired
private TopicDAO topicDAO;
@Autowired
private CategoryDAO categoryDAO;
@Autowired
private SubCategoryDAO subCategoryDAO;
@Autowired
private SectionDAO sectionDAO;
@Autowired
private IndicatorDAO indicatorDAO;
@Autowired
private SectionController sectionController;
public SubCategory<Section<Indicator>> buildSubCategory(SubCategory<Section<Indicator>> subcategoryFull) {
SubCategory<String> subCategory = new SubCategory<>(subcategoryFull);
List<String> sectionCharts = new ArrayList<>();
List<Section<Indicator>> sectionChartsFull = new ArrayList<>();
for(Section section : subcategoryFull.getCharts()) {
Section<Indicator> sectionFull = sectionController.buildSection(section);
sectionChartsFull.add(sectionFull);
sectionCharts.add(sectionFull.getId());
}
subcategoryFull.setCharts(sectionChartsFull);
subCategory.setCharts(sectionCharts);
List<String> sectionNumbers = new ArrayList<>();
List<Section<Indicator>> sectionNumbersFull = new ArrayList<>();
for(Section section : subcategoryFull.getNumbers()) {
Section<Indicator> sectionFull = sectionController.buildSection(section);
sectionNumbersFull.add(sectionFull);
sectionNumbers.add(sectionFull.getId());
}
subcategoryFull.setNumbers(sectionNumbersFull);
subCategory.setNumbers(sectionNumbers);
Date date = new Date();
subCategory.setCreationDate(date);
subCategory.setUpdateDate(date);
subcategoryFull.setCreationDate(date);
subcategoryFull.setUpdateDate(date);
subCategoryDAO.save(subCategory);
subcategoryFull.setId(subCategory.getId());
return subcategoryFull;
public SubCategoryController(StakeholderService stakeholderService, TopicService topicService, CategoryService categoryService, SubCategoryService subCategoryService) {
this.stakeholderService = stakeholderService;
this.topicService = topicService;
this.categoryService = categoryService;
this.subCategoryService = subCategoryService;
}
@PreAuthorize("isAuthenticated()")
/* @PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/save", method = RequestMethod.POST)
public SubCategory<Section<Indicator>> saveSubCategory(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@ -215,7 +162,7 @@ public class SubCategoryController {
subCategoryBasedOnDefault.setUpdateDate(subCategory.getUpdateDate());
subCategoryDAO.save(subCategoryBasedOnDefault);
}
}
}*/
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/delete", method = RequestMethod.DELETE)
@ -226,89 +173,15 @@ public class SubCategoryController {
@RequestParam(required = false) String children) {
log.debug("delete subcategory");
log.debug("Id: " + subcategoryId + " - Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId);
Category category = checkForExceptions(stakeholderId, topicId, categoryId);
SubCategory<String> subcategory = subCategoryDAO.findById(subcategoryId);
if(subcategory != null) {
Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId);
if(subcategory.getDefaultId() != null && !rolesUtils.hasCreateAndDeleteAuthority(stakeholder.getType())) {
// EXCEPTION - Access denied
throw new ForbiddenException("Delete subcategory: You are not authorized to delete a default SubCategory in stakeholder with id: "+stakeholderId);
}
List<String> subcategories = category.getSubCategories();
int index = subcategories.indexOf(subcategoryId);
if(index != -1) {
// this subCategory belongs in default profile
if(category.getDefaultId() == null && children != null) {
onDeleteDefaultSubCategory(subcategoryId, categoryId, children);
}
sectionController.deleteTree(subcategory);
subcategory.setCharts(null);
subcategory.setNumbers(null);
subcategories.remove(index);
categoryDAO.save(category);
subCategoryDAO.delete(subcategoryId);
log.debug("Subcategory deleted!");
} else {
// EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
throw new PathNotValidException("Delete subcategory: Subcategory with id: "+subcategoryId+" not found in Category: "+categoryId);
}
} else {
// EXCEPTION - SubCategory not found
throw new EntityNotFoundException("Delete subcategory: SubCategory with id: "+subcategoryId+" not found");
}
Stakeholder stakeholder = this.stakeholderService.findById(stakeholderId);
Topic topic = this.topicService.findByPath(stakeholder, topicId);
Category category = this.categoryService.findByPath(topic, categoryId);
SubCategory subCategory = this.subCategoryService.findByPath(category, subcategoryId);
this.subCategoryService.delete(stakeholder.getType(), subCategory, true);
return true;
}
public boolean onDeleteDefaultSubCategory(String defaultSubCategoryId, String defaultCategoryId, String children) {
if(children.equals("delete")) {
List<Category> categories = categoryDAO.findByDefaultId(defaultCategoryId);
List<SubCategory> subCategories = subCategoryDAO.findByDefaultId(defaultSubCategoryId);
for(Category category : categories) {
Iterator<SubCategory> subCategoriesIterator = subCategories.iterator();
while(subCategoriesIterator.hasNext()) {
SubCategory subCategory = subCategoriesIterator.next();
String subCategoryId = subCategory.getId();
if(category.getSubCategories() != null && category.getSubCategories().contains(subCategoryId)) {
subCategoriesIterator.remove();
category.getSubCategories().remove(subCategoryId);
categoryDAO.save(category);
sectionController.deleteTree(subCategory);
subCategoryDAO.delete(subCategoryId);
log.debug("SubCategory with id: "+subCategoryId+" deleted!");
break;
}
}
}
} else if(children.equals("disconnect")) {
List<SubCategory> subCategories = subCategoryDAO.findByDefaultId(defaultSubCategoryId);
for(SubCategory subCategory : subCategories) {
sectionController.disConnectTree(subCategory);
subCategory.setDefaultId(null);
subCategoryDAO.save(subCategory);
log.debug("DefaultId for SubCategory with id: "+subCategory.getId()+" empty!");
}
}
return true;
}
@PreAuthorize("isAuthenticated()")
/*@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/reorder", method = RequestMethod.POST)
public List<SubCategory> reorderSubCategories(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@ -341,129 +214,22 @@ public class SubCategoryController {
log.debug("SubCategories reordered!");
return subCategoriesFull;
}
}*/
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/change-visibility", method = RequestMethod.POST)
public SubCategory changeSubCategoryVisibility(@PathVariable("stakeholderId") String stakeholderId,
public SubCategoryFull changeSubCategoryVisibility(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId,
@PathVariable("subcategoryId") String subcategoryId,
@RequestParam("visibility") Visibility visibility, @RequestParam(required = false) Boolean propagate) {
log.debug("change subCategory visibility: "+visibility + " - toggle propagate: "+((propagate != null && propagate) ? "true" : "false"));
@RequestParam("visibility") Visibility visibility,
@RequestParam(defaultValue = "false") Boolean propagate) {
log.debug("change subCategory visibility: " + visibility + " - toggle propagate: " + propagate);
log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId);
SubCategory subCategory = subCategoryDAO.findById(subcategoryId);
if (subCategory == null) {
// EXCEPTION - SubCategory not found
throw new EntityNotFoundException("Change subCategory visibility: SubCategory with id: "+subcategoryId+" not found");
}
Category category = checkForExceptions(stakeholderId, topicId, categoryId);
if (category.getSubCategories().contains(subcategoryId)) {
return changeVisibilityTree(subcategoryId, visibility, propagate);
} else {
// EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
throw new PathNotValidException("Toggle subCategory: SubCategory with id: "+subcategoryId+" not found in Category: "+categoryId);
Stakeholder stakeholder = this.stakeholderService.findById(stakeholderId);
Topic topic = this.topicService.findByPath(stakeholder, topicId);
Category category = this.categoryService.findByPath(topic, categoryId);
SubCategory subCategory = this.subCategoryService.findByPath(category, subcategoryId);
return this.subCategoryService.changeVisibility(stakeholder.getType(), stakeholder.getAlias(), subCategory, visibility, propagate);
}
}
public SubCategory changeVisibilityTree(String subCategoryId, Visibility visibility, Boolean propagate) {
SubCategory<String> subCategory = subCategoryDAO.findById(subCategoryId);
if (subCategory == null) {
// EXCEPTION - SubCategory not found
throw new EntityNotFoundException("Change subCategory visibility: SubCategory with id: " + subCategoryId + " not found");
}
SubCategory<Section> subCategoryFull = new SubCategory(subCategory);
List<Section> chartsFull = new ArrayList();
List<Section> numbersFull = new ArrayList();
if (propagate != null && propagate) {
for (String sectionId : subCategory.getCharts()) {
chartsFull.add(sectionController.changeVisibilityTree(sectionId, visibility));
}
for (String sectionId : subCategory.getNumbers()) {
numbersFull.add(sectionController.changeVisibilityTree(sectionId, visibility));
}
}
subCategory.setVisibility(visibility);
subCategoryDAO.save(subCategory);
log.debug("SubCategory toggled!");
subCategoryFull.setVisibility(visibility);
subCategoryFull.setCharts(chartsFull);
subCategoryFull.setNumbers(numbersFull);
return subCategoryFull;
}
private Category checkForExceptions(String stakeholderId, String topicId, String categoryId) {
Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId);
if(stakeholder == null) {
// EXCEPTION - Stakeholder not found
throw new EntityNotFoundException("Save indicator: Stakeholder with id: " + stakeholderId + " not found");
}
if(!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) {
// EXCEPTION - Access denied
throw new ForbiddenException("CheckForExceptions SubCategory: You are not authorized to update stakeholder with id: "+stakeholderId);
}
Topic<String> topic = topicDAO.findById(topicId);
if(topic == null) {
// EXCEPTION - Topic not found
throw 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<String> 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);
}
return category;
}
public void deleteTree(Category category) {
List<String> subCategories = category.getSubCategories();
for(String subCategoryId : subCategories) {
SubCategory subCategory = subCategoryDAO.findById(subCategoryId);
if (subCategory == null) {
// EXCEPTION - SubCategory not found
throw new EntityNotFoundException("SubCategory delete tree: SubCategory with id: "+subCategoryId+" not found (subCategory exists in category: "+category.getId()+")");
}
sectionController.deleteTree(subCategory);
subCategoryDAO.delete(subCategoryId);
}
}
public void disConnectTree(Category category) {
List<String> subCategories = category.getSubCategories();
for(String subCategoryId : subCategories) {
SubCategory subCategory = subCategoryDAO.findById(subCategoryId);
if (subCategory == null) {
// EXCEPTION - SubCategory not found
throw new EntityNotFoundException("SubCategory disconnect tree: SubCategory with id: "+subCategoryId+" not found (subCategory exists in category: "+category.getId()+")");
}
sectionController.disConnectTree(subCategory);
subCategory.setDefaultId(null);
subCategoryDAO.save(subCategory);
}
}
}
*/

View File

@ -17,8 +17,8 @@ import org.springframework.web.bind.annotation.*;
public class TopicController {
private final Logger log = LogManager.getLogger(this.getClass());
private TopicService topicService;
private StakeholderService stakeholderService;
private final TopicService topicService;
private final StakeholderService stakeholderService;
@Autowired
public TopicController(TopicService topicService, StakeholderService stakeholderService) {
@ -215,9 +215,10 @@ public class TopicController {
public TopicFull changeTopicVisibility(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@RequestParam("visibility") Visibility visibility, @RequestParam(required = false) Boolean propagate) {
log.debug("change topic visibility: " + visibility + " - toggle propagate: " + ((propagate != null && propagate) ? "true" : "false"));
log.debug("change topic visibility: " + visibility + " - toggle propagate: " + propagate);
log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId);
Stakeholder stakeholder = this.stakeholderService.findById(stakeholderId);
return this.topicService.changeVisibility(stakeholder.getType(), stakeholder.getAlias(), topicId, visibility, propagate);
Topic topic = this.topicService.findByPath(stakeholder, topicId);
return this.topicService.changeVisibility(stakeholder.getType(), stakeholder.getAlias(), topic, visibility, propagate);
}
}

View File

@ -31,15 +31,14 @@ public class CategoryService {
this.commonService = commonService;
}
public Category findByPath(Topic topic, String categoryId, String message) {
public Category findByPath(Topic topic, String categoryId) {
if (!topic.getCategories().contains(categoryId)) {
throw new PathNotValidException(message + ": Category with id: " + categoryId + " not found in Topic: " + topic.getId());
throw new PathNotValidException("Category with id: " + categoryId + " not found in Topic: " + topic.getId());
}
return this.dao.findById(categoryId).orElseThrow(() -> new EntityNotFoundException(message + ": Category with id: " + categoryId + " not found"));
return this.dao.findById(categoryId).orElseThrow(() -> new EntityNotFoundException("Category with id: " + categoryId + " not found"));
}
public CategoryFull getFullCategory(String type, String alias, String categoryId) {
Category category = this.find(categoryId);
public CategoryFull getFullCategory(String type, String alias, Category category) {
if (commonService.hasVisibilityAuthority(type, alias, category)) {
return new CategoryFull(category, category.getSubCategories().stream()
.map(subCategoryId -> this.subCategoryService.getFullSubCategory(type, alias, subCategoryId))
@ -49,6 +48,11 @@ public class CategoryService {
}
}
public CategoryFull getFullCategory(String type, String alias, String categoryId) {
Category category = this.find(categoryId);
return this.getFullCategory(type, alias, category);
}
public Category find(String id) {
return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("Category with id: " + id + " not found"));
}
@ -93,20 +97,28 @@ public class CategoryService {
});
}
public CategoryFull changeVisibility(String type, String alias, String id, Visibility visibility, Boolean propagate) {
public CategoryFull changeVisibility(String type, String alias, CategoryFull category, Visibility visibility, Boolean propagate) {
if (this.commonService.hasEditAuthority(type, alias)) {
CategoryFull categoryFull = this.getFullCategory(type, alias, id);
categoryFull.setVisibility(visibility);
category.setVisibility(visibility);
if (propagate) {
categoryFull.setSubCategories(categoryFull.getSubCategories().stream()
.map(category -> this.subCategoryService.changeVisibility(type, alias, category.getId(), visibility, true))
category.setSubCategories(category.getSubCategories().stream()
.map(subCategory -> this.subCategoryService.changeVisibility(type, alias, subCategory, visibility, true))
.collect(Collectors.toList()));
}
Category category = this.save(new Category(categoryFull));
categoryFull.update(category);
return categoryFull;
category.update(this.save(new Category(category)));
return category;
} else {
throw new ForbiddenException("Change category visibility: You are not authorized to update category with id: " + id);
throw new ForbiddenException("Change category visibility: You are not authorized to update category with id: " + category.getId());
}
}
public CategoryFull changeVisibility(String type, String alias, Category category, Visibility visibility, Boolean propagate) {
CategoryFull categoryFull = this.getFullCategory(type, alias, category);
return this.changeVisibility(type, alias, categoryFull, visibility, propagate);
}
public CategoryFull changeVisibility(String type, String alias, String id, Visibility visibility, Boolean propagate) {
Category category = this.find(id);
return this.changeVisibility(type, alias, category, visibility, propagate);
}
}

View File

@ -6,7 +6,9 @@ import eu.dnetlib.uoamonitorservice.dao.SectionDAO;
import eu.dnetlib.uoamonitorservice.dto.SectionFull;
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;
@ -32,6 +34,13 @@ public class IndicatorService {
return this.dao.save(indicator);
}
public Indicator findByPath(Section section, String indicatorId) {
if (!section.getIndicators().contains(indicatorId)) {
throw new PathNotValidException("Indicator with id: " + indicatorId + " not found in Section: " + section.getId());
}
return this.dao.findById(indicatorId).orElseThrow(() -> new EntityNotFoundException("Indicator with id: " + indicatorId + " not found"));
}
public Indicator find(String id) {
return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("Indicator with id: " + id + " not found"));
}
@ -72,13 +81,17 @@ public class IndicatorService {
});
}
public Indicator changeVisibility(String type, String alias, String id, Visibility visibility) {
public Indicator changeVisibility(String type, String alias, Indicator indicator, Visibility visibility) {
if(this.commonService.hasEditAuthority(type, alias)) {
Indicator indicator = this.getIndicator(type, alias, id);
indicator.setVisibility(visibility);
return this.save(indicator);
} else {
throw new ForbiddenException("Change section visibility: You are not authorized to update section with id: " + id);
throw new ForbiddenException("Change section visibility: You are not authorized to update section with id: " + indicator.getId());
}
}
public Indicator changeVisibility(String type, String alias, String id, Visibility visibility) {
Indicator indicator = this.find(id);
return this.changeVisibility(type, alias, indicator, visibility);
}
}

View File

@ -4,9 +4,12 @@ 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;
@ -31,22 +34,11 @@ public class SectionService {
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 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());
}
public SectionFull saveSection(SectionFull sectionFull) {
Section section = new Section(sectionFull);
if(section.getId() != null) {
section.setIndicators(this.getIndicatorsId(section.getId()));
}
return sectionFull;
return this.dao.findById(sectionId).orElseThrow(() -> new EntityNotFoundException("Section with id: " + sectionId + " not found"));
}
public Section find(String id) {
@ -113,17 +105,20 @@ public class SectionService {
});
}
public SectionFull changeVisibility(String type, String alias, String id, Visibility visibility) {
public SectionFull changeVisibility(String type, String alias, SectionFull section, 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))
section.setIndicators(section.getIndicators().stream()
.map(indicator -> this.indicatorService.changeVisibility(type, alias, indicator, visibility))
.collect(Collectors.toList()));
Section section = this.save(new Section(sectionFull));
sectionFull.update(section);
return sectionFull;
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: " + id);
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);
}
}

View File

@ -122,8 +122,8 @@ public class StakeholderService {
if(this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) {
StakeholderFull stakeholderFull = this.getFullStakeholder(stakeholder);
if(propagate) {
stakeholderFull.setTopics(stakeholder.getTopics().stream().
map(topicId -> this.topicService.changeVisibility(stakeholderFull.getType(), stakeholderFull.getAlias(), topicId, visibility, true))
stakeholderFull.setTopics(stakeholderFull.getTopics().stream().
map(topic -> this.topicService.changeVisibility(stakeholderFull.getType(), stakeholderFull.getAlias(), topic.getId(), visibility, true))
.collect(Collectors.toList()));
}
stakeholder = this.save(stakeholder);

View File

@ -38,15 +38,14 @@ public class SubCategoryService {
this.sectionService = sectionService;
}
public SubCategory findByPath(Category category, String subcategoryId, String message) {
public SubCategory findByPath(Category category, String subcategoryId) {
if (!category.getSubCategories().contains(subcategoryId)) {
throw new PathNotValidException(message + ": SubCategory with id: " + subcategoryId + " not found in Category: " + category.getId());
throw new PathNotValidException("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"));
return this.dao.findById(subcategoryId).orElseThrow(() -> new EntityNotFoundException("SubCategory with id: " + subcategoryId + " not found"));
}
public SubCategoryFull getFullSubCategory(String type, String alias, String subCategoryId) {
SubCategory subCategory = this.find(subCategoryId);
public SubCategoryFull getFullSubCategory(String type, String alias, SubCategory subCategory) {
if(commonService.hasVisibilityAuthority(type, alias, subCategory)) {
return new SubCategoryFull(subCategory, subCategory.getNumbers().stream()
.map(sectionId -> this.sectionService.getFullSection(type, alias, sectionId))
@ -59,6 +58,11 @@ public class SubCategoryService {
}
}
public SubCategoryFull getFullSubCategory(String type, String alias, String subCategoryId) {
SubCategory subCategory = this.find(subCategoryId);
return this.getFullSubCategory(type, alias, subCategory);
}
public SubCategory find(String id) {
return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("SubCategory with id: " + id + " not found"));
}
@ -109,23 +113,32 @@ public class SubCategoryService {
});
}
public SubCategoryFull changeVisibility(String type, String alias, String id, Visibility visibility, Boolean propagate) {
public SubCategoryFull changeVisibility(String type, String alias, SubCategoryFull subCategory, Visibility visibility, Boolean propagate) {
if(this.commonService.hasEditAuthority(type, alias)) {
SubCategoryFull subCategoryFull = this.getFullSubCategory(type, alias, id);
subCategoryFull.setVisibility(visibility);
subCategory.setVisibility(visibility);
if(propagate) {
subCategoryFull.setNumbers(subCategoryFull.getNumbers().stream()
.map(category -> this.sectionService.changeVisibility(type, alias, category.getId(), visibility))
subCategory.setNumbers(subCategory.getNumbers().stream()
.map(section -> this.sectionService.changeVisibility(type, alias, section, visibility))
.collect(Collectors.toList()));
subCategoryFull.setCharts(subCategoryFull.getCharts().stream()
.map(category -> this.sectionService.changeVisibility(type, alias, category.getId(), visibility))
subCategory.setCharts(subCategory.getCharts().stream()
.map(section -> this.sectionService.changeVisibility(type, alias, section, visibility))
.collect(Collectors.toList()));
}
SubCategory subCategory = this.save(new SubCategory(subCategoryFull));
subCategoryFull.update(subCategory);
return subCategoryFull;
subCategory.update(this.save(new SubCategory(subCategory)));
return subCategory;
} else {
throw new ForbiddenException("Change subCategory visibility: You are not authorized to update subCategory with id: " + id);
throw new ForbiddenException("Change subCategory visibility: You are not authorized to update subCategory with id: " + subCategory.getId());
}
}
public SubCategoryFull changeVisibility(String type, String alias, SubCategory subCategory, Visibility visibility, Boolean propagate) {
SubCategoryFull subCategoryFull = this.getFullSubCategory(type, alias, subCategory);
return this.changeVisibility(type, alias, subCategoryFull, visibility, propagate);
}
public SubCategoryFull changeVisibility(String type, String alias, String id, Visibility visibility, Boolean propagate) {
SubCategory subCategory = this.find(id);
return this.changeVisibility(type, alias, subCategory, visibility, propagate);
}
}

View File

@ -38,8 +38,7 @@ public class TopicService {
return this.dao.findById(topicId).orElseThrow(() -> new EntityNotFoundException("Topic with id: " + topicId + " not found"));
}
public TopicFull getFullTopic(String type, String alias, String id) {
Topic topic = this.find(id);
public TopicFull getFullTopic(String type, String alias, Topic topic) {
if (commonService.hasVisibilityAuthority(type, alias, topic)) {
return new TopicFull(topic, topic.getCategories().stream()
.map(categoryId -> this.categoryService.getFullCategory(type, alias, categoryId))
@ -49,6 +48,11 @@ public class TopicService {
}
}
public TopicFull getFullTopic(String type, String alias, String id) {
Topic topic = this.find(id);
return this.getFullTopic(type, alias, topic);
}
public Topic find(String id) {
return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("Topic with id: " + id + " not found"));
}
@ -93,20 +97,28 @@ public class TopicService {
});
}
public TopicFull changeVisibility(String type, String alias, String id, Visibility visibility, Boolean propagate) {
public TopicFull changeVisibility(String type, String alias, TopicFull topic, Visibility visibility, Boolean propagate) {
if (this.commonService.hasEditAuthority(type, alias)) {
TopicFull topicFull = this.getFullTopic(type, alias, id);
topicFull.setVisibility(visibility);
topic.setVisibility(visibility);
if (propagate) {
topicFull.setCategories(topicFull.getCategories().stream()
.map(category -> this.categoryService.changeVisibility(type, alias, category.getId(), visibility, true))
topic.setCategories(topic.getCategories().stream()
.map(category -> this.categoryService.changeVisibility(type, alias, category, visibility, true))
.collect(Collectors.toList()));
}
Topic topic = this.save(new Topic(topicFull));
topicFull.update(topic);
return topicFull;
topic.update(this.save(new Topic(topic)));
return topic;
} else {
throw new ForbiddenException("Change topic visibility: You are not authorized to update topic with id: " + id);
throw new ForbiddenException("Change topic visibility: You are not authorized to update topic with id: " + topic.getId());
}
}
public TopicFull changeVisibility(String type, String alias, Topic topic, Visibility visibility, Boolean propagate) {
TopicFull topicFull = this.getFullTopic(type, alias, topic);
return this.changeVisibility(type, alias, topicFull, visibility, propagate);
}
public TopicFull changeVisibility(String type, String alias, String id, Visibility visibility, Boolean propagate) {
Topic topic = this.find(id);
return this.changeVisibility(type, alias, topic, visibility, propagate);
}
}