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

The following examples show how to use org.flowable.engine.impl.persistence.entity.ExecutionEntity#setCurrentFlowElement() . 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: MultiInstanceActivityBehavior.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
protected void cleanupMiRoot(DelegateExecution execution) {
    // Delete multi instance root and all child executions.
    // Create a fresh execution to continue
    
    ExecutionEntity multiInstanceRootExecution = (ExecutionEntity) getMultiInstanceRootExecution(execution);
    FlowElement flowElement = multiInstanceRootExecution.getCurrentFlowElement();
    ExecutionEntity parentExecution = multiInstanceRootExecution.getParent();
    
    ExecutionEntityManager executionEntityManager = CommandContextUtil.getExecutionEntityManager();
    Collection<String> executionIdsNotToSendCancelledEventsFor = execution.isMultiInstanceRoot() ? null : Collections.singletonList(execution.getId());
    executionEntityManager.deleteChildExecutions(multiInstanceRootExecution, null, executionIdsNotToSendCancelledEventsFor, DELETE_REASON_END, true, flowElement);
    executionEntityManager.deleteRelatedDataForExecution(multiInstanceRootExecution, DELETE_REASON_END);
    executionEntityManager.delete(multiInstanceRootExecution);

    ExecutionEntity newExecution = executionEntityManager.createChildExecution(parentExecution);
    newExecution.setCurrentFlowElement(flowElement);
    super.leave(newExecution);
}
 
Example 2
Source File: MultiInstanceActivityBehavior.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
protected void executeCompensationBoundaryEvents(FlowElement flowElement, DelegateExecution execution) {

        // Execute compensation boundary events
        Collection<BoundaryEvent> boundaryEvents = findBoundaryEventsForFlowNode(execution.getProcessDefinitionId(), flowElement);
        if (CollectionUtil.isNotEmpty(boundaryEvents)) {

            // The parent execution becomes a scope, and a child execution is created for each of the boundary events
            for (BoundaryEvent boundaryEvent : boundaryEvents) {

                if (CollectionUtil.isEmpty(boundaryEvent.getEventDefinitions())) {
                    continue;
                }

                if (boundaryEvent.getEventDefinitions().get(0) instanceof CompensateEventDefinition) {
                    ExecutionEntity childExecutionEntity = CommandContextUtil.getExecutionEntityManager()
                            .createChildExecution((ExecutionEntity) execution);
                    childExecutionEntity.setParentId(execution.getId());
                    childExecutionEntity.setCurrentFlowElement(boundaryEvent);
                    childExecutionEntity.setScope(false);

                    ActivityBehavior boundaryEventBehavior = ((ActivityBehavior) boundaryEvent.getBehavior());
                    boundaryEventBehavior.execute(childExecutionEntity);
                }
            }
        }
    }
 
Example 3
Source File: TerminateEndEventActivityBehavior.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
protected void terminateMultiInstanceRoot(ExecutionEntity execution, CommandContext commandContext,
        ExecutionEntityManager executionEntityManager) {

    // When terminateMultiInstance is 'true', we look for the multi instance root and delete it from there.
    ExecutionEntity miRootExecutionEntity = executionEntityManager.findFirstMultiInstanceRoot( execution);
    if (miRootExecutionEntity != null) {

        // Create sibling execution to continue process instance execution before deletion
        ExecutionEntity siblingExecution = executionEntityManager.createChildExecution(miRootExecutionEntity.getParent());
        siblingExecution.setCurrentFlowElement(miRootExecutionEntity.getCurrentFlowElement());

        deleteExecutionEntities(executionEntityManager, miRootExecutionEntity, execution, createDeleteReason(miRootExecutionEntity.getActivityId()));

        CommandContextUtil.getAgenda(commandContext).planTakeOutgoingSequenceFlowsOperation(siblingExecution, true);
    } else {
        defaultTerminateEndEventBehaviour(execution, commandContext, executionEntityManager);
    }
}
 
