Search in sources :

Example 6 with ExecutionEntityManager

use of org.activiti.engine.impl.persistence.entity.ExecutionEntityManager in project Activiti by Activiti.

the class ScopeUtil method throwCompensationEvent.

/**
 * 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;
        // 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);
    }
}
Also used : ExecutionEntity(org.activiti.engine.impl.persistence.entity.ExecutionEntity) CompensateEventSubscriptionEntity(org.activiti.engine.impl.persistence.entity.CompensateEventSubscriptionEntity) ExecutionEntityManager(org.activiti.engine.impl.persistence.entity.ExecutionEntityManager) CompensateEventSubscriptionEntity(org.activiti.engine.impl.persistence.entity.CompensateEventSubscriptionEntity) EventSubscriptionEntity(org.activiti.engine.impl.persistence.entity.EventSubscriptionEntity)

Example 7 with ExecutionEntityManager

use of org.activiti.engine.impl.persistence.entity.ExecutionEntityManager in project Activiti by Activiti.

the class FindActiveActivityIdsCmd method execute.

public List<String> execute(CommandContext commandContext) {
    if (executionId == null) {
        throw new ActivitiIllegalArgumentException("executionId is null");
    }
    ExecutionEntityManager executionEntityManager = commandContext.getExecutionEntityManager();
    ExecutionEntity execution = executionEntityManager.findById(executionId);
    if (execution == null) {
        throw new ActivitiObjectNotFoundException("execution " + executionId + " doesn't exist", Execution.class);
    }
    return findActiveActivityIds(execution);
}
Also used : ExecutionEntity(org.activiti.engine.impl.persistence.entity.ExecutionEntity) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) ActivitiObjectNotFoundException(org.activiti.engine.ActivitiObjectNotFoundException) ExecutionEntityManager(org.activiti.engine.impl.persistence.entity.ExecutionEntityManager)

Example 8 with ExecutionEntityManager

use of org.activiti.engine.impl.persistence.entity.ExecutionEntityManager in project Activiti by Activiti.

the class DestroyScopeOperation method run.

@Override
public void run() {
    // Find the actual scope that needs to be destroyed.
    // This could be the incoming execution, or the first parent execution where isScope = true
    // Find parent scope execution
    ExecutionEntity scopeExecution = execution.isScope() ? execution : findFirstParentScopeExecution(execution);
    if (scopeExecution == null) {
        throw new ActivitiException("Programmatic error: no parent scope execution found for boundary event");
    }
    ExecutionEntityManager executionEntityManager = commandContext.getExecutionEntityManager();
    deleteAllChildExecutions(executionEntityManager, scopeExecution);
    // Delete all scope tasks
    TaskEntityManager taskEntityManager = commandContext.getTaskEntityManager();
    deleteAllScopeTasks(scopeExecution, taskEntityManager);
    // Delete all scope jobs
    TimerJobEntityManager timerJobEntityManager = commandContext.getTimerJobEntityManager();
    deleteAllScopeJobs(scopeExecution, timerJobEntityManager);
    // Remove variables associated with this scope
    VariableInstanceEntityManager variableInstanceEntityManager = commandContext.getVariableInstanceEntityManager();
    removeAllVariablesFromScope(scopeExecution, variableInstanceEntityManager);
    commandContext.getHistoryManager().recordActivityEnd(scopeExecution, scopeExecution.getDeleteReason());
    executionEntityManager.delete(scopeExecution);
}
Also used : ActivitiException(org.activiti.engine.ActivitiException) ExecutionEntity(org.activiti.engine.impl.persistence.entity.ExecutionEntity) TaskEntityManager(org.activiti.engine.impl.persistence.entity.TaskEntityManager) ExecutionEntityManager(org.activiti.engine.impl.persistence.entity.ExecutionEntityManager) TimerJobEntityManager(org.activiti.engine.impl.persistence.entity.TimerJobEntityManager) VariableInstanceEntityManager(org.activiti.engine.impl.persistence.entity.VariableInstanceEntityManager)

Example 9 with ExecutionEntityManager

use of org.activiti.engine.impl.persistence.entity.ExecutionEntityManager in project Activiti by Activiti.

the class EndExecutionOperation method handleProcessInstanceExecution.

protected void handleProcessInstanceExecution(ExecutionEntity processInstanceExecution) {
    ExecutionEntityManager executionEntityManager = commandContext.getExecutionEntityManager();
    // No parent execution == process instance id
    String processInstanceId = processInstanceExecution.getId();
    logger.debug("No parent execution found. Verifying if process instance {} can be stopped.", processInstanceId);
    ExecutionEntity superExecution = processInstanceExecution.getSuperExecution();
    SubProcessActivityBehavior subProcessActivityBehavior = null;
    // copy variables before destroying the ended sub process instance (call activity)
    if (superExecution != null) {
        FlowNode superExecutionElement = (FlowNode) superExecution.getCurrentFlowElement();
        subProcessActivityBehavior = (SubProcessActivityBehavior) superExecutionElement.getBehavior();
        try {
            subProcessActivityBehavior.completing(superExecution, processInstanceExecution);
        } catch (RuntimeException e) {
            logger.error("Error while completing sub process of execution {}", processInstanceExecution, e);
            throw e;
        } catch (Exception e) {
            logger.error("Error while completing sub process of execution {}", processInstanceExecution, e);
            throw new ActivitiException("Error while completing sub process of execution " + processInstanceExecution, e);
        }
    }
    int activeExecutions = getNumberOfActiveChildExecutionsForProcessInstance(executionEntityManager, processInstanceId);
    if (activeExecutions == 0) {
        logger.debug("No active executions found. Ending process instance {} ", processInstanceId);
        // note the use of execution here vs processinstance execution for getting the flowelement
        executionEntityManager.deleteProcessInstanceExecutionEntity(processInstanceId, execution.getCurrentFlowElement() != null ? execution.getCurrentFlowElement().getId() : null, null, false, false);
    } else {
        logger.debug("Active executions found. Process instance {} will not be ended.", processInstanceId);
    }
    Process process = ProcessDefinitionUtil.getProcess(processInstanceExecution.getProcessDefinitionId());
    // Execute execution listeners for process end.
    if (CollectionUtil.isNotEmpty(process.getExecutionListeners())) {
        executeExecutionListeners(process, processInstanceExecution, ExecutionListener.EVENTNAME_END);
    }
    // and trigger execution afterwards if doing a call activity
    if (superExecution != null) {
        superExecution.setSubProcessInstance(null);
        try {
            subProcessActivityBehavior.completed(superExecution);
        } catch (RuntimeException e) {
            logger.error("Error while completing sub process of execution {}", processInstanceExecution, e);
            throw e;
        } catch (Exception e) {
            logger.error("Error while completing sub process of execution {}", processInstanceExecution, e);
            throw new ActivitiException("Error while completing sub process of execution " + processInstanceExecution, e);
        }
    }
}
Also used : ActivitiException(org.activiti.engine.ActivitiException) ExecutionEntity(org.activiti.engine.impl.persistence.entity.ExecutionEntity) Process(org.activiti.bpmn.model.Process) SubProcess(org.activiti.bpmn.model.SubProcess) ExecutionEntityManager(org.activiti.engine.impl.persistence.entity.ExecutionEntityManager) ActivitiException(org.activiti.engine.ActivitiException) SubProcessActivityBehavior(org.activiti.engine.impl.delegate.SubProcessActivityBehavior) FlowNode(org.activiti.bpmn.model.FlowNode)

Example 10 with ExecutionEntityManager

use of org.activiti.engine.impl.persistence.entity.ExecutionEntityManager in project Activiti by Activiti.

the class CallActivityBehavior method execute.

public void execute(DelegateExecution execution) {
    String finalProcessDefinitonKey = null;
    if (processDefinitionExpression != null) {
        finalProcessDefinitonKey = (String) processDefinitionExpression.getValue(execution);
    } else {
        finalProcessDefinitonKey = processDefinitonKey;
    }
    ProcessDefinition processDefinition = findProcessDefinition(finalProcessDefinitonKey, execution.getTenantId());
    // Get model from cache
    Process subProcess = ProcessDefinitionUtil.getProcess(processDefinition.getId());
    if (subProcess == null) {
        throw new ActivitiException("Cannot start a sub process instance. Process model " + processDefinition.getName() + " (id = " + processDefinition.getId() + ") could not be found");
    }
    FlowElement initialFlowElement = subProcess.getInitialFlowElement();
    if (initialFlowElement == null) {
        throw new ActivitiException("No start element found for process definition " + processDefinition.getId());
    }
    // Do not start a process instance if the process definition is suspended
    if (ProcessDefinitionUtil.isProcessDefinitionSuspended(processDefinition.getId())) {
        throw new ActivitiException("Cannot start process instance. Process definition " + processDefinition.getName() + " (id = " + processDefinition.getId() + ") is suspended");
    }
    ProcessEngineConfigurationImpl processEngineConfiguration = Context.getProcessEngineConfiguration();
    ExecutionEntityManager executionEntityManager = Context.getCommandContext().getExecutionEntityManager();
    ExpressionManager expressionManager = processEngineConfiguration.getExpressionManager();
    ExecutionEntity executionEntity = (ExecutionEntity) execution;
    CallActivity callActivity = (CallActivity) executionEntity.getCurrentFlowElement();
    String businessKey = null;
    if (!StringUtils.isEmpty(callActivity.getBusinessKey())) {
        Expression expression = expressionManager.createExpression(callActivity.getBusinessKey());
        businessKey = expression.getValue(execution).toString();
    } else if (callActivity.isInheritBusinessKey()) {
        ExecutionEntity processInstance = executionEntityManager.findById(execution.getProcessInstanceId());
        businessKey = processInstance.getBusinessKey();
    }
    ExecutionEntity subProcessInstance = Context.getCommandContext().getExecutionEntityManager().createSubprocessInstance(processDefinition, executionEntity, businessKey);
    Context.getCommandContext().getHistoryManager().recordSubProcessInstanceStart(executionEntity, subProcessInstance, initialFlowElement);
    // process template-defined data objects
    Map<String, Object> variables = processDataObjects(subProcess.getDataObjects());
    if (callActivity.isInheritVariables()) {
        Map<String, Object> executionVariables = execution.getVariables();
        for (Map.Entry<String, Object> entry : executionVariables.entrySet()) {
            variables.put(entry.getKey(), entry.getValue());
        }
    }
    Map<String, Object> variablesFromExtensionFile = calculateInboundVariables(execution, processDefinition);
    variables.putAll(variablesFromExtensionFile);
    if (!variables.isEmpty()) {
        initializeVariables(subProcessInstance, variables);
    }
    // Create the first execution that will visit all the process definition elements
    ExecutionEntity subProcessInitialExecution = executionEntityManager.createChildExecution(subProcessInstance);
    subProcessInitialExecution.setCurrentFlowElement(initialFlowElement);
    Context.getAgenda().planContinueProcessOperation(subProcessInitialExecution);
    Context.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(ActivitiEventBuilder.createProcessStartedEvent(subProcessInitialExecution, variables, false));
}
Also used : ExpressionManager(org.activiti.engine.impl.el.ExpressionManager) ActivitiException(org.activiti.engine.ActivitiException) ProcessDefinition(org.activiti.engine.repository.ProcessDefinition) Process(org.activiti.bpmn.model.Process) CallActivity(org.activiti.bpmn.model.CallActivity) ExecutionEntityManager(org.activiti.engine.impl.persistence.entity.ExecutionEntityManager) ExecutionEntity(org.activiti.engine.impl.persistence.entity.ExecutionEntity) Expression(org.activiti.engine.delegate.Expression) FlowElement(org.activiti.bpmn.model.FlowElement) ValuedDataObject(org.activiti.bpmn.model.ValuedDataObject) ProcessEngineConfigurationImpl(org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ExecutionEntityManager (org.activiti.engine.impl.persistence.entity.ExecutionEntityManager)30 ExecutionEntity (org.activiti.engine.impl.persistence.entity.ExecutionEntity)29 ActivitiException (org.activiti.engine.ActivitiException)11 CommandContext (org.activiti.engine.impl.interceptor.CommandContext)6 SubProcess (org.activiti.bpmn.model.SubProcess)5 ActivitiObjectNotFoundException (org.activiti.engine.ActivitiObjectNotFoundException)5 FlowElement (org.activiti.bpmn.model.FlowElement)4 ActivitiIllegalArgumentException (org.activiti.engine.ActivitiIllegalArgumentException)4 DelegateExecution (org.activiti.engine.delegate.DelegateExecution)4 ArrayList (java.util.ArrayList)3 Activity (org.activiti.bpmn.model.Activity)3 BoundaryEvent (org.activiti.bpmn.model.BoundaryEvent)3 CallActivity (org.activiti.bpmn.model.CallActivity)3 Process (org.activiti.bpmn.model.Process)2 SequenceFlow (org.activiti.bpmn.model.SequenceFlow)2 CompensateEventSubscriptionEntity (org.activiti.engine.impl.persistence.entity.CompensateEventSubscriptionEntity)2 EventSubscriptionEntity (org.activiti.engine.impl.persistence.entity.EventSubscriptionEntity)2 EventSubscriptionEntityManager (org.activiti.engine.impl.persistence.entity.EventSubscriptionEntityManager)2 ProcessDefinition (org.activiti.engine.repository.ProcessDefinition)2 HashMap (java.util.HashMap)1