Skip to content

Commit

Permalink
[MODCONSKC-20] - Implement sharing of role capability sets
Browse files Browse the repository at this point in the history
  • Loading branch information
azizbekxm committed Aug 20, 2024
1 parent 549cb88 commit e47387c
Show file tree
Hide file tree
Showing 5 changed files with 362 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package org.folio.consortia.service.impl;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.ObjectUtils;
import org.folio.consortia.domain.dto.PublicationRequest;
import org.folio.consortia.domain.dto.SharingRoleDeleteResponse;
import org.folio.consortia.domain.dto.SharingRoleRequest;
import org.folio.consortia.domain.dto.SharingRoleResponse;
import org.folio.consortia.domain.entity.SharingRoleEntity;
import org.folio.consortia.exception.ResourceNotFoundException;
import org.folio.consortia.repository.SharingRoleRepository;
import org.folio.consortia.service.BaseSharingService;
import org.folio.consortia.service.ConsortiumService;
import org.folio.consortia.service.PublicationService;
import org.folio.consortia.service.TenantService;
import org.folio.spring.FolioExecutionContext;
import org.folio.spring.service.SystemUserScopedExecutionService;
import org.springframework.core.task.TaskExecutor;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;

@Service
@Log4j2
public class SharingRoleCapabilitySetService extends BaseSharingService<SharingRoleRequest, SharingRoleResponse, SharingRoleDeleteResponse, SharingRoleEntity> {

private static final String TYPE = "type";

private final SharingRoleRepository sharingRoleRepository;

public SharingRoleCapabilitySetService(TenantService tenantService, ConsortiumService consortiumService,
SystemUserScopedExecutionService systemUserScopedExecutionService,
PublicationService publicationService, FolioExecutionContext folioExecutionContext,
ObjectMapper parentObjectMapper, TaskExecutor asyncTaskExecutor, SharingRoleRepository sharingRoleRepository) {
super(tenantService, consortiumService, systemUserScopedExecutionService, publicationService,
folioExecutionContext, parentObjectMapper, asyncTaskExecutor);
this.sharingRoleRepository = sharingRoleRepository;
}

@Override
protected UUID getConfigId(SharingRoleRequest sharingRoleRequest) {
return sharingRoleRequest.getRoleId();
}

@Override
protected Object getPayload(SharingRoleRequest sharingRoleRequest) {
return sharingRoleRequest.getPayload();
}

@Override
protected void validateSharingConfigRequestOrThrow(UUID roleId, SharingRoleRequest sharingRoleRequest) {
if (ObjectUtils.notEqual(getConfigId(sharingRoleRequest), roleId)) {
throw new IllegalArgumentException("Mismatch id in path to roleId in request body");
}
if (Objects.isNull(getPayload(sharingRoleRequest))) {
throw new IllegalArgumentException("Payload must not be null");
}
if (!sharingRoleRepository.existsByRoleId(roleId)) {
throw new ResourceNotFoundException("roleId", String.valueOf(roleId));
}
}

@Override
protected Set<String> findTenantsByConfigId(UUID roleId) {
return sharingRoleRepository.findTenantsByRoleId(roleId);
}

@Override
protected void saveSharingConfig(List<SharingRoleEntity> sharingRoleEntityList) {
sharingRoleRepository.saveAll(sharingRoleEntityList);
}

@Override
protected void deleteSharingConfig(UUID roleId) {
sharingRoleRepository.deleteByRoleId(roleId);
}

@Override
protected PublicationRequest createPublicationRequest(SharingRoleRequest sharingRoleRequest, String httpMethod) {
PublicationRequest publicationRequest = new PublicationRequest();
publicationRequest.setMethod(httpMethod);
String url = sharingRoleRequest.getUrl();
if (httpMethod.equals(HttpMethod.PUT.toString()) || httpMethod.equals(HttpMethod.DELETE.toString())) {
url += "/" + getConfigId(sharingRoleRequest);
}
publicationRequest.setUrl(url);
publicationRequest.setPayload(getPayload(sharingRoleRequest));
publicationRequest.setTenants(new HashSet<>());
return publicationRequest;
}

@Override
protected SharingRoleEntity createSharingConfigEntityFromRequest(SharingRoleRequest sharingRoleRequest, String tenantId) {
SharingRoleEntity sharingRoleEntity = new SharingRoleEntity();
sharingRoleEntity.setId(UUID.randomUUID());
sharingRoleEntity.setRoleId(sharingRoleEntity.getRoleId());
sharingRoleEntity.setTenantId(tenantId);
return sharingRoleEntity;
}

@Override
protected SharingRoleResponse createSharingConfigResponse(UUID createRolesPcId, UUID updateRolesPcId) {
return new SharingRoleResponse()
.createRolesPCId(createRolesPcId)
.updateRolesPCId(updateRolesPcId);
}

@Override
protected SharingRoleDeleteResponse createSharingConfigResponse(UUID publishRequestId) {
return new SharingRoleDeleteResponse()
.pcId(publishRequestId);
}

@Override
protected ObjectNode updatePayload(SharingRoleRequest sharingConfigRequest, String sourceValue) {
JsonNode payload = objectMapper.convertValue(getPayload(sharingConfigRequest), JsonNode.class);
return ((ObjectNode) payload).set(TYPE, new TextNode(sourceValue));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
https://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd">

<changeSet id="MODCONSKC-20@@Implement sharing of authorization role capability sets" author="azizbekxm">

<createTable tableName="sharing_role_capability_set">
<column name="id" type="uuid">
<constraints primaryKey="true" primaryKeyName="pk_shared_policy_id"/>
</column>
<column name="role_id" type="uuid">
<constraints nullable="false"/>
</column>
<column name="capability_set_ids" type="uuid[]">
<constraints nullable="false"/>
</column>
<column name="tenant_id" type="text">
<constraints nullable="false"/>
</column>
<column name="created_by" type="uuid"/>
<column name="created_date" type="timestamp without time zone" defaultValueComputed="now()">
<constraints nullable="false"/>
</column>
<column name="updated_by" type="uuid"/>
<column name="updated_date" type="timestamp without time zone"/>
</createTable>

<createIndex indexName="capability_set_ids_idx" tableName="sharing_role_capability_set">
<column name="capability_set_ids"/>
</createIndex>
</changeSet>
</databaseChangeLog>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
https://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd">

<changeSet id="MODCONSKC-19@@Implement sharing of authorization roles" author="azizbekxm">

<createTable tableName="sharing_role">
<column name="id" type="uuid">
<constraints primaryKey="true" primaryKeyName="pk_shared_role_id"/>
</column>
<column name="role_id" type="uuid">
<constraints nullable="false"/>
</column>
<column name="tenant_id" type="text">
<constraints nullable="false"/>
</column>
<column name="created_by" type="uuid"/>
<column name="created_date" type="timestamp without time zone" defaultValueComputed="now()">
<constraints nullable="false"/>
</column>
<column name="updated_by" type="uuid"/>
<column name="updated_date" type="timestamp without time zone"/>
</createTable>

<createIndex indexName="role_id_idx" tableName="sharing_role">
<column name="role_id"/>
</createIndex>
</changeSet>
</databaseChangeLog>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
SharingRoleCapabilitySetRequest:
description: "A JSON schema for the Sharing policies object"
type: object
properties:
roleId:
description: id of sharing role record
type: string
format: uuid
url:
description: URL for publishing requests for consortia tenants
type: string
payload:
description: Http request body
type: object
additionalProperties: false
required:
- policyId
- url

SharingCapabilitySetResponse:
description: "A JSON schema for the Sharing policies object response for post request"
type: object
properties:
createPoliciesPCId:
type: string
format: uuid
updatePoliciesPCId:
type: string
format: uuid
additionalProperties: false
required:
- createPoliciesPCId
- updatePoliciesPCId

SharingPolicyDeleteResponse:
description: "A JSON schema for the Sharing policies object response for delete request"
type: object
properties:
pcId:
type: string
format: uuid
additionalProperties: false
required:
- pcId
125 changes: 125 additions & 0 deletions src/main/resources/swagger.api/sharing_role_capability_sets.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
openapi: 3.0.0
info:
title: Sharing role integration API
description: "Sharing role integration API"
version: 0.0.1
servers:
- url: /consortia/{consortiumId}/sharing
paths:
/roles:
post:
summary: start sharing role
operationId: startSharingRole
parameters:
- $ref: "#/components/parameters/consortiumId"
requestBody:
$ref: "#/components/requestBodies/SharingRoleBody"
responses:
"201":
$ref: "#/components/responses/SharingRoleResponse"
"400":
$ref: "#/components/responses/BadRequest"
"404":
$ref: "#/components/responses/NotFound"
"409":
$ref: "#/components/responses/Conflict"
"422":
$ref: "#/components/responses/Conflict"
"500":
$ref: "#/components/responses/InternalServerError"
/roles/{roleId}:
delete:
summary: delete sharing role
operationId: deleteSharingRole
parameters:
- $ref: "#/components/parameters/consortiumId"
- $ref: "#/components/parameters/roleId"
requestBody:
$ref: "#/components/requestBodies/SharingRoleBody"
responses:
"200":
$ref: "#/components/responses/SharingRoleDeleteResponse"
"400":
$ref: "#/components/responses/BadRequest"
"404":
$ref: "#/components/responses/NotFound"
"409":
$ref: "#/components/responses/Conflict"
"422":
$ref: "#/components/responses/Conflict"
"500":
$ref: "#/components/responses/InternalServerError"
components:
requestBodies:
SharingRoleBody:
description: Sharing roles object
required: true
content:
application/json:
schema:
$ref: "schemas/sharingRole.yaml#/SharingRoleRequest"
responses:
SharingRoleResponse:
description: Returns a sharing role object response for post operation
content:
application/json:
schema:
$ref: "schemas/sharingRole.yaml#/SharingRoleResponse"
SharingRoleDeleteResponse:
description: Returns a sharing role response for delete operation
content:
application/json:
schema:
$ref: "schemas/sharingRole.yaml#/SharingRoleDeleteResponse"
NoContent:
description: No content
Conflict:
description: Validation errors
content:
application/json:
schema:
$ref: "schemas/common.yaml#/Errors"
NotFound:
description: Resource not found
content:
application/json:
schema:
$ref: "schemas/common.yaml#/Errors"
BadRequest:
description: Bad request
content:
application/json:
schema:
$ref: "schemas/common.yaml#/Errors"
Unauthorized:
description: Not authorized to perform requested action
content:
text/plain:
example: unable to perform action -- unauthorized
UnprocessableEntity:
description: Validation errors
content:
application/json:
schema:
$ref: 'schemas/common.yaml#/Errors'
InternalServerError:
description: Internal server error
content:
application/json:
schema:
$ref: "schemas/common.yaml#/Error"
parameters:
consortiumId:
in: path
name: consortiumId
schema:
$ref: "schemas/common.yaml#/uuid"
required: true
description: The ID of consortium
roleId:
in: path
name: roleId
schema:
$ref: "schemas/common.yaml#/uuid"
required: true
description: The ID of role

0 comments on commit e47387c

Please sign in to comment.