Search in sources :

Example 6 with IdmTreeNode

use of eu.bcvsolutions.idm.core.model.entity.IdmTreeNode in project CzechIdMng by bcvsolutions.

the class DefaultIdmContractSliceService method toPredicates.

@Override
protected List<Predicate> toPredicates(Root<IdmContractSlice> root, CriteriaQuery<?> query, CriteriaBuilder builder, IdmContractSliceFilter filter) {
    List<Predicate> predicates = super.toPredicates(root, query, builder, filter);
    // quick
    if (StringUtils.isNotEmpty(filter.getText())) {
        Path<IdmTreeNode> wp = root.get(IdmContractSlice_.workPosition);
        predicates.add(builder.or(builder.like(builder.lower(root.get(IdmContractSlice_.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.getTreeNode() != null) {
        predicates.add(builder.equal(root.get(IdmContractSlice_.workPosition).get(AbstractEntity_.id), filter.getTreeNode()));
    }
    if (filter.getIdentity() != null) {
        predicates.add(builder.equal(root.get(IdmContractSlice_.identity).get(AbstractEntity_.id), filter.getIdentity()));
    }
    if (filter.getValidTill() != null) {
        predicates.add(builder.lessThanOrEqualTo(root.get(IdmContractSlice_.validTill), filter.getValidTill()));
    }
    if (filter.getValidFrom() != null) {
        predicates.add(builder.greaterThanOrEqualTo(root.get(IdmContractSlice_.validFrom), filter.getValidFrom()));
    }
    if (filter.getExterne() != null) {
        predicates.add(builder.equal(root.get(IdmContractSlice_.externe), filter.getExterne()));
    }
    if (filter.getMain() != null) {
        predicates.add(builder.equal(root.get(IdmContractSlice_.main), filter.getMain()));
    }
    if (filter.getValid() != null) {
        if (filter.getValid()) {
            final LocalDate today = LocalDate.now();
            predicates.add(builder.and(builder.or(builder.lessThanOrEqualTo(root.get(IdmContractSlice_.validFrom), today), builder.isNull(root.get(IdmContractSlice_.validFrom))), builder.or(builder.greaterThanOrEqualTo(root.get(IdmContractSlice_.validTill), today), builder.isNull(root.get(IdmContractSlice_.validTill))), builder.equal(root.get(IdmContractSlice_.disabled), Boolean.FALSE)));
        } else {
            final LocalDate today = LocalDate.now();
            predicates.add(builder.or(builder.lessThan(root.get(IdmContractSlice_.validTill), today), builder.greaterThan(root.get(IdmContractSlice_.validFrom), today), builder.equal(root.get(IdmContractSlice_.disabled), Boolean.TRUE)));
        }
    }
    if (filter.getValidNowOrInFuture() != null) {
        if (filter.getValidNowOrInFuture()) {
            predicates.add(builder.and(builder.or(builder.greaterThanOrEqualTo(root.get(IdmContractSlice_.validTill), LocalDate.now()), builder.isNull(root.get(IdmContractSlice_.validTill))), builder.equal(root.get(IdmContractSlice_.disabled), Boolean.FALSE)));
        } else {
            predicates.add(builder.lessThan(root.get(IdmContractSlice_.validTill), LocalDate.now()));
        }
    }
    if (filter.getState() != null) {
        predicates.add(builder.equal(root.get(IdmContractSlice_.state), filter.getState()));
    }
    if (filter.getExcludeContract() != null) {
        predicates.add(builder.notEqual(root.get(IdmContractSlice_.id), filter.getExcludeContract()));
    }
    if (filter.getParentContract() != null) {
        predicates.add(builder.equal(root.get(IdmContractSlice_.parentContract).get(IdmIdentityContract_.id), filter.getParentContract()));
    }
    if (filter.getContractCode() != null) {
        predicates.add(builder.equal(root.get(IdmContractSlice_.contractCode), filter.getContractCode()));
    }
    if (filter.getUsingAsContract() != null) {
        predicates.add(builder.equal(root.get(IdmContractSlice_.usingAsContract), filter.getUsingAsContract()));
    }
    if (filter.getShouldBeUsingAsContract() != null) {
        if (filter.getShouldBeUsingAsContract()) {
            final LocalDate today = LocalDate.now();
            predicates.add(builder.and(builder.or(builder.lessThanOrEqualTo(root.get(IdmContractSlice_.validFrom), today), builder.isNull(root.get(IdmContractSlice_.validFrom))), builder.or(builder.greaterThanOrEqualTo(root.get(IdmContractSlice_.validTill), today), builder.isNull(root.get(IdmContractSlice_.validTill)))));
        } else {
            final LocalDate today = LocalDate.now();
            predicates.add(builder.or(builder.greaterThan(root.get(IdmContractSlice_.validFrom), today), builder.lessThan(root.get(IdmContractSlice_.validTill), today)));
        }
    }
    // 
    return predicates;
}
Also used : IdmTreeNode(eu.bcvsolutions.idm.core.model.entity.IdmTreeNode) LocalDate(java.time.LocalDate) Predicate(javax.persistence.criteria.Predicate)

Example 7 with IdmTreeNode

use of eu.bcvsolutions.idm.core.model.entity.IdmTreeNode in project CzechIdMng by bcvsolutions.

the class DefaultIdmTreeNodeService method toPredicates.

@Override
protected List<Predicate> toPredicates(Root<IdmTreeNode> root, CriteriaQuery<?> query, CriteriaBuilder builder, IdmTreeNodeFilter filter) {
    List<Predicate> predicates = super.toPredicates(root, query, builder, filter);
    // fulltext
    if (!StringUtils.isEmpty(filter.getText())) {
        predicates.add(builder.or(builder.like(builder.lower(root.get(IdmTreeNode_.code)), "%" + filter.getText().toLowerCase() + "%"), builder.like(builder.lower(root.get(IdmTreeNode_.name)), "%" + filter.getText().toLowerCase() + "%")));
    }
    // tree type identifier
    if (filter.getTreeTypeId() != null) {
        predicates.add(builder.equal(root.get(IdmTreeNode_.treeType).get(AbstractEntity_.id), filter.getTreeTypeId()));
    }
    // tree type code
    String treeTypeCode = filter.getTreeTypeCode();
    if (StringUtils.isNotEmpty(treeTypeCode)) {
        predicates.add(builder.equal(root.get(IdmTreeNode_.treeType).get(IdmTreeType_.code), treeTypeCode));
    }
    // parent node
    UUID parent = filter.getParent();
    if (parent != null) {
        if (filter.isRecursively()) {
            // forest index needs tree type => same numbers in different trees
            Subquery<IdmTreeType> subqueryTreeType = query.subquery(IdmTreeType.class);
            Root<IdmTreeNode> subRootTreeType = subqueryTreeType.from(IdmTreeNode.class);
            subqueryTreeType.select(subRootTreeType.get(IdmTreeNode_.treeType));
            subqueryTreeType.where(builder.equal(subRootTreeType.get(IdmTreeNode_.id), parent));
            // 
            Subquery<IdmTreeNode> subquery = query.subquery(IdmTreeNode.class);
            Root<IdmTreeNode> subRoot = subquery.from(IdmTreeNode.class);
            subquery.select(subRoot);
            Join<IdmTreeNode, IdmForestIndexEntity> forestIndexPath = subRoot.join(IdmTreeNode_.forestIndex);
            subquery.where(builder.and(builder.equal(subRoot.get(IdmTreeNode_.id), parent), // join tree type
            builder.equal(root.get(IdmTreeNode_.treeType), subqueryTreeType), // Remember that between clause looks like this a >= x <= b, where a and b are boundaries, in our case lft+1 and rgt-1.
            builder.between(root.join(IdmTreeNode_.forestIndex).get(IdmForestIndexEntity_.lft), builder.sum(forestIndexPath.get(IdmForestIndexEntity_.lft), 1L), builder.diff(forestIndexPath.get(IdmForestIndexEntity_.rgt), 1L))));
            predicates.add(builder.exists(subquery));
        } else {
            predicates.add(builder.equal(root.get(IdmTreeNode_.parent).get(AbstractEntity_.id), parent));
        }
    }
    // default tree type
    if (filter.getDefaultTreeType() != null) {
        IdmTreeTypeDto defaultTreeType = treeTypeService.getDefaultTreeType();
        if (defaultTreeType == null) {
            // nothing to find
            predicates.add(builder.disjunction());
        } else {
            predicates.add(builder.equal(root.get(IdmTreeNode_.treeType).get(IdmTreeType_.id), defaultTreeType.getId()));
        }
    }
    // roots
    Boolean roots = filter.getRoots();
    if (roots != null) {
        if (roots) {
            predicates.add(builder.isNull(root.get(IdmTreeNode_.parent)));
        } else {
            predicates.add(builder.isNotNull(root.get(IdmTreeNode_.parent)));
        }
    }
    // 
    return predicates;
}
Also used : IdmTreeType(eu.bcvsolutions.idm.core.model.entity.IdmTreeType) Predicate(javax.persistence.criteria.Predicate) IdmTreeTypeDto(eu.bcvsolutions.idm.core.api.dto.IdmTreeTypeDto) IdmTreeNode(eu.bcvsolutions.idm.core.model.entity.IdmTreeNode) UUID(java.util.UUID) IdmForestIndexEntity(eu.bcvsolutions.idm.core.model.entity.IdmForestIndexEntity)

Example 8 with IdmTreeNode

use of eu.bcvsolutions.idm.core.model.entity.IdmTreeNode in project CzechIdMng by bcvsolutions.

the class DefaultIdmRoleTreeNodeService method getAutomaticRolesByTreeNode.

@Override
@Transactional(readOnly = true)
public Set<IdmRoleTreeNodeDto> getAutomaticRolesByTreeNode(UUID workPosition) {
    Assert.notNull(workPosition, "Tree node (work position) is required.");
    // TODO: we need actual forest index - use uuid and rewrite to subquery
    IdmTreeNode treeNode = treeNodeRepository.findById(workPosition).get();
    // 
    return new HashSet<>(toDtos(repository.findAutomaticRoles(treeNode), false));
}
Also used : IdmTreeNode(eu.bcvsolutions.idm.core.model.entity.IdmTreeNode) HashSet(java.util.HashSet) Transactional(org.springframework.transaction.annotation.Transactional)

Example 9 with IdmTreeNode

use of eu.bcvsolutions.idm.core.model.entity.IdmTreeNode in project CzechIdMng by bcvsolutions.

the class TreeNodeAndTypeRestTest method changeType.

@Test
public void changeType() {
    IdmTreeTypeDto type = getHelper().createTreeType();
    IdmTreeTypeDto type2 = getHelper().createTreeType();
    // save node trought rest
    Map<String, String> body = new HashMap<>();
    body.put("code", getHelper().createName());
    body.put("name", getHelper().createName());
    body.put("treeType", type.getId().toString());
    String jsonContent = toJson(body);
    int status = 0;
    Exception ex = null;
    // test save without privileges
    try {
        status = getMockMvc().perform(post(BaseDtoController.BASE_PATH + "/tree-nodes").content(jsonContent).contentType(MediaType.APPLICATION_JSON)).andReturn().getResponse().getStatus();
    } catch (Exception e) {
        ex = e;
    }
    assertNull(ex);
    assertEquals(403, status);
    // test with privileges
    try {
        status = getMockMvc().perform(post(BaseDtoController.BASE_PATH + "/tree-nodes").with(authentication(getAdminAuthentication())).content(jsonContent).contentType(MediaType.APPLICATION_JSON)).andReturn().getResponse().getStatus();
    } catch (Exception e) {
        ex = e;
    }
    assertNull(ex);
    assertEquals(201, status);
    Page<IdmTreeNode> nodes = this.treeNodeRepository.findChildren(type.getId(), null, PageRequest.of(0, 1));
    assertFalse(nodes.getContent().isEmpty());
    IdmTreeNode node = nodes.getContent().get(0);
    // change treeType
    body.put("id", node.getId().toString());
    body.put("name", node.getName() + "_update");
    body.put("treeType", type2.getId().toString());
    jsonContent = toJson(body);
    status = 0;
    ex = null;
    try {
        status = getMockMvc().perform(post(BaseDtoController.BASE_PATH + "/tree-nodes/").with(authentication(getAdminAuthentication())).content(jsonContent).contentType(MediaType.APPLICATION_JSON)).andReturn().getResponse().getStatus();
    } catch (Exception e) {
        ex = e;
    }
    assertEquals(400, status);
    assertNull(ex);
}
Also used : IdmTreeTypeDto(eu.bcvsolutions.idm.core.api.dto.IdmTreeTypeDto) IdmTreeNode(eu.bcvsolutions.idm.core.model.entity.IdmTreeNode) HashMap(java.util.HashMap) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IdmTreeNodeControllerRestTest(eu.bcvsolutions.idm.core.rest.impl.IdmTreeNodeControllerRestTest) Test(org.junit.Test) AbstractRestTest(eu.bcvsolutions.idm.test.api.AbstractRestTest)

Example 10 with IdmTreeNode

use of eu.bcvsolutions.idm.core.model.entity.IdmTreeNode 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)

Aggregations

IdmTreeNode (eu.bcvsolutions.idm.core.model.entity.IdmTreeNode)14 UUID (java.util.UUID)6 IdmTreeTypeDto (eu.bcvsolutions.idm.core.api.dto.IdmTreeTypeDto)4 IdmIdentityContract (eu.bcvsolutions.idm.core.model.entity.IdmIdentityContract)4 Predicate (javax.persistence.criteria.Predicate)4 IdmTreeType (eu.bcvsolutions.idm.core.model.entity.IdmTreeType)3 Test (org.junit.Test)3 RecursionType (eu.bcvsolutions.idm.core.api.domain.RecursionType)2 IdmContractSliceFilter (eu.bcvsolutions.idm.core.api.dto.filter.IdmContractSliceFilter)2 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)2 AbstractUnitTest (eu.bcvsolutions.idm.test.api.AbstractUnitTest)2 LocalDate (java.time.LocalDate)2 ArrayList (java.util.ArrayList)2 Transactional (org.springframework.transaction.annotation.Transactional)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 IdmTreeNodeDto (eu.bcvsolutions.idm.core.api.dto.IdmTreeNodeDto)1 TreeNodeException (eu.bcvsolutions.idm.core.exception.TreeNodeException)1 IdmForestIndexEntity (eu.bcvsolutions.idm.core.model.entity.IdmForestIndexEntity)1 IdmIdentityRole (eu.bcvsolutions.idm.core.model.entity.IdmIdentityRole)1 IdmTreeNodeControllerRestTest (eu.bcvsolutions.idm.core.rest.impl.IdmTreeNodeControllerRestTest)1