Search in sources :

Example 11 with ResultModel

use of eu.bcvsolutions.idm.core.api.dto.ResultModel in project CzechIdMng by bcvsolutions.

the class DefaultLongRunningTaskManager method cancelTaskByRestart.

private void cancelTaskByRestart(IdmLongRunningTaskDto task) {
    LOG.info("Cancel unprocessed long running task [{}] - tasks was interrupt during instance [{}] restart", task, task.getInstanceId());
    task.setRunning(false);
    ResultModel resultModel = new DefaultResultModel(CoreResultCode.LONG_RUNNING_TASK_CANCELED_BY_RESTART, ImmutableMap.of("taskId", task.getId(), "taskType", task.getTaskType(), "instanceId", task.getInstanceId()));
    task.setResult(new OperationResult.Builder(OperationState.CANCELED).setModel(resultModel).build());
    service.saveInternal(task);
}
Also used : DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) ResultModel(eu.bcvsolutions.idm.core.api.dto.ResultModel) OperationResult(eu.bcvsolutions.idm.core.api.entity.OperationResult)

Example 12 with ResultModel

use of eu.bcvsolutions.idm.core.api.dto.ResultModel in project CzechIdMng by bcvsolutions.

the class DefaultLongRunningTaskManager method interrupt.

@Override
@Transactional
public boolean interrupt(UUID longRunningTaskId) {
    Assert.notNull(longRunningTaskId);
    IdmLongRunningTaskDto task = service.get(longRunningTaskId);
    Assert.notNull(longRunningTaskId);
    String instanceId = configurationService.getInstanceId();
    if (!task.getInstanceId().equals(instanceId)) {
        throw new ResultCodeException(CoreResultCode.LONG_RUNNING_TASK_DIFFERENT_INSTANCE, ImmutableMap.of("taskId", longRunningTaskId, "taskInstanceId", task.getInstanceId(), "currentInstanceId", instanceId));
    }
    if (OperationState.RUNNING != task.getResult().getState()) {
        throw new ResultCodeException(CoreResultCode.LONG_RUNNING_TASK_NOT_RUNNING, ImmutableMap.of("taskId", longRunningTaskId, "taskType", task.getTaskType(), "instanceId", task.getInstanceId()));
    }
    // 
    // interrupt thread
    Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
    for (Thread thread : threadSet) {
        if (thread.getId() == task.getThreadId()) {
            ResultModel resultModel = new DefaultResultModel(CoreResultCode.LONG_RUNNING_TASK_INTERRUPT, ImmutableMap.of("taskId", task.getId(), "taskType", task.getTaskType(), "instanceId", task.getInstanceId()));
            Exception ex = null;
            try {
                thread.interrupt();
            } catch (Exception e) {
                ex = e;
                LOG.error(resultModel.toString(), e);
            }
            task.setRunning(false);
            // 
            if (ex == null) {
                LOG.info("Long running task with id: [{}], was interrupted.", task.getId());
                task.setResult(new OperationResult.Builder(OperationState.CANCELED).setModel(resultModel).build());
            } else {
                LOG.info("Long running task with id: [{}], has some exception during interrupt.", task.getId());
                task.setResult(new OperationResult.Builder(OperationState.EXCEPTION).setModel(resultModel).setCause(ex).build());
            }
            service.save(task);
            return true;
        }
    }
    LOG.warn("For long running task with id: [{}], has not found running thread.", task.getId());
    return false;
}
Also used : IdmLongRunningTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto) DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) ResultModel(eu.bcvsolutions.idm.core.api.dto.ResultModel) OperationResult(eu.bcvsolutions.idm.core.api.entity.OperationResult) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 13 with ResultModel

use of eu.bcvsolutions.idm.core.api.dto.ResultModel 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 14 with ResultModel

use of eu.bcvsolutions.idm.core.api.dto.ResultModel in project CzechIdMng by bcvsolutions.

the class AbstractLongRunningTaskExecutor method end.

/**
 * TODO: save result into long running task - blob, text?
 *
 * @param result
 * @param ex
 * @return
 */
