Search in sources :

Example 1 with TransitionInstanceImpl

use of org.camunda.bpm.engine.impl.persistence.entity.TransitionInstanceImpl in project camunda-bpm-platform by camunda.

the class GetActivityInstanceCmd method createTransitionInstance.

protected TransitionInstanceImpl createTransitionInstance(PvmExecutionImpl execution) {
    TransitionInstanceImpl transitionInstance = new TransitionInstanceImpl();
    // can use execution id as persistent ID for transition as an execution
    // can execute as most one transition at a time.
    transitionInstance.setId(execution.getId());
    transitionInstance.setParentActivityInstanceId(execution.getParentActivityInstanceId());
    transitionInstance.setProcessInstanceId(execution.getProcessInstanceId());
    transitionInstance.setProcessDefinitionId(execution.getProcessDefinitionId());
    transitionInstance.setExecutionId(execution.getId());
    transitionInstance.setActivityId(execution.getActivityId());
    ActivityImpl activity = execution.getActivity();
    if (activity != null) {
        String name = activity.getName();
        if (name == null) {
            name = (String) activity.getProperty("name");
        }
        transitionInstance.setActivityName(name);
        transitionInstance.setActivityType((String) activity.getProperty("type"));
    }
    return transitionInstance;
}
Also used : TransitionInstanceImpl(org.camunda.bpm.engine.impl.persistence.entity.TransitionInstanceImpl) ActivityImpl(org.camunda.bpm.engine.impl.pvm.process.ActivityImpl)

Example 2 with TransitionInstanceImpl

use of org.camunda.bpm.engine.impl.persistence.entity.TransitionInstanceImpl in project camunda-bpm-platform by camunda.

the class GetActivityInstanceCmd method execute.

public ActivityInstance execute(CommandContext commandContext) {
    ensureNotNull("processInstanceId", processInstanceId);
    List<ExecutionEntity> executionList = loadProcessInstance(processInstanceId, commandContext);
    if (executionList.isEmpty()) {
        return null;
    }
    checkGetActivityInstance(processInstanceId, commandContext);
    List<ExecutionEntity> nonEventScopeExecutions = filterNonEventScopeExecutions(executionList);
    List<ExecutionEntity> leaves = filterLeaves(nonEventScopeExecutions);
    // Leaves must be ordered in a predictable way (e.g. by ID)
    // in order to return a stable execution tree with every repeated invocation of this command.
    // For legacy process instances, there may miss scope executions for activities that are now a scope.
    // In this situation, there may be multiple scope candidates for the same instance id; which one
    // can depend on the order the leaves are iterated.
    orderById(leaves);
    ExecutionEntity processInstance = filterProcessInstance(executionList);
    if (processInstance.isEnded()) {
        return null;
    }
    // create act instance for process instance
    ActivityInstanceImpl processActInst = createActivityInstance(processInstance, processInstance.getProcessDefinition(), processInstanceId, null);
    Map<String, ActivityInstanceImpl> activityInstances = new HashMap<String, ActivityInstanceImpl>();
    activityInstances.put(processInstanceId, processActInst);
    Map<String, TransitionInstanceImpl> transitionInstances = new HashMap<String, TransitionInstanceImpl>();
    for (ExecutionEntity leaf : leaves) {
        // it will not have an activity set
        if (leaf.getActivity() == null) {
            continue;
        }
        Map<ScopeImpl, PvmExecutionImpl> activityExecutionMapping = leaf.createActivityExecutionMapping();
        Map<ScopeImpl, PvmExecutionImpl> scopeInstancesToCreate = new HashMap<ScopeImpl, PvmExecutionImpl>(activityExecutionMapping);
        // and does not throw compensation
        if (leaf.getActivityInstanceId() != null) {
            if (!CompensationBehavior.isCompensationThrowing(leaf) || LegacyBehavior.isCompensationThrowing(leaf, activityExecutionMapping)) {
                String parentActivityInstanceId = null;
                parentActivityInstanceId = activityExecutionMapping.get(leaf.getActivity().getFlowScope()).getParentActivityInstanceId();
                ActivityInstanceImpl leafInstance = createActivityInstance(leaf, leaf.getActivity(), leaf.getActivityInstanceId(), parentActivityInstanceId);
                activityInstances.put(leafInstance.getId(), leafInstance);
                scopeInstancesToCreate.remove(leaf.getActivity());
            }
        } else {
            TransitionInstanceImpl transitionInstance = createTransitionInstance(leaf);
            transitionInstances.put(transitionInstance.getId(), transitionInstance);
            scopeInstancesToCreate.remove(leaf.getActivity());
        }
        LegacyBehavior.removeLegacyNonScopesFromMapping(scopeInstancesToCreate);
        scopeInstancesToCreate.remove(leaf.getProcessDefinition());
        // create an activity instance for each scope (including compensation throwing executions)
        for (Map.Entry<ScopeImpl, PvmExecutionImpl> scopeExecutionEntry : scopeInstancesToCreate.entrySet()) {
            ScopeImpl scope = scopeExecutionEntry.getKey();
            PvmExecutionImpl scopeExecution = scopeExecutionEntry.getValue();
            String activityInstanceId = null;
            String parentActivityInstanceId = null;
            activityInstanceId = scopeExecution.getParentActivityInstanceId();
            parentActivityInstanceId = activityExecutionMapping.get(scope.getFlowScope()).getParentActivityInstanceId();
            if (activityInstances.containsKey(activityInstanceId)) {
                continue;
            } else {
                // regardless of the tree structure (compacted or not), the scope's activity instance id
                // is the activity instance id of the parent execution and the parent activity instance id
                // of that is the actual parent activity instance id
                ActivityInstanceImpl scopeInstance = createActivityInstance(scopeExecution, scope, activityInstanceId, parentActivityInstanceId);
                activityInstances.put(activityInstanceId, scopeInstance);
            }
        }
    }
    LegacyBehavior.repairParentRelationships(activityInstances.values(), processInstanceId);
    populateChildInstances(activityInstances, transitionInstances);
    return processActInst;
}
Also used : HashMap(java.util.HashMap) PvmExecutionImpl(org.camunda.bpm.engine.impl.pvm.runtime.PvmExecutionImpl) TransitionInstanceImpl(org.camunda.bpm.engine.impl.persistence.entity.TransitionInstanceImpl) ExecutionEntity(org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity) ActivityInstanceImpl(org.camunda.bpm.engine.impl.persistence.entity.ActivityInstanceImpl) ScopeImpl(org.camunda.bpm.engine.impl.pvm.process.ScopeImpl) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with TransitionInstanceImpl

