From 8a56c3ad8bfc1a827089801573d609654025ab86 Mon Sep 17 00:00:00 2001 From: "k.triantafyllou" Date: Wed, 27 Mar 2024 14:07:41 +0200 Subject: [PATCH] Save category, subcategory, section refactor. --- .../controllers/CategoryController.java | 47 ++---- .../controllers/SectionController.java | 148 ++---------------- .../controllers/StakeholderController.java | 4 +- .../controllers/SubCategoryController.java | 138 ++-------------- .../uoamonitorservice/dto/CategoryFull.java | 2 +- .../uoamonitorservice/dto/SectionFull.java | 7 +- .../dto/StakeholderFull.java | 2 +- .../dto/SubCategoryFull.java | 4 +- .../uoamonitorservice/dto/TopicFull.java | 2 +- .../uoamonitorservice/entities/Category.java | 40 +++-- .../uoamonitorservice/entities/Section.java | 21 +++ .../entities/SubCategory.java | 44 +++++- .../uoamonitorservice/entities/Topic.java | 8 + .../generics/CategoryGeneric.java | 2 + .../generics/SectionGeneric.java | 5 + .../generics/SubCategoryGeneric.java | 32 +--- .../service/CategoryService.java | 63 +++++++- .../service/SectionService.java | 88 ++++++++--- .../service/StakeholderService.java | 3 +- .../service/SubCategoryService.java | 65 +++++++- .../service/TopicService.java | 21 +-- 21 files changed, 348 insertions(+), 398 deletions(-) diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/CategoryController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/CategoryController.java index b6980d5..ce406b4 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/CategoryController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/CategoryController.java @@ -30,17 +30,17 @@ public class CategoryController { this.categoryService = categoryService; } - /*@PreAuthorize("isAuthenticated()") + @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/save", method = RequestMethod.POST) - public Category saveCategory(@PathVariable("stakeholderId") String stakeholderId, + public CategoryFull saveCategory(@PathVariable("stakeholderId") String stakeholderId, @PathVariable("topicId") String topicId, - @RequestBody Category 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 category, String topicId) { + /* public void onSaveDefaultCategory(Category category, String topicId) { log.debug("On save default category"); List 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 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) diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SectionController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SectionController.java index 4531344..f93d6da 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SectionController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SectionController.java @@ -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 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 subCategory = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId); - - Section section = new Section<>(sectionFull); - - Date date = new Date(); - section.setUpdateDate(date); - sectionFull.setUpdateDate(date); - - List indicators = new ArrayList<>(); - - Section 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 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 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 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 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
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, diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java index cc0e850..7df0dfb 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java @@ -77,14 +77,14 @@ public class StakeholderController { } @RequestMapping(value = "/stakeholder", method = RequestMethod.GET) - public List getAllRealStakeholders(@RequestParam(required = false) String type, + public List 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 getMyRealStakeholders(@RequestParam(required = false) String type) { + public List getManagedStakeholders(@RequestParam(required = false) String type) { return stakeholderService.getStakeholdersByTypeAndRole(type, null, true); } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SubCategoryController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SubCategoryController.java index ef9b4d6..32dee17 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SubCategoryController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SubCategoryController.java @@ -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> saveSubCategory(@PathVariable("stakeholderId") String stakeholderId, - @PathVariable("topicId") String topicId, - @PathVariable("categoryId") String categoryId, - @RequestBody SubCategory> 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 subCategory = new SubCategory<>(subcategoryFull); - - Date date = new Date(); - subCategory.setUpdateDate(date); - subcategoryFull.setUpdateDate(date); - - List chartSections = new ArrayList<>(); - List numberSections = new ArrayList<>(); - - SubCategory 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 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 categories = categoryDAO.findByDefaultId(categoryId); - for(Category category : categories) { - SubCategory subCategoryNew = new SubCategory(); - subCategoryNew.copyFromDefault(subCategory); - - subCategoryDAO.save(subCategoryNew); - - List subCategories = category.getSubCategories(); - subCategories.add(subCategoryNew.getId()); - - categoryDAO.save(category); - } - } - - public void onUpdateDefaultSubCategory(SubCategory subCategory, SubCategory oldSubcategory) { - log.debug("On update default subCategory"); - - List 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, diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dto/CategoryFull.java b/src/main/java/eu/dnetlib/uoamonitorservice/dto/CategoryFull.java index 3211e1d..72b82d8 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dto/CategoryFull.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dto/CategoryFull.java @@ -13,6 +13,6 @@ public class CategoryFull extends CategoryGeneric { public CategoryFull(CategoryGeneric category, List subCategories) { super(category); subCategories.removeIf(Objects::isNull); - this.setSubCategories(subCategories); + this.subCategories = subCategories; } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dto/SectionFull.java b/src/main/java/eu/dnetlib/uoamonitorservice/dto/SectionFull.java index fc7f74b..fd6f7f6 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dto/SectionFull.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dto/SectionFull.java @@ -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 { public SectionFull() {} public SectionFull(Section section, List indicators) { super(section); - this.setIndicators(indicators); + indicators.removeIf(Objects::isNull); + this.indicators = indicators; } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dto/StakeholderFull.java b/src/main/java/eu/dnetlib/uoamonitorservice/dto/StakeholderFull.java index 10f84b4..3c14598 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dto/StakeholderFull.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dto/StakeholderFull.java @@ -14,6 +14,6 @@ public class StakeholderFull extends StakeholderGeneric { public StakeholderFull(StakeholderGeneric stakeholder, List topics) { super(stakeholder); topics.removeIf(Objects::isNull); - this.setTopics(topics); + this.topics = topics; } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dto/SubCategoryFull.java b/src/main/java/eu/dnetlib/uoamonitorservice/dto/SubCategoryFull.java index ca16ffd..b326a2a 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dto/SubCategoryFull.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dto/SubCategoryFull.java @@ -12,7 +12,7 @@ public class SubCategoryFull extends SubCategoryGeneric { super(subCategory); numbers.removeIf(Objects::isNull); charts.removeIf(Objects::isNull); - this.setNumbers(numbers); - this.setCharts(charts); + this.numbers = numbers; + this.charts = charts; } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dto/TopicFull.java b/src/main/java/eu/dnetlib/uoamonitorservice/dto/TopicFull.java index 99cf4c5..92a8982 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dto/TopicFull.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dto/TopicFull.java @@ -13,6 +13,6 @@ public class TopicFull extends TopicGeneric { public TopicFull(TopicGeneric topic, List categories) { super(topic); categories.removeIf(Objects::isNull); - this.setCategories(categories); + this.categories = categories; } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Category.java b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Category.java index 21a3ec8..60dbc49 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Category.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Category.java @@ -16,20 +16,42 @@ public class Category extends CategoryGeneric { 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); } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Section.java b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Section.java index 5f9e4de..ecd6497 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Section.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Section.java @@ -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 { 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; + } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/SubCategory.java b/src/main/java/eu/dnetlib/uoamonitorservice/entities/SubCategory.java index 1791cc2..42b83a7 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/SubCategory.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/entities/SubCategory.java @@ -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 { - 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 { 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); + } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Topic.java b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Topic.java index 19eef8d..dd15ff0 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Topic.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Topic.java @@ -47,4 +47,12 @@ public class Topic extends TopicGeneric { } return topic; } + + public void addCategory(String id) { + this.categories.add(id); + } + + public void removeCategory(String id) { + this.categories.remove(id); + } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/generics/CategoryGeneric.java b/src/main/java/eu/dnetlib/uoamonitorservice/generics/CategoryGeneric.java index f0e8f57..30166f7 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/generics/CategoryGeneric.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/generics/CategoryGeneric.java @@ -28,6 +28,8 @@ public class CategoryGeneric extends Common { creationDate = category.getCreationDate(); updateDate = category.getUpdateDate(); defaultId = category.getDefaultId(); + isOverview = category.getIsOverview(); + subCategories = new ArrayList<>(); } public boolean getIsOverview() { diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/generics/SectionGeneric.java b/src/main/java/eu/dnetlib/uoamonitorservice/generics/SectionGeneric.java index 48132b5..0914261 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/generics/SectionGeneric.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/generics/SectionGeneric.java @@ -22,6 +22,7 @@ public class SectionGeneric 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 extends Common { } } + public boolean isNumber() { + return this.type == IndicatorType.NUMBER || this.type == IndicatorType.number; + } + public List getIndicators() { return indicators; } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/generics/SubCategoryGeneric.java b/src/main/java/eu/dnetlib/uoamonitorservice/generics/SubCategoryGeneric.java index 5b45e6f..e1870fa 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/generics/SubCategoryGeneric.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/generics/SubCategoryGeneric.java @@ -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 extends Common { @@ -25,28 +17,8 @@ public class SubCategoryGeneric 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 getCharts() { diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/CategoryService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/CategoryService.java index 7e97767..4fab426 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/CategoryService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/CategoryService.java @@ -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); }); } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/SectionService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/SectionService.java index 696c54e..ec7a606 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/SectionService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/SectionService.java @@ -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 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 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); }); } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java index 56259b0..0e57409 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java @@ -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)) diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/SubCategoryService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/SubCategoryService.java index f0da766..597bbae 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/SubCategoryService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/SubCategoryService.java @@ -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); }); } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/TopicService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/TopicService.java index 93b46f5..eb46604 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/TopicService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/TopicService.java @@ -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 -> {