[Trunk | Monitor Service]: Updated methods "saveBulkIndicators()" and "createSectionsAndSaveBulk()" to import List<Section<Indicator>> instead of List<Indicator>.

This commit is contained in:
Konstantina Galouni 2022-09-08 23:34:25 +03:00
parent 44f52c36d9
commit cddb8a2de1
1 changed files with 31 additions and 31 deletions

View File

@ -9,13 +9,10 @@ import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException;
import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.util.*; import java.util.*;
@RestController @RestController
@ -56,7 +53,7 @@ public class IndicatorController {
@PathVariable("topicId") String topicId, @PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId, @PathVariable("categoryId") String categoryId,
@PathVariable("subcategoryId") String subcategoryId, @PathVariable("subcategoryId") String subcategoryId,
@RequestBody List<Indicator> indicators) throws UnsupportedEncodingException { @RequestBody List<Section<Indicator>> sections) throws UnsupportedEncodingException {
log.debug("save bulk indicators"); log.debug("save bulk indicators");
log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId); log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId);
@ -64,33 +61,35 @@ public class IndicatorController {
Date date = new Date(); Date date = new Date();
createSectionsAndSaveBulk(date, indicators, stakeholder, topicId, categoryId, subcategoryId); createSectionsAndSaveBulk(date, sections, stakeholder, topicId, categoryId, subcategoryId);
// createSectionAndSaveBulk(date, "number", "Numbers imported from file", number_indicators, stakeholder, topicId, categoryId, subcategoryId); // createSectionAndSaveBulk(date, "number", "Numbers imported from file", number_indicators, stakeholder, topicId, categoryId, subcategoryId);
return stakeholderController.setFullEntities(stakeholder, rolesUtils.getRoles()); return stakeholderController.setFullEntities(stakeholder, rolesUtils.getRoles());
} }
private void createSectionsAndSaveBulk(Date date, List<Indicator> new_indicators, private void createSectionsAndSaveBulk(Date date, List<Section<Indicator>> old_sections,
Stakeholder stakeholder, String topicId, String categoryId, String subcategoryId) throws UnsupportedEncodingException { Stakeholder stakeholder, String topicId, String categoryId, String subcategoryId) throws UnsupportedEncodingException {
for(Section<Indicator> section : old_sections) {
Section chart_section = null; Section chart_section = null;
Section number_section = null; Section number_section = null;
List<String> chart_indicators = null; List<String> chart_indicators = null;
List<String> number_indicators = null; List<String> number_indicators = null;
for(Indicator indicator : new_indicators) { if(section.getType().equals("chart")) {
if(indicator.getType().equals("chart")) { chart_section = createSection(chart_section, "chart", (section.getTitle() != null ? section.getTitle() : "Charts")+" imported from file", date, stakeholder, topicId, categoryId, subcategoryId);
if(chart_section == null) {
chart_section = createSection(chart_section, "chart", "Charts imported from file", date, stakeholder, topicId, categoryId, subcategoryId);
chart_indicators = chart_section.getIndicators(); chart_indicators = chart_section.getIndicators();
} else if(section.getType().equals("number")) {
number_section = createSection(number_section, "number", (section.getTitle() != null ? section.getTitle() : "Numbers")+ " imported from file", date, stakeholder, topicId, categoryId, subcategoryId);
number_indicators = number_section.getIndicators();
} }
List<Indicator> indicators = section.getIndicators();
for (Indicator indicator : indicators) {
if (indicator.getType().equals("chart")) {
saveIndicatorAndAddInSection(indicator, date, stakeholder, chart_section, chart_indicators); saveIndicatorAndAddInSection(indicator, date, stakeholder, chart_section, chart_indicators);
} else if (indicator.getType().equals("number")) { } 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); saveIndicatorAndAddInSection(indicator, date, stakeholder, number_section, number_indicators);
} }
} }
@ -102,6 +101,7 @@ public class IndicatorController {
sectionDAO.save(number_section); sectionDAO.save(number_section);
} }
} }
}
private Section createSection(Section section, String type, String title, Date date, private Section createSection(Section section, String type, String title, Date date,
Stakeholder stakeholder, String topicId, String categoryId, String subcategoryId) { Stakeholder stakeholder, String topicId, String categoryId, String subcategoryId) {
@ -112,7 +112,7 @@ public class IndicatorController {
section.setUpdateDate(date); section.setUpdateDate(date);
section.setCreationDate(date); section.setCreationDate(date);
section.setIndicators(new ArrayList<>()); section.setIndicators(new ArrayList<>());
sectionController.saveSection(stakeholder.getId(), topicId, categoryId, subcategoryId, "-1", section); section = sectionController.saveSection(stakeholder.getId(), topicId, categoryId, subcategoryId, "-1", section);
return section; return section;
} }