use of org.camunda.bpm.engine.impl.persistence.entity.TransitionInstanceImpl in project camunda-bpm-platform by camunda.

the class GetActivityInstanceCmd method populateChildInstances.

protected void populateChildInstances(Map<String, ActivityInstanceImpl> activityInstances, Map<String, TransitionInstanceImpl> transitionInstances) {
    Map<ActivityInstanceImpl, List<ActivityInstanceImpl>> childActivityInstances = new HashMap<ActivityInstanceImpl, List<ActivityInstanceImpl>>();
    Map<ActivityInstanceImpl, List<TransitionInstanceImpl>> childTransitionInstances = new HashMap<ActivityInstanceImpl, List<TransitionInstanceImpl>>();
    for (ActivityInstanceImpl instance : activityInstances.values()) {
        if (instance.getParentActivityInstanceId() != null) {
            ActivityInstanceImpl parentInstance = activityInstances.get(instance.getParentActivityInstanceId());
            if (parentInstance == null) {
                throw new ProcessEngineException("No parent activity instance with id " + instance.getParentActivityInstanceId() + " generated");
            }
            putListElement(childActivityInstances, parentInstance, instance);
        }
    }
    for (TransitionInstanceImpl instance : transitionInstances.values()) {
        if (instance.getParentActivityInstanceId() != null) {
            ActivityInstanceImpl parentInstance = activityInstances.get(instance.getParentActivityInstanceId());
            if (parentInstance == null) {
                throw new ProcessEngineException("No parent activity instance with id " + instance.getParentActivityInstanceId() + " generated");
            }
            putListElement(childTransitionInstances, parentInstance, instance);
        }
    }
    for (Map.Entry<ActivityInstanceImpl, List<ActivityInstanceImpl>> entry : childActivityInstances.entrySet()) {
        ActivityInstanceImpl instance = entry.getKey();
        List<ActivityInstanceImpl> childInstances = entry.getValue();
        if (childInstances != null) {
            instance.setChildActivityInstances(childInstances.toArray(new ActivityInstanceImpl[childInstances.size()]));
        }
    }
    for (Map.Entry<ActivityInstanceImpl, List<TransitionInstanceImpl>> entry : childTransitionInstances.entrySet()) {
        ActivityInstanceImpl instance = entry.getKey();
        List<TransitionInstanceImpl> childInstances = entry.getValue();
        if (childTransitionInstances != null) {
            instance.setChildTransitionInstances(childInstances.toArray(new TransitionInstanceImpl[childInstances.size()]));
        }
    }
}
Also used : TransitionInstanceImpl(org.camunda.bpm.engine.impl.persistence.entity.TransitionInstanceImpl) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) ActivityInstanceImpl(org.camunda.bpm.engine.impl.persistence.entity.ActivityInstanceImpl) HashMap(java.util.HashMap) Map(java.util.Map) ProcessEngineException(org.camunda.bpm.engine.ProcessEngineException)

Aggregations

TransitionInstanceImpl (org.camunda.bpm.engine.impl.persistence.entity.TransitionInstanceImpl)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ActivityInstanceImpl (org.camunda.bpm.engine.impl.persistence.entity.ActivityInstanceImpl)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ProcessEngineException (org.camunda.bpm.engine.ProcessEngineException)1 ExecutionEntity (org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity)1 ActivityImpl (org.camunda.bpm.engine.impl.pvm.process.ActivityImpl)1 ScopeImpl (org.camunda.bpm.engine.impl.pvm.process.ScopeImpl)1 PvmExecutionImpl (org.camunda.bpm.engine.impl.pvm.runtime.PvmExecutionImpl)1