Java Code Examples for org.flowable.engine.impl.util.CommandContextUtil#getEventSubscriptionService()

The following examples show how to use org.flowable.engine.impl.util.CommandContextUtil#getEventSubscriptionService() . 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: MessageEventReceivedCmd.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
@Override
protected Void execute(CommandContext commandContext, ExecutionEntity execution) {
    if (messageName == null) {
        throw new FlowableIllegalArgumentException("messageName cannot be null");
    }

    if (Flowable5Util.isFlowable5ProcessDefinitionId(commandContext, execution.getProcessDefinitionId())) {
        Flowable5CompatibilityHandler compatibilityHandler = Flowable5Util.getFlowable5CompatibilityHandler();
        compatibilityHandler.messageEventReceived(messageName, executionId, payload, async);
        return null;
    }

    EventSubscriptionService eventSubscriptionService = CommandContextUtil.getEventSubscriptionService(commandContext);
    List<EventSubscriptionEntity> eventSubscriptions = eventSubscriptionService.findEventSubscriptionsByNameAndExecution(MessageEventHandler.EVENT_HANDLER_TYPE, messageName, executionId);

    if (eventSubscriptions.isEmpty()) {
        throw new FlowableException("Execution with id '" + executionId + "' does not have a subscription to a message event with name '" + messageName + "'");
    }

    // there can be only one:
    EventSubscriptionEntity eventSubscriptionEntity = eventSubscriptions.get(0);
    EventSubscriptionUtil.eventReceived(eventSubscriptionEntity, payload, async);

    return null;
}
 
Example 2
Source File: EventSubscriptionManager.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
protected void removeObsoleteEventRegistryEventSubScription(ProcessDefinitionEntity previousProcessDefinition) {
    // remove all subscriptions for the previous version
    if (previousProcessDefinition != null) {
        EventSubscriptionService eventSubscriptionService = CommandContextUtil.getEventSubscriptionService();
        EventSubscriptionQueryImpl eventSubscriptionQuery = new EventSubscriptionQueryImpl(Context.getCommandContext());
        eventSubscriptionQuery.processDefinitionId(previousProcessDefinition.getId()).scopeType(ScopeTypes.BPMN);
        if (previousProcessDefinition.getTenantId() != null) {
            eventSubscriptionQuery.tenantId(previousProcessDefinition.getTenantId());
        }
        
        List<EventSubscription> subscriptionsToDelete = eventSubscriptionService.findEventSubscriptionsByQueryCriteria(eventSubscriptionQuery);
        for (EventSubscription eventSubscription : subscriptionsToDelete) {
            EventSubscriptionEntity eventSubscriptionEntity = (EventSubscriptionEntity) eventSubscription;
            eventSubscriptionService.deleteEventSubscription(eventSubscriptionEntity);
            CountingEntityUtil.handleDeleteEventSubscriptionEntityCount(eventSubscriptionEntity);
        }
    }
}
 
Example 3
Source File: BoundarySignalEventActivityBehavior.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
@Override
public void trigger(DelegateExecution execution, String triggerName, Object triggerData) {
    ExecutionEntity executionEntity = (ExecutionEntity) execution;
    BoundaryEvent boundaryEvent = (BoundaryEvent) execution.getCurrentFlowElement();
    
    if (boundaryEvent.isCancelActivity()) {
        String eventName = EventDefinitionExpressionUtil.determineSignalName(Context.getCommandContext(), signalEventDefinition,
                ProcessDefinitionUtil.getBpmnModel(execution.getProcessDefinitionId()), execution);
        EventSubscriptionService eventSubscriptionService = CommandContextUtil.getEventSubscriptionService();
        List<EventSubscriptionEntity> eventSubscriptions = executionEntity.getEventSubscriptions();
        for (EventSubscriptionEntity eventSubscription : eventSubscriptions) {
            if (eventSubscription instanceof SignalEventSubscriptionEntity && eventSubscription.getEventName().equals(eventName)) {
                eventSubscriptionService.deleteEventSubscription(eventSubscription);
                CountingEntityUtil.handleDeleteEventSubscriptionEntityCount(eventSubscription);
            }
        }
    }

    super.trigger(executionEntity, triggerName, triggerData);
}
 
Example 4
Source File: EventSubscriptionManager.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
protected void insertEventRegistryEvent(String eventDefinitionKey, StartEvent startEvent, ProcessDefinitionEntity processDefinition, BpmnModel bpmnModel) {
    CommandContext commandContext = Context.getCommandContext();
    EventSubscriptionService eventSubscriptionService = CommandContextUtil.getEventSubscriptionService(commandContext);
    EventSubscriptionBuilder eventSubscriptionBuilder = eventSubscriptionService.createEventSubscriptionBuilder()
            .eventType(eventDefinitionKey)
            .activityId(startEvent.getId())
            .processDefinitionId(processDefinition.getId())
            .scopeType(ScopeTypes.BPMN)
            .configuration(CorrelationUtil.getCorrelationKey(BpmnXMLConstants.ELEMENT_EVENT_CORRELATION_PARAMETER, commandContext, startEvent, null));
            
    if (processDefinition.getTenantId() != null) {
        eventSubscriptionBuilder.tenantId(processDefinition.getTenantId());
    }

    EventSubscription eventSubscription = eventSubscriptionBuilder.create();
    CountingEntityUtil.handleInsertEventSubscriptionEntityCount(eventSubscription);
}
 
Example 5
Source File: BoundaryMessageEventActivityBehavior.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
@Override
public void trigger(DelegateExecution execution, String triggerName, Object triggerData) {
    ExecutionEntity executionEntity = (ExecutionEntity) execution;
    BoundaryEvent boundaryEvent = (BoundaryEvent) execution.getCurrentFlowElement();

    if (boundaryEvent.isCancelActivity()) {

        String messageName = EventDefinitionExpressionUtil.determineMessageName(Context.getCommandContext(), messageEventDefinition, execution);

        EventSubscriptionService eventSubscriptionService = CommandContextUtil.getEventSubscriptionService();
        List<EventSubscriptionEntity> eventSubscriptions = executionEntity.getEventSubscriptions();
        for (EventSubscriptionEntity eventSubscription : eventSubscriptions) {
            if (eventSubscription instanceof MessageEventSubscriptionEntity && eventSubscription.getEventName().equals(messageName)) {
                eventSubscriptionService.deleteEventSubscription(eventSubscription);
                CountingEntityUtil.handleDeleteEventSubscriptionEntityCount(eventSubscription);
            }
        }
    }

    super.trigger(executionEntity, triggerName, triggerData);
}
 
Example 6
Source File: ReceiveEventTaskActivityBehavior.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
@Override
public void trigger(DelegateExecution execution, String signalName, Object signalData) {
    ExecutionEntity executionEntity = (ExecutionEntity) execution;

    Object eventInstance = execution.getTransientVariables().get(EventConstants.EVENT_INSTANCE);
    if (eventInstance instanceof EventInstance) {
        EventInstanceBpmnUtil.handleEventInstanceOutParameters(execution, execution.getCurrentFlowElement(), (EventInstance) eventInstance);
    }

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

    CommandContext commandContext = Context.getCommandContext();
    String eventDefinitionKey = getEventDefinitionKey(commandContext, executionEntity);
    for (EventSubscriptionEntity eventSubscription : eventSubscriptions) {
        if (Objects.equals(eventDefinitionKey, eventSubscription.getEventType())) {
            eventSubscriptionService.deleteEventSubscription(eventSubscription);
            CountingEntityUtil.handleDeleteEventSubscriptionEntityCount(eventSubscription);
        }
    }

    super.leave(execution);
}
 
