Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/p002271 6864 optimization and batch operations #24

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package org.tkit.onecx.permission.domain.daos;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.persistence.NoResultException;
import jakarta.persistence.criteria.Predicate;
import jakarta.transaction.Transactional;

import org.tkit.onecx.permission.domain.criteria.AssignmentSearchCriteria;
Expand Down Expand Up @@ -59,6 +61,28 @@ public PageResult<Assignment> findByCriteria(AssignmentSearchCriteria criteria)
}
}

public void deleteByCriteria(String roleId, List<String> productNames, String permissionId) {
var cb = getEntityManager().getCriteriaBuilder();
var dq = this.deleteQuery();
var root = dq.from(Assignment.class);

List<Predicate> predicates = new ArrayList<>();

predicates.add(cb.equal(root.get(Assignment_.ROLE).get(TraceableEntity_.ID), roleId));

if (productNames != null) {
predicates.add(root.get(Assignment_.PERMISSION).get(Permission_.PRODUCT_NAME).in(productNames));
}

if (permissionId != null) {
predicates.add(cb.equal(root.get(Assignment_.PERMISSION).get(TraceableEntity_.ID), permissionId));
}

dq.where(cb.and(predicates.toArray(new Predicate[0])));

this.getEntityManager().createQuery(dq).executeUpdate();
}

public enum ErrorKeys {

FIND_ENTITY_BY_ID_FAILED,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ public List<Permission> loadByAppId(String appId) {
}
}

public List<Permission> loadByProductNames(List<String> productNames) {
try {
var cb = this.getEntityManager().getCriteriaBuilder();
var cq = cb.createQuery(Permission.class);
var root = cq.from(Permission.class);
cq.where(root.get(Permission_.PRODUCT_NAME).in(productNames));
return this.getEntityManager().createQuery(cq).getResultList();
} catch (Exception ex) {
throw new DAOException(ErrorKeys.ERROR_LOAD_BY_PRODUCT_NAMES, ex);
}
}

