[Trunk | Monitor Service]: StakeholderController.java & TopicController.java & CategoryController.java & SubCategoryController.java & SectionController.java & IndicatorController.java: Updated /change-visibility methods and added @RequestParam(required = false) Boolean propagate, to allow changing visibility of all contents of the updated entity too & updated return type to return the entity instead its visibility & added everywhere method "changeVisibilityTree()".

This commit is contained in:
Konstantina Galouni 2022-09-14 18:02:47 +03:00
parent 902a666c8e
commit 82a8f8ad56
6 changed files with 154 additions and 75 deletions

View File

@ -450,26 +450,13 @@ public class CategoryController {
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/change-visibility", method = RequestMethod.POST)
public Visibility changeCategoryVisibility(@PathVariable("stakeholderId") String stakeholderId,
public Category changeCategoryVisibility(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId,
@RequestParam("visibility") Visibility visibility) {
log.debug("change category visibility: "+visibility);
@RequestParam("visibility") Visibility visibility, @RequestParam(required = false) Boolean propagate) {
log.debug("change category visibility: "+visibility + " - toggle propagate: "+((propagate != null && propagate) ? "true" : "false"));
log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId);
Category category = categoryDAO.findById(categoryId);
if (category == null) {
// EXCEPTION - Category not found
throw new EntityNotFoundException("Change topic visibility: Category with id: "+categoryId+" not found");
}
category.setVisibility(visibility);
this.toggleCategory(stakeholderId, topicId, category);
return category.getVisibility();
}
public void toggleCategory(String stakeholderId, String topicId, Category category) {
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
if (stakeholder != null) {
@ -483,12 +470,11 @@ public class CategoryController {
Topic<String> topic = topicDAO.findById(topicId);
if (topic != null) {
if (stakeholder.getTopics().contains(topicId)) {
if (topic.getCategories().contains(category.getId())) {
categoryDAO.save(category);
log.debug("Category toggled!");
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: "+category.getId()+" not found in Topic: "+topicId);
throw new PathNotValidException("Toggle category: Category with id: "+categoryId+" not found in Topic: "+topicId);
}
} else {
// EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
@ -504,6 +490,32 @@ public class CategoryController {
}
}
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) {

View File

@ -797,7 +797,7 @@ public class IndicatorController {
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{indicatorId}/change-visibility", method = RequestMethod.POST)
public Visibility changeTopicVisibility(@PathVariable("stakeholderId") String stakeholderId,
public Indicator changeIndicatorVisibility(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId,
@PathVariable("subcategoryId") String subcategoryId,
@ -810,27 +810,34 @@ public class IndicatorController {
Indicator indicator = indicatorDAO.findById(indicatorId);
if (indicator == null) {
// EXCEPTION - Indicator not found
throw new EntityNotFoundException("Change indicator visibility: Indicator with id: "+indicatorId+" not found");
throw new EntityNotFoundException("Change indicator visibility: Indicator with id: " + indicatorId + " not found");
}
indicator.setVisibility(visibility);
this.toggleIndicator(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator);
return indicator.getVisibility();
}
public void toggleIndicator(String stakeholderId, String topicId, String categoryId, String subcategoryId, String sectionId, Indicator indicator) {
Section<String> section = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator.getType());
List<String> indicators = section.getIndicators();
if(indicators.contains(indicator.getId())) {
indicatorDAO.save(indicator);
log.debug("Indicator toggled!");
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: "+indicator.getId()+" not found in Section: "+sectionId);
throw new PathNotValidException("Toggle indicators: Indicator with id: "+indicatorId+" not found in Section: "+sectionId);
}
}
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) {

View File

@ -542,4 +542,22 @@ public class SectionController {
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,7 +1,6 @@
package eu.dnetlib.uoamonitorservice.controllers;
import eu.dnetlib.uoaadmintoolslibrary.entities.Portal;
import eu.dnetlib.uoaadmintoolslibrary.entities.PortalType;
import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils;
import eu.dnetlib.uoaadmintoolslibrary.services.PortalService;
import eu.dnetlib.uoamonitorservice.dao.*;
@ -12,7 +11,6 @@ import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.AuthorizationServiceException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
@ -690,9 +688,9 @@ public class StakeholderController {
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/change-visibility", method = RequestMethod.POST)
public Visibility toggleStakeholderAccess(@PathVariable("stakeholderId") String stakeholderId,
@RequestParam("visibility") Visibility visibility) {
log.debug("change stakeholder visibility: "+visibility);
public Visibility changeStakeholderVisibility(@PathVariable("stakeholderId") String stakeholderId,
@RequestParam("visibility") Visibility visibility, @RequestParam(required = false) Boolean propagate) {
log.debug("change stakeholder visibility: "+visibility + " - toggle propagate: "+((propagate != null && propagate) ? "true" : "false"));
log.debug("Stakeholder: "+stakeholderId);
Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId);
@ -711,11 +709,20 @@ public class StakeholderController {
// EXCEPTION - Access denied
throw new ForbiddenException("Change stakeholder visibility: You are not authorized to update stakeholder with id: "+stakeholderId);
}
return changeStakeholderVisibilityTree(stakeholder, visibility, propagate);
}
private Visibility changeStakeholderVisibilityTree(Stakeholder<String> stakeholder, Visibility visibility, Boolean propagate) {
if(propagate != null && propagate) {
for (String topicId : stakeholder.getTopics()) {
topicController.changeVisibilityTree(topicId, visibility, propagate);
}
}
stakeholder.setVisibility(visibility);
stakeholderDAO.save(stakeholder);
log.debug("Stakeholder toggled!");
return stakeholder.getVisibility();
}

View File

@ -436,12 +436,12 @@ public class SubCategoryController {
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/change-visibility", method = RequestMethod.POST)
public Visibility changeTopicVisibility(@PathVariable("stakeholderId") String stakeholderId,
public SubCategory changeSubCategoryVisibility(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId,
@PathVariable("subcategoryId") String subcategoryId,
@RequestParam("visibility") Visibility visibility) {
log.debug("change subCategory visibility: "+visibility);
@RequestParam("visibility") Visibility visibility, @RequestParam(required = false) Boolean propagate) {
log.debug("change subCategory visibility: "+visibility + " - toggle propagate: "+((propagate != null && propagate) ? "true" : "false"));
log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId);
SubCategory subCategory = subCategoryDAO.findById(subcategoryId);
@ -449,25 +449,45 @@ public class SubCategoryController {
// EXCEPTION - SubCategory not found
throw new EntityNotFoundException("Change subCategory visibility: SubCategory with id: "+subcategoryId+" not found");
}
subCategory.setVisibility(visibility);
this.toggleSubCategory(stakeholderId, topicId, categoryId, subCategory);
return subCategory.getVisibility();
}
public void toggleSubCategory(String stakeholderId, String topicId, String categoryId, SubCategory subcategory) {
Category category = checkForExceptions(stakeholderId, topicId, categoryId);
if (category.getSubCategories().contains(subcategory.getId())) {
subCategoryDAO.save(subcategory);
log.debug("SubCategory toggled!");
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: "+subcategory.getId()+" not found in Category: "+categoryId);
throw new PathNotValidException("Toggle subCategory: SubCategory with id: "+subcategoryId+" not found in Category: "+categoryId);
}
}
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) {

View File

@ -8,7 +8,6 @@ import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException;
import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
@ -429,25 +428,12 @@ public class TopicController {
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/change-visibility", method = RequestMethod.POST)
public Visibility changeTopicVisibility(@PathVariable("stakeholderId") String stakeholderId,
public Topic changeTopicVisibility(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@RequestParam("visibility") Visibility visibility) {
log.debug("change topic visibility: "+visibility);
@RequestParam("visibility") Visibility visibility, @RequestParam(required = false) Boolean propagate) {
log.debug("change topic visibility: "+visibility + " - toggle propagate: "+((propagate != null && propagate) ? "true" : "false"));
log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId);
Topic topic = topicDAO.findById(topicId);
if (topic == null) {
// EXCEPTION - Topic not found
throw new EntityNotFoundException("Change topic visibility: Topic with id: "+topicId+" not found");
}
topic.setVisibility(visibility);
this.toggleTopic(stakeholderId, topic);
return topic.getVisibility();
}
public void toggleTopic(String stakeholderId, Topic topic) {
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
if (stakeholder != null) {
@ -458,17 +444,46 @@ public class TopicController {
throw new ForbiddenException("Toggle topic: You are not authorized to update stakeholder with id: "+stakeholderId);
}
if (stakeholder.getTopics().contains(topic.getId())) {
topicDAO.save(topic);
log.debug("Topic toggled!");
if (stakeholder.getTopics().contains(topicId)) {
return changeVisibilityTree(topicId, visibility, propagate);
} else {
// EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
throw new PathNotValidException("Toggle topic: Topic with id: "+topic.getId()+" not found in Stakeholder: "+stakeholderId);
throw new PathNotValidException("Toggle topic: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
}
} else {
// EXCEPTION - Stakeholder not found
throw new EntityNotFoundException("Toggle topic: Stakeholder with id: "+stakeholderId+" not found");
}
// this.toggleTopic(stakeholderId, topic);
}
public Topic changeVisibilityTree(String topicId, Visibility visibility, Boolean propagate) {
Topic<String> topic = topicDAO.findById(topicId);
if (topic == null) {
// EXCEPTION - Topic not found
throw new EntityNotFoundException("Change topic visibility: Topic with id: "+topicId+" not found");
}
Topic<Category> topicFull = new Topic(topic);
List<Category> categoriesFull = new ArrayList<>();
if(propagate != null && propagate) {
for (String categoryId : topic.getCategories()) {
categoriesFull.add(categoryController.changeVisibilityTree(categoryId, visibility, propagate));
}
}
topic.setVisibility(visibility);
topicDAO.save(topic);
log.debug("Topic toggled!");
topicFull.setVisibility(visibility);
topicFull.setCategories(categoriesFull);
return topicFull;
}
public void deleteTree(Stakeholder stakeholder) {