Search in sources :

Example 1 with RQLSubSelectCondition

use of com.infiniteautomation.mango.db.query.RQLSubSelectCondition in project ma-core-public by infiniteautomation.

the class RoleDao method createSubSelectMap.

@Override
protected Map<String, RQLSubSelectCondition> createSubSelectMap() {
    Map<String, RQLSubSelectCondition> subselects = super.createSubSelectMap();
    subselects.put("inherited", (operation, node) -> {
        if (operation != RQLOperation.CONTAINS) {
            throw new RQLVisitException(String.format("Unsupported node type '%s' for field '%s'", node.getName(), node.getArgument(0)));
        }
        PermissionService permissionService = permissionServiceSupplier.get();
        Set<Integer> roleIds = extractArrayArguments(node, o -> o == null ? null : o.toString()).stream().filter(Objects::nonNull).map(permissionService::getRole).filter(Objects::nonNull).map(Role::getId).collect(Collectors.toSet());
        SelectConditionStep<Record1<Integer>> afterWhere;
        if (!roleIds.isEmpty()) {
            SelectJoinStep<Record1<Integer>> select = create.select(RoleInheritance.ROLE_INHERITANCE.roleId).from(RoleInheritance.ROLE_INHERITANCE);
            afterWhere = select.where(RoleInheritance.ROLE_INHERITANCE.inheritedRoleId.in(roleIds));
        } else {
            // Find all roles with no inherited roles
            SelectJoinStep<Record1<Integer>> select = create.select(getIdField()).from(table);
            SelectOnConditionStep<Record1<Integer>> afterJoin = select.leftJoin(RoleInheritance.ROLE_INHERITANCE).on(RoleInheritance.ROLE_INHERITANCE.roleId.eq(getIdField()));
            afterWhere = afterJoin.where(RoleInheritance.ROLE_INHERITANCE.roleId.isNull());
        }
        return table.id.in(afterWhere.asField());
    });
    subselects.put("inheritedBy", (operation, node) -> {
        if (operation != RQLOperation.CONTAINS) {
            throw new RQLVisitException(String.format("Unsupported node type '%s' for field '%s'", node.getName(), node.getArgument(0)));
        }
        PermissionService permissionService = permissionServiceSupplier.get();
        Set<Integer> roleIds = extractArrayArguments(node, o -> o == null ? null : o.toString()).stream().filter(Objects::nonNull).map(permissionService::getRole).filter(Objects::nonNull).map(Role::getId).collect(Collectors.toSet());
        SelectConditionStep<Record1<Integer>> afterWhere;
        if (!roleIds.isEmpty()) {
            // Find all roles inherited by this role
            SelectJoinStep<Record1<Integer>> select = create.select(RoleInheritance.ROLE_INHERITANCE.inheritedRoleId).from(RoleInheritance.ROLE_INHERITANCE);
            afterWhere = select.where(RoleInheritance.ROLE_INHERITANCE.roleId.in(roleIds));
        } else {
            // Find all roles with that are not inherited by any role
            SelectJoinStep<Record1<Integer>> select = create.select(getIdField()).from(table);
            SelectOnConditionStep<Record1<Integer>> afterJoin = select.leftJoin(RoleInheritance.ROLE_INHERITANCE).on(RoleInheritance.ROLE_INHERITANCE.inheritedRoleId.eq(getIdField()));
            afterWhere = afterJoin.where(RoleInheritance.ROLE_INHERITANCE.inheritedRoleId.isNull());
        }
        return table.id.in(afterWhere.asField());
    });
    return subselects;
}
Also used : PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) RQLVisitException(com.infiniteautomation.mango.db.query.RQLToCondition.RQLVisitException) Objects(java.util.Objects) RQLSubSelectCondition(com.infiniteautomation.mango.db.query.RQLSubSelectCondition) Record1(org.jooq.Record1)

Aggregations

RQLSubSelectCondition (com.infiniteautomation.mango.db.query.RQLSubSelectCondition)1 RQLVisitException (com.infiniteautomation.mango.db.query.RQLToCondition.RQLVisitException)1 PermissionService (com.infiniteautomation.mango.spring.service.PermissionService)1 Objects (java.util.Objects)1 Record1 (org.jooq.Record1)1