Java Code Examples for org.activiti.engine.impl.persistence.entity.ExecutionEntity#getParent()

The following examples show how to use org.activiti.engine.impl.persistence.entity.ExecutionEntity#getParent() . 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: ExecutionTreeUtil.java    From activiti6-boot2 with Apache License 2.0 6 votes vote down vote up
public static ExecutionTree buildExecutionTree(DelegateExecution executionEntity) {
  
  // Find highest parent
  ExecutionEntity parentExecution = (ExecutionEntity) executionEntity;
  while (parentExecution.getParentId() != null || parentExecution.getSuperExecutionId() != null) {
    if (parentExecution.getParentId() != null) {
      parentExecution = parentExecution.getParent();
    } else {
      parentExecution = parentExecution.getSuperExecution();
    }
  }
  
  // Collect all child executions now we have the parent
  List<ExecutionEntity> allExecutions = new ArrayList<ExecutionEntity>();
  allExecutions.add(parentExecution);
  collectChildExecutions(parentExecution, allExecutions);
  return buildExecutionTree(allExecutions);
}
 
Example 2
Source File: ActivitiProcessStartedEventImpl.java    From activiti6-boot2 with Apache License 2.0 6 votes vote down vote up
public ActivitiProcessStartedEventImpl(final Object entity, final Map variables, final boolean localScope) {
  super(entity, variables, localScope, ActivitiEventType.PROCESS_STARTED);
  if (entity instanceof ExecutionEntity) {
    ExecutionEntity executionEntity = (ExecutionEntity) entity;
    if (executionEntity.isProcessInstanceType() == false) {
      executionEntity = executionEntity.getParent();
    }
    
    final ExecutionEntity superExecution = executionEntity.getSuperExecution();
    if (superExecution != null) {
      this.nestedProcessDefinitionId = superExecution.getProcessDefinitionId();
      this.nestedProcessInstanceId = superExecution.getProcessInstanceId();
    } else {
      this.nestedProcessDefinitionId = null;
      this.nestedProcessInstanceId = null;
    }
    
  } else {
    this.nestedProcessDefinitionId = null;
    this.nestedProcessInstanceId = null;
  }
}
 
Example 3
Source File: TakeOutgoingSequenceFlowsOperation.java    From activiti6-boot2 with Apache License 2.0 6 votes vote down vote up
/**
 * @param executionEntityToIgnore The execution entity which we can ignore to be ended,
 * as it's the execution currently being handled in this operation.
 */
protected ExecutionEntity findNextParentScopeExecutionWithAllEndedChildExecutions(ExecutionEntity executionEntity, ExecutionEntity executionEntityToIgnore) {
  if (executionEntity.getParentId() != null) {
    ExecutionEntity scopeExecutionEntity = executionEntity.getParent();

    // Find next scope
    while (!scopeExecutionEntity.isScope() || !scopeExecutionEntity.isProcessInstanceType()) {
      scopeExecutionEntity = scopeExecutionEntity.getParent();
    }

    // Return when all child executions for it are ended
    if (allChildExecutionsEnded(scopeExecutionEntity, executionEntityToIgnore)) {
      return scopeExecutionEntity;
    }

  }
  return null;
}
 
Example 4
Source File: ErrorPropagation.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
public static boolean mapException(Exception e, ExecutionEntity execution, List<MapExceptionEntry> exceptionMap) {
  String errorCode = findMatchingExceptionMapping(e, exceptionMap);
  if (errorCode != null) {
    propagateError(errorCode, execution);
    return true;
  } else {
    ExecutionEntity callActivityExecution = null;
    ExecutionEntity parentExecution = execution.getParent();
    while (parentExecution != null && callActivityExecution == null) {
      if (parentExecution.getId().equals(parentExecution.getProcessInstanceId())) {
        if (parentExecution.getSuperExecution() != null) {
          callActivityExecution = parentExecution.getSuperExecution();
        } else {
          parentExecution = null;
        }
      } else {
        parentExecution = parentExecution.getParent();
      }
    }

    if (callActivityExecution != null) {
      CallActivity callActivity = (CallActivity) callActivityExecution.getCurrentFlowElement();
      if (CollectionUtil.isNotEmpty(callActivity.getMapExceptions())) {
        errorCode = findMatchingExceptionMapping(e, callActivity.getMapExceptions());
        if (errorCode != null) {
          propagateError(errorCode, callActivityExecution);
          return true;
        }
      }
    }

    return false;
  }
}
 
