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

The following examples show how to use org.flowable.engine.impl.persistence.entity.ExecutionEntity#setScope() . 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 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 2
Source File: AbstractDynamicStateManager.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
protected List<ExecutionEntity> createBoundaryEvents(List<BoundaryEvent> boundaryEvents, ExecutionEntity execution, CommandContext commandContext) {
    ExecutionEntityManager executionEntityManager = CommandContextUtil.getExecutionEntityManager(commandContext);

    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 (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 = executionEntityManager.createChildExecution(execution);
        childExecutionEntity.setParentId(execution.getId());
        childExecutionEntity.setCurrentFlowElement(boundaryEvent);
        childExecutionEntity.setScope(false);
        boundaryEventExecutions.add(childExecutionEntity);
    }

    return boundaryEventExecutions;
}
 
Example 3
Source File: AbstractDynamicStateManager.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
protected void executeBoundaryEvents(Collection<BoundaryEvent> boundaryEvents, ExecutionEntity 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())
                || (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().createChildExecution(execution);
            childExecutionEntity.setParentId(execution.getId());
            childExecutionEntity.setCurrentFlowElement(boundaryEvent);
            childExecutionEntity.setScope(false);

            ActivityBehavior boundaryEventBehavior = ((ActivityBehavior) boundaryEvent.getBehavior());
            LOGGER.debug("Executing boundary event activityBehavior {} with execution {}", boundaryEventBehavior.getClass(), childExecutionEntity.getId());
            boundaryEventBehavior.execute(childExecutionEntity);
        }
    }
 
Example 4
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 5
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 6
Source File: InjectParallelEmbeddedSubProcessCmd.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) {

    ExecutionEntityManager executionEntityManager = CommandContextUtil.getExecutionEntityManager(commandContext);

    TaskEntity taskEntity = CommandContextUtil.getTaskService().getTask(taskId);
    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 execution = executionEntityManager.createChildExecution(subProcessExecution);
    FlowElement newSubProcess = bpmnModel.getMainProcess().getFlowElement(dynamicEmbeddedSubProcessBuilder.getDynamicSubProcessId(), true);
    execution.setCurrentFlowElement(newSubProcess);

    CommandContextUtil.getAgenda().planContinueProcessOperation(execution);
}
 
Example 7
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 8
Source File: AddMultiInstanceExecutionCmd.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Override
public Execution execute(CommandContext commandContext) {
    ExecutionEntityManager executionEntityManager = CommandContextUtil.getExecutionEntityManager();
    
    ExecutionEntity miExecution = searchForMultiInstanceActivity(activityId, parentExecutionId, executionEntityManager);
    
    if (miExecution == null) {
        throw new FlowableException("No multi instance execution found for activity id " + activityId);
    }
    
    if (Flowable5Util.isFlowable5ProcessDefinitionId(commandContext, miExecution.getProcessDefinitionId())) {
        throw new FlowableException("Flowable 5 process definitions are not supported");
    }
    
    ExecutionEntity childExecution = executionEntityManager.createChildExecution(miExecution);
    childExecution.setCurrentFlowElement(miExecution.getCurrentFlowElement());
    
    BpmnModel bpmnModel = ProcessDefinitionUtil.getBpmnModel(miExecution.getProcessDefinitionId());
    Activity miActivityElement = (Activity) bpmnModel.getFlowElement(miExecution.getActivityId());
    MultiInstanceLoopCharacteristics multiInstanceLoopCharacteristics = miActivityElement.getLoopCharacteristics();
    
    Integer currentNumberOfInstances = (Integer) miExecution.getVariable(NUMBER_OF_INSTANCES);
    miExecution.setVariableLocal(NUMBER_OF_INSTANCES, currentNumberOfInstances + 1);
    
    if (executionVariables != null) {
        childExecution.setVariablesLocal(executionVariables);
    }
    
    if (!multiInstanceLoopCharacteristics.isSequential()) {
        miExecution.setActive(true);
        miExecution.setScope(false);
        
        childExecution.setCurrentFlowElement(miActivityElement);
        CommandContextUtil.getAgenda().planContinueMultiInstanceOperation(childExecution, miExecution, currentNumberOfInstances);
    }
    
    return childExecution;
}
 
