Create changeVisibility method in common service. Move icon to Common class. Enable Mongo Auditing.

This commit is contained in:
Konstantinos Triantafyllou 2024-11-07 12:24:06 +02:00
parent f22967cf27
commit 2a19bbae7f
12 changed files with 55 additions and 117 deletions

View File

@ -10,6 +10,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.mongodb.config.EnableMongoAuditing;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
@ -17,6 +18,7 @@ import java.util.Collections;
@Configuration
@EnableMongoRepositories(basePackages = {"eu.dnetlib.uoamonitorservice.dao", "eu.dnetlib.uoaadmintoolslibrary.dao"})
@EnableMongoAuditing
public class MongoConnection {
private final MongoConfig mongoConfig;

View File

@ -40,12 +40,7 @@ public class Topic extends TopicGeneric<String> {
}
public Topic override(Topic topic, Topic old) {
topic = (Topic) super.override(topic, old);
if(this.getIcon() != null && !this.getIcon().equals(topic.getIcon()) &&
(old.getIcon() == null || old.getIcon().equals(topic.getIcon()))) {
topic.setIcon(this.getIcon());
}
return topic;
return (Topic) super.override(topic, old);
}
public void addCategory(String id) {

View File

@ -1,31 +1,18 @@
package eu.dnetlib.uoamonitorservice.generics;
import org.springframework.data.mongodb.core.mapping.Document;
import java.util.ArrayList;
import java.util.List;
@Document
public class CategoryGeneric<T> extends Common {
protected boolean isOverview;
protected List<T> subCategories;
public CategoryGeneric() {}
public CategoryGeneric(CategoryGeneric category) {
super(category);
isOverview = category.getIsOverview();
subCategories = new ArrayList<>();
}
public boolean getIsOverview() {
return isOverview;
}
public void setIsOverview(boolean isOverview) {
this.isOverview = isOverview;
}
public List<T> getSubCategories() {
return subCategories;
}

View File

@ -26,6 +26,7 @@ public class Common {
protected String name;
protected String description;
protected String alias;
protected String icon;
protected Visibility visibility;
@Transient
protected boolean override;
@ -40,6 +41,7 @@ public class Common {
this.name = common.getName();
this.description = common.getDescription();
this.alias = common.getAlias();
this.icon = common.getIcon();
this.setVisibility(common.getVisibility());
this.override = common.isOverride();
}
@ -60,6 +62,10 @@ public class Common {
(old.getDescription() == null || old.getDescription().equals(common.getDescription()))) {
common.setDescription(this.getDescription());
}
if(this.getIcon() != null && !this.getIcon().equals(common.getIcon()) &&
(old.getIcon() == null || old.getIcon().equals(common.getIcon()))) {
common.setIcon(this.getIcon());
}
return common;
}
@ -119,6 +125,14 @@ public class Common {
this.alias = alias;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public Visibility getVisibility() {
return visibility;
}

View File

@ -4,7 +4,6 @@ import java.util.ArrayList;
import java.util.List;
public class TopicGeneric<T> extends Common {
protected String icon;
protected List<T> categories;
public TopicGeneric() {
@ -12,19 +11,9 @@ public class TopicGeneric<T> extends Common {
public TopicGeneric(TopicGeneric topic) {
super(topic);
icon = topic.getIcon();
categories = new ArrayList<>();
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public List<T> getCategories() {
return categories;
}

View File

@ -5,6 +5,7 @@ import eu.dnetlib.uoamonitorservice.dao.CategoryDAO;
import eu.dnetlib.uoamonitorservice.dao.StakeholderDAO;
import eu.dnetlib.uoamonitorservice.dao.TopicDAO;
import eu.dnetlib.uoamonitorservice.dto.CategoryFull;
import eu.dnetlib.uoamonitorservice.dto.TopicFull;
import eu.dnetlib.uoamonitorservice.entities.Category;
import eu.dnetlib.uoamonitorservice.entities.Stakeholder;
import eu.dnetlib.uoamonitorservice.entities.Topic;
@ -83,10 +84,7 @@ public class CategoryService {
public Category save(Category category) {
if (category.getId() != null) {
category.setSubCategories(this.find(category.getId()).getSubCategories());
} else {
category.setCreationDate(new Date());
}
category.setUpdateDate(new Date());
category.getSubCategories().forEach(this.subCategoryService::find);
return this.dao.save(category);
}
@ -138,7 +136,6 @@ public class CategoryService {
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, this.dao.save(category));
} else {
@ -183,32 +180,19 @@ public class CategoryService {
public void addCategory(Topic topic, String id) {
topic.addCategory(id);
topic.setUpdateDate(new Date());
this.topicDAO.save(topic);
}
public void removeCategory(String id) {
this.topicDAO.findByCategory(id).forEach(topic -> {
topic.removeCategory(id);
topic.setUpdateDate(new Date());
this.topicDAO.save(topic);
});
}
public CategoryFull changeVisibility(Stakeholder stakeholder, CategoryFull category, Visibility visibility, Boolean propagate) {
if (this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) {
Stakeholder saved;
if (!category.getVisibility().equals(visibility)) {
category.setVisibility(visibility);
if(stakeholder.isReference()) {
stakeholder.setOverride(category.getId(), "visibility", visibility);
saved = stakeholderDAO.save(stakeholder);
} else {
saved = stakeholder;
}
} else {
saved = stakeholder;
}
Stakeholder saved = this.commonService.changeVisibility(stakeholder, category, visibility, stakeholderDAO);
if (propagate) {
category.setSubCategories(category.getSubCategories().stream()
.map(subCategory -> this.subCategoryService.changeVisibility(saved, subCategory, visibility, true))
@ -228,4 +212,19 @@ public class CategoryService {
CategoryFull categoryFull = this.getFullCategory(stakeholder, category);
return this.changeVisibility(stakeholder, categoryFull, visibility, propagate);
}
public CategoryFull restoreDefault(Stakeholder stakeholder, Category category) {
if(!stakeholder.isReference()) {
stakeholder.removeOverride(category.getId());
return this.getFullCategory(stakeholderDAO.save(stakeholder), category);
} else if(!stakeholder.isDefault()) {
String id = category.getId();
List<String> subCategories = category.getSubCategories();
category = this.find(category.getDefaultId()).copy();
category.setId(id);
category.setSubCategories(subCategories);
return this.getFullCategory(stakeholder, this.save(category));
}
return this.getFullCategory(stakeholder, category);
}
}

View File

@ -3,6 +3,8 @@ package eu.dnetlib.uoamonitorservice.service;
import eu.dnetlib.uoaauthorizationlibrary.authorization.exceptions.http.ForbiddenException;
import eu.dnetlib.uoaauthorizationlibrary.authorization.exceptions.http.UnauthorizedException;
import eu.dnetlib.uoaauthorizationlibrary.authorization.security.AuthorizationService;
import eu.dnetlib.uoamonitorservice.dao.StakeholderDAO;
import eu.dnetlib.uoamonitorservice.entities.Stakeholder;
import eu.dnetlib.uoamonitorservice.generics.Common;
import eu.dnetlib.uoamonitorservice.primitives.Visibility;
import org.springframework.beans.factory.annotation.Autowired;
@ -52,6 +54,14 @@ public class CommonService {
}
}
public void unauthorized(String message) {
if(authorizationService.getAaiId() != null) {
throw new ForbiddenException(message);
} else {
throw new UnauthorizedException(message);
}
}
public List<String> reorder(List<String> defaultIds, List<Common> commons) {
Map<String, Integer> map = new HashMap<>();
for(int i = 0; i < commons.size(); i++) {
@ -68,11 +78,14 @@ public class CommonService {
return ids;
}
public void unauthorized(String message) {
if(authorizationService.getAaiId() != null) {
throw new ForbiddenException(message);
} else {
throw new UnauthorizedException(message);
public Stakeholder changeVisibility(Stakeholder stakeholder, Common common, Visibility visibility, StakeholderDAO stakeholderDAO) {
if (!common.getVisibility().equals(visibility)) {
common.setVisibility(visibility);
if (stakeholder.isReference()) {
stakeholder.setOverride(common.getId(), "visibility", visibility);
stakeholder = stakeholderDAO.save(stakeholder);
}
}
return stakeholder;
}
}

View File

@ -69,10 +69,6 @@ public class IndicatorService {
}
public Indicator save(Indicator indicator) {
if (indicator.getId() == null) {
indicator.setCreationDate(new Date());
}
indicator.setUpdateDate(new Date());
return this.dao.save(indicator);
}
@ -140,25 +136,19 @@ public class IndicatorService {
public void addIndicator(Section section, String id) {
section.addIndicator(id);
section.setUpdateDate(new Date());
this.sectionDAO.save(section);
}
public void removeIndicator(String id) {
this.sectionDAO.findByIndicator(id).forEach(section -> {
section.removeIndicator(id);
section.setUpdateDate(new Date());
this.sectionDAO.save(section);
});
}
public Indicator changeVisibility(Stakeholder stakeholder, Indicator indicator, Visibility visibility) {
if (this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) {
if (!indicator.getVisibility().equals(visibility)) {
indicator.setVisibility(visibility);
stakeholder.setOverride(indicator.getId(), "visibility", visibility);
stakeholderDAO.save(stakeholder);
}
stakeholder = this.commonService.changeVisibility(stakeholder, indicator, visibility, stakeholderDAO);
if(!stakeholder.isReference()) {
indicator = this.save(indicator);
}

View File

@ -87,10 +87,7 @@ public class SectionService {
public Section save(Section section) {
if (section.getId() != null) {
section.setIndicators(this.find(section.getId()).getIndicators());
} else {
section.setCreationDate(new Date());
}
section.setUpdateDate(new Date());
section.getIndicators().forEach(this.indicatorService::find);
return this.dao.save(section);
}
@ -159,7 +156,6 @@ public class SectionService {
indicators.forEach(this.indicatorService::find);
if (section.getIndicators().size() == indicators.size() && new HashSet<>(section.getIndicators()).containsAll(indicators)) {
section.setIndicators(indicators);
section.setUpdateDate(new Date());
if (reorderChildren) {
this.reorderChildren(stakeholder, section, indicators);
}
@ -213,19 +209,16 @@ public class SectionService {
} else {
subCategory.addChart(id, index);
}
subCategory.setUpdateDate(new Date());
this.subCategoryDAO.save(subCategory);
}
public void removeSection(String id) {
this.subCategoryDAO.findByNumber(id).forEach(subCategory -> {
subCategory.removeNumber(id);
subCategory.setUpdateDate(new Date());
this.subCategoryDAO.save(subCategory);
});
this.subCategoryDAO.findByChart(id).forEach(subCategory -> {
subCategory.removeChart(id);
subCategory.setUpdateDate(new Date());
this.subCategoryDAO.save(subCategory);
});
}

View File

@ -169,10 +169,7 @@ public class StakeholderService {
}
stakeholder.getTopics().forEach(this.topicService::find);
}
} else {
stakeholder.setCreationDate(new Date());
}
stakeholder.setUpdateDate(new Date());
return this.dao.save(stakeholder);
}
@ -181,7 +178,6 @@ public class StakeholderService {
topics.forEach(this.topicService::find);
if (stakeholder.getTopics().size() == topics.size() && new HashSet<>(stakeholder.getTopics()).containsAll(topics)) {
stakeholder.setTopics(topics);
stakeholder.setUpdateDate(new Date());
this.reorderChildren(stakeholder, topics);
return this.getFullStakeholder(this.dao.save(stakeholder));
} else {
@ -277,7 +273,6 @@ public class StakeholderService {
Umbrella<String> umbrella = stakeholder.getUmbrellaOptional().orElseThrow(() -> new NotFoundException("Umbrella not found in the stakeholder with id " + id));
if (umbrella.addType(type)) {
stakeholder.setUmbrella(umbrella);
stakeholder.setUpdateDate(new Date());
return this.getFullStakeholder(this.dao.save(stakeholder)).getUmbrella();
}
throw new BadRequestException("Cannot add type: " + type + " to stakeholder with id " + id);
@ -288,7 +283,6 @@ public class StakeholderService {
Umbrella<String> umbrella = stakeholder.getUmbrellaOptional().orElseThrow(() -> new NotFoundException("Umbrella not found in the stakeholder with id " + id));
if (umbrella.removeType(type)) {
stakeholder.setUmbrella(umbrella);
stakeholder.setUpdateDate(new Date());
return this.getFullStakeholder(this.dao.save(stakeholder)).getUmbrella();
}
throw new BadRequestException("Cannot add type: " + type + " to stakeholder with id " + id);
@ -300,7 +294,6 @@ public class StakeholderService {
Stakeholder child = this.findByPath(childId);
if (child.getType().equals(type.name()) && umbrella.addChild(type, childId)) {
stakeholder.setUmbrella(umbrella);
stakeholder.setUpdateDate(new Date());
return this.getFullStakeholder(this.dao.save(stakeholder)).getUmbrella();
}
throw new BadRequestException("Cannot add child: " + childId + " to stakeholder with id " + id);
@ -312,7 +305,6 @@ public class StakeholderService {
Stakeholder child = this.findByPath(childId);
if (child.getType().equals(type.name()) && umbrella.removeChild(type, childId)) {
stakeholder.setUmbrella(umbrella);
stakeholder.setUpdateDate(new Date());
return this.getFullStakeholder(this.dao.save(stakeholder)).getUmbrella();
}
throw new BadRequestException("Cannot remove child: " + childId + " to stakeholder with id " + id);
@ -323,7 +315,6 @@ public class StakeholderService {
Umbrella<String> umbrella = stakeholder.getUmbrellaOptional().orElseThrow(() -> new NotFoundException("Umbrella not found in the stakeholder with id " + id));
if (stakeholder.getUmbrella().update(types)) {
stakeholder.setUmbrella(umbrella);
stakeholder.setUpdateDate(new Date());
return this.getFullStakeholder(this.dao.save(stakeholder)).getUmbrella();
}
throw new BadRequestException("Cannot update types in umbrella of stakeholder with id " + id);
@ -334,7 +325,6 @@ public class StakeholderService {
Umbrella<String> umbrella = stakeholder.getUmbrellaOptional().orElseThrow(() -> new NotFoundException("Umbrella not found in the stakeholder with id " + id));
if (stakeholder.getUmbrella().update(type, children)) {
stakeholder.setUmbrella(umbrella);
stakeholder.setUpdateDate(new Date());
return this.getFullStakeholder(this.dao.save(stakeholder)).getUmbrella();
}
throw new BadRequestException("Cannot update children of " + type + " in umbrella of stakeholder with id " + id);

View File

@ -94,10 +94,7 @@ public class SubCategoryService {
SubCategory old = this.find(subCategory.getId());
subCategory.setNumbers(old.getNumbers());
subCategory.setCharts(old.getCharts());
} else {
subCategory.setCreationDate(new Date());
}
subCategory.setUpdateDate(new Date());
subCategory.getNumbers().forEach(this.sectionService::find);
subCategory.getCharts().forEach(this.sectionService::find);
return this.dao.save(subCategory);
@ -185,7 +182,6 @@ public class SubCategoryService {
numbers.forEach(this.sectionService::find);
if (subCategory.getNumbers().size() == numbers.size() && new HashSet<>(subCategory.getNumbers()).containsAll(numbers)) {
subCategory.setNumbers(numbers);
subCategory.setUpdateDate(new Date());
this.reorderChildrenNumbers(stakeholder, subCategory, numbers);
return this.getFullSubCategory(stakeholder, this.dao.save(subCategory));
} else {
@ -259,14 +255,12 @@ public class SubCategoryService {
public void addSubCategory(Category category, String id) {
category.addSubCategory(id);
category.setUpdateDate(new Date());
this.categoryDAO.save(category);
}
public void removeSubCategory(String id) {
this.categoryDAO.findBySubCategory(id).forEach(category -> {
category.removeSubCategory(id);
category.setUpdateDate(new Date());
this.categoryDAO.save(category);
});
}
@ -274,18 +268,7 @@ public class SubCategoryService {
public SubCategoryFull changeVisibility(Stakeholder stakeholder, SubCategoryFull subCategory, Visibility visibility, Boolean propagate) {
if(this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) {
Stakeholder saved;
if (!subCategory.getVisibility().equals(visibility)) {
subCategory.setVisibility(visibility);
if(stakeholder.isReference()) {
stakeholder.setOverride(subCategory.getId(), "visibility", visibility);
saved = stakeholderDAO.save(stakeholder);
} else {
saved = stakeholder;
}
} else {
saved = stakeholder;
}
Stakeholder saved = this.commonService.changeVisibility(stakeholder, subCategory, visibility, stakeholderDAO);
if(propagate) {
subCategory.setNumbers(subCategory.getNumbers().stream()
.map(section -> this.sectionService.changeVisibility(saved, section, visibility))

View File

@ -88,10 +88,7 @@ public class TopicService {
public Topic save(Topic topic) {
if (topic.getId() != null) {
topic.setCategories(this.find(topic.getId()).getCategories());
} else {
topic.setCreationDate(new Date());
}
topic.setUpdateDate(new Date());
topic.getCategories().forEach(this.categoryService::find);
return this.dao.save(topic);
}
@ -135,7 +132,6 @@ public class TopicService {
if (topic.getCategories().size() == categories.size() && new HashSet<>(topic.getCategories()).containsAll(categories)) {
topic.setCategories(categories);
this.reorderChildren(stakeholder, topic, categories);
topic.setUpdateDate(new Date());
return this.getFullTopic(stakeholder, this.dao.save(topic));
} else {
throw new NotFoundException("Some categories dont exist in the topic with id " + topic.getId());
@ -179,14 +175,12 @@ public class TopicService {
public void addTopic(Stakeholder stakeholder, String id) {
stakeholder.addTopic(id);
stakeholder.setUpdateDate(new Date());
this.stakeholderDAO.save(stakeholder);
}
public void removeTopic(String id) {
this.stakeholderDAO.findByTopic(id).forEach(stakeholder -> {
stakeholder.removeTopic(id);
stakeholder.setUpdateDate(new Date());
this.stakeholderDAO.save(stakeholder);
});
}
@ -194,18 +188,7 @@ public class TopicService {
public TopicFull changeVisibility(Stakeholder stakeholder, TopicFull topic, Visibility visibility, Boolean propagate) {
if (this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) {
Stakeholder saved;
if (!topic.getVisibility().equals(visibility)) {
topic.setVisibility(visibility);
if(stakeholder.isReference()) {
stakeholder.setOverride(topic.getId(), "visibility", visibility);
saved = stakeholderDAO.save(stakeholder);
} else {
saved = stakeholder;
}
} else {
saved = stakeholder;
}
Stakeholder saved = this.commonService.changeVisibility(stakeholder, topic, visibility, stakeholderDAO);
if (propagate) {
topic.setCategories(topic.getCategories().stream()
.map(category -> this.categoryService.changeVisibility(saved, category, visibility, true))