From 7d4273bea45e7a3bfa7fdba6f3ab41a82c995e99 Mon Sep 17 00:00:00 2001 From: "konstantina.galouni" Date: Wed, 21 Jul 2021 09:46:49 +0000 Subject: [PATCH] [Trunk | Monitor Service]: 1. MonitorServiceCheckDeployController.java: In method "checkEverything()" (/health_check/advanced), added "Date of build" field with current date. 2. Indicator.java: In method "copyFromDefault()" set visibility of new indicator to "RESTRICTED" (instead of copying the default indicator's visibility). 3. IndicatorController.java: [NEW] Bulk add for indicators (imported from file) - get charts and numbers in a list and create chart and number sections to put them. a. Added API call method "saveBulkIndicators()" (POST /{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/save-bulk). b. Added helper methods "createSectionsAndSaveBulk()", "createSection()", "saveIndicatorAndAddInSection()". --- .../controllers/IndicatorController.java | 88 +++++++++++++++++++ .../MonitorServiceCheckDeployController.java | 3 + .../uoamonitorservice/entities/Indicator.java | 2 +- 3 files changed, 92 insertions(+), 1 deletion(-) diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/IndicatorController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/IndicatorController.java index 74cba2f..aafc645 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/IndicatorController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/IndicatorController.java @@ -44,6 +44,94 @@ public class IndicatorController { @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 indicators) 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, indicators, stakeholder, topicId, categoryId, subcategoryId); +// createSectionAndSaveBulk(date, "number", "Numbers imported from file", number_indicators, stakeholder, topicId, categoryId, subcategoryId); + + return stakeholderController.setFullEntities(stakeholder, rolesUtils.getRoles()); + } + + private void createSectionsAndSaveBulk(Date date, List new_indicators, + Stakeholder stakeholder, String topicId, String categoryId, String subcategoryId) throws UnsupportedEncodingException { + Section chart_section = null; + Section number_section = null; + + List chart_indicators = null; + List number_indicators = null; + + for(Indicator indicator : new_indicators) { + if(indicator.getType().equals("chart")) { + if(chart_section == null) { + chart_section = createSection(chart_section, "chart", "Charts imported from file", date, stakeholder, topicId, categoryId, subcategoryId); + chart_indicators = chart_section.getIndicators(); + } + saveIndicatorAndAddInSection(indicator, date, stakeholder, chart_section, chart_indicators); + + } else if(indicator.getType().equals("number")) { + if(number_section == null) { + number_section = createSection(number_section, "number", "Numbers imported from file", date, stakeholder, topicId, categoryId, subcategoryId); + number_indicators = number_section.getIndicators(); + } + saveIndicatorAndAddInSection(indicator, date, stakeholder, 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<>()); + sectionController.saveSection(stakeholder.getId(), topicId, categoryId, subcategoryId, "-1", section); + + return section; + } + + private void saveIndicatorAndAddInSection(Indicator indicator, Date date, Stakeholder stakeholder, 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.getId()); + } 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) diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/MonitorServiceCheckDeployController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/MonitorServiceCheckDeployController.java index 93556d9..260e43e 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/MonitorServiceCheckDeployController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/MonitorServiceCheckDeployController.java @@ -14,6 +14,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; +import java.util.Date; import java.util.HashMap; import java.util.Map; @@ -55,6 +56,8 @@ public class MonitorServiceCheckDeployController { response.put("monitorservice.mongodb.password", mongoConfig.getPassword() == null ? null : "[unexposed value]"); // response.put("Define also", "monitorservice.mongodb.username, monitorservice.mongodb.password"); + response.put("Date of build", new Date().toString()); + return response; } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Indicator.java b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Indicator.java index ecc16bb..27d19a0 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Indicator.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Indicator.java @@ -44,7 +44,7 @@ public class Indicator { setWidth(defaultIndicator.getWidth()); setHeight(defaultIndicator.getHeight()); setTags(defaultIndicator.getTags()); - setVisibility(defaultIndicator.getVisibility()); + setVisibility(Visibility.RESTRICTED); setCreationDate(defaultIndicator.getCreationDate()); setUpdateDate(defaultIndicator.getUpdateDate()); setDefaultId(defaultIndicator.getId());