Example 9
Source File: InjectEmbeddedSubProcessInProcessInstanceCmd.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);
    
    BpmnModel bpmnModel = ProcessDefinitionUtil.getBpmnModel(processDefinitionEntity.getId());
    SubProcess subProcess = (SubProcess) bpmnModel.getFlowElement(dynamicEmbeddedSubProcessBuilder.getDynamicSubProcessId());
    ExecutionEntity subProcessExecution = executionEntityManager.createChildExecution(processInstance);
    subProcessExecution.setScope(true);
    subProcessExecution.setCurrentFlowElement(subProcess);
    CommandContextUtil.getActivityInstanceEntityManager(commandContext).recordActivityStart(subProcessExecution);
    
    ExecutionEntity childExecution = executionEntityManager.createChildExecution(subProcessExecution);
    
    StartEvent initialEvent = null;
    for (FlowElement subElement : subProcess.getFlowElements()) {
        if (subElement instanceof StartEvent) {
            StartEvent startEvent = (StartEvent) subElement;
            if (startEvent.getEventDefinitions().size() == 0) {
                initialEvent = startEvent;
                break;
            }
        }
    }
    
    if (initialEvent == null) {
        throw new FlowableException("Could not find a none start event in dynamic sub process");
    }
    
    childExecution.setCurrentFlowElement(initialEvent);
    
    Context.getAgenda().planContinueProcessOperation(childExecution);
}
 
Example 10
Source File: SubProcessActivityBehavior.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(DelegateExecution execution) {
    SubProcess subProcess = getSubProcessFromExecution(execution);

    FlowElement startElement = getStartElement(subProcess);

    if (startElement == null) {
        throw new FlowableException("No initial activity found for subprocess " + subProcess.getId());
    }

    ExecutionEntity executionEntity = (ExecutionEntity) execution;
    executionEntity.setScope(true);
    
    ProcessEngineConfigurationImpl processEngineConfiguration = CommandContextUtil.getProcessEngineConfiguration();

    // initialize the template-defined data objects as variables
    Map<String, Object> dataObjectVars = processDataObjects(subProcess.getDataObjects());
    if (dataObjectVars != null) {
        executionEntity.setVariablesLocal(dataObjectVars);
    }
    
    CommandContext commandContext = Context.getCommandContext();
    ProcessInstanceHelper processInstanceHelper = processEngineConfiguration.getProcessInstanceHelper();
    processInstanceHelper.processAvailableEventSubProcesses(executionEntity, subProcess, commandContext);

    ExecutionEntity startSubProcessExecution = CommandContextUtil.getExecutionEntityManager(commandContext).createChildExecution(executionEntity);
    startSubProcessExecution.setCurrentFlowElement(startElement);
    CommandContextUtil.getAgenda().planContinueProcessOperation(startSubProcessExecution);
}
 
Example 11
Source File: EventSubProcessTimerStartEventActivityBehavior.java    From flowable-engine with Apache License 2.0 5 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);
            }
        }
    }

    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 12
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 13
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 14
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 15
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);
                        }
                    }
                }
            }
        }
    }
}
 
