Add a field overrides in stakeholder in order to override inside entitities fields in case of references. Currently used only for visibilities.

This commit is contained in:
Konstantinos Triantafyllou 2024-11-05 15:58:11 +02:00
parent d599b7bb2d
commit 6e68a5ece8
15 changed files with 329 additions and 118 deletions

View File

@ -84,8 +84,14 @@ public class CategoryController {
log.debug("change category visibility: " + visibility + " - toggle propagate: " + propagate);
log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId);
Stakeholder stakeholder = this.stakeholderService.findByPath(stakeholderId);
if(stakeholder.isCopy() || stakeholder.getDefaultId() == null) {
Topic topic = this.topicService.findByPath(stakeholder, topicId);
Category category = this.categoryService.findByPath(topic, categoryId);
return ResponseEntity.ok(this.categoryService.changeVisibility(stakeholder.getType(), stakeholder.getAlias(), category, visibility, propagate));
return ResponseEntity.ok(this.categoryService.changeVisibility(stakeholder, category, visibility, propagate));
} else {
Topic topic = this.topicService.findByPath(this.stakeholderService.findByPath(stakeholder.getDefaultId()), topicId);
Category category = this.categoryService.findByPath(topic, categoryId);
return ResponseEntity.ok(this.categoryService.changeVisibility(stakeholder, category, visibility, propagate, stakeholderService));
}
}
}

View File

@ -127,11 +127,20 @@ public class IndicatorController {
log.debug("change indicator visibility: " + visibility);
log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId + " - Section: " + sectionId + " - Indicator: " + indicatorId);
Stakeholder stakeholder = this.stakeholderService.findByPath(stakeholderId);
if(stakeholder.isCopy() || stakeholder.getDefaultId() == null) {
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);
Indicator indicator = this.indicatorService.findByPath(section, indicatorId);
return ResponseEntity.ok(this.indicatorService.changeVisibility(stakeholder.getType(), stakeholder.getAlias(), indicator, visibility));
return ResponseEntity.ok(this.indicatorService.changeVisibility(stakeholder, indicator, visibility));
} else {
Topic topic = this.topicService.findByPath(this.stakeholderService.findByPath(stakeholder.getDefaultId()), topicId);
Category category = this.categoryService.findByPath(topic, categoryId);
SubCategory subCategory = this.subCategoryService.findByPath(category, subcategoryId);
Section section = this.sectionService.findByPath(subCategory, sectionId);
Indicator indicator = this.indicatorService.findByPath(section, indicatorId);
return ResponseEntity.ok(this.indicatorService.changeVisibility(stakeholder, indicator, visibility, stakeholderService));
}
}
}

View File

@ -47,7 +47,7 @@ public class SubCategoryController {
Stakeholder stakeholder = this.stakeholderService.findByPath(stakeholderId);
Topic topic = this.topicService.findByPath(stakeholder, topicId);
Category category = this.categoryService.findByPath(topic, categoryId);
if(subcategoryFull.getId() != null) {
if (subcategoryFull.getId() != null) {
this.subCategoryService.findByPath(category, subcategoryFull.getId());
}
return ResponseEntity.ok(this.subCategoryService.save(stakeholder, category, new SubCategory(subcategoryFull)));
@ -95,9 +95,16 @@ public class SubCategoryController {
log.debug("change subCategory visibility: " + visibility + " - toggle propagate: " + propagate);
log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId);
Stakeholder stakeholder = this.stakeholderService.findByPath(stakeholderId);
if (stakeholder.isCopy() || stakeholder.getDefaultId() == null) {
Topic topic = this.topicService.findByPath(stakeholder, topicId);
Category category = this.categoryService.findByPath(topic, categoryId);
SubCategory subCategory = this.subCategoryService.findByPath(category, subcategoryId);
return ResponseEntity.ok(this.subCategoryService.changeVisibility(stakeholder.getType(), stakeholder.getAlias(), subCategory, visibility, propagate));
return ResponseEntity.ok(this.subCategoryService.changeVisibility(stakeholder, subCategory, visibility, propagate));
} else {
Topic topic = this.topicService.findByPath(this.stakeholderService.findByPath(stakeholder.getDefaultId()), topicId);
Category category = this.categoryService.findByPath(topic, categoryId);
SubCategory subCategory = this.subCategoryService.findByPath(category, subcategoryId);
return ResponseEntity.ok(this.subCategoryService.changeVisibility(stakeholder, subCategory, visibility, propagate, stakeholderService));
}
}
}

View File

@ -72,7 +72,12 @@ public class TopicController {
log.debug("change topic visibility: " + visibility + " - toggle propagate: " + propagate);
log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId);
Stakeholder stakeholder = this.stakeholderService.findByPath(stakeholderId);
if(stakeholder.isCopy() || stakeholder.getDefaultId() == null) {
Topic topic = this.topicService.findByPath(stakeholder, topicId);
return ResponseEntity.ok(this.topicService.changeVisibility(stakeholder.getType(), stakeholder.getAlias(), topic, visibility, propagate));
return ResponseEntity.ok(this.topicService.changeVisibility(stakeholder, topic, visibility, propagate));
} else {
Topic topic = this.topicService.findByPath(this.stakeholderService.findByPath(stakeholder.getDefaultId()), topicId);
return ResponseEntity.ok(this.topicService.changeVisibility(stakeholder, topic, visibility, propagate, stakeholderService));
}
}
}