Example 4
Source File: SequentialMultiInstanceBehavior.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
public void continueSequentialMultiInstance(DelegateExecution execution, int loopCounter, ExecutionEntity multiInstanceRootExecution) {
    try {
        
        if (execution.getCurrentFlowElement() instanceof SubProcess) {
            ExecutionEntityManager executionEntityManager = CommandContextUtil.getExecutionEntityManager();
            ExecutionEntity executionToContinue = executionEntityManager.createChildExecution(multiInstanceRootExecution);
            executionToContinue.setCurrentFlowElement(execution.getCurrentFlowElement());
            executionToContinue.setScope(true);
            executeOriginalBehavior(executionToContinue, multiInstanceRootExecution, loopCounter);
        } else {
            CommandContextUtil.getActivityInstanceEntityManager().recordActivityEnd((ExecutionEntity) execution, null);
            executeOriginalBehavior(execution, multiInstanceRootExecution, loopCounter);
        }

    } catch (BpmnError error) {
        // re-throw business fault so that it can be caught by an Error
        // Intermediate Event or Error Event Sub-Process in the process
        throw error;
    } catch (Exception e) {
        throw new FlowableException("Could not execute inner activity behavior of multi instance behavior", e);
    }
}
 
Example 5
Source File: EndExecutionOperation.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
protected void handleMultiInstanceSubProcess(ExecutionEntityManager executionEntityManager, ExecutionEntity parentExecution) {
    List<ExecutionEntity> activeChildExecutions = getActiveChildExecutionsForExecution(executionEntityManager, parentExecution.getId());
    boolean containsOtherChildExecutions = false;
    for (ExecutionEntity activeExecution : activeChildExecutions) {
        if (!activeExecution.getId().equals(execution.getId())) {
            containsOtherChildExecutions = true;
        }
    }

    if (!containsOtherChildExecutions) {

        // Destroy the current scope (subprocess) and leave via the subprocess

        ScopeUtil.createCopyOfSubProcessExecutionForCompensation(parentExecution);
        agenda.planDestroyScopeOperation(parentExecution);

        SubProcess subProcess = execution.getCurrentFlowElement().getSubProcess();
        MultiInstanceActivityBehavior multiInstanceBehavior = (MultiInstanceActivityBehavior) subProcess.getBehavior();
        parentExecution.setCurrentFlowElement(subProcess);
        multiInstanceBehavior.leave(parentExecution);
    }
}
 
Example 6
Source File: AbstractBpmnActivityBehavior.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
protected void executeCompensateBoundaryEvents(Collection<BoundaryEvent> boundaryEvents, DelegateExecution execution) {

        // The parent execution becomes a scope, and a child execution is created for each of the boundary events
        for (BoundaryEvent boundaryEvent : boundaryEvents) {

            if (CollectionUtil.isEmpty(boundaryEvent.getEventDefinitions())) {
                continue;
            }

            if (!(boundaryEvent.getEventDefinitions().get(0) instanceof CompensateEventDefinition)) {
                continue;
            }

            ExecutionEntity childExecutionEntity = CommandContextUtil.getExecutionEntityManager().createChildExecution((ExecutionEntity) execution);
            childExecutionEntity.setParentId(execution.getId());
            childExecutionEntity.setCurrentFlowElement(boundaryEvent);
            childExecutionEntity.setScope(false);

            ActivityBehavior boundaryEventBehavior = ((ActivityBehavior) boundaryEvent.getBehavior());
            boundaryEventBehavior.execute(childExecutionEntity);
        }

    }
 
Example 7
Source File: CompleteAdhocSubProcessCmd.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
@Override
public Void execute(CommandContext commandContext) {
    ExecutionEntityManager executionEntityManager = CommandContextUtil.getExecutionEntityManager(commandContext);
    ExecutionEntity execution = executionEntityManager.findById(executionId);
    if (execution == null) {
        throw new FlowableObjectNotFoundException("No execution found for id '" + executionId + "'", ExecutionEntity.class);
    }

    if (!(execution.getCurrentFlowElement() instanceof AdhocSubProcess)) {
        throw new FlowableException("The current flow element of the requested execution is not an ad-hoc sub process");
    }

    List<? extends ExecutionEntity> childExecutions = execution.getExecutions();
    if (childExecutions.size() > 0) {
        throw new FlowableException("Ad-hoc sub process has running child executions that need to be completed first");
    }

    ExecutionEntity outgoingFlowExecution = executionEntityManager.createChildExecution(execution.getParent());
    outgoingFlowExecution.setCurrentFlowElement(execution.getCurrentFlowElement());

    executionEntityManager.deleteExecutionAndRelatedData(execution, null, false);

    CommandContextUtil.getAgenda().planTakeOutgoingSequenceFlowsOperation(outgoingFlowExecution, true);

    return null;
}
 