public List<Permission> findPermissionForUser(String appId, List<String> roles) {
try {
var cb = this.getEntityManager().getCriteriaBuilder();
Expand All @@ -78,6 +90,7 @@ public enum ErrorKeys {

ERROR_FIND_PERMISSION_FOR_USER,
ERROR_LOAD_BY_APP_ID,
ERROR_FIND_PERMISSION_BY_CRITERIA;
ERROR_FIND_PERMISSION_BY_CRITERIA,
ERROR_LOAD_BY_PRODUCT_NAMES;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;
import jakarta.validation.ConstraintViolationException;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.Response;
Expand All @@ -18,9 +19,7 @@
import org.tkit.quarkus.log.cdi.LogService;

import gen.org.tkit.onecx.permission.rs.internal.AssignmentInternalApi;
import gen.org.tkit.onecx.permission.rs.internal.model.AssignmentSearchCriteriaDTO;
import gen.org.tkit.onecx.permission.rs.internal.model.CreateAssignmentRequestDTO;
import gen.org.tkit.onecx.permission.rs.internal.model.ProblemDetailResponseDTO;
import gen.org.tkit.onecx.permission.rs.internal.model.*;

@LogService
@ApplicationScoped
Expand Down Expand Up @@ -53,6 +52,16 @@ public Response getAssignment(String id) {
return Response.ok(mapper.map(data)).build();
}

@Override
@Transactional
public Response revokeAssignments(RevokeAssignmentRequestDTO createRevokeAssignmentRequestDTO) {
dao.deleteByCriteria(createRevokeAssignmentRequestDTO.getRoleId(),
createRevokeAssignmentRequestDTO.getProductNames(),
createRevokeAssignmentRequestDTO.getPermissionId());

return Response.status(Response.Status.NO_CONTENT).build();
}

@Override
public Response searchAssignments(AssignmentSearchCriteriaDTO assignmentSearchCriteriaDTO) {
var criteria = mapper.map(assignmentSearchCriteriaDTO);
Expand All @@ -79,6 +88,26 @@ public Response createAssignment(CreateAssignmentRequestDTO createAssignmentRequ
.build();
}

@Override
@Transactional
public Response createProductAssignment(CreateProductAssignmentRequestDTO createAssignmentRequestDTO) {
var role = roleDAO.findById(createAssignmentRequestDTO.getRoleId());
if (role == null) {
return Response.status(Response.Status.NOT_FOUND).build();
}

var permissions = permissionDAO.loadByProductNames(createAssignmentRequestDTO.getProductNames());
if (permissions.isEmpty()) {
return Response.status(Response.Status.NOT_FOUND).build();
}
var data = mapper.createList(role, permissions);

dao.deleteByCriteria(role.getId(), createAssignmentRequestDTO.getProductNames(), null);
dao.create(data);
return Response.status(Response.Status.CREATED).build();

}

@Override
public Response deleteAssignment(String id) {
dao.deleteQueryById(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ public Response createRole(CreateRoleRequestDTO createRoleRequestDTO) {
role = dao.create(role);
return Response
.created(uriInfo.getAbsolutePathBuilder().path(role.getId()).build())
.entity(mapper.map(role))
.build();
.entity(mapper.map(role)).build();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ public List<Item> getClasses() {
}),
item(10, CreateAssignmentRequestDTO.class, x -> {
CreateAssignmentRequestDTO d = (CreateAssignmentRequestDTO) x;
return CreateAssignmentRequestDTO.class.getSimpleName() + ":r=" + d.getRoleId() + ",p="
return CreateAssignmentRequestDTO.class.getSimpleName() + ":r=" + d.getRoleId() + ":p="
+ d.getPermissionId();
}),
item(10, CreateRoleRequestDTO.class,
x -> x.getClass().getSimpleName() + ":" + ((CreateRoleRequestDTO) x).getName()),
x -> x.getClass().getSimpleName() + ": size: " + ((CreateRoleRequestDTO) x).getName()),
item(10, UpdateRoleRequestDTO.class,
x -> x.getClass().getSimpleName() + ":" + ((UpdateRoleRequestDTO) x).getName()));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.tkit.onecx.permission.rs.internal.mappers;

import java.util.ArrayList;
import java.util.List;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.tkit.onecx.permission.domain.criteria.AssignmentSearchCriteria;
Expand Down Expand Up @@ -36,4 +39,10 @@ public interface AssignmentMapper {

@Mapping(target = "appId", source = "permission.appId")
AssignmentDTO map(Assignment data);

default List<Assignment> createList(Role role, List<Permission> permissions) {
List<Assignment> assignments = new ArrayList<>();
permissions.forEach(permission -> assignments.add(create(role, permission)));
return assignments;
}
}
75 changes: 71 additions & 4 deletions src/main/openapi/onecx-permission-internal-openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,50 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/ProblemDetailResponse'
/internal/assignments/product:
post:
tags:
- assignmentInternal
description: Create new assignment
operationId: createProductAssignment
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/CreateProductAssignmentRequest'
responses:
201:
description: New assignment created
404:
description: Data not found
400:
description: Bad request
content:
application/json:
schema:
$ref: '#/components/schemas/ProblemDetailResponse'
/internal/assignments/revoke:
post:
tags:
- assignmentInternal
description: delete assignments by criteria
operationId: revokeAssignments
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/RevokeAssignmentRequest'
responses:
204:
description: New assignment created
400:
description: Bad request
content:
application/json:
schema:
$ref: '#/components/schemas/ProblemDetailResponse'
/internal/assignments/{id}:
get:
tags:
Expand Down Expand Up @@ -318,6 +362,19 @@ components:
type: array
items:
$ref: '#/components/schemas/Assignment'
RevokeAssignmentRequest:
type: object
required:
- roleId
properties:
roleId:
type: string
permissionId:
type: string
productNames:
type: array
items:
type: string
CreateAssignmentRequest:
type: object
required:
Expand All @@ -328,6 +385,18 @@ components:
type: string
permissionId:
type: string
CreateProductAssignmentRequest:
type: object
required:
- roleId
- productNames
properties:
roleId:
type: string
productNames:
type: array
items:
type: string
Assignment:
type: object
properties:
Expand All @@ -348,6 +417,8 @@ components:
type: string
appId:
type: string
id:
type: string
UpdateRoleRequest:
type: object
required:
Expand All @@ -358,17 +429,13 @@ components:
type: integer
name:
type: string
shortDescription:
type: string
description:
type: string
CreateRoleRequest:
type: object
properties:
name:
type: string
shortDescription:
type: string
description:
type: string
Role:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ void methodExceptionTests() {
PermissionDAO.ErrorKeys.ERROR_FIND_PERMISSION_FOR_USER);
methodExceptionTests(() -> dao.loadByAppId(null),
PermissionDAO.ErrorKeys.ERROR_LOAD_BY_APP_ID);
methodExceptionTests(() -> dao.loadByProductNames(null),
PermissionDAO.ErrorKeys.ERROR_LOAD_BY_PRODUCT_NAMES);
methodExceptionTests(() -> dao.findByCriteria(null),
PermissionDAO.ErrorKeys.ERROR_FIND_PERMISSION_BY_CRITERIA);
}
Expand Down
Loading
Loading