Java Code Examples for org.flowable.engine.impl.persistence.entity.ExecutionEntity#isProcessInstanceType()

The following examples show how to use org.flowable.engine.impl.persistence.entity.ExecutionEntity#isProcessInstanceType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SetProcessInstanceBusinessKeyCmd.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
@Override
public Void execute(CommandContext commandContext) {
    ExecutionEntityManager executionManager = CommandContextUtil.getExecutionEntityManager(commandContext);
    ExecutionEntity processInstance = executionManager.findById(processInstanceId);
    if (processInstance == null) {
        throw new FlowableObjectNotFoundException("No process instance found for id = '" + processInstanceId + "'.", ProcessInstance.class);
    } else if (!processInstance.isProcessInstanceType()) {
        throw new FlowableIllegalArgumentException("A process instance id is required, but the provided id " + "'" + processInstanceId + "' " + "points to a child execution of process instance " + "'"
                + processInstance.getProcessInstanceId() + "'. " + "Please invoke the " + getClass().getSimpleName() + " with a root execution id.");
    }

    if (Flowable5Util.isFlowable5ProcessDefinitionId(commandContext, processInstance.getProcessDefinitionId())) {
        CommandContextUtil.getProcessEngineConfiguration(commandContext).getFlowable5CompatibilityHandler().updateBusinessKey(processInstanceId, businessKey);
        return null;
    }

    executionManager.updateProcessInstanceBusinessKey(processInstance, businessKey);

    return null;
}
 
Example 2
Source File: AbstractDynamicStateManager.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
protected ExecutionEntity resolveParentExecutionToDelete(ExecutionEntity execution, Collection<FlowElementMoveEntry> moveToFlowElements) {
    ExecutionEntity parentExecution = execution.getParent();

    if (parentExecution.isProcessInstanceType()) {
        return null;
    }

    if (!isSubProcessContainerOfAnyFlowElement(parentExecution.getActivityId(), moveToFlowElements)) {
        ExecutionEntity subProcessParentExecution = resolveParentExecutionToDelete(parentExecution, moveToFlowElements);
        if (subProcessParentExecution != null) {
            return subProcessParentExecution;
        } else {
            return parentExecution;
        }
    }

    return null;
}
 
Example 3
Source File: TerminateEndEventActivityBehavior.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
protected void deleteExecutionEntities(ExecutionEntityManager executionEntityManager, ExecutionEntity rootExecutionEntity,
                ExecutionEntity executionAtTerminateEndEvent, String deleteReason) {

    FlowElement terminateEndEvent = executionAtTerminateEndEvent.getCurrentFlowElement();
    
    List<ExecutionEntity> childExecutions = executionEntityManager.collectChildren(rootExecutionEntity);
    for (ExecutionEntity childExecution : childExecutions) {
        if (childExecution.isProcessInstanceType()) {
            sendProcessInstanceCompletedEvent(childExecution, terminateEndEvent);
        }
    }
    
    CommandContextUtil.getExecutionEntityManager().deleteChildExecutions(rootExecutionEntity, null, null, deleteReason, true, terminateEndEvent);
    sendProcessInstanceCompletedEvent(rootExecutionEntity, terminateEndEvent);
    executionEntityManager.deleteExecutionAndRelatedData(rootExecutionEntity, deleteReason, false);
}
 
Example 4
Source File: EvaluateConditionalEventsCmd.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
@Override
protected Object execute(CommandContext commandContext, ExecutionEntity execution) {
    if (!execution.isProcessInstanceType()) {
        throw new FlowableException("Execution is not of type process instance");
    }
    
    if (processVariables != null) {
        execution.setVariables(processVariables);
    }

    if (transientVariables != null) {
        execution.setTransientVariables(transientVariables);
    }

    CommandContextUtil.getAgenda(commandContext).planEvaluateConditionalEventsOperation(execution);

    return null;
}
 
Example 5
Source File: TakeOutgoingSequenceFlowsOperation.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
/**
 * @param executionEntityToIgnore
 *            The execution entity which we can ignore to be ended, as it's the execution currently being handled in this operation.
 */
