Search in sources :

Example 71 with PageRequest

use of org.springframework.data.domain.PageRequest in project CzechIdMng by bcvsolutions.

the class IdentityRoleExpirationTaskExecutor method process.

@Override
public Boolean process() {
    this.counter = 0L;
    // 
    int pageSize = 100;
    boolean hasNextPage = false;
    do {
        // 0 => from start - roles from previous search are already removed
        Page<IdmIdentityRoleDto> roles = service.findExpiredRoles(expiration, new PageRequest(0, pageSize));
        hasNextPage = roles.hasContent();
        if (count == null) {
            count = roles.getTotalElements();
        }
        for (Iterator<IdmIdentityRoleDto> i = roles.iterator(); i.hasNext() && hasNextPage; ) {
            IdmIdentityRoleDto role = i.next();
            LOG.debug("Remove role: [{}] from contract id: [{}].", role.getRole(), role.getIdentityContract());
            service.delete(role);
            ++counter;
            hasNextPage &= updateState();
        }
    } while (hasNextPage);
    LOG.info("Expired roles removal task ended. Removed roles: [{}].", counter);
    return Boolean.TRUE;
}
Also used : PageRequest(org.springframework.data.domain.PageRequest) IdmIdentityRoleDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityRoleDto)

Example 72 with PageRequest

use of org.springframework.data.domain.PageRequest in project CzechIdMng by bcvsolutions.

the class ProcessAutomaticRoleByAttributeTaskExecutor method process.

@Override
public Boolean process() {
    UUID automaticRoleId = getAutomaticRoleId();
    IdmAutomaticRoleAttributeDto automaticRolAttributeDto = automaticRoleAttributeService.get(automaticRoleId);
    if (automaticRoleId == null || automaticRolAttributeDto == null) {
        throw new ResultCodeException(CoreResultCode.AUTOMATIC_ROLE_TASK_EMPTY);
    }
    Set<AbstractIdmAutomaticRoleDto> setWithAutomaticRole = Sets.newHashSet(automaticRolAttributeDto);
    // 
    List<String> failedEntitiesAdd = new ArrayList<>();
    List<String> failedEntitiesRemove = new ArrayList<>();
    // 
    // by contract
    Page<UUID> newPassedContracts = automaticRoleAttributeService.getContractsForAutomaticRole(automaticRoleId, true, new PageRequest(0, PAGE_SIZE));
    Page<UUID> newNotPassedContracts = automaticRoleAttributeService.getContractsForAutomaticRole(automaticRoleId, false, new PageRequest(0, PAGE_SIZE));
    // 
    counter = 0L;
    count = Long.valueOf(newPassedContracts.getTotalElements() + newNotPassedContracts.getTotalElements());
    // 
    // assign new passed roles
    boolean canContinue = true;
    // 
    // process contracts
    canContinue = true;
    while (canContinue) {
        for (UUID contractId : newPassedContracts) {
            IdmIdentityContractDto contract = identityContractService.get(contractId);
            // check for contract validity
            if (contract.getState() == ContractState.DISABLED || !contract.isValidNowOrInFuture()) {
                continue;
            }
            // 
            try {
                automaticRoleAttributeService.addAutomaticRoles(contract, setWithAutomaticRole);
                counter++;
            } catch (Exception ex) {
                LOG.error("Error while add new automatic role id [{}] to contract id [{}] and identity id [{}]", automaticRoleId, contractId, contract.getIdentity(), ex);
                failedEntitiesAdd.add(contractId.toString());
            } finally {
                canContinue = updateState();
                if (!canContinue) {
                    break;
                }
            }
        }
        if (newPassedContracts.hasNext()) {
            newPassedContracts = automaticRoleAttributeService.getContractsForAutomaticRole(automaticRoleId, true, newPassedContracts.nextPageable());
        } else {
            break;
        }
    }
    // 
    while (canContinue) {
        for (UUID contractId : newNotPassedContracts) {
            try {
                automaticRoleAttributeService.removeAutomaticRoles(contractId, setWithAutomaticRole);
                counter++;
            } catch (Exception ex) {
                LOG.error("Error while remove automatic role id [{}] from contract id [{}].", automaticRoleId, contractId, ex);
                failedEntitiesRemove.add(contractId.toString());
            } finally {
                canContinue = updateState();
                if (!canContinue) {
                    break;
                }
            }
        }
        if (newNotPassedContracts.hasNext()) {
            newNotPassedContracts = automaticRoleAttributeService.getContractsForAutomaticRole(automaticRoleId, false, newNotPassedContracts.nextPageable());
        } else {
            break;
        }
    }
    // 
    if (!failedEntitiesAdd.isEmpty() || !failedEntitiesRemove.isEmpty()) {
        throw new ResultCodeException(CoreResultCode.AUTOMATIC_ROLE_PROCESS_TASK_NOT_COMPLETE, ImmutableMap.of("automaticRole", automaticRoleId, "failedAddEntities", StringUtils.join(failedEntitiesAdd, ","), "failedRemoveEntities", StringUtils.join(failedEntitiesRemove, ",")));
    }
    // 
    return Boolean.TRUE;
}
Also used : ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) ArrayList(java.util.ArrayList) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) IdmAutomaticRoleAttributeDto(eu.bcvsolutions.idm.core.api.dto.IdmAutomaticRoleAttributeDto) PageRequest(org.springframework.data.domain.PageRequest) AbstractIdmAutomaticRoleDto(eu.bcvsolutions.idm.core.api.dto.AbstractIdmAutomaticRoleDto) UUID(java.util.UUID) IdmIdentityContractDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityContractDto)