Example 5
Source File: SubProcessVariableSnapshotter.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
public void setVariablesSnapshots(ExecutionEntity sourceExecution, ExecutionEntity snapshotHolder) {
    snapshotHolder.setVariablesLocal(sourceExecution.getVariablesLocal());

    ExecutionEntity parentExecution = sourceExecution.getParent();
    if (parentExecution != null && parentExecution.isMultiInstanceRoot()) {
        snapshotHolder.setVariablesLocal(parentExecution.getVariablesLocal());
    }

}
 
Example 6
Source File: ErrorPropagation.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
private static ActivityExecution getSuperExecution(ActivityExecution execution) {
    ExecutionEntity executionEntity = (ExecutionEntity) execution;
    ExecutionEntity superExecution = executionEntity.getProcessInstance().getSuperExecution();
    if (superExecution != null && !superExecution.isScope()) {
        return superExecution.getParent();
    }
    return superExecution;
}
 
Example 7
Source File: TerminateEndEventActivityBehavior.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
protected ActivityExecution findRootProcessInstanceExecution(ExecutionEntity execution) {
    ExecutionEntity currentExecution = execution;
    while (currentExecution.getParentId() != null || currentExecution.getSuperExecutionId() != null) {
        ExecutionEntity parentExecution = currentExecution.getParent();
        if (parentExecution != null) {
            currentExecution = parentExecution;
        } else if (currentExecution.getSuperExecutionId() != null) {
            currentExecution = currentExecution.getSuperExecution();
        }
    }
    return currentExecution;
}
 
Example 8
Source File: TerminateEndEventActivityBehavior.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
protected List<ExecutionEntity> orderExecutionsRootToLeaf(ExecutionEntity execution) {

        // Find root process instance
        ExecutionEntity rootExecution = execution;
        while (rootExecution.getParent() != null || rootExecution.getSuperExecution() != null) {
            rootExecution = rootExecution.getParent() != null ? rootExecution.getParent() : rootExecution.getSuperExecution();
        }

        return orderExecutionsRootToLeaf(rootExecution, new ArrayList<>());
    }
 
Example 9
Source File: CounterSignCmd.java    From lemon with Apache License 2.0 5 votes vote down vote up
/**
 * <li>移除一条并行实例
 */
private void removeParallelInstance() {
    List<ExecutionEntity> executions = getActivieExecutions();

    for (ExecutionEntity executionEntity : executions) {
        String executionVariableAssignee = (String) executionEntity
                .getVariableLocal(collectionElementVariableName);

        if ((executionVariableAssignee != null)
                && executionVariableAssignee.equals(assignee)) {
            executionEntity.remove();

            ExecutionEntity parentConcurrentExecution = executionEntity
                    .getParent();

            if (getActivity().getProperty("type").equals("subProcess")) {
                parentConcurrentExecution = parentConcurrentExecution
                        .getParent();
            }

            setLoopVariable(parentConcurrentExecution, "nrOfInstances",
                    (Integer) parentConcurrentExecution
                            .getVariableLocal("nrOfInstances") - 1);
            setLoopVariable(parentConcurrentExecution,
                    "nrOfActiveInstances",
                    (Integer) parentConcurrentExecution
                            .getVariableLocal("nrOfActiveInstances") - 1);

            break;
        }
    }
}
 
Example 10
Source File: ErrorPropagation.java    From activiti6-boot2 with Apache License 2.0 4 votes vote down vote up
protected static void executeCatch(Map<String, List<Event>> eventMap, DelegateExecution delegateExecution, String errorId) {
  Event matchingEvent = null;
  ExecutionEntity currentExecution = (ExecutionEntity) delegateExecution;
  ExecutionEntity parentExecution = null;

  if (eventMap.containsKey(currentExecution.getActivityId())) {
    matchingEvent = eventMap.get(currentExecution.getActivityId()).get(0);

    // Check for multi instance
    if (currentExecution.getParentId() != null && currentExecution.getParent().isMultiInstanceRoot()) {
      parentExecution = currentExecution.getParent();
    } else {
      parentExecution = currentExecution;
    }

  } else {
    parentExecution = currentExecution.getParent();

    // Traverse parents until one is found that is a scope and matches the activity the boundary event is defined on
    while (matchingEvent == null && parentExecution != null) {
      FlowElementsContainer currentContainer = null;
      if (parentExecution.getCurrentFlowElement() instanceof FlowElementsContainer) {
        currentContainer = (FlowElementsContainer) parentExecution.getCurrentFlowElement();
      } else if (parentExecution.getId().equals(parentExecution.getProcessInstanceId())) {
        currentContainer = ProcessDefinitionUtil.getProcess(parentExecution.getProcessDefinitionId());
      }

      for (String refId : eventMap.keySet()) {
        List<Event> events = eventMap.get(refId);
        if (CollectionUtil.isNotEmpty(events) && events.get(0) instanceof StartEvent) {
          if (currentContainer.getFlowElement(refId) != null) {
            matchingEvent = events.get(0);
          }
        }
      }

      if (matchingEvent == null) {
        if (eventMap.containsKey(parentExecution.getActivityId())) {
          matchingEvent = eventMap.get(parentExecution.getActivityId()).get(0);

          // Check for multi instance
          if (parentExecution.getParentId() != null && parentExecution.getParent().isMultiInstanceRoot()) {
            parentExecution = parentExecution.getParent();
          }

        } else if (StringUtils.isNotEmpty(parentExecution.getParentId())) {
          parentExecution = parentExecution.getParent();
        } else {
          parentExecution = null;
        }
      }
    }
  }

  if (matchingEvent != null && parentExecution != null) {
    executeEventHandler(matchingEvent, parentExecution, currentExecution, errorId);
  } else {
    throw new ActivitiException("No matching parent execution for error code " + errorId + " found");
  }
}
 