protected ExecutionEntity findNextParentScopeExecutionWithAllEndedChildExecutions(ExecutionEntity executionEntity, ExecutionEntity executionEntityToIgnore) {
    if (executionEntity.getParentId() != null) {
        ExecutionEntity scopeExecutionEntity = executionEntity.getParent();

        // Find next scope
        while (!scopeExecutionEntity.isScope() || !scopeExecutionEntity.isProcessInstanceType()) {
            scopeExecutionEntity = scopeExecutionEntity.getParent();
        }

        // Return when all child executions for it are ended
        if (allChildExecutionsEnded(scopeExecutionEntity, executionEntityToIgnore)) {
            return scopeExecutionEntity;
        }

    }
    return null;
}
 
Example 6
Source File: FlowableProcessStartedEventImpl.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
public FlowableProcessStartedEventImpl(final Object entity, final Map variables, final boolean localScope) {
    super(entity, variables, localScope, FlowableEngineEventType.PROCESS_STARTED);
    if (entity instanceof ExecutionEntity) {
        ExecutionEntity executionEntity = (ExecutionEntity) entity;
        if (!executionEntity.isProcessInstanceType()) {
            executionEntity = executionEntity.getParent();
        }

        final ExecutionEntity superExecution = executionEntity.getSuperExecution();
        if (superExecution != null) {
            this.nestedProcessDefinitionId = superExecution.getProcessDefinitionId();
            this.nestedProcessInstanceId = superExecution.getProcessInstanceId();
        } else {
            this.nestedProcessDefinitionId = null;
            this.nestedProcessInstanceId = null;
        }

    } else {
        this.nestedProcessDefinitionId = null;
        this.nestedProcessInstanceId = null;
    }
}
 
Example 7
Source File: EndExecutionOperation.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
protected ExecutionEntity handleRegularExecutionEnd(ExecutionEntityManager executionEntityManager, ExecutionEntity parentExecution) {
    ExecutionEntity executionToContinue = null;

    if (!parentExecution.isProcessInstanceType()
            && !(parentExecution.getCurrentFlowElement() instanceof SubProcess)) {
        parentExecution.setCurrentFlowElement(execution.getCurrentFlowElement());
    }

    if (execution.getCurrentFlowElement() instanceof SubProcess) {
        SubProcess currentSubProcess = (SubProcess) execution.getCurrentFlowElement();
        if (currentSubProcess.getOutgoingFlows().size() > 0) {
            // create a new execution to take the outgoing sequence flows
            executionToContinue = executionEntityManager.createChildExecution(parentExecution);
            executionToContinue.setCurrentFlowElement(execution.getCurrentFlowElement());

        } else {
            if (!parentExecution.getId().equals(parentExecution.getProcessInstanceId())) {
                // create a new execution to take the outgoing sequence flows
                executionToContinue = executionEntityManager.createChildExecution(parentExecution.getParent());
                executionToContinue.setCurrentFlowElement(parentExecution.getCurrentFlowElement());

                executionEntityManager.deleteChildExecutions(parentExecution, null, false);
                executionEntityManager.deleteExecutionAndRelatedData(parentExecution, null, false);

            } else {
                executionToContinue = parentExecution;
            }
        }

    } else {
        executionToContinue = parentExecution;
    }
    return executionToContinue;
}
 