View File

@ -3,7 +3,6 @@ package eu.dnetlib.uoamonitorservice.entities;
import eu.dnetlib.uoamonitorservice.dto.CategoryFull;
import eu.dnetlib.uoamonitorservice.generics.CategoryGeneric;
import eu.dnetlib.uoamonitorservice.generics.Common;
import eu.dnetlib.uoamonitorservice.generics.TopicGeneric;
import org.springframework.data.mongodb.core.mapping.Document;
import java.util.ArrayList;

View File

@ -1,15 +1,10 @@
package eu.dnetlib.uoamonitorservice.entities;
import eu.dnetlib.uoamonitorservice.dto.StakeholderFull;
import eu.dnetlib.uoamonitorservice.generics.Common;
import eu.dnetlib.uoamonitorservice.generics.StakeholderGeneric;
import eu.dnetlib.uoamonitorservice.primitives.Umbrella;
import eu.dnetlib.uoamonitorservice.service.StakeholderService;
import org.springframework.data.mongodb.core.mapping.Document;
import java.util.Objects;
import java.util.stream.Collectors;
@Document
public class Stakeholder extends StakeholderGeneric<String, String> {

View File

@ -1,12 +1,15 @@
package eu.dnetlib.uoamonitorservice.generics;
import com.fasterxml.jackson.annotation.JsonProperty;
import eu.dnetlib.uoamonitorservice.entities.Stakeholder;
import eu.dnetlib.uoamonitorservice.primitives.Visibility;
import eu.dnetlib.uoamonitorservice.utils.GenericAccessor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.LastModifiedDate;
import java.util.Date;
import java.util.Map;
public class Common {
@Id
@ -20,7 +23,7 @@ public class Common {
protected String name;
protected String description;
protected String alias;
protected Visibility visibility = Visibility.PRIVATE;
protected Visibility visibility;
public void update(Common common) {
this.id = common.getId();
@ -104,4 +107,8 @@ public class Common {
public void setVisibility(Visibility visibility) {
this.visibility = visibility;
}
public void override(Stakeholder stakeholder) {
stakeholder.getOverride(this.id).forEach((field, value) -> GenericAccessor.setField(this, field, value));
}
}

View File

@ -5,10 +5,7 @@ import eu.dnetlib.uoamonitorservice.primitives.Locale;
import eu.dnetlib.uoamonitorservice.primitives.StakeholderType;
import eu.dnetlib.uoamonitorservice.primitives.Umbrella;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.*;
public class StakeholderGeneric<T, S> extends Common {
protected StakeholderType type;
@ -25,6 +22,7 @@ public class StakeholderGeneric<T, S> extends Common {
protected Boolean copy;
protected List<T> topics = new ArrayList<>();
protected Umbrella<S> umbrella;
protected Map<String, Map<String, Object>> overrides;
public StakeholderGeneric() {
}
@ -45,10 +43,13 @@ public class StakeholderGeneric<T, S> extends Common {
defaultId = stakeholder.getDefaultId();
setLocale(stakeholder.getLocale());
setVisibility(stakeholder.getVisibility());
this.funderType = stakeholder.getFunderType();
this.copy = stakeholder.isCopy();
this.standalone = stakeholder.isStandalone();
funderType = stakeholder.getFunderType();
copy = stakeholder.isCopy();
if(!copy) {
this.overrides = new HashMap<>();
}
creationDate = stakeholder.getCreationDate();
standalone = stakeholder.isStandalone();
updateDate = stakeholder.getUpdateDate();
}
@ -171,6 +172,15 @@ public class StakeholderGeneric<T, S> extends Common {
this.topics = topics;
}
@JsonIgnore
public Map<String, Map<String, Object>> getOverrides() {
return overrides;
}
public void setOverrides(Map<String, Map<String, Object>> overrides) {
this.overrides = overrides;
}
public Umbrella<S> getUmbrella() {
return umbrella;
}
@ -183,4 +193,17 @@ public class StakeholderGeneric<T, S> extends Common {
public void setUmbrella(Umbrella<S> umbrella) {
this.umbrella =umbrella;
}
public Map<String, Object> getOverride(String id) {
if(this.overrides == null) {
this.overrides = new HashMap<>();
}
return this.overrides.computeIfAbsent(id, k -> new HashMap<>());
}
public <V> void setOverride(String id, String field, V value) {
if(!this.copy && this.defaultId != null) {
this.getOverride(id).put(field, value);
}
}
}

View File

@ -14,7 +14,6 @@ import eu.dnetlib.uoamonitorservice.primitives.Visibility;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
@ -49,19 +48,20 @@ public class CategoryService {
return this.dao.findById(categoryId).orElseThrow(() -> new NotFoundException("Category with id: " + categoryId + " not found"));
}
public CategoryFull getFullCategory(String type, String alias, Category category) {
if (commonService.hasVisibilityAuthority(type, alias, category)) {
public CategoryFull getFullCategory(Stakeholder stakeholder, Category category) {
category.override(stakeholder);
if (commonService.hasVisibilityAuthority(stakeholder.getType(), stakeholder.getAlias(), category)) {
return new CategoryFull(category, category.getSubCategories().stream()
.map(subCategoryId -> this.subCategoryService.getFullSubCategory(type, alias, subCategoryId))
.map(subCategoryId -> this.subCategoryService.getFullSubCategory(stakeholder, subCategoryId))
.collect(Collectors.toList()));
} else {
return null;
}
}
public CategoryFull getFullCategory(String type, String alias, String categoryId) {
public CategoryFull getFullCategory(Stakeholder stakeholder, String categoryId) {
Category category = this.find(categoryId);
return this.getFullCategory(type, alias, category);
return this.getFullCategory(stakeholder, category);
}
public String build(String id) {
@ -81,7 +81,7 @@ public class CategoryService {
}
public Category save(Category category) {
if(category.getId() != null) {
if (category.getId() != null) {
category.setSubCategories(this.find(category.getId()).getSubCategories());
} else {
category.setCreationDate(new Date());
@ -108,7 +108,7 @@ public class CategoryService {
if (this.commonService.hasCreateAuthority(stakeholder.getType())) {
category = this.save(category);
this.createChildren(topic, category);
if(createOverview) {
if (createOverview) {
this.subCategoryService.save(stakeholder, category, category.createOverview());
}
this.addCategory(topic, category.getId());
@ -116,7 +116,7 @@ public class CategoryService {
this.commonService.unauthorized("You are not authorized to create a category in stakeholder with id: " + stakeholder.getId());
}
}
return this.getFullCategory(stakeholder.getType(), stakeholder.getAlias(), category);
return this.getFullCategory(stakeholder, category);
}
public void createChildren(Topic defaultTopic, Category category) {
@ -134,13 +134,13 @@ public class CategoryService {
}
public CategoryFull reorderSubCategories(Stakeholder stakeholder, Category category, List<String> subcategories) {
if(this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) {
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);
category.setUpdateDate(new Date());
this.reorderChildren(stakeholder, category, subcategories);
return this.getFullCategory(stakeholder.getType(), stakeholder.getAlias(), this.dao.save(category));
return this.getFullCategory(stakeholder, this.dao.save(category));
} else {
throw new NotFoundException("Some subCategories dont exist in the category with id " + category.getId());
}
@ -152,7 +152,7 @@ public class CategoryService {
public void reorderChildren(Stakeholder defaultStakeholder, Category defaultCategory, List<String> defaultSubCategories) {
this.stakeholderDAO.findByDefaultIdAndCopyIsTrue(defaultStakeholder.getId()).forEach(stakeholder -> {
this.dao.findByDefaultId(defaultCategory.getId()).stream().map(category -> this.getFullCategory(stakeholder.getType(), stakeholder.getAlias(), category)).forEach(category -> {
this.dao.findByDefaultId(defaultCategory.getId()).stream().map(category -> this.getFullCategory(stakeholder, category)).forEach(category -> {
this.reorderSubCategories(stakeholder, new Category(category),
this.commonService.reorder(defaultSubCategories, category.getSubCategories().stream().map(subCategory -> (Common) subCategory).collect(Collectors.toList())));
});
@ -195,12 +195,12 @@ public class CategoryService {
});
}
public CategoryFull changeVisibility(String type, String alias, CategoryFull category, Visibility visibility, Boolean propagate) {
if (this.commonService.hasEditAuthority(type, alias)) {
public CategoryFull changeVisibility(Stakeholder stakeholder, CategoryFull category, Visibility visibility, Boolean propagate) {
if (this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) {
category.setVisibility(visibility);
if (propagate) {
category.setSubCategories(category.getSubCategories().stream()
.map(subCategory -> this.subCategoryService.changeVisibility(type, alias, subCategory, visibility, true))
.map(subCategory -> this.subCategoryService.changeVisibility(stakeholder, subCategory, visibility, true))
.collect(Collectors.toList()));
}
category.update(this.save(new Category(category)));
@ -211,8 +211,30 @@ public class CategoryService {
return null;
}
public CategoryFull changeVisibility(String type, String alias, Category category, Visibility visibility, Boolean propagate) {
CategoryFull categoryFull = this.getFullCategory(type, alias, category);
return this.changeVisibility(type, alias, categoryFull, visibility, propagate);
public CategoryFull changeVisibility(Stakeholder stakeholder, Category category, Visibility visibility, Boolean propagate) {
CategoryFull categoryFull = this.getFullCategory(stakeholder, category);
return this.changeVisibility(stakeholder, categoryFull, visibility, propagate);
}
public CategoryFull changeVisibility(Stakeholder stakeholder, CategoryFull category, Visibility visibility, Boolean propagate, StakeholderService stakeholderService) {
if (this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) {
category.setVisibility(visibility);
stakeholder.setOverride(category.getId(), "visibility", visibility);
Stakeholder saved = stakeholderService.save(stakeholder);
if (propagate) {
category.setSubCategories(category.getSubCategories().stream()
.map(subCategory -> this.subCategoryService.changeVisibility(saved, subCategory, visibility, true, stakeholderService))
.collect(Collectors.toList()));
}
return category;
} else {
this.commonService.unauthorized("Change category visibility: You are not authorized to update category with id: " + category.getId());
}
return null;
}
public CategoryFull changeVisibility(Stakeholder stakeholder, Category category, Visibility visibility, Boolean propagate, StakeholderService stakeholderService) {
CategoryFull categoryFull = this.getFullCategory(stakeholder, category);
return this.changeVisibility(stakeholder, categoryFull, visibility, propagate, stakeholderService);
}
}

View File

@ -26,7 +26,7 @@ public class IndicatorService {
private final CommonService commonService;
@Autowired
public IndicatorService(StakeholderDAO stakeholderDAO, TopicDAO topicDAO, CategoryDAO categoryDAO,SubCategoryDAO subCategoryDAO,
public IndicatorService(StakeholderDAO stakeholderDAO, TopicDAO topicDAO, CategoryDAO categoryDAO, SubCategoryDAO subCategoryDAO,
SectionDAO sectionDAO, IndicatorDAO dao, CommonService commonService) {
this.stakeholderDAO = stakeholderDAO;
this.topicDAO = topicDAO;
@ -48,9 +48,10 @@ public class IndicatorService {
return this.dao.findById(indicatorId).orElseThrow(() -> new NotFoundException("Indicator with id: " + indicatorId + " not found"));
}
public Indicator getIndicator(String type, String alias, String id) {
public Indicator getIndicator(Stakeholder stakeholder, String id) {
Indicator indicator = this.find(id);
if(this.commonService.hasVisibilityAuthority(type, alias, indicator)) {
indicator.override(stakeholder);
if (this.commonService.hasVisibilityAuthority(stakeholder.getType(), stakeholder.getAlias(), indicator)) {
return indicator;
} else {
return null;
@ -68,7 +69,7 @@ public class IndicatorService {
}
public Indicator save(Indicator indicator) {
if(indicator.getId() == null) {
if (indicator.getId() == null) {
indicator.setCreationDate(new Date());
}
indicator.setUpdateDate(new Date());
@ -76,7 +77,7 @@ public class IndicatorService {
}
public Indicator save(Stakeholder stakeholder, Section section, Indicator indicator) {
if(indicator.getId() != null) {
if (indicator.getId() != null) {
if (this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) {
this.updateChildren(indicator);
indicator = this.save(indicator);
@ -118,11 +119,11 @@ public class IndicatorService {
}
public void delete(String type, Indicator indicator, boolean remove) {
if(this.commonService.hasDeleteAuthority(type)) {
if (this.commonService.hasDeleteAuthority(type)) {
this.dao.findByDefaultId(indicator.getId()).forEach(child -> {
this.delete(type, child.getId(), remove);
});
if(remove) {
if (remove) {
this.removeIndicator(indicator.getId());
}
this.dao.delete(indicator);
@ -151,8 +152,8 @@ public class IndicatorService {
});
}
public Indicator changeVisibility(String type, String alias, Indicator indicator, Visibility visibility) {
if(this.commonService.hasEditAuthority(type, alias)) {
public Indicator changeVisibility(Stakeholder stakeholder, Indicator indicator, Visibility visibility) {
if (this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) {
indicator.setVisibility(visibility);
return this.save(indicator);
} else {
@ -160,4 +161,16 @@ public class IndicatorService {
}
return null;
}
public Indicator changeVisibility(Stakeholder stakeholder, Indicator indicator, Visibility visibility, StakeholderService stakeholderService) {
if (this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) {
indicator.setVisibility(visibility);
stakeholder.setOverride(indicator.getId(), "visibility", visibility);
stakeholderService.save(stakeholder);
return indicator;
} else {
this.commonService.unauthorized("Change section visibility: You are not authorized to update section with id: " + indicator.getId());
}
return null;
}
}

View File

@ -51,15 +51,16 @@ public class SectionService {
return this.dao.findById(sectionId).orElseThrow(() -> new NotFoundException("Section with id: " + sectionId + " not found"));
}
public SectionFull getFullSection(String type, String alias, Section section) {
public SectionFull getFullSection(Stakeholder stakeholder, Section section) {
section.override(stakeholder);
return new SectionFull(section, section.getIndicators().stream()
.map(indicatorId -> this.indicatorService.getIndicator(type, alias, indicatorId))
.map(indicatorId -> this.indicatorService.getIndicator(stakeholder, indicatorId))
.collect(Collectors.toList()));
}
public SectionFull getFullSection(String type, String alias, String id) {
public SectionFull getFullSection(Stakeholder stakeholder, String id) {
Section section = this.find(id);
return this.getFullSection(type, alias, section);
return this.getFullSection(stakeholder, section);
}
public SectionFull saveFull(SectionFull sectionFull) {
@ -125,7 +126,7 @@ public class SectionService {
this.commonService.unauthorized("You are not authorized to create a section in stakeholder with id: " + stakeholder.getId());
}
}
return this.getFullSection(stakeholder.getType(), stakeholder.getAlias(), section);
return this.getFullSection(stakeholder, section);
}
public void createChildren(SubCategory defaultSubCategory, Section section, int index) {
@ -162,7 +163,7 @@ public class SectionService {
if (reorderChildren) {
this.reorderChildren(stakeholder, section, indicators);
}
return this.getFullSection(stakeholder.getType(), stakeholder.getAlias(), this.dao.save(section));
return this.getFullSection(stakeholder, this.dao.save(section));
} else {
throw new NotFoundException("Some indicators dont exist in the section with id " + section.getId());
}
@ -174,7 +175,7 @@ public class SectionService {
public void reorderChildren(Stakeholder defaultStakeholder, Section defaultSection, List<String> defaultIndicators) {
this.stakeholderDAO.findByDefaultIdAndCopyIsTrue(defaultStakeholder.getId()).forEach(stakeholder ->
this.dao.findByDefaultId(defaultSection.getId()).stream().map(section -> this.getFullSection(stakeholder.getType(), stakeholder.getAlias(), section)).forEach(section ->
this.dao.findByDefaultId(defaultSection.getId()).stream().map(section -> this.getFullSection(stakeholder, section)).forEach(section ->
this.reorderIndicators(stakeholder, new Section(section), this.commonService.reorder(defaultIndicators, section.getIndicators().stream().map(indicator -> (Common) indicator).collect(Collectors.toList())))
)
);
@ -229,10 +230,10 @@ public class SectionService {
});
}
public SectionFull changeVisibility(String type, String alias, SectionFull section, Visibility visibility) {
if (this.commonService.hasEditAuthority(type, alias)) {
public SectionFull changeVisibility(Stakeholder stakeholder, SectionFull section, Visibility visibility) {
if (this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) {
section.setIndicators(section.getIndicators().stream()
.map(indicator -> this.indicatorService.changeVisibility(type, alias, indicator, visibility))
.map(indicator -> this.indicatorService.changeVisibility(stakeholder, indicator, visibility))
.collect(Collectors.toList()));
section.update(this.save(new Section(section)));
return section;
@ -241,4 +242,16 @@ public class SectionService {
}
return null;
}
public SectionFull changeVisibility(Stakeholder stakeholder, SectionFull section, Visibility visibility, StakeholderService stakeholderService) {
if (this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) {
section.setIndicators(section.getIndicators().stream()
.map(indicator -> this.indicatorService.changeVisibility(stakeholder, indicator, visibility, stakeholderService))
.collect(Collectors.toList()));
return section;
} else {
this.commonService.unauthorized("Change section visibility: You are not authorized to update section with id: " + section.getId());
}
return null;
}
}

View File

@ -88,13 +88,13 @@ public class StakeholderService {
public StakeholderFull getFullStakeholder(Stakeholder stakeholder) {
List<TopicFull> topics = stakeholder.getTopics().stream()
.map(topicId -> topicService.getFullTopic(stakeholder.getType(), stakeholder.getAlias(), topicId))
.map(topicId -> topicService.getFullTopic(stakeholder, topicId))
.collect(Collectors.toList());
if (!stakeholder.isCopy() && stakeholder.getDefaultId() != null) {
Stakeholder defaultStakeholder = this.findByPath(stakeholder.getDefaultId());
if (defaultStakeholder != null) {
topics = defaultStakeholder.getTopics().stream()
.map(topicId -> topicService.getFullTopic(stakeholder.getType(), stakeholder.getAlias(), topicId))
.map(topicId -> topicService.getFullTopic(stakeholder, topicId))
.collect(Collectors.toList());
}
}
@ -148,6 +148,9 @@ public class StakeholderService {
public Stakeholder save(Stakeholder stakeholder) {
if (stakeholder.getId() != null) {
Stakeholder old = this.findByPath(stakeholder.getId());
if(stakeholder.getOverrides() == null) {
stakeholder.setOverrides(old.getOverrides());
}
stakeholder.setUmbrella(old.getUmbrella());
stakeholder.setStandalone(old.isStandalone());
stakeholder.setDefaultId(old.getDefaultId());
@ -162,6 +165,7 @@ public class StakeholderService {
if (defaultStakeholder != null) {
stakeholder.setTopics(defaultStakeholder.getTopics().stream().map(this.topicService::build).collect(Collectors.toList()));
}
stakeholder.setOverrides(new HashMap<>());
}
stakeholder.getTopics().forEach(this.topicService::find);
}
@ -224,7 +228,7 @@ public class StakeholderService {
if (this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) {
if (propagate) {
stakeholder.setTopics(stakeholder.getTopics().stream().
map(topic -> this.topicService.changeVisibility(stakeholder.getType(), stakeholder.getAlias(), topic, visibility, true))
map(topic -> this.topicService.changeVisibility(new Stakeholder(stakeholder, this), topic, visibility, true))
.collect(Collectors.toList()));
}
stakeholder.setVisibility(visibility);

View File

@ -53,22 +53,23 @@ public class SubCategoryService {
return this.dao.findById(subcategoryId).orElseThrow(() -> new NotFoundException("SubCategory with id: " + subcategoryId + " not found"));
}
public SubCategoryFull getFullSubCategory(String type, String alias, SubCategory subCategory) {
if(commonService.hasVisibilityAuthority(type, alias, subCategory)) {
public SubCategoryFull getFullSubCategory(Stakeholder stakeholder, SubCategory subCategory) {
subCategory.override(stakeholder);
if(commonService.hasVisibilityAuthority(stakeholder.getType(), stakeholder.getAlias(), subCategory)) {
return new SubCategoryFull(subCategory, subCategory.getNumbers().stream()
.map(sectionId -> this.sectionService.getFullSection(type, alias, sectionId))
.map(sectionId -> this.sectionService.getFullSection(stakeholder, sectionId))
.collect(Collectors.toList()),
subCategory.getCharts().stream()
.map(sectionId -> this.sectionService.getFullSection(type, alias, sectionId))
.map(sectionId -> this.sectionService.getFullSection(stakeholder, sectionId))
.collect(Collectors.toList()));
} else {
return null;
}
}
public SubCategoryFull getFullSubCategory(String type, String alias, String subCategoryId) {
public SubCategoryFull getFullSubCategory(Stakeholder stakeholder, String subCategoryId) {
SubCategory subCategory = this.find(subCategoryId);
return this.getFullSubCategory(type, alias, subCategory);
return this.getFullSubCategory(stakeholder, subCategory);
}
public String build(String id) {
@ -122,7 +123,7 @@ public class SubCategoryService {
this.commonService.unauthorized("You are not authorized to create a subCategory in stakeholder with id: " + stakeholder.getId());
}
}
return this.getFullSubCategory(stakeholder.getType(), stakeholder.getAlias(), subCategory);
return this.getFullSubCategory(stakeholder, subCategory);
}
public void createChildren(Category defaultCategory, SubCategory subCategory) {
@ -150,7 +151,7 @@ public class SubCategoryService {
this.sectionService.reorderIndicators(stakeholder, from, moveIndicator.getFrom().getIndicators(), false);
this.sectionService.reorderIndicators(stakeholder, to, moveIndicator.getTo().getIndicators(), false);
this.moveIndicatorChildren(stakeholder, subCategory, moveIndicator);
return this.getFullSubCategory(stakeholder.getType(), stakeholder.getAlias(), subCategory);
return this.getFullSubCategory(stakeholder, subCategory);
} else {
this.commonService.unauthorized("You are not authorized to move indicators in subCategory with id: " + subCategory.getId());
}
@ -160,8 +161,8 @@ public class SubCategoryService {
public void moveIndicatorChildren(Stakeholder defaultStakeholder, SubCategory defaultSubCategory, MoveIndicator moveIndicator) {
this.stakeholderDAO.findByDefaultIdAndCopyIsTrue(defaultStakeholder.getId()).forEach(stakeholder -> {
this.dao.findByDefaultId(defaultSubCategory.getId()).stream()
.map(subCategory -> this.getFullSubCategory(stakeholder.getType(), stakeholder. getAlias(), subCategory))
.collect(Collectors.toList()).forEach(subCategory -> {
.map(subCategory -> this.getFullSubCategory(stakeholder, subCategory))
.toList().forEach(subCategory -> {
SectionFull from = subCategory.getSectionByDefaultId(moveIndicator.getFrom().getId()).orElse(null);
SectionFull to = subCategory.getSectionByDefaultId(moveIndicator.getTo().getId()).orElse(null);
if(from != null && to != null) {
@ -186,7 +187,7 @@ public class SubCategoryService {
subCategory.setNumbers(numbers);
subCategory.setUpdateDate(new Date());
this.reorderChildrenNumbers(stakeholder, subCategory, numbers);
return this.getFullSubCategory(stakeholder.getType(), stakeholder.getAlias(), this.dao.save(subCategory));
return this.getFullSubCategory(stakeholder, this.dao.save(subCategory));
} else {
throw new NotFoundException("Some sections dont exist in the subCategory with id " + subCategory.getId());
}
@ -202,7 +203,7 @@ public class SubCategoryService {
if (subCategory.getCharts().size() == charts.size() && new HashSet<>(subCategory.getCharts()).containsAll(charts)) {
subCategory.setCharts(charts);
this.reorderChildrenCharts(stakeholder, subCategory, charts);
return this.getFullSubCategory(stakeholder.getType(), stakeholder.getAlias(), this.dao.save(subCategory));
return this.getFullSubCategory(stakeholder, this.dao.save(subCategory));
} else {
throw new NotFoundException("Some sections dont exist in the subCategory with id " + subCategory.getId());
}
@ -214,7 +215,7 @@ public class SubCategoryService {
public void reorderChildrenNumbers(Stakeholder defaultStakeholder, SubCategory defaultSubCategory, List<String> defaultSections) {
this.stakeholderDAO.findByDefaultIdAndCopyIsTrue(defaultStakeholder.getId()).forEach(stakeholder -> {
this.dao.findByDefaultId(defaultSubCategory.getId()).stream().map(subCategory -> this.getFullSubCategory(stakeholder.getType(), stakeholder.getAlias(), subCategory)).forEach(subCategory -> {
this.dao.findByDefaultId(defaultSubCategory.getId()).stream().map(subCategory -> this.getFullSubCategory(stakeholder, subCategory)).forEach(subCategory -> {
this.reorderNumbers(stakeholder, new SubCategory(subCategory),
this.commonService.reorder(defaultSections, subCategory.getNumbers().stream().map(section -> (Common) section).collect(Collectors.toList())));
});
@ -223,7 +224,7 @@ public class SubCategoryService {
public void reorderChildrenCharts(Stakeholder defaultStakeholder, SubCategory defaultSubCategory, List<String> defaultSections) {
this.stakeholderDAO.findByDefaultIdAndCopyIsTrue(defaultStakeholder.getId()).forEach(stakeholder -> {
this.dao.findByDefaultId(defaultSubCategory.getId()).stream().map(subCategory -> this.getFullSubCategory(stakeholder.getType(), stakeholder.getAlias(), subCategory)).forEach(subCategory -> {
this.dao.findByDefaultId(defaultSubCategory.getId()).stream().map(subCategory -> this.getFullSubCategory(stakeholder, subCategory)).forEach(subCategory -> {
this.reorderCharts(stakeholder, new SubCategory(subCategory),
this.commonService.reorder(defaultSections, subCategory.getCharts().stream().map(section -> (Common) section).collect(Collectors.toList())));
});
@ -270,15 +271,15 @@ public class SubCategoryService {
});
}
public SubCategoryFull changeVisibility(String type, String alias, SubCategoryFull subCategory, Visibility visibility, Boolean propagate) {
if(this.commonService.hasEditAuthority(type, alias)) {
public SubCategoryFull changeVisibility(Stakeholder stakeholder, SubCategoryFull subCategory, Visibility visibility, Boolean propagate) {
if(this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) {
subCategory.setVisibility(visibility);
if(propagate) {
subCategory.setNumbers(subCategory.getNumbers().stream()
.map(section -> this.sectionService.changeVisibility(type, alias, section, visibility))
.map(section -> this.sectionService.changeVisibility(stakeholder, section, visibility))
.collect(Collectors.toList()));
subCategory.setCharts(subCategory.getCharts().stream()
.map(section -> this.sectionService.changeVisibility(type, alias, section, visibility))
.map(section -> this.sectionService.changeVisibility(stakeholder, section, visibility))
.collect(Collectors.toList()));
}
subCategory.update(this.save(new SubCategory(subCategory)));
@ -289,8 +290,33 @@ public class SubCategoryService {
return null;
}
public SubCategoryFull changeVisibility(String type, String alias, SubCategory subCategory, Visibility visibility, Boolean propagate) {
SubCategoryFull subCategoryFull = this.getFullSubCategory(type, alias, subCategory);
return this.changeVisibility(type, alias, subCategoryFull, visibility, propagate);
public SubCategoryFull changeVisibility(Stakeholder stakeholder, SubCategory subCategory, Visibility visibility, Boolean propagate) {
SubCategoryFull subCategoryFull = this.getFullSubCategory(stakeholder, subCategory);
return this.changeVisibility(stakeholder, subCategoryFull, visibility, propagate);
}
public SubCategoryFull changeVisibility(Stakeholder stakeholder, SubCategoryFull subCategory, Visibility visibility, Boolean propagate, StakeholderService stakeholderService) {
if(this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) {
subCategory.setVisibility(visibility);
stakeholder.setOverride(subCategory.getId(), "visibility", visibility);
Stakeholder saved = stakeholderService.save(stakeholder);
if(propagate) {
subCategory.setNumbers(subCategory.getNumbers().stream()
.map(section -> this.sectionService.changeVisibility(saved, section, visibility, stakeholderService))
.collect(Collectors.toList()));
subCategory.setCharts(subCategory.getCharts().stream()
.map(section -> this.sectionService.changeVisibility(saved, section, visibility, stakeholderService))
.collect(Collectors.toList()));
}
return subCategory;
} else {
this.commonService.unauthorized("Change subCategory visibility: You are not authorized to update subCategory with id: " + subCategory.getId());
}
return null;
}
public SubCategoryFull changeVisibility(Stakeholder stakeholder, SubCategory subCategory, Visibility visibility, Boolean propagate, StakeholderService stakeholderService) {
SubCategoryFull subCategoryFull = this.getFullSubCategory(stakeholder, subCategory);
return this.changeVisibility(stakeholder, subCategoryFull, visibility, propagate, stakeholderService);
}
}

View File

@ -45,19 +45,20 @@ public class TopicService {
return this.dao.findById(topicId).orElseThrow(() -> new NotFoundException("Topic with id: " + topicId + " not found"));
}
public TopicFull getFullTopic(String type, String alias, Topic topic) {
if (commonService.hasVisibilityAuthority(type, alias, topic)) {
public TopicFull getFullTopic(Stakeholder stakeholder, Topic topic) {
topic.override(stakeholder);
if (commonService.hasVisibilityAuthority(stakeholder.getType(), stakeholder.getAlias(), topic)) {
return new TopicFull(topic, topic.getCategories().stream()
.map(categoryId -> this.categoryService.getFullCategory(type, alias, categoryId))
.map(categoryId -> this.categoryService.getFullCategory(stakeholder, categoryId))
.collect(Collectors.toList()));
} else {
return null;
}
}
public TopicFull getFullTopic(String type, String alias, String id) {
public TopicFull getFullTopic(Stakeholder stakeholder, String id) {
Topic topic = this.find(id);
return this.getFullTopic(type, alias, topic);
return this.getFullTopic(stakeholder, topic);
}
public String build(String id) {
@ -104,7 +105,7 @@ public class TopicService {
this.commonService.unauthorized("You are not authorized to create a topic in stakeholder with id: " + stakeholder.getId());
}
}
return this.getFullTopic(stakeholder.getType(), stakeholder.getAlias(), topic);
return this.getFullTopic(stakeholder, topic);
}
public void createChildren(Stakeholder defaultStakeholder, Topic topic) {
@ -126,7 +127,7 @@ public class TopicService {
topic.setCategories(categories);
this.reorderChildren(stakeholder, topic, categories);
topic.setUpdateDate(new Date());
return this.getFullTopic(stakeholder.getType(), stakeholder.getAlias(), this.dao.save(topic));
return this.getFullTopic(stakeholder, this.dao.save(topic));
} else {
throw new NotFoundException("Some categories dont exist in the topic with id " + topic.getId());
}
@ -138,7 +139,7 @@ public class TopicService {
public void reorderChildren(Stakeholder defaultStakeholder, Topic defaultTopic, List<String> defaultCategories) {
this.stakeholderDAO.findByDefaultIdAndCopyIsTrue(defaultStakeholder.getId()).forEach(stakeholder -> {
this.dao.findByDefaultId(defaultTopic.getId()).stream().map(topic -> this.getFullTopic(stakeholder.getType(), stakeholder.getAlias(), topic)).forEach(topic -> {
this.dao.findByDefaultId(defaultTopic.getId()).stream().map(topic -> this.getFullTopic(stakeholder, topic)).forEach(topic -> {
this.reorderCategories(stakeholder, new Topic(topic),
this.commonService.reorder(defaultCategories, topic.getCategories().stream().map(category -> (Common) category).collect(Collectors.toList())));
});
@ -181,12 +182,12 @@ public class TopicService {
});
}
public TopicFull changeVisibility(String type, String alias, TopicFull topic, Visibility visibility, Boolean propagate) {
if (this.commonService.hasEditAuthority(type, alias)) {
public TopicFull changeVisibility(Stakeholder stakeholder, TopicFull topic, Visibility visibility, Boolean propagate) {
if (this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) {
topic.setVisibility(visibility);
if (propagate) {
topic.setCategories(topic.getCategories().stream()
.map(category -> this.categoryService.changeVisibility(type, alias, category, visibility, true))
.map(category -> this.categoryService.changeVisibility(stakeholder, category, visibility, true))
.collect(Collectors.toList()));
}
topic.update(this.save(new Topic(topic)));
@ -197,8 +198,30 @@ public class TopicService {
return null;
}
public TopicFull changeVisibility(String type, String alias, Topic topic, Visibility visibility, Boolean propagate) {
TopicFull topicFull = this.getFullTopic(type, alias, topic);
return this.changeVisibility(type, alias, topicFull, visibility, propagate);
public TopicFull changeVisibility(Stakeholder stakeholder, Topic topic, Visibility visibility, Boolean propagate) {
TopicFull topicFull = this.getFullTopic(stakeholder, topic);
return this.changeVisibility(stakeholder, topicFull, visibility, propagate);
}
public TopicFull changeVisibility(Stakeholder stakeholder, TopicFull topic, Visibility visibility, Boolean propagate, StakeholderService stakeholderService) {
if (this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) {
topic.setVisibility(visibility);
stakeholder.setOverride(topic.getId(), "visibility", visibility);
Stakeholder saved = stakeholderService.save(stakeholder);
if (propagate) {
topic.setCategories(topic.getCategories().stream()
.map(category -> this.categoryService.changeVisibility(saved, category, visibility, true, stakeholderService))
.collect(Collectors.toList()));
}
return topic;
} else {
this.commonService.unauthorized("Change topic visibility: You are not authorized to update topic with id: " + topic.getId());
}
return null;
}
public TopicFull changeVisibility(Stakeholder stakeholder, Topic topic, Visibility visibility, Boolean propagate, StakeholderService stakeholderService) {
TopicFull topicFull = this.getFullTopic(stakeholder, topic);
return this.changeVisibility(stakeholder, topicFull, visibility, propagate, stakeholderService);
}
}

View File

@ -0,0 +1,59 @@
package eu.dnetlib.uoamonitorservice.utils;
import java.lang.reflect.Field;
public class GenericAccessor {
public static <T> void setField(Object obj, String fieldName, T value) {
try {
Field field = findField(obj.getClass(), fieldName);
field.setAccessible(true); // Allows access to private fields
if (field.getType().isEnum()) {
if (value instanceof String stringValue) {
Enum<?> enumValue = getEnumValue(field.getType(), stringValue);
field.set(obj, enumValue);
} else if (field.getType().isAssignableFrom(value.getClass())) {
// Directly assign if types match
field.set(obj, value);
} else {
throw new IllegalArgumentException("Incompatible types: cannot assign " + value.getClass().getName() +
" to " + field.getType().getName());
}
} else {
// Handle other field types normally
field.set(obj, value);
}
} catch (NoSuchFieldException | IllegalAccessException e) {
System.err.println("Error setting field: " + e.getMessage());
}
}
public static Object getField(Object obj, String fieldName) {
try {
Field field = findField(obj.getClass(), fieldName); // Updated to search in superclasses
field.setAccessible(true); // Allows access to private fields
return field.get(obj);
} catch (NoSuchFieldException | IllegalAccessException e) {
System.err.println("Error getting field: " + e.getMessage());
return null;
}
}
private static Field findField(Class<?> clazz, String fieldName) throws NoSuchFieldException {
Class<?> currentClass = clazz;
while (currentClass != null) {
try {
return currentClass.getDeclaredField(fieldName);
} catch (NoSuchFieldException e) {
currentClass = currentClass.getSuperclass();
}
}
throw new NoSuchFieldException("Field '" + fieldName + "' not found in class hierarchy of " + clazz.getName());
}
// Safely get the Enum value using generics
@SuppressWarnings("unchecked")
private static <E extends Enum<E>> E getEnumValue(Class<?> enumClass, String value) {
return Enum.valueOf((Class<E>) enumClass, value);
}
}