use of eu.bcvsolutions.idm.core.workflow.model.dto.WorkflowTaskInstanceDto in project CzechIdMng by bcvsolutions.
the class DelegationEventListener method onEvent.
@Override
public void onEvent(ActivitiEvent event) {
LOG.debug("DelegationEventListener - recieve event [{}]", event.getType());
switch(event.getType()) {
case TASK_CREATED:
if (event instanceof ActivitiEntityEventImpl && ((ActivitiEntityEventImpl) event).getEntity() instanceof TaskEntity) {
TaskEntity taskEntity = (TaskEntity) ((ActivitiEntityEventImpl) event).getEntity();
if (taskEntity != null && taskEntity.getCandidates() != null) {
String processDefinitionId = taskEntity.getProcessDefinitionId();
String processDelegationType = delegationManager.getProcessDelegationType(processDefinitionId);
if (processDelegationType == null) {
// If process doesn't have a delegation type defined, then will be used default.
processDelegationType = DefaultDelegationType.NAME;
}
String delegationType = processDelegationType;
taskEntity.getCandidates().forEach(identityLink -> {
String user = identityLink.getUserId();
Assert.notNull(user, "User id cannot be null!");
Assert.isTrue(EntityUtils.isUuid(user), "User id must be UUID in this phase!");
UUID userId = UUID.fromString(user);
WorkflowTaskInstanceDto mockTask = new WorkflowTaskInstanceDto();
mockTask.setId(taskEntity.getId());
List<IdmDelegationDefinitionDto> delegationDefinitions = delegationManager.findDelegation(delegationType, userId, null, mockTask);
if (CollectionUtils.isEmpty(delegationDefinitions)) {
// No delegation found.
return;
}
// Create delegation.
delegationDefinitions.forEach(delegationDefinition -> {
delegationManager.delegate(mockTask, delegationDefinition);
UUID delegate = delegationDefinition.getDelegate();
Assert.notNull(delegate, "Delegate cannot be null!");
taskEntity.addCandidateUser(delegate.toString());
});
// Remove delegator form task and add delegate as candidate.
taskEntity.deleteUserIdentityLink(user, identityLink.getType());
// Add delegator to the task as participant (delegator must have permission for read the task)
taskEntity.addUserIdentityLink(user, IdentityLinkType.PARTICIPANT);
});
}
}
break;
case TASK_COMPLETED:
// Check if exists task delegation for this task and set state on executed.
if (event instanceof ActivitiEntityEventImpl && ((ActivitiEntityEventImpl) event).getEntity() instanceof TaskEntity) {
TaskEntity taskEntity = (TaskEntity) ((ActivitiEntityEventImpl) event).getEntity();
if (taskEntity.getId() != null) {
IdmDelegationFilter delegationFilter = new IdmDelegationFilter();
delegationFilter.setOwnerId(DtoUtils.toUuid(taskEntity.getId()));
delegationFilter.setOwnerType(WorkflowTaskInstanceDto.class.getCanonicalName());
IdmDelegationDto delegation = delegationService.find(delegationFilter, null).getContent().stream().findFirst().orElse(null);
if (delegation != null) {
delegation.setOwnerState(new OperationResultDto(OperationState.EXECUTED));
delegationService.save(delegation);
}
}
}
break;
case ACTIVITY_CANCELLED:
// Check if exists task delegation for this cancelled activity and set state to the cancelled.
if (event instanceof ActivitiActivityCancelledEvent) {
// Find activiti task by name (event doesn't contains ID of user task)
ActivitiActivityCancelledEvent cancelledEvent = (ActivitiActivityCancelledEvent) event;
TaskQuery taskQuery = taskService.createTaskQuery();
taskQuery.taskDefinitionKey(cancelledEvent.getActivityId());
taskQuery.processInstanceId(cancelledEvent.getProcessInstanceId());
taskQuery.processDefinitionId(cancelledEvent.getProcessDefinitionId());
// Search a task.
List<Task> tasks = taskQuery.list();
if (tasks != null && tasks.size() == 1) {
Task task = tasks.get(0);
if (task.getId() != null) {
WorkflowTaskInstanceDto mockTask = new WorkflowTaskInstanceDto();
mockTask.setId(task.getId());
List<IdmDelegationDto> delegations = delegationManager.findDelegationForOwner(mockTask);
if (!CollectionUtils.isEmpty(delegations)) {
delegations.forEach(delegation -> {
delegation.setOwnerState(new OperationResultDto(OperationState.CANCELED));
delegationService.save(delegation);
});
}
}
}
}
break;
// Delete IdmDelegations using this workflow task - ensures integrity.
case ENTITY_DELETED:
if (event instanceof ActivitiEntityEventImpl && ((ActivitiEntityEventImpl) event).getEntity() instanceof HistoricTaskInstanceEntity) {
HistoricTaskInstanceEntity taskEntity = (HistoricTaskInstanceEntity) ((ActivitiEntityEventImpl) event).getEntity();
String taskId = taskEntity.getId();
Assert.notNull(taskId, "Task ID cannot be null here!");
IdmDelegationFilter delegationFilter = new IdmDelegationFilter();
delegationFilter.setOwnerId(DtoUtils.toUuid(taskId));
delegationFilter.setOwnerType(WorkflowTaskInstanceDto.class.getCanonicalName());
// Delete delegation connected to this task.
delegationService.find(delegationFilter, null).getContent().forEach(delegation -> {
delegationService.delete(delegation);
});
}
break;
default:
LOG.debug("DelegationEventListener - receive not required event [{}]", event.getType());
}
}
use of eu.bcvsolutions.idm.core.workflow.model.dto.WorkflowTaskInstanceDto in project CzechIdMng by bcvsolutions.
the class DefaultWorkflowTaskInstanceService method get.
@Override
public WorkflowTaskInstanceDto get(Serializable id, WorkflowFilterDto context, BasePermission... permission) {
if (context == null) {
context = new WorkflowFilterDto();
}
context.setId(UUID.fromString(String.valueOf(id)));
List<WorkflowTaskInstanceDto> tasks = internalSearch(context, null, permission).getContent();
if (!tasks.isEmpty()) {
return tasks.get(0);
}
// Current task doesn't exists, we try to find historic task.
WorkflowHistoricTaskInstanceDto historicTask = historicTaskInstanceService.get(String.valueOf(id), context);
if (historicTask != null) {
return historicTask;
}
return null;
}
use of eu.bcvsolutions.idm.core.workflow.model.dto.WorkflowTaskInstanceDto in project CzechIdMng by bcvsolutions.
the class ApproveRoleByManagerDelegationType method findDelegation.
@Override
public List<IdmDelegationDefinitionDto> findDelegation(UUID delegatorId, UUID contractId, BaseDto owner) {
Assert.isTrue(owner instanceof WorkflowTaskInstanceDto, "Owner must be workflow task for this delegation type!");
WorkflowTaskInstanceDto task = (WorkflowTaskInstanceDto) owner;
Object conceptRoleObj = taskInstanceService.getProcessVariable(task.getId(), ROLE_CONCEPT_PROPERTY);
if (conceptRoleObj instanceof VariableInstance) {
VariableInstance variableInstance = (VariableInstance) conceptRoleObj;
conceptRoleObj = variableInstance.getValue();
}
if (!(conceptRoleObj instanceof IdmConceptRoleRequestDto)) {
throw new CoreException(MessageFormat.format("For this delegation type [{0}], must workflow task contains '{1}' [WorkflowTaskInstanceDto] variable", NAME, ROLE_CONCEPT_PROPERTY));
}
IdmConceptRoleRequestDto conceptRole = (IdmConceptRoleRequestDto) conceptRoleObj;
UUID contractFromRequest = conceptRole.getIdentityContract();
Assert.notNull(contractFromRequest, "Contract ID from the concept of the request cannot be null!");
// Filtering managers contracts by subordinates contract.
IdmIdentityContractFilter contractFilter = new IdmIdentityContractFilter();
contractFilter.setIdentity(delegatorId);
contractFilter.setManagersByContract(contractFromRequest);
List<IdmIdentityContractDto> managersContracts = identityContractService.find(contractFilter, null).getContent();
if (managersContracts.isEmpty()) {
return null;
}
Set<IdmDelegationDefinitionDto> resultDelegations = Sets.newHashSet();
// Find all valid delegation definitions for manager's contract.
managersContracts.stream().map((managersContract) -> super.findDelegation(delegatorId, managersContract.getId(), owner)).filter((delegations) -> (!CollectionUtils.isEmpty(delegations))).forEachOrdered((delegations) -> {
resultDelegations.addAll(delegations);
});
return Lists.newArrayList(resultDelegations);
}
use of eu.bcvsolutions.idm.core.workflow.model.dto.WorkflowTaskInstanceDto in project CzechIdMng by bcvsolutions.
the class DefaultIdmConceptRoleRequestService method cancelWF.
private void cancelWF(IdmConceptRoleRequestDto dto) {
if (!Strings.isNullOrEmpty(dto.getWfProcessId())) {
WorkflowFilterDto filter = new WorkflowFilterDto();
filter.setProcessInstanceId(dto.getWfProcessId());
List<WorkflowProcessInstanceDto> resources = workflowProcessInstanceService.find(filter, null).getContent();
if (resources.isEmpty()) {
// Process with this ID not exist ... maybe was ended
this.addToLog(dto, MessageFormat.format("Workflow process with ID [{0}] was not deleted, because was not found. Maybe was ended before.", dto.getWfProcessId()));
} else {
// Before delete/cancel process we try to finish process as disapprove. Cancel
// process does not trigger the parent process. That means without correct
// ending of process, parent process will be frozen!
// Find active task for this process.
WorkflowFilterDto taskFilter = new WorkflowFilterDto();
taskFilter.setProcessInstanceId(dto.getWfProcessId());
List<WorkflowTaskInstanceDto> tasks = workflowTaskInstanceService.find(taskFilter, null).getContent();
if (tasks.size() == 1) {
WorkflowTaskInstanceDto task = tasks.get(0);
DecisionFormTypeDto disapprove = //
task.getDecisions().stream().filter(decision -> WorkflowTaskInstanceService.WORKFLOW_DECISION_DISAPPROVE.equals(decision.getId())).findFirst().orElse(null);
if (disapprove != null) {
// Active task exists and has decision for 'disapprove'. Complete task (process)
// with this decision.
workflowTaskInstanceService.completeTask(task.getId(), disapprove.getId(), null, null, null);
this.addToLog(dto, MessageFormat.format("Workflow process with ID [{0}] was disapproved, because this concept is deleted/canceled", dto.getWfProcessId()));
return;
}
}
// We wasn't able to disapprove this process, we cancel him now.
workflowProcessInstanceService.delete(dto.getWfProcessId(), "Role concept use this WF, was deleted. This WF was deleted too.");
this.addToLog(dto, MessageFormat.format("Workflow process with ID [{0}] was deleted, because this concept is deleted/canceled", dto.getWfProcessId()));
}
}
}
use of eu.bcvsolutions.idm.core.workflow.model.dto.WorkflowTaskInstanceDto in project CzechIdMng by bcvsolutions.
the class DelegationIntegrationTest method testDefaultTaskDelegationForMoreDelegates.
@Test
public void testDefaultTaskDelegationForMoreDelegates() {
// Enable approving of a role-request by manager.
getHelper().setConfigurationValue(ChangeIdentityPermissionTest.APPROVE_BY_MANAGER_ENABLE, true);
IdmRoleDto roleOne = getHelper().createRole();
IdmIdentityDto delegator = getHelper().createIdentity();
IdmIdentityDto delegateOne = getHelper().createIdentity();
IdmIdentityDto delegateTwo = getHelper().createIdentity();
IdmIdentityDto subordinate = getHelper().createIdentity();
IdmIdentityContractDto subordinateContract = getHelper().createContract(subordinate);
getHelper().createContractGuarantee(subordinateContract, delegator);
IdmIdentityFilter identityFilter = new IdmIdentityFilter();
identityFilter.setManagersByContract(subordinateContract.getId());
// Check if delegator is manager for subordinate.
boolean delegatorIsManager = getHelper().getService(IdmIdentityService.class).find(identityFilter, null).getContent().stream().filter(identity -> identity.getId().equals(delegator.getId())).findFirst().isPresent();
assertTrue(delegatorIsManager);
WorkflowFilterDto taskFilter = new WorkflowFilterDto();
taskFilter.setCandidateOrAssigned(delegator.getUsername());
List<WorkflowTaskInstanceDto> tasks = workflowTaskInstanceService.find(taskFilter, null).getContent();
assertEquals(0, tasks.size());
// Assing role -> reuqest will be in progress state.
IdmRoleRequestDto roleRequest = getHelper().createRoleRequest(subordinateContract, ConceptRoleRequestOperation.ADD, roleOne);
roleRequest.setExecuteImmediately(false);
roleRequest = roleRequestService.save(roleRequest);
roleRequest = getHelper().executeRequest(roleRequest, false, true);
assertEquals(RoleRequestState.IN_PROGRESS, roleRequest.getState());
tasks = workflowTaskInstanceService.find(taskFilter, null).getContent();
assertEquals(1, tasks.size());
List<IdentityLinkDto> identityLinks = tasks.get(0).getIdentityLinks();
assertEquals(1, identityLinks.size());
// Task is assigned to the delegator now.
assertEquals(delegator.getId(), UUID.fromString(identityLinks.get(0).getUserId()));
// Create default delegations.
IdmDelegationDefinitionDto definition = new IdmDelegationDefinitionDto();
definition.setType(DefaultDelegationType.NAME);
definition.setDelegator(delegator.getId());
definition.setDelegate(delegateOne.getId());
delegationDefinitionService.save(definition);
definition = new IdmDelegationDefinitionDto();
definition.setType(DefaultDelegationType.NAME);
definition.setDelegator(delegator.getId());
definition.setDelegate(delegateTwo.getId());
delegationDefinitionService.save(definition);
// Delete previous role-request.
roleRequestService.delete(roleRequest);
// Assing role -> reuqest will be in progress state.
roleRequest = getHelper().createRoleRequest(subordinateContract, ConceptRoleRequestOperation.ADD, roleOne);
roleRequest.setExecuteImmediately(false);
roleRequest = roleRequestService.save(roleRequest);
roleRequest = getHelper().executeRequest(roleRequest, false, true);
assertEquals(RoleRequestState.IN_PROGRESS, roleRequest.getState());
taskFilter.setCandidateOrAssigned(delegator.getUsername());
tasks = workflowTaskInstanceService.find(taskFilter, null).getContent();
// Should be 0, because task is not assigned to the delegator anymore.
assertEquals(0, tasks.size());
// Login as delegate.
taskFilter.setCandidateOrAssigned(delegateOne.getUsername());
tasks = workflowTaskInstanceService.find(taskFilter, null).getContent();
assertEquals(1, tasks.size());
taskFilter.setCandidateOrAssigned(delegateTwo.getUsername());
tasks = workflowTaskInstanceService.find(taskFilter, null).getContent();
assertEquals(1, tasks.size());
identityLinks = tasks.get(0).getIdentityLinks();
assertEquals(3, identityLinks.size());
// Task have to be assigned to the delegate One now.
assertEquals(1, identityLinks.stream().filter(identityLink -> IdentityLinkType.CANDIDATE.equals(identityLink.getType())).filter(identityLink -> delegateOne.getId().equals(UUID.fromString(identityLink.getUserId()))).count());
// Task have to be assigned also to the delegate Two.
assertEquals(1, identityLinks.stream().filter(identityLink -> IdentityLinkType.CANDIDATE.equals(identityLink.getType())).filter(identityLink -> delegateOne.getId().equals(UUID.fromString(identityLink.getUserId()))).count());
}
Aggregations