Reduced code duplication

This commit is contained in:
stefvdwel 2021-02-16 11:43:36 +01:00 committed by Pedro Igor
parent ee28be982f
commit 11b0c23937
2 changed files with 16 additions and 48 deletions

View file

@ -65,6 +65,19 @@ public class JPAPermissionTicketStore implements PermissionTicketStore {
querybuilder.select(root.get("id")); querybuilder.select(root.get("id"));
List<Predicate> predicates = getPredicates(builder, root, resourceServerId, attributes);
querybuilder.where(predicates.toArray(new Predicate[predicates.size()])).orderBy(builder.asc(root.get("id")));
TypedQuery query = entityManager.createQuery(querybuilder);
return query.getResultStream().count();
}
private List<Predicate> getPredicates(CriteriaBuilder builder,
Root<PermissionTicketEntity> root,
String resourceServerId,
Map<String, String> attributes) {
List<Predicate> predicates = new ArrayList(); List<Predicate> predicates = new ArrayList();
if (resourceServerId != null) { if (resourceServerId != null) {
@ -106,12 +119,7 @@ public class JPAPermissionTicketStore implements PermissionTicketStore {
throw new RuntimeException("Unsupported filter [" + name + "]"); throw new RuntimeException("Unsupported filter [" + name + "]");
} }
}); });
return predicates;
querybuilder.where(predicates.toArray(new Predicate[predicates.size()])).orderBy(builder.asc(root.get("id")));
TypedQuery query = entityManager.createQuery(querybuilder);
return query.getResultStream().count();
} }
@Override @Override
@ -234,47 +242,7 @@ public class JPAPermissionTicketStore implements PermissionTicketStore {
querybuilder.select(root.get("id")); querybuilder.select(root.get("id"));
List<Predicate> predicates = new ArrayList(); List<Predicate> predicates = getPredicates(builder, root, resourceServerId, attributes);
if (resourceServerId != null) {
predicates.add(builder.equal(root.get("resourceServer").get("id"), resourceServerId));
}
attributes.forEach((name, value) -> {
if (PermissionTicket.ID.equals(name)) {
predicates.add(root.get(name).in(value));
} else if (PermissionTicket.SCOPE.equals(name)) {
predicates.add(root.join("scope").get("id").in(value));
} else if (PermissionTicket.SCOPE_IS_NULL.equals(name)) {
if (Boolean.valueOf(value)) {
predicates.add(builder.isNull(root.get("scope")));
} else {
predicates.add(builder.isNotNull(root.get("scope")));
}
} else if (PermissionTicket.RESOURCE.equals(name)) {
predicates.add(root.join("resource").get("id").in(value));
} else if (PermissionTicket.RESOURCE_NAME.equals(name)) {
predicates.add(root.join("resource").get("name").in(value));
} else if (PermissionTicket.OWNER.equals(name)) {
predicates.add(builder.equal(root.get("owner"), value));
} else if (PermissionTicket.REQUESTER.equals(name)) {
predicates.add(builder.equal(root.get("requester"), value));
} else if (PermissionTicket.GRANTED.equals(name)) {
if (Boolean.valueOf(value)) {
predicates.add(builder.isNotNull(root.get("grantedTimestamp")));
} else {
predicates.add(builder.isNull(root.get("grantedTimestamp")));
}
} else if (PermissionTicket.REQUESTER_IS_NULL.equals(name)) {
predicates.add(builder.isNull(root.get("requester")));
} else if (PermissionTicket.POLICY_IS_NOT_NULL.equals(name)) {
predicates.add(builder.isNotNull(root.get("policy")));
} else if (PermissionTicket.POLICY.equals(name)) {
predicates.add(root.join("policy").get("id").in(value));
} else {
throw new RuntimeException("Unsupported filter [" + name + "]");
}
});
querybuilder.where(predicates.toArray(new Predicate[predicates.size()])).orderBy(builder.asc(root.get("id"))); querybuilder.where(predicates.toArray(new Predicate[predicates.size()])).orderBy(builder.asc(root.get("id")));

View file

@ -202,7 +202,7 @@ public class PermissionTicketService {
@Path("/count") @Path("/count")
@GET @GET
@Produces("application/json") @Produces("application/json")
public Response getResourceCount(@QueryParam("scopeId") String scopeId, public Response getPermissionCount(@QueryParam("scopeId") String scopeId,
@QueryParam("resourceId") String resourceId, @QueryParam("resourceId") String resourceId,
@QueryParam("owner") String owner, @QueryParam("owner") String owner,
@QueryParam("requester") String requester, @QueryParam("requester") String requester,