protected V end(V result, Exception ex) {
    Assert.notNull(longRunningTaskId);
    IdmLongRunningTaskDto task = longRunningTaskService.get(longRunningTaskId);
    Assert.notNull(task, "Long running task has to be prepared before task is ended");
    LOG.debug("Long running task ends [{}] with result [{}].", longRunningTaskId, result);
    // 
    setStateProperties(task);
    // 
    if (ex != null) {
        ResultModel resultModel;
        if (ex instanceof ResultCodeException) {
            resultModel = ((ResultCodeException) ex).getError().getError();
        } else {
            resultModel = new DefaultResultModel(CoreResultCode.LONG_RUNNING_TASK_FAILED, ImmutableMap.of("taskId", longRunningTaskId, "taskType", task.getTaskType(), "instanceId", task.getInstanceId()));
        }
        LOG.error(resultModel.toString(), ex);
        task.setResult(new OperationResult.Builder(OperationState.EXCEPTION).setModel(resultModel).setCause(ex).build());
    } else if (OperationState.isRunnable(task.getResultState())) {
        // executed standardly
        LOG.debug("Long running task ended [{}] standardly, previous state [{}], result [{}].", longRunningTaskId, task.getResultState(), result);
        task.setResult(new OperationResult.Builder(OperationState.EXECUTED).build());
    }
    // 
    // publish event - LRT ended
    // TODO: result is not persisted - propagate him in event?
    entityEventManager.publishEvent(new LongRunningTaskEvent(LongRunningTaskEventType.END, task));
    // 
    return result;
}
Also used : IdmLongRunningTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto) DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) ResultModel(eu.bcvsolutions.idm.core.api.dto.ResultModel) OperationResult(eu.bcvsolutions.idm.core.api.entity.OperationResult) LongRunningTaskEvent(eu.bcvsolutions.idm.core.scheduler.api.event.LongRunningTaskEvent)

Example 15 with ResultModel

use of eu.bcvsolutions.idm.core.api.dto.ResultModel in project CzechIdMng by bcvsolutions.

the class IdmMessageDtoUnitTest method testModelOveloadLevel.

@Test
public void testModelOveloadLevel() {
    ResultModel model = new DefaultResultModel(CoreResultCode.INTERNAL_SERVER_ERROR);
    IdmMessageDto message = new IdmMessageDto.Builder().setModel(model).setLevel(NotificationLevel.SUCCESS).build();
    Assert.assertEquals(NotificationLevel.SUCCESS, message.getLevel());
}
Also used : DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) IdmMessageDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmMessageDto) DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) ResultModel(eu.bcvsolutions.idm.core.api.dto.ResultModel) AbstractUnitTest(eu.bcvsolutions.idm.test.api.AbstractUnitTest) Test(org.junit.Test)

Aggregations

DefaultResultModel (eu.bcvsolutions.idm.core.api.dto.DefaultResultModel)20 ResultModel (eu.bcvsolutions.idm.core.api.dto.ResultModel)20 IdmMessageDto (eu.bcvsolutions.idm.core.notification.api.dto.IdmMessageDto)13 OperationResult (eu.bcvsolutions.idm.core.api.entity.OperationResult)10 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)7 SysSystemDto (eu.bcvsolutions.idm.acc.dto.SysSystemDto)6 AbstractUnitTest (eu.bcvsolutions.idm.test.api.AbstractUnitTest)6 Test (org.junit.Test)6 Transactional (org.springframework.transaction.annotation.Transactional)5 SysProvisioningOperationDto (eu.bcvsolutions.idm.acc.dto.SysProvisioningOperationDto)4 IdmLongRunningTaskDto (eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto)3 SysProvisioningBatchDto (eu.bcvsolutions.idm.acc.dto.SysProvisioningBatchDto)2 DefaultEventResult (eu.bcvsolutions.idm.core.api.event.DefaultEventResult)2 PageRequest (org.springframework.data.domain.PageRequest)2 Objects (com.google.common.base.Objects)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Lists (com.google.common.collect.Lists)1 SysSystemEntityDto (eu.bcvsolutions.idm.acc.dto.SysSystemEntityDto)1 ProvisioningException (eu.bcvsolutions.idm.acc.exception.ProvisioningException)1 CoreModuleDescriptor (eu.bcvsolutions.idm.core.CoreModuleDescriptor)1