Example 8
Source File: TakeOutgoingSequenceFlowsOperation.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
protected void cleanupCompensation() {

        // The compensation is at the end here. Simply stop the execution.
        CommandContextUtil.getExecutionEntityManager(commandContext).deleteExecutionAndRelatedData(execution, null, false);

        ExecutionEntity parentExecutionEntity = execution.getParent();
        if (parentExecutionEntity.isScope() && !parentExecutionEntity.isProcessInstanceType()) {

            if (allChildExecutionsEnded(parentExecutionEntity, null)) {

                // Go up the hierarchy to check if the next scope is ended too.
                // This could happen if only the compensation activity is still active, but the
                // main process is already finished.

                ExecutionEntity executionEntityToEnd = parentExecutionEntity;
                ExecutionEntity scopeExecutionEntity = findNextParentScopeExecutionWithAllEndedChildExecutions(parentExecutionEntity, parentExecutionEntity);
                while (scopeExecutionEntity != null) {
                    executionEntityToEnd = scopeExecutionEntity;
                    scopeExecutionEntity = findNextParentScopeExecutionWithAllEndedChildExecutions(scopeExecutionEntity, parentExecutionEntity);
                }

                if (executionEntityToEnd.isProcessInstanceType()) {
                    agenda.planEndExecutionOperation(executionEntityToEnd);
                } else {
                    agenda.planDestroyScopeOperation(executionEntityToEnd);
                }

            }
        }
    }
 
Example 9
Source File: AbortProcessListener.java    From multiapps-controller with Apache License 2.0 5 votes vote down vote up
private static boolean hasCorrectEntityType(FlowableEngineEntityEvent event) {
    if (!(event.getEntity() instanceof ExecutionEntity)) {
        return false;
    }
    ExecutionEntity executionEntity = (ExecutionEntity) event.getEntity();
    return executionEntity.isProcessInstanceType() && Constants.PROCESS_ABORTED.equals(executionEntity.getDeleteReason());
}
 
Example 10
Source File: SetProcessDefinitionVersionCmd.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Override
public Void execute(CommandContext commandContext) {
    // check that the new process definition is just another version of the same
    // process definition that the process instance is using
    ExecutionEntityManager executionManager = CommandContextUtil.getExecutionEntityManager(commandContext);
    ExecutionEntity processInstance = executionManager.findById(processInstanceId);
    if (processInstance == null) {
        throw new FlowableObjectNotFoundException("No process instance found for id = '" + processInstanceId + "'.", ProcessInstance.class);
    } else if (!processInstance.isProcessInstanceType()) {
        throw new FlowableIllegalArgumentException("A process instance id is required, but the provided id " + "'" + processInstanceId + "' " + "points to a child execution of process instance " + "'"
                + processInstance.getProcessInstanceId() + "'. " + "Please invoke the " + getClass().getSimpleName() + " with a root execution id.");
    }

    DeploymentManager deploymentCache = CommandContextUtil.getProcessEngineConfiguration(commandContext).getDeploymentManager();
    ProcessDefinition currentProcessDefinition = deploymentCache.findDeployedProcessDefinitionById(processInstance.getProcessDefinitionId());

    ProcessDefinition newProcessDefinition = deploymentCache
            .findDeployedProcessDefinitionByKeyAndVersionAndTenantId(currentProcessDefinition.getKey(), processDefinitionVersion, currentProcessDefinition.getTenantId());

    if (Flowable5Util.isFlowable5ProcessDefinition(currentProcessDefinition, commandContext) && !Flowable5Util
        .isFlowable5ProcessDefinition(newProcessDefinition, commandContext)) {
        throw new FlowableIllegalArgumentException("The current process definition (id = '" + currentProcessDefinition.getId() + "') is a v5 definition."
            + " However the new process definition (id = '" + newProcessDefinition.getId() + "') is not a v5 definition.");
    }

    validateAndSwitchVersionOfExecution(commandContext, processInstance, newProcessDefinition);

    // switch the historic process instance to the new process definition version
    CommandContextUtil.getHistoryManager(commandContext).recordProcessDefinitionChange(processInstanceId, newProcessDefinition.getId());

    // switch all sub-executions of the process instance to the new process definition version
    Collection<ExecutionEntity> childExecutions = executionManager.findChildExecutionsByProcessInstanceId(processInstanceId);
    for (ExecutionEntity executionEntity : childExecutions) {
        validateAndSwitchVersionOfExecution(commandContext, executionEntity, newProcessDefinition);
    }

    return null;
}
 
