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

338 lines
19 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.Iterator;
import java.util.List;
@RestController
@CrossOrigin(origins = "*")
public class IndicatorController {
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;
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/save", method = RequestMethod.POST)
public Indicator saveIndicator(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId,
@PathVariable("subcategoryId") String subcategoryId,
@RequestBody Indicator indicator) {
log.debug("save indicator");
// if(stakeholderId == null) {
// // EXCEPTION - Parameter for Stakeholder is not accepted
// }
// if(topicId == null) {
// // EXCEPTION - Parameter for Topic is not accepted
// }
// if(categoryId == null) {
// // EXCEPTION - Parameter for Category is not accepted
// }
// if(subcategoryId == null) {
// // EXCEPTION - Parameter for SubCategory is not accepted
// }
// if(indicator == null) {
// // EXCEPTION - Parameter for Indicator is not accepted
// }
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) {
if (category.getSubCategories().contains(subcategoryId)) {
Indicator indicatorSaved = indicatorDAO.save(indicator);
List<String> indicators = null;
//if(indicator.hasType("chart")) {
if(indicator.getType().equals("chart")) {
indicators = subcategory.getCharts();
//} else if(indicator.hasType("number")) {
} else if(indicator.getType().equals("number")) {
indicators = subcategory.getNumbers();
}
int index = indicators.indexOf(indicatorSaved.getId());
if (index == -1) {
indicators.add(indicatorSaved.getId());
subCategoryDAO.save(subcategory);
log.debug("Indicator saved!");
indicator.setId(indicatorSaved.getId());
}
} else {
// EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
throw new PathNotValidException("Save indicator: SubCategory with id: "+subcategoryId+" not found in Category: "+categoryId);
}
} else {
// EXCEPTION - SubCategory not found
throw new EntityNotFoundException("Save indicator: SubCategory with id: "+subcategoryId+" not found");
}
} else {
// EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
throw new PathNotValidException("Save indicator: Category with id: "+categoryId+" not found in Topic: "+topicId);
}
} else {
// EXCEPTION - Category not found
throw new EntityNotFoundException("Save indicator: Category with id: "+categoryId+" not found");
}
} else {
// EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
throw new PathNotValidException("Save indicator: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
}
} else {
// EXCEPTION - Topic not found
throw new EntityNotFoundException("Save indicator: Topic with id: "+topicId+" not found");
}
} else {
// EXCEPTION - Stakeholder not found
throw new EntityNotFoundException("Save indicator: Stakeholder with id: "+stakeholderId+" not found");
}
return indicator;
}
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{indicatorId}/delete", method = RequestMethod.DELETE)
public boolean deleteIndicator(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId,
@PathVariable("subcategoryId") String subcategoryId,
@PathVariable("indicatorId") String indicatorId) {
log.debug("delete indicator");
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) {
if(category.getSubCategories().contains(subcategoryId)) {
Indicator indicator = indicatorDAO.findById(indicatorId);
if(indicator != null) {
List<String> indicators = null;
if (indicator.getType().equals("chart")) {
indicators = subcategory.getCharts();
} else if (indicator.getType().equals("number")) {
indicators = subcategory.getNumbers();
}
int index = indicators.indexOf(indicatorId);
if (index != -1) {
indicators.remove(index);
subCategoryDAO.save(subcategory);
indicatorDAO.delete(indicatorId);
log.debug("Indicator deleted!");
} else {
// EXCEPTION - Indicator not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias();
throw new PathNotValidException("Delete indicator: Indicator with id: "+indicatorId+" not found in SubCategory: "+subcategoryId);
}
} else {
// EXCEPTION - Indicator not found
throw new EntityNotFoundException("Delete indicator: Indicator with id: "+indicatorId+" not found");
}
} else {
// EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
throw new PathNotValidException("Delete indicator: SubCategory with id: "+subcategoryId+" not found in Category: "+categoryId);
}
} else {
// EXCEPTION - SubCategory not found
throw new EntityNotFoundException("Delete indicator: SubCategory with id: "+subcategoryId+" not found");
}
} else {
// EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
throw new PathNotValidException("Delete indicator: Category with id: "+categoryId+" not found in Topic: "+topicId);
}
} else {
// EXCEPTION - Category not found
throw new EntityNotFoundException("Delete indicator: Category with id: "+categoryId+" not found");
}
} else {
// EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
throw new PathNotValidException("Delete indicator: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
}
} else {
// EXCEPTION - Topic not found
throw new EntityNotFoundException("Delete indicator: Topic with id: "+topicId+" not found");
}
} else {
// EXCEPTION - Stakeholder not found
throw new EntityNotFoundException("Delete indicator: Stakeholder with id: "+stakeholderId+" not found");
}
return true;
}
@RequestMapping(value = "/{stakeholderId}/charts/delete", method = RequestMethod.DELETE)
public boolean deleteAllChartIndicators(@PathVariable("stakeholderId") String stakeholderId) {
log.debug("delete indicator");
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
if(stakeholder != null) {
for(String topicId : stakeholder.getTopics()) {
Topic<String> topic = topicDAO.findById(topicId);
if(topic != null) {
for(String categoryId : topic.getCategories()) {
Category<String> category = categoryDAO.findById(categoryId);
if(category != null) {
for(String subcategoryId : category.getSubCategories()) {
SubCategory<String> subcategory = subCategoryDAO.findById(subcategoryId);
if(subcategory != null) {
List<String> indicators = subcategory.getCharts();
Iterator<String> indicatorsIterator = subcategory.getCharts().iterator();
while (indicatorsIterator.hasNext()) {
String indicatorId = indicatorsIterator.next();
Indicator indicator = indicatorDAO.findById(indicatorId);
if (indicator != null) {
int index = indicators.indexOf(indicatorId);
if (index != -1) {
indicatorsIterator.remove();
//indicators.remove(index);
indicatorDAO.delete(indicatorId);
log.debug("Indicator deleted!");
} else {
// EXCEPTION - Indicator not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias();
throw new PathNotValidException("Delete indicator: Indicator with id: " + indicatorId + " not found in SubCategory: " + subcategoryId);
}
} else {
// EXCEPTION - Indicator not found
throw new EntityNotFoundException("Delete indicator: Indicator with id: " + indicatorId + " not found");
}
}
subCategoryDAO.save(subcategory);
} else {
// EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
throw new PathNotValidException("Delete indicator: SubCategory with id: "+subcategoryId+" not found in Category: "+categoryId);
}
}
} else {
// EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
throw new PathNotValidException("Delete indicator: Category with id: "+categoryId+" not found in Topic: "+topicId);
}
}
} else {
// EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
throw new PathNotValidException("Delete indicator: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
}
}
} else {
// EXCEPTION - Stakeholder not found
throw new EntityNotFoundException("Delete indicator: Stakeholder with id: "+stakeholderId+" not found");
}
return true;
}
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{type}/reorder", method = RequestMethod.POST)
public boolean reorderIndicators(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId,
@PathVariable("subcategoryId") String subcategoryId,
@PathVariable("type") String type,
@RequestBody List<Indicator> indicatorsFull) {
log.debug("reorder indicators");
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) {
if (category.getSubCategories().contains(subcategoryId)) {
List<String> indicators = new ArrayList<>();
for(Indicator indicator : indicatorsFull) {
indicators.add(indicator.getId());
}
if(type.equals("chart")) {
subcategory.setCharts(indicators);
} else if(type.equals("number")) {
subcategory.setNumbers(indicators);
}
subCategoryDAO.save(subcategory);
indicators = null;
log.debug("Indicators reordered!");
} else {
// EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
throw new PathNotValidException("Reorder indicators: SubCategory with id: "+subcategoryId+" not found in Category: "+categoryId);
}
} else {
// EXCEPTION - SubCategory not found
throw new EntityNotFoundException("Reorder indicators: SubCategory with id: "+subcategoryId+" not found");
}
} else {
// EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
throw new PathNotValidException("Reorder indicators: Category with id: "+categoryId+" not found in Topic: "+topicId);
}
} else {
// EXCEPTION - Category not found
throw new EntityNotFoundException("Reorder indicators: Category with id: "+categoryId+" not found");
}
} else {
// EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
throw new PathNotValidException("Reorder indicators: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
}
} else {
// EXCEPTION - Topic not found
throw new EntityNotFoundException("Reorder indicators: Topic with id: "+topicId+" not found");
}
} else {
// EXCEPTION - Stakeholder not found
throw new EntityNotFoundException("Reorder indicators: Stakeholder with id: "+stakeholderId+" not found");
}
return true;
}
}