From 00b17fad1c3aba0a8964a54dbeb7427b539c085e Mon Sep 17 00:00:00 2001 From: "k.triantafyllou" Date: Wed, 12 Jun 2024 01:42:49 +0300 Subject: [PATCH] [umbrella]: Add reorder methods in updateUmbrella. --- .../controllers/StakeholderController.java | 2 +- .../uoamonitorservice/dto/UpdateUmbrella.java | 28 ++++++++++++--- .../uoamonitorservice/primitives/Action.java | 2 +- .../primitives/Umbrella.java | 31 +++++++++++++--- .../service/StakeholderService.java | 36 ++++++++++++++++--- 5 files changed, 85 insertions(+), 14 deletions(-) diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java index f04cbe2..21424cd 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java @@ -155,7 +155,7 @@ public class StakeholderController { @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{stakeholderId}/umbrella", method = RequestMethod.POST) - public Umbrella changeStakeholderVisibility(@PathVariable("stakeholderId") String stakeholderId, @RequestBody UpdateUmbrella update) { + public Umbrella updateUmbrella(@PathVariable("stakeholderId") String stakeholderId, @RequestBody UpdateUmbrella update) { log.debug("update stakeholder umbrella"); log.debug("Stakeholder: " + stakeholderId); Stakeholder stakeholder = this.stakeholderService.findByPath(stakeholderId); diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/dto/UpdateUmbrella.java b/src/main/java/eu/dnetlib/uoamonitorservice/dto/UpdateUmbrella.java index 9117c6e..c2f7953 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/dto/UpdateUmbrella.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/dto/UpdateUmbrella.java @@ -1,21 +1,25 @@ package eu.dnetlib.uoamonitorservice.dto; import eu.dnetlib.uoamonitorservice.primitives.Action; +import eu.dnetlib.uoamonitorservice.primitives.StakeholderType; + +import java.util.List; public class UpdateUmbrella { - String type; + StakeholderType type; Action action; String child; + List types; + List children; public UpdateUmbrella() { } - - public String getType() { + public StakeholderType getType() { return type; } - public void setType(String type) { + public void setType(StakeholderType type) { this.type = type; } @@ -34,4 +38,20 @@ public class UpdateUmbrella { public void setChild(String child) { this.child = child; } + + public List getTypes() { + return types; + } + + public void setTypes(List types) { + this.types = types; + } + + public List getChildren() { + return children; + } + + public void setChildren(List children) { + this.children = children; + } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/primitives/Action.java b/src/main/java/eu/dnetlib/uoamonitorservice/primitives/Action.java index e95e3ea..b8ac5d3 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/primitives/Action.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/primitives/Action.java @@ -1,5 +1,5 @@ package eu.dnetlib.uoamonitorservice.primitives; public enum Action { - ADD, REMOVE + ADD, REMOVE, UPDATE } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/primitives/Umbrella.java b/src/main/java/eu/dnetlib/uoamonitorservice/primitives/Umbrella.java index 8d2a08c..c9df23f 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/primitives/Umbrella.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/primitives/Umbrella.java @@ -4,10 +4,7 @@ import eu.dnetlib.uoamonitorservice.entities.Stakeholder; import eu.dnetlib.uoamonitorservice.generics.Common; import eu.dnetlib.uoamonitorservice.service.StakeholderService; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.stream.Collectors; public class Umbrella { @@ -99,4 +96,30 @@ public class Umbrella { } return false; } + + public boolean update(List types) { + if(types == null) { + return false; + } else if(types.size() != this.types.size()) { + return false; + } else { + if(new HashSet<>(this.types).containsAll(types)) { + this.types = types; + return true; + } + return false; + } + } + + public boolean update(StakeholderType type, List children) { + if(children == null) { + return false; + } else if(this.children.get(type) == null) { + return false; + } else if(children.size() != this.children.get(type).size()) { + return false; + } else { + return new HashSet<>(this.children.get(type)).containsAll(children); + } + } } diff --git a/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java b/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java index 4595d6d..a53ca73 100644 --- a/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java +++ b/src/main/java/eu/dnetlib/uoamonitorservice/service/StakeholderService.java @@ -246,15 +246,21 @@ public class StakeholderService { if (this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) { if(update.getAction().equals(Action.ADD)) { if(update.getChild() != null) { - return this.addChild(stakeholder.getId(), StakeholderType.valueOf(update.getType()), update.getChild()); + return this.addChild(stakeholder.getId(), update.getType(), update.getChild()); } else { - return this.addType(stakeholder.getId(), StakeholderType.valueOf(update.getType())); + return this.addType(stakeholder.getId(), update.getType()); } } else if(update.getAction().equals(Action.REMOVE)) { if(update.getChild() != null) { - return this.removeChild(stakeholder.getId(), StakeholderType.valueOf(update.getType()), update.getChild()); + return this.removeChild(stakeholder.getId(), update.getType(), update.getChild()); } else { - return this.removeType(stakeholder.getId(), StakeholderType.valueOf(update.getType())); + return this.removeType(stakeholder.getId(), update.getType()); + } + } else if(update.getAction().equals(Action.UPDATE)) { + if(update.getType() != null) { + return this.updateChildren(stakeholder.getId(), update.getType(), update.getChildren()); + } else { + return this.updateTypes(stakeholder.getId(), update.getTypes()); } } } else { @@ -308,4 +314,26 @@ public class StakeholderService { } throw new BadRequestException("Cannot remove child: " + childId + " to stakeholder with id " + id); } + + public Umbrella updateTypes(String id, List types) { + Stakeholder stakeholder = this.findByPath(id); + Umbrella umbrella = stakeholder.getUmbrellaOptional().orElseThrow(() -> new EntityNotFoundException("Umbrella not found in the stakeholder with id " + id)); + if (stakeholder.getUmbrella().update(types)) { + stakeholder.setUmbrella(umbrella); + stakeholder.setUpdateDate(new Date()); + return this.getFullStakeholder(this.dao.save(stakeholder)).getUmbrella(); + } + throw new BadRequestException("Cannot update types in umbrella of stakeholder with id " + id); + } + + public Umbrella updateChildren(String id, StakeholderType type, List children) { + Stakeholder stakeholder = this.findByPath(id); + Umbrella umbrella = stakeholder.getUmbrellaOptional().orElseThrow(() -> new EntityNotFoundException("Umbrella not found in the stakeholder with id " + id)); + if (stakeholder.getUmbrella().update(type, children)) { + stakeholder.setUmbrella(umbrella); + stakeholder.setUpdateDate(new Date()); + return this.getFullStakeholder(this.dao.save(stakeholder)).getUmbrella(); + } + throw new BadRequestException("Cannot update children of " + type + " in umbrella of stakeholder with id " + id); + } }