Example 11
Source File: SetProcessInstanceNameCmd.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Override
public Void execute(CommandContext commandContext) {
    if (processInstanceId == null) {
        throw new FlowableIllegalArgumentException("processInstanceId is null");
    }

    ExecutionEntity execution = CommandContextUtil.getExecutionEntityManager(commandContext).findById(processInstanceId);
    
    if (execution == null) {
        
        if (CommandContextUtil.getProcessEngineConfiguration(commandContext).isFlowable5CompatibilityEnabled()) {
            Flowable5CompatibilityHandler compatibilityHandler = Flowable5Util.getFlowable5CompatibilityHandler();
            if (compatibilityHandler != null) {
                ProcessInstance processInstance = compatibilityHandler.getProcessInstance(processInstanceId);
                if (processInstance != null) {
                    compatibilityHandler.setProcessInstanceName(processInstance.getId(), name);
                    return null;
                }
            }
        }
        
        throw new FlowableObjectNotFoundException("process instance " + processInstanceId + " doesn't exist", ProcessInstance.class);
    }
    
    if (!execution.isProcessInstanceType()) {
        throw new FlowableObjectNotFoundException("process instance " + processInstanceId + " doesn't exist, the given ID references an execution, though", ProcessInstance.class);
    }

    if (execution.isSuspended()) {
        throw new FlowableException("process instance " + processInstanceId + " is suspended, cannot set name");
    }

    // Actually set the name
    execution.setName(name);

    // Record the change in history
    CommandContextUtil.getHistoryManager(commandContext).recordProcessInstanceNameChange(execution, name);

    return null;
}
 
Example 12
Source File: FlowableProcessTerminatedEventImpl.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
public FlowableProcessTerminatedEventImpl(ExecutionEntity execution, Object cause) {
    super(execution, FlowableEngineEventType.PROCESS_COMPLETED_WITH_TERMINATE_END_EVENT);
    if (!execution.isProcessInstanceType()) {
        throw new FlowableException("Execution '"+ execution +"' is not a processInstance");
    }
    
    this.subScopeId = execution.getId();
    this.scopeId = execution.getProcessInstanceId();
    this.scopeDefinitionId = execution.getProcessDefinitionId();
    this.scopeType = ScopeTypes.BPMN;
    this.cause = cause;
}
 
Example 13
Source File: TerminateEndEventActivityBehavior.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
protected void sendProcessInstanceCompletedEvent(ExecutionEntity execution, FlowElement terminateEndEvent) {
    Process process = ProcessDefinitionUtil.getProcess(execution.getProcessDefinitionId());
    CommandContextUtil.getProcessEngineConfiguration().getListenerNotificationHelper()
        .executeExecutionListeners(process, execution, ExecutionListener.EVENTNAME_END);

    FlowableEventDispatcher eventDispatcher = CommandContextUtil.getProcessEngineConfiguration().getEventDispatcher();
    if (eventDispatcher != null && eventDispatcher.isEnabled()) {
        if ((execution.isProcessInstanceType() && execution.getSuperExecutionId() == null) ||
                (execution.getParentId() == null && execution.getSuperExecutionId() != null)) {

            // This event should only be fired if terminate end event is part of the process definition for the process instance execution,
            // otherwise a regular cancel event of the process instance will be fired (see above).
            boolean fireEvent = true;
            if (!terminateAll) {
                Process processForExecution = ProcessDefinitionUtil.getProcess(execution.getProcessDefinitionId());
                Process processForTerminateEndEvent = getProcessForTerminateEndEvent(terminateEndEvent);
                fireEvent = processForExecution.getId().equals(processForTerminateEndEvent.getId());
            }
            
            if (fireEvent) {
                eventDispatcher
                    .dispatchEvent(FlowableEventBuilder.createTerminateEvent(execution, terminateEndEvent));
            }
            
        }
    }

}
 
