Search in sources :

Example 11 with IdmTreeNode

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

Example 12 with IdmTreeNode

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

the class RebuildTreeNodeIndexTaskExecutor method process.

@Override
public Boolean process() {
    if (!configurationService.getBooleanValue(DefaultForestIndexService.PROPERTY_INDEX_ENABLED, true)) {
        throw new ResultCodeException(CoreResultCode.FOREST_INDEX_DISABLED, ImmutableMap.of("property", DefaultForestIndexService.PROPERTY_INDEX_ENABLED));
    }
    IdmTreeTypeDto treeType = getTreeType();
    String longRunningTaskId = configurationService.getValue(treeTypeService.getConfigurationPropertyName(treeTypeCode, IdmTreeTypeService.CONFIGURATION_PROPERTY_REBUILD));
    if (StringUtils.hasLength(longRunningTaskId) && !longRunningTaskId.equals(getLongRunningTaskId().toString())) {
        throw new ResultCodeException(CoreResultCode.FOREST_INDEX_RUNNING, ImmutableMap.of("type", IdmTreeNode.toForestTreeType(treeType.getId())));
    }
    // 
    LOG.info("Starting rebuilding tree node index for tree type code [{}].", treeTypeCode);
    // clear all rgt, lft
    try {
        forestIndexService.dropIndexes(IdmTreeNode.toForestTreeType(treeType.getId()));
    } finally {
        configurationService.setBooleanValue(treeTypeService.getConfigurationPropertyName(treeTypeCode, IdmTreeTypeService.CONFIGURATION_PROPERTY_VALID), false);
    }
    try {
        configurationService.setValue(treeTypeService.getConfigurationPropertyName(treeTypeCode, IdmTreeTypeService.CONFIGURATION_PROPERTY_REBUILD), getLongRunningTaskId().toString());
        // 
        count = treeNodeRepository.findByTreeType_Id(treeType.getId(), PageRequest.of(0, 1)).getTotalElements();
        counter = 0L;
        boolean canContinue = true;
        Page<IdmTreeNode> roots = treeNodeRepository.findRoots(treeType.getId(), PageRequest.of(0, 100, new Sort(Direction.ASC, AbstractEntity_.id.getName())));
        while (canContinue) {
            canContinue = processChildren(roots.getContent());
            if (!canContinue) {
                break;
            }
            if (!roots.hasNext()) {
                break;
            }
            roots = treeNodeRepository.findRoots(treeType.getId(), roots.nextPageable());
        }
        // 
        if (count.equals(counter)) {
            configurationService.deleteValue(treeTypeService.getConfigurationPropertyName(treeTypeCode, IdmTreeTypeService.CONFIGURATION_PROPERTY_VALID));
            LOG.info("Tree node index for tree type code [{}] was successfully rebuilt (index size [{}]).", treeTypeCode, counter);
            return Boolean.TRUE;
        }
        // 
        LOG.warn("Tree node index for tree type code [{}] rebuild was canceled (index size [{}]).", treeTypeCode, counter);
        return Boolean.FALSE;
    } finally {
        configurationService.deleteValue(treeTypeService.getConfigurationPropertyName(treeTypeCode, IdmTreeTypeService.CONFIGURATION_PROPERTY_REBUILD));
    }
}
Also used : IdmTreeTypeDto(eu.bcvsolutions.idm.core.api.dto.IdmTreeTypeDto) IdmTreeNode(eu.bcvsolutions.idm.core.model.entity.IdmTreeNode) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) Sort(org.springframework.data.domain.Sort)

Example 13 with IdmTreeNode

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

the class IdentityByTreeNodeEvaluator method getPredicate.

