Save category, subcategory, section refactor.

This commit is contained in:
Konstantinos Triantafyllou 2024-03-27 14:07:41 +02:00
parent c3cc942332
commit 8a56c3ad8b
21 changed files with 348 additions and 398 deletions

View File

@ -30,17 +30,17 @@ public class CategoryController {
this.categoryService = categoryService;
}
/*@PreAuthorize("isAuthenticated()")
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/save", method = RequestMethod.POST)
public Category<SubCategory> saveCategory(@PathVariable("stakeholderId") String stakeholderId,
public CategoryFull saveCategory(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@RequestBody Category<SubCategory> categoryFull) {
@RequestBody CategoryFull categoryFull) {
log.debug("save category");
log.debug("Alias: " + categoryFull.getAlias() + " - Id: " + categoryFull.getId() + " - Stakeholder: " + stakeholderId + " - Topic: " + topicId);
Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId);
if (stakeholder != null) {
Stakeholder stakeholder = stakeholderService.findByPath(stakeholderId);
Topic topic = topicService.findByPath(stakeholder, topicId);
return this.categoryService.save(stakeholder, topic, new Category(categoryFull));
/*if (stakeholder != null) {
if (!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) {
// EXCEPTION - Access denied
throw new ForbiddenException("Save Category: You are not authorized to update stakeholder with id: " + stakeholderId);
@ -128,10 +128,10 @@ public class CategoryController {
// EXCEPTION - Stakeholder not found
throw new EntityNotFoundException("Save category: Stakeholder with id: " + stakeholderId + " not found");
}
return categoryFull;
}*/
return categoryFull;*/
}
/*public void onSaveDefaultCategory(Category<String> category, String topicId) {
/* public void onSaveDefaultCategory(Category<String> category, String topicId) {
log.debug("On save default category");
List<Topic> topics = topicDAO.findByDefaultId(topicId);
@ -151,33 +151,6 @@ public class CategoryController {
subCategoryController.onSaveDefaultSubCategory(subCategoryOverview, category.getId());
}*/
/*public void onUpdateDefaultCategory(Category category, Category oldCategory) {
log.debug("On update default category");
List<Category> categories = categoryDAO.findByDefaultId(category.getId());
boolean changed = false;
for (Category categoryBasedOnDefault : categories) {
if (category.getName() != null && !category.getName().equals(categoryBasedOnDefault.getName())
&& (oldCategory.getName() == null || oldCategory.getName().equals(categoryBasedOnDefault.getName()))) {
categoryBasedOnDefault.setName(category.getName());
categoryBasedOnDefault.setAlias(category.getAlias());
changed = true;
}
if (category.getDescription() != null && !category.getDescription().equals(categoryBasedOnDefault.getDescription())
&& (oldCategory.getDescription() == null || oldCategory.getDescription().equals(categoryBasedOnDefault.getDescription()))) {
categoryBasedOnDefault.setDescription(category.getDescription());
changed = true;
}
if (!changed) {
continue;
}
categoryBasedOnDefault.setUpdateDate(category.getUpdateDate());
categoryDAO.save(categoryBasedOnDefault);
}
}*/
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/delete", method = RequestMethod.DELETE)

View File