Example 14
Source File: AbstractHistoryManager.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
protected HistoricActivityInstanceEntity findHistoricActivityInstance(ExecutionEntity execution, String activityId, boolean endTimeMustBeNull) {

        // No use looking for the HistoricActivityInstance when no activityId is provided.
        if (activityId == null) {
            return null;
        }

        String executionId = execution.getId();

        // Check the cache
        HistoricActivityInstanceEntity historicActivityInstanceEntityFromCache = getHistoricActivityInstanceFromCache(executionId, activityId, endTimeMustBeNull);
        if (historicActivityInstanceEntityFromCache != null) {
            return historicActivityInstanceEntityFromCache;
        }

        // If the execution was freshly created, there is no need to check the database,
        // there can never be an entry for a historic activity instance with this execution id.
        if (!execution.isInserted() && !execution.isProcessInstanceType()) {

            // Check the database
            List<HistoricActivityInstanceEntity> historicActivityInstances = getHistoricActivityInstanceEntityManager()
                            .findUnfinishedHistoricActivityInstancesByExecutionAndActivityId(executionId, activityId);

            if (historicActivityInstances.size() > 0 && (!endTimeMustBeNull || historicActivityInstances.get(0).getEndTime() == null)) {
                return historicActivityInstances.get(0);
            }

        }

        return null;
    }
 
Example 15
Source File: AbstractDynamicStateManager.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
protected List<ExecutionEntity> resolveActiveExecutions(String processInstanceId, String activityId, CommandContext commandContext) {
    ExecutionEntityManager executionEntityManager = CommandContextUtil.getExecutionEntityManager(commandContext);
    ExecutionEntity processExecution = executionEntityManager.findById(processInstanceId);

    if (processExecution == null) {
        throw new FlowableException("Execution could not be found with id " + processInstanceId);
    }

    if (!processExecution.isProcessInstanceType()) {
        throw new FlowableException("Execution is not a process instance type execution for id " + processInstanceId);
    }

    if (Flowable5Util.isFlowable5ProcessDefinitionId(commandContext, processExecution.getProcessDefinitionId())) {
        throw new FlowableException("Flowable 5 process definitions are not supported");
    }

    List<ExecutionEntity> childExecutions = executionEntityManager.findChildExecutionsByProcessInstanceId(processExecution.getId());

    List<ExecutionEntity> executions = childExecutions.stream()
        .filter(e -> e.getCurrentActivityId() != null)
        .filter(e -> e.getCurrentActivityId().equals(activityId))
        .collect(Collectors.toList());

    if (executions.isEmpty()) {
        throw new FlowableException("Active execution could not be found with activity id " + activityId);
    }

    return executions;
}
 
Example 16
Source File: CountingEntityUtil.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
public static boolean isExecutionRelatedEntityCountEnabled(ExecutionEntity executionEntity) {
    if (executionEntity.isProcessInstanceType() || executionEntity instanceof CountingExecutionEntity) {
        return isExecutionRelatedEntityCountEnabled((CountingExecutionEntity) executionEntity);
    }
    return false;
}
 
