package eu.dnetlib.uoamonitorservice.controllers; import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException; import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils; import eu.dnetlib.uoamonitorservice.dao.*; import eu.dnetlib.uoamonitorservice.entities.*; import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; import eu.dnetlib.uoamonitorservice.primitives.IndicatorPath; import eu.dnetlib.uoamonitorservice.primitives.ReorderEvent; 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.*; import java.io.UnsupportedEncodingException; import java.util.*; @RestController @CrossOrigin(origins = "*") public class IndicatorController { private final Logger log = LogManager.getLogger(this.getClass()); @Autowired private RolesUtils rolesUtils; @Autowired private StakeholderDAO stakeholderDAO; @Autowired private TopicDAO topicDAO; @Autowired private CategoryDAO categoryDAO; @Autowired private SubCategoryDAO subCategoryDAO; @Autowired private SectionDAO sectionDAO; @Autowired private IndicatorDAO indicatorDAO; @Autowired private SectionController sectionController; @Autowired private StakeholderController stakeholderController; @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> 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> old_sections, Stakeholder stakeholder, String topicId, String categoryId, String subcategoryId) throws UnsupportedEncodingException { for (Section section : old_sections) { if (section == null) { continue; } Section chart_section = null; Section number_section = null; List chart_indicators = null; List 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 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 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 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 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 subCategories = subCategoryDAO.findByDefaultId(defaultSubcategoryId); for (SubCategory subCategory : subCategories) { List 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 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 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 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 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 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 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); Indicator indicator = indicatorDAO.findById(indicatorId); if (indicator != null) { Section section = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator.getType()); Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); if (indicator.getDefaultId() != null && !rolesUtils.hasCreateAndDeleteAuthority(stakeholder.getType())) { // EXCEPTION - Access denied throw new ForbiddenException("Delete indicator: You are not authorized to delete a default Indicator in stakeholder with id: " + stakeholderId); } List indicators = section.getIndicators(); int index = indicators.indexOf(indicatorId); if (index != -1) { // this indicator belongs in default profile if (section.getDefaultId() == null && children != null) { onDeleteDefaultIndicator(indicatorId, sectionId, children); } indicators.remove(index); sectionDAO.save(section); 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(); -> Section: section.getTitle(); throw new PathNotValidException("Delete indicator: Indicator with id: " + indicatorId + " not found in Sectiom: " + sectionId); } } else { // EXCEPTION - Indicator not found throw new EntityNotFoundException("Delete indicator: Indicator with id: " + indicatorId + " not found"); } return true; } public boolean onDeleteDefaultIndicator(String defaultIndicatorId, String defaultSectionId, String children) { if (children.equals("delete")) { // 2nd way List
sections = sectionDAO.findByDefaultId(defaultSectionId); List indicators = indicatorDAO.findByDefaultId(defaultIndicatorId); for (Section section : sections) { Iterator indicatorsIterator = indicators.iterator(); while (indicatorsIterator.hasNext()) { String indicatorId = indicatorsIterator.next().getId(); if (section.getIndicators().contains(indicatorId)) { indicatorsIterator.remove(); section.getIndicators().remove(indicatorId); sectionDAO.save(section); indicatorDAO.delete(indicatorId); log.debug("Indicator with id: " + indicatorId + " deleted!"); break; } } } } else if (children.equals("disconnect")) { List indicators = indicatorDAO.findByDefaultId(defaultIndicatorId); for (Indicator indicator : indicators) { indicator.setDefaultId(null); indicatorDAO.save(indicator); log.debug("DefaultId for Indicator with id: " + indicator.getId() + " empty!"); } } return true; } @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{type}/reorder", method = RequestMethod.POST) public List 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 indicators = reorderEvent.getIds(); String actionType = reorderEvent.getAction(); String targetId = reorderEvent.getTarget(); Section section = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId, sectionId, type); List oldIndicators = section.getIndicators(); for (String indicatorId : oldIndicators) { if ((!actionType.equals("removed") || !targetId.equals(indicatorId)) && !indicators.contains(indicatorId)) { indicators.add(indicatorId); } } section.setIndicators(indicators); List 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); Indicator indicator = indicatorDAO.findById(indicatorId); if (indicator == null) { // EXCEPTION - Indicator not found throw new EntityNotFoundException("Change indicator visibility: Indicator with id: " + indicatorId + " not found"); } Section section = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator.getType()); List indicators = section.getIndicators(); if (indicators.contains(indicatorId)) { return changeVisibilityTree(indicatorId, indicator, visibility); } else { // EXCEPTION - Indicator not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subCategory.getAlias(); -> Section: section.getTitle(); throw new PathNotValidException("Toggle indicators: Indicator with id: " + indicatorId + " not found in Section: " + sectionId); } } public Indicator changeVisibilityTree(String indicatorId, Indicator indicator, Visibility visibility) { if (indicator == null) { indicator = indicatorDAO.findById(indicatorId); if (indicator == null) { // EXCEPTION - Indicator not found throw new EntityNotFoundException("Change indicator visibility: Indicator with id: " + indicatorId + " not found"); } } indicator.setVisibility(visibility); indicatorDAO.save(indicator); log.debug("Indicator toggled!"); return indicator; } private Section checkForExceptions(String stakeholderId, String topicId, String categoryId, String subcategoryId, String sectionId, String indicatorType) { Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); if (stakeholder == null) { // EXCEPTION - Stakeholder not found throw new EntityNotFoundException("Save indicator: Stakeholder with id: " + stakeholderId + " not found"); } if (!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { // EXCEPTION - Access denied throw new ForbiddenException("CheckForExceptions Indicator: You are not authorized to update stakeholder with id: " + stakeholderId); } Topic topic = topicDAO.findById(topicId); if (topic == null) { // EXCEPTION - Topic not found throw new EntityNotFoundException("Save indicator: Topic with id: " + topicId + " not found"); } if (!stakeholder.getTopics().contains(topicId)) { // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias(); throw new PathNotValidException("Save indicator: Topic with id: " + topicId + " not found in Stakeholder: " + stakeholderId); } Category category = categoryDAO.findById(categoryId); if (category == null) { // EXCEPTION - Category not found throw new EntityNotFoundException("Save indicator: Category with id: " + categoryId + " not found"); } if (!topic.getCategories().contains(categoryId)) { // 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); } SubCategory subcategory = subCategoryDAO.findById(subcategoryId); if (subcategory == null) { // EXCEPTION - SubCategory not found throw new EntityNotFoundException("Save indicator: SubCategory with id: " + subcategoryId + " not found"); } if (!category.getSubCategories().contains(subcategoryId)) { // 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); } Section section = sectionDAO.findById(sectionId); if (section == null) { // EXCEPTION - Section not found throw new EntityNotFoundException("Save indicator: Section with id: " + sectionId + " not found"); } if (indicatorType.equals("chart")) { if (!subcategory.getCharts().contains(sectionId)) { // EXCEPTION - Section not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias(); throw new PathNotValidException("Save indicator: SubCategory with id: " + subcategoryId + " not found in Category: " + categoryId); } } else if (indicatorType.equals("number")) { if (!subcategory.getNumbers().contains(sectionId)) { // EXCEPTION - Section not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias(); throw new PathNotValidException("Save indicator: SubCategory with id: " + subcategoryId + " not found in Category: " + categoryId); } } return section; } public void deleteTree(Section section) { List indicators = section.getIndicators(); for (String indicatorId : indicators) { indicatorDAO.delete(indicatorId); } } public void disConnectTree(Section section) { List indicators = section.getIndicators(); for (String indicatorId : indicators) { Indicator indicator = indicatorDAO.findById(indicatorId); indicator.setDefaultId(null); indicatorDAO.save(indicator); } } }