[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;
@ -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() == event.getIds().size() && new HashSet<>(section.getIndicators()).containsAll(event.getIds())) {
section.setIndicators(event.getIds());
if(event.getAction().equals("moved")) {
this.reorderChildren(stakeholder, section, event);
}
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());
@ -150,6 +152,34 @@ 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)) {
this.dao.findByDefaultId(section.getId()).forEach(child -> {

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;
@ -108,6 +109,7 @@ public class StakeholderService {
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,6 +119,13 @@ 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())) {

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 -> {