Example 17
Source File: AbstractSetProcessInstanceStateCmd.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
@Override
public Void execute(CommandContext commandContext) {

    if (processInstanceId == null) {
        throw new FlowableIllegalArgumentException("ProcessInstanceId cannot be null.");
    }

    ExecutionEntityManager executionEntityManager = CommandContextUtil.getExecutionEntityManager(commandContext);
    ExecutionEntity executionEntity = executionEntityManager.findById(processInstanceId);

    if (executionEntity == null) {
        throw new FlowableObjectNotFoundException("Cannot find processInstance for id '" + processInstanceId + "'.", Execution.class);
    }
    if (!executionEntity.isProcessInstanceType()) {
        throw new FlowableException("Cannot set suspension state for execution '" + processInstanceId + "': not a process instance.");
    }

    if (Flowable5Util.isFlowable5ProcessDefinitionId(commandContext, executionEntity.getProcessDefinitionId())) {
        if (getNewState() == SuspensionState.ACTIVE) {
            CommandContextUtil.getProcessEngineConfiguration().getFlowable5CompatibilityHandler().activateProcessInstance(processInstanceId);
        } else {
            CommandContextUtil.getProcessEngineConfiguration().getFlowable5CompatibilityHandler().suspendProcessInstance(processInstanceId);
        }
        return null;
    }

    SuspensionStateUtil.setSuspensionState(executionEntity, getNewState());
    executionEntityManager.update(executionEntity, false);

    // All child executions are suspended
    Collection<ExecutionEntity> childExecutions = executionEntityManager.findChildExecutionsByProcessInstanceId(processInstanceId);
    for (ExecutionEntity childExecution : childExecutions) {
        if (!childExecution.getId().equals(processInstanceId)) {
            SuspensionStateUtil.setSuspensionState(childExecution, getNewState());
            executionEntityManager.update(childExecution, false);
        }
    }

    // All tasks are suspended
    List<TaskEntity> tasks = CommandContextUtil.getTaskService().findTasksByProcessInstanceId(processInstanceId);
    for (TaskEntity taskEntity : tasks) {
        SuspensionStateUtil.setSuspensionState(taskEntity, getNewState());
        CommandContextUtil.getTaskService().updateTask(taskEntity, false);
    }

    // All jobs are suspended
    JobService jobService = CommandContextUtil.getJobService(commandContext);
    if (getNewState() == SuspensionState.ACTIVE) {
        List<SuspendedJobEntity> suspendedJobs = jobService.findSuspendedJobsByProcessInstanceId(processInstanceId);
        for (SuspendedJobEntity suspendedJob : suspendedJobs) {
            jobService.activateSuspendedJob(suspendedJob);
        }

    } else {
        TimerJobService timerJobService = CommandContextUtil.getTimerJobService(commandContext);
        List<TimerJobEntity> timerJobs = timerJobService.findTimerJobsByProcessInstanceId(processInstanceId);
        for (TimerJobEntity timerJob : timerJobs) {
            jobService.moveJobToSuspendedJob(timerJob);
        }

        List<JobEntity> jobs = jobService.findJobsByProcessInstanceId(processInstanceId);
        for (JobEntity job : jobs) {
            jobService.moveJobToSuspendedJob(job);
        }

        List<ExternalWorkerJobEntity> externalWorkerJobs = CommandContextUtil.getJobServiceConfiguration(commandContext)
                .getExternalWorkerJobEntityManager()
                .findJobsByProcessInstanceId(processInstanceId);

        for (ExternalWorkerJobEntity externalWorkerJob : externalWorkerJobs) {
            jobService.moveJobToSuspendedJob(externalWorkerJob);
        }

    }

    return null;
}
 
Example 18
Source File: ErrorPropagation.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
protected static void executeEventHandler(Event event, ExecutionEntity parentExecution, ExecutionEntity currentExecution, String errorId) {
    ProcessEngineConfigurationImpl processEngineConfiguration = CommandContextUtil.getProcessEngineConfiguration();
    FlowableEventDispatcher eventDispatcher = null;
    if (processEngineConfiguration != null) {
        eventDispatcher = processEngineConfiguration.getEventDispatcher();
    }
    if (eventDispatcher != null && eventDispatcher.isEnabled()) {
        BpmnModel bpmnModel = ProcessDefinitionUtil.getBpmnModel(parentExecution.getProcessDefinitionId());
        if (bpmnModel != null) {

            String errorCode = bpmnModel.getErrors().get(errorId);
            if (errorCode == null) {
                errorCode = errorId;
            }

            processEngineConfiguration.getEventDispatcher().dispatchEvent(
                    FlowableEventBuilder.createErrorEvent(FlowableEngineEventType.ACTIVITY_ERROR_RECEIVED, event.getId(), errorId, errorCode, parentExecution.getId(),
                            parentExecution.getProcessInstanceId(), parentExecution.getProcessDefinitionId()));
        }
    }

    if (event instanceof StartEvent) {
        ExecutionEntityManager executionEntityManager = CommandContextUtil.getExecutionEntityManager();

        if (parentExecution.isProcessInstanceType()) {
            executionEntityManager.deleteChildExecutions(parentExecution, null, true);
        } else if (!currentExecution.getParentId().equals(parentExecution.getId())) {
            CommandContextUtil.getAgenda().planDestroyScopeOperation(currentExecution);
        } else {
            executionEntityManager.deleteExecutionAndRelatedData(currentExecution, null, false);
        }

        ExecutionEntity eventSubProcessExecution = executionEntityManager.createChildExecution(parentExecution);
        if (event.getSubProcess() != null) {
            eventSubProcessExecution.setCurrentFlowElement(event.getSubProcess());
            CommandContextUtil.getActivityInstanceEntityManager().recordActivityStart(eventSubProcessExecution);
            ExecutionEntity subProcessStartEventExecution = executionEntityManager.createChildExecution(eventSubProcessExecution);
            subProcessStartEventExecution.setCurrentFlowElement(event);
            CommandContextUtil.getAgenda().planContinueProcessOperation(subProcessStartEventExecution);
            
        } else {
            eventSubProcessExecution.setCurrentFlowElement(event);
            CommandContextUtil.getAgenda().planContinueProcessOperation(eventSubProcessExecution);
        }

    } else {
        ExecutionEntity boundaryExecution = null;
        List<? extends ExecutionEntity> childExecutions = parentExecution.getExecutions();
        for (ExecutionEntity childExecution : childExecutions) {
            if (childExecution != null
                    && childExecution.getActivityId() != null
                    && childExecution.getActivityId().equals(event.getId())) {
                boundaryExecution = childExecution;
            }
        }

        CommandContextUtil.getAgenda().planTriggerExecutionOperation(boundaryExecution);
    }
}
 