Example 73 with PageRequest

use of org.springframework.data.domain.PageRequest 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());
        // 
        Page<IdmTreeNode> nodes = treeNodeRepository.findByTreeType_Id(treeType.getId(), new PageRequest(0, 100, new Sort("id")));
        count = nodes.getTotalElements();
        counter = 0L;
        boolean canContinue = true;
        while (canContinue) {
            for (IdmTreeNode node : nodes) {
                if (node.getForestIndex() == null) {
                    forestIndexService.index(node.getForestTreeType(), node.getId(), node.getParentId());
                }
                counter++;
                canContinue = updateState();
                if (!canContinue) {
                    break;
                }
            }
            ;
            if (!nodes.hasNext()) {
                break;
            }
            nodes = treeNodeRepository.findByTreeType_Id(treeType.getId(), nodes.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) PageRequest(org.springframework.data.domain.PageRequest) IdmTreeNode(eu.bcvsolutions.idm.core.model.entity.IdmTreeNode) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) Sort(org.springframework.data.domain.Sort)

Example 74 with PageRequest

use of org.springframework.data.domain.PageRequest in project CzechIdMng by bcvsolutions.

the class RemoveAutomaticRoleTaskExecutor method end.

@Override
protected Boolean end(Boolean result, Exception ex) {
    Boolean ended = super.end(result, ex);
    // 
    if (BooleanUtils.isTrue(ended)) {
        IdmRoleDto role = DtoUtils.getEmbedded(getAutomaticRole(), IdmRoleTreeNode_.role, IdmRoleDto.class);
        // 
        long assignedRoles = identityRoleService.findByAutomaticRole(getAutomaticRoleId(), new PageRequest(0, 1)).getTotalElements();
        if (assignedRoles != 0) {
            LOG.debug("Remove role [{}] by automatic role [{}] is not complete, some roles [{}] remains assigned to identities.", role.getCode(), getAutomaticRole().getId(), assignedRoles);
            return ended;
        }
        // 
        LOG.debug("Remove role [{}] by automatic role [{}]", role.getCode(), getAutomaticRole().getId());
        try {
            // 
            // Find all concepts and remove relation on role tree
            IdmConceptRoleRequestFilter conceptRequestFilter = new IdmConceptRoleRequestFilter();
            conceptRequestFilter.setAutomaticRole(getAutomaticRoleId());
            // 
            List<IdmConceptRoleRequestDto> concepts = conceptRequestService.find(conceptRequestFilter, null).getContent();
            for (IdmConceptRoleRequestDto concept : concepts) {
                IdmRoleRequestDto request = roleRequestService.get(concept.getRoleRequest());
                String message = null;
                if (concept.getState().isTerminatedState()) {
                    message = MessageFormat.format("Role tree node [{0}] (reqested in concept [{1}]) was deleted (not from this role request)!", getAutomaticRoleId(), concept.getId());
                } else {
                    message = MessageFormat.format("Request change in concept [{0}], was not executed, because requested RoleTreeNode [{1}] was deleted (not from this role request)!", concept.getId(), getAutomaticRoleId());
                    concept.setState(RoleRequestState.CANCELED);
                }
                roleRequestService.addToLog(request, message);
                conceptRequestService.addToLog(concept, message);
                concept.setAutomaticRole(null);
                roleRequestService.save(request);
                conceptRequestService.save(concept);
            }
            // Find all automatic role requests and remove relation on automatic role
            if (automaticRoleId != null) {
                IdmAutomaticRoleRequestFilter automaticRoleRequestFilter = new IdmAutomaticRoleRequestFilter();
                automaticRoleRequestFilter.setAutomaticRoleId(automaticRoleId);
                automaticRoleRequestService.find(automaticRoleRequestFilter, null).getContent().forEach(request -> {
                    request.setAutomaticRole(null);
                    automaticRoleRequestService.save(request);
                // WFs cannot be cancel here, because this method can be called from the same WF
                // automaticRoleRequestService.cancel(request);
                });
            }
            // by default is this allowed
            if (this.isDeleteEntity()) {
                // delete entity
                if (getAutomaticRole() instanceof IdmRoleTreeNodeDto) {
                    roleTreeNodeService.deleteInternalById(getAutomaticRole().getId());
                } else {
                    // remove all rules
                    automaticRoleAttributeRuleService.deleteAllByAttribute(getAutomaticRole().getId());
                    automaticRoleAttributeService.deleteInternalById(getAutomaticRole().getId());
                }
            }
            // 
            LOG.debug("End: Remove role [{}] by automatic role [{}].", role.getCode(), getAutomaticRole().getId());
        // 
        } catch (Exception O_o) {
            LOG.debug("Remove role [{}] by automatic role [{}] failed", role.getCode(), getAutomaticRole().getId(), O_o);
            // 
            IdmLongRunningTaskDto task = longRunningTaskService.get(getLongRunningTaskId());
            ResultModel resultModel = new DefaultResultModel(CoreResultCode.LONG_RUNNING_TASK_FAILED, ImmutableMap.of("taskId", getLongRunningTaskId(), "taskType", task.getTaskType(), "instanceId", task.getInstanceId()));
            saveResult(resultModel, OperationState.EXCEPTION, O_o);
        }
    }
    // 
    return ended;
}
Also used : IdmRoleDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleDto) IdmLongRunningTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto) IdmRoleTreeNodeDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleTreeNodeDto) DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) ResultModel(eu.bcvsolutions.idm.core.api.dto.ResultModel) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) IdmConceptRoleRequestFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmConceptRoleRequestFilter) PageRequest(org.springframework.data.domain.PageRequest) IdmConceptRoleRequestDto(eu.bcvsolutions.idm.core.api.dto.IdmConceptRoleRequestDto) IdmAutomaticRoleRequestFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmAutomaticRoleRequestFilter) IdmRoleRequestDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleRequestDto)