@ -1,5 +1,6 @@
package eu.dnetlib.uoamonitorservice.controllers;
import eu.dnetlib.uoamonitorservice.dto.SectionFull;
import eu.dnetlib.uoamonitorservice.entities.*;
import eu.dnetlib.uoamonitorservice.service.*;
import org.apache.logging.log4j.LogManager;
@ -29,154 +30,23 @@ public class SectionController {
this.sectionService = sectionService;
}
/*@PreAuthorize("isAuthenticated()")
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/save/{index}", method = RequestMethod.POST)
public Section saveSection(@PathVariable("stakeholderId") String stakeholderId,
public SectionFull saveSection(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId,
@PathVariable("subcategoryId") String subcategoryId,
@PathVariable("index") String index,
@RequestBody Section<Indicator> sectionFull) {
@RequestBody SectionFull sectionFull) {
log.debug("save section");
log.debug("Name: "+sectionFull.getTitle() + " - Id: "+sectionFull.getId() + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId);
SubCategory<String> subCategory = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId);
Section<String> section = new Section<>(sectionFull);
Date date = new Date();
section.setUpdateDate(date);
sectionFull.setUpdateDate(date);
List<String> indicators = new ArrayList<>();
Section<String> oldSection = null;
if(sectionFull.getId() != null) {
oldSection = sectionDAO.findById(sectionFull.getId());
if(oldSection == null) {
// EXCEPTION - Section not found
throw new EntityNotFoundException("save section: Section with id: " + sectionFull.getId() + " not found");
}
for(String indicatorId : oldSection.getIndicators()) {
Indicator indicator = indicatorDAO.findById(indicatorId);
if (indicator == null) {
// EXCEPTION - Indicator not found
throw new EntityNotFoundException("Save section: Indicator with id: "+indicatorId+" not found (indicator exists in section: "+section.getId()+")");
}
indicators.add(indicator.getId());
}
} else { // section does not exist in DB
section.setCreationDate(date);
sectionFull.setCreationDate(date);
for(Indicator indicator : sectionFull.getIndicators()) {
indicators.add(indicator.getId());
}
}
String sectionId = sectionFull.getId();
section.setIndicators(indicators);
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
// this section belongs in default profile and it is new or it is updated
if(stakeholder.getDefaultId() == null) {
if(sectionId == null) {
sectionDAO.save(section);
onSaveDefaultSection(section, topicId, categoryId, subcategoryId, stakeholder);
}
else {
onUpdateDefaultSection(section, stakeholder, oldSection);
sectionDAO.save(section);
}
} else {
sectionDAO.save(section);
}
List<String> sections = null;
if(sectionFull.getType().equals("chart")) {
sections = subCategory.getCharts();
} else if(sectionFull.getType().equals("number")) {
sections = subCategory.getNumbers();
}
int existing_index = sections.indexOf(section.getId());
if (existing_index == -1) {
if(Integer.parseInt(index) != -1) {
sections.add(Integer.parseInt(index), section.getId());
} else {
sections.add(section.getId());
}
subCategoryDAO.save(subCategory);
log.debug("Section saved!");
sectionFull.setId(section.getId());
}
return sectionFull;
Stakeholder stakeholder = this.stakeholderService.findByPath(stakeholderId);
Topic topic = this.topicService.findByPath(stakeholder, topicId);
Category category = this.categoryService.findByPath(topic, categoryId);
SubCategory subCategory = this.subCategoryService.findByPath(category, subcategoryId);
return this.sectionService.save(stakeholder, subCategory, new Section(sectionFull), Integer.parseInt(index));
}
public void onSaveDefaultSection(Section section,
String defaultTopicId, String defaultCategoryId,
String defaultSubcategoryId, Stakeholder defaultStakeholder) {
log.debug("On save default section");
// new section in default profile - add it on profiles of the same type
List<SubCategory> subCategories = subCategoryDAO.findByDefaultId(defaultSubcategoryId);
for (SubCategory subCategory : subCategories) {
Category parentCategory = categoryDAO.findBySubCategoriesContaining(subCategory.getId());
Topic parentTopic = topicDAO.findByCategoriesContaining(parentCategory.getId());
Stakeholder parentStakeholder = stakeholderDAO.findByTopicsContaining(parentTopic.getId());
Section sectionNew = new Section();
sectionNew.copyFromDefault(section);
sectionNew.setStakeholderAlias(parentStakeholder.getAlias());
sectionDAO.save(sectionNew);
List<String> sections = null;
if (section.getType().equals("chart")) {
sections = subCategory.getCharts();
} else if (section.getType().equals("number")) {
sections = subCategory.getNumbers();
}
sections.add(sectionNew.getId());
subCategoryDAO.save(subCategory);
}
}
public void onUpdateDefaultSection(Section section, Stakeholder stakeholder, Section oldSection) {
log.debug("On update default section");
// section already exists - check if changed and update all sections based on it
boolean changed = false;
List<Section> sections = sectionDAO.findByDefaultId(section.getId());
for(Section sectionBasedOnDefault : sections) {
if(section.getTitle() != null && !section.getTitle().equals(sectionBasedOnDefault.getTitle())
&& (oldSection.getTitle() == null || oldSection.getTitle().equals(sectionBasedOnDefault.getTitle()))) {
sectionBasedOnDefault.setTitle(section.getTitle());
changed = true;
}
if(!changed) {
// break;
continue;
}
// sectionBasedOnDefault.setTitle(section.getTitle());
sectionBasedOnDefault.setUpdateDate(section.getUpdateDate());
sectionDAO.save(sectionBasedOnDefault);
}
}*/
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/delete", method = RequestMethod.DELETE)
public boolean deleteSection(@PathVariable("stakeholderId") String stakeholderId,

View File

@ -77,14 +77,14 @@ public class StakeholderController {
}
@RequestMapping(value = "/stakeholder", method = RequestMethod.GET)
public List<Stakeholder> getAllRealStakeholders(@RequestParam(required = false) String type,
public List<Stakeholder> getVisibleStakeholders(@RequestParam(required = false) String type,
@RequestParam(required = false) String defaultId) {
return stakeholderService.getStakeholdersByTypeAndRole(type, defaultId, false);
}
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/my-stakeholder", method = RequestMethod.GET)
public List<Stakeholder> getMyRealStakeholders(@RequestParam(required = false) String type) {
public List<Stakeholder> getManagedStakeholders(@RequestParam(required = false) String type) {
return stakeholderService.getStakeholdersByTypeAndRole(type, null, true);
}

View File

@ -1,6 +1,8 @@
package eu.dnetlib.uoamonitorservice.controllers;
import eu.dnetlib.uoamonitorservice.dto.CategoryFull;
import eu.dnetlib.uoamonitorservice.dto.SubCategoryFull;
import eu.dnetlib.uoamonitorservice.dto.TopicFull;
import eu.dnetlib.uoamonitorservice.entities.Category;
import eu.dnetlib.uoamonitorservice.entities.Stakeholder;
import eu.dnetlib.uoamonitorservice.entities.SubCategory;
@ -34,136 +36,20 @@ public class SubCategoryController {
this.subCategoryService = subCategoryService;
}
/* @PreAuthorize("isAuthenticated()")
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/save", method = RequestMethod.POST)
public SubCategory<Section<Indicator>> saveSubCategory(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId,
@RequestBody SubCategory<Section<Indicator>> subcategoryFull) {
public SubCategoryFull saveSubCategory(@PathVariable("stakeholderId") String stakeholderId,
@PathVariable("topicId") String topicId,
@PathVariable("categoryId") String categoryId,
@RequestBody SubCategoryFull subcategoryFull) {
log.debug("save subcategory");
log.debug("Alias: "+subcategoryFull.getAlias() + " - Id: "+subcategoryFull.getId() + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId);
Category category = checkForExceptions(stakeholderId, topicId, categoryId);
SubCategory<String> subCategory = new SubCategory<>(subcategoryFull);
Date date = new Date();
subCategory.setUpdateDate(date);
subcategoryFull.setUpdateDate(date);
List<String> chartSections = new ArrayList<>();
List<String> numberSections = new ArrayList<>();
SubCategory<String> oldSubcategory = null;
if(subcategoryFull.getId() != null) {
oldSubcategory = subCategoryDAO.findById(subcategoryFull.getId());
if(oldSubcategory == null) {
// EXCEPTION - SubCategory not found
throw new EntityNotFoundException("save subcategory: SubCategory with id: " + subcategoryFull.getId() + " not found");
}
for(String chartSectionId : oldSubcategory.getCharts()) {
Section section = sectionDAO.findById(chartSectionId);
if (section == null) {
// EXCEPTION - Section not found
throw new EntityNotFoundException("Save subcategory: Chart section with id: "+chartSectionId+" not found (section exists in subcategory: "+subCategory.getId()+")");
}
chartSections.add(section.getId());
}
for(String numberSectionId : oldSubcategory.getNumbers()) {
Section section = sectionDAO.findById(numberSectionId);
if (section == null) {
// EXCEPTION - Section not found
throw new EntityNotFoundException("Save subcategory: Number section with id: "+numberSectionId+" not found (section exists in subcategory: "+subCategory.getId()+")");
}
numberSections.add(section.getId());
}
} else { // subcategory does not exist in DB
subCategory.setCreationDate(date);
subcategoryFull.setCreationDate(date);
for(Section chartSection : subcategoryFull.getCharts()) {
chartSections.add(chartSection.getId());
}
for(Section numberSection : subcategoryFull.getNumbers()) {
numberSections.add(numberSection.getId());
}
}
subCategory.setCharts(chartSections);
subCategory.setNumbers(numberSections);
Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId);
if(stakeholder.getDefaultId() == null) {
if(subcategoryFull.getId() == null) {
subCategoryDAO.save(subCategory);
onSaveDefaultSubCategory(subCategory, categoryId);
} else {
onUpdateDefaultSubCategory(subCategory, oldSubcategory);
subCategoryDAO.save(subCategory);
}
} else {
subCategoryDAO.save(subCategory);
}
List<String> subcategories = category.getSubCategories();
int index = subcategories.indexOf(subCategory.getId());
if(index == -1) {
subcategories.add(subCategory.getId());
categoryDAO.save(category);
log.debug("Subcategory saved!");
subcategoryFull.setId(subCategory.getId());
}
return subcategoryFull;
log.debug("Alias: " + subcategoryFull.getAlias() + " - Id: " + subcategoryFull.getId() + " - Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId);
Stakeholder stakeholder = this.stakeholderService.findByPath(stakeholderId);
Topic topic = this.topicService.findByPath(stakeholder, topicId);
Category category = this.categoryService.findByPath(topic, categoryId);
return this.subCategoryService.save(stakeholder, category, new SubCategory(subcategoryFull));
}
public void onSaveDefaultSubCategory(SubCategory subCategory, String categoryId) {
log.debug("On save default subCategory");
List<Category> categories = categoryDAO.findByDefaultId(categoryId);
for(Category category : categories) {
SubCategory subCategoryNew = new SubCategory();
subCategoryNew.copyFromDefault(subCategory);
subCategoryDAO.save(subCategoryNew);
List<String> subCategories = category.getSubCategories();
subCategories.add(subCategoryNew.getId());
categoryDAO.save(category);
}
}
public void onUpdateDefaultSubCategory(SubCategory subCategory, SubCategory oldSubcategory) {
log.debug("On update default subCategory");
List<SubCategory> subCategories = subCategoryDAO.findByDefaultId(subCategory.getId());
boolean changed = false;
for(SubCategory subCategoryBasedOnDefault : subCategories) {
if(subCategory.getName() != null && !subCategory.getName().equals(subCategoryBasedOnDefault.getName())
&& (oldSubcategory.getName() == null || oldSubcategory.getName().equals(subCategoryBasedOnDefault.getName()))) {
subCategoryBasedOnDefault.setName(subCategory.getName());
subCategoryBasedOnDefault.setAlias(subCategory.getAlias());
changed = true;
}
if(subCategory.getDescription() != null && !subCategory.getDescription().equals(subCategoryBasedOnDefault.getDescription())
&& (oldSubcategory.getDescription() == null || oldSubcategory.getDescription().equals(subCategoryBasedOnDefault.getDescription()))) {
subCategoryBasedOnDefault.setDescription(subCategory.getDescription());
changed = true;
}
if(!changed) {
continue;
}
subCategoryBasedOnDefault.setUpdateDate(subCategory.getUpdateDate());
subCategoryDAO.save(subCategoryBasedOnDefault);
}
}*/
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/delete", method = RequestMethod.DELETE)
public boolean deleteSubCategory(@PathVariable("stakeholderId") String stakeholderId,

View File

@ -13,6 +13,6 @@ public class CategoryFull extends CategoryGeneric<SubCategoryFull> {
public CategoryFull(CategoryGeneric category, List<SubCategoryFull> subCategories) {
super(category);
subCategories.removeIf(Objects::isNull);
this.setSubCategories(subCategories);
this.subCategories = subCategories;
}
}

View File

@ -1,19 +1,18 @@
package eu.dnetlib.uoamonitorservice.dto;
import eu.dnetlib.uoamonitorservice.dao.IndicatorDAO;
import eu.dnetlib.uoamonitorservice.entities.Indicator;
import eu.dnetlib.uoamonitorservice.entities.Section;
import eu.dnetlib.uoamonitorservice.generics.SectionGeneric;
import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException;
import java.util.List;
import java.util.stream.Collectors;
import java.util.Objects;
public class SectionFull extends SectionGeneric<Indicator> {
public SectionFull() {}
public SectionFull(Section section, List<Indicator> indicators) {
super(section);
this.setIndicators(indicators);
indicators.removeIf(Objects::isNull);
this.indicators = indicators;
}
}

View File

@ -14,6 +14,6 @@ public class StakeholderFull extends StakeholderGeneric<TopicFull> {
public StakeholderFull(StakeholderGeneric stakeholder, List<TopicFull> topics) {
super(stakeholder);
topics.removeIf(Objects::isNull);
this.setTopics(topics);
this.topics = topics;
}
}

View File

@ -12,7 +12,7 @@ public class SubCategoryFull extends SubCategoryGeneric<SectionFull> {
super(subCategory);
numbers.removeIf(Objects::isNull);
charts.removeIf(Objects::isNull);
this.setNumbers(numbers);
this.setCharts(charts);
this.numbers = numbers;
this.charts = charts;
}
}

View File

@ -13,6 +13,6 @@ public class TopicFull extends TopicGeneric<CategoryFull> {
public TopicFull(TopicGeneric topic, List<CategoryFull> categories) {
super(topic);
categories.removeIf(Objects::isNull);
this.setCategories(categories);
this.categories = categories;
}
}

View File

@ -16,20 +16,42 @@ public class Category extends CategoryGeneric<String> {
super();
}
public Category(Category category) {
super(category);
}
public Category(CategoryFull category) {
super(category);
this.subCategories = category.getSubCategories().stream().map(Common::getId).collect(Collectors.toList());
this.subCategories.removeIf(Objects::isNull);
}
public void copyFromDefault(CategoryGeneric defaultCategory) {
setName(defaultCategory.getName());
setAlias(defaultCategory.getAlias());
setDescription(defaultCategory.getDescription());
setVisibility(defaultCategory.getVisibility());
setCreationDate(defaultCategory.getCreationDate());
setUpdateDate(defaultCategory.getUpdateDate());
setDefaultId(defaultCategory.getId());
setSubCategories(new ArrayList<>());
public Category copy() {
Category category = new Category(this);
category.setDefaultId(this.getId());
category.setId(null);
return category;
}
public Category override(Category category, Category old) {
return (Category) super.override(category, old);
}
public SubCategory createOverview() {
SubCategory subCategory = new SubCategory();
subCategory.setName("Overview");
subCategory.setAlias("overview");
subCategory.setVisibility(this.getVisibility());
subCategory.setCharts(new ArrayList<>());
subCategory.setNumbers(new ArrayList<>());
return subCategory;
}
public void addSubCategory(String id) {
this.subCategories.add(id);
}
public void removeSubCategory(String id) {
this.subCategories.remove(id);
}
}

View File

@ -1,6 +1,7 @@
package eu.dnetlib.uoamonitorservice.entities;
import eu.dnetlib.uoamonitorservice.dto.SectionFull;
import eu.dnetlib.uoamonitorservice.generics.Common;
import eu.dnetlib.uoamonitorservice.generics.SectionGeneric;
import org.springframework.data.mongodb.core.mapping.Document;
@ -12,9 +13,29 @@ import java.util.stream.Collectors;
public class Section extends SectionGeneric<String> {
public Section() {}
public Section(Section section) {
super(section);
}
public Section(SectionFull section) {
super(section);
this.indicators = section.getIndicators().stream().map(Indicator::getId).collect(Collectors.toList());
this.indicators.removeIf(Objects::isNull);
}
public Section copy() {
Section section = new Section(this);
section.setDefaultId(this.getId());
section.setId(null);
return section;
}
public Section override(Section section, Section old) {
section = (Section) super.override(section, old);
if(this.getTitle() != null && !this.getTitle().equals(section.getTitle()) &&
(old.getTitle() == null || old.getTitle().equals(section.getTitle()))) {
section.setTitle(this.getTitle());
}
return section;
}
}

View File

@ -5,12 +5,19 @@ import eu.dnetlib.uoamonitorservice.generics.Common;
import eu.dnetlib.uoamonitorservice.generics.SubCategoryGeneric;
import org.springframework.data.mongodb.core.mapping.Document;
import java.util.ArrayList;
import java.util.Objects;
import java.util.stream.Collectors;
@Document
public class SubCategory extends SubCategoryGeneric<String> {
public SubCategory() {}
public SubCategory() {
super();
}
public SubCategory(SubCategory subCategory) {
super(subCategory);
}
public SubCategory(SubCategoryFull subCategory) {
super(subCategory);
@ -19,4 +26,39 @@ public class SubCategory extends SubCategoryGeneric<String> {
this.numbers.removeIf(Objects::isNull);
this.charts.removeIf(Objects::isNull);
}
public SubCategory copy() {
SubCategory subCategory = new SubCategory(this);
subCategory.setDefaultId(this.getId());
subCategory.setId(null);
return subCategory;
}
public SubCategory override(SubCategory subCategory, SubCategory old) {
return (SubCategory) super.override(subCategory, old);
}
public void addNumber(String id, int index) {
if(index == -1) {
this.numbers.add(id);
} else {
this.numbers.add(index, id);
}
}
public void removeNumber(String id) {
this.numbers.remove(id);
}
public void addChart(String id, int index) {
if(index == -1) {
this.charts.add(id);
} else {
this.charts.add(index, id);
}
}
public void removeChart(String id) {
this.charts.remove(id);
}
}

View File

@ -47,4 +47,12 @@ public class Topic extends TopicGeneric<String> {
}
return topic;
}
public void addCategory(String id) {
this.categories.add(id);
}
public void removeCategory(String id) {
this.categories.remove(id);
}
}

View File

@ -28,6 +28,8 @@ public class CategoryGeneric<StringOrSubcategory> extends Common {
creationDate = category.getCreationDate();
updateDate = category.getUpdateDate();
defaultId = category.getDefaultId();
isOverview = category.getIsOverview();
subCategories = new ArrayList<>();
}
public boolean getIsOverview() {

View File

@ -22,6 +22,7 @@ public class SectionGeneric<StringOrIndicator> extends Common {
setType(section.getType());
creationDate = section.getCreationDate();
updateDate = section.getUpdateDate();
indicators = new ArrayList<>();
}
public void copyFromDefault(SectionGeneric defaultSection) {
@ -64,6 +65,10 @@ public class SectionGeneric<StringOrIndicator> extends Common {
}
}
public boolean isNumber() {
return this.type == IndicatorType.NUMBER || this.type == IndicatorType.number;
}
public List<StringOrIndicator> getIndicators() {
return indicators;
}

View File

@ -1,14 +1,6 @@
package eu.dnetlib.uoamonitorservice.generics;
import com.fasterxml.jackson.annotation.JsonProperty;
import eu.dnetlib.uoamonitorservice.entities.Category;
import eu.dnetlib.uoamonitorservice.primitives.Visibility;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.LastModifiedDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class SubCategoryGeneric<StringOrSection> extends Common {
@ -25,28 +17,8 @@ public class SubCategoryGeneric<StringOrSection> extends Common {
creationDate = subCategory.getCreationDate();
updateDate = subCategory.getUpdateDate();
defaultId = subCategory.getDefaultId();
}
public void createOverviewSubCategory(Category category) {
setName("Overview");
setAlias("overview");
setVisibility(category.getVisibility());
setCreationDate(category.getCreationDate());
setUpdateDate(category.getUpdateDate());
setCharts(new ArrayList<>());
setNumbers(new ArrayList<>());
}
public void copyFromDefault(SubCategoryGeneric defaultSubCategory) {
setName(defaultSubCategory.getName());
setAlias(defaultSubCategory.getAlias());
setDescription(defaultSubCategory.getDescription());
setVisibility(defaultSubCategory.getVisibility());
setCreationDate(defaultSubCategory.getCreationDate());
setUpdateDate(defaultSubCategory.getUpdateDate());
setDefaultId(defaultSubCategory.getId());
setCharts(new ArrayList<>());
setNumbers(new ArrayList<>());
numbers = new ArrayList<>();
charts = new ArrayList<>();
}
public List<StringOrSection> getCharts() {

View File

@ -2,9 +2,12 @@ package eu.dnetlib.uoamonitorservice.service;
import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException;
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;
import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException;
import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException;
@ -17,6 +20,7 @@ import java.util.stream.Collectors;
@Service
public class CategoryService {
private final StakeholderDAO stakeholderDAO;
private final TopicDAO topicDAO;
private final CategoryDAO dao;
@ -24,13 +28,18 @@ public class CategoryService {
private final CommonService commonService;
@Autowired
public CategoryService(TopicDAO topicDAO, CategoryDAO dao, SubCategoryService subCategoryService, CommonService commonService) {
public CategoryService(StakeholderDAO stakeholderDAO, TopicDAO topicDAO, CategoryDAO dao, SubCategoryService subCategoryService, CommonService commonService) {
this.stakeholderDAO = stakeholderDAO;
this.topicDAO = topicDAO;
this.dao = dao;
this.subCategoryService = subCategoryService;
this.commonService = commonService;
}
public Category find(String id) {
return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("Category with id: " + id + " not found"));
}
public Category findByPath(Topic topic, String categoryId) {
if (!topic.getCategories().contains(categoryId)) {
throw new PathNotValidException("Category with id: " + categoryId + " not found in Topic: " + topic.getId());
@ -53,9 +62,6 @@ public class CategoryService {
return this.getFullCategory(type, alias, category);
}
public Category find(String id) {
return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("Category with id: " + id + " not found"));
}
public CategoryFull buildCategory(CategoryFull categoryFull) {
categoryFull.setSubCategories(categoryFull.getSubCategories().stream().map(this.subCategoryService::buildSubcategory).collect(Collectors.toList()));
@ -68,6 +74,48 @@ public class CategoryService {
return this.dao.save(category);
}
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())) {
category.setSubCategories(this.find(topic.getId()).getSubCategories());
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())));
});
}
public void delete(String type, Category category, boolean remove) {
if (this.commonService.hasDeleteAuthority(type)) {
this.dao.findByDefaultId(category.getId()).forEach(child -> {
@ -90,9 +138,14 @@ public class CategoryService {
this.delete(type, category, remove);
}
public void addCategory(Topic topic, String id) {
topic.addCategory(id);
this.topicDAO.save(topic);
}
public void removeCategory(String id) {
this.topicDAO.findByCategoriesContaining(id).forEach(topic -> {
topic.getCategories().remove(id);
topic.removeCategory(id);
this.topicDAO.save(topic);
});
}

View File

@ -1,13 +1,9 @@
package eu.dnetlib.uoamonitorservice.service;
import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException;
import eu.dnetlib.uoamonitorservice.dao.SectionDAO;
import eu.dnetlib.uoamonitorservice.dao.SubCategoryDAO;
import eu.dnetlib.uoamonitorservice.dao.*;
import eu.dnetlib.uoamonitorservice.dto.SectionFull;
import eu.dnetlib.uoamonitorservice.entities.Category;
import eu.dnetlib.uoamonitorservice.entities.Indicator;
import eu.dnetlib.uoamonitorservice.entities.Section;
import eu.dnetlib.uoamonitorservice.entities.SubCategory;
import eu.dnetlib.uoamonitorservice.entities.*;
import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException;
import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException;
import eu.dnetlib.uoamonitorservice.primitives.Visibility;
@ -20,6 +16,9 @@ import java.util.stream.Collectors;
@Service
public class SectionService {
private final StakeholderDAO stakeholderDAO;
private final TopicDAO topicDAO;
private final CategoryDAO categoryDAO;
private final SubCategoryDAO subCategoryDAO;
private final SectionDAO dao;
@ -27,13 +26,20 @@ public class SectionService {
private final CommonService commonService;
@Autowired
public SectionService(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;
this.subCategoryDAO = subCategoryDAO;
this.dao = dao;
this.indicatorService = indicatorService;
this.commonService = commonService;
}
public Section find(String id) {
return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("Section with id: " + id + " not found"));
}
public Section findByPath(SubCategory subCategory, String sectionId) {
if (!subCategory.getNumbers().contains(sectionId) && !subCategory.getCharts().contains(sectionId)) {
throw new PathNotValidException("Section with id: " + sectionId + " not found in SubCategory: " + subCategory.getId());
@ -41,24 +47,15 @@ public class SectionService {
return this.dao.findById(sectionId).orElseThrow(() -> new EntityNotFoundException("Section with id: " + sectionId + " not found"));
}
public Section find(String id) {
return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("Section with id: " + id + " not found"));
}
public SectionFull getFullSection(String type, String alias, String id) {
Section section = this.find(id);
public SectionFull getFullSection(String type, String alias, Section section) {
return new SectionFull(section, section.getIndicators().stream()
.map(indicatorId -> this.indicatorService.getIndicator(type, alias, indicatorId))
.collect(Collectors.toList()));
}
public List<Indicator> getIndicators(String id) {
public SectionFull getFullSection(String type, String alias, String id) {
Section section = this.find(id);
return section.getIndicators().stream().map(this.indicatorService::find).collect(Collectors.toList());
}
public List<String> getIndicatorsId(String id) {
return this.getIndicators(id).stream().map(Indicator::getId).collect(Collectors.toList());
return this.getFullSection(type, alias, section);
}
public SectionFull buildSection(SectionFull sectionFull) {
@ -72,6 +69,46 @@ public class SectionService {
return this.dao.save(section);
}
public SectionFull save(Stakeholder stakeholder, SubCategory subCategory, Section section, int index) {
section.setStakeholderAlias(stakeholder.getAlias());
if (section.getId() != null) {
if (this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) {
section.setIndicators(this.find(section.getId()).getIndicators());
this.updateChildren(section);
section = this.save(section);
} else {
throw new ForbiddenException("You are not authorized to update stakeholder with id: " + stakeholder.getId());
}
} else {
if (this.commonService.hasCreateAuthority(stakeholder.getType())) {
section = this.save(section);
this.createChildren(subCategory, section, index);
this.addSection(subCategory, section.getId(), index);
} else {
throw new ForbiddenException("You are not authorized to create a section in stakeholder with id: " + stakeholder.getId());
}
}
return this.getFullSection(stakeholder.getType(), stakeholder.getAlias(), section);
}
public void createChildren(SubCategory defaultSubCategory, Section section, int index) {
this.subCategoryDAO.findByDefaultId(defaultSubCategory.getId()).forEach(subCategory -> {
this.categoryDAO.findBySubCategoriesContaining(subCategory.getId()).forEach(category -> {
this.topicDAO.findByCategoriesContaining(category.getId()).forEach(topic -> {
this.stakeholderDAO.findByTopicsContaining(topic.getId()).forEach(stakeholder -> {
this.save(stakeholder, subCategory, section.copy(), index);
});
});
});
});
}
public void updateChildren(Section section) {
this.dao.findByDefaultId(section.getId()).forEach(child -> {
this.save(section.override(child, this.find(section.getId())));
});
}
public void delete(String type, Section section, boolean remove) {
if(this.commonService.hasDeleteAuthority(type)) {
this.dao.findByDefaultId(section.getId()).forEach(child -> {
@ -94,13 +131,22 @@ public class SectionService {
this.delete(type, section, remove);
}
public void addSection(SubCategory subCategory, String id, int index) {
if(this.find(id).isNumber()) {
subCategory.addNumber(id, index);
} else {
subCategory.addChart(id, index);
}
this.subCategoryDAO.save(subCategory);
}
public void removeSection(String id) {
this.subCategoryDAO.findByNumbersContaining(id).forEach(subCategory -> {
subCategory.getNumbers().remove(id);
subCategory.removeNumber(id);
this.subCategoryDAO.save(subCategory);
});
this.subCategoryDAO.findByChartsContaining(id).forEach(subCategory -> {
subCategory.getCharts().remove(id);
subCategory.removeChart(id);
this.subCategoryDAO.save(subCategory);
});
}

View File

@ -78,8 +78,7 @@ public class StakeholderService {
}
public StakeholderFull getFullStakeholder(Stakeholder stakeholder) {
if(this.commonService.hasAccessAuthority(stakeholder.getType(), stakeholder.getAlias(), stakeholder.getDefaultId() == null) ||
(stakeholder.getDefaultId() != null && 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))

View File

@ -2,11 +2,14 @@ package eu.dnetlib.uoamonitorservice.service;
import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException;
import eu.dnetlib.uoamonitorservice.dao.CategoryDAO;
import eu.dnetlib.uoamonitorservice.dao.StakeholderDAO;
import eu.dnetlib.uoamonitorservice.dao.SubCategoryDAO;
import eu.dnetlib.uoamonitorservice.dao.TopicDAO;
import eu.dnetlib.uoamonitorservice.dto.CategoryFull;
import eu.dnetlib.uoamonitorservice.dto.SubCategoryFull;
import eu.dnetlib.uoamonitorservice.dto.TopicFull;
import eu.dnetlib.uoamonitorservice.entities.Category;
import eu.dnetlib.uoamonitorservice.entities.Stakeholder;
import eu.dnetlib.uoamonitorservice.entities.SubCategory;
import eu.dnetlib.uoamonitorservice.entities.Topic;
import eu.dnetlib.uoamonitorservice.generics.Common;
@ -24,6 +27,8 @@ import java.util.stream.Collectors;
@Service
public class SubCategoryService {
private final StakeholderDAO stakeholderDAO;
private final TopicDAO topicDAO;
private final CategoryDAO categoryDAO;
private final SubCategoryDAO dao;
@ -31,13 +36,19 @@ public class SubCategoryService {
private final SectionService sectionService;
@Autowired
public SubCategoryService(CategoryDAO categoryDAO, SubCategoryDAO dao,CommonService commonService, SectionService sectionService) {
public SubCategoryService(StakeholderDAO stakeholderDAO, TopicDAO topicDAO, CategoryDAO categoryDAO, SubCategoryDAO dao,CommonService commonService, SectionService sectionService) {
this.stakeholderDAO = stakeholderDAO;
this.topicDAO = topicDAO;
this.categoryDAO = categoryDAO;
this.dao = dao;
this.commonService = commonService;
this.sectionService = sectionService;
}
public SubCategory find(String id) {
return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("SubCategory with id: " + id + " not found"));
}
public SubCategory findByPath(Category category, String subcategoryId) {
if (!category.getSubCategories().contains(subcategoryId)) {
throw new PathNotValidException("SubCategory with id: " + subcategoryId + " not found in Category: " + category.getId());
@ -63,10 +74,6 @@ public class SubCategoryService {
return this.getFullSubCategory(type, alias, subCategory);
}
public SubCategory find(String id) {
return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("SubCategory with id: " + id + " not found"));
}
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()));
@ -80,7 +87,46 @@ public class SubCategoryService {
return this.dao.save(subCategory);
}
public void delete(String type, SubCategory subCategory, boolean remove ) {
public SubCategoryFull save(Stakeholder stakeholder, Category category, SubCategory subCategory) {
if (subCategory.getId() != null) {
if (this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) {
SubCategory old = this.find(subCategory.getId());
subCategory.setNumbers(old.getNumbers());
subCategory.setCharts(old.getCharts());
this.updateChildren(subCategory);
subCategory = this.save(subCategory);
} else {
throw new ForbiddenException("You are not authorized to update stakeholder with id: " + stakeholder.getId());
}
} else {
if (this.commonService.hasCreateAuthority(stakeholder.getType())) {
subCategory = this.save(subCategory);
this.createChildren(category, subCategory);
this.addSubCategory(category, subCategory.getId());
} else {
throw new ForbiddenException("You are not authorized to create a subCategory in stakeholder with id: " + stakeholder.getId());
}
}
return this.getFullSubCategory(stakeholder.getType(), stakeholder.getAlias(), subCategory);
}
public void createChildren(Category defaultCategory, SubCategory subCategory) {
this.categoryDAO.findByDefaultId(defaultCategory.getId()).forEach(category -> {
this.topicDAO.findByCategoriesContaining(category.getId()).forEach(topic -> {
this.stakeholderDAO.findByTopicsContaining(topic.getId()).forEach(stakeholder -> {
this.save(stakeholder, category, subCategory.copy());
});
});
});
}
public void updateChildren(SubCategory subCategory) {
this.dao.findByDefaultId(subCategory.getId()).forEach(child -> {
this.save(subCategory.override(child, this.find(subCategory.getId())));
});
}
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);
@ -106,9 +152,14 @@ public class SubCategoryService {
this.delete(type, subCategory, remove);
}
public void addSubCategory(Category category, String id) {
category.addSubCategory(id);
this.categoryDAO.save(category);
}
public void removeSubCategory(String id) {
this.categoryDAO.findBySubCategoriesContaining(id).forEach(category -> {
category.getSubCategories().remove(id);
category.removeSubCategory(id);
this.categoryDAO.save(category);
});
}

View File

@ -32,6 +32,10 @@ public class TopicService {
this.commonService = commonService;
}
public Topic find(String id) {
return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("Topic with id: " + id + " not found"));
}
public Topic findByPath(Stakeholder stakeholder, String topicId) {
if (!stakeholder.getTopics().contains(topicId)) {
throw new PathNotValidException("Topic with id: " + topicId + " not found in Stakeholder: " + stakeholder.getId());
@ -54,19 +58,21 @@ public class TopicService {
return this.getFullTopic(type, alias, topic);
}
public Topic find(String id) {
return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("Topic with id: " + id + " not found"));
}
public TopicFull buildTopic(TopicFull topicFull) {
topicFull.setCategories(topicFull.getCategories().stream().map(this.categoryService::buildCategory).collect(Collectors.toList()));
topicFull.update(this.save(new Topic(topicFull)));
return topicFull;
}
public Topic save(Topic topic) {
topic.getCategories().forEach(this.categoryService::find);
return this.dao.save(topic);
}
public TopicFull save(Stakeholder stakeholder, Topic topic) {
if(topic.getId() != null) {
if(this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) {
topic.setCategories(this.find(topic.getId()).getCategories());
this.updateChildren(topic);
topic = this.save(topic);
} else {
@ -74,8 +80,8 @@ public class TopicService {
}
} else {
if(this.commonService.hasCreateAuthority(stakeholder.getType())) {
this.createChildren(stakeholder, topic);
topic = this.save(topic);
this.createChildren(stakeholder, topic);
this.addTopic(stakeholder, topic.getId());
} else {
throw new ForbiddenException("You are not authorized to create a topic in stakeholder with id: " + stakeholder.getId());
@ -96,11 +102,6 @@ public class TopicService {
});
}
public Topic save(Topic topic) {
topic.getCategories().forEach(this.categoryService::find);
return this.dao.save(topic);
}
public void delete(String type, Topic topic, boolean remove) {
if (this.commonService.hasDeleteAuthority(type)) {
this.dao.findByDefaultId(topic.getId()).forEach(child -> {