Example 16
Source File: CompensationEventHandler.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
@Override
public void handleEvent(EventSubscriptionEntity eventSubscription, Object payload, CommandContext commandContext) {

    String configuration = eventSubscription.getConfiguration();
    if (configuration == null) {
        throw new FlowableException("Compensating execution not set for compensate event subscription with id " + eventSubscription.getId());
    }

    ExecutionEntity compensatingExecution = CommandContextUtil.getExecutionEntityManager(commandContext).findById(configuration);

    String processDefinitionId = compensatingExecution.getProcessDefinitionId();
    Process process = ProcessDefinitionUtil.getProcess(processDefinitionId);
    if (process == null) {
        throw new FlowableException("Cannot start process instance. Process model (id = " + processDefinitionId + ") could not be found");
    }

    FlowElement flowElement = process.getFlowElement(eventSubscription.getActivityId(), true);

    if (flowElement instanceof SubProcess && !((SubProcess) flowElement).isForCompensation()) {

        // descend into scope:
        compensatingExecution.setScope(true);
        List<CompensateEventSubscriptionEntity> eventsForThisScope = CommandContextUtil.getEventSubscriptionService(commandContext).findCompensateEventSubscriptionsByExecutionId(compensatingExecution.getId());
        ScopeUtil.throwCompensationEvent(eventsForThisScope, compensatingExecution, false);

    } else {

        try {

            FlowableEventDispatcher eventDispatcher = CommandContextUtil.getProcessEngineConfiguration(commandContext).getEventDispatcher();
            if (eventDispatcher != null && eventDispatcher.isEnabled()) {
                eventDispatcher.dispatchEvent(
                        FlowableEventBuilder.createActivityEvent(FlowableEngineEventType.ACTIVITY_COMPENSATE, flowElement.getId(), flowElement.getName(),
                                compensatingExecution.getId(), compensatingExecution.getProcessInstanceId(), compensatingExecution.getProcessDefinitionId(), flowElement));
            }
            
            Activity compensationActivity = null;
            Activity activity = (Activity) flowElement;
            if (!activity.isForCompensation() && activity.getBoundaryEvents().size() > 0) {
                for (BoundaryEvent boundaryEvent : activity.getBoundaryEvents()) {
                    if (boundaryEvent.getEventDefinitions().size() > 0 && boundaryEvent.getEventDefinitions().get(0) instanceof CompensateEventDefinition) {
                        List<Association> associations = process.findAssociationsWithSourceRefRecursive(boundaryEvent.getId());
                        for (Association association : associations) {
                            FlowElement targetElement = process.getFlowElement(association.getTargetRef(), true);
                            if (targetElement instanceof Activity) {
                                Activity targetActivity = (Activity) targetElement;
                                if (targetActivity.isForCompensation()) {
                                    compensationActivity = targetActivity;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            
            if (compensationActivity != null) {
                flowElement = compensationActivity;
            }
            
            compensatingExecution.setCurrentFlowElement(flowElement);
            CommandContextUtil.getAgenda().planContinueProcessInCompensation(compensatingExecution);

        } catch (Exception e) {
            throw new FlowableException("Error while handling compensation event " + eventSubscription, e);
        }

    }
}
 
Example 17
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 18
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 19
Source File: AbstractDynamicStateManager.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
protected ExecutionEntity createEmbeddedSubProcessHierarchy(SubProcess subProcess, ExecutionEntity defaultParentExecution, Map<String, SubProcess> subProcessesToCreate, Set<String> movingExecutionIds, ProcessInstanceChangeState processInstanceChangeState, CommandContext commandContext) {
    if (processInstanceChangeState.getProcessInstanceActiveEmbeddedExecutions().containsKey(subProcess.getId())) {
        return processInstanceChangeState.getProcessInstanceActiveEmbeddedExecutions().get(subProcess.getId()).get(0);
    }

    if (processInstanceChangeState.getCreatedEmbeddedSubProcesses().containsKey(subProcess.getId())) {
        return processInstanceChangeState.getCreatedEmbeddedSubProcesses().get(subProcess.getId());
    }

    //Create the parent, if needed
    ExecutionEntity parentSubProcess = defaultParentExecution;
    if (subProcess.getSubProcess() != null) {
        parentSubProcess = createEmbeddedSubProcessHierarchy(subProcess.getSubProcess(), defaultParentExecution, subProcessesToCreate, movingExecutionIds, processInstanceChangeState, commandContext);
        processInstanceChangeState.getCreatedEmbeddedSubProcesses().put(subProcess.getSubProcess().getId(), parentSubProcess);
    }
    ExecutionEntityManager executionEntityManager = CommandContextUtil.getExecutionEntityManager(commandContext);

    ExecutionEntity subProcessExecution = executionEntityManager.createChildExecution(parentSubProcess);
    subProcessExecution.setCurrentFlowElement(subProcess);
    subProcessExecution.setScope(true);

    FlowableEventDispatcher eventDispatcher = CommandContextUtil.getEventDispatcher();
    if (eventDispatcher != null && eventDispatcher.isEnabled()) {
        eventDispatcher.dispatchEvent(
            FlowableEventBuilder.createActivityEvent(FlowableEngineEventType.ACTIVITY_STARTED, subProcess.getId(), subProcess.getName(), subProcessExecution.getId(),
                subProcessExecution.getProcessInstanceId(), subProcessExecution.getProcessDefinitionId(), subProcess));
    }

    subProcessExecution.setVariablesLocal(processDataObjects(subProcess.getDataObjects()));

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

    List<BoundaryEvent> boundaryEvents = subProcess.getBoundaryEvents();
    if (CollectionUtil.isNotEmpty(boundaryEvents)) {
        executeBoundaryEvents(boundaryEvents, subProcessExecution);
    }

    if (subProcess instanceof EventSubProcess) {
        processCreatedEventSubProcess((EventSubProcess) subProcess, subProcessExecution, movingExecutionIds, commandContext);
    }

    ProcessInstanceHelper processInstanceHelper = CommandContextUtil.getProcessEngineConfiguration(commandContext).getProcessInstanceHelper();

    //Process containing child Event SubProcesses not contained in this creation hierarchy
    List<EventSubProcess> childEventSubProcesses = subProcess.findAllSubFlowElementInFlowMapOfType(EventSubProcess.class);
    childEventSubProcesses.stream()
        .filter(childEventSubProcess -> !subProcessesToCreate.containsKey(childEventSubProcess.getId()))
        .forEach(childEventSubProcess -> processInstanceHelper.processEventSubProcess(subProcessExecution, childEventSubProcess, commandContext));

    return subProcessExecution;
}