2024-03-08 19:55:51 +01:00
|
|
|
package eu.dnetlib.uoamonitorservice.service;
|
|
|
|
|
2024-03-12 23:20:55 +01:00
|
|
|
import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException;
|
2024-03-08 19:55:51 +01:00
|
|
|
import eu.dnetlib.uoamonitorservice.dao.CategoryDAO;
|
2024-03-27 13:07:41 +01:00
|
|
|
import eu.dnetlib.uoamonitorservice.dao.StakeholderDAO;
|
2024-03-12 23:20:55 +01:00
|
|
|
import eu.dnetlib.uoamonitorservice.dao.TopicDAO;
|
2024-03-08 19:55:51 +01:00
|
|
|
import eu.dnetlib.uoamonitorservice.dto.CategoryFull;
|
|
|
|
import eu.dnetlib.uoamonitorservice.entities.Category;
|
2024-03-27 13:07:41 +01:00
|
|
|
import eu.dnetlib.uoamonitorservice.entities.Stakeholder;
|
2024-03-08 19:55:51 +01:00
|
|
|
import eu.dnetlib.uoamonitorservice.entities.Topic;
|
2024-04-02 21:26:57 +02:00
|
|
|
import eu.dnetlib.uoamonitorservice.generics.Common;
|
2024-03-08 19:55:51 +01:00
|
|
|
import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException;
|
|
|
|
import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException;
|
2024-03-12 23:20:55 +01:00
|
|
|
import eu.dnetlib.uoamonitorservice.primitives.Visibility;
|
2024-03-08 19:55:51 +01:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
2024-04-06 11:40:52 +02:00
|
|
|
import java.util.Date;
|
2024-04-02 13:49:02 +02:00
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.List;
|
2024-03-08 19:55:51 +01:00
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
@Service
|
|
|
|
public class CategoryService {
|
|
|
|
|
2024-03-27 13:07:41 +01:00
|
|
|
private final StakeholderDAO stakeholderDAO;
|
2024-03-12 23:20:55 +01:00
|
|
|
private final TopicDAO topicDAO;
|
2024-03-08 19:55:51 +01:00
|
|
|
private final CategoryDAO dao;
|
|
|
|
private final SubCategoryService subCategoryService;
|
|
|
|
private final CommonService commonService;
|
|
|
|
|
|
|
|
@Autowired
|
2024-03-27 13:07:41 +01:00
|
|
|
public CategoryService(StakeholderDAO stakeholderDAO, TopicDAO topicDAO, CategoryDAO dao, SubCategoryService subCategoryService, CommonService commonService) {
|
|
|
|
this.stakeholderDAO = stakeholderDAO;
|
2024-03-12 23:20:55 +01:00
|
|
|
this.topicDAO = topicDAO;
|
|
|
|
this.dao = dao;
|
2024-03-08 19:55:51 +01:00
|
|
|
this.subCategoryService = subCategoryService;
|
|
|
|
this.commonService = commonService;
|
|
|
|
}
|
|
|
|
|
2024-03-27 13:07:41 +01:00
|
|
|
public Category find(String id) {
|
|
|
|
return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("Category with id: " + id + " not found"));
|
|
|
|
}
|
|
|
|
|
2024-03-14 18:01:28 +01:00
|
|
|
public Category findByPath(Topic topic, String categoryId) {
|
2024-03-08 19:55:51 +01:00
|
|
|
if (!topic.getCategories().contains(categoryId)) {
|
2024-03-14 18:01:28 +01:00
|
|
|
throw new PathNotValidException("Category with id: " + categoryId + " not found in Topic: " + topic.getId());
|
2024-03-08 19:55:51 +01:00
|
|
|
}
|
2024-03-14 18:01:28 +01:00
|
|
|
return this.dao.findById(categoryId).orElseThrow(() -> new EntityNotFoundException("Category with id: " + categoryId + " not found"));
|
2024-03-08 19:55:51 +01:00
|
|
|
}
|
|
|
|
|
2024-03-14 18:01:28 +01:00
|
|
|
public CategoryFull getFullCategory(String type, String alias, Category category) {
|
|
|
|
if (commonService.hasVisibilityAuthority(type, alias, category)) {
|
|
|
|
return new CategoryFull(category, category.getSubCategories().stream()
|
2024-03-08 19:55:51 +01:00
|
|
|
.map(subCategoryId -> this.subCategoryService.getFullSubCategory(type, alias, subCategoryId))
|
|
|
|
.collect(Collectors.toList()));
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-14 18:01:28 +01:00
|
|
|
public CategoryFull getFullCategory(String type, String alias, String categoryId) {
|
|
|
|
Category category = this.find(categoryId);
|
|
|
|
return this.getFullCategory(type, alias, category);
|
|
|
|
}
|
|
|
|
|
2024-04-12 10:37:28 +02:00
|
|
|
public String build(String id) {
|
|
|
|
Category category = this.find(id);
|
|
|
|
Category copy = category.copy();
|
|
|
|
copy.setSubCategories(category.getSubCategories().stream().map(this.subCategoryService::build).collect(Collectors.toList()));
|
|
|
|
return this.save(copy).getId();
|
|
|
|
}
|
|
|
|
|
2024-03-12 23:20:55 +01:00
|
|
|
|
2024-04-12 10:37:28 +02:00
|
|
|
public String copy(String id) {
|
|
|
|
Category category = this.find(id);
|
|
|
|
Category copy = new Category(category);
|
|
|
|
copy.setId(null);
|
|
|
|
copy.setSubCategories(category.getSubCategories().stream().map(this.subCategoryService::copy).collect(Collectors.toList()));
|
|
|
|
return this.save(copy).getId();
|
2024-03-12 23:20:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public Category save(Category category) {
|
2024-04-02 13:49:02 +02:00
|
|
|
if(category.getId() != null) {
|
|
|
|
category.setSubCategories(this.find(category.getId()).getSubCategories());
|
2024-04-06 11:40:52 +02:00
|
|
|
} else {
|
|
|
|
category.setCreationDate(new Date());
|
2024-04-02 13:49:02 +02:00
|
|
|
}
|
2024-04-06 11:40:52 +02:00
|
|
|
category.setUpdateDate(new Date());
|
2024-03-12 23:20:55 +01:00
|
|
|
category.getSubCategories().forEach(this.subCategoryService::find);
|
|
|
|
return this.dao.save(category);
|
|
|
|
}
|
|
|
|
|
2024-03-27 13:07:41 +01:00
|
|
|
public CategoryFull save(Stakeholder stakeholder, Topic topic, Category category) {
|
|
|
|
return this.save(stakeholder, topic, category, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public CategoryFull save(Stakeholder stakeholder, Topic topic, Category category, boolean createOverview) {
|
|
|
|
if (category.getId() != null) {
|
|
|
|
if (this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) {
|
2024-04-02 21:26:57 +02:00
|
|
|
category.setSubCategories(this.find(category.getId()).getSubCategories());
|
2024-03-27 13:07:41 +01:00
|
|
|
this.updateChildren(category);
|
|
|
|
category = this.save(category);
|
|
|
|
} else {
|
|
|
|
throw new ForbiddenException("You are not authorized to update stakeholder with id: " + stakeholder.getId());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (this.commonService.hasCreateAuthority(stakeholder.getType())) {
|
|
|
|
category = this.save(category);
|
|
|
|
this.createChildren(topic, category);
|
|
|
|
if(createOverview) {
|
|
|
|
this.subCategoryService.save(stakeholder, category, category.createOverview());
|
|
|
|
}
|
|
|
|
this.addCategory(topic, category.getId());
|
|
|
|
} else {
|
|
|
|
throw new ForbiddenException("You are not authorized to create a category in stakeholder with id: " + stakeholder.getId());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this.getFullCategory(stakeholder.getType(), stakeholder.getAlias(), category);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void createChildren(Topic defaultTopic, Category category) {
|
|
|
|
this.topicDAO.findByDefaultId(defaultTopic.getId()).forEach(topic -> {
|
|
|
|
this.stakeholderDAO.findByTopicsContaining(topic.getId()).forEach(stakeholder -> {
|
|
|
|
this.save(stakeholder, topic, category.copy(), false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public void updateChildren(Category category) {
|
|
|
|
this.dao.findByDefaultId(category.getId()).forEach(child -> {
|
|
|
|
this.save(category.override(child, this.find(category.getId())));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-04-02 13:49:02 +02:00
|
|
|
public CategoryFull reorderSubCategories(Stakeholder stakeholder, Category category, List<String> subcategories) {
|
|
|
|
if(this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) {
|
|
|
|
subcategories.forEach(this.subCategoryService::find);
|
|
|
|
if (category.getSubCategories().size() == subcategories.size() && new HashSet<>(category.getSubCategories()).containsAll(subcategories)) {
|
|
|
|
category.setSubCategories(subcategories);
|
2024-04-06 11:40:52 +02:00
|
|
|
category.setUpdateDate(new Date());
|
2024-04-02 21:26:57 +02:00
|
|
|
this.reorderChildren(stakeholder, category, subcategories);
|
2024-04-02 13:49:02 +02:00
|
|
|
return this.getFullCategory(stakeholder.getType(), stakeholder.getAlias(), this.dao.save(category));
|
|
|
|
} else {
|
|
|
|
throw new EntityNotFoundException("Some subCategories dont exist in the category with id " + category.getId());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new ForbiddenException("You are not authorized to reorder subCategories in category with id: " + category.getId());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-02 21:26:57 +02:00
|
|
|
public void reorderChildren(Stakeholder defaultStakeholder, Category defaultCategory, List<String> defaultSubCategories) {
|
2024-04-15 09:42:39 +02:00
|
|
|
this.stakeholderDAO.findByDefaultIdAndCopyTrue(defaultStakeholder.getId()).forEach(stakeholder -> {
|
2024-04-02 21:26:57 +02:00
|
|
|
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())));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-03-12 23:20:55 +01:00
|
|
|
public void delete(String type, Category category, boolean remove) {
|
2024-03-14 18:01:28 +01:00
|
|
|
if (this.commonService.hasDeleteAuthority(type)) {
|
2024-03-12 23:20:55 +01:00
|
|
|
this.dao.findByDefaultId(category.getId()).forEach(child -> {
|
|
|
|
this.delete(type, child.getId(), remove);
|
|
|
|
});
|
|
|
|
category.getSubCategories().forEach(subcategoryId -> {
|
|
|
|
this.subCategoryService.delete(type, subcategoryId, false);
|
|
|
|
});
|
2024-03-14 18:01:28 +01:00
|
|
|
if (remove) {
|
2024-03-12 23:20:55 +01:00
|
|
|
this.removeCategory(category.getId());
|
|
|
|
}
|
|
|
|
this.dao.delete(category);
|
|
|
|
} else {
|
|
|
|
throw new ForbiddenException("Delete category: You are not authorized to delete category with id: " + category.getId());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void delete(String type, String id, boolean remove) {
|
|
|
|
Category category = this.find(id);
|
|
|
|
this.delete(type, category, remove);
|
|
|
|
}
|
|
|
|
|
2024-03-27 13:07:41 +01:00
|
|
|
public void addCategory(Topic topic, String id) {
|
|
|
|
topic.addCategory(id);
|
2024-04-06 11:40:52 +02:00
|
|
|
topic.setUpdateDate(new Date());
|
2024-03-27 13:07:41 +01:00
|
|
|
this.topicDAO.save(topic);
|
|
|
|
}
|
|
|
|
|
2024-03-12 23:20:55 +01:00
|
|
|
public void removeCategory(String id) {
|
|
|
|
this.topicDAO.findByCategoriesContaining(id).forEach(topic -> {
|
2024-03-27 13:07:41 +01:00
|
|
|
topic.removeCategory(id);
|
2024-04-06 11:40:52 +02:00
|
|
|
topic.setUpdateDate(new Date());
|
2024-03-12 23:20:55 +01:00
|
|
|
this.topicDAO.save(topic);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-03-14 18:01:28 +01:00
|
|
|
public CategoryFull changeVisibility(String type, String alias, CategoryFull category, Visibility visibility, Boolean propagate) {
|
|
|
|
if (this.commonService.hasEditAuthority(type, alias)) {
|
|
|
|
category.setVisibility(visibility);
|
|
|
|
if (propagate) {
|
|
|
|
category.setSubCategories(category.getSubCategories().stream()
|
|
|
|
.map(subCategory -> this.subCategoryService.changeVisibility(type, alias, subCategory, visibility, true))
|
2024-03-12 23:20:55 +01:00
|
|
|
.collect(Collectors.toList()));
|
|
|
|
}
|
2024-03-14 18:01:28 +01:00
|
|
|
category.update(this.save(new Category(category)));
|
|
|
|
return category;
|
2024-03-12 23:20:55 +01:00
|
|
|
} else {
|
2024-03-14 18:01:28 +01:00
|
|
|
throw new ForbiddenException("Change category visibility: You are not authorized to update category with id: " + category.getId());
|
2024-03-12 23:20:55 +01:00
|
|
|
}
|
|
|
|
}
|
2024-03-14 18:01:28 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2024-03-08 19:55:51 +01:00
|
|
|
}
|