Refactor save topic endpoint and fix change visibility.
This commit is contained in:
parent
cf505bd214
commit
c3cc942332
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,133 +26,15 @@ public class TopicController {
|
|||
this.stakeholderService = stakeholderService;
|
||||
}
|
||||
|
||||
/*@PreAuthorize("isAuthenticated()")
|
||||
@PreAuthorize("isAuthenticated()")
|
||||
@RequestMapping(value = "/{stakeholderId}/save", method = RequestMethod.POST)
|
||||
public Topic<Category> saveTopic(@PathVariable("stakeholderId") String stakeholderId,
|
||||
@RequestBody Topic<Category> 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<String> topic = new Topic<>(topicFull);
|
||||
Date date = new Date();
|
||||
topic.setUpdateDate(date);
|
||||
topicFull.setUpdateDate(date);
|
||||
|
||||
List<String> categories = new ArrayList<>();
|
||||
|
||||
Topic<String> 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<String> 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<Stakeholder> stakeholders = stakeholderDAO.findByDefaultId(stakeholderId);
|
||||
for(Stakeholder _stakeholder : stakeholders) {
|
||||
Topic topicNew = new Topic();
|
||||
topicNew.copyFromDefault(topic);
|
||||
|
||||
topicDAO.save(topicNew);
|
||||
|
||||
List<String> topics = _stakeholder.getTopics();
|
||||
topics.add(topicNew.getId());
|
||||
|
||||
stakeholderDAO.save(_stakeholder);
|
||||
}
|
||||
}
|
||||
|
||||
public void onUpdateDefaultTopic(Topic topic, Topic oldTopic) {
|
||||
log.debug("On update default topic");
|
||||
|
||||
List<Topic> 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);
|
||||
|
|
|
@ -20,4 +20,12 @@ public class Stakeholder extends StakeholderGeneric<String> {
|
|||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<String> {
|
|||
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<String> 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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -27,18 +27,7 @@ public class TopicGeneric<StringOrCategory> 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<>();
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue