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

The following examples show how to use org.flowable.engine.impl.persistence.entity.ExecutionEntity#setEventScope() . 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: ScopeUtil.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
/**
 * we create a separate execution for each compensation handler invocation.
 */
public static void throwCompensationEvent(List<CompensateEventSubscriptionEntity> eventSubscriptions, DelegateExecution execution, boolean async) {

    ExecutionEntityManager executionEntityManager = CommandContextUtil.getExecutionEntityManager();

    // first spawn the compensating executions
    for (EventSubscriptionEntity eventSubscription : eventSubscriptions) {
        ExecutionEntity compensatingExecution = null;

        // check whether compensating execution is already created (which is the case when compensating an embedded subprocess,
        // where the compensating execution is created when leaving the subprocess and holds snapshot data).
        if (eventSubscription.getConfiguration() != null) {
            compensatingExecution = executionEntityManager.findById(eventSubscription.getConfiguration());
            compensatingExecution.setParent(compensatingExecution.getProcessInstance());
            compensatingExecution.setEventScope(false);
        } else {
            compensatingExecution = executionEntityManager.createChildExecution((ExecutionEntity) execution);
            eventSubscription.setConfiguration(compensatingExecution.getId());
        }

    }

    // signal compensation events in reverse order of their 'created' timestamp
    Collections.sort(eventSubscriptions, new Comparator<EventSubscriptionEntity>() {
        @Override
        public int compare(EventSubscriptionEntity o1, EventSubscriptionEntity o2) {
            return o2.getCreated().compareTo(o1.getCreated());
        }
    });

    for (CompensateEventSubscriptionEntity compensateEventSubscriptionEntity : eventSubscriptions) {
        EventSubscriptionUtil.eventReceived(compensateEventSubscriptionEntity, null, async);
    }
}
 
Example 2
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 3
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 4
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 5
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 6
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);
}