@Override
public Predicate getPredicate(Root<IdmIdentity> root, CriteriaQuery<?> query, CriteriaBuilder builder, AuthorizationPolicy policy, BasePermission... permission) {
    // check before apply evaluator
    UUID treeNodeId = getUuid(policy);
    if (treeNodeId == null) {
        return null;
    }
    // subquery to treenode (contract position -> treenode)
    Subquery<IdmIdentityContract> subquery = query.subquery(IdmIdentityContract.class);
    Root<IdmIdentityContract> subRoot = subquery.from(IdmIdentityContract.class);
    subquery.select(subRoot);
    Subquery<IdmTreeNode> subqueryTreeNode = query.subquery(IdmTreeNode.class);
    Root<IdmTreeNode> subqueryTreeNodeRoot = subqueryTreeNode.from(IdmTreeNode.class);
    subqueryTreeNode.select(subqueryTreeNodeRoot);
    subqueryTreeNode.where(builder.and(builder.equal(subqueryTreeNodeRoot.get(IdmTreeNode_.id), treeNodeId), builder.equal(subRoot.get(IdmIdentityContract_.workPosition).get(IdmTreeNode_.treeType), subqueryTreeNodeRoot.get(IdmTreeNode_.treeType)), builder.between(subRoot.get(IdmIdentityContract_.workPosition).get(IdmTreeNode_.forestIndex).get(IdmForestIndexEntity_.lft), subqueryTreeNodeRoot.get(IdmTreeNode_.forestIndex).get(IdmForestIndexEntity_.lft), subqueryTreeNodeRoot.get(IdmTreeNode_.forestIndex).get(IdmForestIndexEntity_.rgt))));
    subquery.where(builder.and(// correlation attr
    builder.equal(subRoot.get(IdmIdentityContract_.identity), root), builder.exists(subqueryTreeNode)));
    return builder.exists(subquery);
}
Also used : IdmTreeNode(eu.bcvsolutions.idm.core.model.entity.IdmTreeNode) UUID(java.util.UUID) IdmIdentityContract(eu.bcvsolutions.idm.core.model.entity.IdmIdentityContract)

Example 14 with IdmTreeNode

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

the class DefaultIdmIdentityContractServiceUnitTest method testPrimeContractWithDefaultTreeType.

@Test
public void testPrimeContractWithDefaultTreeType() {
    List<IdmIdentityContract> contracts = new ArrayList<>();
    IdmIdentityContract contractWithDefaultPosition = new IdmIdentityContract(UUID.randomUUID());
    contractWithDefaultPosition.setMain(false);
    IdmTreeNode defaultWorkPosition = new IdmTreeNode();
    defaultWorkPosition.setId(UUID.randomUUID());
    IdmTreeType defaultTreeType = new IdmTreeType(UUID.randomUUID());
    defaultWorkPosition.setTreeType(defaultTreeType);
    contractWithDefaultPosition.setWorkPosition(defaultWorkPosition);
    // 
    IdmIdentityContract otherContract = new IdmIdentityContract(UUID.randomUUID());
    otherContract.setMain(false);
    IdmTreeNode workPosition = new IdmTreeNode();
    workPosition.setId(UUID.randomUUID());
    workPosition.setTreeType(new IdmTreeType(UUID.randomUUID()));
    otherContract.setWorkPosition(workPosition);
    // 
    contracts.add(contractWithDefaultPosition);
    contracts.add(otherContract);
    // 
    when(applicationContext.getBean(LookupService.class)).thenReturn(lookupService);
    when(lookupService.toDto(any(), any(), any())).then(new LookupAnswer());
    when(repository.findAllByIdentity_Id(any(UUID.class), any())).thenReturn(contracts);
    when(treeConfiguration.getDefaultType()).thenReturn(new IdmTreeTypeDto(defaultTreeType.getId()));
    when(contractSliceService.count(any(IdmContractSliceFilter.class))).thenReturn(0L);
    // 
    Assert.assertEquals(contractWithDefaultPosition.getId(), service.getPrimeContract(UUID.randomUUID()).getId());
}
Also used : IdmTreeTypeDto(eu.bcvsolutions.idm.core.api.dto.IdmTreeTypeDto) IdmTreeNode(eu.bcvsolutions.idm.core.model.entity.IdmTreeNode) ArrayList(java.util.ArrayList) IdmContractSliceFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmContractSliceFilter) UUID(java.util.UUID) IdmIdentityContract(eu.bcvsolutions.idm.core.model.entity.IdmIdentityContract) IdmTreeType(eu.bcvsolutions.idm.core.model.entity.IdmTreeType) Test(org.junit.Test) AbstractUnitTest(eu.bcvsolutions.idm.test.api.AbstractUnitTest)

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