164 lines
6.3 KiB
Java
164 lines
6.3 KiB
Java
package eu.dnetlib.uoamonitorservice.controllers;
|
|
|
|
import eu.dnetlib.uoamonitorservice.dao.*;
|
|
import eu.dnetlib.uoamonitorservice.entities.Category;
|
|
import eu.dnetlib.uoamonitorservice.entities.Stakeholder;
|
|
import eu.dnetlib.uoamonitorservice.entities.SubCategory;
|
|
import eu.dnetlib.uoamonitorservice.entities.Topic;
|
|
import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException;
|
|
import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException;
|
|
import org.apache.log4j.Logger;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
@RestController
|
|
@CrossOrigin(origins = "*")
|
|
public class TopicController {
|
|
private final Logger log = Logger.getLogger(this.getClass());
|
|
|
|
@Autowired
|
|
private StakeholderDAO stakeholderDAO;
|
|
|
|
@Autowired
|
|
private TopicDAO topicDAO;
|
|
|
|
@Autowired
|
|
private CategoryDAO categoryDAO;
|
|
|
|
@Autowired
|
|
private SubCategoryDAO subCategoryDAO;
|
|
|
|
@Autowired
|
|
private IndicatorDAO indicatorDAO;
|
|
|
|
@Autowired
|
|
private CategoryController categoryController;
|
|
|
|
public Topic<Category> buildTopic(Topic<Category> topicFull) {
|
|
Topic<String> topic = new Topic<>(topicFull);
|
|
|
|
List<String> categories = new ArrayList<>();
|
|
List<Category> categoriesFull = new ArrayList<>();
|
|
for(Category<SubCategory> category : topicFull.getCategories()) {
|
|
Category<SubCategory> categoryFull = categoryController.buildCategory(category);
|
|
categoriesFull.add(categoryFull);
|
|
categories.add(categoryFull.getId());
|
|
}
|
|
topicFull.setCategories(categoriesFull);
|
|
topic.setCategories(categories);
|
|
|
|
Topic<String> topicSaved = topicDAO.save(topic);
|
|
|
|
topicFull.setId(topicSaved.getId());
|
|
return topicFull;
|
|
}
|
|
|
|
@RequestMapping(value = "/{stakeholderId}/save", method = RequestMethod.POST)
|
|
public Topic<Category> saveTopic(@PathVariable("stakeholderId") String stakeholderId,
|
|
@RequestBody Topic<Category> topicFull) {
|
|
log.debug("save topic");
|
|
|
|
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
|
|
|
|
if(stakeholder != null) {
|
|
|
|
Topic<String> topic = new Topic<>(topicFull);
|
|
|
|
List<String> categories = new ArrayList<>();
|
|
for(Category category : topicFull.getCategories()) {
|
|
categories.add(category.getId());
|
|
}
|
|
topic.setCategories(categories);
|
|
|
|
Topic<String> topicSaved = topicDAO.save(topic);
|
|
|
|
List<String> topics = stakeholder.getTopics();
|
|
int index = topics.indexOf(topicSaved.getId());
|
|
if(index == -1) {
|
|
topics.add(topicSaved.getId());
|
|
stakeholderDAO.save(stakeholder);
|
|
log.debug("Topic saved!");
|
|
|
|
topicFull.setId(topicSaved.getId());
|
|
}
|
|
|
|
categories = null;
|
|
topic = null;
|
|
} else {
|
|
// EXCEPTION - Stakeholder not found
|
|
throw new EntityNotFoundException("Save topic: Stakeholder with id: "+stakeholderId+" not found");
|
|
}
|
|
return topicFull;
|
|
}
|
|
|
|
@RequestMapping(value = "/{stakeholderId}/{topicId}/delete", method = RequestMethod.DELETE)
|
|
public boolean deleteTopic(@PathVariable("stakeholderId") String stakeholderId,
|
|
@PathVariable("topicId") String topicId) {
|
|
log.debug("delete topic");
|
|
|
|
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
|
|
|
|
if(stakeholder != null) {
|
|
|
|
Topic<String> topic = topicDAO.findById(topicId);
|
|
if(topic != null) {
|
|
|
|
List<String> topics = stakeholder.getTopics();
|
|
int index = topics.indexOf(topicId);
|
|
if(index != -1) {
|
|
for(String categoryId : topic.getCategories()) {
|
|
Category<String> category = categoryDAO.findById(categoryId);
|
|
if(category == null) {
|
|
// EXCEPTION - Category not found
|
|
throw new EntityNotFoundException("Delete topic: Category with id: "+categoryId+" not found (category exists in topic: "+topicId+")");
|
|
}
|
|
|
|
for(String subCategoryId : category.getSubCategories()) {
|
|
SubCategory<String> subcategory = subCategoryDAO.findById(subCategoryId);
|
|
if (subcategory == null) {
|
|
// EXCEPTION - SubCategory not found
|
|
throw new EntityNotFoundException("Delete stakeholder: SubCategory with id: "+subCategoryId+" not found (subcategory exists in category: "+categoryId+")");
|
|
}
|
|
|
|
for (String chartId : subcategory.getCharts()) {
|
|
indicatorDAO.delete(chartId);
|
|
}
|
|
subcategory.setCharts(null);
|
|
|
|
for (String numberId : subcategory.getNumbers()) {
|
|
indicatorDAO.delete(numberId);
|
|
}
|
|
subcategory.setNumbers(null);
|
|
|
|
subCategoryDAO.delete(subCategoryId);
|
|
}
|
|
category.setSubCategories(null);
|
|
categoryDAO.delete(categoryId);
|
|
}
|
|
topic.setCategories(null);
|
|
|
|
topics.remove(index);
|
|
stakeholderDAO.save(stakeholder);
|
|
|
|
topicDAO.delete(topicId);
|
|
log.debug("Category deleted!");
|
|
} else {
|
|
// EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
|
|
throw new PathNotValidException("Delete topic: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
|
|
}
|
|
|
|
} else {
|
|
// EXCEPTION - Topic not found
|
|
throw new EntityNotFoundException("Delete topic: Topic with id: "+topicId+" not found");
|
|
}
|
|
} else {
|
|
// EXCEPTION - Stakeholder not found
|
|
throw new EntityNotFoundException("Delete topic: Stakeholder with id: "+stakeholderId+" not found");
|
|
}
|
|
return true;
|
|
}
|
|
}
|