From c3cc942332e3f1bae9237a2af73c19315619f885 Mon Sep 17 00:00:00 2001 From: "k.triantafyllou" Date: Thu, 21 Mar 2024 12:42:34 +0200 Subject: [PATCH] Refactor save topic endpoint and fix change visibility. --- .../controllers/StakeholderController.java | 5 +- .../controllers/TopicController.java | 130 +----------------- .../entities/Stakeholder.java | 8 ++ .../uoamonitorservice/entities/Topic.java | 27 ++++ .../uoamonitorservice/generics/Common.java | 13 ++ .../generics/TopicGeneric.java | 13 +- .../service/CategoryService.java | 5 - .../service/IndicatorService.java | 5 - .../service/SectionService.java | 7 +- .../service/StakeholderService.java | 22 +-- .../service/SubCategoryService.java | 6 - .../service/TopicService.java | 45 +++++- 12 files changed, 110 insertions(+), 176 deletions(-) diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java index d72bd06..cc0e850 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java @@ -126,8 +126,9 @@ public class StakeholderController { @RequestMapping(value = "/{stakeholderId}/change-visibility", method = RequestMethod.POST) public StakeholderFull changeStakeholderVisibility(@PathVariable("stakeholderId") String stakeholderId, @RequestParam("visibility") Visibility visibility, @RequestParam(defaultValue = "false") Boolean propagate) { - log.debug("change stakeholder visibility: " + visibility + " - toggle propagate: " + ((propagate != null && propagate) ? "true" : "false")); + log.debug("change stakeholder visibility: " + visibility + " - toggle propagate: " + propagate); log.debug("Stakeholder: " + stakeholderId); - return this.stakeholderService.changeVisibility(stakeholderId, visibility, propagate); + Stakeholder stakeholder = this.stakeholderService.findByPath(stakeholderId); + return this.stakeholderService.changeVisibility(stakeholder, visibility, propagate); } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/TopicController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/TopicController.java index c0b1546..1c9ff6a 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/TopicController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/TopicController.java @@ -26,133 +26,15 @@ public class TopicController { this.stakeholderService = stakeholderService; } - /*@PreAuthorize("isAuthenticated()") + @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/save", method = RequestMethod.POST) - public Topic saveTopic(@PathVariable("stakeholderId") String stakeholderId, - @RequestBody Topic topicFull) { + public TopicFull saveTopic(@PathVariable("stakeholderId") String stakeholderId, @RequestBody TopicFull topicFull) { log.debug("save topic"); - log.debug("Alias: "+topicFull.getAlias() + " - Id: "+topicFull.getId()+ " - Stakeholder: "+stakeholderId); - - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); - - if(stakeholder != null) { - if(!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { - // EXCEPTION - Access denied - throw new ForbiddenException("Save Topic: You are not authorized to update stakeholder with id: "+stakeholderId); - } - - Topic topic = new Topic<>(topicFull); - Date date = new Date(); - topic.setUpdateDate(date); - topicFull.setUpdateDate(date); - - List categories = new ArrayList<>(); - - Topic oldTopic = null; - if(topicFull.getId() != null) { - oldTopic = topicDAO.findById(topicFull.getId()); - if(oldTopic == null) { - // EXCEPTION - Topic not found - throw new EntityNotFoundException("save topic: Topic with id: "+topicFull.getId()+" not found"); - } - for(String categoryId : oldTopic.getCategories()) { - Category category = categoryDAO.findById(categoryId); - if (category == null) { - // EXCEPTION - Category not found - throw new EntityNotFoundException("Save topic: Category with id: "+categoryId+" not found (category exists in topic: "+topic.getId()+")"); - } - categories.add(category.getId()); - } - } else { // topic does not exist in DB - topic.setCreationDate(date); - topicFull.setCreationDate(date); - - for(Category category : topicFull.getCategories()) { - categories.add(category.getId()); - } - } - - topic.setCategories(categories); - - if(stakeholder.getDefaultId() == null) { - if(topicFull.getId() == null) { - topicDAO.save(topic); - onSaveDefaultTopic(topic, stakeholderId); - } else { - onUpdateDefaultTopic(topic, oldTopic); - topicDAO.save(topic); - } - } else { - topicDAO.save(topic); - } - - List topics = stakeholder.getTopics(); - int index = topics.indexOf(topic.getId()); - if(index == -1) { - topics.add(topic.getId()); - stakeholderDAO.save(stakeholder); - log.debug("Topic saved!"); - - topicFull.setId(topic.getId()); - } - } else { - // EXCEPTION - Stakeholder not found - throw new EntityNotFoundException("Save topic: Stakeholder with id: "+stakeholderId+" not found"); - } - return topicFull; + log.debug("Alias: " + topicFull.getAlias() + " - Id: " + topicFull.getId() + " - Stakeholder: " + stakeholderId); + Stakeholder stakeholder = this.stakeholderService.findByPath(stakeholderId); + return this.topicService.save(stakeholder, new Topic(topicFull)); } - public void onSaveDefaultTopic(Topic topic, String stakeholderId) { - log.debug("On save default topic"); - - List stakeholders = stakeholderDAO.findByDefaultId(stakeholderId); - for(Stakeholder _stakeholder : stakeholders) { - Topic topicNew = new Topic(); - topicNew.copyFromDefault(topic); - - topicDAO.save(topicNew); - - List topics = _stakeholder.getTopics(); - topics.add(topicNew.getId()); - - stakeholderDAO.save(_stakeholder); - } - } - - public void onUpdateDefaultTopic(Topic topic, Topic oldTopic) { - log.debug("On update default topic"); - - List topics = topicDAO.findByDefaultId(topic.getId()); - boolean changed = false; - for(Topic topicBasedOnDefault : topics) { - if(topic.getName() != null && !topic.getName().equals(topicBasedOnDefault.getName()) - && (oldTopic.getName() == null || oldTopic.getName().equals(topicBasedOnDefault.getName()))) { - - topicBasedOnDefault.setName(topic.getName()); - topicBasedOnDefault.setAlias(topic.getAlias()); - changed = true; - } - if(topic.getDescription() != null && !topic.getDescription().equals(topicBasedOnDefault.getDescription()) - && (oldTopic.getDescription() == null || oldTopic.getDescription().equals(topicBasedOnDefault.getDescription()))) { - - topicBasedOnDefault.setDescription(topic.getDescription()); - changed = true; - } - if(topic.getIcon() != null && !topic.getIcon().equals(topicBasedOnDefault.getIcon()) - && (oldTopic.getIcon() == null || oldTopic.getIcon().equals(topicBasedOnDefault.getIcon()))) { - - topicBasedOnDefault.setIcon(topic.getIcon()); - changed = true; - } - - if(!changed) { - continue; - } - topicBasedOnDefault.setUpdateDate(topic.getUpdateDate()); - topicDAO.save(topicBasedOnDefault); - } - }*/ - @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/delete", method = RequestMethod.DELETE) public boolean deleteTopic(@PathVariable("stakeholderId") String stakeholderId, @@ -214,7 +96,7 @@ public class TopicController { @RequestMapping(value = "/{stakeholderId}/{topicId}/change-visibility", method = RequestMethod.POST) public TopicFull changeTopicVisibility(@PathVariable("stakeholderId") String stakeholderId, @PathVariable("topicId") String topicId, - @RequestParam("visibility") Visibility visibility, @RequestParam(required = false) Boolean propagate) { + @RequestParam("visibility") Visibility visibility, @RequestParam(defaultValue = "false") Boolean propagate) { log.debug("change topic visibility: " + visibility + " - toggle propagate: " + propagate); log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId); Stakeholder stakeholder = this.stakeholderService.findById(stakeholderId); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Stakeholder.java b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Stakeholder.java index 184dc8c..319b1c2 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Stakeholder.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Stakeholder.java @@ -20,4 +20,12 @@ public class Stakeholder extends StakeholderGeneric { this.topics = stakeholder.getTopics().stream().map(Common::getId).collect(Collectors.toList()); this.topics.removeIf(Objects::isNull); } + + public void addTopic(String id) { + this.topics.add(id); + } + + public void removeTopic(String id) { + this.topics.remove(id); + } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Topic.java b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Topic.java index e11ccb2..19eef8d 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Topic.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Topic.java @@ -5,6 +5,8 @@ import eu.dnetlib.uoamonitorservice.generics.Common; import eu.dnetlib.uoamonitorservice.generics.TopicGeneric; import org.springframework.data.mongodb.core.mapping.Document; +import java.util.ArrayList; +import java.util.List; import java.util.Objects; import java.util.stream.Collectors; @@ -15,9 +17,34 @@ public class Topic extends TopicGeneric { super(); } + public Topic(Topic topic) { + super(topic); + } + public Topic(TopicFull topic) { super(topic); this.categories = topic.getCategories().stream().map(Common::getId).collect(Collectors.toList()); this.categories.removeIf(Objects::isNull); } + + public Topic(TopicFull topic, List categories) { + super(topic); + this.categories = new ArrayList<>(categories); + } + + public Topic copy() { + Topic topic = new Topic(this); + topic.setDefaultId(this.getId()); + topic.setId(null); + return topic; + } + + public Topic override(Topic topic, Topic old) { + topic = (Topic) super.override(topic, old); + if(this.getIcon() != null && !this.getIcon().equals(topic.getIcon()) && + (old.getIcon() == null || old.getIcon().equals(topic.getIcon()))) { + topic.setIcon(this.getIcon()); + } + return topic; + } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/generics/Common.java b/src/main/java/eu/dnetlib/uoamonitorservice/generics/Common.java index 8a77b1e..2075c09 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/generics/Common.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/generics/Common.java @@ -29,6 +29,19 @@ public class Common { this.updateDate = common.getUpdateDate(); } + public Common override(Common common, Common old) { + if(this.getName() != null && !this.getName().equals(common.getName()) && + (old.getName() == null || old.getName().equals(common.getName()))) { + common.setName(this.getName()); + common.setAlias(this.getAlias()); + } + if(this.getDescription() != null && !this.getDescription().equals(common.getDescription()) && + (old.getDescription() == null || old.getDescription().equals(common.getDescription()))) { + common.setDescription(this.getDescription()); + } + return common; + } + public String getId() { return id; } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/generics/TopicGeneric.java b/src/main/java/eu/dnetlib/uoamonitorservice/generics/TopicGeneric.java index 7dc1104..af48845 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/generics/TopicGeneric.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/generics/TopicGeneric.java @@ -27,18 +27,7 @@ public class TopicGeneric extends Common { creationDate = topic.getCreationDate(); updateDate = topic.getUpdateDate(); defaultId = topic.getDefaultId(); - } - - public void copyFromDefault(TopicGeneric defaultTopic) { - setName(defaultTopic.getName()); - setAlias(defaultTopic.getAlias()); - setDescription(defaultTopic.getDescription()); - setIcon(defaultTopic.getIcon()); - setVisibility(defaultTopic.getVisibility()); - setCreationDate(defaultTopic.getCreationDate()); - setUpdateDate(defaultTopic.getUpdateDate()); - setDefaultId(defaultTopic.getId()); - setCategories(new ArrayList<>()); + categories = new ArrayList<>(); } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/CategoryService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/CategoryService.java index be68f64..7e97767 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/CategoryService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/CategoryService.java @@ -116,9 +116,4 @@ public class CategoryService { 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); - } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/IndicatorService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/IndicatorService.java index 9f8dbec..7030aa1 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/IndicatorService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/IndicatorService.java @@ -89,9 +89,4 @@ public class IndicatorService { 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); - } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/SectionService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/SectionService.java index 780f254..696c54e 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/SectionService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/SectionService.java @@ -35,7 +35,7 @@ public class SectionService { } public Section findByPath(SubCategory subCategory, String sectionId) { - if (!subCategory.getNumbers().contains(sectionId) || !subCategory.getCharts().contains(sectionId)) { + if (!subCategory.getNumbers().contains(sectionId) && !subCategory.getCharts().contains(sectionId)) { throw new PathNotValidException("Section with id: " + sectionId + " not found in SubCategory: " + subCategory.getId()); } return this.dao.findById(sectionId).orElseThrow(() -> new EntityNotFoundException("Section with id: " + sectionId + " not found")); @@ -116,9 +116,4 @@ public class SectionService { 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); - } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java index 57b031c..56259b0 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java @@ -116,21 +116,23 @@ public class StakeholderService { } } - public StakeholderFull changeVisibility(String id, Visibility visibility, Boolean propagate) { - Stakeholder stakeholder = this.findById(id); - stakeholder.setVisibility(visibility); + public StakeholderFull changeVisibility(StakeholderFull stakeholder, Visibility visibility, Boolean propagate) { if(this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) { - StakeholderFull stakeholderFull = this.getFullStakeholder(stakeholder); if(propagate) { - stakeholderFull.setTopics(stakeholderFull.getTopics().stream(). - map(topic -> this.topicService.changeVisibility(stakeholderFull.getType(), stakeholderFull.getAlias(), topic.getId(), visibility, true)) + stakeholder.setTopics(stakeholder.getTopics().stream(). + map(topic -> this.topicService.changeVisibility(stakeholder.getType(), stakeholder.getAlias(), topic, visibility, true)) .collect(Collectors.toList())); } - stakeholder = this.save(stakeholder); - stakeholderFull.update(stakeholder); - return stakeholderFull; + stakeholder.setVisibility(visibility); + stakeholder.update(this.save(new Stakeholder(stakeholder))); + return stakeholder; } else { - throw new ForbiddenException("Change stakeholder visibility: You are not authorized to update stakeholder with id: " + id); + throw new ForbiddenException("Change stakeholder visibility: You are not authorized to update stakeholder with id: " + stakeholder.getId()); } } + + public StakeholderFull changeVisibility(Stakeholder stakeholder, Visibility visibility, Boolean propagate) { + StakeholderFull stakeholderFull = this.getFullStakeholder(stakeholder); + return this.changeVisibility(stakeholderFull, visibility, propagate); + } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/SubCategoryService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/SubCategoryService.java index a81560e..f0da766 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/SubCategoryService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/SubCategoryService.java @@ -135,10 +135,4 @@ public class SubCategoryService { 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); - } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/TopicService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/TopicService.java index 3fd8657..93b46f5 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/TopicService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/TopicService.java @@ -12,6 +12,7 @@ import eu.dnetlib.uoamonitorservice.primitives.Visibility; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.List; import java.util.stream.Collectors; @Service @@ -63,6 +64,38 @@ public class TopicService { return topicFull; } + public TopicFull save(Stakeholder stakeholder, Topic topic) { + if(topic.getId() != null) { + if(this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) { + this.updateChildren(topic); + topic = this.save(topic); + } else { + throw new ForbiddenException("You are not authorized to update stakeholder with id: " + stakeholder.getId()); + } + } else { + if(this.commonService.hasCreateAuthority(stakeholder.getType())) { + this.createChildren(stakeholder, topic); + topic = this.save(topic); + this.addTopic(stakeholder, topic.getId()); + } else { + throw new ForbiddenException("You are not authorized to create a topic in stakeholder with id: " + stakeholder.getId()); + } + } + return this.getFullTopic(stakeholder.getType(), stakeholder.getAlias(), topic); + } + + public void createChildren(Stakeholder defaultStakeholder, Topic topic) { + this.stakeholderDAO.findByDefaultId(defaultStakeholder.getId()).forEach(stakeholder -> { + this.save(stakeholder, topic.copy()); + }); + } + + public void updateChildren(Topic topic) { + this.dao.findByDefaultId(topic.getId()).forEach(child -> { + this.save(topic.override(child, this.find(topic.getId()))); + }); + } + public Topic save(Topic topic) { topic.getCategories().forEach(this.categoryService::find); return this.dao.save(topic); @@ -90,9 +123,14 @@ public class TopicService { this.delete(type, topic, remove); } + public void addTopic(Stakeholder stakeholder, String id) { + stakeholder.addTopic(id); + this.stakeholderDAO.save(stakeholder); + } + public void removeTopic(String id) { this.stakeholderDAO.findByTopicsContaining(id).forEach(stakeholder -> { - stakeholder.getTopics().remove(id); + stakeholder.removeTopic(id); this.stakeholderDAO.save(stakeholder); }); } @@ -116,9 +154,4 @@ public class TopicService { 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); - } }