Example 7
Source File: IntermediateCatchEventRegistryEventActivityBehavior.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
protected ExecutionEntity deleteEventSubscription(DelegateExecution execution) {
    ExecutionEntity executionEntity = (ExecutionEntity) execution;

    Object eventInstance = execution.getTransientVariables().get(EventConstants.EVENT_INSTANCE);
    if (eventInstance instanceof EventInstance) {
        EventInstanceBpmnUtil.handleEventInstanceOutParameters(execution, execution.getCurrentFlowElement(), (EventInstance) eventInstance);
    }

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

    CommandContext commandContext = Context.getCommandContext();
    String eventDefinitionKey = getEventDefinitionKey(commandContext, executionEntity);
    for (EventSubscriptionEntity eventSubscription : eventSubscriptions) {
        if (Objects.equals(eventDefinitionKey, eventSubscription.getEventType())) {
            eventSubscriptionService.deleteEventSubscription(eventSubscription);
            CountingEntityUtil.handleDeleteEventSubscriptionEntityCount(eventSubscription);
        }
    }
    return executionEntity;
}
 
Example 8
Source File: IntermediateCatchMessageEventActivityBehavior.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
protected ExecutionEntity deleteMessageEventSubScription(DelegateExecution execution) {
    CommandContext commandContext = Context.getCommandContext();
    ExecutionEntity executionEntity = (ExecutionEntity) execution;
    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);
        }
    }
    return executionEntity;
}
 
Example 9
Source File: BoundaryEventRegistryEventActivityBehavior.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
@Override
public void trigger(DelegateExecution execution, String triggerName, Object triggerData) {
    ExecutionEntity executionEntity = (ExecutionEntity) execution;
    BoundaryEvent boundaryEvent = (BoundaryEvent) execution.getCurrentFlowElement();
    
    Object eventInstance = execution.getTransientVariables().get(EventConstants.EVENT_INSTANCE);
    if (eventInstance instanceof EventInstance) {
        EventInstanceBpmnUtil.handleEventInstanceOutParameters(execution, boundaryEvent, (EventInstance) eventInstance);
    }

    if (boundaryEvent.isCancelActivity()) {
        EventSubscriptionService eventSubscriptionService = CommandContextUtil.getEventSubscriptionService();
        List<EventSubscriptionEntity> eventSubscriptions = executionEntity.getEventSubscriptions();
        
        CommandContext commandContext = Context.getCommandContext();
        String eventDefinitionKey = getEventDefinitionKey(commandContext, executionEntity);
        for (EventSubscriptionEntity eventSubscription : eventSubscriptions) {
            if (Objects.equals(eventDefinitionKey, eventSubscription.getEventType())) {
                eventSubscriptionService.deleteEventSubscription(eventSubscription);
                CountingEntityUtil.handleDeleteEventSubscriptionEntityCount(eventSubscription);
            }
        }
    }

    super.trigger(executionEntity, triggerName, triggerData);
}
 
Example 10
Source File: IntermediateCatchSignalEventActivityBehavior.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
protected ExecutionEntity deleteSignalEventSubscription(DelegateExecution execution) {
    ExecutionEntity executionEntity = (ExecutionEntity) execution;

    String eventName = EventDefinitionExpressionUtil.determineSignalName(Context.getCommandContext(), signalEventDefinition,
        ProcessDefinitionUtil.getBpmnModel(execution.getProcessDefinitionId()), execution);
    EventSubscriptionService eventSubscriptionService = CommandContextUtil.getEventSubscriptionService();
    List<EventSubscriptionEntity> eventSubscriptions = executionEntity.getEventSubscriptions();
    for (EventSubscriptionEntity eventSubscription : eventSubscriptions) {
        if (eventSubscription instanceof SignalEventSubscriptionEntity && eventSubscription.getEventName().equals(eventName)) {

            eventSubscriptionService.deleteEventSubscription(eventSubscription);
            CountingEntityUtil.handleDeleteEventSubscriptionEntityCount(eventSubscription);
        }
    }
    return executionEntity;
}
 