Example 19
Source File: BoundaryCompensateEventActivityBehavior.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
@Override
public void execute(DelegateExecution execution) {
    ExecutionEntity executionEntity = (ExecutionEntity) execution;
    BoundaryEvent boundaryEvent = (BoundaryEvent) execution.getCurrentFlowElement();

    Process process = ProcessDefinitionUtil.getProcess(execution.getProcessDefinitionId());
    if (process == null) {
        throw new FlowableException("Process model (id = " + execution.getId() + ") could not be found");
    }

    Activity sourceActivity = null;
    Activity compensationActivity = null;
    List<Association> associations = process.findAssociationsWithSourceRefRecursive(boundaryEvent.getId());
    for (Association association : associations) {
        sourceActivity = boundaryEvent.getAttachedToRef();
        FlowElement targetElement = process.getFlowElement(association.getTargetRef(), true);
        if (targetElement instanceof Activity) {
            Activity activity = (Activity) targetElement;
            if (activity.isForCompensation()) {
                compensationActivity = activity;
                break;
            }
        }
    }
    
    if (sourceActivity == null) {
        throw new FlowableException("Parent activity for boundary compensation event could not be found");
    }

    if (compensationActivity == null) {
        throw new FlowableException("Compensation activity could not be found (or it is missing 'isForCompensation=\"true\"'");
    }

    // find SubProcess or Process instance execution
    ExecutionEntity scopeExecution = null;
    ExecutionEntity parentExecution = executionEntity.getParent();
    while (scopeExecution == null && parentExecution != null) {
        if (parentExecution.getCurrentFlowElement() instanceof SubProcess) {
            scopeExecution = parentExecution;

        } else if (parentExecution.isProcessInstanceType()) {
            scopeExecution = parentExecution;
        } else {
            parentExecution = parentExecution.getParent();
        }
    }

    if (scopeExecution == null) {
        throw new FlowableException("Could not find a scope execution for compensation boundary event " + boundaryEvent.getId());
    }

    EventSubscriptionEntity eventSubscription = (EventSubscriptionEntity) CommandContextUtil.getEventSubscriptionService().createEventSubscriptionBuilder()
                    .eventType(CompensateEventSubscriptionEntity.EVENT_TYPE)
                    .executionId(scopeExecution.getId())
                    .processInstanceId(scopeExecution.getProcessInstanceId())
                    .activityId(sourceActivity.getId())
                    .tenantId(scopeExecution.getTenantId())
                    .create();
    
    CountingEntityUtil.handleInsertEventSubscriptionEntityCount(eventSubscription);
}