207 lines
9.8 KiB
Java
207 lines
9.8 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 SubCategoryController {
|
|
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;
|
|
|
|
public SubCategory<Indicator> buildSubCategory(SubCategory<Indicator> subcategoryFull) {
|
|
SubCategory<String> subCategory = new SubCategory<>(subcategoryFull);
|
|
|
|
List<String> charts = new ArrayList<>();
|
|
List<Indicator> chartsFull = new ArrayList<>();
|
|
for(Indicator chart : subcategoryFull.getCharts()) {
|
|
Indicator chartSaved = indicatorDAO.save(chart);
|
|
chart.setId(chartSaved.getId());
|
|
chartsFull.add(chart);
|
|
charts.add(chartSaved.getId());
|
|
}
|
|
subcategoryFull.setCharts(chartsFull);
|
|
subCategory.setCharts(charts);
|
|
|
|
List<String> numbers = new ArrayList<>();
|
|
List<Indicator> numbersFull = new ArrayList<>();
|
|
for(Indicator numbr : subcategoryFull.getNumbers()) {
|
|
Indicator numberSaved = indicatorDAO.save(numbr);
|
|
numbr.setId(numberSaved.getId());
|
|
numbersFull.add(numbr);
|
|
numbers.add(numberSaved.getId());
|
|
}
|
|
subcategoryFull.setNumbers(numbersFull);
|
|
subCategory.setNumbers(numbers);
|
|
|
|
SubCategory<String> subCategorySaved = subCategoryDAO.save(subCategory);
|
|
|
|
subcategoryFull.setId(subCategorySaved.getId());
|
|
return subcategoryFull;
|
|
}
|
|
|
|
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/save", method = RequestMethod.POST)
|
|
public SubCategory<Indicator> saveSubCategory(@PathVariable("stakeholderId") String stakeholderId,
|
|
@PathVariable("topicId") String topicId,
|
|
@PathVariable("categoryId") String categoryId,
|
|
@RequestBody SubCategory<Indicator> subcategoryFull) {
|
|
log.debug("save subcategory");
|
|
|
|
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) {
|
|
if(topic.getCategories().contains(categoryId)) {
|
|
SubCategory<String> subCategory = new SubCategory<>(subcategoryFull);
|
|
|
|
List<String> charts = new ArrayList<>();
|
|
for(Indicator chart : subcategoryFull.getCharts()) {
|
|
charts.add(chart.getId());
|
|
}
|
|
subCategory.setCharts(charts);
|
|
|
|
List<String> numbers = new ArrayList<>();
|
|
for(Indicator numbr : subcategoryFull.getNumbers()) {
|
|
numbers.add(numbr.getId());
|
|
}
|
|
subCategory.setNumbers(numbers);
|
|
|
|
SubCategory<String> subCategorySaved = subCategoryDAO.save(subCategory);
|
|
|
|
List<String> subcategories = category.getSubCategories();
|
|
int index = subcategories.indexOf(subCategorySaved.getId());
|
|
if(index == -1) {
|
|
subcategories.add(subCategorySaved.getId());
|
|
categoryDAO.save(category);
|
|
log.debug("Subcategory saved!");
|
|
|
|
subcategoryFull.setId(subCategorySaved.getId());
|
|
}
|
|
|
|
charts = null;
|
|
numbers = null;
|
|
subCategory = null;
|
|
} else {
|
|
// EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
|
|
throw new PathNotValidException("Save subcategory: Category with id: "+categoryId+" not found in Topic: "+topicId);
|
|
}
|
|
} else {
|
|
// EXCEPTION - Category not found
|
|
throw new EntityNotFoundException("Save subcategory: Category with id: "+categoryId+" not found");
|
|
}
|
|
} else {
|
|
// EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
|
|
throw new PathNotValidException("Save subcategory: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
|
|
}
|
|
} else {
|
|
// EXCEPTION - Topic not found
|
|
throw new EntityNotFoundException("Save subcategory: Topic with id: "+topicId+" not found");
|
|
}
|
|
} else {
|
|
// EXCEPTION - Stakeholder not found
|
|
throw new EntityNotFoundException("Save subcategory: Stakeholder with id: "+stakeholderId+" not found");
|
|
}
|
|
return subcategoryFull;
|
|
}
|
|
|
|
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/delete", method = RequestMethod.DELETE)
|
|
public boolean deleteSubCategory(@PathVariable("stakeholderId") String stakeholderId,
|
|
@PathVariable("topicId") String topicId,
|
|
@PathVariable("categoryId") String categoryId,
|
|
@PathVariable("subcategoryId") String subcategoryId) {
|
|
log.debug("delete subcategory");
|
|
|
|
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) {
|
|
if(topic.getCategories().contains(categoryId)) {
|
|
|
|
SubCategory<String> subcategory = subCategoryDAO.findById(subcategoryId);
|
|
if(subcategory != null) {
|
|
List<String> subcategories = category.getSubCategories();
|
|
int index = subcategories.indexOf(subcategoryId);
|
|
if(index != -1) {
|
|
for(String chartId : subcategory.getCharts()) {
|
|
indicatorDAO.delete(chartId);
|
|
}
|
|
subcategory.setCharts(null);
|
|
|
|
for(String numberId : subcategory.getNumbers()) {
|
|
indicatorDAO.delete(numberId);
|
|
}
|
|
subcategory.setNumbers(null);
|
|
|
|
subcategories.remove(index);
|
|
categoryDAO.save(category);
|
|
|
|
subCategoryDAO.delete(subcategoryId);
|
|
log.debug("Subcategory deleted!");
|
|
} else {
|
|
// EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
|
|
throw new PathNotValidException("Delete subcategory: Subcategory with id: "+subcategoryId+" not found in Category: "+categoryId);
|
|
}
|
|
|
|
} else {
|
|
// EXCEPTION - SubCategory not found
|
|
throw new EntityNotFoundException("Delete subcategory: SubCategory with id: "+subcategoryId+" not found");
|
|
}
|
|
} else {
|
|
// EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
|
|
throw new PathNotValidException("Delete subcategory: Category with id: "+categoryId+" not found in Topic: "+topicId);
|
|
}
|
|
} else {
|
|
// EXCEPTION - Category not found
|
|
throw new EntityNotFoundException("Delete subcategory: Category with id: "+categoryId+" not found");
|
|
}
|
|
} else {
|
|
// EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
|
|
throw new PathNotValidException("Delete subcategory: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
|
|
}
|
|
} else {
|
|
// EXCEPTION - Topic not found
|
|
throw new EntityNotFoundException("Delete subcategory: Topic with id: "+topicId+" not found");
|
|
}
|
|
} else {
|
|
// EXCEPTION - Stakeholder not found
|
|
throw new EntityNotFoundException("Delete subcategory: Stakeholder with id: "+stakeholderId+" not found");
|
|
}
|
|
return true;
|
|
}
|
|
|
|
}
|