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

531 lines
30 KiB
Java

package eu.dnetlib.uoamonitorservice.controllers;
import eu.dnetlib.uoamonitorservice.entities.*;
import eu.dnetlib.uoamonitorservice.primitives.Visibility;
import eu.dnetlib.uoamonitorservice.service.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
@RestController
@CrossOrigin(origins = "*")
public class IndicatorController {
private final Logger log = LogManager.getLogger(this.getClass());
private final StakeholderService stakeholderService;
private final TopicService topicService;
private final CategoryService categoryService;
private final SubCategoryService subCategoryService;
private final SectionService sectionService;
private final IndicatorService indicatorService;
@Autowired
public IndicatorController(StakeholderService stakeholderService, TopicService topicService, CategoryService categoryService,
SubCategoryService subCategoryService, SectionService sectionService, IndicatorService indicatorService) {
this.stakeholderService = stakeholderService;
this.topicService = topicService;
this.categoryService = categoryService;
this.subCategoryService = subCategoryService;
this.sectionService = sectionService;
this.indicatorService = indicatorService;
}
/*@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/save-bulk", method = RequestMethod.POST)
public Stakeholder saveBulkIndicators(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId,
@PathVariable("subcategoryId") String subcategoryId,
@RequestBody List<Section<Indicator>> sections) throws UnsupportedEncodingException {
log.debug("save bulk indicators");
log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId);
Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId);
Date date = new Date();
createSectionsAndSaveBulk(date, sections, stakeholder, topicId, categoryId, subcategoryId);
return stakeholderController.setFullEntities(stakeholder);
}
private void createSectionsAndSaveBulk(Date date, List<Section<Indicator>> old_sections,
Stakeholder stakeholder, String topicId, String categoryId, String subcategoryId) throws UnsupportedEncodingException {
for (Section<Indicator> section : old_sections) {
if (section == null) {
continue;
}
Section chart_section = null;
Section number_section = null;
List<String> chart_indicators = null;
List<String> number_indicators = null;
if (section.getType().equals("chart")) {
chart_section = createSection(chart_section, "chart", section.getTitle(), date, stakeholder, topicId, categoryId, subcategoryId);
chart_indicators = chart_section.getIndicators();
} else if (section.getType().equals("number")) {
number_section = createSection(number_section, "number", section.getTitle(), date, stakeholder, topicId, categoryId, subcategoryId);
number_indicators = number_section.getIndicators();
}
List<Indicator> indicators = section.getIndicators();
for (Indicator indicator : indicators) {
if (indicator == null) {
continue;
}
if (indicator.getType().equals("chart")) {
saveIndicatorAndAddInSection(indicator, date, stakeholder, subcategoryId, chart_section, chart_indicators);
} else if (indicator.getType().equals("number")) {
saveIndicatorAndAddInSection(indicator, date, stakeholder, subcategoryId, number_section, number_indicators);
}
}
if (chart_section != null) {
sectionDAO.save(chart_section);
}
if (number_section != null) {
sectionDAO.save(number_section);
}
}
}
private Section createSection(Section section, String type, String title, Date date,
Stakeholder stakeholder, String topicId, String categoryId, String subcategoryId) {
section = new Section<>();
section.setType(type);
section.setTitle(title);
section.setStakeholderAlias(stakeholder.getAlias());
section.setUpdateDate(date);
section.setCreationDate(date);
section.setIndicators(new ArrayList<>());
section = sectionController.saveSection(stakeholder.getId(), topicId, categoryId, subcategoryId, "-1", section);
return section;
}
private void saveIndicatorAndAddInSection(Indicator indicator, Date date, Stakeholder stakeholder, String subcategoryId, Section section, List<String> indicators) throws UnsupportedEncodingException {
// indicator does not exist in DB
indicator.setCreationDate(date);
indicator.setUpdateDate(date);
if (stakeholder.getDefaultId() == null) { // this indicator belongs in default profile and it is new
indicatorDAO.save(indicator);
onSaveDefaultIndicator(indicator, section, subcategoryId);
} else { // this indicator belongs in a stakeholder's profile and it is new
indicatorDAO.save(indicator);
}
indicators.add(indicator.getId());
log.debug("Indicator saved!");
}
*/
/* @PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/save", method = RequestMethod.POST)
public Indicator saveIndicator(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId,
@PathVariable("subcategoryId") String subcategoryId,
@PathVariable("sectionId") String sectionId,
@RequestBody Indicator indicator) throws UnsupportedEncodingException {
log.debug("save indicator");
log.debug("Name: " + indicator.getName() + " - Id: " + indicator.getId() + " - Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId + " - Section: " + sectionId);
Section<String> section = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator.getType());
Date date = new Date();
indicator.setUpdateDate(date);
Indicator oldIndicator = null;
if (indicator.getId() != null) {
oldIndicator = indicatorDAO.findById(indicator.getId());
if (oldIndicator == null) {
// EXCEPTION - Indicator not found
throw new EntityNotFoundException("save indicator: Indicator with id: " + indicator.getId() + " not found");
}
} else { // indicator does not exist in DB
indicator.setCreationDate(date);
}
String indicatorId = indicator.getId();
Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId);
// this indicator belongs in default profile and it is new or it is updated
if (stakeholder.getDefaultId() == null) {
if (indicatorId == null) {
indicatorDAO.save(indicator);
onSaveDefaultIndicator(indicator, section, subcategoryId);
} else {
onUpdateDefaultIndicator(indicator, stakeholder, oldIndicator);
indicatorDAO.save(indicator);
}
} else {
indicatorDAO.save(indicator);
}
List<String> indicators = section.getIndicators();
int index = indicators.indexOf(indicator.getId());
if (index == -1) {
indicators.add(indicator.getId());
sectionDAO.save(section);
log.debug("Indicator saved!");
}
return indicator;
}
public void onSaveDefaultIndicator(Indicator indicator, Section defaultSection, String defaultSubcategoryId) throws UnsupportedEncodingException {
log.debug("On save default indicator");
// new indicator in default profile - add it on profiles of the same type
List<SubCategory> subCategories = subCategoryDAO.findByDefaultId(defaultSubcategoryId);
for (SubCategory subCategory : subCategories) {
List<String> sections = null;
if (defaultSection.getType().equals("chart")) {
sections = subCategory.getCharts();
} else {
sections = subCategory.getNumbers();
}
for (String sectionId : sections) {
Section section = sectionDAO.findById(sectionId);
if (section.getDefaultId() != null && section.getDefaultId().equals(defaultSection.getId())) {
Indicator indicatorNew = new Indicator();
indicatorNew.copyFromDefault(indicator, subCategory.getVisibility());
indicatorDAO.save(indicatorNew);
List<String> indicators = section.getIndicators();
indicators.add(indicatorNew.getId());
sectionDAO.save(section);
}
}
}
}
public void onUpdateDefaultIndicator(Indicator indicator, Stakeholder stakeholder, Indicator oldIndicator) throws UnsupportedEncodingException {
log.debug("On update default indicator");
// indicator already exists - check if changed and update all indicators based on it
boolean changed;
List<Indicator> indicators = indicatorDAO.findByDefaultId(indicator.getId());
for (Indicator indicatorBasedOnDefault : indicators) {
changed = false;
if ((
(indicator.getName() == null && oldIndicator.getName() != null)
||
(indicator.getName() != null && !indicator.getName().equals(indicatorBasedOnDefault.getName()))
) && (
(oldIndicator.getName() == null && indicatorBasedOnDefault.getName() == null)
||
(oldIndicator.getName() != null && oldIndicator.getName().equals(indicatorBasedOnDefault.getName()))
)) {
indicatorBasedOnDefault.setName(indicator.getName());
changed = true;
}
if (indicator.getDescription() != null && !indicator.getDescription().equals(indicatorBasedOnDefault.getDescription())
|| indicator.getDescription() == null && indicatorBasedOnDefault.getDescription() != null) {
indicatorBasedOnDefault.setDescription(indicator.getDescription());
changed = true;
}
if ((
(indicator.getAdditionalDescription() == null && oldIndicator.getAdditionalDescription() != null)
||
(indicator.getAdditionalDescription() != null && !indicator.getAdditionalDescription().equals(indicatorBasedOnDefault.getAdditionalDescription()))
) && (
(oldIndicator.getAdditionalDescription() == null && indicatorBasedOnDefault.getAdditionalDescription() == null)
||
(oldIndicator.getAdditionalDescription() != null && oldIndicator.getAdditionalDescription().equals(indicatorBasedOnDefault.getAdditionalDescription()))
)) {
indicatorBasedOnDefault.setAdditionalDescription(indicator.getAdditionalDescription());
changed = true;
}
int i = 0;
List<IndicatorPath> indicatorPaths = indicatorBasedOnDefault.getIndicatorPaths();
if (indicatorPaths == null && indicator.getIndicatorPaths() != null) {
indicatorPaths = new ArrayList<>();
}
for (IndicatorPath indicatorPath : indicator.getIndicatorPaths()) {
IndicatorPath indicatorPathBasedOnDefault = null;
if (i < indicatorPaths.size()) {
indicatorPathBasedOnDefault = indicatorPaths.get(i);
}
if (indicatorPathBasedOnDefault == null) {
// Add new indicator path in existing indicators
IndicatorPath indicatorPathNew = new IndicatorPath(indicatorPath);
indicatorPaths.add(indicatorPathNew);
changed = true;
} else {
IndicatorPath oldIndicatorPath = oldIndicator.getIndicatorPaths().get(i);
// Check if there are changes in indicator path and update existing indicators if needed
log.debug("update indicator path: " + i + " (indicator id: " + indicatorBasedOnDefault.getId() + ")");
if ((
(indicatorPath.getType() == null && oldIndicatorPath.getType() != null)
||
(indicatorPath.getType() != null && !indicatorPath.getType().equals(indicatorPathBasedOnDefault.getType()))
) && (
(oldIndicatorPath.getType() == null && indicatorPathBasedOnDefault.getType() == null)
||
(oldIndicatorPath.getType() != null && oldIndicatorPath.getType().equals(indicatorPathBasedOnDefault.getType()))
)) {
indicatorPathBasedOnDefault.setType(indicatorPath.getType());
changed = true; // parameter "type" needs to be changed as well
}
log.debug("After type check: " + changed);
if ((
(indicatorPath.getFormat() == null && oldIndicatorPath.getFormat() != null)
||
(indicatorPath.getFormat() != null && !indicatorPath.getFormat().equals(indicatorPathBasedOnDefault.getFormat()))
) && (
(oldIndicatorPath.getFormat() == null && indicatorPathBasedOnDefault.getFormat() == null)
||
(oldIndicatorPath.getFormat() != null && oldIndicatorPath.getFormat().equals(indicatorPathBasedOnDefault.getFormat()))
)) {
indicatorPathBasedOnDefault.setFormat(indicatorPath.getFormat());
changed = true;
}
log.debug("After type check: " + changed);
if ((
(indicatorPath.getSource() == null && oldIndicatorPath.getSource() != null)
||
(indicatorPath.getSource() != null && !indicatorPath.getSource().equals(indicatorPathBasedOnDefault.getSource()))
) && (
(oldIndicatorPath.getSource() == null && indicatorPathBasedOnDefault.getSource() == null)
||
(oldIndicatorPath.getSource() != null && oldIndicatorPath.getSource().equals(indicatorPathBasedOnDefault.getSource()))
)) {
indicatorPathBasedOnDefault.setSource(indicatorPath.getSource());
changed = true;
}
log.debug("After source check: " + changed);
if ((
(indicatorPath.getUrl() == null && oldIndicatorPath.getUrl() != null)
||
(indicatorPath.getUrl() != null && !indicatorPath.getUrl().equals(indicatorPathBasedOnDefault.getUrl()))
) && (
(oldIndicatorPath.getUrl() == null && indicatorPathBasedOnDefault.getUrl() == null)
||
(oldIndicatorPath.getUrl() != null && oldIndicatorPath.getUrl().equals(indicatorPathBasedOnDefault.getUrl()))
)) {
indicatorPathBasedOnDefault.setUrl(indicatorPath.getUrl());
changed = true;
}
log.debug("After url check: " + changed);
if ((
(indicatorPath.getChartObject() == null && oldIndicatorPath.getChartObject() != null)
||
(indicatorPath.getChartObject() != null && !indicatorPath.getChartObject().equals(indicatorPathBasedOnDefault.getChartObject()))
) && (
(oldIndicatorPath.getChartObject() == null && indicatorPathBasedOnDefault.getChartObject() == null)
||
(oldIndicatorPath.getChartObject() != null && oldIndicatorPath.getChartObject().equals(indicatorPathBasedOnDefault.getChartObject()))
)) {
indicatorPathBasedOnDefault.setChartObject(indicatorPath.getChartObject());
changed = true;
}
log.debug("After chartObject check: " + changed);
if (indicatorPath.getParameters() != null) {
if (indicatorPathBasedOnDefault.getParameters() == null) {
indicatorPathBasedOnDefault.setParameters(new HashMap<>());
}
for (Map.Entry<String, String> parameter : indicatorPath.getParameters().entrySet()) {
log.debug("\nindicatorPath: parameter.getKey(): " + parameter.getKey() + " - value: " + parameter.getValue()
+ "\nindicatorPathBasedOnDefault:parameters:key: " + indicatorPathBasedOnDefault.getParameters().get(parameter.getKey())
+ "\noldIndicatorPath:parameters:key: " + (oldIndicatorPath.getParameters() == null ? "null" : oldIndicatorPath.getParameters().get(parameter.getKey())));
if (!indicatorPathBasedOnDefault.getParameters().containsKey(parameter.getKey())
|| (oldIndicatorPath.getParameters() == null || oldIndicatorPath.getParameters().get(parameter.getKey()) == null
|| (oldIndicatorPath.getParameters().get(parameter.getKey()).equals(indicatorPathBasedOnDefault.getParameters().get(parameter.getKey()))
&& !parameter.getValue().equals(indicatorPathBasedOnDefault.getParameters().get(parameter.getKey()))))
) {
indicatorPathBasedOnDefault.getParameters().put(parameter.getKey(), parameter.getValue());
changed = true;
}
}
// When deleting indicator path parameters in a default profile, delete them also from all children profiles
if (oldIndicatorPath.getParameters() != null && indicatorPath.getParameters().size() < oldIndicatorPath.getParameters().size()) {
for (Map.Entry<String, String> parameter : oldIndicatorPath.getParameters().entrySet()) {
if (!indicatorPath.getParameters().containsKey(parameter.getKey())) {
indicatorPathBasedOnDefault.getParameters().remove(parameter.getKey());
}
}
}
}
log.debug("After parameters check: " + changed);
if (indicatorPath.getJsonPath() != null) {
boolean jsonPathChanged = false;
boolean breaked = false;
int oldJsonPathSize = 0;
if (oldIndicatorPath.getJsonPath() != null) {
oldJsonPathSize = oldIndicatorPath.getJsonPath().size();
}
int basedOnDefaultJsonPathSize = 0;
if (indicatorPathBasedOnDefault.getJsonPath() != null) {
basedOnDefaultJsonPathSize = indicatorPathBasedOnDefault.getJsonPath().size();
}
log.debug("old: " + oldJsonPathSize + " - based on default: " + basedOnDefaultJsonPathSize + " - new: " + indicatorPath.getJsonPath().size());
if (oldJsonPathSize == basedOnDefaultJsonPathSize) {
if (indicatorPathBasedOnDefault.getJsonPath() == null && indicatorPath.getJsonPath().size() > 0) {
indicatorPathBasedOnDefault.setJsonPath(new ArrayList<>());
}
int basedOnDefaultIndex = 0;
int oldIndex = 0;
Iterator<String> jsonStringBasedOnDefaultIterator = indicatorPathBasedOnDefault.getJsonPath().iterator();
while (jsonStringBasedOnDefaultIterator.hasNext()) {
String jsonStringBasedOnDefault = jsonStringBasedOnDefaultIterator.next();
if (oldIndicatorPath.getJsonPath().get(oldIndex).equals(jsonStringBasedOnDefault)) {
if (basedOnDefaultIndex >= indicatorPath.getJsonPath().size()) { // string deleted
jsonStringBasedOnDefaultIterator.remove();
jsonPathChanged = true;
} else { // check if string changed
if (!indicatorPath.getJsonPath().get(basedOnDefaultIndex).equals(jsonStringBasedOnDefault)) {
indicatorPathBasedOnDefault.getJsonPath().set(basedOnDefaultIndex, indicatorPath.getJsonPath().get(basedOnDefaultIndex));
jsonPathChanged = true;
}
basedOnDefaultIndex++;
}
oldIndex++;
} else {
breaked = true;
jsonPathChanged = false;
log.debug("not the same: " + oldIndex);
break;
}
}
int index = 0;
if (!breaked && indicatorPath.getJsonPath().size() > indicatorPathBasedOnDefault.getJsonPath().size()) { // strings added
jsonPathChanged = true;
for (index = indicatorPathBasedOnDefault.getJsonPath().size(); index < indicatorPath.getJsonPath().size(); index++) {
indicatorPathBasedOnDefault.getJsonPath().add(indicatorPath.getJsonPath().get(index));
}
}
if (jsonPathChanged) {
changed = true;
}
}
// TODO when deleting indicator path json path strings... --> is this done? (line 327)
}
log.debug("After jsonPath check: " + changed);
}
i++;
}
// TODO when deleting indicator paths...
if (!changed) {
// break;
continue;
}
indicatorBasedOnDefault.setUpdateDate(indicator.getUpdateDate());
indicatorDAO.save(indicatorBasedOnDefault);
}
}
*/
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{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("sectionId") String sectionId,
@PathVariable("indicatorId") String indicatorId,
@RequestParam(required = false) String children) {
log.debug("delete indicator");
log.debug("Id: " + indicatorId + " - Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId + " - Section: " + sectionId);
Stakeholder stakeholder = this.stakeholderService.findById(stakeholderId);
Topic topic = this.topicService.findByPath(stakeholder, topicId);
Category category = this.categoryService.findByPath(topic, categoryId);
SubCategory subCategory = this.subCategoryService.findByPath(category, subcategoryId);
Section section = this.sectionService.findByPath(subCategory, sectionId);
Indicator indicator = this.indicatorService.findByPath(section, indicatorId);
this.indicatorService.delete(stakeholder.getType(), indicator, true);
return true;
}
/*@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{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("sectionId") String sectionId,
@PathVariable("type") String type,
@RequestBody ReorderEvent reorderEvent) {
log.debug("reorder indicators of type: " + type);
log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId + " - Section: " + sectionId);
List<String> indicators = reorderEvent.getIds();
String actionType = reorderEvent.getAction();
String targetId = reorderEvent.getTarget();
Section<String> section = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId, sectionId, type);
List<String> oldIndicators = section.getIndicators();
for (String indicatorId : oldIndicators) {
if ((!actionType.equals("removed") || !targetId.equals(indicatorId)) && !indicators.contains(indicatorId)) {
indicators.add(indicatorId);
}
}
section.setIndicators(indicators);
List<Indicator> indicatorsFull = new ArrayList<>();
for (String indicatorId : indicators) {
Indicator indicator = indicatorDAO.findById(indicatorId);
if (indicator == null) {
// EXCEPTION - Indicator not found
throw new EntityNotFoundException("Reorder indicators: Indicator with id: " + indicatorId + " not found");
}
indicatorsFull.add(indicator);
}
sectionDAO.save(section);
log.debug("Indicators reordered!");
return indicatorsFull;
}*/
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{indicatorId}/change-visibility", method = RequestMethod.POST)
public Indicator changeIndicatorVisibility(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId,
@PathVariable("subcategoryId") String subcategoryId,
@PathVariable("sectionId") String sectionId,
@PathVariable("indicatorId") String indicatorId,
@RequestParam("visibility") Visibility visibility) {
log.debug("change indicator visibility: " + visibility);
log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId + " - Section: " + sectionId + " - Indicator: " + indicatorId);
Stakeholder stakeholder = this.stakeholderService.findById(stakeholderId);
Topic topic = this.topicService.findByPath(stakeholder, topicId);
Category category = this.categoryService.findByPath(topic, categoryId);
SubCategory subCategory = this.subCategoryService.findByPath(category, subcategoryId);
Section section = this.sectionService.findByPath(subCategory, sectionId);
Indicator indicator = this.indicatorService.findByPath(section, indicatorId);
return this.indicatorService.changeVisibility(stakeholder.getType(), stakeholder.getAlias(), indicator, visibility);
}
}