From e345f5b689f02e8655b1abbf1092b34cf98f890a Mon Sep 17 00:00:00 2001 From: "k.triantafyllou" Date: Fri, 8 Mar 2024 20:55:51 +0200 Subject: [PATCH] [new-model | WIP]: Refactoring the code in order to improve performance and code. --- .../controllers/CategoryController.java | 11 +- .../controllers/IndicatorController.java | 11 +- .../controllers/SectionController.java | 108 +----- .../controllers/StakeholderController.java | 359 ++++-------------- .../controllers/SubCategoryController.java | 17 +- .../controllers/TopicController.java | 21 +- .../uoamonitorservice/dao/CategoryDAO.java | 5 +- .../uoamonitorservice/dao/IndicatorDAO.java | 3 +- .../uoamonitorservice/dao/SectionDAO.java | 3 +- .../uoamonitorservice/dao/StakeholderDAO.java | 7 +- .../uoamonitorservice/dao/SubCategoryDAO.java | 3 +- .../uoamonitorservice/dao/TopicDAO.java | 5 +- .../uoamonitorservice/dto/CategoryFull.java | 18 + .../uoamonitorservice/dto/SectionFull.java | 19 + .../dto/StakeholderFull.java | 15 + .../dto/SubCategoryFull.java | 18 + .../uoamonitorservice/dto/TopicFull.java | 18 + .../uoamonitorservice/entities/Category.java | 127 +------ .../uoamonitorservice/entities/Indicator.java | 43 +-- .../uoamonitorservice/entities/Section.java | 118 +----- .../entities/Stakeholder.java | 222 +---------- .../entities/SubCategory.java | 137 +------ .../uoamonitorservice/entities/Topic.java | 130 +------ .../entities/Visibility.java | 38 -- .../generics/CategoryGeneric.java | 88 +++++ .../uoamonitorservice/generics/Common.java | 42 ++ .../generics/SectionGeneric.java | 119 ++++++ .../generics/StakeholderGeneric.java | 193 ++++++++++ .../generics/SubCategoryGeneric.java | 107 ++++++ .../generics/TopicGeneric.java | 100 +++++ .../primitives/Visibility.java | 5 + .../service/CategoryService.java | 52 +++ .../service/CommonService.java | 37 ++ .../service/IndicatorService.java | 38 ++ .../service/SectionService.java | 71 ++++ .../service/StakeholderService.java | 59 ++- .../service/SubCategoryService.java | 55 +++ .../service/TopicService.java | 53 +++ 38 files changed, 1286 insertions(+), 1189 deletions(-) create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/dto/CategoryFull.java create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/dto/SectionFull.java create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/dto/StakeholderFull.java create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/dto/SubCategoryFull.java create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/dto/TopicFull.java delete mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/entities/Visibility.java create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/generics/CategoryGeneric.java create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/generics/Common.java create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/generics/SectionGeneric.java create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/generics/StakeholderGeneric.java create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/generics/SubCategoryGeneric.java create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/generics/TopicGeneric.java create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/primitives/Visibility.java create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/service/CategoryService.java create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/service/CommonService.java create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/service/IndicatorService.java create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/service/SectionService.java create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/service/SubCategoryService.java create mode 100644 src/main/java/eu/dnetlib/uoamonitorservice/service/TopicService.java diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/CategoryController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/CategoryController.java index ea5110e..cc91eab 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/CategoryController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/CategoryController.java @@ -1,3 +1,4 @@ +/* package eu.dnetlib.uoamonitorservice.controllers; import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException; @@ -9,6 +10,7 @@ import eu.dnetlib.uoamonitorservice.dao.TopicDAO; import eu.dnetlib.uoamonitorservice.entities.*; import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; +import eu.dnetlib.uoamonitorservice.primitives.Visibility; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; @@ -77,7 +79,7 @@ public class CategoryController { log.debug("save category"); log.debug("Alias: " + categoryFull.getAlias() + " - Id: " + categoryFull.getId() + " - Stakeholder: " + stakeholderId + " - Topic: " + topicId); - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); + Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); if (stakeholder != null) { if (!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { @@ -227,7 +229,7 @@ public class CategoryController { log.debug("delete category"); log.debug("Id: " + categoryId + " - Stakeholder: " + stakeholderId + " - Topic: " + topicId); - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); + Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); if (stakeholder != null) { if (!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { @@ -370,7 +372,7 @@ public class CategoryController { log.debug("change category visibility: " + visibility + " - toggle propagate: " + ((propagate != null && propagate) ? "true" : "false")); log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId); - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); + Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); if (stakeholder != null) { if (!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { @@ -430,7 +432,7 @@ public class CategoryController { private Topic checkForExceptions(String stakeholderId, String topicId) { - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); + Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); if (stakeholder == null) { // EXCEPTION - Stakeholder not found @@ -486,3 +488,4 @@ public class CategoryController { } } } +*/ diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/IndicatorController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/IndicatorController.java index f8746a5..d1fc52b 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/IndicatorController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/IndicatorController.java @@ -1,3 +1,4 @@ +/* package eu.dnetlib.uoamonitorservice.controllers; @@ -9,6 +10,7 @@ import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; import eu.dnetlib.uoamonitorservice.primitives.IndicatorPath; import eu.dnetlib.uoamonitorservice.primitives.ReorderEvent; +import eu.dnetlib.uoamonitorservice.primitives.Visibility; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; @@ -60,7 +62,7 @@ public class IndicatorController { log.debug("save bulk indicators"); log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId); - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); + Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); Date date = new Date(); @@ -171,7 +173,7 @@ public class IndicatorController { String indicatorId = indicator.getId(); - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); + Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); // this indicator belongs in default profile and it is new or it is updated if (stakeholder.getDefaultId() == null) { if (indicatorId == null) { @@ -477,7 +479,7 @@ public class IndicatorController { if (indicator != null) { Section section = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator.getType()); - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); + Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); if (indicator.getDefaultId() != null && !rolesUtils.hasCreateAndDeleteAuthority(stakeholder.getType())) { // EXCEPTION - Access denied throw new ForbiddenException("Delete indicator: You are not authorized to delete a default Indicator in stakeholder with id: " + stakeholderId); @@ -633,7 +635,7 @@ public class IndicatorController { private Section checkForExceptions(String stakeholderId, String topicId, String categoryId, String subcategoryId, String sectionId, String indicatorType) { - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); + Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); if (stakeholder == null) { // EXCEPTION - Stakeholder not found @@ -714,3 +716,4 @@ public class IndicatorController { } } } +*/ diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SectionController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SectionController.java index 90b47b0..05720fa 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SectionController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SectionController.java @@ -1,11 +1,16 @@ +/* package eu.dnetlib.uoamonitorservice.controllers; import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils; import eu.dnetlib.uoamonitorservice.dao.*; +import eu.dnetlib.uoamonitorservice.dto.SectionFull; import eu.dnetlib.uoamonitorservice.entities.*; import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException; import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; +import eu.dnetlib.uoamonitorservice.primitives.Visibility; +import eu.dnetlib.uoamonitorservice.service.SectionService; +import eu.dnetlib.uoamonitorservice.service.SubCategoryService; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; @@ -14,7 +19,6 @@ import org.springframework.web.bind.annotation.*; import java.util.ArrayList; import java.util.Date; -import java.util.Iterator; import java.util.List; @RestController @@ -22,71 +26,22 @@ import java.util.List; public class SectionController { private final Logger log = LogManager.getLogger(this.getClass()); - @Autowired - private RolesUtils rolesUtils; - - @Autowired - private StakeholderDAO stakeholderDAO; - - @Autowired - private TopicDAO topicDAO; - - @Autowired - private CategoryDAO categoryDAO; - - @Autowired - private SubCategoryDAO subCategoryDAO; - - @Autowired - private SectionDAO sectionDAO; - - @Autowired - private IndicatorDAO indicatorDAO; - - @Autowired - private IndicatorController indicatorController; - - public Section buildSection(Section sectionFull) { - Section section = new Section<>(sectionFull); - - List indicators = new ArrayList<>(); - List indicatorsFull = new ArrayList<>(); - for(Indicator chart : sectionFull.getIndicators()) { - Indicator chartSaved = indicatorDAO.save(chart); - chart.setId(chartSaved.getId()); - indicatorsFull.add(chart); - indicators.add(chartSaved.getId()); - } - sectionFull.setIndicators(indicatorsFull); - section.setIndicators(indicators); - - Date date = new Date(); - section.setCreationDate(date); - section.setUpdateDate(date); - - sectionFull.setCreationDate(date); - sectionFull.setUpdateDate(date); - - sectionDAO.save(section); - - sectionFull.setId(section.getId()); - return sectionFull; - } + private SectionService service; @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/save/{index}", method = RequestMethod.POST) - public Section saveSection(@PathVariable("stakeholderId") String stakeholderId, - @PathVariable("topicId") String topicId, - @PathVariable("categoryId") String categoryId, - @PathVariable("subcategoryId") String subcategoryId, - @PathVariable("index") String index, - @RequestBody Section sectionFull) { + public SectionFull saveSection(@PathVariable("stakeholderId") String stakeholderId, + @PathVariable("topicId") String topicId, + @PathVariable("categoryId") String categoryId, + @PathVariable("subcategoryId") String subcategoryId, + @PathVariable("index") String index, + @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); + SubCategory subCategory = this.subCategoryService.checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId, "Save Section"); - Section section = new Section<>(sectionFull); + Section section = new Section(sectionFull); Date date = new Date(); section.setUpdateDate(date); @@ -123,7 +78,7 @@ public class SectionController { section.setIndicators(indicators); - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); + 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) { @@ -200,23 +155,13 @@ public class SectionController { private SubCategory checkForExceptions(String stakeholderId, String topicId, String categoryId, String subcategoryId) { - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); - - if(stakeholder == null) { - // EXCEPTION - Stakeholder not found - throw new EntityNotFoundException("Save indicator: Stakeholder with id: " + stakeholderId + " not found"); - } + Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId).orElseThrow(() -> new EntityNotFoundException("Save indicator: Stakeholder with id: " + stakeholderId + " not found")); if(!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { // EXCEPTION - Access denied throw new ForbiddenException("CheckForExceptions Section: You are not authorized to update stakeholder with id: "+stakeholderId); } - Topic topic = topicDAO.findById(topicId); - if(topic == null) { - // EXCEPTION - Topic not found - throw new EntityNotFoundException("Save indicator: Topic with id: "+topicId+" not found"); - } - + Topic topic = topicDAO.findById(topicId).orElseThrow(() -> new EntityNotFoundException("Save indicator: Topic with id: "+topicId+" not found")); if(!stakeholder.getTopics().contains(topicId)) { // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias(); throw new PathNotValidException("Save indicator: Topic with id: " + topicId + " not found in Stakeholder: " + stakeholderId); @@ -304,22 +249,5 @@ public class SectionController { sectionDAO.save(section); } } - - public Section changeVisibilityTree(String sectionId, Visibility visibility) { - Section section = sectionDAO.findById(sectionId); - if (section == null) { - // EXCEPTION - Section not found - throw new EntityNotFoundException("Change section visibility: Section with id: " + sectionId + " not found"); - } - - Section sectionFull = new Section(section); - List indicatorsFull = new ArrayList(); - - for (String indicatorId : section.getIndicators()) { - indicatorsFull.add(indicatorController.changeVisibilityTree(indicatorId, null, visibility)); - } - - sectionFull.setIndicators(indicatorsFull); - return sectionFull; - } } +*/ diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java index 8e2a33f..cb1f8ba 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java @@ -1,57 +1,33 @@ package eu.dnetlib.uoamonitorservice.controllers; -import eu.dnetlib.uoaadmintoolslibrary.entities.Portal; -import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils; -import eu.dnetlib.uoaadmintoolslibrary.services.PortalService; -import eu.dnetlib.uoamonitorservice.dao.*; -import eu.dnetlib.uoamonitorservice.entities.*; -import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException; +import eu.dnetlib.uoaadmintoolslibrary.services.PortalService; +import eu.dnetlib.uoamonitorservice.dto.StakeholderFull; +import eu.dnetlib.uoamonitorservice.entities.Stakeholder; import eu.dnetlib.uoamonitorservice.service.StakeholderService; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; - import org.springframework.security.access.AccessDeniedException; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; -import java.util.*; +import java.util.List; +import java.util.stream.Collectors; @RestController @CrossOrigin(origins = "*") public class StakeholderController { private final Logger log = LogManager.getLogger(this.getClass()); - @Autowired - private RolesUtils rolesUtils; - - @Autowired + private PortalService portalService; private StakeholderService stakeholderService; @Autowired - private StakeholderDAO stakeholderDAO; - - @Autowired - private TopicDAO topicDAO; - - @Autowired - private CategoryDAO categoryDAO; - - @Autowired - private SubCategoryDAO subCategoryDAO; - - @Autowired - private SectionDAO sectionDAO; - - @Autowired - private IndicatorDAO indicatorDAO; - - @Autowired - private TopicController topicController; - - @Autowired - private PortalService portalService; + public StakeholderController(PortalService portalService, StakeholderService stakeholderService) { + this.portalService = portalService; + this.stakeholderService = stakeholderService; + } @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/stakeholder/alias", method = RequestMethod.GET) @@ -63,221 +39,46 @@ public class StakeholderController { return stakeholderAlias; } -// @PreAuthorize("isAuthenticated()") - @PreAuthorize("hasAnyAuthority(" + - "@AuthorizationService.PORTAL_ADMIN, " + - "@AuthorizationService.curator(#stakeholderFull.getType()))") + /* @PreAuthorize("hasAnyAuthority(" + + "@AuthorizationService.PORTAL_ADMIN, " + + "@AuthorizationService.curator(#stakeholderFull.getType()))") @RequestMapping(value = "/build-stakeholder", method = RequestMethod.POST) - public Stakeholder>>>> buildFullStakeholder(@RequestBody Stakeholder>>>> stakeholderFull) { + public StakeholderFull buildFullStakeholder(@RequestBody StakeholderFull stakeholderFull) { log.debug("build stakeholder"); - log.debug("Alias: "+stakeholderFull.getAlias()); - - Stakeholder stakeholder = new Stakeholder<>(stakeholderFull); - - List topics = new ArrayList<>(); - List>>>> topicsFull = new ArrayList<>(); - for(Topic topic : stakeholderFull.getTopics()) { - Topic>>> topicFull = topicController.buildTopic(topic); - topicsFull.add(topicFull); - topics.add(topicFull.getId()); - } - stakeholderFull.setTopics(topicsFull); - stakeholder.setTopics(topics); - + log.debug("Alias: " + stakeholderFull.getAlias()); + Stakeholder stakeholder = new Stakeholder(stakeholderFull); + stakeholderFull.setTopics(stakeholderFull.getTopics().stream().map(topic -> topicController.buildTopic(topic))); + stakeholder.setTopics(stakeholderFull.getTopics().stream().map(TopicFull::getId).collect(Collectors.toList())); Date date = new Date(); stakeholder.setCreationDate(date); stakeholder.setUpdateDate(date); - stakeholderFull.setCreationDate(date); stakeholderFull.setUpdateDate(date); - - Stakeholder stakeholderSaved = stakeholderDAO.save(stakeholder); - stakeholderFull.setId(stakeholderSaved.getId()); - + stakeholder = stakeholderDAO.save(stakeholder); + stakeholderFull.setId(stakeholder.getId()); Portal portal = portalService.getPortal(stakeholderFull.getAlias()); - if(portal == null) { + if (portal == null) { portal = new Portal(); portal.setPid(stakeholderFull.getAlias()); portal.setName(stakeholderFull.getName()); portal.setType(stakeholderFull.getType()); portalService.insertPortal(portal); } - return stakeholderFull; - //return null; - } + }*/ - public Stakeholder setFullEntities(Stakeholder stakeholder) { - boolean addAll = false; - boolean addPublicAndRestricted = false; - - if(rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { - addAll = true; - addPublicAndRestricted = true; - } else if(rolesUtils.isMember(stakeholder.getType(), stakeholder.getAlias())) { - addPublicAndRestricted = true; - } - - Stakeholder stakeholderFull = new Stakeholder<>(stakeholder); - - List topics = new ArrayList<>(); - - for (String topicId: (List)stakeholder.getTopics()) { - Topic topic = topicDAO.findById(topicId); - if(topic == null) { - // EXCEPTION - Topic not found - throw new EntityNotFoundException("Get stakeholder: Topic with id: "+topicId+" not found (topic exists in stakeholder: "+stakeholder.getId()+")"); - } - - if((!addAll && topic.getVisibility() == Visibility.PRIVATE) - || (!addPublicAndRestricted && topic.getVisibility() == Visibility.RESTRICTED)) { - continue; - } - - Topic topicFull = new Topic(topic); - - List categories = new ArrayList<>(); - - for(String categoryId : topic.getCategories()) { - Category category = categoryDAO.findById(categoryId); - if(category == null) { - // EXCEPTION - Category not found - throw new EntityNotFoundException("Get stakeholder: Category with id: "+categoryId+" not found (category exists in topic: "+topicId+")"); - } - - if((!addAll && category.getVisibility() == Visibility.PRIVATE) - || (!addPublicAndRestricted && category.getVisibility() == Visibility.RESTRICTED)) { - continue; - } - - Category categoryFull = new Category(category); - - List subCategories = new ArrayList<>(); - - for(String subCategoryId : category.getSubCategories()) { - SubCategory subCategory = subCategoryDAO.findById(subCategoryId); - if(subCategory == null) { - // EXCEPTION - SubCategory not found - throw new EntityNotFoundException("Get stakeholder: SubCategory with id: "+subCategoryId+" not found (subCategory exists in category: "+categoryId+")"); - } - - if((!addAll && subCategory.getVisibility() == Visibility.PRIVATE) - || (!addPublicAndRestricted && subCategory.getVisibility() == Visibility.RESTRICTED)) { - continue; - } - - SubCategory subCategoryFull = new SubCategory>(subCategory); - - List
sectionsCharts = new ArrayList<>(); - - for(String sectionId : subCategory.getCharts()) { - sectionsCharts.add(getSectionFull(sectionId, subCategoryId, addAll, addPublicAndRestricted)); - } - subCategoryFull.setCharts(sectionsCharts); - - List
sectionsNumbers = new ArrayList<>(); - - for(String sectionId : subCategory.getNumbers()) { - sectionsNumbers.add(getSectionFull(sectionId, subCategoryId, addAll, addPublicAndRestricted)); - } - subCategoryFull.setNumbers(sectionsNumbers); - subCategories.add(subCategoryFull); - } - categoryFull.setSubCategories(subCategories); - categories.add(categoryFull); - } - topicFull.setCategories(categories); - topics.add(topicFull); - } - stakeholderFull.setTopics(topics); - return stakeholderFull; - } - - private Section getSectionFull(String sectionId, String subCategoryId, boolean addAll, boolean addPublicAndRestricted) { - Section section = sectionDAO.findById(sectionId); - if (section == null) { - // EXCEPTION - Section not found - throw new EntityNotFoundException("Get stakeholder: Section with id: " + sectionId + " not found (section exists in subCategory: " + subCategoryId + ")"); - } - - Section sectionFull = new Section(section); - - List indicators = new ArrayList<>(); - for (String indicatorId : section.getIndicators()) { - Indicator indicator = indicatorDAO.findById(indicatorId); - if (indicator == null) { - // EXCEPTION - Indicator not found - throw new EntityNotFoundException("Get stakeholder: Indicator with id: " + indicatorId + " not found (indicator exists in section: " + sectionId + ")"); - } - - if((!addAll && indicator.getVisibility() == Visibility.PRIVATE) - || (!addPublicAndRestricted && indicator.getVisibility() == Visibility.RESTRICTED)) { - continue; - } - - indicators.add(indicator); - } - sectionFull.setIndicators(indicators); - - return sectionFull; - } - - @PreAuthorize("hasAnyAuthority(" + - "@AuthorizationService.PORTAL_ADMIN)") + @PreAuthorize("hasAnyAuthority(" + "@AuthorizationService.PORTAL_ADMIN)") @RequestMapping(value = "/stakeholder/all", method = RequestMethod.GET) public List getAllStakeholders(@RequestParam(required = false) String type) { -// log.debug("get all stakeholders" + (type != null ? " with type: "+type : "")); - - List stakeholders; - if(type == null) { - stakeholders = stakeholderDAO.findAll(); - } else { - stakeholders = stakeholderDAO.findByType(type); - } - - List stakeholdersFull = new ArrayList<>(); - for(Stakeholder stakeholder : stakeholders) { - stakeholdersFull.add(this.setFullEntities(stakeholder)); - } - - return stakeholdersFull; + return this.stakeholderService.getAll(type); } @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/stakeholder/default", method = RequestMethod.GET) - public List getAllDefaultStakeholders(@RequestParam(required = false) String type) { -// log.debug("get all default stakeholders" + (type != null ? " with type: "+type : "")); - - List stakeholders; - if(type == null) { - stakeholders = stakeholderDAO.findByDefaultId(null); - } else { - stakeholders = stakeholderDAO.findByDefaultIdAndType(null, type); - } - - List stakeholdersFull = new ArrayList<>(); - - // Remove stakeholders for which i do not have authority - if(stakeholders != null && stakeholders.size() > 0) { - if (rolesUtils.isPortalAdmin()) { - for(Stakeholder stakeholder : stakeholders) { - stakeholdersFull.add(this.setFullEntities(stakeholder)); - } - return stakeholdersFull; - } - - Iterator stakeholderIterator = stakeholders.iterator(); - while(stakeholderIterator.hasNext()) { - Stakeholder stakeholder = stakeholderIterator.next(); - if(rolesUtils.isCurator(stakeholder.getType())) { - stakeholdersFull.add(this.setFullEntities(stakeholder)); - continue; - } - stakeholderIterator.remove(); - } - } - - return stakeholdersFull; + public List getAllDefaultStakeholders(@RequestParam(required = false) String type) { + return this.stakeholderService.getAllDefaultByRole(type).stream() + .map(stakeholder -> this.stakeholderService.getFullStakeholder(stakeholder)) + .collect(Collectors.toList()); } @RequestMapping(value = "/stakeholder", method = RequestMethod.GET) @@ -293,126 +94,98 @@ public class StakeholderController { } @RequestMapping(value = "/stakeholder/{alias:.+}", method = RequestMethod.GET) - public Stakeholder getStakeholder(@PathVariable("alias") String alias) { - - Stakeholder stakeholder = stakeholderDAO.findByAlias(alias); - if(stakeholder == null) { - // EXCEPTION - Stakeholder not found - throw new EntityNotFoundException("Get stakeholder: Stakeholder with alias: "+alias+" not found"); + public StakeholderFull getStakeholder(@PathVariable("alias") String alias) { + StakeholderFull stakeholder = this.stakeholderService.getFullStakeholder(this.stakeholderService.findByAlias(alias)); + if (stakeholder == null) { + throw new ForbiddenException("Get stakeholder: You are not authorized to access stakeholder with alias: " + alias); } - - if(stakeholder.getDefaultId() == null && !rolesUtils.isLoggedIn()) { - // EXCEPTION - Unauthorized - throw new AccessDeniedException("Get stakeholder: You are not authorized (not logged in) to access stakeholder with alias: "+alias); - } - if(stakeholder.getDefaultId() == null && !rolesUtils.hasCreateAndDeleteAuthority(stakeholder.getType())) { - // EXCEPTION - Access denied - throw new ForbiddenException("Get stakeholder: You are not authorized to access stakeholder with alias: "+alias); - } - - if((stakeholder.getVisibility() == Visibility.PRIVATE && !rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias()) - || (stakeholder.getVisibility() == Visibility.RESTRICTED && !rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias()) && !rolesUtils.isMember(stakeholder.getType(), stakeholder.getAlias())))) { -// // EXCEPTION - Access denied - List topicsEmpty = stakeholder.getTopics(); - topicsEmpty.clear(); - stakeholder.setTopics(topicsEmpty); - stakeholder.setVisibility(Visibility.PRIVATE); - return stakeholder; - } - - return this.setFullEntities(stakeholder); + return stakeholder; } - @PreAuthorize("hasAnyAuthority(" - + "@AuthorizationService.PORTAL_ADMIN, " - + "@AuthorizationService.curator(#_stakeholder.getType()), " - + "@AuthorizationService.manager(#_stakeholder.getType(), #_stakeholder.getAlias()) " - + ")") + /*@PreAuthorize("hasAnyAuthority(" + + "@AuthorizationService.PORTAL_ADMIN, " + + "@AuthorizationService.curator(#_stakeholder.getType()), " + + "@AuthorizationService.manager(#_stakeholder.getType(), #_stakeholder.getAlias()) " + + ")") @RequestMapping(value = "/save", method = RequestMethod.POST) - public Stakeholder saveStakeholder(@RequestBody Stakeholder _stakeholder) { + public Stakeholder saveStakeholder(@RequestBody Stakeholder stakeholder) { log.debug("save stakeholder"); - log.debug("Alias: "+_stakeholder.getAlias() + " - Id: "+_stakeholder.getId()); - Stakeholder stakeholder = new Stakeholder<>(_stakeholder); - + log.debug("Alias: " + stakeholder.getAlias() + " - Id: " + stakeholder.getId()); Date date = new Date(); stakeholder.setUpdateDate(date); List topics = new ArrayList<>(); - if(_stakeholder.getId() == null) { + if (stakeholder.getId() == null) { stakeholder.setCreationDate(date); } else { - Stakeholder oldStakeholder = stakeholderDAO.findById(_stakeholder.getId()); - if(oldStakeholder == null) { + Stakeholder oldStakeholder = stakeholderDAO.findById(stakeholder.getId()); + if (oldStakeholder == null) { // EXCEPTION - Stakeholder not found - throw new EntityNotFoundException("save stakeholder: Stakeholder with id: "+_stakeholder.getId()+" not found"); + throw new EntityNotFoundException("save stakeholder: Stakeholder with id: " + stakeholder.getId() + " not found"); } - for(String topicId : oldStakeholder.getTopics()) { + for (String topicId : oldStakeholder.getTopics()) { Topic topic = topicDAO.findById(topicId); if (topic == null) { // EXCEPTION - Topic not found - throw new EntityNotFoundException("Save stakeholder: Topic with id: "+topicId+" not found (topic exists in stakeholder: "+stakeholder.getId()+")"); + throw new EntityNotFoundException("Save stakeholder: Topic with id: " + topicId + " not found (topic exists in stakeholder: " + stakeholder.getId() + ")"); } topics.add(topic.getId()); } } stakeholder.setTopics(topics); - Stakeholder stakeholderSaved = stakeholderDAO.save(stakeholder); - _stakeholder.setId(stakeholderSaved.getId()); - _stakeholder.setCreationDate(stakeholderSaved.getCreationDate()); - _stakeholder.setUpdateDate(stakeholderSaved.getUpdateDate()); - return _stakeholder; - } + return stakeholderDAO.save(stakeholder); + }*/ - @PreAuthorize("isAuthenticated()") + /* @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/delete", method = RequestMethod.DELETE) public boolean deleteStakeholder(@PathVariable("stakeholderId") String stakeholderId) { log.debug("delete stakeholder"); - log.debug("Id: "+stakeholderId); + log.debug("Id: " + stakeholderId); - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); + Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); String pid = null; - if(stakeholder != null) { + if (stakeholder != null) { pid = stakeholder.getAlias(); - if(!rolesUtils.hasCreateAndDeleteAuthority(stakeholder.getType())) { + if (!rolesUtils.hasCreateAndDeleteAuthority(stakeholder.getType())) { // EXCEPTION - Access denied - throw new ForbiddenException("Delete stakeholder: You are not authorized to delete stakeholder with id: "+stakeholderId); + throw new ForbiddenException("Delete stakeholder: You are not authorized to delete stakeholder with id: " + stakeholderId); } topicController.deleteTree(stakeholder); stakeholder.setTopics(null); stakeholderDAO.delete(stakeholderId); log.debug("Stakeholder deleted!"); Portal portal = portalService.getPortal(pid); - if(portal != null) { + if (portal != null) { portalService.deletePortal(portal.getId()); } } else { // EXCEPTION - Stakeholder not found - throw new EntityNotFoundException("Delete stakeholder: Stakeholder with id: "+stakeholderId+" not found"); + throw new EntityNotFoundException("Delete stakeholder: Stakeholder with id: " + stakeholderId + " not found"); } return true; - } + }*/ - @PreAuthorize("isAuthenticated()") + /*@PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/change-visibility", method = RequestMethod.POST) public Stakeholder changeStakeholderVisibility(@PathVariable("stakeholderId") String stakeholderId, - @RequestParam("visibility") Visibility visibility, @RequestParam(required = false) Boolean propagate) { - log.debug("change stakeholder visibility: "+visibility + " - toggle propagate: "+((propagate != null && propagate) ? "true" : "false")); - log.debug("Stakeholder: "+stakeholderId); + @RequestParam("visibility") Visibility visibility, @RequestParam(required = false) Boolean propagate) { + log.debug("change stakeholder visibility: " + visibility + " - toggle propagate: " + ((propagate != null && propagate) ? "true" : "false")); + log.debug("Stakeholder: " + stakeholderId); Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); if (stakeholder == null) { // EXCEPTION - Stakeholder not found - throw new EntityNotFoundException("Change stakeholder visibility: Stakeholder with id: "+stakeholderId+" not found"); + throw new EntityNotFoundException("Change stakeholder visibility: Stakeholder with id: " + stakeholderId + " not found"); } - if(!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { + if (!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { // EXCEPTION - Access denied - throw new ForbiddenException("Change stakeholder visibility: You are not authorized to update stakeholder with id: "+stakeholderId); + throw new ForbiddenException("Change stakeholder visibility: You are not authorized to update stakeholder with id: " + stakeholderId); } return changeStakeholderVisibilityTree(stakeholder, visibility, propagate); } - private Stakeholder changeStakeholderVisibilityTree(Stakeholder stakeholder, Visibility visibility, Boolean propagate) { + private Stakeholder changeStakeholderVisibilityTree(Stakeholder stakeholder, Visibility visibility, Boolean propagate) { Stakeholder stakeholderFull = new Stakeholder<>(stakeholder); List topicsFull = new ArrayList<>(); @@ -430,5 +203,5 @@ public class StakeholderController { stakeholderFull.setTopics(topicsFull); return stakeholder; - } + }*/ } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SubCategoryController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SubCategoryController.java index aaa499b..7d86bb6 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SubCategoryController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/SubCategoryController.java @@ -1,3 +1,4 @@ +/* package eu.dnetlib.uoamonitorservice.controllers; import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils; @@ -6,6 +7,7 @@ import eu.dnetlib.uoamonitorservice.entities.*; import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException; import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; +import eu.dnetlib.uoamonitorservice.primitives.Visibility; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; @@ -145,7 +147,7 @@ public class SubCategoryController { subCategory.setCharts(chartSections); subCategory.setNumbers(numberSections); - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); + Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); if(stakeholder.getDefaultId() == null) { if(subcategoryFull.getId() == null) { subCategoryDAO.save(subCategory); @@ -230,7 +232,7 @@ public class SubCategoryController { SubCategory subcategory = subCategoryDAO.findById(subcategoryId); if(subcategory != null) { - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); + Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); if(subcategory.getDefaultId() != null && !rolesUtils.hasCreateAndDeleteAuthority(stakeholder.getType())) { // EXCEPTION - Access denied throw new ForbiddenException("Delete subcategory: You are not authorized to delete a default SubCategory in stakeholder with id: "+stakeholderId); @@ -344,10 +346,10 @@ public class SubCategoryController { @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/change-visibility", method = RequestMethod.POST) public SubCategory changeSubCategoryVisibility(@PathVariable("stakeholderId") String stakeholderId, - @PathVariable("topicId") String topicId, - @PathVariable("categoryId") String categoryId, - @PathVariable("subcategoryId") String subcategoryId, - @RequestParam("visibility") Visibility visibility, @RequestParam(required = false) Boolean propagate) { + @PathVariable("topicId") String topicId, + @PathVariable("categoryId") String categoryId, + @PathVariable("subcategoryId") String subcategoryId, + @RequestParam("visibility") Visibility visibility, @RequestParam(required = false) Boolean propagate) { log.debug("change subCategory visibility: "+visibility + " - toggle propagate: "+((propagate != null && propagate) ? "true" : "false")); log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId); @@ -398,7 +400,7 @@ public class SubCategoryController { private Category checkForExceptions(String stakeholderId, String topicId, String categoryId) { - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); + Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); if(stakeholder == null) { // EXCEPTION - Stakeholder not found @@ -464,3 +466,4 @@ public class SubCategoryController { } } } +*/ diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/TopicController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/TopicController.java index de3c423..4099a67 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/TopicController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/TopicController.java @@ -1,11 +1,14 @@ +/* package eu.dnetlib.uoamonitorservice.controllers; import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils; import eu.dnetlib.uoamonitorservice.dao.*; +import eu.dnetlib.uoamonitorservice.dto.TopicFull; import eu.dnetlib.uoamonitorservice.entities.*; import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException; import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; +import eu.dnetlib.uoamonitorservice.primitives.Visibility; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; @@ -37,9 +40,8 @@ public class TopicController { @Autowired private CategoryDAO categoryDAO; - public Topic buildTopic(Topic topicFull) { - Topic topic = new Topic<>(topicFull); - + public TopicFull buildTopic(TopicFull topicFull) { + Topic topic = new Topic(topicFull); List categories = new ArrayList<>(); List categoriesFull = new ArrayList<>(); for(Category category : topicFull.getCategories()) { @@ -70,7 +72,7 @@ public class TopicController { log.debug("save topic"); log.debug("Alias: "+topicFull.getAlias() + " - Id: "+topicFull.getId()+ " - Stakeholder: "+stakeholderId); - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); + Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); if(stakeholder != null) { if(!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { @@ -198,7 +200,7 @@ public class TopicController { log.debug("delete topic"); log.debug("Id: "+topicId + " - Stakeholder: "+stakeholderId); - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); + Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); if(stakeholder != null) { @@ -296,7 +298,7 @@ public class TopicController { log.debug("reorder topics"); log.debug("Stakeholder: "+stakeholderId); - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); + Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); if(stakeholder != null) { if(!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { @@ -335,12 +337,12 @@ public class TopicController { @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/{topicId}/change-visibility", method = RequestMethod.POST) public Topic changeTopicVisibility(@PathVariable("stakeholderId") String stakeholderId, - @PathVariable("topicId") String topicId, - @RequestParam("visibility") Visibility visibility, @RequestParam(required = false) Boolean propagate) { + @PathVariable("topicId") String topicId, + @RequestParam("visibility") Visibility visibility, @RequestParam(required = false) Boolean propagate) { log.debug("change topic visibility: "+visibility + " - toggle propagate: "+((propagate != null && propagate) ? "true" : "false")); log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId); - Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); + Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId); if (stakeholder != null) { if(!rolesUtils.hasUpdateAuthority(stakeholder.getType(), stakeholder.getAlias())) { @@ -399,3 +401,4 @@ public class TopicController { } } } +*/ diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dao/CategoryDAO.java b/src/main/java/eu/dnetlib/uoamonitorservice/dao/CategoryDAO.java index b8d4db0..eb99550 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dao/CategoryDAO.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dao/CategoryDAO.java @@ -5,15 +5,16 @@ import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.stereotype.Repository; import java.util.List; +import java.util.Optional; @Repository public interface CategoryDAO extends MongoRepository { List findAll(); List findByDefaultId(String DefaultId); - Category findBySubCategoriesContaining(String subCategory); + Optional findBySubCategoriesContaining(String subCategory); - Category findById(String Id); + Optional findById(String Id); void delete(String Id); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dao/IndicatorDAO.java b/src/main/java/eu/dnetlib/uoamonitorservice/dao/IndicatorDAO.java index 8940da8..e121bae 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dao/IndicatorDAO.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dao/IndicatorDAO.java @@ -5,13 +5,14 @@ import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.stereotype.Repository; import java.util.List; +import java.util.Optional; @Repository public interface IndicatorDAO extends MongoRepository { List findAll(); List findByDefaultId(String DefaultId); - Indicator findById(String Id); + Optional findById(String Id); void delete(String Id); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dao/SectionDAO.java b/src/main/java/eu/dnetlib/uoamonitorservice/dao/SectionDAO.java index cd24e5a..bc27f31 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dao/SectionDAO.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dao/SectionDAO.java @@ -5,13 +5,14 @@ import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.stereotype.Repository; import java.util.List; +import java.util.Optional; @Repository public interface SectionDAO extends MongoRepository { List
findAll(); List
findByDefaultId(String DefaultId); - Section findById(String Id); + Optional
findById(String Id); void delete(String Id); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dao/StakeholderDAO.java b/src/main/java/eu/dnetlib/uoamonitorservice/dao/StakeholderDAO.java index 113c105..2e9a14d 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dao/StakeholderDAO.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dao/StakeholderDAO.java @@ -5,6 +5,7 @@ import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.stereotype.Repository; import java.util.List; +import java.util.Optional; @Repository public interface StakeholderDAO extends MongoRepository { @@ -17,10 +18,10 @@ public interface StakeholderDAO extends MongoRepository { List findByDefaultIdNot(String DefaultId); List findByDefaultIdNotAndType(String DefaultId, String Type); - Stakeholder findByTopicsContaining(String topic); + Optional findByTopicsContaining(String topic); - Stakeholder findById(String Id); - Stakeholder findByAlias(String Alias); + Optional findById(String Id); + Optional findByAlias(String Alias); void delete(String Id); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dao/SubCategoryDAO.java b/src/main/java/eu/dnetlib/uoamonitorservice/dao/SubCategoryDAO.java index 2e22285..1b28c99 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dao/SubCategoryDAO.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dao/SubCategoryDAO.java @@ -5,13 +5,14 @@ import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.stereotype.Repository; import java.util.List; +import java.util.Optional; @Repository public interface SubCategoryDAO extends MongoRepository { List findAll(); List findByDefaultId(String DefaultId); - SubCategory findById(String Id); + Optional findById(String Id); void delete(String Id); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dao/TopicDAO.java b/src/main/java/eu/dnetlib/uoamonitorservice/dao/TopicDAO.java index f6a36fc..4683610 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dao/TopicDAO.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dao/TopicDAO.java @@ -5,15 +5,16 @@ import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.stereotype.Repository; import java.util.List; +import java.util.Optional; @Repository public interface TopicDAO extends MongoRepository { List findAll(); List findByDefaultId(String DefaultId); - Topic findByCategoriesContaining(String category); + Optional findByCategoriesContaining(String category); - Topic findById(String Id); + Optional findById(String Id); void delete(String Id); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dto/CategoryFull.java b/src/main/java/eu/dnetlib/uoamonitorservice/dto/CategoryFull.java new file mode 100644 index 0000000..3211e1d --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dto/CategoryFull.java @@ -0,0 +1,18 @@ +package eu.dnetlib.uoamonitorservice.dto; + +import eu.dnetlib.uoamonitorservice.generics.CategoryGeneric; + +import java.util.List; +import java.util.Objects; + +public class CategoryFull extends CategoryGeneric { + public CategoryFull() { + super(); + } + + public CategoryFull(CategoryGeneric category, List subCategories) { + super(category); + subCategories.removeIf(Objects::isNull); + this.setSubCategories(subCategories); + } +} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dto/SectionFull.java b/src/main/java/eu/dnetlib/uoamonitorservice/dto/SectionFull.java new file mode 100644 index 0000000..fc7f74b --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dto/SectionFull.java @@ -0,0 +1,19 @@ +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; + +public class SectionFull extends SectionGeneric { + public SectionFull() {} + + public SectionFull(Section section, List indicators) { + super(section); + this.setIndicators(indicators); + } +} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dto/StakeholderFull.java b/src/main/java/eu/dnetlib/uoamonitorservice/dto/StakeholderFull.java new file mode 100644 index 0000000..1db6f7a --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dto/StakeholderFull.java @@ -0,0 +1,15 @@ +package eu.dnetlib.uoamonitorservice.dto; + +import eu.dnetlib.uoamonitorservice.generics.StakeholderGeneric; + +import java.util.List; +import java.util.Objects; + +public class StakeholderFull extends StakeholderGeneric { + + public StakeholderFull(StakeholderGeneric stakeholder, List topics) { + super(stakeholder); + topics.removeIf(Objects::isNull); + this.setTopics(topics); + } +} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dto/SubCategoryFull.java b/src/main/java/eu/dnetlib/uoamonitorservice/dto/SubCategoryFull.java new file mode 100644 index 0000000..ca16ffd --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dto/SubCategoryFull.java @@ -0,0 +1,18 @@ +package eu.dnetlib.uoamonitorservice.dto; + +import eu.dnetlib.uoamonitorservice.generics.SubCategoryGeneric; + +import java.util.List; +import java.util.Objects; + +public class SubCategoryFull extends SubCategoryGeneric { + public SubCategoryFull() {} + + public SubCategoryFull(SubCategoryGeneric subCategory, List numbers, List charts) { + super(subCategory); + numbers.removeIf(Objects::isNull); + charts.removeIf(Objects::isNull); + this.setNumbers(numbers); + this.setCharts(charts); + } +} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dto/TopicFull.java b/src/main/java/eu/dnetlib/uoamonitorservice/dto/TopicFull.java new file mode 100644 index 0000000..99cf4c5 --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dto/TopicFull.java @@ -0,0 +1,18 @@ +package eu.dnetlib.uoamonitorservice.dto; + +import eu.dnetlib.uoamonitorservice.generics.TopicGeneric; + +import java.util.List; +import java.util.Objects; + +public class TopicFull extends TopicGeneric { + public TopicFull() { + super(); + } + + public TopicFull(TopicGeneric topic, List categories) { + super(topic); + categories.removeIf(Objects::isNull); + this.setCategories(categories); + } +} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Category.java b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Category.java index 6656b97..3eb926e 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Category.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Category.java @@ -1,129 +1,30 @@ package eu.dnetlib.uoamonitorservice.entities; -import com.fasterxml.jackson.annotation.JsonProperty; -import org.springframework.data.annotation.Id; +import eu.dnetlib.uoamonitorservice.dto.CategoryFull; +import eu.dnetlib.uoamonitorservice.generics.CategoryGeneric; +import eu.dnetlib.uoamonitorservice.generics.TopicGeneric; +import org.springframework.data.mongodb.core.mapping.Document; import java.util.ArrayList; -import java.util.Date; -import java.util.List; -public class Category { - @Id - @JsonProperty("_id") - private String id; - - private String name; - private String alias; - private String description; - private Visibility visibility = Visibility.PRIVATE; - private Date creationDate; - private Date updateDate; - private boolean isOverview; - private String defaultId; - private List subCategories; - - public Category() {} - public Category(Category category) { - id = category.getId(); - name = category.getName(); - alias = category.getAlias(); - description = category.getDescription(); - setVisibility(category.getVisibility()); - creationDate = category.getCreationDate(); - updateDate = category.getUpdateDate(); - isOverview = category.getIsOverview(); - defaultId = category.getDefaultId(); +@Document +public class Category extends CategoryGeneric { + public Category() { + super(); } - public void copyFromDefault(Category defaultCategory) { + public Category(CategoryFull category) { + super(category); + } + + public void copyFromDefault(CategoryGeneric defaultCategory) { setName(defaultCategory.getName()); setAlias(defaultCategory.getAlias()); setDescription(defaultCategory.getDescription()); setVisibility(defaultCategory.getVisibility()); setCreationDate(defaultCategory.getCreationDate()); setUpdateDate(defaultCategory.getUpdateDate()); - setIsOverview(defaultCategory.getIsOverview()); setDefaultId(defaultCategory.getId()); - setSubCategories(new ArrayList()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getAlias() { - return alias; - } - - public void setAlias(String alias) { - this.alias = alias; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public Visibility getVisibility() { - return visibility; - } - - public void setVisibility(Visibility visibility) { - this.visibility = visibility; - } - - public Date getCreationDate() { - return creationDate; - } - - public void setCreationDate(Date creationDate) { - this.creationDate = creationDate; - } - - public Date getUpdateDate() { - return updateDate; - } - - public void setUpdateDate(Date updateDate) { - this.updateDate = updateDate; - } - - public boolean getIsOverview() { - return isOverview; - } - - public void setIsOverview(boolean isOverview) { - this.isOverview = isOverview; - } - - public String getDefaultId() { - return defaultId; - } - - public void setDefaultId(String defaultId) { - this.defaultId = defaultId; - } - - public List getSubCategories() { - return subCategories; - } - - public void setSubCategories(List subCategories) { - this.subCategories = subCategories; + setSubCategories(new ArrayList<>()); } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Indicator.java b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Indicator.java index a4aa824..88467cb 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Indicator.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Indicator.java @@ -1,28 +1,30 @@ package eu.dnetlib.uoamonitorservice.entities; import com.fasterxml.jackson.annotation.JsonProperty; +import eu.dnetlib.uoamonitorservice.generics.Common; import eu.dnetlib.uoamonitorservice.primitives.IndicatorPath; import eu.dnetlib.uoamonitorservice.primitives.IndicatorSize; import eu.dnetlib.uoamonitorservice.primitives.IndicatorType; +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.Date; import java.util.List; -public class Indicator { +public class Indicator extends Common { @Id @JsonProperty("_id") private String id; - - private String name; - private String description; private String additionalDescription; private IndicatorType type; //number,chart private IndicatorSize width; //small,medium,large private IndicatorSize height = IndicatorSize.MEDIUM; //small,medium,large private List tags; // this field is not used anywhere now - private Visibility visibility = Visibility.PRIVATE; + @CreatedDate private Date creationDate; + @LastModifiedDate private Date updateDate; private String defaultId; private List indicatorPaths; @@ -50,22 +52,6 @@ public class Indicator { this.id = id; } - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - public String getAdditionalDescription() { return additionalDescription; } @@ -81,13 +67,6 @@ public class Indicator { return type.name(); } -// public String getStringType() { -// return type.name(); -// } - -// public void setType(IndicatorType type) { -// this.type = type; -// } public void setType(String type) { if(type == null) { this.type = null; @@ -121,14 +100,6 @@ public class Indicator { this.tags = tags; } - public Visibility getVisibility() { - return visibility; - } - - public void setVisibility(Visibility visibility) { - this.visibility = visibility; - } - public Date getCreationDate() { return creationDate; } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Section.java b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Section.java index c0664af..667e8bd 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Section.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Section.java @@ -1,115 +1,21 @@ package eu.dnetlib.uoamonitorservice.entities; -import com.fasterxml.jackson.annotation.JsonProperty; -import eu.dnetlib.uoamonitorservice.primitives.IndicatorType; -import org.springframework.data.annotation.Id; +import eu.dnetlib.uoamonitorservice.dto.SectionFull; +import eu.dnetlib.uoamonitorservice.generics.SectionGeneric; +import org.springframework.data.mongodb.core.mapping.Document; -import java.util.ArrayList; -import java.util.Date; import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; -public class Section { - @Id - @JsonProperty("_id") - private String id; - - private String title; - private String defaultId; - private String stakeholderAlias; - private IndicatorType type; - private Date creationDate; - private Date updateDate; - private List indicators; - +@Document +public class Section extends SectionGeneric { public Section() {} - public Section(Section section) { - id = section.getId(); - title = section.getTitle(); - defaultId = section.getDefaultId(); - stakeholderAlias = section.getStakeholderAlias(); - setType(section.getType()); - creationDate = section.getCreationDate(); - updateDate = section.getUpdateDate(); - } - - public void copyFromDefault(Section defaultSection) { - setTitle(defaultSection.getTitle()); - setType(defaultSection.getType()); - setDefaultId(defaultSection.id); - setCreationDate(defaultSection.getCreationDate()); - setUpdateDate(defaultSection.getUpdateDate()); - setIndicators(new ArrayList<>()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getTitle() { - return title; - } - - public void setTitle(String title) { - this.title = title; - } - - public String getDefaultId() { - return defaultId; - } - - public void setDefaultId(String defaultId) { - this.defaultId = defaultId; - } - - public String getStakeholderAlias() { - return stakeholderAlias; - } - - public void setStakeholderAlias(String stakeholderAlias) { - this.stakeholderAlias = stakeholderAlias; - } - - public String getType() { - if(type == null) { - return null; - } - return type.name(); - } - - public void setType(String type) { - if(type == null) { - this.type = null; - } else { - this.type = IndicatorType.valueOf(type); - } - } - - public Date getCreationDate() { - return creationDate; - } - - public void setCreationDate(Date creationDate) { - this.creationDate = creationDate; - } - - public Date getUpdateDate() { - return updateDate; - } - - public void setUpdateDate(Date updateDate) { - this.updateDate = updateDate; - } - - public List getIndicators() { - return indicators; - } - - public void setIndicators(List indicators) { - this.indicators = indicators; + public Section(SectionFull section) { + super(section); + List indicators = section.getIndicators().stream().map(Indicator::getId).collect(Collectors.toList()); + indicators.removeIf(Objects::isNull); + this.setIndicators(indicators); } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Stakeholder.java b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Stakeholder.java index 4d72840..bd66180 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Stakeholder.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Stakeholder.java @@ -1,224 +1,16 @@ package eu.dnetlib.uoamonitorservice.entities; -import com.fasterxml.jackson.annotation.JsonProperty; -import eu.dnetlib.uoamonitorservice.primitives.Locale; -import eu.dnetlib.uoamonitorservice.primitives.StakeholderType; -import org.springframework.data.annotation.Id; +import eu.dnetlib.uoamonitorservice.generics.StakeholderGeneric; +import org.springframework.data.mongodb.core.mapping.Document; -import java.util.Date; -import java.util.List; - - -public class Stakeholder { - @Id - @JsonProperty("_id") - private String id; - - private StakeholderType type; // private StakeholderType type; - private Date projectUpdateDate = null; - private String index_id; - private String index_name; - private String index_shortName; - private String statsProfile = "monitor"; - private String logoUrl; - private boolean isUpload = false; - private String name; - private String alias; - private String description; - private String defaultId = null; - private Locale locale = Locale.EU; - private Visibility visibility = Visibility.PRIVATE; - private String funderType; - private Date creationDate; - private Date updateDate; - - private List topics; +@Document +public class Stakeholder extends StakeholderGeneric { public Stakeholder() { - - } - public Stakeholder(Stakeholder stakeholder) { - id = stakeholder.getId(); - setType(stakeholder.getType()); - projectUpdateDate = stakeholder.getProjectUpdateDate(); - index_id = stakeholder.getIndex_id(); - index_name = stakeholder.getIndex_name(); - index_shortName = stakeholder.getIndex_shortName(); - statsProfile = stakeholder.getStatsProfile(); - logoUrl = stakeholder.getLogoUrl(); - isUpload = stakeholder.getIsUpload(); - name = stakeholder.getName(); - alias = stakeholder.getAlias(); - description = stakeholder.getDescription(); - defaultId = stakeholder.getDefaultId(); - setLocale(stakeholder.getLocale()); - setVisibility(stakeholder.getVisibility()); - this.funderType = stakeholder.getFunderType(); - creationDate = stakeholder.getCreationDate(); - updateDate = stakeholder.getUpdateDate(); + super(); } - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getType() { - if(type == null) { - return null; - } - return type.name(); - } - - public void setType(String type) { - if(type == null) { - this.type = null; - } else { - StakeholderType stakeholderType = StakeholderType.valueOf(type); - this.type = stakeholderType; - } - } - - public Date getProjectUpdateDate() { - return projectUpdateDate; - } - - public void setProjectUpdateDate(Date projectUpdateDate) { - this.projectUpdateDate = projectUpdateDate; - } - - public String getIndex_id() { - return index_id; - } - - public void setIndex_id(String index_id) { - this.index_id = index_id; - } - - public String getIndex_name() { - return index_name; - } - - public void setIndex_name(String index_name) { - this.index_name = index_name; - } - - public String getIndex_shortName() { - return index_shortName; - } - - public void setIndex_shortName(String index_shortName) { - this.index_shortName = index_shortName; - } - - public String getStatsProfile() { - return statsProfile; - } - - public void setStatsProfile(String statsProfile) { - this.statsProfile = statsProfile; - } - - public String getLogoUrl() { - return logoUrl; - } - - public void setLogoUrl(String logoUrl) { - this.logoUrl = logoUrl; - } - - public boolean getIsUpload() { - return isUpload; - } - - public void setIsUpload(boolean isUpload) { - this.isUpload = isUpload; - } - - public String getName() { return name; } - - public void setName(String name) { this.name = name; } - - public String getAlias() { - return alias; - } - - public void setAlias(String alias) { - this.alias = alias; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public String getDefaultId() { - return defaultId; - } - - public void setDefaultId(String defaultId) { - this.defaultId = defaultId; - } - - public String getLocale() { - return locale.label; - } - - public void setLocale(String label) { - Locale locale = null; - for (Locale l : Locale.values()) { - if (l.label.equals(label)) { - locale = l; - } - } - this.locale = locale; - } - - public Visibility getVisibility() { - //return visibility.getLabel(); - return visibility; - } - - public void setVisibility(Visibility visibility) { - //this.visibility = Visibility.get(visibility); - this.visibility = visibility; - } - - public String getFunderType() { - return funderType; - } - - public void setFunderType(String funderType) { - this.funderType = funderType; - } - - public Date getCreationDate() { - return creationDate; - } - - public void setCreationDate(Date creationDate) { - this.creationDate = creationDate; - } - - public Date getUpdateDate() { - return updateDate; - } - - public void setUpdateDate(Date updateDate) { - this.updateDate = updateDate; - } - - public List getTopics() { - return topics; - } - - public void setTopics(List topics) { - this.topics = topics; + public Stakeholder(StakeholderGeneric stakeholder) { + super(stakeholder); } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/SubCategory.java b/src/main/java/eu/dnetlib/uoamonitorservice/entities/SubCategory.java index fc1c242..7a67d66 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/SubCategory.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/entities/SubCategory.java @@ -1,138 +1,13 @@ package eu.dnetlib.uoamonitorservice.entities; -import com.fasterxml.jackson.annotation.JsonProperty; -import org.springframework.data.annotation.Id; - -import java.util.ArrayList; -import java.util.Date; -import java.util.List; - -public class SubCategory { - @Id - @JsonProperty("_id") - private String id; - - private String name; - private String alias; - private String description; - private Visibility visibility = Visibility.PRIVATE; - private Date creationDate; - private Date updateDate; - private String defaultId; - private List charts; - private List numbers; +import eu.dnetlib.uoamonitorservice.generics.SubCategoryGeneric; +import org.springframework.data.mongodb.core.mapping.Document; +@Document +public class SubCategory extends SubCategoryGeneric { public SubCategory() {} - public SubCategory(SubCategory subCategory) { - id = subCategory.getId(); - name = subCategory.getName(); - alias = subCategory.getAlias(); - description = subCategory.getDescription(); - setVisibility(subCategory.getVisibility()); - 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(SubCategory 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()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getAlias() { - return alias; - } - - public void setAlias(String alias) { - this.alias = alias; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public Visibility getVisibility() { - return visibility; - } - - public void setVisibility(Visibility visibility) { - this.visibility = visibility; - } - - public Date getCreationDate() { - return creationDate; - } - - public void setCreationDate(Date creationDate) { - this.creationDate = creationDate; - } - - public Date getUpdateDate() { - return updateDate; - } - - public void setUpdateDate(Date updateDate) { - this.updateDate = updateDate; - } - - public String getDefaultId() { - return defaultId; - } - - public void setDefaultId(String defaultId) { - this.defaultId = defaultId; - } - - public List getCharts() { - return charts; - } - - public void setCharts(List charts) { - this.charts = charts; - } - - public List getNumbers() { - return numbers; - } - - public void setNumbers(List numbers) { - this.numbers = numbers; + public SubCategory(SubCategoryGeneric subCategory) { + super(subCategory); } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Topic.java b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Topic.java index 1fa77ae..a235cf9 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Topic.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Topic.java @@ -1,129 +1,15 @@ package eu.dnetlib.uoamonitorservice.entities; -import com.fasterxml.jackson.annotation.JsonProperty; -import org.springframework.data.annotation.Id; +import eu.dnetlib.uoamonitorservice.generics.TopicGeneric; +import org.springframework.data.mongodb.core.mapping.Document; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; +@Document +public class Topic extends TopicGeneric { -public class Topic { - @Id - @JsonProperty("_id") - private String id; - - private String name; - private String alias; - private String description; - private String icon; - private Visibility visibility = Visibility.PRIVATE; - private Date creationDate; - private Date updateDate; - private String defaultId; - private List categories; - - public Topic() {} - public Topic(Topic topic) { - id = topic.getId(); - name = topic.getName(); - alias = topic.getAlias(); - description = topic.getDescription(); - icon = topic.getIcon(); - setVisibility(topic.getVisibility()); - creationDate = topic.getCreationDate(); - updateDate = topic.getUpdateDate(); - defaultId = topic.getDefaultId(); + public Topic() { + super(); } - - public void copyFromDefault(Topic defaultTopic) { - setName(defaultTopic.getName()); - setAlias(defaultTopic.getAlias()); - setDescription(defaultTopic.getDescription()); - setIcon(defaultTopic.getIcon()); - setVisibility(defaultTopic.getVisibility()); - setCreationDate(defaultTopic.getCreationDate()); - setUpdateDate(defaultTopic.getUpdateDate()); - setDefaultId(defaultTopic.getId()); - setCategories(new ArrayList()); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getAlias() { - return alias; - } - - public void setAlias(String alias) { - this.alias = alias; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public String getIcon() { - return icon; - } - - public void setIcon(String icon) { - this.icon = icon; - } - - public Visibility getVisibility() { - return visibility; - } - - public void setVisibility(Visibility visibility) { - this.visibility = visibility; - } - - public Date getCreationDate() { - return creationDate; - } - - public void setCreationDate(Date creationDate) { - this.creationDate = creationDate; - } - - public Date getUpdateDate() { - return updateDate; - } - - public void setUpdateDate(Date updateDate) { - this.updateDate = updateDate; - } - - public String getDefaultId() { - return defaultId; - } - - public void setDefaultId(String defaultId) { - this.defaultId = defaultId; - } - - public List getCategories() { - return categories; - } - - public void setCategories(List categories) { - this.categories = categories; + public Topic(TopicGeneric topic) { + super(topic); } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Visibility.java b/src/main/java/eu/dnetlib/uoamonitorservice/entities/Visibility.java deleted file mode 100644 index 50b655c..0000000 --- a/src/main/java/eu/dnetlib/uoamonitorservice/entities/Visibility.java +++ /dev/null @@ -1,38 +0,0 @@ -package eu.dnetlib.uoamonitorservice.entities; - -import java.util.HashMap; -import java.util.Map; - -public enum Visibility { - // Do not rename or remove existring values. This may cause problems with already stored values in DB - // PUBLIC("public"), RESTRICTED("restricted"), PRIVATE("private"); - - PUBLIC, RESTRICTED, PRIVATE -// private String label; -// -// private Visibility(String label) { -// this.label = label; -// } -// -// public String getLabel() { -// return this.label; -// } -// -// //Lookup table -// private static final Map lookup = new HashMap<>(); -// -// //Populate the lookup table on loading time -// static -// { -// for(Visibility visibility : Visibility.values()) -// { -// lookup.put(visibility.getLabel(), visibility); -// } -// } -// -// //This method can be used for reverse lookup purpose -// public static Visibility get(String visibility) -// { -// return lookup.get(visibility); -// } -} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/generics/CategoryGeneric.java b/src/main/java/eu/dnetlib/uoamonitorservice/generics/CategoryGeneric.java new file mode 100644 index 0000000..0ba9657 --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/generics/CategoryGeneric.java @@ -0,0 +1,88 @@ +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 org.springframework.data.mongodb.core.mapping.Document; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +@Document +public class CategoryGeneric extends Common { + @Id + @JsonProperty("_id") + protected String id; + @CreatedDate + protected Date creationDate; + @LastModifiedDate + protected Date updateDate; + protected boolean isOverview; + protected String defaultId; + protected List subCategories; + + public CategoryGeneric() {} + + public CategoryGeneric(CategoryGeneric category) { + id = category.getId(); + name = category.getName(); + alias = category.getAlias(); + description = category.getDescription(); + setVisibility(category.getVisibility()); + creationDate = category.getCreationDate(); + updateDate = category.getUpdateDate(); + defaultId = category.getDefaultId(); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getCreationDate() { + return creationDate; + } + + public void setCreationDate(Date creationDate) { + this.creationDate = creationDate; + } + + public Date getUpdateDate() { + return updateDate; + } + + public void setUpdateDate(Date updateDate) { + this.updateDate = updateDate; + } + + public boolean getIsOverview() { + return isOverview; + } + + public void setIsOverview(boolean isOverview) { + this.isOverview = isOverview; + } + + public String getDefaultId() { + return defaultId; + } + + public void setDefaultId(String defaultId) { + this.defaultId = defaultId; + } + + public List getSubCategories() { + return subCategories; + } + + public void setSubCategories(List subCategories) { + this.subCategories = subCategories; + } +} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/generics/Common.java b/src/main/java/eu/dnetlib/uoamonitorservice/generics/Common.java new file mode 100644 index 0000000..35cb80b --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/generics/Common.java @@ -0,0 +1,42 @@ +package eu.dnetlib.uoamonitorservice.generics; + +import eu.dnetlib.uoamonitorservice.primitives.Visibility; + +public class Common { + protected String name; + protected String description; + protected String alias; + protected Visibility visibility = Visibility.PRIVATE; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getAlias() { + return alias; + } + + public void setAlias(String alias) { + this.alias = alias; + } + + public Visibility getVisibility() { + return visibility; + } + + public void setVisibility(Visibility visibility) { + this.visibility = visibility; + } +} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/generics/SectionGeneric.java b/src/main/java/eu/dnetlib/uoamonitorservice/generics/SectionGeneric.java new file mode 100644 index 0000000..995ac50 --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/generics/SectionGeneric.java @@ -0,0 +1,119 @@ +package eu.dnetlib.uoamonitorservice.generics; + +import com.fasterxml.jackson.annotation.JsonProperty; +import eu.dnetlib.uoamonitorservice.primitives.IndicatorType; +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 SectionGeneric { + @Id + @JsonProperty("_id") + protected String id; + protected String title; + protected String defaultId; + protected String stakeholderAlias; + protected IndicatorType type; + @CreatedDate + protected Date creationDate; + @LastModifiedDate + protected Date updateDate; + protected List indicators; + + public SectionGeneric() { + } + + public SectionGeneric(SectionGeneric section) { + id = section.getId(); + title = section.getTitle(); + defaultId = section.getDefaultId(); + stakeholderAlias = section.getStakeholderAlias(); + setType(section.getType()); + creationDate = section.getCreationDate(); + updateDate = section.getUpdateDate(); + } + + public void copyFromDefault(SectionGeneric defaultSection) { + setTitle(defaultSection.getTitle()); + setType(defaultSection.getType()); + setDefaultId(defaultSection.id); + setCreationDate(defaultSection.getCreationDate()); + setUpdateDate(defaultSection.getUpdateDate()); + setIndicators(new ArrayList<>()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getDefaultId() { + return defaultId; + } + + public void setDefaultId(String defaultId) { + this.defaultId = defaultId; + } + + public String getStakeholderAlias() { + return stakeholderAlias; + } + + public void setStakeholderAlias(String stakeholderAlias) { + this.stakeholderAlias = stakeholderAlias; + } + + public String getType() { + if (type == null) { + return null; + } + return type.name(); + } + + public void setType(String type) { + if (type == null) { + this.type = null; + } else { + this.type = IndicatorType.valueOf(type); + } + } + + public Date getCreationDate() { + return creationDate; + } + + public void setCreationDate(Date creationDate) { + this.creationDate = creationDate; + } + + public Date getUpdateDate() { + return updateDate; + } + + public void setUpdateDate(Date updateDate) { + this.updateDate = updateDate; + } + + public List getIndicators() { + return indicators; + } + + public void setIndicators(List indicators) { + this.indicators = indicators; + } +} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/generics/StakeholderGeneric.java b/src/main/java/eu/dnetlib/uoamonitorservice/generics/StakeholderGeneric.java new file mode 100644 index 0000000..a5912b9 --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/generics/StakeholderGeneric.java @@ -0,0 +1,193 @@ +package eu.dnetlib.uoamonitorservice.generics; + +import com.fasterxml.jackson.annotation.JsonProperty; +import eu.dnetlib.uoamonitorservice.primitives.Locale; +import eu.dnetlib.uoamonitorservice.primitives.StakeholderType; +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.Date; +import java.util.List; + + +public class StakeholderGeneric extends Common { + @Id + @JsonProperty("_id") + protected String id; + protected StakeholderType type; + protected Date projectUpdateDate = null; + protected String index_id; + protected String index_name; + protected String index_shortName; + protected String statsProfile = "monitor"; + protected String logoUrl; + protected boolean isUpload = false; + protected String defaultId = null; + protected Locale locale = Locale.EU; + protected String funderType; + @CreatedDate + protected Date creationDate; + @LastModifiedDate + protected Date updateDate; + protected List topics; + + public StakeholderGeneric() { + } + + public StakeholderGeneric(StakeholderGeneric stakeholder) { + id = stakeholder.getId(); + setType(stakeholder.getType()); + projectUpdateDate = stakeholder.getProjectUpdateDate(); + index_id = stakeholder.getIndex_id(); + index_name = stakeholder.getIndex_name(); + index_shortName = stakeholder.getIndex_shortName(); + statsProfile = stakeholder.getStatsProfile(); + logoUrl = stakeholder.getLogoUrl(); + isUpload = stakeholder.getIsUpload(); + name = stakeholder.getName(); + alias = stakeholder.getAlias(); + description = stakeholder.getDescription(); + defaultId = stakeholder.getDefaultId(); + setLocale(stakeholder.getLocale()); + setVisibility(stakeholder.getVisibility()); + this.funderType = stakeholder.getFunderType(); + creationDate = stakeholder.getCreationDate(); + updateDate = stakeholder.getUpdateDate(); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getType() { + if (type == null) { + return null; + } + return type.name(); + } + + public void setType(String type) { + if (type == null) { + this.type = null; + } else { + StakeholderType stakeholderType = StakeholderType.valueOf(type); + this.type = stakeholderType; + } + } + + public Date getProjectUpdateDate() { + return projectUpdateDate; + } + + public void setProjectUpdateDate(Date projectUpdateDate) { + this.projectUpdateDate = projectUpdateDate; + } + + public String getIndex_id() { + return index_id; + } + + public void setIndex_id(String index_id) { + this.index_id = index_id; + } + + public String getIndex_name() { + return index_name; + } + + public void setIndex_name(String index_name) { + this.index_name = index_name; + } + + public String getIndex_shortName() { + return index_shortName; + } + + public void setIndex_shortName(String index_shortName) { + this.index_shortName = index_shortName; + } + + public String getStatsProfile() { + return statsProfile; + } + + public void setStatsProfile(String statsProfile) { + this.statsProfile = statsProfile; + } + + public String getLogoUrl() { + return logoUrl; + } + + public void setLogoUrl(String logoUrl) { + this.logoUrl = logoUrl; + } + + public boolean getIsUpload() { + return isUpload; + } + + public void setIsUpload(boolean isUpload) { + this.isUpload = isUpload; + } + + public String getDefaultId() { + return defaultId; + } + + public void setDefaultId(String defaultId) { + this.defaultId = defaultId; + } + + public String getLocale() { + return locale.label; + } + + public void setLocale(String label) { + Locale locale = null; + for (Locale l : Locale.values()) { + if (l.label.equals(label)) { + locale = l; + } + } + this.locale = locale; + } + + public String getFunderType() { + return funderType; + } + + public void setFunderType(String funderType) { + this.funderType = funderType; + } + + public Date getCreationDate() { + return creationDate; + } + + public void setCreationDate(Date creationDate) { + this.creationDate = creationDate; + } + + public Date getUpdateDate() { + return updateDate; + } + + public void setUpdateDate(Date updateDate) { + this.updateDate = updateDate; + } + + public List getTopics() { + return topics; + } + + public void setTopics(List topics) { + this.topics = topics; + } +} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/generics/SubCategoryGeneric.java b/src/main/java/eu/dnetlib/uoamonitorservice/generics/SubCategoryGeneric.java new file mode 100644 index 0000000..c285d17 --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/generics/SubCategoryGeneric.java @@ -0,0 +1,107 @@ +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 { + @Id + @JsonProperty("_id") + protected String id; + @CreatedDate + protected Date creationDate; + @LastModifiedDate + protected Date updateDate; + protected String defaultId; + protected List charts; + protected List numbers; + + public SubCategoryGeneric() {} + public SubCategoryGeneric(SubCategoryGeneric subCategory) { + id = subCategory.getId(); + name = subCategory.getName(); + alias = subCategory.getAlias(); + description = subCategory.getDescription(); + setVisibility(subCategory.getVisibility()); + 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()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Date getCreationDate() { + return creationDate; + } + + public void setCreationDate(Date creationDate) { + this.creationDate = creationDate; + } + + public Date getUpdateDate() { + return updateDate; + } + + public void setUpdateDate(Date updateDate) { + this.updateDate = updateDate; + } + + public String getDefaultId() { + return defaultId; + } + + public void setDefaultId(String defaultId) { + this.defaultId = defaultId; + } + + public List getCharts() { + return charts; + } + + public void setCharts(List charts) { + this.charts = charts; + } + + public List getNumbers() { + return numbers; + } + + public void setNumbers(List numbers) { + this.numbers = numbers; + } +} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/generics/TopicGeneric.java b/src/main/java/eu/dnetlib/uoamonitorservice/generics/TopicGeneric.java new file mode 100644 index 0000000..0ea1693 --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/generics/TopicGeneric.java @@ -0,0 +1,100 @@ +package eu.dnetlib.uoamonitorservice.generics; + +import com.fasterxml.jackson.annotation.JsonProperty; +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 TopicGeneric extends Common { + @Id + @JsonProperty("_id") + protected String id; + protected String icon; + @CreatedDate + protected Date creationDate; + @LastModifiedDate + protected Date updateDate; + protected String defaultId; + protected List categories; + + public TopicGeneric() { + } + + public TopicGeneric(TopicGeneric topic) { + id = topic.getId(); + name = topic.getName(); + alias = topic.getAlias(); + description = topic.getDescription(); + icon = topic.getIcon(); + setVisibility(topic.getVisibility()); + creationDate = topic.getCreationDate(); + updateDate = topic.getUpdateDate(); + defaultId = topic.getDefaultId(); + } + + public void copyFromDefault(TopicGeneric defaultTopic) { + setName(defaultTopic.getName()); + setAlias(defaultTopic.getAlias()); + setDescription(defaultTopic.getDescription()); + setIcon(defaultTopic.getIcon()); + setVisibility(defaultTopic.getVisibility()); + setCreationDate(defaultTopic.getCreationDate()); + setUpdateDate(defaultTopic.getUpdateDate()); + setDefaultId(defaultTopic.getId()); + setCategories(new ArrayList<>()); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + + public String getIcon() { + return icon; + } + + public void setIcon(String icon) { + this.icon = icon; + } + + public Date getCreationDate() { + return creationDate; + } + + public void setCreationDate(Date creationDate) { + this.creationDate = creationDate; + } + + public Date getUpdateDate() { + return updateDate; + } + + public void setUpdateDate(Date updateDate) { + this.updateDate = updateDate; + } + + public String getDefaultId() { + return defaultId; + } + + public void setDefaultId(String defaultId) { + this.defaultId = defaultId; + } + + public List getCategories() { + return categories; + } + + public void setCategories(List categories) { + this.categories = categories; + } +} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/primitives/Visibility.java b/src/main/java/eu/dnetlib/uoamonitorservice/primitives/Visibility.java new file mode 100644 index 0000000..575482c --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/primitives/Visibility.java @@ -0,0 +1,5 @@ +package eu.dnetlib.uoamonitorservice.primitives; + +public enum Visibility { + PUBLIC, RESTRICTED, PRIVATE +} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/CategoryService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/CategoryService.java new file mode 100644 index 0000000..cfb8e35 --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/CategoryService.java @@ -0,0 +1,52 @@ +package eu.dnetlib.uoamonitorservice.service; + +import eu.dnetlib.uoamonitorservice.dao.CategoryDAO; +import eu.dnetlib.uoamonitorservice.dto.CategoryFull; +import eu.dnetlib.uoamonitorservice.dto.TopicFull; +import eu.dnetlib.uoamonitorservice.entities.Category; +import eu.dnetlib.uoamonitorservice.entities.Topic; +import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; +import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.access.method.P; +import org.springframework.stereotype.Service; + +import java.util.stream.Collectors; + +@Service +public class CategoryService { + + private final CategoryDAO dao; + + private final SubCategoryService subCategoryService; + private final CommonService commonService; + + @Autowired + public CategoryService(SubCategoryService subCategoryService, CommonService commonService, CategoryDAO dao) { + this.subCategoryService = subCategoryService; + this.commonService = commonService; + this.dao = dao; + } + + public Category findByPath(Topic topic, String categoryId, String message) { + if (!topic.getCategories().contains(categoryId)) { + throw new PathNotValidException(message + ": Category with id: " + categoryId + " not found in Topic: " + topic.getId()); + } + return this.dao.findById(categoryId).orElseThrow(() -> new EntityNotFoundException(message + ": Category with id: " + categoryId + " not found")); + } + + public CategoryFull getFullCategory(String type, String alias, String categoryId) { + Category category = this.find(categoryId); + if(commonService.hasVisibilityAuthority(type, alias, category)) { + return new CategoryFull(category, category.getSubCategories().stream() + .map(subCategoryId -> this.subCategoryService.getFullSubCategory(type, alias, subCategoryId)) + .collect(Collectors.toList())); + } else { + return null; + } + } + + public Category find(String id) { + return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("Category with id: " + id + " not found")); + } +} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/CommonService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/CommonService.java new file mode 100644 index 0000000..cf43df1 --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/CommonService.java @@ -0,0 +1,37 @@ +package eu.dnetlib.uoamonitorservice.service; + +import eu.dnetlib.uoaauthorizationlibrary.security.AuthorizationService; +import eu.dnetlib.uoamonitorservice.generics.Common; +import eu.dnetlib.uoamonitorservice.primitives.Visibility; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class CommonService { + + AuthorizationService authorizationService; + + @Autowired + public CommonService(AuthorizationService authorizationService) { + this.authorizationService = authorizationService; + } + + public boolean hasAccessAuthority(String type, String alias, boolean isDefault) { + if(this.authorizationService.isPortalAdmin() || this.authorizationService.isCurator(type)) { + return true; + } else if(!isDefault) { + return this.authorizationService.isManager(type, alias); + } + return false; + } + + public boolean hasVisibilityAuthority(String type, String alias, Common common) { + if(authorizationService.isPortalAdmin() || authorizationService.isCurator(type) || authorizationService.isManager(type, alias)) { + return true; + } else if(authorizationService.isMember(type, alias) && common.getVisibility() != Visibility.PRIVATE) { + return true; + } else { + return common.getVisibility() == Visibility.PUBLIC; + } + } +} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/IndicatorService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/IndicatorService.java new file mode 100644 index 0000000..06ed3d5 --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/IndicatorService.java @@ -0,0 +1,38 @@ +package eu.dnetlib.uoamonitorservice.service; + +import eu.dnetlib.uoamonitorservice.dao.IndicatorDAO; +import eu.dnetlib.uoamonitorservice.entities.Indicator; +import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class IndicatorService { + + private final IndicatorDAO dao; + + private final CommonService commonService; + + @Autowired + public IndicatorService(IndicatorDAO dao, CommonService commonService) { + this.dao = dao; + this.commonService = commonService; + } + + public Indicator save(Indicator indicator) { + return this.dao.save(indicator); + } + + public Indicator find(String id) { + return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("Indicator with id: " + id + " not found")); + } + + public Indicator getIndicator(String type, String alias, String id) { + Indicator indicator = this.find(id); + if(this.commonService.hasVisibilityAuthority(type, alias, indicator)) { + return indicator; + } else { + return null; + } + } +} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/SectionService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/SectionService.java new file mode 100644 index 0000000..40f9c7f --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/SectionService.java @@ -0,0 +1,71 @@ +package eu.dnetlib.uoamonitorservice.service; + +import eu.dnetlib.uoamonitorservice.dao.SectionDAO; +import eu.dnetlib.uoamonitorservice.dto.SectionFull; +import eu.dnetlib.uoamonitorservice.entities.Indicator; +import eu.dnetlib.uoamonitorservice.entities.Section; +import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; +import eu.dnetlib.uoamonitorservice.primitives.Visibility; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.stream.Collectors; + +@Service +public class SectionService { + + private final SectionDAO dao; + + private final IndicatorService indicatorService; + + @Autowired + public SectionService(SectionDAO dao, IndicatorService indicatorService) { + this.dao = dao; + this.indicatorService = indicatorService; + } + + public SectionFull buildSection(SectionFull sectionFull) { + sectionFull.setIndicators(sectionFull.getIndicators().stream().map(indicator -> this.indicatorService.save(indicator)).collect(Collectors.toList())); + Section section = dao.save(new Section(sectionFull)); + return new SectionFull(section, sectionFull.getIndicators()); + } + + public SectionFull changeVisibilityTree(String sectionId, Visibility visibility) { + Section section = this.find(sectionId); + SectionFull sectionFull = new SectionFull(section, section.getIndicators().stream().map(indicatorId -> this.indicatorService.find(indicatorId)).collect(Collectors.toList())); + sectionFull.setIndicators(sectionFull.getIndicators().stream().map(indicator -> { + indicator.setVisibility(visibility); + return indicatorService.save(indicator); + }).collect(Collectors.toList())); + return sectionFull; + } + + public SectionFull saveSection(SectionFull sectionFull) { + Section section = new Section(sectionFull); + if(section.getId() != null) { + section.setIndicators(this.getIndicatorsId(section.getId())); + } + return sectionFull; + } + + 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); + return new SectionFull(section, section.getIndicators().stream() + .map(indicatorId -> this.indicatorService.getIndicator(type, alias, indicatorId)) + .collect(Collectors.toList())); + } + + public List getIndicators(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()); + } +} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java index 61b4ba4..fc94b40 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java @@ -1,9 +1,11 @@ package eu.dnetlib.uoamonitorservice.service; -import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils; +import eu.dnetlib.uoaauthorizationlibrary.security.AuthorizationService; import eu.dnetlib.uoamonitorservice.dao.StakeholderDAO; +import eu.dnetlib.uoamonitorservice.dto.StakeholderFull; import eu.dnetlib.uoamonitorservice.entities.Stakeholder; -import eu.dnetlib.uoamonitorservice.entities.Visibility; +import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; +import eu.dnetlib.uoamonitorservice.primitives.Visibility; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -13,16 +15,39 @@ import java.util.stream.Collectors; @Service public class StakeholderService { - @Autowired - StakeholderDAO dao; + private final StakeholderDAO dao; + + private final CommonService commonService; + private final TopicService topicService; @Autowired - RolesUtils rolesUtils; + public StakeholderService(StakeholderDAO dao, CommonService commonService, TopicService topicService) { + this.dao = dao; + this.commonService = commonService; + this.topicService = topicService; + } + + public Stakeholder findByAlias(String alias) { + return this.dao.findByAlias(alias).orElseThrow(() -> new EntityNotFoundException("Stakeholder with alias: " + alias + " not found")); + } public List getAllAliases() { return this.dao.findAll().stream().map(Stakeholder::getAlias).collect(Collectors.toList()); } + public List getAll(String type) { + if(type != null) { + return this.dao.findByType(type); + } + return this.dao.findAll(); + } + + public List getAllDefaultByRole(String type) { + return (type == null?this.dao.findByDefaultId(null):this.dao.findByDefaultIdAndType(null, type)).stream() + .filter(stakeholder -> this.commonService.hasAccessAuthority(stakeholder.getType(), stakeholder.getAlias(), true)) + .collect(Collectors.toList()); + } + public List getStakeholdersByTypeAndRole(String type, String defaultId, boolean manage) { List stakeholders; if(type != null && defaultId != null) { @@ -35,12 +60,24 @@ public class StakeholderService { stakeholders = dao.findByDefaultIdNot(null); } return stakeholders.stream().filter(stakeholder -> - (!manage && - (stakeholder.getVisibility() == Visibility.PUBLIC - || stakeholder.getVisibility() == Visibility.RESTRICTED)) - || rolesUtils.isPortalAdmin() - || rolesUtils.isCurator(stakeholder.getType()) - || rolesUtils.isManager(stakeholder.getType(), stakeholder.getId())) + (!manage && (stakeholder.getVisibility() == Visibility.PUBLIC || stakeholder.getVisibility() == Visibility.RESTRICTED)) + || this.commonService.hasAccessAuthority(stakeholder.getType(), stakeholder.getAlias(), false)) .collect(Collectors.toList()); } + + public Stakeholder findByPath(String stakeholderId) { + return dao.findById(stakeholderId).orElseThrow(() -> new EntityNotFoundException("Stakeholder with id: " + stakeholderId + " not found")); + } + + 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))) { + return new StakeholderFull(stakeholder, + stakeholder.getTopics().stream() + .map(topicId -> topicService.getFullTopic(stakeholder.getType(), stakeholder.getAlias(), topicId)) + .collect(Collectors.toList())); + } else { + return null; + } + } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/SubCategoryService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/SubCategoryService.java new file mode 100644 index 0000000..5307049 --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/SubCategoryService.java @@ -0,0 +1,55 @@ +package eu.dnetlib.uoamonitorservice.service; + +import eu.dnetlib.uoamonitorservice.dao.SubCategoryDAO; +import eu.dnetlib.uoamonitorservice.dto.SubCategoryFull; +import eu.dnetlib.uoamonitorservice.dto.TopicFull; +import eu.dnetlib.uoamonitorservice.entities.Category; +import eu.dnetlib.uoamonitorservice.entities.SubCategory; +import eu.dnetlib.uoamonitorservice.entities.Topic; +import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; +import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.stream.Collectors; + +@Service +public class SubCategoryService { + + private final SubCategoryDAO dao; + + private final CommonService commonService; + private final SectionService sectionService; + + @Autowired + public SubCategoryService( CommonService commonService, SubCategoryDAO dao, SectionService sectionService) { + this.commonService = commonService; + this.dao = dao; + this.sectionService = sectionService; + } + + public SubCategory findByPath(Category category, String subcategoryId, String message) { + if (!category.getSubCategories().contains(subcategoryId)) { + throw new PathNotValidException(message + ": SubCategory with id: " + subcategoryId + " not found in Category: " + category.getId()); + } + return this.dao.findById(subcategoryId).orElseThrow(() -> new EntityNotFoundException(message + ": SubCategory with id: " + subcategoryId + " not found")); + } + + public SubCategoryFull getFullSubCategory(String type, String alias, String subCategoryId) { + SubCategory subCategory = this.find(subCategoryId); + if(commonService.hasVisibilityAuthority(type, alias, subCategory)) { + return new SubCategoryFull(subCategory, subCategory.getNumbers().stream() + .map(sectionId -> this.sectionService.getFullSection(type, alias, sectionId)) + .collect(Collectors.toList()), + subCategory.getCharts().stream() + .map(sectionId -> this.sectionService.getFullSection(type, alias, sectionId)) + .collect(Collectors.toList())); + } else { + return null; + } + } + + public SubCategory find(String id) { + return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("SubCategory with id: " + id + " not found")); + } +} diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/TopicService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/TopicService.java new file mode 100644 index 0000000..1c0024e --- /dev/null +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/TopicService.java @@ -0,0 +1,53 @@ +package eu.dnetlib.uoamonitorservice.service; + +import eu.dnetlib.uoamonitorservice.dao.TopicDAO; +import eu.dnetlib.uoamonitorservice.dto.TopicFull; +import eu.dnetlib.uoamonitorservice.entities.Indicator; +import eu.dnetlib.uoamonitorservice.entities.Stakeholder; +import eu.dnetlib.uoamonitorservice.entities.Topic; +import eu.dnetlib.uoamonitorservice.generics.Common; +import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException; +import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.access.method.P; +import org.springframework.stereotype.Service; + +import java.util.stream.Collectors; + +@Service +public class TopicService { + + private final TopicDAO dao; + + private final CategoryService categoryService; + private final CommonService commonService; + + @Autowired + public TopicService(TopicDAO dao, CategoryService categoryService, CommonService commonService) { + this.dao = dao; + this.categoryService = categoryService; + this.commonService = commonService; + } + + public Topic findByPath(Stakeholder stakeholder, String topicId, String message) { + if (!stakeholder.getTopics().contains(topicId)) { + throw new PathNotValidException(message + ": Topic with id: " + topicId + " not found in Stakeholder: " + stakeholder.getId()); + } + return this.dao.findById(topicId).orElseThrow(() -> new EntityNotFoundException(message + ": Topic with id: " + topicId + " not found")); + } + + public TopicFull getFullTopic(String type, String alias, String id) { + Topic topic = this.find(id); + if(commonService.hasVisibilityAuthority(type, alias, topic)) { + return new TopicFull(topic, topic.getCategories().stream() + .map(categoryId -> this.categoryService.getFullCategory(type, alias, categoryId)) + .collect(Collectors.toList())); + } else { + return null; + } + } + + public Topic find(String id) { + return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("Topic with id: " + id + " not found")); + } +}