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

The following examples show how to use org.activiti.engine.impl.persistence.entity.ExecutionEntityManager#deleteExecutionAndRelatedData() . 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: 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 3
Source File: EndExecutionOperation.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
protected boolean isAllEventScopeExecutions(ExecutionEntityManager executionEntityManager, ExecutionEntity parentExecution) {
  boolean allEventScopeExecutions = true;
  List<ExecutionEntity> executions = executionEntityManager.findChildExecutionsByParentExecutionId(parentExecution.getId());
  for (ExecutionEntity childExecution : executions) {
    if (childExecution.isEventScope()) {
      executionEntityManager.deleteExecutionAndRelatedData(childExecution, null, false);
    } else {
      allEventScopeExecutions = false;
      break;
    }
  }
  return allEventScopeExecutions;
}
 
Example 4
Source File: BoundaryEventActivityBehavior.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
protected void deleteChildExecutions(ExecutionEntity parentExecution, ExecutionEntity notToDeleteExecution, CommandContext commandContext) {

    // TODO: would be good if this deleteChildExecutions could be removed and the one on the executionEntityManager is used
    // The problem however, is that the 'notToDeleteExecution' is passed here.
    // This could be solved by not reusing an execution, but creating a new

    // Delete all child executions
    ExecutionEntityManager executionEntityManager = commandContext.getExecutionEntityManager();
    Collection<ExecutionEntity> childExecutions = executionEntityManager.findChildExecutionsByParentExecutionId(parentExecution.getId());
    if (CollectionUtil.isNotEmpty(childExecutions)) {
      for (ExecutionEntity childExecution : childExecutions) {
        if (childExecution.getId().equals(notToDeleteExecution.getId()) == false) {
          deleteChildExecutions(childExecution, notToDeleteExecution, commandContext);
        }
      }
    }

    String deleteReason = DeleteReason.BOUNDARY_EVENT_INTERRUPTING + " (" + notToDeleteExecution.getCurrentActivityId() + ")";
    if (parentExecution.getCurrentFlowElement() instanceof CallActivity) {
      ExecutionEntity subProcessExecution = executionEntityManager.findSubProcessInstanceBySuperExecutionId(parentExecution.getId());
      if (subProcessExecution != null) {
        executionEntityManager.deleteProcessInstanceExecutionEntity(subProcessExecution.getId(),
            subProcessExecution.getCurrentActivityId(), deleteReason, true, true);
      }
    }

    executionEntityManager.deleteExecutionAndRelatedData(parentExecution, deleteReason, false);
  }
 
Example 5
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 6
Source File: TerminateEndEventActivityBehavior.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
protected void deleteExecutionEntities(ExecutionEntityManager executionEntityManager, ExecutionEntity rootExecutionEntity, String deleteReason) {

    List<ExecutionEntity> childExecutions = executionEntityManager.collectChildren(rootExecutionEntity);
    for (int i=childExecutions.size()-1; i>=0; i--) {
      executionEntityManager.deleteExecutionAndRelatedData(childExecutions.get(i), deleteReason, false);
    }
    executionEntityManager.deleteExecutionAndRelatedData(rootExecutionEntity, deleteReason, false);
  }
 
Example 7
Source File: ParallelMultiInstanceBehavior.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
protected void deleteChildExecutions(ExecutionEntity parentExecution, boolean deleteExecution, CommandContext commandContext) {
  // Delete all child executions
  ExecutionEntityManager executionEntityManager = commandContext.getExecutionEntityManager();
  Collection<ExecutionEntity> childExecutions = executionEntityManager.findChildExecutionsByParentExecutionId(parentExecution.getId());
  if (CollectionUtil.isNotEmpty(childExecutions)) {
    for (ExecutionEntity childExecution : childExecutions) {
      deleteChildExecutions(childExecution, true, commandContext);
    }
  }

  if (deleteExecution) {
    executionEntityManager.deleteExecutionAndRelatedData(parentExecution, null, false);
  }
}
 
