Refactor reorder methods and save sections in bulk

This commit is contained in:
Konstantinos Triantafyllou 2024-04-02 14:49:02 +03:00
parent 8a56c3ad8b
commit c83dc441d4
16 changed files with 363 additions and 745 deletions

View File

@ -14,6 +14,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@CrossOrigin(origins = "*")
public class CategoryController {
@ -40,117 +42,8 @@ public class CategoryController {
Stakeholder stakeholder = stakeholderService.findByPath(stakeholderId);
Topic topic = topicService.findByPath(stakeholder, topicId);
return this.categoryService.save(stakeholder, topic, new Category(categoryFull));
/*if (stakeholder != null) {
if (!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) {
// EXCEPTION - Access denied
throw new ForbiddenException("Save Category: You are not authorized to update stakeholder with id: " + stakeholderId);
}
Category<String> oldCategory = null;
if (categoryFull.getId() != null) {
oldCategory = categoryDAO.findById(categoryFull.getId());
if (oldCategory == null) {
// EXCEPTION - Category not found
throw new EntityNotFoundException("save category: Category with id: " + categoryFull.getId() + " not found");
}
}
Topic<String> topic = topicDAO.findById(topicId);
if (topic != null) {
if (stakeholder.getTopics().contains(topicId)) {
Category<String> category = new Category<>(categoryFull);
Date date = new Date();
category.setUpdateDate(date);
categoryFull.setUpdateDate(date);
List<String> subCategories = new ArrayList<>();
// if category not exists (no id), create a new default subcategory, identical to category
if (categoryFull.getId() == null) {
category.setCreationDate(date);
categoryFull.setCreationDate(date);
SubCategory<String> subCategory = new SubCategory<>();
subCategory.createOverviewSubCategory(categoryFull);
subCategoryDAO.save(subCategory);
List<SubCategory> subCategoriesFull = categoryFull.getSubCategories();
subCategoriesFull.add(subCategory);
for (SubCategory oldSubCategory : subCategoriesFull) {
subCategories.add(oldSubCategory.getId());
}
} else {
for (String subCategoryId : oldCategory.getSubCategories()) {
SubCategory subCategory = subCategoryDAO.findById(subCategoryId);
if (subCategory == null) {
// EXCEPTION - SubCategory not found
throw new EntityNotFoundException("Save category: SubCategory with id: " + subCategoryId + " not found (subcategory exists in category: " + category.getId() + ")");
}
subCategories.add(subCategory.getId());
}
}
category.setSubCategories(subCategories);
if (stakeholder.getDefaultId() == null) {
if (categoryFull.getId() == null) {
categoryDAO.save(category);
onSaveDefaultCategory(category, topicId);
} else {
onUpdateDefaultCategory(category, oldCategory);
categoryDAO.save(category);
}
} else {
categoryDAO.save(category);
}
List<String> categories = topic.getCategories();
int index = categories.indexOf(category.getId());
if (index == -1) {
categories.add(category.getId());
topicDAO.save(topic);
log.debug("Category saved!");
categoryFull.setId(category.getId());
}
} else {
// EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
throw new PathNotValidException("Save category: Topic with id: " + topicId + " not found in Stakeholder: " + stakeholderId);
}
} else {
// EXCEPTION - Topic not found
throw new EntityNotFoundException("Save category: Topic with id: " + topicId + " not found");
}
} else {
// EXCEPTION - Stakeholder not found
throw new EntityNotFoundException("Save category: Stakeholder with id: " + stakeholderId + " not found");
}
return categoryFull;*/
}
/* public void onSaveDefaultCategory(Category<String> category, String topicId) {
log.debug("On save default category");
List<Topic> topics = topicDAO.findByDefaultId(topicId);
for (Topic topic : topics) {
Category categoryNew = new Category();
categoryNew.copyFromDefault(category);
categoryDAO.save(categoryNew);
List<String> categories = topic.getCategories();
categories.add(categoryNew.getId());
topicDAO.save(topic);
}
String subCategoryOverviewId = category.getSubCategories().get(0);
SubCategory subCategoryOverview = subCategoryDAO.findById(subCategoryOverviewId);
subCategoryController.onSaveDefaultSubCategory(subCategoryOverview, category.getId());
}*/
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/delete", method = RequestMethod.DELETE)
@ -160,46 +53,24 @@ public class CategoryController {
@RequestParam(required = false) String children) {
log.debug("delete category");
log.debug("Id: " + categoryId + " - Stakeholder: " + stakeholderId + " - Topic: " + topicId);
Stakeholder stakeholder = this.stakeholderService.findById(stakeholderId);
Stakeholder stakeholder = this.stakeholderService.find(stakeholderId);
Topic topic = this.topicService.findByPath(stakeholder, topicId);
Category category = this.categoryService.findByPath(topic, categoryId);
this.categoryService.delete(stakeholder.getType(), category, true);
return true;
}
/*@PreAuthorize("isAuthenticated()")
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/reorder", method = RequestMethod.POST)
public List<Category> reorderCategories(@PathVariable("stakeholderId") String stakeholderId,
public List<CategoryFull> reorderCategories(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@RequestBody List<String> categories) {
log.debug("reorder categories");
log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId);
Topic<String> topic = checkForExceptions(stakeholderId, topicId);
List<String> oldCategories = topic.getCategories();
for (String categoryId : oldCategories) {
if (!categories.contains(categoryId)) {
categories.add(categoryId);
}
}
topic.setCategories(categories);
List<Category> categoriesFull = new ArrayList<>();
for (String categoryId : categories) {
Category category = categoryDAO.findById(categoryId);
if (category == null) {
// EXCEPTION - Category not found
throw new EntityNotFoundException("Reorder Categories: Category with id: " + categoryId + " not found");
}
categoriesFull.add(category);
}
topicDAO.save(topic);
log.debug("Categories reordered!");
return categoriesFull;
}*/
Stakeholder stakeholder = stakeholderService.findByPath(stakeholderId);
Topic topic = topicService.findByPath(stakeholder, topicId);
return this.topicService.reorderCategories(stakeholder, topic, categories).getCategories();
}
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/change-visibility", method = RequestMethod.POST)
@ -209,7 +80,7 @@ public class CategoryController {
@RequestParam("visibility") Visibility visibility, @RequestParam(defaultValue = "false") Boolean propagate) {
log.debug("change category visibility: " + visibility + " - toggle propagate: " + propagate);
log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId);
Stakeholder stakeholder = this.stakeholderService.findById(stakeholderId);
Stakeholder stakeholder = this.stakeholderService.find(stakeholderId);
Topic topic = this.topicService.findByPath(stakeholder, topicId);
Category category = this.categoryService.findByPath(topic, categoryId);
return this.categoryService.changeVisibility(stakeholder.getType(), stakeholder.getAlias(), category, visibility, propagate);

