Search in sources :

Example 1 with RecursionType

use of eu.bcvsolutions.idm.core.api.domain.RecursionType in project CzechIdMng by bcvsolutions.

the class DefaultIdmIdentityContractService method toPredicates.

@Override
protected List<Predicate> toPredicates(Root<IdmIdentityContract> root, CriteriaQuery<?> query, CriteriaBuilder builder, IdmIdentityContractFilter filter) {
    List<Predicate> predicates = super.toPredicates(root, query, builder, filter);
    // quick
    if (StringUtils.isNotEmpty(filter.getText())) {
        Path<IdmTreeNode> wp = root.get(IdmIdentityContract_.workPosition);
        predicates.add(builder.or(builder.like(builder.lower(root.get(IdmIdentityContract_.position)), "%" + filter.getText().toLowerCase() + "%"), builder.like(builder.lower(wp.get(IdmTreeNode_.name)), "%" + filter.getText().toLowerCase() + "%"), builder.like(builder.lower(wp.get(IdmTreeNode_.code)), "%" + filter.getText().toLowerCase() + "%")));
    }
    if (filter.getIdentity() != null) {
        predicates.add(builder.equal(root.get(IdmIdentityContract_.identity).get(AbstractEntity_.id), filter.getIdentity()));
    }
    if (filter.getValidTill() != null) {
        predicates.add(builder.lessThanOrEqualTo(root.get(IdmIdentityContract_.validTill), filter.getValidTill()));
    }
    if (filter.getValidFrom() != null) {
        predicates.add(builder.greaterThanOrEqualTo(root.get(IdmIdentityContract_.validFrom), filter.getValidFrom()));
    }
    if (filter.getExterne() != null) {
        predicates.add(builder.equal(root.get(IdmIdentityContract_.externe), filter.getExterne()));
    }
    if (filter.getMain() != null) {
        predicates.add(builder.equal(root.get(IdmIdentityContract_.main), filter.getMain()));
    }
    if (filter.getValid() != null) {
        final LocalDate today = LocalDate.now();
        // 
        if (filter.getValid()) {
            // EXCLUDED contracts remain valid ...
            predicates.add(builder.and(RepositoryUtils.getValidPredicate(root, builder, today), builder.equal(root.get(IdmIdentityContract_.disabled), Boolean.FALSE)));
        } else {
            predicates.add(builder.or(builder.lessThan(root.get(IdmIdentityContract_.validTill), today), builder.greaterThan(root.get(IdmIdentityContract_.validFrom), today), builder.equal(root.get(IdmIdentityContract_.disabled), Boolean.TRUE)));
        }
    }
    Boolean validNowOrInFuture = filter.getValidNowOrInFuture();
    if (validNowOrInFuture != null) {
        if (validNowOrInFuture) {
            predicates.add(builder.and(builder.or(builder.greaterThanOrEqualTo(root.get(IdmIdentityContract_.validTill), LocalDate.now()), builder.isNull(root.get(IdmIdentityContract_.validTill))), builder.equal(root.get(IdmIdentityContract_.disabled), Boolean.FALSE)));
        } else {
            predicates.add(builder.lessThan(root.get(IdmIdentityContract_.validTill), LocalDate.now()));
        }
    }
    if (filter.getState() != null) {
        predicates.add(builder.equal(root.get(IdmIdentityContract_.state), filter.getState()));
    }
    if (StringUtils.isNotEmpty(filter.getPosition())) {
        predicates.add(builder.equal(root.get(IdmIdentityContract_.position), filter.getPosition()));
    }
    UUID workPosition = filter.getWorkPosition();
    if (workPosition != null) {
        RecursionType recursionType = filter.getRecursionType();
        if (recursionType == RecursionType.NO) {
            // NO recursion => equals on work position only.
            predicates.add(builder.equal(root.get(IdmIdentityContract_.workPosition).get(IdmTreeNode_.id), filter.getWorkPosition()));
        } else {
            // prepare subquery for tree nodes and index
            Subquery<IdmTreeNode> subqueryTreeNode = query.subquery(IdmTreeNode.class);
            Root<IdmTreeNode> subqueryTreeNodeRoot = subqueryTreeNode.from(IdmTreeNode.class);
            subqueryTreeNode.select(subqueryTreeNodeRoot);
            // 
            if (recursionType == RecursionType.DOWN) {
                subqueryTreeNode.where(builder.and(builder.equal(subqueryTreeNodeRoot.get(IdmTreeNode_.id), workPosition), builder.equal(root.get(IdmIdentityContract_.workPosition).get(IdmTreeNode_.treeType), subqueryTreeNodeRoot.get(IdmTreeNode_.treeType)), builder.between(root.get(IdmIdentityContract_.workPosition).get(IdmTreeNode_.forestIndex).get(IdmForestIndexEntity_.lft), subqueryTreeNodeRoot.get(IdmTreeNode_.forestIndex).get(IdmForestIndexEntity_.lft), subqueryTreeNodeRoot.get(IdmTreeNode_.forestIndex).get(IdmForestIndexEntity_.rgt))));
            } else {
                // UP
                subqueryTreeNode.where(builder.and(builder.equal(subqueryTreeNodeRoot.get(IdmTreeNode_.id), workPosition), builder.equal(root.get(IdmIdentityContract_.workPosition).get(IdmTreeNode_.treeType), subqueryTreeNodeRoot.get(IdmTreeNode_.treeType)), builder.between(subqueryTreeNodeRoot.get(IdmTreeNode_.forestIndex).get(IdmForestIndexEntity_.lft), root.get(IdmIdentityContract_.workPosition).get(IdmTreeNode_.forestIndex).get(IdmForestIndexEntity_.lft), root.get(IdmIdentityContract_.workPosition).get(IdmTreeNode_.forestIndex).get(IdmForestIndexEntity_.rgt))));
            }
            // 
            predicates.add(builder.exists(subqueryTreeNode));
        }
    }
    Boolean excluded = filter.getExcluded();
    if (excluded != null) {
        Predicate excludedPredicate = builder.equal(root.get(IdmIdentityContract_.state), ContractState.EXCLUDED);
        if (excluded) {
            predicates.add(excludedPredicate);
        } else {
            predicates.add(builder.or(builder.not(excludedPredicate), builder.isNull(root.get(IdmIdentityContract_.state))));
        }
    }
    UUID roleId = filter.getRoleId();
    if (roleId != null) {
        Subquery<IdmIdentityRole> identityRoleSubquery = query.subquery(IdmIdentityRole.class);
        Root<IdmIdentityRole> subRootIdentityRole = identityRoleSubquery.from(IdmIdentityRole.class);
        identityRoleSubquery.select(subRootIdentityRole);
        identityRoleSubquery.where(builder.and(builder.equal(subRootIdentityRole.get(IdmIdentityRole_.identityContract), root), builder.equal(subRootIdentityRole.get(IdmIdentityRole_.role).get(AbstractEntity_.id), roleId)));
        predicates.add(builder.exists(identityRoleSubquery));
    }
    // 
    return predicates;
}
Also used : RecursionType(eu.bcvsolutions.idm.core.api.domain.RecursionType) LocalDate(java.time.LocalDate) Predicate(javax.persistence.criteria.Predicate) IdmTreeNode(eu.bcvsolutions.idm.core.model.entity.IdmTreeNode) IdmIdentityRole(eu.bcvsolutions.idm.core.model.entity.IdmIdentityRole) UUID(java.util.UUID)