Example 11
Source File: EventSubscriptionManager.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
protected void removeObsoleteEventSubscriptionsImpl(ProcessDefinitionEntity processDefinition, String eventHandlerType) {
    // remove all subscriptions for the previous version
    EventSubscriptionService eventSubscriptionService = CommandContextUtil.getEventSubscriptionService();
    List<EventSubscriptionEntity> subscriptionsToDelete = eventSubscriptionService
                    .findEventSubscriptionsByTypeAndProcessDefinitionId(eventHandlerType, processDefinition.getId(), processDefinition.getTenantId());

    for (EventSubscriptionEntity eventSubscriptionEntity : subscriptionsToDelete) {
        eventSubscriptionService.deleteEventSubscription(eventSubscriptionEntity);
        CountingEntityUtil.handleDeleteEventSubscriptionEntityCount(eventSubscriptionEntity);
    }
}
 
Example 12
Source File: ExecutionEntityManagerImpl.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
protected void deleteEventSubScriptions(ExecutionEntity executionEntity, boolean enableExecutionRelationshipCounts, boolean eventDispatcherEnabled) {
    if (!enableExecutionRelationshipCounts
            || (enableExecutionRelationshipCounts && ((CountingExecutionEntity) executionEntity).getEventSubscriptionCount() > 0)) {
        
        EventSubscriptionService eventSubscriptionService = CommandContextUtil.getEventSubscriptionService();
        
        boolean deleteEventSubscriptions = true;
        if (eventDispatcherEnabled) {
            List<EventSubscriptionEntity> eventSubscriptions = eventSubscriptionService.findEventSubscriptionsByExecution(executionEntity.getId());
            for (EventSubscriptionEntity eventSubscription : eventSubscriptions) {
                
                fireEntityDeletedEvent(eventSubscription);
                if (MessageEventSubscriptionEntity.EVENT_TYPE.equals(eventSubscription.getEventType())) {
                    getEventDispatcher().dispatchEvent(FlowableEventBuilder.createMessageEvent(FlowableEngineEventType.ACTIVITY_MESSAGE_CANCELLED,
                            eventSubscription.getActivityId(), eventSubscription.getEventName(), null, eventSubscription.getExecutionId(),
                            eventSubscription.getProcessInstanceId(), eventSubscription.getProcessDefinitionId()));
                }
            }
            
            deleteEventSubscriptions = !eventSubscriptions.isEmpty();
        }
        
        if (deleteEventSubscriptions) {
            eventSubscriptionService.deleteEventSubscriptionsByExecutionId(executionEntity.getId());
        }
    }
}
 
Example 13
Source File: IntermediateThrowSignalEventActivityBehavior.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(DelegateExecution execution) {

    CommandContext commandContext = Context.getCommandContext();

    List<SignalEventSubscriptionEntity> subscriptionEntities = null;
    if (processInstanceScope) {
        subscriptionEntities = commandContext
                .getEventSubscriptionEntityManager()
                .findSignalEventSubscriptionsByProcessInstanceAndEventName(execution.getProcessInstanceId(), signalDefinition.getEventName());
    } else {
        subscriptionEntities = commandContext
                .getEventSubscriptionEntityManager()
                .findSignalEventSubscriptionsByEventName(signalDefinition.getEventName(), execution.getTenantId());
    }

    for (SignalEventSubscriptionEntity signalEventSubscriptionEntity : subscriptionEntities) {
        ProcessDefinition processDefinition = ProcessDefinitionUtil.getProcessDefinition(signalEventSubscriptionEntity.getProcessDefinitionId());
        if (Flowable5Util.isVersion5Tag(processDefinition.getEngineVersion())) {
            signalEventSubscriptionEntity.eventReceived(null, signalDefinition.isAsync());
            
        } else {
            EventSubscriptionService eventSubscriptionService = CommandContextUtil.getEventSubscriptionService();
            EventSubscriptionEntity flowable6EventSubscription = eventSubscriptionService.findById(signalEventSubscriptionEntity.getId());
            EventSubscriptionUtil.eventReceived(flowable6EventSubscription, null, signalDefinition.isAsync());
        }
    }

    ActivityExecution activityExecution = (ActivityExecution) execution;
    if (activityExecution.getActivity() != null) { // don't continue if process has already finished
        leave(activityExecution);
    }
}
 