View File

@ -1,7 +1,10 @@
package eu.dnetlib.uoamonitorservice.controllers;
import eu.dnetlib.uoamonitorservice.dto.SectionFull;
import eu.dnetlib.uoamonitorservice.dto.StakeholderFull;
import eu.dnetlib.uoamonitorservice.entities.*;
import eu.dnetlib.uoamonitorservice.primitives.ReorderEvent;
import eu.dnetlib.uoamonitorservice.primitives.Visibility;
import eu.dnetlib.uoamonitorservice.service.*;
import org.apache.logging.log4j.LogManager;
@ -10,6 +13,10 @@ 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.ArrayList;
import java.util.List;
@RestController
@CrossOrigin(origins = "*")
public class IndicatorController {
@ -33,417 +40,41 @@ public class IndicatorController {
this.indicatorService = indicatorService;
}
/*@PreAuthorize("isAuthenticated()")
@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 {
public StakeholderFull saveBulkIndicators(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId,
@PathVariable("subcategoryId") String subcategoryId,
@RequestBody List<SectionFull> 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);
Stakeholder stakeholder = this.stakeholderService.findByPath(stakeholderId);
Topic topic = this.topicService.findByPath(stakeholder, topicId);
Category category = this.categoryService.findByPath(topic, categoryId);
SubCategory subCategory = this.subCategoryService.findByPath(category, subcategoryId);
this.sectionService.saveBulk(stakeholder, subCategory, sections);
return this.stakeholderService.getFullStakeholder(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()")
@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 {
@RequestBody Indicator indicator) {
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;
Stakeholder stakeholder = this.stakeholderService.findByPath(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);
return this.indicatorService.save(stakeholder, section, 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,
@ -455,7 +86,7 @@ public class IndicatorController {
@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);
Stakeholder stakeholder = this.stakeholderService.find(stakeholderId);
Topic topic = this.topicService.findByPath(stakeholder, topicId);
Category category = this.categoryService.findByPath(topic, categoryId);
SubCategory subCategory = this.subCategoryService.findByPath(category, subcategoryId);
@ -466,7 +97,7 @@ public class IndicatorController {
}
/*@PreAuthorize("isAuthenticated()")
@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,
@ -477,36 +108,13 @@ public class IndicatorController {
@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;
}*/
Stakeholder stakeholder = this.stakeholderService.find(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);
return this.sectionService.reorderIndicators(stakeholder, section, reorderEvent).getIndicators();
}
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{indicatorId}/change-visibility", method = RequestMethod.POST)
@ -519,7 +127,7 @@ public class IndicatorController {
@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);
Stakeholder stakeholder = this.stakeholderService.find(stakeholderId);
Topic topic = this.topicService.findByPath(stakeholder, topicId);
Category category = this.categoryService.findByPath(topic, categoryId);
SubCategory subCategory = this.subCategoryService.findByPath(category, subcategoryId);

View File