Example 11
Source File: BoundaryCompensateEventActivityBehavior.java    From activiti6-boot2 with Apache License 2.0 4 votes vote down vote up
@Override
public void execute(DelegateExecution execution) {
  ExecutionEntity executionEntity = (ExecutionEntity) execution;
  BoundaryEvent boundaryEvent = (BoundaryEvent) execution.getCurrentFlowElement();
  
  Process process = ProcessDefinitionUtil.getProcess(execution.getProcessDefinitionId());
  if (process == null) {
    throw new ActivitiException("Process model (id = " + execution.getId() + ") could not be found");
  }
  
  Activity compensationActivity = null;
  List<Association> associations = process.findAssociationsWithSourceRefRecursive(boundaryEvent.getId());
  for (Association association : associations) {
    FlowElement targetElement = process.getFlowElement(association.getTargetRef(), true);
    if (targetElement instanceof Activity) {
      Activity activity = (Activity) targetElement;
      if (activity.isForCompensation()) {
        compensationActivity = activity;
        break;
      }
    }
  }
  
  if (compensationActivity == null) {
    throw new ActivitiException("Compensation activity could not be found (or it is missing 'isForCompensation=\"true\"'");
  }
  
  // find SubProcess or Process instance execution
  ExecutionEntity scopeExecution = null;
  ExecutionEntity parentExecution = executionEntity.getParent();
  while (scopeExecution == null && parentExecution != null) {
    if (parentExecution.getCurrentFlowElement() instanceof SubProcess) {
      scopeExecution = parentExecution;
      
    } else if (parentExecution.isProcessInstanceType()) {
      scopeExecution = parentExecution;
    } else {
      parentExecution = parentExecution.getParent();
    }
  }
  
  if (scopeExecution == null) {
    throw new ActivitiException("Could not find a scope execution for compensation boundary event " + boundaryEvent.getId());
  }
  
  Context.getCommandContext().getEventSubscriptionEntityManager().insertCompensationEvent(
      scopeExecution, compensationActivity.getId());
}
 
Example 12
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);
  }
}
 
Example 13
Source File: ScopeUtil.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
/**
 * returns the top-most execution sitting in an activity part of the scope defined by 'scopeActivity'.
 */
public static ExecutionEntity findScopeExecutionForScope(ExecutionEntity execution, PvmScope scopeActivity) {

    // TODO: this feels hacky!

    if (scopeActivity instanceof PvmProcessDefinition) {
        return execution.getProcessInstance();

    } else {

        ActivityImpl currentActivity = execution.getActivity();
        ExecutionEntity candiadateExecution = null;
        ExecutionEntity originalExecution = execution;

        while (execution != null) {
            currentActivity = execution.getActivity();
            if (scopeActivity.getActivities().contains(currentActivity) /* does not search rec */
                    || scopeActivity.equals(currentActivity)) {
                // found a candidate execution; lets still check whether we find an
                // execution which is also sitting in an activity part of this scope
                // higher up the hierarchy
                candiadateExecution = execution;
            } else if (currentActivity != null
                    && currentActivity.contains((ActivityImpl) scopeActivity) /* searches rec */) {
                // now we're too "high", the candidate execution is the one.
                break;
            }

            execution = execution.getParent();
        }

        // if activity is scope, we need to get the parent at least:
        if (originalExecution == candiadateExecution
                && originalExecution.getActivity().isScope()
                && !originalExecution.getActivity().equals(scopeActivity)) {
            candiadateExecution = originalExecution.getParent();
        }

        return candiadateExecution;
    }
}
 
