Search in sources :

Example 1 with ActivityInstanceImpl

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

the class GetActivityInstanceCmd method createActivityInstance.

protected ActivityInstanceImpl createActivityInstance(PvmExecutionImpl scopeExecution, ScopeImpl scope, String activityInstanceId, String parentActivityInstanceId) {
    ActivityInstanceImpl actInst = new ActivityInstanceImpl();
    actInst.setId(activityInstanceId);
    actInst.setParentActivityInstanceId(parentActivityInstanceId);
    actInst.setProcessInstanceId(scopeExecution.getProcessInstanceId());
    actInst.setProcessDefinitionId(scopeExecution.getProcessDefinitionId());
    actInst.setBusinessKey(scopeExecution.getBusinessKey());
    actInst.setActivityId(scope.getId());
    String name = scope.getName();
    if (name == null) {
        name = (String) scope.getProperty("name");
    }
    actInst.setActivityName(name);
    if (scope.getId().equals(scopeExecution.getProcessDefinition().getId())) {
        actInst.setActivityType("processDefinition");
    } else {
        actInst.setActivityType((String) scope.getProperty("type"));
    }
    List<String> executionIds = new ArrayList<String>();
    executionIds.add(scopeExecution.getId());
    for (PvmExecutionImpl childExecution : scopeExecution.getNonEventScopeExecutions()) {
        // add all concurrent children that are not in an activity
        if (childExecution.isConcurrent() && (childExecution.getActivityId() == null)) {
            executionIds.add(childExecution.getId());
        }
    }
    actInst.setExecutionIds(executionIds.toArray(new String[executionIds.size()]));
    return actInst;
}
Also used : PvmExecutionImpl(org.camunda.bpm.engine.impl.pvm.runtime.PvmExecutionImpl) ArrayList(java.util.ArrayList) ActivityInstanceImpl(org.camunda.bpm.engine.impl.persistence.entity.ActivityInstanceImpl)

Example 2 with ActivityInstanceImpl

use of org.camunda.bpm.engine.impl.persistence.entity.ActivityInstanceImpl 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 ActivityInstanceImpl

use of org.camunda.bpm.engine.impl.persistence.entity.ActivityInstanceImpl 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

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