2024-03-08 19:55:51 +01:00
|
|
|
package eu.dnetlib.uoamonitorservice.service;
|
|
|
|
|
2024-03-12 23:20:55 +01:00
|
|
|
import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException;
|
|
|
|
import eu.dnetlib.uoamonitorservice.dao.StakeholderDAO;
|
2024-03-08 19:55:51 +01:00
|
|
|
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;
|
2024-03-12 23:20:55 +01:00
|
|
|
import eu.dnetlib.uoamonitorservice.primitives.Visibility;
|
2024-03-08 19:55:51 +01:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
@Service
|
|
|
|
public class TopicService {
|
|
|
|
|
2024-03-12 23:20:55 +01:00
|
|
|
private final StakeholderDAO stakeholderDAO;
|
2024-03-08 19:55:51 +01:00
|
|
|
private final TopicDAO dao;
|
|
|
|
|
|
|
|
private final CategoryService categoryService;
|
|
|
|
private final CommonService commonService;
|
|
|
|
|
|
|
|
@Autowired
|
2024-03-12 23:20:55 +01:00
|
|
|
public TopicService(StakeholderDAO stakeholderDAO, TopicDAO dao, CategoryService categoryService, CommonService commonService) {
|
|
|
|
this.stakeholderDAO = stakeholderDAO;
|
2024-03-08 19:55:51 +01:00
|
|
|
this.dao = dao;
|
|
|
|
this.categoryService = categoryService;
|
|
|
|
this.commonService = commonService;
|
|
|
|
}
|
|
|
|
|
2024-03-12 23:20:55 +01:00
|
|
|
public Topic findByPath(Stakeholder stakeholder, String topicId) {
|
2024-03-08 19:55:51 +01:00
|
|
|
if (!stakeholder.getTopics().contains(topicId)) {
|
2024-03-12 23:20:55 +01:00
|
|
|
throw new PathNotValidException("Topic with id: " + topicId + " not found in Stakeholder: " + stakeholder.getId());
|
2024-03-08 19:55:51 +01:00
|
|
|
}
|
2024-03-12 23:20:55 +01:00
|
|
|
return this.dao.findById(topicId).orElseThrow(() -> new EntityNotFoundException("Topic with id: " + topicId + " not found"));
|
2024-03-08 19:55:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public TopicFull getFullTopic(String type, String alias, String id) {
|
|
|
|
Topic topic = this.find(id);
|
2024-03-12 23:20:55 +01:00
|
|
|
if (commonService.hasVisibilityAuthority(type, alias, topic)) {
|
|
|
|
return new TopicFull(topic, topic.getCategories().stream()
|
|
|
|
.map(categoryId -> this.categoryService.getFullCategory(type, alias, categoryId))
|
|
|
|
.collect(Collectors.toList()));
|
2024-03-08 19:55:51 +01:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Topic find(String id) {
|
|
|
|
return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("Topic with id: " + id + " not found"));
|
|
|
|
}
|
2024-03-12 23:20:55 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2024-03-08 19:55:51 +01:00
|
|
|
}
|