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

54 lines
2.1 KiB
Java

package eu.dnetlib.uoamonitorservice.service;
import eu.dnetlib.uoamonitorservice.dao.TopicDAO;
import eu.dnetlib.uoamonitorservice.dto.TopicFull;
import eu.dnetlib.uoamonitorservice.entities.Indicator;
import eu.dnetlib.uoamonitorservice.entities.Stakeholder;
import eu.dnetlib.uoamonitorservice.entities.Topic;
import eu.dnetlib.uoamonitorservice.generics.Common;
import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException;
import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.method.P;
import org.springframework.stereotype.Service;
import java.util.stream.Collectors;
@Service
public class TopicService {
private final TopicDAO dao;
private final CategoryService categoryService;
private final CommonService commonService;
@Autowired
public TopicService(TopicDAO dao, CategoryService categoryService, CommonService commonService) {
this.dao = dao;
this.categoryService = categoryService;
this.commonService = commonService;
}
public Topic findByPath(Stakeholder stakeholder, String topicId, String message) {
if (!stakeholder.getTopics().contains(topicId)) {
throw new PathNotValidException(message + ": Topic with id: " + topicId + " not found in Stakeholder: " + stakeholder.getId());
}
return this.dao.findById(topicId).orElseThrow(() -> new EntityNotFoundException(message + ": 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"));
}
}