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;
|
|
|
|
import eu.dnetlib.uoamonitorservice.dao.CategoryDAO;
|
2024-03-08 19:55:51 +01:00
|
|
|
import eu.dnetlib.uoamonitorservice.dao.SubCategoryDAO;
|
2024-03-12 23:20:55 +01:00
|
|
|
import eu.dnetlib.uoamonitorservice.dto.CategoryFull;
|
2024-03-08 19:55:51 +01:00
|
|
|
import eu.dnetlib.uoamonitorservice.dto.SubCategoryFull;
|
|
|
|
import eu.dnetlib.uoamonitorservice.dto.TopicFull;
|
|
|
|
import eu.dnetlib.uoamonitorservice.entities.Category;
|
|
|
|
import eu.dnetlib.uoamonitorservice.entities.SubCategory;
|
|
|
|
import eu.dnetlib.uoamonitorservice.entities.Topic;
|
2024-03-12 23:20:55 +01:00
|
|
|
import eu.dnetlib.uoamonitorservice.generics.Common;
|
|
|
|
import eu.dnetlib.uoamonitorservice.generics.SectionGeneric;
|
|
|
|
import eu.dnetlib.uoamonitorservice.generics.SubCategoryGeneric;
|
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.IndicatorType;
|
|
|
|
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;
|
|
|
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
@Service
|
|
|
|
public class SubCategoryService {
|
|
|
|
|
2024-03-12 23:20:55 +01:00
|
|
|
private final CategoryDAO categoryDAO;
|
2024-03-08 19:55:51 +01:00
|
|
|
private final SubCategoryDAO dao;
|
|
|
|
|
|
|
|
private final CommonService commonService;
|
|
|
|
private final SectionService sectionService;
|
|
|
|
|
|
|
|
@Autowired
|
2024-03-12 23:20:55 +01:00
|
|
|
public SubCategoryService(CategoryDAO categoryDAO, SubCategoryDAO dao,CommonService commonService, SectionService sectionService) {
|
|
|
|
this.categoryDAO = categoryDAO;
|
2024-03-08 19:55:51 +01:00
|
|
|
this.dao = dao;
|
2024-03-12 23:20:55 +01:00
|
|
|
this.commonService = commonService;
|
2024-03-08 19:55:51 +01:00
|
|
|
this.sectionService = sectionService;
|
|
|
|
}
|
|
|
|
|
2024-03-14 18:01:28 +01:00
|
|
|
public SubCategory findByPath(Category category, String subcategoryId) {
|
2024-03-08 19:55:51 +01:00
|
|
|
if (!category.getSubCategories().contains(subcategoryId)) {
|
2024-03-14 18:01:28 +01:00
|
|
|
throw new PathNotValidException("SubCategory with id: " + subcategoryId + " not found in Category: " + category.getId());
|
2024-03-08 19:55:51 +01:00
|
|
|
}
|
2024-03-14 18:01:28 +01:00
|
|
|
return this.dao.findById(subcategoryId).orElseThrow(() -> new EntityNotFoundException("SubCategory with id: " + subcategoryId + " not found"));
|
2024-03-08 19:55:51 +01:00
|
|
|
}
|
|
|
|
|
2024-03-14 18:01:28 +01:00
|
|
|
public SubCategoryFull getFullSubCategory(String type, String alias, SubCategory subCategory) {
|
2024-03-08 19:55:51 +01:00
|
|
|
if(commonService.hasVisibilityAuthority(type, alias, subCategory)) {
|
|
|
|
return new SubCategoryFull(subCategory, subCategory.getNumbers().stream()
|
|
|
|
.map(sectionId -> this.sectionService.getFullSection(type, alias, sectionId))
|
|
|
|
.collect(Collectors.toList()),
|
|
|
|
subCategory.getCharts().stream()
|
|
|
|
.map(sectionId -> this.sectionService.getFullSection(type, alias, sectionId))
|
|
|
|
.collect(Collectors.toList()));
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-14 18:01:28 +01:00
|
|
|
public SubCategoryFull getFullSubCategory(String type, String alias, String subCategoryId) {
|
|
|
|
SubCategory subCategory = this.find(subCategoryId);
|
|
|
|
return this.getFullSubCategory(type, alias, subCategory);
|
|
|
|
}
|
|
|
|
|
2024-03-08 19:55:51 +01:00
|
|
|
public SubCategory find(String id) {
|
|
|
|
return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("SubCategory with id: " + id + " not found"));
|
|
|
|
}
|
2024-03-12 23:20:55 +01:00
|
|
|
|
|
|
|
public SubCategoryFull buildSubcategory(SubCategoryFull subCategoryFull) {
|
|
|
|
subCategoryFull.setNumbers(subCategoryFull.getNumbers().stream().map(this.sectionService::buildSection).collect(Collectors.toList()));
|
|
|
|
subCategoryFull.setCharts(subCategoryFull.getCharts().stream().map(this.sectionService::buildSection).collect(Collectors.toList()));
|
|
|
|
subCategoryFull.update(this.save(new SubCategory(subCategoryFull)));
|
|
|
|
return subCategoryFull;
|
|
|
|
}
|
|
|
|
|
|
|
|
public SubCategory save(SubCategory subCategory) {
|
|
|
|
subCategory.getNumbers().forEach(this.sectionService::find);
|
|
|
|
subCategory.getCharts().forEach(this.sectionService::find);
|
|
|
|
return this.dao.save(subCategory);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void delete(String type, SubCategory subCategory, boolean remove ) {
|
|
|
|
if(this.commonService.hasDeleteAuthority(type)) {
|
|
|
|
this.dao.findByDefaultId(subCategory.getId()).forEach(child -> {
|
|
|
|
this.delete(type, child.getId(), remove);
|
|
|
|
});
|
|
|
|
|
|
|
|
subCategory.getNumbers().forEach(sectionId -> {
|
|
|
|
this.sectionService.delete(type, sectionId, false);
|
|
|
|
});
|
|
|
|
subCategory.getCharts().forEach(sectionId -> {
|
|
|
|
this.sectionService.delete(type, sectionId, false);
|
|
|
|
});
|
|
|
|
if (remove) {
|
|
|
|
this.removeSubCategory(subCategory.getId());
|
|
|
|
}
|
|
|
|
this.dao.delete(subCategory);
|
|
|
|
} else {
|
|
|
|
throw new ForbiddenException("Delete subCategory: You are not authorized to delete subCategory with id: " + subCategory.getId());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void delete(String type, String id, boolean remove) {
|
|
|
|
SubCategory subCategory = this.find(id);
|
|
|
|
this.delete(type, subCategory, remove);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void removeSubCategory(String id) {
|
|
|
|
this.categoryDAO.findBySubCategoriesContaining(id).forEach(category -> {
|
|
|
|
category.getSubCategories().remove(id);
|
|
|
|
this.categoryDAO.save(category);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-03-14 18:01:28 +01:00
|
|
|
public SubCategoryFull changeVisibility(String type, String alias, SubCategoryFull subCategory, Visibility visibility, Boolean propagate) {
|
2024-03-12 23:20:55 +01:00
|
|
|
if(this.commonService.hasEditAuthority(type, alias)) {
|
2024-03-14 18:01:28 +01:00
|
|
|
subCategory.setVisibility(visibility);
|
2024-03-12 23:20:55 +01:00
|
|
|
if(propagate) {
|
2024-03-14 18:01:28 +01:00
|
|
|
subCategory.setNumbers(subCategory.getNumbers().stream()
|
|
|
|
.map(section -> this.sectionService.changeVisibility(type, alias, section, visibility))
|
2024-03-12 23:20:55 +01:00
|
|
|
.collect(Collectors.toList()));
|
2024-03-14 18:01:28 +01:00
|
|
|
subCategory.setCharts(subCategory.getCharts().stream()
|
|
|
|
.map(section -> this.sectionService.changeVisibility(type, alias, section, visibility))
|
2024-03-12 23:20:55 +01:00
|
|
|
.collect(Collectors.toList()));
|
|
|
|
}
|
2024-03-14 18:01:28 +01:00
|
|
|
subCategory.update(this.save(new SubCategory(subCategory)));
|
|
|
|
return subCategory;
|
2024-03-12 23:20:55 +01:00
|
|
|
} else {
|
2024-03-14 18:01:28 +01:00
|
|
|
throw new ForbiddenException("Change subCategory visibility: You are not authorized to update subCategory with id: " + subCategory.getId());
|
2024-03-12 23:20:55 +01:00
|
|
|
}
|
|
|
|
}
|
2024-03-14 18:01:28 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2024-03-08 19:55:51 +01:00
|
|
|
}
|