Example 2 with RecursionType

use of eu.bcvsolutions.idm.core.api.domain.RecursionType in project CzechIdMng by bcvsolutions.

the class DefaultIdmContractPositionService method toPredicates.

@Override
protected List<Predicate> toPredicates(Root<IdmContractPosition> root, CriteriaQuery<?> query, CriteriaBuilder builder, IdmContractPositionFilter filter) {
    List<Predicate> predicates = super.toPredicates(root, query, builder, filter);
    // contract id
    UUID contractId = filter.getIdentityContractId();
    if (contractId != null) {
        predicates.add(builder.equal(root.get(IdmContractPosition_.identityContract).get(IdmIdentityContract_.id), contractId));
    }
    // tree node id
    UUID workPosition = filter.getWorkPosition();
    if (workPosition != null) {
        RecursionType recursionType = filter.getRecursionType();
        if (recursionType == RecursionType.NO) {
            // NO recursion => equals on work position only.
            predicates.add(builder.equal(root.get(IdmContractPosition_.workPosition).get(IdmTreeNode_.id), workPosition));
        } else {
            // prepare subquery for tree nodes and index
            Subquery<IdmTreeNode> subqueryTreeNode = query.subquery(IdmTreeNode.class);
            Root<IdmTreeNode> subqueryTreeNodeRoot = subqueryTreeNode.from(IdmTreeNode.class);
            subqueryTreeNode.select(subqueryTreeNodeRoot);
            // 
            if (recursionType == RecursionType.DOWN) {
                subqueryTreeNode.where(builder.and(builder.equal(subqueryTreeNodeRoot.get(IdmTreeNode_.id), workPosition), builder.equal(root.get(IdmContractPosition_.workPosition).get(IdmTreeNode_.treeType), subqueryTreeNodeRoot.get(IdmTreeNode_.treeType)), builder.between(root.get(IdmContractPosition_.workPosition).get(IdmTreeNode_.forestIndex).get(IdmForestIndexEntity_.lft), subqueryTreeNodeRoot.get(IdmTreeNode_.forestIndex).get(IdmForestIndexEntity_.lft), subqueryTreeNodeRoot.get(IdmTreeNode_.forestIndex).get(IdmForestIndexEntity_.rgt))));
            } else {
                // UP
                subqueryTreeNode.where(builder.and(builder.equal(subqueryTreeNodeRoot.get(IdmTreeNode_.id), workPosition), builder.equal(root.get(IdmContractPosition_.workPosition).get(IdmTreeNode_.treeType), subqueryTreeNodeRoot.get(IdmTreeNode_.treeType)), builder.between(subqueryTreeNodeRoot.get(IdmTreeNode_.forestIndex).get(IdmForestIndexEntity_.lft), root.get(IdmContractPosition_.workPosition).get(IdmTreeNode_.forestIndex).get(IdmForestIndexEntity_.lft), root.get(IdmContractPosition_.workPosition).get(IdmTreeNode_.forestIndex).get(IdmForestIndexEntity_.rgt))));
            }
            // 
            predicates.add(builder.exists(subqueryTreeNode));
        }
    }
    UUID identity = filter.getIdentity();
    if (identity != null) {
        predicates.add(builder.equal(root.get(IdmContractPosition_.identityContract).get(IdmIdentityContract_.identity).get(IdmIdentity_.id), identity));
    }
    Boolean validNowOrInFuture = filter.getValidNowOrInFuture();
    if (validNowOrInFuture != null) {
        Path<IdmIdentityContract> pathContract = root.get(IdmContractPosition_.identityContract);
        // 
        if (validNowOrInFuture) {
            predicates.add(builder.and(builder.or(builder.greaterThanOrEqualTo(pathContract.get(IdmIdentityContract_.validTill), LocalDate.now()), builder.isNull(pathContract.get(IdmIdentityContract_.validTill))), builder.equal(pathContract.get(IdmIdentityContract_.disabled), Boolean.FALSE)));
        } else {
            predicates.add(builder.lessThan(pathContract.get(IdmIdentityContract_.validTill), LocalDate.now()));
        }
    }
    // 
    return predicates;
}
Also used : RecursionType(eu.bcvsolutions.idm.core.api.domain.RecursionType) IdmTreeNode(eu.bcvsolutions.idm.core.model.entity.IdmTreeNode) UUID(java.util.UUID) IdmIdentityContract(eu.bcvsolutions.idm.core.model.entity.IdmIdentityContract) Predicate(javax.persistence.criteria.Predicate)

Aggregations

RecursionType (eu.bcvsolutions.idm.core.api.domain.RecursionType)2 IdmTreeNode (eu.bcvsolutions.idm.core.model.entity.IdmTreeNode)2 UUID (java.util.UUID)2 Predicate (javax.persistence.criteria.Predicate)2 IdmIdentityContract (eu.bcvsolutions.idm.core.model.entity.IdmIdentityContract)1 IdmIdentityRole (eu.bcvsolutions.idm.core.model.entity.IdmIdentityRole)1 LocalDate (java.time.LocalDate)1