From 806ea718a5778e11471e137370f5dd9507516fab Mon Sep 17 00:00:00 2001 From: Azher2Ali <121898125+Azher2Ali@users.noreply.github.com> Date: Fri, 21 Apr 2023 00:25:06 -0400 Subject: [PATCH 1/5] Committing changes related to visa permission api changes --- .../ego/controller/VisaController.java | 26 ++++++++++- .../ego/model/dto/VisaUpdateRequest.java | 3 -- .../repository/VisaPermissionRepository.java | 18 ++++++++ .../ego/service/VisaPermissionService.java | 46 ++++++++++++++++++- .../bio/overture/ego/service/VisaService.java | 5 +- 5 files changed, 90 insertions(+), 8 deletions(-) create mode 100644 src/main/java/bio/overture/ego/repository/VisaPermissionRepository.java diff --git a/src/main/java/bio/overture/ego/controller/VisaController.java b/src/main/java/bio/overture/ego/controller/VisaController.java index 396a967b..771004ff 100644 --- a/src/main/java/bio/overture/ego/controller/VisaController.java +++ b/src/main/java/bio/overture/ego/controller/VisaController.java @@ -10,7 +10,9 @@ import bio.overture.ego.view.Views; import com.fasterxml.jackson.annotation.JsonView; import io.swagger.annotations.*; +import java.util.List; import java.util.UUID; +import javax.validation.constraints.NotNull; import lombok.NonNull; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; @@ -28,6 +30,8 @@ public class VisaController { /** Dependencies */ private final VisaService visaService; + private final VisaPermissionService visaPermissionService; + private final UserPermissionService userPermissionService; private final GroupPermissionService groupPermissionService; private final ApplicationPermissionService applicationPermissionService; @@ -35,10 +39,12 @@ public class VisaController { @Autowired public VisaController( @NonNull VisaService visaService, + @NotNull VisaPermissionService visaPermissionService, @NonNull UserPermissionService userPermissionService, @NonNull GroupPermissionService groupPermissionService, @NonNull ApplicationPermissionService applicationPermissionService) { this.visaService = visaService; + this.visaPermissionService = visaPermissionService; this.groupPermissionService = groupPermissionService; this.userPermissionService = userPermissionService; this.applicationPermissionService = applicationPermissionService; @@ -91,8 +97,9 @@ public VisaController( public @ResponseBody Visa updateVisa( @ApiIgnore @RequestHeader(value = "Authorization", required = true) final String authorization, + @PathVariable(value = "id", required = true) UUID id, @RequestBody(required = true) VisaUpdateRequest visaRequest) { - return visaService.partialUpdate(visaRequest); + return visaService.partialUpdate(id, visaRequest); } @AdminScoped @@ -104,4 +111,21 @@ public void deleteVisa( @PathVariable(value = "id", required = true) UUID id) { visaService.delete(id); } + + /* + * This method is used to fetch visa permissions using visa id + * @param visaId UUID + * @return visaPermissions List + */ + @AdminScoped + @RequestMapping(method = GET, value = "/permissions/{id}") + @ApiResponses(value = {@ApiResponse(code = 200, message = "Get VisaPermissions by visaId")}) + @JsonView(Views.REST.class) + public @ResponseBody List getPermissionsByVisaId( + @ApiIgnore @RequestHeader(value = "Authorization", required = true) + final String authorization, + @PathVariable(value = "id", required = true) UUID id) { + System.out.println(id); + return visaPermissionService.getPermissionsByVisaId(id); + } } diff --git a/src/main/java/bio/overture/ego/model/dto/VisaUpdateRequest.java b/src/main/java/bio/overture/ego/model/dto/VisaUpdateRequest.java index d7008a75..ecf7d08f 100644 --- a/src/main/java/bio/overture/ego/model/dto/VisaUpdateRequest.java +++ b/src/main/java/bio/overture/ego/model/dto/VisaUpdateRequest.java @@ -1,6 +1,5 @@ package bio.overture.ego.model.dto; -import java.util.UUID; import javax.validation.constraints.NotNull; import lombok.AllArgsConstructor; import lombok.Builder; @@ -13,8 +12,6 @@ @NoArgsConstructor public class VisaUpdateRequest { - @NotNull private UUID id; - @NotNull private String type; @NotNull private String source; diff --git a/src/main/java/bio/overture/ego/repository/VisaPermissionRepository.java b/src/main/java/bio/overture/ego/repository/VisaPermissionRepository.java new file mode 100644 index 00000000..9236fa7b --- /dev/null +++ b/src/main/java/bio/overture/ego/repository/VisaPermissionRepository.java @@ -0,0 +1,18 @@ +package bio.overture.ego.repository; + +import bio.overture.ego.model.entity.VisaPermission; +import java.util.List; +import java.util.Optional; +import java.util.UUID; + +public interface VisaPermissionRepository extends NamedRepository { + @Override + @Deprecated + default Optional findByName(String name) { + return null; + } + + List findAll(); + + List findByVisaId(UUID visaId); +} diff --git a/src/main/java/bio/overture/ego/service/VisaPermissionService.java b/src/main/java/bio/overture/ego/service/VisaPermissionService.java index 65440038..98d32b0b 100644 --- a/src/main/java/bio/overture/ego/service/VisaPermissionService.java +++ b/src/main/java/bio/overture/ego/service/VisaPermissionService.java @@ -1,7 +1,16 @@ package bio.overture.ego.service; -import bio.overture.ego.model.entity.*; +import static java.lang.String.format; + +import bio.overture.ego.event.token.ApiKeyEventsPublisher; +import bio.overture.ego.model.entity.VisaPermission; +import bio.overture.ego.model.exceptions.NotFoundException; +import bio.overture.ego.repository.VisaPermissionRepository; +import java.util.List; +import java.util.UUID; +import lombok.NonNull; import lombok.extern.slf4j.Slf4j; +import lombok.val; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -9,8 +18,41 @@ @Slf4j @Service @Transactional -public class VisaPermissionService { +public class VisaPermissionService extends AbstractNamedService { /** Dependencies */ @Autowired private VisaService visaService; + + @Autowired private VisaPermissionRepository visaPermissionRepository; + private final ApiKeyEventsPublisher apiKeyEventsPublisher; + + @Autowired + public VisaPermissionService( + @NonNull VisaPermissionRepository visaPermissionRepository, + @NonNull VisaService visaService, + @NonNull ApiKeyEventsPublisher apiKeyEventsPublisher) { + super(VisaPermission.class, visaPermissionRepository); + this.visaPermissionRepository = visaPermissionRepository; + this.visaService = visaService; + this.apiKeyEventsPublisher = apiKeyEventsPublisher; + } + + public List getPermissionsByVisaId(@NonNull UUID visaId) { + val result = (List) visaPermissionRepository.findByVisaId(visaId); + System.out.println("Result :::::::::::::::::::::::" + result); + if (result.isEmpty()) { + throw new NotFoundException(format("No VisaPermissions exists with visaId '%s'", visaId)); + } + return result; + } + + @Override + public VisaPermission getById(@NonNull UUID uuid) { + return super.getById(uuid); + } + + @Override + public VisaPermission getWithRelationships(UUID uuid) { + return null; + } } diff --git a/src/main/java/bio/overture/ego/service/VisaService.java b/src/main/java/bio/overture/ego/service/VisaService.java index c919b7db..72b4184b 100644 --- a/src/main/java/bio/overture/ego/service/VisaService.java +++ b/src/main/java/bio/overture/ego/service/VisaService.java @@ -11,6 +11,7 @@ import bio.overture.ego.repository.VisaRepository; import java.util.Optional; import java.util.UUID; +import javax.validation.constraints.NotNull; import lombok.NonNull; import lombok.extern.slf4j.Slf4j; import lombok.val; @@ -69,8 +70,8 @@ public Page listVisa(@NonNull Pageable pageable) { return visaRepository.findAll(pageable); } - public Visa partialUpdate(@NonNull VisaUpdateRequest updateRequest) { - val visa = getById(updateRequest.getId()); + public Visa partialUpdate(@NotNull UUID id, @NonNull VisaUpdateRequest updateRequest) { + val visa = getById(id); VISA_CONVERTER.updateVisa(updateRequest, visa); return getRepository().save(visa); } From 5f7a843806481fef852d7b014ba47ff81bb506cf Mon Sep 17 00:00:00 2001 From: Azher2Ali <121898125+Azher2Ali@users.noreply.github.com> Date: Fri, 21 Apr 2023 02:12:02 -0400 Subject: [PATCH 2/5] Committing changes related to visa permissions create and update api --- .../ego/controller/VisaController.java | 37 ++++++++++++++-- .../ego/model/dto/VisaPermissionRequest.java | 18 ++++++++ .../overture/ego/model/dto/VisaRequest.java | 3 -- .../ego/model/dto/VisaUpdateRequest.java | 22 ---------- .../repository/VisaPermissionRepository.java | 4 ++ .../ego/service/VisaPermissionService.java | 44 ++++++++++++++++++- .../bio/overture/ego/service/VisaService.java | 5 +-- 7 files changed, 101 insertions(+), 32 deletions(-) create mode 100644 src/main/java/bio/overture/ego/model/dto/VisaPermissionRequest.java delete mode 100644 src/main/java/bio/overture/ego/model/dto/VisaUpdateRequest.java diff --git a/src/main/java/bio/overture/ego/controller/VisaController.java b/src/main/java/bio/overture/ego/controller/VisaController.java index 771004ff..957d9214 100644 --- a/src/main/java/bio/overture/ego/controller/VisaController.java +++ b/src/main/java/bio/overture/ego/controller/VisaController.java @@ -98,7 +98,7 @@ public VisaController( @ApiIgnore @RequestHeader(value = "Authorization", required = true) final String authorization, @PathVariable(value = "id", required = true) UUID id, - @RequestBody(required = true) VisaUpdateRequest visaRequest) { + @RequestBody(required = true) VisaRequest visaRequest) { return visaService.partialUpdate(id, visaRequest); } @@ -118,14 +118,45 @@ public void deleteVisa( * @return visaPermissions List */ @AdminScoped - @RequestMapping(method = GET, value = "/permissions/{id}") + @RequestMapping(method = GET, value = "/permissions/visaId/{id}") @ApiResponses(value = {@ApiResponse(code = 200, message = "Get VisaPermissions by visaId")}) @JsonView(Views.REST.class) public @ResponseBody List getPermissionsByVisaId( @ApiIgnore @RequestHeader(value = "Authorization", required = true) final String authorization, @PathVariable(value = "id", required = true) UUID id) { - System.out.println(id); return visaPermissionService.getPermissionsByVisaId(id); } + + /* + * This method is used to fetch visa permissions using policy id + * @param policyId UUID + * @return visaPermissions List + */ + @AdminScoped + @RequestMapping(method = GET, value = "/permissions/policyId/{id}") + @ApiResponses(value = {@ApiResponse(code = 200, message = "Get VisaPermissions by policyId")}) + @JsonView(Views.REST.class) + public @ResponseBody List getPermissionsByPolicyId( + @ApiIgnore @RequestHeader(value = "Authorization", required = true) + final String authorization, + @PathVariable(value = "id", required = true) UUID id) { + return visaPermissionService.getPermissionsByPolicyId(id); + } + + /* + * This method is used to create/update visa permissions + * @param visaPermissionRequest VisaPermissionRequest + * @return visaPermission VisaPermission + */ + @AdminScoped + @RequestMapping(method = POST, value = "/permissions") + @ApiResponses(value = {@ApiResponse(code = 200, message = "Create or Update VisaPermission")}) + @JsonView(Views.REST.class) + public @ResponseBody VisaPermission createOrUpdatePermissions( + @ApiIgnore @RequestHeader(value = "Authorization", required = true) + final String authorization, + @RequestBody(required = true) VisaPermissionRequest visaPermissionRequest) { + return visaPermissionService.createOrUpdatePermissions(visaPermissionRequest); + } } diff --git a/src/main/java/bio/overture/ego/model/dto/VisaPermissionRequest.java b/src/main/java/bio/overture/ego/model/dto/VisaPermissionRequest.java new file mode 100644 index 00000000..83c38223 --- /dev/null +++ b/src/main/java/bio/overture/ego/model/dto/VisaPermissionRequest.java @@ -0,0 +1,18 @@ +package bio.overture.ego.model.dto; + +import bio.overture.ego.model.enums.AccessLevel; +import java.util.UUID; +import lombok.*; + +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor +public class VisaPermissionRequest { + + private UUID entity; + + private UUID visaId; + + private AccessLevel mask; +} diff --git a/src/main/java/bio/overture/ego/model/dto/VisaRequest.java b/src/main/java/bio/overture/ego/model/dto/VisaRequest.java index 2a60d7a6..6225d995 100644 --- a/src/main/java/bio/overture/ego/model/dto/VisaRequest.java +++ b/src/main/java/bio/overture/ego/model/dto/VisaRequest.java @@ -1,6 +1,5 @@ package bio.overture.ego.model.dto; -import java.util.UUID; import javax.validation.constraints.NotNull; import lombok.AllArgsConstructor; import lombok.Builder; @@ -13,8 +12,6 @@ @NoArgsConstructor public class VisaRequest { - private UUID id; - @NotNull private String type; @NotNull private String source; diff --git a/src/main/java/bio/overture/ego/model/dto/VisaUpdateRequest.java b/src/main/java/bio/overture/ego/model/dto/VisaUpdateRequest.java deleted file mode 100644 index ecf7d08f..00000000 --- a/src/main/java/bio/overture/ego/model/dto/VisaUpdateRequest.java +++ /dev/null @@ -1,22 +0,0 @@ -package bio.overture.ego.model.dto; - -import javax.validation.constraints.NotNull; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; - -@Data -@Builder -@AllArgsConstructor -@NoArgsConstructor -public class VisaUpdateRequest { - - @NotNull private String type; - - @NotNull private String source; - - @NotNull private String value; - - @NotNull private String by; -} diff --git a/src/main/java/bio/overture/ego/repository/VisaPermissionRepository.java b/src/main/java/bio/overture/ego/repository/VisaPermissionRepository.java index 9236fa7b..504d20cb 100644 --- a/src/main/java/bio/overture/ego/repository/VisaPermissionRepository.java +++ b/src/main/java/bio/overture/ego/repository/VisaPermissionRepository.java @@ -15,4 +15,8 @@ default Optional findByName(String name) { List findAll(); List findByVisaId(UUID visaId); + + List findByPolicyId(UUID entity); + + List findByPolicyIdAndVisaId(UUID entity, UUID visaId); } diff --git a/src/main/java/bio/overture/ego/service/VisaPermissionService.java b/src/main/java/bio/overture/ego/service/VisaPermissionService.java index 98d32b0b..090c1d8c 100644 --- a/src/main/java/bio/overture/ego/service/VisaPermissionService.java +++ b/src/main/java/bio/overture/ego/service/VisaPermissionService.java @@ -1,8 +1,10 @@ package bio.overture.ego.service; import static java.lang.String.format; +import static org.mapstruct.factory.Mappers.getMapper; import bio.overture.ego.event.token.ApiKeyEventsPublisher; +import bio.overture.ego.model.dto.VisaPermissionRequest; import bio.overture.ego.model.entity.VisaPermission; import bio.overture.ego.model.exceptions.NotFoundException; import bio.overture.ego.repository.VisaPermissionRepository; @@ -11,6 +13,10 @@ import lombok.NonNull; import lombok.extern.slf4j.Slf4j; import lombok.val; +import org.mapstruct.Mapper; +import org.mapstruct.MappingTarget; +import org.mapstruct.NullValueCheckStrategy; +import org.mapstruct.ReportingPolicy; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -26,6 +32,9 @@ public class VisaPermissionService extends AbstractNamedService getPermissionsByVisaId(@NonNull UUID visaId) { val result = (List) visaPermissionRepository.findByVisaId(visaId); - System.out.println("Result :::::::::::::::::::::::" + result); if (result.isEmpty()) { throw new NotFoundException(format("No VisaPermissions exists with visaId '%s'", visaId)); } return result; } + public List getPermissionsByPolicyId(@NonNull UUID policyId) { + val result = (List) visaPermissionRepository.findByPolicyId(policyId); + if (result.isEmpty()) { + throw new NotFoundException(format("No VisaPermissions exists with policyId '%s'", policyId)); + } + return result; + } + + public VisaPermission createOrUpdatePermissions( + @NonNull VisaPermissionRequest visaPermissionRequest) { + List visaPermissionEntities = + visaPermissionRepository.findByPolicyIdAndVisaId( + visaPermissionRequest.getEntity(), visaPermissionRequest.getVisaId()); + if (visaPermissionEntities == null && visaPermissionEntities.isEmpty()) { + return visaPermissionRepository.save( + VISA_PERMISSION_CONVERTER.createVisaPermission(visaPermissionRequest)); + } else { + VISA_PERMISSION_CONVERTER.updateVisaPermission( + visaPermissionRequest, visaPermissionEntities.get(0)); + return visaPermissionRepository.save(visaPermissionEntities.get(0)); + } + } + @Override public VisaPermission getById(@NonNull UUID uuid) { return super.getById(uuid); @@ -55,4 +86,15 @@ public VisaPermission getById(@NonNull UUID uuid) { public VisaPermission getWithRelationships(UUID uuid) { return null; } + + @Mapper( + nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS, + unmappedTargetPolicy = ReportingPolicy.WARN) + public abstract static class VisaPermissionConverter { + public abstract VisaPermission createVisaPermission( + VisaPermissionRequest visaPermissionRequest); + + public abstract void updateVisaPermission( + VisaPermissionRequest visaPermissionRequest, @MappingTarget VisaPermission visaPermission); + } } diff --git a/src/main/java/bio/overture/ego/service/VisaService.java b/src/main/java/bio/overture/ego/service/VisaService.java index 72b4184b..70cf00c2 100644 --- a/src/main/java/bio/overture/ego/service/VisaService.java +++ b/src/main/java/bio/overture/ego/service/VisaService.java @@ -6,7 +6,6 @@ import bio.overture.ego.event.token.ApiKeyEventsPublisher; import bio.overture.ego.model.dto.VisaRequest; -import bio.overture.ego.model.dto.VisaUpdateRequest; import bio.overture.ego.model.entity.Visa; import bio.overture.ego.repository.VisaRepository; import java.util.Optional; @@ -70,7 +69,7 @@ public Page listVisa(@NonNull Pageable pageable) { return visaRepository.findAll(pageable); } - public Visa partialUpdate(@NotNull UUID id, @NonNull VisaUpdateRequest updateRequest) { + public Visa partialUpdate(@NotNull UUID id, @NonNull VisaRequest updateRequest) { val visa = getById(id); VISA_CONVERTER.updateVisa(updateRequest, visa); return getRepository().save(visa); @@ -82,7 +81,7 @@ public Visa partialUpdate(@NotNull UUID id, @NonNull VisaUpdateRequest updateReq public abstract static class VisaConverter { public abstract Visa convertToVisa(VisaRequest request); - public abstract void updateVisa(VisaUpdateRequest request, @MappingTarget Visa visaToUpdate); + public abstract void updateVisa(VisaRequest request, @MappingTarget Visa visaToUpdate); } @Override From 4bfa395474690cc082d75e06d9b32d980ddbacb1 Mon Sep 17 00:00:00 2001 From: Azher2Ali <121898125+Azher2Ali@users.noreply.github.com> Date: Wed, 26 Apr 2023 13:11:19 -0400 Subject: [PATCH 3/5] Committing changes related to visaPermission CRUD endpoints implementation --- Jenkinsfile | 4 +- .../ego/controller/VisaController.java | 36 ++++++++++++++- .../ego/model/dto/VisaPermissionRequest.java | 4 +- .../bio/overture/ego/model/entity/Visa.java | 2 +- .../ego/model/entity/VisaPermission.java | 45 ++++++------------- .../overture/ego/model/enums/JavaFields.java | 3 +- .../overture/ego/model/enums/SqlFields.java | 8 +--- .../bio/overture/ego/model/enums/Tables.java | 2 - .../repository/VisaPermissionRepository.java | 12 +++-- .../ego/service/VisaPermissionService.java | 31 +++++++++---- .../flyway/sql/V1_23__add_visa_pcl.sql | 10 ++--- 11 files changed, 91 insertions(+), 66 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index e99a9923..6b8ed599 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,6 +1,4 @@ -def commit = "UNKNOWN" -def version = "UNKNOWN" -def chartversion = "3.1.0" +def chartversion = "3.1.1" pipeline { agent { diff --git a/src/main/java/bio/overture/ego/controller/VisaController.java b/src/main/java/bio/overture/ego/controller/VisaController.java index 957d9214..d320d048 100644 --- a/src/main/java/bio/overture/ego/controller/VisaController.java +++ b/src/main/java/bio/overture/ego/controller/VisaController.java @@ -1,6 +1,5 @@ package bio.overture.ego.controller; -import static bio.overture.ego.controller.resolver.PageableResolver.*; import static org.springframework.web.bind.annotation.RequestMethod.*; import bio.overture.ego.model.dto.*; @@ -67,6 +66,10 @@ public VisaController( return visaService.getById(id); } + /* + * This method is used to list all visas + * @return visas List + */ @AdminScoped @RequestMapping(method = GET, value = "") @ApiResponses(value = {@ApiResponse(code = 200, message = "All Visas")}) @@ -78,6 +81,11 @@ public VisaController( return new PageDTO<>(visaService.listVisa(pageable)); } + /* + * This method is used to create visa using visa create request + * @param visaRequest VisaRequest + * @return Visa visa + */ @AdminScoped @RequestMapping(method = POST, value = "") @ApiResponses( @@ -91,6 +99,12 @@ public VisaController( return visaService.create(visaRequest); } + /* + * This method is used to update visa using visa id and update request + * @param visaId UUID + * @param visaRequest VisaRequest + * @return Visa visa + */ @AdminScoped @RequestMapping(method = PUT, value = "/{id}") @ApiResponses(value = {@ApiResponse(code = 200, message = "Update Visa", response = Visa.class)}) @@ -102,6 +116,10 @@ public VisaController( return visaService.partialUpdate(id, visaRequest); } + /* + * This method is used to delete visa using visa id + * @param visaId UUID + */ @AdminScoped @RequestMapping(method = DELETE, value = "/{id}") @ResponseStatus(value = HttpStatus.OK) @@ -159,4 +177,20 @@ public void deleteVisa( @RequestBody(required = true) VisaPermissionRequest visaPermissionRequest) { return visaPermissionService.createOrUpdatePermissions(visaPermissionRequest); } + + /* + * This method is used to delete/remove visa permissions + * @param visaPermissionRequest VisaPermissionRequest + */ + @AdminScoped + @RequestMapping(method = DELETE, value = "/permissions") + @ApiResponses(value = {@ApiResponse(code = 200, message = "Remove VisaPermission")}) + @JsonView(Views.REST.class) + public @ResponseBody void removePermissions( + @ApiIgnore @RequestHeader(value = "Authorization", required = true) + final String authorization, + @PathVariable(value = "policyId", required = true) UUID policyId, + @PathVariable(value = "visaId", required = true) UUID visaId) { + visaPermissionService.removePermission(policyId, visaId); + } } diff --git a/src/main/java/bio/overture/ego/model/dto/VisaPermissionRequest.java b/src/main/java/bio/overture/ego/model/dto/VisaPermissionRequest.java index 83c38223..770c1e42 100644 --- a/src/main/java/bio/overture/ego/model/dto/VisaPermissionRequest.java +++ b/src/main/java/bio/overture/ego/model/dto/VisaPermissionRequest.java @@ -10,9 +10,9 @@ @NoArgsConstructor public class VisaPermissionRequest { - private UUID entity; + private UUID policyId; private UUID visaId; - private AccessLevel mask; + private AccessLevel accessLevel; } diff --git a/src/main/java/bio/overture/ego/model/entity/Visa.java b/src/main/java/bio/overture/ego/model/entity/Visa.java index 403b1684..f4574d69 100644 --- a/src/main/java/bio/overture/ego/model/entity/Visa.java +++ b/src/main/java/bio/overture/ego/model/entity/Visa.java @@ -64,7 +64,7 @@ public class Visa implements Identifiable { private String by; @JsonIgnore - @ManyToMany(mappedBy = "visaId", cascade = CascadeType.ALL, fetch = FetchType.LAZY) + @OneToMany(mappedBy = JavaFields.VISA, cascade = CascadeType.ALL, fetch = FetchType.LAZY) @Builder.Default private Set visaPermissions = newHashSet(); } diff --git a/src/main/java/bio/overture/ego/model/entity/VisaPermission.java b/src/main/java/bio/overture/ego/model/entity/VisaPermission.java index e441e596..3b7207b4 100644 --- a/src/main/java/bio/overture/ego/model/entity/VisaPermission.java +++ b/src/main/java/bio/overture/ego/model/entity/VisaPermission.java @@ -1,20 +1,13 @@ package bio.overture.ego.model.entity; -import static bio.overture.ego.model.enums.AccessLevel.EGO_ACCESS_LEVEL_ENUM; - -import bio.overture.ego.model.enums.AccessLevel; +import bio.overture.ego.model.enums.JavaFields; import bio.overture.ego.model.enums.SqlFields; import bio.overture.ego.model.enums.Tables; import bio.overture.ego.view.Views; import com.fasterxml.jackson.annotation.JsonView; -import java.util.Collection; -import java.util.UUID; import javax.persistence.*; -import javax.validation.constraints.NotNull; import lombok.*; import lombok.experimental.FieldNameConstants; -import org.hibernate.annotations.GenericGenerator; -import org.hibernate.annotations.Type; @Entity @Table(name = Tables.ACLVISAPERMISSION) @@ -25,32 +18,20 @@ @JsonView(Views.REST.class) @ToString(callSuper = true) @FieldNameConstants +@EqualsAndHashCode( + callSuper = true, + of = {"id"}) +@NamedEntityGraph( + name = "visa-permission-entity-with-relationships", + attributeNodes = { + @NamedAttributeNode(value = JavaFields.POLICY), + @NamedAttributeNode(value = JavaFields.VISA) + }) public class VisaPermission extends AbstractPermission { - @Id - @Column(name = SqlFields.ID, updatable = false, nullable = false) - @GenericGenerator(name = "aclp_uuid", strategy = "org.hibernate.id.UUIDGenerator") - @GeneratedValue(generator = "aclp_uuid") - private UUID id; - - @JoinColumn(name = SqlFields.ID, nullable = false) - private UUID entity; - - @JoinColumn(name = SqlFields.ID, nullable = false) - private UUID visaId; - - @NotNull - @Column(name = SqlFields.MASK, nullable = false) - @Enumerated(EnumType.STRING) - @Type(type = EGO_ACCESS_LEVEL_ENUM) - private AccessLevel mask; - - @ManyToMany - @JoinTable( - name = "ga4ghvisa", - joinColumns = @JoinColumn(name = "aclp_id", referencedColumnName = "visaId"), - inverseJoinColumns = @JoinColumn(name = "visa_id", referencedColumnName = "id")) - private Collection visas; + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = SqlFields.VISA_ID, nullable = false) + private Visa visa; @Override public Visa getOwner() { diff --git a/src/main/java/bio/overture/ego/model/enums/JavaFields.java b/src/main/java/bio/overture/ego/model/enums/JavaFields.java index aecbeaf1..5592b257 100644 --- a/src/main/java/bio/overture/ego/model/enums/JavaFields.java +++ b/src/main/java/bio/overture/ego/model/enums/JavaFields.java @@ -71,6 +71,7 @@ public class JavaFields { public static final String VALUE = "value"; public static final String BY = "by"; - public static final String VISAPERMISSION = "ACLVISAPERMISSION"; + public static final String GA4GHVISA = "ga4ghvisa"; + public static final String VISA = "visa"; } diff --git a/src/main/java/bio/overture/ego/model/enums/SqlFields.java b/src/main/java/bio/overture/ego/model/enums/SqlFields.java index 488b3c8c..dfdf0a5f 100644 --- a/src/main/java/bio/overture/ego/model/enums/SqlFields.java +++ b/src/main/java/bio/overture/ego/model/enums/SqlFields.java @@ -40,12 +40,6 @@ public class SqlFields { public static final String ERRORREDIRECTURI = "errorredirecturi"; public static final String SOURCE = "source"; public static final String VALUE = "value"; - public static final String BY = "by"; - - public static final String ENTITY = "entity"; - - public static final String VISAID = "visaId"; - - public static final String MASK = "mask"; + public static final String VISA_ID = "visa_id"; } diff --git a/src/main/java/bio/overture/ego/model/enums/Tables.java b/src/main/java/bio/overture/ego/model/enums/Tables.java index 861fe797..e4198aaf 100644 --- a/src/main/java/bio/overture/ego/model/enums/Tables.java +++ b/src/main/java/bio/overture/ego/model/enums/Tables.java @@ -22,8 +22,6 @@ public class Tables { public static final String APPLICATION_PERMISSION = "applicationpermission"; public static final String DEFAULTPROVIDERTRIPWIRE = "defaultprovidertripwire"; public static final String INITTRIPWIRE = "inittripwire"; - public static final String GA4GHVISA = "ga4ghvisa"; - public static final String ACLVISAPERMISSION = "ACLVISAPERMISSION"; } diff --git a/src/main/java/bio/overture/ego/repository/VisaPermissionRepository.java b/src/main/java/bio/overture/ego/repository/VisaPermissionRepository.java index 504d20cb..abee391d 100644 --- a/src/main/java/bio/overture/ego/repository/VisaPermissionRepository.java +++ b/src/main/java/bio/overture/ego/repository/VisaPermissionRepository.java @@ -1,9 +1,12 @@ package bio.overture.ego.repository; +import static org.springframework.data.jpa.repository.EntityGraph.EntityGraphType.FETCH; + import bio.overture.ego.model.entity.VisaPermission; import java.util.List; import java.util.Optional; import java.util.UUID; +import org.springframework.data.jpa.repository.EntityGraph; public interface VisaPermissionRepository extends NamedRepository { @Override @@ -14,9 +17,12 @@ default Optional findByName(String name) { List findAll(); - List findByVisaId(UUID visaId); + @EntityGraph(value = "visa-permission-entity-with-relationships", type = FETCH) + List findByVisa_Id(UUID visa_id); - List findByPolicyId(UUID entity); + @EntityGraph(value = "visa-permission-entity-with-relationships", type = FETCH) + List findByPolicy_Id(UUID policy_id); - List findByPolicyIdAndVisaId(UUID entity, UUID visaId); + @EntityGraph(value = "visa-permission-entity-with-relationships", type = FETCH) + List findByPolicyIdAndVisaId(UUID policy_id, UUID visa_id); } diff --git a/src/main/java/bio/overture/ego/service/VisaPermissionService.java b/src/main/java/bio/overture/ego/service/VisaPermissionService.java index 090c1d8c..20ae8096 100644 --- a/src/main/java/bio/overture/ego/service/VisaPermissionService.java +++ b/src/main/java/bio/overture/ego/service/VisaPermissionService.java @@ -10,6 +10,7 @@ import bio.overture.ego.repository.VisaPermissionRepository; import java.util.List; import java.util.UUID; +import javax.validation.constraints.NotNull; import lombok.NonNull; import lombok.extern.slf4j.Slf4j; import lombok.val; @@ -29,6 +30,8 @@ public class VisaPermissionService extends AbstractNamedService getPermissionsByVisaId(@NonNull UUID visaId) { - val result = (List) visaPermissionRepository.findByVisaId(visaId); + val result = (List) visaPermissionRepository.findByVisa_Id(visaId); if (result.isEmpty()) { throw new NotFoundException(format("No VisaPermissions exists with visaId '%s'", visaId)); } @@ -55,7 +58,7 @@ public List getPermissionsByVisaId(@NonNull UUID visaId) { } public List getPermissionsByPolicyId(@NonNull UUID policyId) { - val result = (List) visaPermissionRepository.findByPolicyId(policyId); + val result = (List) visaPermissionRepository.findByPolicy_Id(policyId); if (result.isEmpty()) { throw new NotFoundException(format("No VisaPermissions exists with policyId '%s'", policyId)); } @@ -64,12 +67,16 @@ public List getPermissionsByPolicyId(@NonNull UUID policyId) { public VisaPermission createOrUpdatePermissions( @NonNull VisaPermissionRequest visaPermissionRequest) { + VisaPermission visaPermission = null; List visaPermissionEntities = visaPermissionRepository.findByPolicyIdAndVisaId( - visaPermissionRequest.getEntity(), visaPermissionRequest.getVisaId()); - if (visaPermissionEntities == null && visaPermissionEntities.isEmpty()) { - return visaPermissionRepository.save( - VISA_PERMISSION_CONVERTER.createVisaPermission(visaPermissionRequest)); + visaPermissionRequest.getPolicyId(), visaPermissionRequest.getVisaId()); + if (visaPermissionEntities.isEmpty()) { + visaPermission = new VisaPermission(); + visaPermission.setVisa(visaService.getById(visaPermissionRequest.getVisaId())); + visaPermission.setPolicy(policyService.getById(visaPermissionRequest.getPolicyId())); + visaPermission.setAccessLevel(visaPermissionRequest.getAccessLevel()); + return visaPermissionRepository.save(visaPermission); } else { VISA_PERMISSION_CONVERTER.updateVisaPermission( visaPermissionRequest, visaPermissionEntities.get(0)); @@ -77,6 +84,15 @@ public VisaPermission createOrUpdatePermissions( } } + public void removePermission(@NonNull UUID policyId, @NotNull UUID visaId) { + VisaPermission visaPermission = null; + List visaPermissionEntities = + visaPermissionRepository.findByPolicyIdAndVisaId(policyId, visaId); + if (!visaPermissionEntities.isEmpty()) { + visaPermissionRepository.deleteById(visaPermissionEntities.get(0).getId()); + } + } + @Override public VisaPermission getById(@NonNull UUID uuid) { return super.getById(uuid); @@ -91,9 +107,6 @@ public VisaPermission getWithRelationships(UUID uuid) { nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS, unmappedTargetPolicy = ReportingPolicy.WARN) public abstract static class VisaPermissionConverter { - public abstract VisaPermission createVisaPermission( - VisaPermissionRequest visaPermissionRequest); - public abstract void updateVisaPermission( VisaPermissionRequest visaPermissionRequest, @MappingTarget VisaPermission visaPermission); } diff --git a/src/main/resources/flyway/sql/V1_23__add_visa_pcl.sql b/src/main/resources/flyway/sql/V1_23__add_visa_pcl.sql index 35557eef..0d2e780c 100644 --- a/src/main/resources/flyway/sql/V1_23__add_visa_pcl.sql +++ b/src/main/resources/flyway/sql/V1_23__add_visa_pcl.sql @@ -1,8 +1,8 @@ CREATE TABLE ACLVISAPERMISSION ( id UUID PRIMARY KEY, - entity UUID, - visaId UUID, - mask ACLMASK NOT NULL, - FOREIGN KEY (entity) REFERENCES POLICY(id), - FOREIGN KEY (visaId) REFERENCES GA4GHVISA(id) + policy_id UUID, + visa_id UUID, + access_level ACLMASK NOT NULL, + FOREIGN KEY (policy_id) REFERENCES POLICY(id), + FOREIGN KEY (visa_id) REFERENCES GA4GHVISA(id) ); From 9a6367c6f03dec4b9b0e3f45e8944f527cd76a88 Mon Sep 17 00:00:00 2001 From: Azher2Ali <121898125+Azher2Ali@users.noreply.github.com> Date: Fri, 28 Apr 2023 12:22:39 -0400 Subject: [PATCH 4/5] committing changes related to remove api fixes --- src/main/java/bio/overture/ego/controller/VisaController.java | 2 +- .../java/bio/overture/ego/service/VisaPermissionService.java | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/bio/overture/ego/controller/VisaController.java b/src/main/java/bio/overture/ego/controller/VisaController.java index d320d048..aa9e6447 100644 --- a/src/main/java/bio/overture/ego/controller/VisaController.java +++ b/src/main/java/bio/overture/ego/controller/VisaController.java @@ -183,7 +183,7 @@ public void deleteVisa( * @param visaPermissionRequest VisaPermissionRequest */ @AdminScoped - @RequestMapping(method = DELETE, value = "/permissions") + @RequestMapping(method = DELETE, value = "/permissions/{policyId}/{visaId}") @ApiResponses(value = {@ApiResponse(code = 200, message = "Remove VisaPermission")}) @JsonView(Views.REST.class) public @ResponseBody void removePermissions( diff --git a/src/main/java/bio/overture/ego/service/VisaPermissionService.java b/src/main/java/bio/overture/ego/service/VisaPermissionService.java index 20ae8096..21b98ce3 100644 --- a/src/main/java/bio/overture/ego/service/VisaPermissionService.java +++ b/src/main/java/bio/overture/ego/service/VisaPermissionService.java @@ -90,6 +90,9 @@ public void removePermission(@NonNull UUID policyId, @NotNull UUID visaId) { visaPermissionRepository.findByPolicyIdAndVisaId(policyId, visaId); if (!visaPermissionEntities.isEmpty()) { visaPermissionRepository.deleteById(visaPermissionEntities.get(0).getId()); + } else { + throw new NotFoundException( + format("No VisaPermissions exists with policyId '%s' and visaId '%s'", policyId, visaId)); } } From c39e6fbcdcf0e0fd3a5d1cb4c946816af594982f Mon Sep 17 00:00:00 2001 From: Azher2Ali <121898125+Azher2Ali@users.noreply.github.com> Date: Tue, 2 May 2023 15:16:55 -0400 Subject: [PATCH 5/5] Committing changes related to spring migration --- .../ego/controller/VisaController.java | 26 ++++++++++++------- .../ego/model/entity/VisaPermission.java | 4 --- .../ego/service/VisaPermissionService.java | 2 +- .../bio/overture/ego/service/VisaService.java | 1 - 4 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/main/java/bio/overture/ego/controller/VisaController.java b/src/main/java/bio/overture/ego/controller/VisaController.java index 7c3066fd..03997a97 100644 --- a/src/main/java/bio/overture/ego/controller/VisaController.java +++ b/src/main/java/bio/overture/ego/controller/VisaController.java @@ -8,10 +8,13 @@ import bio.overture.ego.service.*; import bio.overture.ego.view.Views; import com.fasterxml.jackson.annotation.JsonView; -import io.swagger.annotations.*; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.responses.ApiResponses; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.validation.constraints.NotNull; import java.util.List; import java.util.UUID; -import javax.validation.constraints.NotNull; import lombok.NonNull; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; @@ -135,10 +138,11 @@ public void deleteVisa( */ @AdminScoped @RequestMapping(method = GET, value = "/permissions/visaId/{id}") - @ApiResponses(value = {@ApiResponse(code = 200, message = "Get VisaPermissions by visaId")}) + @ApiResponses( + value = {@ApiResponse(responseCode = "200", description = "Get VisaPermissions by visaId")}) @JsonView(Views.REST.class) public @ResponseBody List getPermissionsByVisaId( - @ApiIgnore @RequestHeader(value = "Authorization", required = true) + @Parameter(hidden = true) @RequestHeader(value = "Authorization", required = true) final String authorization, @PathVariable(value = "id", required = true) UUID id) { return visaPermissionService.getPermissionsByVisaId(id); @@ -151,10 +155,11 @@ public void deleteVisa( */ @AdminScoped @RequestMapping(method = GET, value = "/permissions/policyId/{id}") - @ApiResponses(value = {@ApiResponse(code = 200, message = "Get VisaPermissions by policyId")}) + @ApiResponses( + value = {@ApiResponse(responseCode = "200", description = "Get VisaPermissions by policyId")}) @JsonView(Views.REST.class) public @ResponseBody List getPermissionsByPolicyId( - @ApiIgnore @RequestHeader(value = "Authorization", required = true) + @Parameter(hidden = true) @RequestHeader(value = "Authorization", required = true) final String authorization, @PathVariable(value = "id", required = true) UUID id) { return visaPermissionService.getPermissionsByPolicyId(id); @@ -167,10 +172,11 @@ public void deleteVisa( */ @AdminScoped @RequestMapping(method = POST, value = "/permissions") - @ApiResponses(value = {@ApiResponse(code = 200, message = "Create or Update VisaPermission")}) + @ApiResponses( + value = {@ApiResponse(responseCode = "200", description = "Create or Update VisaPermission")}) @JsonView(Views.REST.class) public @ResponseBody VisaPermission createOrUpdatePermissions( - @ApiIgnore @RequestHeader(value = "Authorization", required = true) + @Parameter(hidden = true) @RequestHeader(value = "Authorization", required = true) final String authorization, @RequestBody(required = true) VisaPermissionRequest visaPermissionRequest) { return visaPermissionService.createOrUpdatePermissions(visaPermissionRequest); @@ -182,10 +188,10 @@ public void deleteVisa( */ @AdminScoped @RequestMapping(method = DELETE, value = "/permissions/{policyId}/{visaId}") - @ApiResponses(value = {@ApiResponse(code = 200, message = "Remove VisaPermission")}) + @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "Remove VisaPermission")}) @JsonView(Views.REST.class) public @ResponseBody void removePermissions( - @ApiIgnore @RequestHeader(value = "Authorization", required = true) + @Parameter(hidden = true) @RequestHeader(value = "Authorization", required = true) final String authorization, @PathVariable(value = "policyId", required = true) UUID policyId, @PathVariable(value = "visaId", required = true) UUID visaId) { diff --git a/src/main/java/bio/overture/ego/model/entity/VisaPermission.java b/src/main/java/bio/overture/ego/model/entity/VisaPermission.java index 9c0a5b41..ebac950a 100644 --- a/src/main/java/bio/overture/ego/model/entity/VisaPermission.java +++ b/src/main/java/bio/overture/ego/model/entity/VisaPermission.java @@ -5,11 +5,7 @@ import bio.overture.ego.model.enums.Tables; import bio.overture.ego.view.Views; import com.fasterxml.jackson.annotation.JsonView; -import com.vladmihalcea.hibernate.type.basic.PostgreSQLEnumType; import jakarta.persistence.*; -import jakarta.validation.constraints.NotNull; -import java.util.Collection; -import java.util.UUID; import lombok.*; import lombok.experimental.FieldNameConstants; diff --git a/src/main/java/bio/overture/ego/service/VisaPermissionService.java b/src/main/java/bio/overture/ego/service/VisaPermissionService.java index 21b98ce3..6057ce5b 100644 --- a/src/main/java/bio/overture/ego/service/VisaPermissionService.java +++ b/src/main/java/bio/overture/ego/service/VisaPermissionService.java @@ -8,9 +8,9 @@ import bio.overture.ego.model.entity.VisaPermission; import bio.overture.ego.model.exceptions.NotFoundException; import bio.overture.ego.repository.VisaPermissionRepository; +import jakarta.validation.constraints.NotNull; import java.util.List; import java.util.UUID; -import javax.validation.constraints.NotNull; import lombok.NonNull; import lombok.extern.slf4j.Slf4j; import lombok.val; diff --git a/src/main/java/bio/overture/ego/service/VisaService.java b/src/main/java/bio/overture/ego/service/VisaService.java index cac9c9b4..9e65f8db 100644 --- a/src/main/java/bio/overture/ego/service/VisaService.java +++ b/src/main/java/bio/overture/ego/service/VisaService.java @@ -11,7 +11,6 @@ import jakarta.validation.constraints.NotNull; import java.util.Optional; import java.util.UUID; -import javax.validation.constraints.NotNull; import lombok.NonNull; import lombok.extern.slf4j.Slf4j; import lombok.val;