Example 14
Source File: SendEventTaskActivityBehavior.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Override
public void trigger(DelegateExecution execution, String signalName, Object signalData) {
    if (sendEventServiceTask.isTriggerable()) {
        Object eventInstance = execution.getTransientVariables().get(EventConstants.EVENT_INSTANCE);
        if (eventInstance instanceof EventInstance) {
            EventInstanceBpmnUtil.handleEventInstanceOutParameters(execution, sendEventServiceTask, (EventInstance) eventInstance);
        }

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

        String eventType = null;
        if (StringUtils.isNotEmpty(sendEventServiceTask.getTriggerEventType())) {
            eventType = sendEventServiceTask.getTriggerEventType();
        } else {
            eventType = sendEventServiceTask.getEventType();
        }
        
        EventModel eventModel = null;
        if (Objects.equals(ProcessEngineConfiguration.NO_TENANT_ID, execution.getTenantId())) {
            eventModel = CommandContextUtil.getEventRepositoryService(commandContext).getEventModelByKey(eventType);
        } else {
            eventModel = CommandContextUtil.getEventRepositoryService(commandContext).getEventModelByKey(eventType, execution.getTenantId());
        }

        for (EventSubscriptionEntity eventSubscription : eventSubscriptions) {
            if (Objects.equals(eventModel.getKey(), eventSubscription.getEventType())) {
                eventSubscriptionService.deleteEventSubscription(eventSubscription);
            }
        }
        
        leave(execution);
    }
}
 
Example 15
Source File: SignalThrowingEventListener.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Override
public void onEvent(FlowableEvent event) {
    if (isValidEvent(event) && event instanceof FlowableEngineEvent) {

        FlowableEngineEvent engineEvent = (FlowableEngineEvent) event;

        if (engineEvent.getProcessInstanceId() == null && processInstanceScope) {
            throw new FlowableIllegalArgumentException("Cannot throw process-instance scoped signal, since the dispatched event is not part of an ongoing process instance");
        }

        CommandContext commandContext = Context.getCommandContext();
        EventSubscriptionService eventSubscriptionService = CommandContextUtil.getEventSubscriptionService(commandContext);
        List<SignalEventSubscriptionEntity> subscriptionEntities = null;
        if (processInstanceScope) {
            subscriptionEntities = eventSubscriptionService.findSignalEventSubscriptionsByProcessInstanceAndEventName(engineEvent.getProcessInstanceId(), signalName);
        } else {
            String tenantId = null;
            if (engineEvent.getProcessDefinitionId() != null) {
                ProcessDefinition processDefinition = CommandContextUtil.getProcessEngineConfiguration(commandContext)
                        .getDeploymentManager()
                        .findDeployedProcessDefinitionById(engineEvent.getProcessDefinitionId());
                tenantId = processDefinition.getTenantId();
            }
            subscriptionEntities = eventSubscriptionService.findSignalEventSubscriptionsByEventName(signalName, tenantId);
        }

        for (SignalEventSubscriptionEntity signalEventSubscriptionEntity : subscriptionEntities) {
            EventSubscriptionUtil.eventReceived(signalEventSubscriptionEntity, null, false);
        }
    }
}
 
Example 16
Source File: ProcessEventJobHandler.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(JobEntity job, String configuration, VariableScope variableScope, CommandContext commandContext) {

    EventSubscriptionService eventSubscriptionService = CommandContextUtil.getEventSubscriptionService(commandContext);

    // lookup subscription:
    EventSubscriptionEntity eventSubscriptionEntity = eventSubscriptionService.findById(configuration);

    // if event subscription is null, ignore
    if (eventSubscriptionEntity != null) {
        EventSubscriptionUtil.eventReceived(eventSubscriptionEntity, null, false);
    }

}
 
Example 17
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 18
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 19
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 20
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());
    }
}