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

598 lines
33 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;
import java.util.Map;
@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");
log.debug("Name: "+indicator.getName() + " - Id: "+indicator.getId() + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId);
// 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)) {
String indicatorId = indicator.getId();
indicatorDAO.save(indicator);
// this indicator belongs in default profile and it is new or it is updated
if(stakeholder.getDefaultId() == null) {
if(indicatorId == null) {
onSaveDefaultIndicator(indicator, topicId, categoryId, subcategoryId, stakeholder);
}
else {
onUpdateDefaultIndicator(indicator, stakeholder);
}
}
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(indicator.getId());
if (index == -1) {
indicators.add(indicator.getId());
subCategoryDAO.save(subcategory);
log.debug("Indicator saved!");
indicator.setId(indicator.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;
}
public void onSaveDefaultIndicator(Indicator indicator,
String defaultTopicId, String defaultCategoryId,
String defaultSubcategoryId, Stakeholder defaultStakeholder) {
log.debug("On save default indicator");
// List<Stakeholder> stakeholders = stakeholderDAO.findByDefaultId(defaultStakeholder.getId());
// for(Stakeholder stakeholder : stakeholders) {
// List<String> topicIds = stakeholder.getTopics();
// Topic topic = null;
// for(String topicId : topicIds) {
// topic = topicDAO.findById(topicId);
// if(topic.getDefaultId().equals(defaultTopicId)) {
// break;
// }
// }
//
// List<String> categoryIds = topic.getCategories();
// Category category = null;
// for(String categoryId : categoryIds) {
// category = categoryDAO.findById(categoryId);
// if(category.getDefaultId().equals(defaultCategoryId)) {
// break;
// }
// }
//
// List<String> subCategoryIds = category.getSubCategories();
// SubCategory subCategory = null;
// for(String subCategoryId : subCategoryIds) {
// subCategory = subCategoryDAO.findById(subCategoryId);
// if(subCategory.getDefaultId().equals(defaultSubcategoryId)) {
// break;
// }
// }
//
// Indicator indicatorNew = new Indicator(indicator);
// //indicatorNew.setStakeholderId(defaultStakeholder.getId());
// for (IndicatorPath indicatorPath : indicatorNew.getIndicatorPaths()) {
// parameterMapping(indicatorPath, stakeholder);
// }
//
// indicatorDAO.save(indicatorNew);
//
// List<String> indicators = null;
// if (indicator.getType().equals("chart")) {
// indicators = subCategory.getCharts();
// } else if (indicator.getType().equals("number")) {
// indicators = subCategory.getNumbers();
// }
// indicators.add(indicatorNew.getId());
//
// subCategoryDAO.save(subCategory);
// }
// new indicator in default profile - add it on profiles of the same type
List<SubCategory> subCategories = subCategoryDAO.findByDefaultId(defaultSubcategoryId);
for (SubCategory subCategory : subCategories) {
Indicator indicatorNew = new Indicator();
indicatorNew.copyFromDefault(indicator);
for (IndicatorPath indicatorPath : indicatorNew.getIndicatorPaths()) {
Stakeholder stakeholder = stakeholderDAO.findById(subCategory.getStakeholderId());
parameterMapping(indicatorPath, stakeholder);
}
indicatorDAO.save(indicatorNew);
List<String> indicators = null;
if (indicator.getType().equals("chart")) {
indicators = subCategory.getCharts();
} else if (indicator.getType().equals("number")) {
indicators = subCategory.getNumbers();
}
indicators.add(indicatorNew.getId());
subCategoryDAO.save(subCategory);
}
}
public void onUpdateDefaultIndicator(Indicator indicator, Stakeholder stakeholder) {
log.debug("On update default indicator");
// indicator already exists - check if changed and update all indicators based on it
boolean changed = false;
List<Indicator> indicators = indicatorDAO.findByDefaultId(indicator.getId());
for(Indicator indicatorBasedOnDefault : indicators) {
int i = 0;
List<IndicatorPath> indicatorPaths = indicatorBasedOnDefault.getIndicatorPaths();
for (IndicatorPath indicatorPath : indicator.getIndicatorPaths()) {
IndicatorPath indicatorPathBasedOnDefault = indicatorBasedOnDefault.getIndicatorPaths().get(i);
if(indicatorPathBasedOnDefault == null) {
// Add new indicator path in existing indicators
IndicatorPath indicatorPathNew = new IndicatorPath(indicatorPath);
parameterMapping(indicatorPathNew, stakeholder);
indicatorPaths.add(indicatorPathNew);
changed = true;
} else {
// Check if there are changes in indicator path and update existing indicators if needed
if(!indicatorPath.getType().equals(indicatorPathBasedOnDefault.getType())) {
indicatorPathBasedOnDefault.setType(indicatorPath.getType());
changed = true;
}
if(!indicatorPath.getSource().equals(indicatorPathBasedOnDefault.getSource())) {
indicatorPathBasedOnDefault.setSource(indicatorPath.getSource());
changed = true;
}
if(!indicatorPath.getUrl().equals(indicatorPathBasedOnDefault.getUrl())) {
indicatorPathBasedOnDefault.setUrl(indicatorPath.getUrl());
changed = true;
}
if(!indicatorPath.getChartObject().equals(indicatorPathBasedOnDefault.getChartObject())) {
indicatorPathBasedOnDefault.setChartObject(indicatorPath.getChartObject());
changed = true;
}
if(indicatorPath.getParameters().size() != indicatorPathBasedOnDefault.getParameters().size()) {
for (Map.Entry<String, String> parameter : indicatorPath.getParameters().entrySet()) {
if(!indicatorPathBasedOnDefault.getParameters().containsKey(parameter.getKey())) {
indicatorPathBasedOnDefault.getParameters().put(parameter.getKey(), parameter.getValue());
}
}
parameterMapping(indicatorPathBasedOnDefault, stakeholder);
changed = true;
}
int j=0;
for(String jsonString : indicatorPath.getJsonPath()) {
String jsonStringBasedOnDefault = indicatorPathBasedOnDefault.getJsonPath().get(j);
if(!jsonString.equals(jsonStringBasedOnDefault)) {
indicatorPathBasedOnDefault.getJsonPath().set(j, jsonString);
changed = true;
}
j++;
}
}
i++;
}
if(!changed) {
break;
}
indicatorDAO.save(indicatorBasedOnDefault);
}
}
public void parameterMapping(IndicatorPath indicatorPath, Stakeholder stakeholder) {
if (indicatorPath.getParameters().containsKey("funder_name")) {
indicatorPath.getParameters().put("funder_name", stakeholder.getIndex_name());
} else if (indicatorPath.getParameters().containsKey("fsn")) {
indicatorPath.getParameters().put("fsn", stakeholder.getIndex_name().toLowerCase());
} else if (indicatorPath.getParameters().containsKey("funder_id")) {
indicatorPath.getParameters().put("funder_id", stakeholder.getIndex_id());
}
}
@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");
log.debug("Id: "+indicatorId + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId);
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 all chart indicators of stakeholder");
log.debug("Stakeholder: "+stakeholderId);
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 List<Indicator> reorderIndicators(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId,
@PathVariable("subcategoryId") String subcategoryId,
@PathVariable("type") String type,
@RequestBody List<String> indicators) {
log.debug("reorder indicators of type: "+type);
log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId);
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)) {
if(type.equals("chart")) {
subcategory.setCharts(indicators);
} else if(type.equals("number")) {
subcategory.setNumbers(indicators);
}
subCategoryDAO.save(subcategory);
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");
}
List<Indicator> indicatorsFull = new ArrayList<>();
for(String indicatorId : indicators) {
indicatorsFull.add(indicatorDAO.findById(indicatorId));
}
return indicatorsFull;
}
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{indicatorId}/toggle-status", method = RequestMethod.POST)
public Boolean toggleIndicatorStatus(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId,
@PathVariable("subcategoryId") String subcategoryId,
@PathVariable("indicatorId") String indicatorId) {
log.debug("toggle indicator status (isActive)");
log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId+ " - Indicator: "+indicatorId);
Indicator indicator = indicatorDAO.findById(indicatorId);
if (indicator == null) {
// EXCEPTION - Indicator not found
throw new EntityNotFoundException("Toggle indicator status: Indicator with id: "+indicatorId+" not found");
}
indicator.setIsActive(!indicator.getIsActive());
this.toggleIndicator(stakeholderId, topicId, categoryId, subcategoryId, indicator);
return indicator.getIsActive();
}
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{indicatorId}/toggle-access", method = RequestMethod.POST)
public Boolean toggleIndicatorAccess(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId,
@PathVariable("subcategoryId") String subcategoryId,
@PathVariable("indicatorId") String indicatorId) {
log.debug("toggle indicator access (isPublic)");
log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId+ " - Indicator: "+indicatorId);
Indicator indicator = indicatorDAO.findById(indicatorId);
if (indicator == null) {
// EXCEPTION - Indicator not found
throw new EntityNotFoundException("Toggle indicator access: Indicator with id: "+indicatorId+" not found");
}
indicator.setIsPublic(!indicator.getIsPublic());
this.toggleIndicator(stakeholderId, topicId, categoryId, subcategoryId, indicator);
return indicator.getIsPublic();
}
public void toggleIndicator(String stakeholderId, String topicId, String categoryId, String subcategoryId, Indicator 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)) {
indicatorDAO.save(indicator);
log.debug("Indicator toggled!");
} 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");
}
}
}