Java Code Examples for org.activiti.engine.impl.persistence.entity.ExecutionEntityManager#createChildExecution()

The following examples show how to use org.activiti.engine.impl.persistence.entity.ExecutionEntityManager#createChildExecution() . 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: CompleteAdhocSubProcessCmd.java    From activiti6-boot2 with Apache License 2.0 6 votes vote down vote up
public Void execute(CommandContext commandContext) {
  ExecutionEntityManager executionEntityManager = commandContext.getExecutionEntityManager();
  ExecutionEntity execution = executionEntityManager.findById(executionId);
  if (execution == null) {
    throw new ActivitiObjectNotFoundException("No execution found for id '" + executionId + "'", ExecutionEntity.class);
  }

  if (execution.getCurrentFlowElement() instanceof AdhocSubProcess == false) {
    throw new ActivitiException("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 ActivitiException("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);

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

  return null;
}
 
Example 2
Source File: TerminateEndEventActivityBehavior.java    From activiti6-boot2 with Apache License 2.0 6 votes vote down vote up
protected void terminateMultiInstanceRoot(DelegateExecution 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((ExecutionEntity) 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, createDeleteReason(miRootExecutionEntity.getActivityId()));

    Context.getAgenda().planTakeOutgoingSequenceFlowsOperation(siblingExecution, true);
  } else {
    defaultTerminateEndEventBehaviour(execution, commandContext, executionEntityManager);
  }
}
 
Example 3
Source File: EndExecutionOperation.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
protected ExecutionEntity handleRegularExecutionEnd(ExecutionEntityManager executionEntityManager, ExecutionEntity parentExecution) {
  ExecutionEntity executionToContinue = null;

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

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

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

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

      } else {
        executionToContinue = parentExecution;
      }
    }

  } else {
    executionToContinue = parentExecution;
  }
  return executionToContinue;
}
 
Example 4
Source File: ScopeUtil.java    From activiti6-boot2 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 = Context.getCommandContext().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>() {
    public int compare(EventSubscriptionEntity o1, EventSubscriptionEntity o2) {
      return o2.getCreated().compareTo(o1.getCreated());
    }
  });

  for (CompensateEventSubscriptionEntity compensateEventSubscriptionEntity : eventSubscriptions) {
    Context.getCommandContext().getEventSubscriptionEntityManager().eventReceived(compensateEventSubscriptionEntity, null, async);
  }
}
 
Example 5
Source File: BoundaryEventActivityBehavior.java    From activiti6-boot2 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 = commandContext.getExecutionEntityManager();

    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 ActivitiException("Programmatic error: no parent scope execution found for boundary event");
    }

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

    Context.getAgenda().planTakeOutgoingSequenceFlowsOperation(nonInterruptingExecution, true);
  }
 
Example 6
Source File: EventSubProcessMessageStartEventActivityBehavior.java    From activiti6-boot2 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 = commandContext.getExecutionEntityManager();
  ExecutionEntity executionEntity = (ExecutionEntity) execution;
  
  StartEvent startEvent = (StartEvent) execution.getCurrentFlowElement();
  if (startEvent.isInterrupting()) {  
    List<ExecutionEntity> childExecutions = executionEntityManager.findChildExecutionsByParentExecutionId(executionEntity.getParentId());
    for (ExecutionEntity childExecution : childExecutions) {
      if (childExecution.getId().equals(executionEntity.getId()) == false) {
        executionEntityManager.deleteExecutionAndRelatedData(childExecution, 
            DeleteReason.EVENT_SUBPROCESS_INTERRUPTING + "(" + startEvent.getId() + ")", false);
      }
    }
  }
  
  EventSubscriptionEntityManager eventSubscriptionEntityManager = Context.getCommandContext().getEventSubscriptionEntityManager();
  List<EventSubscriptionEntity> eventSubscriptions = executionEntity.getEventSubscriptions();
  for (EventSubscriptionEntity eventSubscription : eventSubscriptions) {
    if (eventSubscription instanceof MessageEventSubscriptionEntity && eventSubscription.getEventName().equals(messageEventDefinition.getMessageRef())) {

      eventSubscriptionEntityManager.delete(eventSubscription);
    }
  }
  
  executionEntity.setCurrentFlowElement((SubProcess) executionEntity.getCurrentFlowElement().getParentContainer());
  executionEntity.setScope(true);
  
  ExecutionEntity outgoingFlowExecution = executionEntityManager.createChildExecution(executionEntity);
  outgoingFlowExecution.setCurrentFlowElement(startEvent);
  
  leave(outgoingFlowExecution);
}
 
Example 7
Source File: EndExecutionOperation.java    From activiti6-boot2 with Apache License 2.0 4 votes vote down vote up
protected ExecutionEntity handleSubProcessEnd(ExecutionEntityManager executionEntityManager, ExecutionEntity parentExecution, SubProcess subProcess) {

    ExecutionEntity executionToContinue = null;
    // create a new execution to take the outgoing sequence flows
    executionToContinue = executionEntityManager.createChildExecution(parentExecution.getParent());
    executionToContinue.setCurrentFlowElement(subProcess);

    boolean hasCompensation = false;
    if (subProcess instanceof Transaction) {
      hasCompensation = true;
    } else {
      for (FlowElement subElement : subProcess.getFlowElements()) {
        if (subElement instanceof Activity) {
          Activity subActivity = (Activity) subElement;
          if (CollectionUtil.isNotEmpty(subActivity.getBoundaryEvents())) {
            for (BoundaryEvent boundaryEvent : subActivity.getBoundaryEvents()) {
              if (CollectionUtil.isNotEmpty(boundaryEvent.getEventDefinitions()) &&
                  boundaryEvent.getEventDefinitions().get(0) instanceof CompensateEventDefinition) {
                hasCompensation = true;
                break;
              }
            }
          }
        }
      }
    }

    // All executions will be cleaned up afterwards. However, for compensation we need
    // a copy of these executions so we can use them later on when the compensation is thrown.
    // The following method does exactly that, and moves the executions beneath the process instance.
    if (hasCompensation) {
      ScopeUtil.createCopyOfSubProcessExecutionForCompensation(parentExecution);
    }

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

    Context.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(
        ActivitiEventBuilder.createActivityEvent(ActivitiEventType.ACTIVITY_COMPLETED, subProcess.getId(), subProcess.getName(),
            parentExecution.getId(), parentExecution.getProcessInstanceId(), parentExecution.getProcessDefinitionId(), subProcess));
    return executionToContinue;
  }
 
