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

113 lines
4.6 KiB
Java

package eu.dnetlib.uoamonitorservice.service;
import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException;
import eu.dnetlib.uoamonitorservice.dao.StakeholderDAO;
import eu.dnetlib.uoamonitorservice.dao.TopicDAO;
import eu.dnetlib.uoamonitorservice.dto.TopicFull;
import eu.dnetlib.uoamonitorservice.entities.Stakeholder;
import eu.dnetlib.uoamonitorservice.entities.Topic;
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;
import java.util.stream.Collectors;
@Service
public class TopicService {
private final StakeholderDAO stakeholderDAO;
private final TopicDAO dao;
private final CategoryService categoryService;
private final CommonService commonService;
@Autowired
public TopicService(StakeholderDAO stakeholderDAO, TopicDAO dao, CategoryService categoryService, CommonService commonService) {
this.stakeholderDAO = stakeholderDAO;
this.dao = dao;
this.categoryService = categoryService;
this.commonService = commonService;
}
public Topic findByPath(Stakeholder stakeholder, String topicId) {
if (!stakeholder.getTopics().contains(topicId)) {
throw new PathNotValidException("Topic with id: " + topicId + " not found in Stakeholder: " + stakeholder.getId());
}
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);
if (commonService.hasVisibilityAuthority(type, alias, topic)) {
return new TopicFull(topic, topic.getCategories().stream()
.map(categoryId -> this.categoryService.getFullCategory(type, alias, categoryId))
.collect(Collectors.toList()));
} else {
return null;
}
}
public Topic find(String id) {
return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("Topic with id: " + id + " not found"));
}
public TopicFull buildTopic(TopicFull topicFull) {
topicFull.setCategories(topicFull.getCategories().stream().map(this.categoryService::buildCategory).collect(Collectors.toList()));
topicFull.update(this.save(new Topic(topicFull)));
return topicFull;
}
public Topic save(Topic topic) {
topic.getCategories().forEach(this.categoryService::find);
return this.dao.save(topic);
}
public void delete(String type, Topic topic, boolean remove) {
if (this.commonService.hasDeleteAuthority(type)) {
this.dao.findByDefaultId(topic.getId()).forEach(child -> {
this.delete(type, child.getId(), remove);
});
topic.getCategories().forEach(categoryId -> {
this.categoryService.delete(type, categoryId, false);
});
if(remove) {
this.removeTopic(topic.getId());
}
this.dao.delete(topic);
} else {
throw new ForbiddenException("Delete topic: You are not authorized to delete topic with id: " + topic.getId());
}
}
public void delete(String type, String id, boolean remove) {
Topic topic = this.find(id);
this.delete(type, topic, remove);
}
public void removeTopic(String id) {
this.stakeholderDAO.findByTopicsContaining(id).forEach(stakeholder -> {
stakeholder.getTopics().remove(id);
this.stakeholderDAO.save(stakeholder);
});
}
public TopicFull changeVisibility(String type, String alias, String id, Visibility visibility, Boolean propagate) {
if (this.commonService.hasEditAuthority(type, alias)) {
TopicFull topicFull = this.getFullTopic(type, alias, id);
topicFull.setVisibility(visibility);
if (propagate) {
topicFull.setCategories(topicFull.getCategories().stream()
.map(category -> this.categoryService.changeVisibility(type, alias, category.getId(), visibility, true))
.collect(Collectors.toList()));
}
Topic topic = this.save(new Topic(topicFull));
topicFull.update(topic);
return topicFull;
} else {
throw new ForbiddenException("Change topic visibility: You are not authorized to update topic with id: " + id);
}
}
}