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.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.ArrayList; import java.util.HashMap; 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 SectionDAO sectionDAO; @Autowired private IndicatorDAO indicatorDAO; @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()); Indicator oldIndicator = null; if(indicator.getId() != null) { oldIndicator = indicatorDAO.findById(indicator.getId()); } String indicatorId = indicator.getId(); indicatorDAO.save(indicator); 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) { onSaveDefaultIndicator(indicator, sectionId); } else { onUpdateDefaultIndicator(indicator, stakeholder, oldIndicator); } } 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, String defaultSectionId) throws UnsupportedEncodingException { log.debug("On save default indicator"); // new indicator in default profile - add it on profiles of the same type List
sections = sectionDAO.findByDefaultId(defaultSectionId); for (Section section : sections) { Indicator indicatorNew = new Indicator(); indicatorNew.copyFromDefault(indicator); for (IndicatorPath indicatorPath : indicatorNew.getIndicatorPaths()) { Stakeholder stakeholder = stakeholderDAO.findByAlias(section.getStakeholderAlias()); parameterMapping(indicatorPath, stakeholder); } 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 = false; List indicators = indicatorDAO.findByDefaultId(indicator.getId()); for(Indicator indicatorBasedOnDefault : indicators) { if(indicator.getName() != null && !indicator.getName().equals(indicatorBasedOnDefault.getName()) && (oldIndicator.getName() == null || oldIndicator.getName().equals(indicatorBasedOnDefault.getName()))) { indicatorBasedOnDefault.setName(indicator.getName()); changed = true; } if(indicator.getDescription() != null && !indicator.getDescription().equals(indicatorBasedOnDefault.getDescription()) && (oldIndicator.getDescription() == null || oldIndicator.getDescription().equals(indicatorBasedOnDefault.getDescription()))) { indicatorBasedOnDefault.setName(indicator.getName()); changed = true; } int i = 0; List 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 { 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); log.debug("indicatorPath.getType(): "+indicatorPath.getType()); log.debug("indicatorPathBasedOnDefault.getType(): "+indicatorPathBasedOnDefault.getType()); log.debug("oldIndicatorPath.getType(): "+oldIndicatorPath.getType()); if(indicatorPath.getType() != null && !indicatorPath.getType().equals(indicatorPathBasedOnDefault.getType()) && (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.getSource() != null && !indicatorPath.getSource().equals(indicatorPathBasedOnDefault.getSource()) && (oldIndicatorPath.getSource().equals(indicatorPathBasedOnDefault.getSource()))) { indicatorPathBasedOnDefault.setSource(indicatorPath.getSource()); changed = true; } log.debug("After source check: "+changed); if(indicatorPath.getUrl() != null && !indicatorPath.getUrl().equals(indicatorPathBasedOnDefault.getUrl()) && (oldIndicatorPath.getUrl().equals(indicatorPathBasedOnDefault.getUrl()))) { indicatorPathBasedOnDefault.setUrl(indicatorPath.getUrl()); changed = true; } log.debug("After url check: "+changed); if(indicatorPath.getChartObject() != null && !indicatorPath.getChartObject().equals(indicatorPathBasedOnDefault.getChartObject()) && (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<>()); } //if (indicatorPath.getParameters().size() != indicatorPathBasedOnDefault.getParameters().size()) { //log.debug("Different number of parameters"); 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().get(parameter.getKey())); if (!indicatorPathBasedOnDefault.getParameters().containsKey(parameter.getKey()) || (oldIndicatorPath.getParameters() == 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; } // else if(parameter.getKey().equals("type")) { // indicatorPathBasedOnDefault.getParameters().put(parameter.getKey(), parameter.getValue()); // changed = true; // } } parameterMapping(indicatorPathBasedOnDefault, stakeholder); //} log.debug("After parameters check: " + changed); } if(indicatorPath.getJsonPath() != null) { int j = 0; for (String jsonString : indicatorPath.getJsonPath()) { log.debug("indicatorPath.getJsonPath(): " + jsonString); String jsonStringBasedOnDefault = null; if(indicatorPathBasedOnDefault.getJsonPath() != null ) { jsonStringBasedOnDefault = indicatorPathBasedOnDefault.getJsonPath().get(j); } else { indicatorPathBasedOnDefault.setJsonPath(new ArrayList<>()); } log.debug("indicatorPathBasedOnDefault.getJsonPath().get(" + j + "): " + jsonStringBasedOnDefault); if (!jsonString.equals(jsonStringBasedOnDefault) && (oldIndicatorPath.getJsonPath() == null || oldIndicatorPath.getJsonPath().get(i).equals(jsonStringBasedOnDefault)) ) { indicatorPathBasedOnDefault.getJsonPath().set(j, jsonString); changed = true; } j++; } log.debug("After jsonPath check: " + changed); } } i++; } if(!changed) { // break; continue; } indicatorDAO.save(indicatorBasedOnDefault); } } public void parameterMapping(IndicatorPath indicatorPath, Stakeholder stakeholder) throws UnsupportedEncodingException { if (indicatorPath.getParameters() != null) { if (indicatorPath.getParameters().containsKey("index_name")) { indicatorPath.getParameters().put("index_name", stakeholder.getIndex_name()); } else if (indicatorPath.getParameters().containsKey("index_shortName")) { indicatorPath.getParameters().put("index_shortName", stakeholder.getIndex_name().toLowerCase()); } else if (indicatorPath.getParameters().containsKey("index_id")) { indicatorPath.getParameters().put("index_id", stakeholder.getIndex_id()); } } // // url encoding for number indicators // String url = indicatorPath.getUrl(); // String encoded_index_id = urlEncode(URLEncoder.encode(stakeholder.getIndex_id(), "UTF-8")); // url = url.replace("index_id", encoded_index_id); // String encoded_index_name = urlEncode(URLEncoder.encode(stakeholder.getIndex_name(), "UTF-8")); // url = url.replace("index_name", encoded_index_name); // String encoded_index_shortName = urlEncode(URLEncoder.encode(stakeholder.getIndex_shortName(), "UTF-8")); // url = url.replace("index_shortName", encoded_index_shortName); // indicatorPath.setUrl(url); } public String urlEncode(String encodedIndicatorPathField) { String indicatorPathField = ""; for( int i=0; i section = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator.getType()); List indicators = section.getIndicators(); int index = indicators.indexOf(indicatorId); if (index != -1) { 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; } // @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 stakeholder = stakeholderDAO.findById(stakeholderId); // if(stakeholder != null) { // // for(String topicId : stakeholder.getTopics()) { // Topic topic = topicDAO.findById(topicId); // if(topic != null) { // for(String categoryId : topic.getCategories()) { // Category category = categoryDAO.findById(categoryId); // if(category != null) { // for(String subcategoryId : category.getSubCategories()) { // SubCategory subcategory = subCategoryDAO.findById(subcategoryId); // if(subcategory != null) { // // for(String sectionId : subcategory.getCharts()) { // Section section = sectionDAO.findById(sectionId); // if (section != null) { // // List indicators = section.getIndicators(); // Iterator indicatorsIterator = section.getIndicators().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(); -> Section: section.getTitle(); // throw new PathNotValidException("Delete indicator: Indicator with id: " + indicatorId + " not found in Section: " + sectionId); // } // } else { // // EXCEPTION - Indicator not found // throw new EntityNotFoundException("Delete indicator: Indicator with id: " + indicatorId + " not found"); // } // } // sectionDAO.save(section); // } else { // // EXCEPTION - Section not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias(); // throw new PathNotValidException("Delete indicator: Section with id: " + sectionId + " not found in SubCategory: " + subcategoryId); // } // } // } 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}/{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 List indicators) { log.debug("reorder indicators of type: "+type); log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId + " - Section: "+sectionId); Section section = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId, sectionId, type); section.setIndicators(indicators); sectionDAO.save(section); log.debug("Indicators reordered!"); List indicatorsFull = new ArrayList<>(); for(String indicatorId : indicators) { indicatorsFull.add(indicatorDAO.findById(indicatorId)); } return indicatorsFull; } @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{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("sectionId") String sectionId, @PathVariable("indicatorId") String indicatorId) { log.debug("toggle indicator status (isActive)"); 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("Toggle indicator status: Indicator with id: "+indicatorId+" not found"); } indicator.setIsActive(!indicator.getIsActive()); this.toggleIndicator(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator); return indicator.getIsActive(); } @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{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("sectionId") String sectionId, @PathVariable("indicatorId") String indicatorId) { log.debug("toggle indicator access (isPublic)"); 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("Toggle indicator access: Indicator with id: "+indicatorId+" not found"); } indicator.setIsPublic(!indicator.getIsPublic()); this.toggleIndicator(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator); return indicator.getIsPublic(); } public void toggleIndicator(String stakeholderId, String topicId, String categoryId, String subcategoryId, String sectionId, Indicator indicator) { Section section = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator.getType()); List indicators = section.getIndicators(); if(indicators.contains(indicator.getId())) { indicatorDAO.save(indicator); log.debug("Indicator toggled!"); } 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: "+indicator.getId()+" not found in Section: "+sectionId); } } 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"); } 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; } }