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