[new-model]: Reorder for move action for all entities. Doesn't work for added and removed, need a different implementation.

This commit is contained in:
Konstantinos Triantafyllou 2024-04-02 22:26:57 +03:00
parent c83dc441d4
commit eee0814016
8 changed files with 148 additions and 34 deletions

View File

@ -10,8 +10,7 @@ import java.util.Optional;
@Repository
public interface SubCategoryDAO extends MongoRepository<SubCategory, String> {
List<SubCategory> findAll();
List<SubCategory> findByDefaultId(String DefaultId);
List<SubCategory> findByDefaultId(String defaultId);
List<SubCategory> findByNumbersContaining(String id);
List<SubCategory> findByChartsContaining(String id);

View File

@ -7,6 +7,15 @@ public class ReorderEvent {
private String target;
private List<String> ids;
public ReorderEvent() {
}
public ReorderEvent(String action, String target, List<String> ids) {
this.action = action;
this.target = target;
this.ids = ids;
}
public String getAction() {
return action;
}

View File

@ -9,6 +9,7 @@ import eu.dnetlib.uoamonitorservice.dto.TopicFull;
import eu.dnetlib.uoamonitorservice.entities.Category;
import eu.dnetlib.uoamonitorservice.entities.Stakeholder;
import eu.dnetlib.uoamonitorservice.entities.Topic;
import eu.dnetlib.uoamonitorservice.generics.Common;
import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException;
import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException;
import eu.dnetlib.uoamonitorservice.primitives.Visibility;
@ -86,7 +87,7 @@ public class CategoryService {
public CategoryFull save(Stakeholder stakeholder, Topic topic, Category category, boolean createOverview) {
if (category.getId() != null) {
if (this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) {
category.setSubCategories(this.find(topic.getId()).getSubCategories());
category.setSubCategories(this.find(category.getId()).getSubCategories());
this.updateChildren(category);
category = this.save(category);
} else {
@ -126,6 +127,7 @@ public class CategoryService {
subcategories.forEach(this.subCategoryService::find);
if (category.getSubCategories().size() == subcategories.size() && new HashSet<>(category.getSubCategories()).containsAll(subcategories)) {
category.setSubCategories(subcategories);
this.reorderChildren(stakeholder, category, 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());
@ -135,6 +137,15 @@ public class CategoryService {
}
}
public void reorderChildren(Stakeholder defaultStakeholder, Category defaultCategory, List<String> defaultSubCategories) {
this.stakeholderDAO.findByDefaultId(defaultStakeholder.getId()).forEach(stakeholder -> {
this.dao.findByDefaultId(defaultCategory.getId()).stream().map(category -> this.getFullCategory(stakeholder.getType(), stakeholder.getAlias(), category)).forEach(category -> {
this.reorderSubCategories(stakeholder, new Category(category),
this.commonService.reorder(defaultSubCategories, category.getSubCategories().stream().map(subCategory -> (Common) subCategory).collect(Collectors.toList())));
});
});
}
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,18 @@
package eu.dnetlib.uoamonitorservice.service;
import eu.dnetlib.uoaauthorizationlibrary.security.AuthorizationService;
import eu.dnetlib.uoamonitorservice.dto.TopicFull;
import eu.dnetlib.uoamonitorservice.generics.Common;
import eu.dnetlib.uoamonitorservice.primitives.Visibility;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service
public class CommonService {
@ -46,4 +53,20 @@ public class CommonService {
return common.getVisibility() == Visibility.PUBLIC;
}
}
public List<String> reorder(List<String> defaultIds, List<Common> commons) {
Map<String, Integer> map = new HashMap<>();
for(int i = 0; i < commons.size(); i++) {
if (!defaultIds.contains(commons.get(i).getDefaultId())) {
map.put(commons.get(i).getId(), i);
}
}
commons = commons.stream().filter(common -> defaultIds.contains(common.getDefaultId())).collect(Collectors.toList());;
commons.sort(Comparator.comparingInt(common -> defaultIds.indexOf(common.getDefaultId())));
List<String> ids = commons.stream().map(Common::getId).collect(Collectors.toList());
map.keySet().forEach(key -> {
ids.add(map.get(key), key);
});
return ids;
}
}

View File

@ -3,9 +3,8 @@ 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.Section;
import eu.dnetlib.uoamonitorservice.entities.Stakeholder;
import eu.dnetlib.uoamonitorservice.entities.SubCategory;
import eu.dnetlib.uoamonitorservice.entities.*;
import eu.dnetlib.uoamonitorservice.generics.Common;
import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException;
import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException;
import eu.dnetlib.uoamonitorservice.primitives.ReorderEvent;
@ -30,7 +29,7 @@ public class SectionService {
private final CommonService commonService;
@Autowired
public SectionService(StakeholderDAO stakeholderDAO, TopicDAO topicDAO, CategoryDAO categoryDAO,SubCategoryDAO subCategoryDAO, SectionDAO dao, IndicatorService indicatorService, CommonService commonService) {
public SectionService(StakeholderDAO stakeholderDAO, TopicDAO topicDAO, CategoryDAO categoryDAO, SubCategoryDAO subCategoryDAO, SectionDAO dao, IndicatorService indicatorService, CommonService commonService) {
this.stakeholderDAO = stakeholderDAO;
this.topicDAO = topicDAO;
this.categoryDAO = categoryDAO;
@ -69,7 +68,7 @@ public class SectionService {
}
public Section save(Section section) {
if(section.getId() != null) {
if (section.getId() != null) {
section.setIndicators(this.find(section.getId()).getIndicators());
}
section.getIndicators().forEach(this.indicatorService::find);
@ -117,7 +116,7 @@ public class SectionService {
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());
this.indicatorService.createChildren(section, this.indicatorService.find(indicator));
});
});
});
@ -131,16 +130,19 @@ public class SectionService {
});
}
public SectionFull reorderIndicators(Stakeholder stakeholder, Section section, ReorderEvent reorderEvent) {
public SectionFull reorderIndicators(Stakeholder stakeholder, Section section, ReorderEvent event) {
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());
event.getIds().forEach(this.indicatorService::find);
if (event.getAction().equals("added")) {
section.addIndicator(event.getTarget());
} else if (event.getAction().equals("removed")) {
section.removeIndicator(event.getTarget());
}
if (section.getIndicators().size() == reorderEvent.getIds().size() && new HashSet<>(section.getIndicators()).containsAll(reorderEvent.getIds())) {
section.setIndicators(reorderEvent.getIds());
if (section.getIndicators().size() == event.getIds().size() && new HashSet<>(section.getIndicators()).containsAll(event.getIds())) {
section.setIndicators(event.getIds());
if(event.getAction().equals("moved")) {
this.reorderChildren(stakeholder, section, event);
}
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());
@ -150,8 +152,36 @@ public class SectionService {
}
}
public void reorderChildren(Stakeholder defaultStakeholder, Section defaultSection, ReorderEvent event) {
this.stakeholderDAO.findByDefaultId(defaultStakeholder.getId()).forEach(stakeholder -> {
this.dao.findByDefaultId(defaultSection.getId()).stream().map(section -> this.getFullSection(stakeholder.getType(), stakeholder.getAlias(), section)).forEach(section -> {
Indicator target;
if(section.isNumber()) {
target = this.subCategoryDAO.findByNumbersContaining(section.getId()).stream().flatMap(subCategory -> subCategory.getNumbers().stream())
.map(id -> this.getFullSection(stakeholder.getType(), stakeholder.getAlias(), id)).flatMap(sectionFull -> section.getIndicators().stream())
.filter(indicator -> indicator.getDefaultId().equals(event.getTarget())).findFirst().orElse(null);
} else {
target = this.subCategoryDAO.findByChartsContaining(section.getId()).stream().flatMap(subCategory -> subCategory.getCharts().stream())
.map(id -> this.getFullSection(stakeholder.getType(), stakeholder.getAlias(), id)).flatMap(sectionFull -> section.getIndicators().stream())
.filter(indicator -> indicator.getDefaultId().equals(event.getTarget())).findFirst().orElse(null);
}
if (target != null) {
List<Common> indicators = section.getIndicators().stream().map(indicator -> (Common) indicator).collect(Collectors.toList());
if(event.getAction().equals("removed")) {
indicators.removeIf(indicator -> indicator.getId().equals(target.getId()));
} else if(event.getAction().equals("added")) {
indicators.add(target);
}
List<String> ids = this.commonService.reorder(event.getIds(), indicators);
ReorderEvent childEvent = new ReorderEvent(event.getAction(), target.getId(), ids);
this.reorderIndicators(stakeholder, new Section(section), childEvent);
}
});
});
}
public void delete(String type, Section section, boolean remove) {
if(this.commonService.hasDeleteAuthority(type)) {
if (this.commonService.hasDeleteAuthority(type)) {
this.dao.findByDefaultId(section.getId()).forEach(child -> {
this.delete(type, child.getId(), remove);
});
@ -177,7 +207,7 @@ public class SectionService {
}
public void addSection(SubCategory subCategory, String id, int index) {
if(this.find(id).isNumber()) {
if (this.find(id).isNumber()) {
subCategory.addNumber(id, index);
} else {
subCategory.addChart(id, index);
@ -197,7 +227,7 @@ public class SectionService {
}
public SectionFull changeVisibility(String type, String alias, SectionFull section, Visibility visibility) {
if(this.commonService.hasEditAuthority(type, alias)) {
if (this.commonService.hasEditAuthority(type, alias)) {
section.setIndicators(section.getIndicators().stream()
.map(indicator -> this.indicatorService.changeVisibility(type, alias, indicator, visibility))
.collect(Collectors.toList()));

View File

@ -4,6 +4,7 @@ import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException;
import eu.dnetlib.uoamonitorservice.dao.StakeholderDAO;
import eu.dnetlib.uoamonitorservice.dto.StakeholderFull;
import eu.dnetlib.uoamonitorservice.entities.Stakeholder;
import eu.dnetlib.uoamonitorservice.generics.Common;
import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException;
import eu.dnetlib.uoamonitorservice.primitives.Visibility;
import org.springframework.beans.factory.annotation.Autowired;
@ -36,7 +37,7 @@ public class StakeholderService {
return this.dao.findByAlias(alias).orElseThrow(() -> new EntityNotFoundException("Stakeholder with alias: " + alias + " not found"));
}
public List<Stakeholder> findByDefaultId(String id ){
public List<Stakeholder> findByDefaultId(String id) {
return this.dao.findByDefaultId(id);
}
@ -49,37 +50,37 @@ public class StakeholderService {
}
public List<Stakeholder> getAll(String type) {
if(type != null) {
if (type != null) {
return this.dao.findByType(type);
}
return this.dao.findAll();
}
public List<Stakeholder> getAllDefaultByRole(String type) {
return (type == null?this.dao.findByDefaultId(null):this.dao.findByDefaultIdAndType(null, type)).stream()
return (type == null ? this.dao.findByDefaultId(null) : this.dao.findByDefaultIdAndType(null, type)).stream()
.filter(stakeholder -> this.commonService.hasAccessAuthority(stakeholder.getType(), stakeholder.getAlias(), true))
.collect(Collectors.toList());
}
public List<Stakeholder> getStakeholdersByTypeAndRole(String type, String defaultId, boolean manage) {
List<Stakeholder> stakeholders;
if(type != null && defaultId != null) {
if (type != null && defaultId != null) {
stakeholders = dao.findByDefaultIdAndType(defaultId, type);
} else if(defaultId != null) {
} else if (defaultId != null) {
stakeholders = dao.findByDefaultId(defaultId);
} else if(type != null) {
} else if (type != null) {
stakeholders = dao.findByDefaultIdNotAndType(null, type);
} else {
stakeholders = dao.findByDefaultIdNot(null);
}
return stakeholders.stream().filter(stakeholder ->
(!manage && (stakeholder.getVisibility() == Visibility.PUBLIC || stakeholder.getVisibility() == Visibility.RESTRICTED))
|| this.commonService.hasAccessAuthority(stakeholder.getType(), stakeholder.getAlias(), false))
(!manage && (stakeholder.getVisibility() == Visibility.PUBLIC || stakeholder.getVisibility() == Visibility.RESTRICTED))
|| this.commonService.hasAccessAuthority(stakeholder.getType(), stakeholder.getAlias(), false))
.collect(Collectors.toList());
}
public StakeholderFull getFullStakeholder(Stakeholder stakeholder) {
if(this.commonService.hasVisibilityAuthority(stakeholder.getType(), stakeholder.getAlias(), stakeholder)) {
if (this.commonService.hasVisibilityAuthority(stakeholder.getType(), stakeholder.getAlias(), stakeholder)) {
return new StakeholderFull(stakeholder,
stakeholder.getTopics().stream()
.map(topicId -> topicService.getFullTopic(stakeholder.getType(), stakeholder.getAlias(), topicId))
@ -96,7 +97,7 @@ public class StakeholderService {
}
public Stakeholder save(Stakeholder stakeholder) {
if(stakeholder.getId() != null) {
if (stakeholder.getId() != null) {
stakeholder.setTopics(this.find(stakeholder.getId()).getTopics());
}
stakeholder.getTopics().forEach(this.topicService::find);
@ -104,10 +105,11 @@ public class StakeholderService {
}
public StakeholderFull reorderTopics(Stakeholder stakeholder, List<String> topics) {
if(this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) {
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);
this.reorderChildren(stakeholder, topics);
return this.getFullStakeholder(this.dao.save(stakeholder));
} else {
throw new EntityNotFoundException("Some topics dont exist in the stakeholder with id " + stakeholder.getId());
@ -117,9 +119,16 @@ public class StakeholderService {
}
}
public void reorderChildren(Stakeholder defaultStakeholder, List<String> defaultTopics) {
this.dao.findByDefaultId(defaultStakeholder.getId()).stream().map(this::getFullStakeholder).forEach(stakeholder -> {
this.reorderTopics(new Stakeholder(stakeholder),
this.commonService.reorder(defaultTopics, stakeholder.getTopics().stream().map(topic -> (Common) topic).collect(Collectors.toList())));
});
}
public String delete(String id) {
Stakeholder stakeholder = this.find(id);
if(this.commonService.hasDeleteAuthority(stakeholder.getType())) {
if (this.commonService.hasDeleteAuthority(stakeholder.getType())) {
this.dao.findByDefaultId(stakeholder.getId()).forEach(child -> {
this.delete(child.getId());
});
@ -134,8 +143,8 @@ public class StakeholderService {
}
public StakeholderFull changeVisibility(StakeholderFull stakeholder, Visibility visibility, Boolean propagate) {
if(this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) {
if(propagate) {
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))
.collect(Collectors.toList()));

View File

@ -9,8 +9,10 @@ import eu.dnetlib.uoamonitorservice.dto.SubCategoryFull;
import eu.dnetlib.uoamonitorservice.entities.Category;
import eu.dnetlib.uoamonitorservice.entities.Stakeholder;
import eu.dnetlib.uoamonitorservice.entities.SubCategory;
import eu.dnetlib.uoamonitorservice.generics.Common;
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;
@ -131,6 +133,7 @@ public class SubCategoryService {
numbers.forEach(this.sectionService::find);
if (subCategory.getNumbers().size() == numbers.size() && new HashSet<>(subCategory.getNumbers()).containsAll(numbers)) {
subCategory.setNumbers(numbers);
this.reorderChildrenNumbers(stakeholder, subCategory, 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());
@ -145,6 +148,7 @@ public class SubCategoryService {
charts.forEach(this.sectionService::find);
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));
} else {
throw new EntityNotFoundException("Some sections dont exist in the subCategory with id " + subCategory.getId());
@ -154,6 +158,24 @@ public class SubCategoryService {
}
}
public void reorderChildrenNumbers(Stakeholder defaultStakeholder, SubCategory defaultSubCategory, List<String> defaultSections) {
this.stakeholderDAO.findByDefaultId(defaultStakeholder.getId()).forEach(stakeholder -> {
this.dao.findByDefaultId(defaultSubCategory.getId()).stream().map(subCategory -> this.getFullSubCategory(stakeholder.getType(), stakeholder.getAlias(), subCategory)).forEach(subCategory -> {
this.reorderNumbers(stakeholder, new SubCategory(subCategory),
this.commonService.reorder(defaultSections, subCategory.getNumbers().stream().map(section -> (Common) section).collect(Collectors.toList())));
});
});
}
public void reorderChildrenCharts(Stakeholder defaultStakeholder, SubCategory defaultSubCategory, List<String> defaultSections) {
this.stakeholderDAO.findByDefaultId(defaultStakeholder.getId()).forEach(stakeholder -> {
this.dao.findByDefaultId(defaultSubCategory.getId()).stream().map(subCategory -> this.getFullSubCategory(stakeholder.getType(), stakeholder.getAlias(), subCategory)).forEach(subCategory -> {
this.reorderCharts(stakeholder, new SubCategory(subCategory),
this.commonService.reorder(defaultSections, subCategory.getCharts().stream().map(section -> (Common) section).collect(Collectors.toList())));
});
});
}
public void delete(String type, SubCategory subCategory, boolean remove) {
if(this.commonService.hasDeleteAuthority(type)) {
this.dao.findByDefaultId(subCategory.getId()).forEach(child -> {

View File

@ -6,6 +6,7 @@ import eu.dnetlib.uoamonitorservice.dao.TopicDAO;
import eu.dnetlib.uoamonitorservice.dto.TopicFull;
import eu.dnetlib.uoamonitorservice.entities.Stakeholder;
import eu.dnetlib.uoamonitorservice.entities.Topic;
import eu.dnetlib.uoamonitorservice.generics.Common;
import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException;
import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException;
import eu.dnetlib.uoamonitorservice.primitives.Visibility;
@ -111,6 +112,7 @@ public class TopicService {
categories.forEach(this.categoryService::find);
if (topic.getCategories().size() == categories.size() && new HashSet<>(topic.getCategories()).containsAll(categories)) {
topic.setCategories(categories);
this.reorderChildren(stakeholder, topic, 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());
@ -120,6 +122,15 @@ public class TopicService {
}
}
public void reorderChildren(Stakeholder defaultStakeholder, Topic defaultTopic, List<String> defaultCategories) {
this.stakeholderDAO.findByDefaultId(defaultStakeholder.getId()).forEach(stakeholder -> {
this.dao.findByDefaultId(defaultTopic.getId()).stream().map(topic -> this.getFullTopic(stakeholder.getType(), stakeholder.getAlias(), topic)).forEach(topic -> {
this.reorderCategories(stakeholder, new Topic(topic),
this.commonService.reorder(defaultCategories, topic.getCategories().stream().map(category -> (Common) category).collect(Collectors.toList())));
});
});
}
public void delete(String type, Topic topic, boolean remove) {
if (this.commonService.hasDeleteAuthority(type)) {
this.dao.findByDefaultId(topic.getId()).forEach(child -> {