Example 14
Source File: BoundaryEventActivityBehavior.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public void execute(DelegateExecution execution) {
    ExecutionEntity executionEntity = (ExecutionEntity) execution;
    ActivityImpl boundaryActivity = executionEntity.getProcessDefinition().findActivity(activityId);
    ActivityImpl interruptedActivity = executionEntity.getActivity();

    List<PvmTransition> outgoingTransitions = boundaryActivity.getOutgoingTransitions();
    List<ExecutionEntity> interruptedExecutions = null;

    if (interrupting) {

        // Call activity
        if (executionEntity.getSubProcessInstance() != null) {
            executionEntity.getSubProcessInstance().deleteCascade(executionEntity.getDeleteReason());
        } else {
            Context.getCommandContext().getHistoryManager().recordActivityEnd(executionEntity);
        }

        executionEntity.setActivity(boundaryActivity);

        interruptedExecutions = new ArrayList<>(executionEntity.getExecutions());
        for (ExecutionEntity interruptedExecution : interruptedExecutions) {
            interruptedExecution.deleteCascade("interrupting boundary event '" + executionEntity.getActivity().getId() + "' fired");
        }

        executionEntity.takeAll(outgoingTransitions, (List) interruptedExecutions);
    } else {
        // non interrupting event, introduced with BPMN 2.0, we need to create a new execution in this case

        // create a new execution and move it out from the timer activity
        ExecutionEntity concurrentRoot = executionEntity.getParent().isConcurrent() ? executionEntity.getParent() : executionEntity;
        ExecutionEntity outgoingExecution = concurrentRoot.createExecution();

        outgoingExecution.setActive(true);
        outgoingExecution.setScope(false);
        outgoingExecution.setConcurrent(true);

        outgoingExecution.takeAll(outgoingTransitions, Collections.EMPTY_LIST);
        outgoingExecution.remove();
        // now we have to move the execution back to the real activity
        // since the execution stays there (non interrupting) and it was
        // set to the boundary event before
        executionEntity.setActivity(interruptedActivity);
    }
}
 
Example 15
Source File: ParallelMultiInstanceBehavior.java    From flowable-engine 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 of the parallel instances
 */
@Override
public void leave(ActivityExecution execution) {
    callActivityEndListeners(execution);

    int nrOfInstances = getLoopVariable(execution, NUMBER_OF_INSTANCES);
    if (nrOfInstances == 0) {
        // Empty collection, just leave.
        super.leave(execution);
        return;
    }

    int loopCounter = getLoopVariable(execution, getCollectionElementIndexVariable());
    int nrOfCompletedInstances = getLoopVariable(execution, NUMBER_OF_COMPLETED_INSTANCES) + 1;
    int nrOfActiveInstances = getLoopVariable(execution, NUMBER_OF_ACTIVE_INSTANCES) - 1;

    if (isExtraScopeNeeded()) {
        // In case an extra scope was created, it must be destroyed first before going further
        ExecutionEntity extraScope = (ExecutionEntity) execution;
        execution = execution.getParent();
        extraScope.remove();
    }

    if (execution.getParent() != null) { // will be null in case of empty collection
        setLoopVariable(execution.getParent(), NUMBER_OF_COMPLETED_INSTANCES, nrOfCompletedInstances);
        setLoopVariable(execution.getParent(), NUMBER_OF_ACTIVE_INSTANCES, nrOfActiveInstances);
    }
    logLoopDetails(execution, "instance completed", loopCounter, nrOfCompletedInstances, nrOfActiveInstances, nrOfInstances);

    ExecutionEntity executionEntity = (ExecutionEntity) execution;

    if (executionEntity.getParent() != null) {

        executionEntity.inactivate();
        executionEntity.getParent().forceUpdate();

        List<ActivityExecution> joinedExecutions = executionEntity.findInactiveConcurrentExecutions(execution.getActivity());
        if (joinedExecutions.size() >= nrOfInstances || completionConditionSatisfied(execution)) {

            // Removing all active child executions (ie because completionCondition is true)
            List<ExecutionEntity> executionsToRemove = new ArrayList<>();
            for (ActivityExecution childExecution : executionEntity.getParent().getExecutions()) {
                if (childExecution.isActive()) {
                    executionsToRemove.add((ExecutionEntity) childExecution);
                }
            }
            for (ExecutionEntity executionToRemove : executionsToRemove) {
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("Execution {} still active, but multi-instance is completed. Removing this execution.", executionToRemove);
                }
                executionToRemove.inactivate();
                executionToRemove.deleteCascade("multi-instance completed");
            }
            executionEntity.takeAll(executionEntity.getActivity().getOutgoingTransitions(), joinedExecutions);
        }

    } else {
        super.leave(executionEntity);
    }
}