uoa-monitor-service/src/main/java/eu/dnetlib/uoamonitorservice/controllers/TopicController.java

224 lines
9.7 KiB
Java

package eu.dnetlib.uoamonitorservice.controllers;
import eu.dnetlib.uoamonitorservice.dto.TopicFull;
import eu.dnetlib.uoamonitorservice.entities.Stakeholder;
import eu.dnetlib.uoamonitorservice.entities.Topic;
import eu.dnetlib.uoamonitorservice.primitives.Visibility;
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.*;
@RestController
@CrossOrigin(origins = "*")
public class TopicController {
private final Logger log = LogManager.getLogger(this.getClass());
private TopicService topicService;
private StakeholderService stakeholderService;
@Autowired
public TopicController(TopicService topicService, StakeholderService stakeholderService) {
this.topicService = topicService;
this.stakeholderService = stakeholderService;
}
/*@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/save", method = RequestMethod.POST)
public Topic<Category> saveTopic(@PathVariable("stakeholderId") String stakeholderId,
@RequestBody Topic<Category> 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;
}
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,
@PathVariable("topicId") String topicId,
@RequestParam(required = false) String children) {
log.debug("delete topic");
log.debug("Id: " + topicId + " - Stakeholder: " + stakeholderId);
Stakeholder stakeholder = stakeholderService.findById(stakeholderId);
Topic topic = this.topicService.findByPath(stakeholder, topicId);
this.topicService.delete(stakeholder.getType(), topic, true);
return true;
}
/*@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/reorder", method = RequestMethod.POST)
public List<Topic> reorderTopics(@PathVariable("stakeholderId") String stakeholderId,
@RequestBody List<String> topics) {
log.debug("reorder topics");
log.debug("Stakeholder: "+stakeholderId);
Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId);
if(stakeholder != null) {
if(!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) {
// EXCEPTION - Access denied
throw new ForbiddenException("Reorder topics: You are not authorized to update stakeholder with id: "+stakeholderId);
}
List<String> oldTopics = stakeholder.getTopics();
for (String topicId : oldTopics) {
if (!topics.contains(topicId)) {
topics.add(topicId);
}
}
stakeholder.setTopics(topics);
List<Topic> topicsFull = new ArrayList<>();
for (String topicId : topics) {
Topic topic = topicDAO.findById(topicId);
if(topic == null) {
// EXCEPTION - Topic not found
throw new EntityNotFoundException("Reorder Topics: Topic with id: " + topicId + " not found");
}
topicsFull.add(topic);
}
stakeholderDAO.save(stakeholder);
log.debug("Topics reordered!");
return topicsFull;
} else {
// EXCEPTION - Stakeholder not found
throw new EntityNotFoundException("Reorder topics: Stakeholder with id: "+stakeholderId+" not found");
}
}*/
@PreAuthorize("isAuthenticated()")
@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) {
log.debug("change topic visibility: " + visibility + " - toggle propagate: " + ((propagate != null && propagate) ? "true" : "false"));
log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId);
Stakeholder stakeholder = this.stakeholderService.findById(stakeholderId);
return this.topicService.changeVisibility(stakeholder.getType(), stakeholder.getAlias(), topicId, visibility, propagate);
}
}