@ -2,6 +2,7 @@ package eu.dnetlib.uoamonitorservice.controllers;
import eu.dnetlib.uoamonitorservice.dto.SectionFull;
import eu.dnetlib.uoamonitorservice.entities.*;
import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException;
import eu.dnetlib.uoamonitorservice.service.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@ -9,6 +10,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@CrossOrigin(origins = "*")
public class SectionController {
@ -33,13 +36,13 @@ public class SectionController {
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/save/{index}", method = RequestMethod.POST)
public SectionFull saveSection(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId,
@PathVariable("subcategoryId") String subcategoryId,
@PathVariable("index") String index,
@RequestBody SectionFull sectionFull) {
@PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId,
@PathVariable("subcategoryId") String subcategoryId,
@PathVariable("index") String index,
@RequestBody SectionFull sectionFull) {
log.debug("save section");
log.debug("Name: "+sectionFull.getTitle() + " - Id: "+sectionFull.getId() + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId);
log.debug("Name: " + sectionFull.getTitle() + " - Id: " + sectionFull.getId() + " - Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId);
Stakeholder stakeholder = this.stakeholderService.findByPath(stakeholderId);
Topic topic = this.topicService.findByPath(stakeholder, topicId);
Category category = this.categoryService.findByPath(topic, categoryId);
@ -57,7 +60,7 @@ public class SectionController {
@RequestParam(required = false) String children) {
log.debug("delete section");
log.debug("Id: " + sectionId + " - Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId);
Stakeholder stakeholder = this.stakeholderService.findById(stakeholderId);
Stakeholder stakeholder = this.stakeholderService.find(stakeholderId);
Topic topic = this.topicService.findByPath(stakeholder, topicId);
Category category = this.categoryService.findByPath(topic, categoryId);
SubCategory subCategory = this.subCategoryService.findByPath(category, subcategoryId);
@ -66,50 +69,26 @@ public class SectionController {
return true;
}
/*@PreAuthorize("isAuthenticated()")
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{type}/reorder", method = RequestMethod.POST)
public List<Section> reorderSections(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId,
@PathVariable("subcategoryId") String subcategoryId,
@PathVariable("type") String type,
@RequestBody List<String> sections) {
log.debug("reorder sections of type: "+type);
log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId);
SubCategory<String> subCategory = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId);
if (type.equals("chart")) {
List<String> oldSections = subCategory.getCharts();
for (String sectionId : oldSections) {
if (!sections.contains(sectionId)) {
sections.add(sectionId);
}
}
subCategory.setCharts(sections);
} else if (type.equals("number")) {
List<String> oldSections = subCategory.getNumbers();
for (String sectionId : oldSections) {
if (!sections.contains(sectionId)) {
sections.add(sectionId);
}
}
subCategory.setNumbers(sections);
public List<SectionFull> reorderSections(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId,
@PathVariable("subcategoryId") String subcategoryId,
@PathVariable("type") String type,
@RequestBody List<String> sections) {
log.debug("reorder sections of type: " + type);
log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId);
Stakeholder stakeholder = this.stakeholderService.findByPath(stakeholderId);
Topic topic = this.topicService.findByPath(stakeholder, topicId);
Category category = this.categoryService.findByPath(topic, categoryId);
SubCategory subCategory = this.subCategoryService.findByPath(category, subcategoryId);
if (type.equals("number")) {
return this.subCategoryService.reorderNumbers(stakeholder, subCategory, sections).getNumbers();
} else if (type.equals("chart")) {
return this.subCategoryService.reorderCharts(stakeholder, subCategory, sections).getCharts();
} else {
throw new PathNotValidException("Type is not valid");
}
List<Section> sectionsFull = new ArrayList<>();
for(String sectionId : sections) {
Section section = sectionDAO.findById(sectionId);
if(section == null) {
// EXCEPTION - Section not found
throw new EntityNotFoundException("Reorder sections: Section with id: " + sectionId + " not found");
}
sectionsFull.add(section);
}
subCategoryDAO.save(subCategory);
log.debug("Sections reordered!");
return sectionsFull;
}*/
}
}

View File

@ -47,10 +47,9 @@ public class StakeholderController {
"@AuthorizationService.PORTAL_ADMIN, " +
"@AuthorizationService.curator(#stakeholder.getType()))")
@RequestMapping(value = "/build-stakeholder", method = RequestMethod.POST)
public StakeholderFull buildFullStakeholder(@RequestBody StakeholderFull stakeholder) {
public StakeholderFull buildStakeholder(@RequestBody StakeholderFull stakeholder) {
log.debug("build stakeholder");
log.debug("Alias: " + stakeholder.getAlias());
stakeholder = this.stakeholderService.buildStakeholder(stakeholder);
Portal portal = portalService.getPortal(stakeholder.getAlias());
if (portal == null) {
portal = new Portal();
@ -59,7 +58,7 @@ public class StakeholderController {
portal.setType(stakeholder.getType());
portalService.insertPortal(portal);
}
return stakeholder;
return this.stakeholderService.buildStakeholder(stakeholder);
}
@PreAuthorize("hasAnyAuthority(" + "@AuthorizationService.PORTAL_ADMIN)")
@ -106,8 +105,7 @@ public class StakeholderController {
public StakeholderFull saveStakeholder(@RequestBody StakeholderFull stakeholder) {
log.debug("save stakeholder");
log.debug("Alias: " + stakeholder.getAlias() + " - Id: " + stakeholder.getId());
stakeholder.update(this.stakeholderService.save(new Stakeholder(stakeholder)));
return stakeholder;
return this.stakeholderService.getFullStakeholder(this.stakeholderService.save(new Stakeholder(stakeholder)));
}
@PreAuthorize("isAuthenticated()")

View File

@ -1,8 +1,6 @@
package eu.dnetlib.uoamonitorservice.controllers;
import eu.dnetlib.uoamonitorservice.dto.CategoryFull;
import eu.dnetlib.uoamonitorservice.dto.SubCategoryFull;
import eu.dnetlib.uoamonitorservice.dto.TopicFull;
import eu.dnetlib.uoamonitorservice.entities.Category;
import eu.dnetlib.uoamonitorservice.entities.Stakeholder;
import eu.dnetlib.uoamonitorservice.entities.SubCategory;
@ -18,6 +16,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@CrossOrigin(origins = "*")
public class SubCategoryController {
@ -59,7 +59,7 @@ public class SubCategoryController {
@RequestParam(required = false) String children) {
log.debug("delete subcategory");
log.debug("Id: " + subcategoryId + " - Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId);
Stakeholder stakeholder = this.stakeholderService.findById(stakeholderId);
Stakeholder stakeholder = this.stakeholderService.find(stakeholderId);
Topic topic = this.topicService.findByPath(stakeholder, topicId);
Category category = this.categoryService.findByPath(topic, categoryId);
SubCategory subCategory = this.subCategoryService.findByPath(category, subcategoryId);
@ -67,40 +67,19 @@ public class SubCategoryController {
return true;
}
/*@PreAuthorize("isAuthenticated()")
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/reorder", method = RequestMethod.POST)
public List<SubCategory> reorderSubCategories(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId,
@RequestBody List<String> subCategories) {
public List<SubCategoryFull> reorderSubCategories(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId,
@RequestBody List<String> subCategories) {
log.debug("reorder subCategories");
log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId);
Category<String> category = checkForExceptions(stakeholderId, topicId, categoryId);
List<String> oldSubcategories = category.getSubCategories();
for (String subcategoryId : oldSubcategories) {
if (!subCategories.contains(subcategoryId)) {
subCategories.add(subcategoryId);
}
}
category.setSubCategories(subCategories);
List<SubCategory> subCategoriesFull = new ArrayList<>();
for(String subCategoryId : subCategories) {
SubCategory subCategory = subCategoryDAO.findById(subCategoryId);
if(subCategory == null) {
// EXCEPTION - SubCategory not found
throw new EntityNotFoundException("Reorder subCategories: subCategory with id: " + subCategoryId + " not found");
}
subCategoriesFull.add(subCategory);
}
categoryDAO.save(category);
log.debug("SubCategories reordered!");
return subCategoriesFull;
}*/
log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId);
Stakeholder stakeholder = this.stakeholderService.findByPath(stakeholderId);
Topic topic = this.topicService.findByPath(stakeholder, topicId);
Category category = this.categoryService.findByPath(topic, categoryId);
return this.categoryService.reorderSubCategories(stakeholder, category, subCategories).getSubCategories();
}
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/change-visibility", method = RequestMethod.POST)
@ -112,7 +91,7 @@ public class SubCategoryController {
@RequestParam(defaultValue = "false") Boolean propagate) {
log.debug("change subCategory visibility: " + visibility + " - toggle propagate: " + propagate);
log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId);
Stakeholder stakeholder = this.stakeholderService.findById(stakeholderId);
Stakeholder stakeholder = this.stakeholderService.find(stakeholderId);
Topic topic = this.topicService.findByPath(stakeholder, topicId);
Category category = this.categoryService.findByPath(topic, categoryId);
SubCategory subCategory = this.subCategoryService.findByPath(category, subcategoryId);

View File

@ -12,6 +12,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@CrossOrigin(origins = "*")
public class TopicController {
@ -42,55 +44,22 @@ public class TopicController {
@RequestParam(required = false) String children) {
log.debug("delete topic");
log.debug("Id: " + topicId + " - Stakeholder: " + stakeholderId);
Stakeholder stakeholder = stakeholderService.findById(stakeholderId);
Stakeholder stakeholder = stakeholderService.find(stakeholderId);
Topic topic = this.topicService.findByPath(stakeholder, topicId);
this.topicService.delete(stakeholder.getType(), topic, true);
return true;
}
/*@PreAuthorize("isAuthenticated()")
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/reorder", method = RequestMethod.POST)
public List<Topic> reorderTopics(@PathVariable("stakeholderId") String stakeholderId,
@RequestBody List<String> topics) {
public List<TopicFull> reorderTopics(@PathVariable("stakeholderId") String stakeholderId,
@RequestBody List<String> topics) {
log.debug("reorder topics");
log.debug("Stakeholder: "+stakeholderId);
Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId);
if(stakeholder != null) {
if(!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) {
// EXCEPTION - Access denied
throw new ForbiddenException("Reorder topics: You are not authorized to update stakeholder with id: "+stakeholderId);
}
List<String> oldTopics = stakeholder.getTopics();
for (String topicId : oldTopics) {
if (!topics.contains(topicId)) {
topics.add(topicId);
}
}
stakeholder.setTopics(topics);
List<Topic> topicsFull = new ArrayList<>();
for (String topicId : topics) {
Topic topic = topicDAO.findById(topicId);
if(topic == null) {
// EXCEPTION - Topic not found
throw new EntityNotFoundException("Reorder Topics: Topic with id: " + topicId + " not found");
}
topicsFull.add(topic);
}
stakeholderDAO.save(stakeholder);
log.debug("Topics reordered!");
return topicsFull;
} else {
// EXCEPTION - Stakeholder not found
throw new EntityNotFoundException("Reorder topics: Stakeholder with id: "+stakeholderId+" not found");
}
}*/
log.debug("Stakeholder: " + stakeholderId);
Stakeholder stakeholder = stakeholderService.findByPath(stakeholderId);
return this.stakeholderService.reorderTopics(stakeholder, topics).getTopics();
}
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/change-visibility", method = RequestMethod.POST)
@ -99,7 +68,7 @@ public class TopicController {
@RequestParam("visibility") Visibility visibility, @RequestParam(defaultValue = "false") Boolean propagate) {
log.debug("change topic visibility: " + visibility + " - toggle propagate: " + propagate);
log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId);
Stakeholder stakeholder = this.stakeholderService.findById(stakeholderId);
Stakeholder stakeholder = this.stakeholderService.find(stakeholderId);
Topic topic = this.topicService.findByPath(stakeholder, topicId);
return this.topicService.changeVisibility(stakeholder.getType(), stakeholder.getAlias(), topic, visibility, propagate);
}

View File

@ -21,19 +21,47 @@ public class Indicator extends Common {
private List<String> tags; // this field is not used anywhere now
private List<IndicatorPath> indicatorPaths;
public void copyFromDefault(Indicator defaultIndicator, Visibility visibility) {
setName(defaultIndicator.getName());
setDescription(defaultIndicator.getDescription());
setAdditionalDescription(defaultIndicator.getAdditionalDescription());
setType(defaultIndicator.getType());
setWidth(defaultIndicator.getWidth());
setHeight(defaultIndicator.getHeight());
setTags(defaultIndicator.getTags());
setVisibility(visibility);
setCreationDate(defaultIndicator.getCreationDate());
setUpdateDate(defaultIndicator.getUpdateDate());
setDefaultId(defaultIndicator.getId());
setIndicatorPaths(defaultIndicator.getIndicatorPaths());
public Indicator() {}
public Indicator(Indicator indicator) {
id = indicator.getId();
defaultId = indicator.getDefaultId();
name = indicator.getName();
description = indicator.getDescription();
additionalDescription = indicator.getAdditionalDescription();
creationDate = indicator.getCreationDate();
updateDate = indicator.getUpdateDate();
visibility = indicator.getVisibility();
indicatorPaths = indicator.getIndicatorPaths();
tags = indicator.getTags();
setType(indicator.getType());
width = indicator.getWidth();
height = indicator.getHeight();
}
public Indicator copy() {
Indicator indicator = new Indicator(this);
indicator.setDefaultId(this.getId());
indicator.setId(null);
return indicator;
}
public Indicator override(Indicator indicator, Indicator old) {
indicator = (Indicator) super.override(indicator, old);
if(this.getAdditionalDescription() != null && !this.getAdditionalDescription().equals(indicator.getAdditionalDescription()) &&
(old.getAdditionalDescription() == null || old.getAdditionalDescription().equals(indicator.getAdditionalDescription()))) {
indicator.setAdditionalDescription(this.getAdditionalDescription());
}
if(this.getWidth() != null && !this.getWidth().equals(indicator.getWidth()) &&
(old.getWidth() == null || old.getWidth().equals(indicator.getWidth()))) {
indicator.setWidth(this.getWidth());
}
if(this.getHeight() != null && !this.getHeight().equals(indicator.getHeight()) &&
(old.getHeight() == null || old.getHeight().equals(indicator.getHeight()))) {
indicator.setHeight(this.getHeight());
}
indicator.setIndicatorPaths(this.getIndicatorPaths());
return indicator;
}
public String getAdditionalDescription() {

View File

@ -5,6 +5,7 @@ import eu.dnetlib.uoamonitorservice.generics.Common;
import eu.dnetlib.uoamonitorservice.generics.SectionGeneric;
import org.springframework.data.mongodb.core.mapping.Document;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
@ -38,4 +39,12 @@ public class Section extends SectionGeneric<String> {
}
return section;
}
public void addIndicator(String id) {
this.indicators.add(id);
}
public void removeIndicator(String id) {
this.indicators.remove(id);
}
}

View File

@ -5,6 +5,8 @@ import eu.dnetlib.uoamonitorservice.generics.Common;
import eu.dnetlib.uoamonitorservice.generics.StakeholderGeneric;
import org.springframework.data.mongodb.core.mapping.Document;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

View File

@ -25,15 +25,6 @@ public class SectionGeneric<StringOrIndicator> extends Common {
indicators = new ArrayList<>();
}
public void copyFromDefault(SectionGeneric defaultSection) {
setTitle(defaultSection.getTitle());
setType(defaultSection.getType());
setDefaultId(defaultSection.id);
setCreationDate(defaultSection.getCreationDate());
setUpdateDate(defaultSection.getUpdateDate());
setIndicators(new ArrayList<>());
}
public String getTitle() {
return title;
}

View File

@ -15,6 +15,8 @@ import eu.dnetlib.uoamonitorservice.primitives.Visibility;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;
@Service
@ -70,6 +72,9 @@ public class CategoryService {
}
public Category save(Category category) {
if(category.getId() != null) {
category.setSubCategories(this.find(category.getId()).getSubCategories());
}
category.getSubCategories().forEach(this.subCategoryService::find);
return this.dao.save(category);
}
@ -116,6 +121,20 @@ public class CategoryService {
});
}
public CategoryFull reorderSubCategories(Stakeholder stakeholder, Category category, List<String> subcategories) {
if(this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) {
subcategories.forEach(this.subCategoryService::find);
if (category.getSubCategories().size() == subcategories.size() && new HashSet<>(category.getSubCategories()).containsAll(subcategories)) {
category.setSubCategories(subcategories);
return this.getFullCategory(stakeholder.getType(), stakeholder.getAlias(), this.dao.save(category));
} else {
throw new EntityNotFoundException("Some subCategories dont exist in the category with id " + category.getId());
}
} else {
throw new ForbiddenException("You are not authorized to reorder subCategories in category with id: " + category.getId());
}
}
public void delete(String type, Category category, boolean remove) {
if (this.commonService.hasDeleteAuthority(type)) {
this.dao.findByDefaultId(category.getId()).forEach(child -> {

View File

@ -1,11 +1,11 @@
package eu.dnetlib.uoamonitorservice.service;
import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException;
import eu.dnetlib.uoamonitorservice.dao.IndicatorDAO;
import eu.dnetlib.uoamonitorservice.dao.SectionDAO;
import eu.dnetlib.uoamonitorservice.dao.*;
import eu.dnetlib.uoamonitorservice.dto.SectionFull;
import eu.dnetlib.uoamonitorservice.entities.Indicator;
import eu.dnetlib.uoamonitorservice.entities.Section;
import eu.dnetlib.uoamonitorservice.entities.Stakeholder;
import eu.dnetlib.uoamonitorservice.entities.SubCategory;
import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException;
import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException;
@ -13,25 +13,34 @@ import eu.dnetlib.uoamonitorservice.primitives.Visibility;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class IndicatorService {
private final StakeholderDAO stakeholderDAO;
private final TopicDAO topicDAO;
private final CategoryDAO categoryDAO;
private final SubCategoryDAO subCategoryDAO;
private final SectionDAO sectionDAO;
private final IndicatorDAO dao;
private final CommonService commonService;
@Autowired
public IndicatorService(SectionDAO sectionDAO, IndicatorDAO dao, CommonService commonService) {
public IndicatorService(StakeholderDAO stakeholderDAO, TopicDAO topicDAO, CategoryDAO categoryDAO,SubCategoryDAO subCategoryDAO,
SectionDAO sectionDAO, IndicatorDAO dao, CommonService commonService) {
this.stakeholderDAO = stakeholderDAO;
this.topicDAO = topicDAO;
this.categoryDAO = categoryDAO;
this.subCategoryDAO = subCategoryDAO;
this.sectionDAO = sectionDAO;
this.dao = dao;
this.commonService = commonService;
}
public Indicator save(Indicator indicator) {
return this.dao.save(indicator);
public Indicator find(String id) {
return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("Indicator with id: " + id + " not found"));
}
public Indicator findByPath(Section section, String indicatorId) {
@ -41,10 +50,6 @@ public class IndicatorService {
return this.dao.findById(indicatorId).orElseThrow(() -> new EntityNotFoundException("Indicator with id: " + indicatorId + " not found"));
}
public Indicator find(String id) {
return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("Indicator with id: " + id + " not found"));
}
public Indicator getIndicator(String type, String alias, String id) {
Indicator indicator = this.find(id);
if(this.commonService.hasVisibilityAuthority(type, alias, indicator)) {
@ -54,6 +59,52 @@ public class IndicatorService {
}
}
public Indicator save(Indicator indicator) {
return this.dao.save(indicator);
}
public Indicator save(Stakeholder stakeholder, Section section, Indicator indicator) {
if(indicator.getId() != null) {
if (this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) {
this.updateChildren(indicator);
indicator = this.save(indicator);
} else {
throw new ForbiddenException("You are not authorized to update stakeholder with id: " + stakeholder.getId());
}
} else {
if (this.commonService.hasCreateAuthority(stakeholder.getType())) {
indicator = this.save(indicator);
this.createChildren(section, indicator);
this.addIndicator(section, indicator.getId());
} else {
throw new ForbiddenException("You are not authorized to create an indicator in stakeholder with id: " + stakeholder.getId());
}
}
return indicator;
}
public void createChildren(Section defaultSection, Indicator indicator) {
this.sectionDAO.findByDefaultId(defaultSection.getId()).forEach(section -> {
List<SubCategory> subCategories = this.subCategoryDAO.findByNumbersContaining(section.getId());
subCategories.addAll(this.subCategoryDAO.findByChartsContaining(section.getId()));
subCategories.forEach(subCategory -> {
this.categoryDAO.findBySubCategoriesContaining(subCategory.getId()).forEach(category -> {
this.topicDAO.findByCategoriesContaining(category.getId()).forEach(topic -> {
this.stakeholderDAO.findByTopicsContaining(topic.getId()).forEach(stakeholder -> {
this.save(stakeholder, section, indicator.copy());
});
});
});
});
});
}
public void updateChildren(Indicator indicator) {
this.dao.findByDefaultId(indicator.getId()).forEach(child -> {
this.save(indicator.override(child, this.find(indicator.getId())));
});
}
public void delete(String type, Indicator indicator, boolean remove) {
if(this.commonService.hasDeleteAuthority(type)) {
this.dao.findByDefaultId(indicator.getId()).forEach(child -> {
@ -74,9 +125,14 @@ public class IndicatorService {
this.delete(type, indicator, remove);
}
public void addIndicator(Section section, String id) {
section.addIndicator(id);
this.sectionDAO.save(section);
}
public void removeIndicator(String id) {
this.sectionDAO.findByIndicatorsContaining(id).forEach(section -> {
section.getIndicators().remove(id);
section.removeIndicator(id);
this.sectionDAO.save(section);
});
}

View File

@ -3,13 +3,17 @@ package eu.dnetlib.uoamonitorservice.service;
import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException;
import eu.dnetlib.uoamonitorservice.dao.*;
import eu.dnetlib.uoamonitorservice.dto.SectionFull;
import eu.dnetlib.uoamonitorservice.entities.*;
import eu.dnetlib.uoamonitorservice.entities.Section;
import eu.dnetlib.uoamonitorservice.entities.Stakeholder;
import eu.dnetlib.uoamonitorservice.entities.SubCategory;
import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException;
import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException;
import eu.dnetlib.uoamonitorservice.primitives.ReorderEvent;
import eu.dnetlib.uoamonitorservice.primitives.Visibility;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;
@ -65,10 +69,25 @@ public class SectionService {
}
public Section save(Section section) {
if(section.getId() != null) {
section.setIndicators(this.find(section.getId()).getIndicators());
}
section.getIndicators().forEach(this.indicatorService::find);
return this.dao.save(section);
}
public void saveBulk(Stakeholder stakeholder, SubCategory subCategory, List<SectionFull> sections) {
if (this.commonService.hasCreateAuthority(stakeholder.getType())) {
sections = sections.stream().map(this::buildSection).collect(Collectors.toList());
sections.forEach(section -> {
this.addSection(subCategory, section.getId());
this.createChildren(subCategory, new Section(section), -1);
});
} else {
throw new ForbiddenException("You are not authorized to create sections in stakeholder with id: " + stakeholder.getId());
}
}
public SectionFull save(Stakeholder stakeholder, SubCategory subCategory, Section section, int index) {
section.setStakeholderAlias(stakeholder.getAlias());
if (section.getId() != null) {
@ -97,6 +116,9 @@ public class SectionService {
this.topicDAO.findByCategoriesContaining(category.getId()).forEach(topic -> {
this.stakeholderDAO.findByTopicsContaining(topic.getId()).forEach(stakeholder -> {
this.save(stakeholder, subCategory, section.copy(), index);
section.getIndicators().forEach(indicator -> {
this.indicatorService.createChildren(section, this.indicatorService.find(indicator).copy());
});
});
});
});
@ -109,6 +131,25 @@ public class SectionService {
});
}
public SectionFull reorderIndicators(Stakeholder stakeholder, Section section, ReorderEvent reorderEvent) {
if (this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) {
reorderEvent.getIds().forEach(this.indicatorService::find);
if (reorderEvent.getAction().equals("added")) {
section.addIndicator(reorderEvent.getTarget());
} else if (reorderEvent.getAction().equals("removed")) {
section.removeIndicator(reorderEvent.getTarget());
}
if (section.getIndicators().size() == reorderEvent.getIds().size() && new HashSet<>(section.getIndicators()).containsAll(reorderEvent.getIds())) {
section.setIndicators(reorderEvent.getIds());
return this.getFullSection(stakeholder.getType(), stakeholder.getAlias(), this.dao.save(section));
} else {
throw new EntityNotFoundException("Some indicators dont exist in the section with id " + section.getId());
}
} else {
throw new ForbiddenException("You are not authorized to reorder indicators in section with id: " + section.getId());
}
}
public void delete(String type, Section section, boolean remove) {
if(this.commonService.hasDeleteAuthority(type)) {
this.dao.findByDefaultId(section.getId()).forEach(child -> {
@ -131,6 +172,10 @@ public class SectionService {
this.delete(type, section, remove);
}
public void addSection(SubCategory subCategory, String id) {
this.addSection(subCategory, id, -1);
}
public void addSection(SubCategory subCategory, String id, int index) {
if(this.find(id).isNumber()) {
subCategory.addNumber(id, index);

View File

@ -9,6 +9,7 @@ import eu.dnetlib.uoamonitorservice.primitives.Visibility;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;
@ -27,7 +28,7 @@ public class StakeholderService {
this.topicService = topicService;
}
public Stakeholder findById(String id) {
public Stakeholder find(String id) {
return this.dao.findById(id).orElseThrow(() -> new EntityNotFoundException("Stakeholder with id: " + id + " not found"));
}
@ -95,12 +96,29 @@ public class StakeholderService {
}
public Stakeholder save(Stakeholder stakeholder) {
if(stakeholder.getId() != null) {
stakeholder.setTopics(this.find(stakeholder.getId()).getTopics());
}
stakeholder.getTopics().forEach(this.topicService::find);
return this.dao.save(stakeholder);
}
public StakeholderFull reorderTopics(Stakeholder stakeholder, List<String> topics) {
if(this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) {
topics.forEach(this.topicService::find);
if (stakeholder.getTopics().size() == topics.size() && new HashSet<>(stakeholder.getTopics()).containsAll(topics)) {
stakeholder.setTopics(topics);
return this.getFullStakeholder(this.dao.save(stakeholder));
} else {
throw new EntityNotFoundException("Some topics dont exist in the stakeholder with id " + stakeholder.getId());
}
} else {
throw new ForbiddenException("You are not authorized to reorder topics in stakeholder with id: " + stakeholder.getId());
}
}
public String delete(String id) {
Stakeholder stakeholder = this.findById(id);
Stakeholder stakeholder = this.find(id);
if(this.commonService.hasDeleteAuthority(stakeholder.getType())) {
this.dao.findByDefaultId(stakeholder.getId()).forEach(child -> {
this.delete(child.getId());

View File

@ -5,23 +5,18 @@ import eu.dnetlib.uoamonitorservice.dao.CategoryDAO;
import eu.dnetlib.uoamonitorservice.dao.StakeholderDAO;
import eu.dnetlib.uoamonitorservice.dao.SubCategoryDAO;
import eu.dnetlib.uoamonitorservice.dao.TopicDAO;
import eu.dnetlib.uoamonitorservice.dto.CategoryFull;
import eu.dnetlib.uoamonitorservice.dto.SubCategoryFull;
import eu.dnetlib.uoamonitorservice.dto.TopicFull;
import eu.dnetlib.uoamonitorservice.entities.Category;
import eu.dnetlib.uoamonitorservice.entities.Stakeholder;
import eu.dnetlib.uoamonitorservice.entities.SubCategory;
import eu.dnetlib.uoamonitorservice.entities.Topic;
import eu.dnetlib.uoamonitorservice.generics.Common;
import eu.dnetlib.uoamonitorservice.generics.SectionGeneric;
import eu.dnetlib.uoamonitorservice.generics.SubCategoryGeneric;
import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException;
import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException;
import eu.dnetlib.uoamonitorservice.primitives.IndicatorType;
import eu.dnetlib.uoamonitorservice.primitives.Visibility;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;
@Service
@ -82,6 +77,11 @@ public class SubCategoryService {
}
public SubCategory save(SubCategory subCategory) {
if(subCategory.getId() != null) {
SubCategory old = this.find(subCategory.getId());
subCategory.setNumbers(old.getNumbers());
subCategory.setCharts(old.getCharts());
}
subCategory.getNumbers().forEach(this.sectionService::find);
subCategory.getCharts().forEach(this.sectionService::find);
return this.dao.save(subCategory);
@ -126,6 +126,34 @@ public class SubCategoryService {
});
}
public SubCategoryFull reorderNumbers(Stakeholder stakeholder, SubCategory subCategory, List<String> numbers) {
if(this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) {
numbers.forEach(this.sectionService::find);
if (subCategory.getNumbers().size() == numbers.size() && new HashSet<>(subCategory.getNumbers()).containsAll(numbers)) {
subCategory.setNumbers(numbers);
return this.getFullSubCategory(stakeholder.getType(), stakeholder.getAlias(), this.dao.save(subCategory));
} else {
throw new EntityNotFoundException("Some sections dont exist in the subCategory with id " + subCategory.getId());
}
} else {
throw new ForbiddenException("You are not authorized to reorder sections in subCategory with id: " + subCategory.getId());
}
}
public SubCategoryFull reorderCharts(Stakeholder stakeholder, SubCategory subCategory, List<String> charts) {
if(this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) {
charts.forEach(this.sectionService::find);
if (subCategory.getCharts().size() == charts.size() && new HashSet<>(subCategory.getCharts()).containsAll(charts)) {
subCategory.setCharts(charts);
return this.getFullSubCategory(stakeholder.getType(), stakeholder.getAlias(), this.dao.save(subCategory));
} else {
throw new EntityNotFoundException("Some sections dont exist in the subCategory with id " + subCategory.getId());
}
} else {
throw new ForbiddenException("You are not authorized to reorder sections in subCategory with id: " + subCategory.getId());
}
}
public void delete(String type, SubCategory subCategory, boolean remove) {
if(this.commonService.hasDeleteAuthority(type)) {
this.dao.findByDefaultId(subCategory.getId()).forEach(child -> {

View File

@ -12,6 +12,7 @@ import eu.dnetlib.uoamonitorservice.primitives.Visibility;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;
@ -65,6 +66,9 @@ public class TopicService {
}
public Topic save(Topic topic) {
if(topic.getId() != null) {
topic.setCategories(this.find(topic.getId()).getCategories());
}
topic.getCategories().forEach(this.categoryService::find);
return this.dao.save(topic);
}
@ -102,6 +106,20 @@ public class TopicService {
});
}
public TopicFull reorderCategories(Stakeholder stakeholder, Topic topic, List<String> categories) {
if(this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) {
categories.forEach(this.categoryService::find);
if (topic.getCategories().size() == categories.size() && new HashSet<>(topic.getCategories()).containsAll(categories)) {
topic.setCategories(categories);
return this.getFullTopic(stakeholder.getType(), stakeholder.getAlias(), this.dao.save(topic));
} else {
throw new EntityNotFoundException("Some categories dont exist in the topic with id " + topic.getId());
}
} else {
throw new ForbiddenException("You are not authorized to reorder categories in topic with id: " + topic.getId());
}
}
public void delete(String type, Topic topic, boolean remove) {
if (this.commonService.hasDeleteAuthority(type)) {
this.dao.findByDefaultId(topic.getId()).forEach(child -> {