Example 8
Source File: InjectParallelUserTaskCmd.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
@Override
protected void updateExecutions(CommandContext commandContext, ProcessDefinitionEntity processDefinitionEntity, 
        ExecutionEntity processInstance, List<ExecutionEntity> childExecutions) {
    
    TaskEntity taskEntity = CommandContextUtil.getTaskService().getTask(taskId);
    
    ExecutionEntityManager executionEntityManager = CommandContextUtil.getExecutionEntityManager(commandContext);
    ExecutionEntity executionAtTask = executionEntityManager.findById(taskEntity.getExecutionId());

    BpmnModel bpmnModel = ProcessDefinitionUtil.getBpmnModel(processDefinitionEntity.getId());
    FlowElement taskElement = bpmnModel.getFlowElement(executionAtTask.getCurrentActivityId());
    FlowElement subProcessElement = bpmnModel.getFlowElement(((SubProcess) taskElement.getParentContainer()).getId());
    ExecutionEntity subProcessExecution = executionEntityManager.createChildExecution(executionAtTask.getParent());
    subProcessExecution.setScope(true);
    subProcessExecution.setCurrentFlowElement(subProcessElement);
    CommandContextUtil.getActivityInstanceEntityManager(commandContext).recordActivityStart(subProcessExecution);
    
    executionAtTask.setParent(subProcessExecution);
    
    ExecutionEntity taskExecution = executionEntityManager.createChildExecution(subProcessExecution);

    FlowElement userTaskElement = bpmnModel.getFlowElement(dynamicUserTaskBuilder.getDynamicTaskId());
    taskExecution.setCurrentFlowElement(userTaskElement);
    
    Context.getAgenda().planContinueProcessOperation(taskExecution);
}
 
Example 9
Source File: ContinueProcessOperation.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
protected void createChildExecutionForSubProcess(SubProcess subProcess) {
    ExecutionEntity parentScopeExecution = findFirstParentScopeExecution(execution);

    // Create the sub process execution that can be used to set variables
    // We create a new execution and delete the incoming one to have a proper scope that
    // does not conflict anything with any existing scopes

    ExecutionEntity subProcessExecution = CommandContextUtil.getExecutionEntityManager(commandContext).createChildExecution(parentScopeExecution);
    subProcessExecution.setCurrentFlowElement(subProcess);
    subProcessExecution.setScope(true);

    CommandContextUtil.getExecutionEntityManager(commandContext).deleteRelatedDataForExecution(execution, null);
    CommandContextUtil.getExecutionEntityManager(commandContext).delete(execution);
    execution = subProcessExecution;
}
 