Example 8
Source File: CancelEndEventActivityBehavior.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
protected void deleteChildExecutions(ExecutionEntity parentExecution, ExecutionEntity notToDeleteExecution,
    CommandContext commandContext, String deleteReason) {
  // Delete all child executions
  ExecutionEntityManager executionEntityManager = commandContext.getExecutionEntityManager();
  Collection<ExecutionEntity> childExecutions = executionEntityManager.findChildExecutionsByParentExecutionId(parentExecution.getId());
  if (CollectionUtil.isNotEmpty(childExecutions)) {
    for (ExecutionEntity childExecution : childExecutions) {
      if (childExecution.getId().equals(notToDeleteExecution.getId()) == false) {
        deleteChildExecutions(childExecution, notToDeleteExecution, commandContext, deleteReason);
      }
    }
  }

  executionEntityManager.deleteExecutionAndRelatedData(parentExecution, deleteReason, false);
}
 
Example 9
Source File: EndExecutionOperation.java    From activiti6-boot2 with Apache License 2.0 4 votes vote down vote up
protected void handleRegularExecution() {

    ExecutionEntityManager executionEntityManager = commandContext.getExecutionEntityManager();

    // There will be a parent execution (or else we would be in the process instance handling method)
    ExecutionEntity parentExecution = executionEntityManager.findById(execution.getParentId());

    // If the execution is a scope, all the child executions must be deleted first.
    if (execution.isScope()) {
      executionEntityManager.deleteChildExecutions(execution, null, false);
    }

    // Delete current execution
    logger.debug("Ending execution {}", execution.getId());
    executionEntityManager.deleteExecutionAndRelatedData(execution, null, false);

    logger.debug("Parent execution found. Continuing process using execution {}", parentExecution.getId());

    // When ending an execution in a multi instance subprocess , special care is needed
    if (isEndEventInMultiInstanceSubprocess(execution)) {
        handleMultiInstanceSubProcess(executionEntityManager, parentExecution);
        return;
    }

    SubProcess subProcess = execution.getCurrentFlowElement().getSubProcess();

    // If there are no more active child executions, the process can be continued
    // If not (eg an embedded subprocess still has active elements, we cannot continue)
    if (getNumberOfActiveChildExecutionsForExecution(executionEntityManager, parentExecution.getId()) == 0
        || isAllEventScopeExecutions(executionEntityManager, parentExecution)) {

      ExecutionEntity executionToContinue = null;

      if (subProcess != null) {

        // In case of ending a subprocess: go up in the scopes and continue via the parent scope
        // unless its a compensation, then we don't need to do anything and can just end it

        if (subProcess.isForCompensation()) {
          Context.getAgenda().planEndExecutionOperation(parentExecution);
        } else {
          executionToContinue = handleSubProcessEnd(executionEntityManager, parentExecution, subProcess);
        }

      } else {

        // In the 'regular' case (not being in a subprocess), we use the parent execution to
        // continue process instance execution

        executionToContinue = handleRegularExecutionEnd(executionEntityManager, parentExecution);
      }

      if (executionToContinue != null) {
        // only continue with outgoing sequence flows if the execution is
        // not the process instance root execution (otherwise the process instance is finished)
        if (executionToContinue.isProcessInstanceType()) {
          handleProcessInstanceExecution(executionToContinue);

        } else {
          Context.getAgenda().planTakeOutgoingSequenceFlowsOperation(executionToContinue, true);
        }
      }

    }
  }
 
