192 lines
8.6 KiB
Java
192 lines
8.6 KiB
Java
package eu.dnetlib.uoamonitorservice.controllers;
|
|
|
|
import eu.dnetlib.uoamonitorservice.dao.*;
|
|
import eu.dnetlib.uoamonitorservice.entities.*;
|
|
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 CategoryController {
|
|
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 SubCategoryController subCategoryController;
|
|
|
|
public Category<SubCategory> buildCategory(Category<SubCategory> categoryFull) {
|
|
Category<String> category = new Category<>(categoryFull);
|
|
|
|
List<String> subCategories = new ArrayList<>();
|
|
List<SubCategory> subCategoriesFull = new ArrayList<>();
|
|
for(SubCategory<Indicator> subCategory : categoryFull.getSubCategories()) {
|
|
SubCategory<Indicator> subcategoryFull = subCategoryController.buildSubCategory(subCategory);
|
|
subCategoriesFull.add(subcategoryFull);
|
|
subCategories.add(subcategoryFull.getId());
|
|
}
|
|
categoryFull.setSubCategories(subCategoriesFull);
|
|
category.setSubCategories(subCategories);
|
|
|
|
Category<String> categorySaved = categoryDAO.save(category);
|
|
|
|
categoryFull.setId(categorySaved.getId());
|
|
return categoryFull;
|
|
}
|
|
|
|
@RequestMapping(value = "/{stakeholderId}/{topicId}/save", method = RequestMethod.POST)
|
|
public Category<SubCategory> saveCategory(@PathVariable("stakeholderId") String stakeholderId,
|
|
@PathVariable("topicId") String topicId,
|
|
@RequestBody Category<SubCategory> categoryFull) {
|
|
log.debug("save category");
|
|
|
|
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
|
|
|
|
if(stakeholder != null) {
|
|
|
|
Topic<String> topic = topicDAO.findById(topicId);
|
|
if(topic != null) {
|
|
if(stakeholder.getTopics().contains(topicId)) {
|
|
// if category not exists (no id), create a new default subcategory, identical to category
|
|
if(categoryFull.getId() == null) {
|
|
SubCategory<String> subCategory = new SubCategory<>();
|
|
subCategory.setName(categoryFull.getName());
|
|
subCategory.setAlias(categoryFull.getAlias());
|
|
subCategory.setIsActive(categoryFull.getIsActive());
|
|
subCategory.setIsPublic(categoryFull.getIsPublic());
|
|
subCategory.setIsDefault(categoryFull.getIsDefault());
|
|
subCategory.setCharts(new ArrayList<String>());
|
|
subCategory.setNumbers(new ArrayList<String>());
|
|
|
|
SubCategory<String> subCategorySaved = subCategoryDAO.save(subCategory);
|
|
List<SubCategory> subCategories = categoryFull.getSubCategories();
|
|
subCategories.add(subCategorySaved);
|
|
}
|
|
|
|
|
|
Category<String> category = new Category<>(categoryFull);
|
|
|
|
List<String> subCategories = new ArrayList<>();
|
|
for(SubCategory subCategory : categoryFull.getSubCategories()) {
|
|
subCategories.add(subCategory.getId());
|
|
}
|
|
category.setSubCategories(subCategories);
|
|
|
|
Category<String> categorySaved = categoryDAO.save(category);
|
|
|
|
List<String> categories = topic.getCategories();
|
|
int index = categories.indexOf(categorySaved.getId());
|
|
if(index == -1) {
|
|
categories.add(categorySaved.getId());
|
|
topicDAO.save(topic);
|
|
log.debug("Category saved!");
|
|
|
|
categoryFull.setId(categorySaved.getId());
|
|
}
|
|
|
|
subCategories = null;
|
|
category = null;
|
|
} else {
|
|
// EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
|
|
throw new PathNotValidException("Save category: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
|
|
}
|
|
} else {
|
|
// EXCEPTION - Topic not found
|
|
throw new EntityNotFoundException("Save category: Topic with id: "+topicId+" not found");
|
|
}
|
|
} else {
|
|
// EXCEPTION - Stakeholder not found
|
|
throw new EntityNotFoundException("Save category: Stakeholder with id: "+stakeholderId+" not found");
|
|
}
|
|
return categoryFull;
|
|
}
|
|
|
|
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/delete", method = RequestMethod.DELETE)
|
|
public boolean deleteCategory(@PathVariable("stakeholderId") String stakeholderId,
|
|
@PathVariable("topicId") String topicId,
|
|
@PathVariable("categoryId") String categoryId) {
|
|
log.debug("delete category");
|
|
|
|
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
|
|
|
|
if(stakeholder != null) {
|
|
|
|
Topic<String> topic = topicDAO.findById(topicId);
|
|
if(topic != null) {
|
|
if(stakeholder.getTopics().contains(topicId)) {
|
|
|
|
Category<String> category = categoryDAO.findById(categoryId);
|
|
if(category != null) {
|
|
|
|
List<String> categories = topic.getCategories();
|
|
int index = categories.indexOf(categoryId);
|
|
if(index != -1) {
|
|
for(String subCategoryId : category.getSubCategories()) {
|
|
SubCategory<String> subcategory = subCategoryDAO.findById(subCategoryId);
|
|
if(subcategory == null) {
|
|
// EXCEPTION - SubCategory not found
|
|
throw new EntityNotFoundException("Delete category: 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);
|
|
|
|
categories.remove(index);
|
|
topicDAO.save(topic);
|
|
|
|
categoryDAO.delete(categoryId);
|
|
log.debug("Category deleted!");
|
|
} else {
|
|
// EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
|
|
throw new PathNotValidException("Delete category: Category with id: "+categoryId+" not found in Topic: "+topicId);
|
|
}
|
|
|
|
} else {
|
|
// EXCEPTION - Category not found
|
|
throw new EntityNotFoundException("Delete category: Category with id: "+categoryId+" not found");
|
|
}
|
|
} else {
|
|
// EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
|
|
throw new PathNotValidException("Delete category: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
|
|
}
|
|
} else {
|
|
// EXCEPTION - Topic not found
|
|
throw new EntityNotFoundException("Delete category: Topic with id: "+topicId+" not found");
|
|
}
|
|
} else {
|
|
// EXCEPTION - Stakeholder not found
|
|
throw new EntityNotFoundException("Delete category: Stakeholder with id: "+stakeholderId+" not found");
|
|
}
|
|
return true;
|
|
}
|
|
}
|