Example 8
Source File: ErrorPropagation.java    From activiti6-boot2 with Apache License 2.0 4 votes vote down vote up
protected static void executeEventHandler(Event event, ExecutionEntity parentExecution, ExecutionEntity currentExecution, String errorId) {
  if (Context.getProcessEngineConfiguration() != null && Context.getProcessEngineConfiguration().getEventDispatcher().isEnabled()) {
    BpmnModel bpmnModel = ProcessDefinitionUtil.getBpmnModel(parentExecution.getProcessDefinitionId());
    if (bpmnModel != null) {

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

      Context.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(
        ActivitiEventBuilder.createErrorEvent(ActivitiEventType.ACTIVITY_ERROR_RECEIVED, event.getId(), errorId, errorCode, parentExecution.getId(),
            parentExecution.getProcessInstanceId(), parentExecution.getProcessDefinitionId()));
    }
  }

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

    if (currentExecution.getParentId().equals(parentExecution.getId()) == false) {
      Context.getAgenda().planDestroyScopeOperation(currentExecution);
    } else {
      executionEntityManager.deleteExecutionAndRelatedData(currentExecution, null, false);
    }

    ExecutionEntity eventSubProcessExecution = executionEntityManager.createChildExecution(parentExecution);
    eventSubProcessExecution.setCurrentFlowElement(event);
    Context.getAgenda().planContinueProcessOperation(eventSubProcessExecution);

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

    Context.getAgenda().planTriggerExecutionOperation(boundaryExecution);
  }
}
 
Example 9
Source File: SequentialMultiInstanceBehavior.java    From activiti6-boot2 with Apache License 2.0 4 votes vote down vote up
/**
 * Called when the wrapped {@link ActivityBehavior} calls the {@link AbstractBpmnActivityBehavior#leave(ActivityExecution)} method. Handles the completion of one instance, and executes the logic for
 * the sequential behavior.
 */
public void leave(DelegateExecution childExecution) {
  DelegateExecution multiInstanceRootExecution = getMultiInstanceRootExecution(childExecution);
  int nrOfInstances = getLoopVariable(multiInstanceRootExecution, NUMBER_OF_INSTANCES);
  int loopCounter = getLoopVariable(childExecution, getCollectionElementIndexVariable()) + 1;
  int nrOfCompletedInstances = getLoopVariable(multiInstanceRootExecution, NUMBER_OF_COMPLETED_INSTANCES) + 1;
  int nrOfActiveInstances = getLoopVariable(multiInstanceRootExecution, NUMBER_OF_ACTIVE_INSTANCES);

  setLoopVariable(multiInstanceRootExecution, NUMBER_OF_COMPLETED_INSTANCES, nrOfCompletedInstances);
  setLoopVariable(childExecution, getCollectionElementIndexVariable(), loopCounter);
  logLoopDetails(childExecution, "instance completed", loopCounter, nrOfCompletedInstances, nrOfActiveInstances, nrOfInstances);
  
  Context.getCommandContext().getHistoryManager().recordActivityEnd((ExecutionEntity) childExecution, null);
  callActivityEndListeners(childExecution);
  
  //executeCompensationBoundaryEvents(execution.getCurrentFlowElement(), execution);

  if (loopCounter >= nrOfInstances || completionConditionSatisfied(multiInstanceRootExecution)) {
    removeLocalLoopVariable(childExecution, getCollectionElementIndexVariable());
    multiInstanceRootExecution.setMultiInstanceRoot(false);
    multiInstanceRootExecution.setScope(false);
    multiInstanceRootExecution.setCurrentFlowElement(childExecution.getCurrentFlowElement());
    Context.getCommandContext().getExecutionEntityManager().deleteChildExecutions((ExecutionEntity) multiInstanceRootExecution, "MI_END", false);
    super.leave(multiInstanceRootExecution);
    
  } else {
    try {
      
      if (childExecution.getCurrentFlowElement() instanceof SubProcess) {
        ExecutionEntityManager executionEntityManager = Context.getCommandContext().getExecutionEntityManager();
        ExecutionEntity executionToContinue = executionEntityManager.createChildExecution((ExecutionEntity) multiInstanceRootExecution);
        executionToContinue.setCurrentFlowElement(childExecution.getCurrentFlowElement());
        executionToContinue.setScope(true);
        setLoopVariable(executionToContinue, getCollectionElementIndexVariable(), loopCounter);
        executeOriginalBehavior(executionToContinue, loopCounter);
      } else {
        executeOriginalBehavior(childExecution, 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 ActivitiException("Could not execute inner activity behavior of multi instance behavior", e);
    }
  }
}