Example 10
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 11
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 12
Source File: ParallelGatewayActivityBehavior.java    From activiti6-boot2 with Apache License 2.0 4 votes vote down vote up
public void execute(DelegateExecution execution) {

    // First off all, deactivate the execution
    execution.inactivate();

    // Join
    FlowElement flowElement = execution.getCurrentFlowElement();
    ParallelGateway parallelGateway = null;
    if (flowElement instanceof ParallelGateway) {
      parallelGateway = (ParallelGateway) flowElement;
    } else {
      throw new ActivitiException("Programmatic error: parallel gateway behaviour can only be applied" + " to a ParallelGateway instance, but got an instance of " + flowElement);
    }

    lockFirstParentScope(execution);

    DelegateExecution multiInstanceExecution = null;
    if (hasMultiInstanceParent(parallelGateway)) {
      multiInstanceExecution = findMultiInstanceParentExecution(execution);
    }

    ExecutionEntityManager executionEntityManager = Context.getCommandContext().getExecutionEntityManager();
    Collection<ExecutionEntity> joinedExecutions = executionEntityManager.findInactiveExecutionsByActivityIdAndProcessInstanceId(execution.getCurrentActivityId(), execution.getProcessInstanceId());
    if (multiInstanceExecution != null) {
      joinedExecutions = cleanJoinedExecutions(joinedExecutions, multiInstanceExecution);
    }

    int nbrOfExecutionsToJoin = parallelGateway.getIncomingFlows().size();
    int nbrOfExecutionsCurrentlyJoined = joinedExecutions.size();

    // Fork

    // Is needed to set the endTime for all historic activity joins
    Context.getCommandContext().getHistoryManager().recordActivityEnd((ExecutionEntity) execution, null);

    if (nbrOfExecutionsCurrentlyJoined == nbrOfExecutionsToJoin) {

      // Fork
      if (log.isDebugEnabled()) {
        log.debug("parallel gateway '{}' activates: {} of {} joined", execution.getCurrentActivityId(), nbrOfExecutionsCurrentlyJoined, nbrOfExecutionsToJoin);
      }

      if (parallelGateway.getIncomingFlows().size() > 1) {

        // All (now inactive) children are deleted.
        for (ExecutionEntity joinedExecution : joinedExecutions) {

          // The current execution will be reused and not deleted
          if (!joinedExecution.getId().equals(execution.getId())) {
            executionEntityManager.deleteExecutionAndRelatedData(joinedExecution, null, false);
          }

        }
      }

      // TODO: potential optimization here: reuse more then 1 execution, only 1 currently
      Context.getAgenda().planTakeOutgoingSequenceFlowsOperation((ExecutionEntity) execution, false); // false -> ignoring conditions on parallel gw

    } else if (log.isDebugEnabled()) {
      log.debug("parallel gateway '{}' does not activate: {} of {} joined", execution.getCurrentActivityId(), nbrOfExecutionsCurrentlyJoined, nbrOfExecutionsToJoin);
    }

  }
 
Example 13
Source File: InclusiveGatewayActivityBehavior.java    From activiti6-boot2 with Apache License 2.0 4 votes vote down vote up
protected void executeInclusiveGatewayLogic(ExecutionEntity execution) {
  CommandContext commandContext = Context.getCommandContext();
  ExecutionEntityManager executionEntityManager = commandContext.getExecutionEntityManager();

  lockFirstParentScope(execution);

  Collection<ExecutionEntity> allExecutions = executionEntityManager.findChildExecutionsByProcessInstanceId(execution.getProcessInstanceId());
  Iterator<ExecutionEntity> executionIterator = allExecutions.iterator();
  boolean oneExecutionCanReachGateway = false;
  while (!oneExecutionCanReachGateway && executionIterator.hasNext()) {
    ExecutionEntity executionEntity = executionIterator.next();
    if (!executionEntity.getActivityId().equals(execution.getCurrentActivityId())) {
      boolean canReachGateway = ExecutionGraphUtil.isReachable(execution.getProcessDefinitionId(), executionEntity.getActivityId(), execution.getCurrentActivityId());
      if (canReachGateway) {
        oneExecutionCanReachGateway = true;
      }
    } else if (executionEntity.getActivityId().equals(execution.getCurrentActivityId()) && executionEntity.isActive()) {
      // Special case: the execution has reached the inc gw, but the operation hasn't been executed yet for that execution
      oneExecutionCanReachGateway = true;
    }
  }

  // If no execution can reach the gateway, the gateway activates and executes fork behavior
  if (!oneExecutionCanReachGateway) {

    logger.debug("Inclusive gateway cannot be reached by any execution and is activated");

    // Kill all executions here (except the incoming)
    Collection<ExecutionEntity> executionsInGateway = executionEntityManager
        .findInactiveExecutionsByActivityIdAndProcessInstanceId(execution.getCurrentActivityId(), execution.getProcessInstanceId());
    for (ExecutionEntity executionEntityInGateway : executionsInGateway) {
      if (!executionEntityInGateway.getId().equals(execution.getId())) {
        commandContext.getHistoryManager().recordActivityEnd(executionEntityInGateway, null);
        executionEntityManager.deleteExecutionAndRelatedData(executionEntityInGateway, null, false);
      }
    }

    // Leave
    commandContext.getAgenda().planTakeOutgoingSequenceFlowsOperation(execution, true);
  }
}
 