Example 75 with PageRequest

use of org.springframework.data.domain.PageRequest in project CzechIdMng by bcvsolutions.

the class AbstractReadDtoService method toDtoPage.

/**
 * Converts list of entities wrapped to Page object to list of DTOs wrapped
 * to Page object.
 *
 * @param entityPage
 * @return
 */
protected Page<DTO> toDtoPage(Page<E> entityPage) {
    List<DTO> dtos = this.toDtos(entityPage.getContent(), true);
    PageRequest pageRequest = null;
    if (entityPage.getSize() > 0) {
        pageRequest = new PageRequest(entityPage.getNumber(), entityPage.getSize(), entityPage.getSort());
    }
    Page<DTO> dtoPage = new PageImpl<>(dtos, pageRequest, entityPage.getTotalElements());
    return dtoPage;
}
Also used : PageImpl(org.springframework.data.domain.PageImpl) PageRequest(org.springframework.data.domain.PageRequest)

Aggregations

PageRequest (org.springframework.data.domain.PageRequest)106 Sort (org.springframework.data.domain.Sort)29 Pageable (org.springframework.data.domain.Pageable)25 Test (org.junit.Test)16 ArrayList (java.util.ArrayList)14 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)10 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)10 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)9 Transactional (org.springframework.transaction.annotation.Transactional)9 UUID (java.util.UUID)8 IdmRoleDto (eu.bcvsolutions.idm.core.api.dto.IdmRoleDto)7 ApiOperation (io.swagger.annotations.ApiOperation)7 IdmTreeTypeDto (eu.bcvsolutions.idm.core.api.dto.IdmTreeTypeDto)6 IdmTreeNode (eu.bcvsolutions.idm.core.model.entity.IdmTreeNode)6 List (java.util.List)6 IdmIdentityContractDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityContractDto)5 IdmIdentityRoleDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityRoleDto)5 IdmIdentityFilter (eu.bcvsolutions.idm.core.api.dto.filter.IdmIdentityFilter)5 AbstractIntegrationTest (eu.bcvsolutions.idm.test.api.AbstractIntegrationTest)5 PageImpl (org.springframework.data.domain.PageImpl)5