Example 10
Source File: EscalationPropagation.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
protected static void executeEventHandler(Event event, ExecutionEntity parentExecution, ExecutionEntity currentExecution, String escalationCode, String escalationName) {
    ProcessEngineConfigurationImpl processEngineConfiguration = CommandContextUtil.getProcessEngineConfiguration();
    FlowableEventDispatcher eventDispatcher = null;
    if (processEngineConfiguration != null) {
        eventDispatcher = processEngineConfiguration.getEventDispatcher();
    }
    
    if (eventDispatcher != null && eventDispatcher.isEnabled()) {
        processEngineConfiguration.getEventDispatcher().dispatchEvent(
                FlowableEventBuilder.createEscalationEvent(FlowableEngineEventType.ACTIVITY_ESCALATION_RECEIVED, event.getId(), escalationCode, 
                                escalationName, parentExecution.getId(), parentExecution.getProcessInstanceId(), parentExecution.getProcessDefinitionId()));
    }

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

        ExecutionEntity eventSubProcessExecution = executionEntityManager.createChildExecution(parentExecution);
        eventSubProcessExecution.setCurrentFlowElement(event.getSubProcess() != null ? event.getSubProcess() : 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 11
Source File: ExecuteActivityForAdhocSubProcessCmd.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Override
public Execution execute(CommandContext commandContext) {
    ExecutionEntity execution = CommandContextUtil.getExecutionEntityManager(commandContext).findById(executionId);
    if (execution == null) {
        throw new FlowableObjectNotFoundException("No execution found for id '" + executionId + "'", ExecutionEntity.class);
    }

    if (!(execution.getCurrentFlowElement() instanceof AdhocSubProcess)) {
        throw new FlowableException("The current flow element of the requested execution is not an ad-hoc sub process");
    }

    FlowNode foundNode = null;
    AdhocSubProcess adhocSubProcess = (AdhocSubProcess) execution.getCurrentFlowElement();

    // if sequential ordering, only one child execution can be active
    if (adhocSubProcess.hasSequentialOrdering()) {
        if (execution.getExecutions().size() > 0) {
            throw new FlowableException("Sequential ad-hoc sub process already has an active execution");
        }
    }

    for (FlowElement flowElement : adhocSubProcess.getFlowElements()) {
        if (activityId.equals(flowElement.getId()) && flowElement instanceof FlowNode) {
            FlowNode flowNode = (FlowNode) flowElement;
            if (flowNode.getIncomingFlows().size() == 0) {
                foundNode = flowNode;
            }
        }
    }

    if (foundNode == null) {
        throw new FlowableException("The requested activity with id " + activityId + " can not be enabled");
    }

    ExecutionEntity activityExecution = CommandContextUtil.getExecutionEntityManager().createChildExecution(execution);
    activityExecution.setCurrentFlowElement(foundNode);
    CommandContextUtil.getAgenda().planContinueProcessOperation(activityExecution);

    return activityExecution;
}
 
Example 12
Source File: InjectUserTaskInProcessInstanceCmd.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Override
protected void updateExecutions(CommandContext commandContext, ProcessDefinitionEntity processDefinitionEntity, 
        ExecutionEntity processInstance, List<ExecutionEntity> childExecutions) {

    ExecutionEntityManager executionEntityManager = CommandContextUtil.getExecutionEntityManager(commandContext);
    ExecutionEntity execution = executionEntityManager.createChildExecution(processInstance);
    
    BpmnModel bpmnModel = ProcessDefinitionUtil.getBpmnModel(processDefinitionEntity.getId());
    UserTask userTask = (UserTask) bpmnModel.getProcessById(processDefinitionEntity.getKey()).getFlowElement(dynamicUserTaskBuilder.getDynamicTaskId());
    execution.setCurrentFlowElement(userTask);

    Context.getAgenda().planContinueProcessOperation(execution);
}
 
Example 13
Source File: BoundaryEventActivityBehavior.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
protected void executeNonInterruptingBehavior(ExecutionEntity executionEntity, CommandContext commandContext) {
    // Non-interrupting: the current execution is given the first parent
    // scope (which isn't its direct parent)
    //
    // Why? Because this execution does NOT have anything to do with
    // the current parent execution (the one where the boundary event is on): when it is deleted or whatever,
    // this does not impact this new execution at all, it is completely independent in that regard.

    // Note: if the parent of the parent does not exists, this becomes a concurrent execution in the process instance!

    ExecutionEntityManager executionEntityManager = CommandContextUtil.getExecutionEntityManager(commandContext);

    ExecutionEntity parentExecutionEntity = executionEntityManager.findById(executionEntity.getParentId());

    ExecutionEntity scopeExecution = null;
    ExecutionEntity currentlyExaminedExecution = executionEntityManager.findById(parentExecutionEntity.getParentId());
    while (currentlyExaminedExecution != null && scopeExecution == null) {
        if (currentlyExaminedExecution.isScope()) {
            scopeExecution = currentlyExaminedExecution;
        } else {
            currentlyExaminedExecution = executionEntityManager.findById(currentlyExaminedExecution.getParentId());
        }
    }

    if (scopeExecution == null) {
        throw new FlowableException("Programmatic error: no parent scope execution found for boundary event");
    }

    CommandContextUtil.getActivityInstanceEntityManager(commandContext).recordActivityEnd(executionEntity, null);

    ExecutionEntity nonInterruptingExecution = executionEntityManager.createChildExecution(scopeExecution);
    nonInterruptingExecution.setActive(false);
    nonInterruptingExecution.setCurrentFlowElement(executionEntity.getCurrentFlowElement());

    CommandContextUtil.getAgenda(commandContext).planTakeOutgoingSequenceFlowsOperation(nonInterruptingExecution, true);
}
 
Example 14
Source File: ContinueProcessOperation.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
protected List<ExecutionEntity> createBoundaryEvents(List<BoundaryEvent> boundaryEvents, ExecutionEntity execution) {

        List<ExecutionEntity> boundaryEventExecutions = new ArrayList<>(boundaryEvents.size());

        // The parent execution becomes a scope, and a child execution is created for each of the boundary events
        for (BoundaryEvent boundaryEvent : boundaryEvents) {

            if (!(boundaryEvent.getBehavior() instanceof BoundaryEventRegistryEventActivityBehavior)) {
                if (CollectionUtil.isEmpty(boundaryEvent.getEventDefinitions())
                        || (boundaryEvent.getEventDefinitions().get(0) instanceof CompensateEventDefinition)) {
                    continue;
                }
            }

            // A Child execution of the current execution is created to represent the boundary event being active
            ExecutionEntity childExecutionEntity = CommandContextUtil.getExecutionEntityManager(commandContext).createChildExecution(execution);
            childExecutionEntity.setParentId(execution.getId());
            childExecutionEntity.setCurrentFlowElement(boundaryEvent);
            childExecutionEntity.setScope(false);
            boundaryEventExecutions.add(childExecutionEntity);
            
            CommandContextUtil.getActivityInstanceEntityManager(commandContext).recordActivityStart(childExecutionEntity);
            
            ProcessEngineConfigurationImpl processEngineConfiguration = CommandContextUtil.getProcessEngineConfiguration(commandContext);
            if (processEngineConfiguration.isLoggingSessionEnabled()) {
                BpmnLoggingSessionUtil.addLoggingData(BpmnLoggingSessionUtil.getBoundaryCreateEventType(boundaryEvent), 
                                "Creating boundary event (" + BpmnLoggingSessionUtil.getBoundaryEventType(boundaryEvent) + 
                                ") for execution id " + childExecutionEntity.getId(), childExecutionEntity);
            }
        }

        return boundaryEventExecutions;
    }
 
Example 15
Source File: EventSubProcessEventRegistryStartEventActivityBehavior.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
@Override
public void trigger(DelegateExecution execution, String triggerName, Object triggerData) {
    CommandContext commandContext = Context.getCommandContext();
    ExecutionEntityManager executionEntityManager = CommandContextUtil.getExecutionEntityManager(commandContext);
    ExecutionEntity executionEntity = (ExecutionEntity) execution;

    StartEvent startEvent = (StartEvent) execution.getCurrentFlowElement();
    if (startEvent.isInterrupting()) {
        List<ExecutionEntity> childExecutions = executionEntityManager.collectChildren(executionEntity.getParent());
        for (int i = childExecutions.size() - 1; i >= 0; i--) {
            ExecutionEntity childExecutionEntity = childExecutions.get(i);
            if (!childExecutionEntity.isEnded() && !childExecutionEntity.getId().equals(executionEntity.getId())) {
                executionEntityManager.deleteExecutionAndRelatedData(childExecutionEntity,
                        DeleteReason.EVENT_SUBPROCESS_INTERRUPTING + "(" + startEvent.getId() + ")", false);
            }
        }

        EventSubscriptionService eventSubscriptionService = CommandContextUtil.getEventSubscriptionService(commandContext);
        List<EventSubscriptionEntity> eventSubscriptions = executionEntity.getEventSubscriptions();

        for (EventSubscriptionEntity eventSubscription : eventSubscriptions) {
            if (Objects.equals(eventDefinitionKey, eventSubscription.getEventType())) {
                eventSubscriptionService.deleteEventSubscription(eventSubscription);
                CountingEntityUtil.handleDeleteEventSubscriptionEntityCount(eventSubscription);
            }
        }
    }

    ExecutionEntity newSubProcessExecution = executionEntityManager.createChildExecution(executionEntity.getParent());
    newSubProcessExecution.setCurrentFlowElement((SubProcess) executionEntity.getCurrentFlowElement().getParentContainer());
    newSubProcessExecution.setEventScope(false);
    newSubProcessExecution.setScope(true);

    CommandContextUtil.getActivityInstanceEntityManager(commandContext).recordActivityStart(newSubProcessExecution);

    ExecutionEntity outgoingFlowExecution = executionEntityManager.createChildExecution(newSubProcessExecution);
    outgoingFlowExecution.setCurrentFlowElement(startEvent);

    CommandContextUtil.getActivityInstanceEntityManager(commandContext).recordActivityStart(outgoingFlowExecution);

    leave(outgoingFlowExecution);
}
 
Example 16
Source File: EventSubProcessMessageStartEventActivityBehavior.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
@Override
public void trigger(DelegateExecution execution, String triggerName, Object triggerData) {
    CommandContext commandContext = Context.getCommandContext();
    ExecutionEntityManager executionEntityManager = CommandContextUtil.getExecutionEntityManager(commandContext);
    ExecutionEntity executionEntity = (ExecutionEntity) execution;

    StartEvent startEvent = (StartEvent) execution.getCurrentFlowElement();
    if (startEvent.isInterrupting()) {
        List<ExecutionEntity> childExecutions = executionEntityManager.collectChildren(executionEntity.getParent());
        for (int i = childExecutions.size() - 1; i >= 0; i--) {
            ExecutionEntity childExecutionEntity = childExecutions.get(i);
            if (!childExecutionEntity.isEnded() && !childExecutionEntity.getId().equals(executionEntity.getId())) {
                executionEntityManager.deleteExecutionAndRelatedData(childExecutionEntity,
                        DeleteReason.EVENT_SUBPROCESS_INTERRUPTING + "(" + startEvent.getId() + ")", false);
            }
        }

        EventSubscriptionService eventSubscriptionService = CommandContextUtil.getEventSubscriptionService(commandContext);
        List<EventSubscriptionEntity> eventSubscriptions = executionEntity.getEventSubscriptions();

        String messageName = EventDefinitionExpressionUtil.determineMessageName(commandContext, messageEventDefinition, execution);
        for (EventSubscriptionEntity eventSubscription : eventSubscriptions) {
            if (eventSubscription instanceof MessageEventSubscriptionEntity && eventSubscription.getEventName().equals(messageName)) {

                eventSubscriptionService.deleteEventSubscription(eventSubscription);
                CountingEntityUtil.handleDeleteEventSubscriptionEntityCount(eventSubscription);
            }
        }
    }

    ExecutionEntity newSubProcessExecution = executionEntityManager.createChildExecution(executionEntity.getParent());
    newSubProcessExecution.setCurrentFlowElement((SubProcess) executionEntity.getCurrentFlowElement().getParentContainer());
    newSubProcessExecution.setEventScope(false);
    newSubProcessExecution.setScope(true);

    CommandContextUtil.getActivityInstanceEntityManager(commandContext).recordActivityStart(newSubProcessExecution);

    ExecutionEntity outgoingFlowExecution = executionEntityManager.createChildExecution(newSubProcessExecution);
    outgoingFlowExecution.setCurrentFlowElement(startEvent);

    CommandContextUtil.getActivityInstanceEntityManager(commandContext).recordActivityStart(outgoingFlowExecution);

    leave(outgoingFlowExecution);
}
 
Example 17
Source File: ScopeUtil.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a new event scope execution and moves existing event subscriptions to this new execution
 */
public static void createCopyOfSubProcessExecutionForCompensation(ExecutionEntity subProcessExecution) {
    EventSubscriptionService eventSubscriptionService = CommandContextUtil.getEventSubscriptionService();
    List<EventSubscriptionEntity> eventSubscriptions = eventSubscriptionService.findEventSubscriptionsByExecutionAndType(subProcessExecution.getId(), "compensate");

    List<CompensateEventSubscriptionEntity> compensateEventSubscriptions = new ArrayList<>();
    for (EventSubscriptionEntity event : eventSubscriptions) {
        if (event instanceof CompensateEventSubscriptionEntity) {
            compensateEventSubscriptions.add((CompensateEventSubscriptionEntity) event);
        }
    }

    if (CollectionUtil.isNotEmpty(compensateEventSubscriptions)) {

        ExecutionEntity processInstanceExecutionEntity = subProcessExecution.getProcessInstance();

        ExecutionEntity eventScopeExecution = CommandContextUtil.getExecutionEntityManager().createChildExecution(processInstanceExecutionEntity);
        eventScopeExecution.setActive(false);
        eventScopeExecution.setEventScope(true);
        eventScopeExecution.setCurrentFlowElement(subProcessExecution.getCurrentFlowElement());

        // copy local variables to eventScopeExecution by value. This way,
        // the eventScopeExecution references a 'snapshot' of the local variables
        Map<String, Object> variables = subProcessExecution.getVariablesLocal();
        for (Entry<String, Object> variable : variables.entrySet()) {
            eventScopeExecution.setVariableLocal(variable.getKey(), variable.getValue(), subProcessExecution, true);
        }

        // set event subscriptions to the event scope execution:
        for (CompensateEventSubscriptionEntity eventSubscriptionEntity : compensateEventSubscriptions) {
            eventSubscriptionService.deleteEventSubscription(eventSubscriptionEntity);
            CountingEntityUtil.handleDeleteEventSubscriptionEntityCount(eventSubscriptionEntity);

            EventSubscriptionEntity newSubscription = (EventSubscriptionEntity) eventSubscriptionService.createEventSubscriptionBuilder()
                            .eventType(CompensateEventSubscriptionEntity.EVENT_TYPE)
                            .executionId(eventScopeExecution.getId())
                            .processInstanceId(eventScopeExecution.getProcessInstanceId())
                            .activityId(eventSubscriptionEntity.getActivityId())
                            .tenantId(eventScopeExecution.getTenantId())
                            .create();
            
            CountingEntityUtil.handleInsertEventSubscriptionEntityCount(newSubscription);
            
            newSubscription.setConfiguration(eventSubscriptionEntity.getConfiguration());
            newSubscription.setCreated(eventSubscriptionEntity.getCreated());
        }

        EventSubscriptionEntity eventSubscription = (EventSubscriptionEntity) eventSubscriptionService.createEventSubscriptionBuilder()
                        .eventType(CompensateEventSubscriptionEntity.EVENT_TYPE)
                        .executionId(processInstanceExecutionEntity.getId())
                        .processInstanceId(processInstanceExecutionEntity.getProcessInstanceId())
                        .activityId(eventScopeExecution.getCurrentFlowElement().getId())
                        .tenantId(processInstanceExecutionEntity.getTenantId())
                        .create();
        
        CountingEntityUtil.handleInsertEventSubscriptionEntityCount(eventSubscription);
        
        eventSubscription.setConfiguration(eventScopeExecution.getId());
    }
}
 
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: EventSubProcessSignalStartEventActivityBehavior.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
@Override
public void trigger(DelegateExecution execution, String triggerName, Object triggerData) {
    CommandContext commandContext = Context.getCommandContext();
    ExecutionEntityManager executionEntityManager = CommandContextUtil.getExecutionEntityManager(commandContext);
    ExecutionEntity executionEntity = (ExecutionEntity) execution;

    String eventName = EventDefinitionExpressionUtil.determineSignalName(commandContext, signalEventDefinition,
        ProcessDefinitionUtil.getBpmnModel(execution.getProcessDefinitionId()), execution);

    StartEvent startEvent = (StartEvent) execution.getCurrentFlowElement();
    if (startEvent.isInterrupting()) {
        List<ExecutionEntity> childExecutions = executionEntityManager.collectChildren(executionEntity.getParent());
        for (int i = childExecutions.size() - 1; i >= 0; i--) {
            ExecutionEntity childExecutionEntity = childExecutions.get(i);
            if (!childExecutionEntity.isEnded() && !childExecutionEntity.getId().equals(executionEntity.getId())) {
                executionEntityManager.deleteExecutionAndRelatedData(childExecutionEntity,
                        DeleteReason.EVENT_SUBPROCESS_INTERRUPTING + "(" + startEvent.getId() + ")", false);
            }
        }

        EventSubscriptionService eventSubscriptionService = CommandContextUtil.getEventSubscriptionService(commandContext);
        List<EventSubscriptionEntity> eventSubscriptions = executionEntity.getEventSubscriptions();

        for (EventSubscriptionEntity eventSubscription : eventSubscriptions) {
            if (eventSubscription instanceof SignalEventSubscriptionEntity && eventSubscription.getEventName().equals(eventName)) {

                eventSubscriptionService.deleteEventSubscription(eventSubscription);
                CountingEntityUtil.handleDeleteEventSubscriptionEntityCount(eventSubscription);
            }
        }
    }
    
    ExecutionEntity newSubProcessExecution = executionEntityManager.createChildExecution(executionEntity.getParent());
    newSubProcessExecution.setCurrentFlowElement((SubProcess) executionEntity.getCurrentFlowElement().getParentContainer());
    newSubProcessExecution.setEventScope(false);
    newSubProcessExecution.setScope(true);

    CommandContextUtil.getActivityInstanceEntityManager(commandContext).recordActivityStart(newSubProcessExecution);

    ExecutionEntity outgoingFlowExecution = executionEntityManager.createChildExecution(newSubProcessExecution);
    outgoingFlowExecution.setCurrentFlowElement(startEvent);

    CommandContextUtil.getActivityInstanceEntityManager(commandContext).recordActivityStart(outgoingFlowExecution);

    leave(outgoingFlowExecution);
}
 
Example 20
Source File: EvaluateConditionalEventsOperation.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
protected void evaluateEventSubProcesses(List<EventSubProcess> eventSubProcesses, ExecutionEntity parentExecution) {
    if (eventSubProcesses != null) {
        for (EventSubProcess eventSubProcess : eventSubProcesses) {
            List<StartEvent> startEvents = eventSubProcess.findAllSubFlowElementInFlowMapOfType(StartEvent.class);
            if (startEvents != null) {
                for (StartEvent startEvent : startEvents) {
                    
                    if (startEvent.getEventDefinitions() != null && !startEvent.getEventDefinitions().isEmpty() && 
                                    startEvent.getEventDefinitions().get(0) instanceof ConditionalEventDefinition) {
                        
                        CommandContext commandContext = CommandContextUtil.getCommandContext();
                        ConditionalEventDefinition conditionalEventDefinition = (ConditionalEventDefinition) startEvent.getEventDefinitions().get(0);
                        
                        boolean conditionIsTrue = false;
                        String conditionExpression = conditionalEventDefinition.getConditionExpression();
                        if (StringUtils.isNotEmpty(conditionExpression)) {
                            Expression expression = CommandContextUtil.getProcessEngineConfiguration(commandContext).getExpressionManager().createExpression(conditionExpression);
                            Object result = expression.getValue(parentExecution);
                            if (result != null && result instanceof Boolean && (Boolean) result) {
                                conditionIsTrue = true;
                            }
                        
                        } else {
                            conditionIsTrue = true;
                        }
                        
                        if (conditionIsTrue) {
                            ExecutionEntityManager executionEntityManager = CommandContextUtil.getExecutionEntityManager(commandContext);
                            if (startEvent.isInterrupting()) {
                                executionEntityManager.deleteChildExecutions(parentExecution, null, true);
                            }

                            ExecutionEntity eventSubProcessExecution = executionEntityManager.createChildExecution(parentExecution);
                            eventSubProcessExecution.setScope(true);
                            eventSubProcessExecution.setCurrentFlowElement(eventSubProcess);

                            CommandContextUtil.getActivityInstanceEntityManager(commandContext).recordActivityStart(eventSubProcessExecution);
                            
                            ExecutionEntity startEventSubProcessExecution = executionEntityManager.createChildExecution(eventSubProcessExecution);
                            startEventSubProcessExecution.setCurrentFlowElement(startEvent);
                            
                            CommandContextUtil.getAgenda(commandContext).planContinueProcessOperation(startEventSubProcessExecution);
                        }
                    }
                }
            }
        }
    }
}