Example 14
Source File: BoundaryCancelEventActivityBehavior.java    From activiti6-boot2 with Apache License 2.0 4 votes vote down vote up
@Override
public void trigger(DelegateExecution execution, String triggerName, Object triggerData) {
  BoundaryEvent boundaryEvent = (BoundaryEvent) execution.getCurrentFlowElement();
  
  CommandContext commandContext = Context.getCommandContext();
  ExecutionEntityManager executionEntityManager = commandContext.getExecutionEntityManager();
  
  ExecutionEntity subProcessExecution = null;
  // TODO: this can be optimized. A full search in the all executions shouldn't be needed
  List<ExecutionEntity> processInstanceExecutions = executionEntityManager.findChildExecutionsByProcessInstanceId(execution.getProcessInstanceId());
  for (ExecutionEntity childExecution : processInstanceExecutions) {
    if (childExecution.getCurrentFlowElement() != null 
        && childExecution.getCurrentFlowElement().getId().equals(boundaryEvent.getAttachedToRefId())) {
      subProcessExecution = childExecution;
      break;
    }
  }
  
  if (subProcessExecution == null) {
    throw new ActivitiException("No execution found for sub process of boundary cancel event " + boundaryEvent.getId());
  }
  
  EventSubscriptionEntityManager eventSubscriptionEntityManager = commandContext.getEventSubscriptionEntityManager();
  List<CompensateEventSubscriptionEntity> eventSubscriptions = eventSubscriptionEntityManager.findCompensateEventSubscriptionsByExecutionId(subProcessExecution.getParentId());

  if (eventSubscriptions.isEmpty()) {
    leave(execution);
  } else {
    
    String deleteReason = DeleteReason.BOUNDARY_EVENT_INTERRUPTING + "(" + boundaryEvent.getId() + ")";
    
    // cancel boundary is always sync
    ScopeUtil.throwCompensationEvent(eventSubscriptions, execution, false);
    executionEntityManager.deleteExecutionAndRelatedData(subProcessExecution, deleteReason, false);
    if (subProcessExecution.getCurrentFlowElement() instanceof Activity) {
      Activity activity = (Activity) subProcessExecution.getCurrentFlowElement();
      if (activity.getLoopCharacteristics() != null) {
        ExecutionEntity miExecution = subProcessExecution.getParent();
        List<ExecutionEntity> miChildExecutions = executionEntityManager.findChildExecutionsByParentExecutionId(miExecution.getId());
        for (ExecutionEntity miChildExecution : miChildExecutions) {
          if (subProcessExecution.getId().equals(miChildExecution.getId()) == false && activity.getId().equals(miChildExecution.getCurrentActivityId())) {
            executionEntityManager.deleteExecutionAndRelatedData(miChildExecution, deleteReason, false);
          }
        }
      }
    }
    leave(execution);
  }
}