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;
}
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;
}
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));
}
}
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;
}
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;
}
Aggregations