Search in sources :

Example 26 with ExecutionEntityManager

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

the class SetProcessDefinitionVersionCmd method execute.

public Void execute(CommandContext commandContext) {
    // check that the new process definition is just another version of the same
    // process definition that the process instance is using
    ExecutionEntityManager executionManager = commandContext.getExecutionEntityManager();
    ExecutionEntity processInstance = executionManager.findById(processInstanceId);
    if (processInstance == null) {
        throw new ActivitiObjectNotFoundException("No process instance found for id = '" + processInstanceId + "'.", ProcessInstance.class);
    } else if (!processInstance.isProcessInstanceType()) {
        throw new ActivitiIllegalArgumentException("A process instance id is required, but the provided id " + "'" + processInstanceId + "' " + "points to a child execution of process instance " + "'" + processInstance.getProcessInstanceId() + "'. " + "Please invoke the " + getClass().getSimpleName() + " with a root execution id.");
    }
    DeploymentManager deploymentCache = commandContext.getProcessEngineConfiguration().getDeploymentManager();
    ProcessDefinition currentProcessDefinition = deploymentCache.findDeployedProcessDefinitionById(processInstance.getProcessDefinitionId());
    ProcessDefinition newProcessDefinition = deploymentCache.findDeployedProcessDefinitionByKeyAndVersionAndTenantId(currentProcessDefinition.getKey(), processDefinitionVersion, currentProcessDefinition.getTenantId());
    validateAndSwitchVersionOfExecution(commandContext, processInstance, newProcessDefinition);
    // switch the historic process instance to the new process definition version
    commandContext.getHistoryManager().recordProcessDefinitionChange(processInstanceId, newProcessDefinition.getId());
    // switch all sub-executions of the process instance to the new process definition version
    Collection<ExecutionEntity> childExecutions = executionManager.findChildExecutionsByProcessInstanceId(processInstanceId);
    for (ExecutionEntity executionEntity : childExecutions) {
        validateAndSwitchVersionOfExecution(commandContext, executionEntity, newProcessDefinition);
    }
    return null;
}
Also used : ExecutionEntity(org.activiti.engine.impl.persistence.entity.ExecutionEntity) DeploymentManager(org.activiti.engine.impl.persistence.deploy.DeploymentManager) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) ProcessDefinition(org.activiti.engine.repository.ProcessDefinition) ActivitiObjectNotFoundException(org.activiti.engine.ActivitiObjectNotFoundException) ExecutionEntityManager(org.activiti.engine.impl.persistence.entity.ExecutionEntityManager)

Example 27 with ExecutionEntityManager

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

the class SetProcessInstanceBusinessKeyCmd method execute.

public Void execute(CommandContext commandContext) {
    ExecutionEntityManager executionManager = commandContext.getExecutionEntityManager();
    ExecutionEntity processInstance = executionManager.findById(processInstanceId);
    if (processInstance == null) {
        throw new ActivitiObjectNotFoundException("No process instance found for id = '" + processInstanceId + "'.", ProcessInstance.class);
    } else if (!processInstance.isProcessInstanceType()) {
        throw new ActivitiIllegalArgumentException("A process instance id is required, but the provided id " + "'" + processInstanceId + "' " + "points to a child execution of process instance " + "'" + processInstance.getProcessInstanceId() + "'. " + "Please invoke the " + getClass().getSimpleName() + " with a root execution id.");
    }
    executionManager.updateProcessInstanceBusinessKey(processInstance, businessKey);
    return null;
}
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 28 with ExecutionEntityManager

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

the class CancelEndEventActivityBehavior method execute.

@Override
public void execute(DelegateExecution execution) {
    ExecutionEntity executionEntity = (ExecutionEntity) execution;
    CommandContext commandContext = Context.getCommandContext();
    ExecutionEntityManager executionEntityManager = commandContext.getExecutionEntityManager();
    // find cancel boundary event:
    ExecutionEntity parentScopeExecution = null;
    ExecutionEntity currentlyExaminedExecution = executionEntityManager.findById(executionEntity.getParentId());
    while (currentlyExaminedExecution != null && parentScopeExecution == null) {
        if (currentlyExaminedExecution.getCurrentFlowElement() instanceof SubProcess) {
            parentScopeExecution = currentlyExaminedExecution;
            SubProcess subProcess = (SubProcess) currentlyExaminedExecution.getCurrentFlowElement();
            if (subProcess.getLoopCharacteristics() != null) {
                ExecutionEntity miExecution = parentScopeExecution.getParent();
                FlowElement miElement = miExecution.getCurrentFlowElement();
                if (miElement != null && miElement.getId().equals(subProcess.getId())) {
                    parentScopeExecution = miExecution;
                }
            }
        } else {
            currentlyExaminedExecution = executionEntityManager.findById(currentlyExaminedExecution.getParentId());
        }
    }
    if (parentScopeExecution == null) {
        throw new ActivitiException("No sub process execution found for cancel end event " + executionEntity.getCurrentActivityId());
    }
    SubProcess subProcess = (SubProcess) parentScopeExecution.getCurrentFlowElement();
    BoundaryEvent cancelBoundaryEvent = null;
    if (CollectionUtil.isNotEmpty(subProcess.getBoundaryEvents())) {
        for (BoundaryEvent boundaryEvent : subProcess.getBoundaryEvents()) {
            if (CollectionUtil.isNotEmpty(boundaryEvent.getEventDefinitions()) && boundaryEvent.getEventDefinitions().get(0) instanceof CancelEventDefinition) {
                cancelBoundaryEvent = boundaryEvent;
                break;
            }
        }
    }
    if (cancelBoundaryEvent == null) {
        throw new ActivitiException("Could not find cancel boundary event for cancel end event " + executionEntity.getCurrentActivityId());
    }
    ExecutionEntity newParentScopeExecution = null;
    currentlyExaminedExecution = executionEntityManager.findById(parentScopeExecution.getParentId());
    while (currentlyExaminedExecution != null && newParentScopeExecution == null) {
        if (currentlyExaminedExecution.isScope()) {
            newParentScopeExecution = currentlyExaminedExecution;
        } else {
            currentlyExaminedExecution = executionEntityManager.findById(currentlyExaminedExecution.getParentId());
        }
    }
    if (newParentScopeExecution == null) {
        throw new ActivitiException("Programmatic error: no parent scope execution found for boundary event " + cancelBoundaryEvent.getId());
    }
    ScopeUtil.createCopyOfSubProcessExecutionForCompensation(parentScopeExecution);
    if (subProcess.getLoopCharacteristics() != null) {
        List<? extends ExecutionEntity> multiInstanceExecutions = parentScopeExecution.getExecutions();
        List<ExecutionEntity> executionsToDelete = new ArrayList<ExecutionEntity>();
        for (ExecutionEntity multiInstanceExecution : multiInstanceExecutions) {
            if (!multiInstanceExecution.getId().equals(parentScopeExecution.getId())) {
                ScopeUtil.createCopyOfSubProcessExecutionForCompensation(multiInstanceExecution);
                // end all executions in the scope of the transaction
                executionsToDelete.add(multiInstanceExecution);
                deleteChildExecutions(multiInstanceExecution, executionEntity, commandContext, DeleteReason.TRANSACTION_CANCELED);
            }
        }
        for (ExecutionEntity executionEntityToDelete : executionsToDelete) {
            deleteChildExecutions(executionEntityToDelete, executionEntity, commandContext, DeleteReason.TRANSACTION_CANCELED);
        }
    }
    // The current activity is finished (and will not be ended in the deleteChildExecutions)
    commandContext.getHistoryManager().recordActivityEnd(executionEntity, null);
    // set new parent for boundary event execution
    executionEntity.setParent(newParentScopeExecution);
    executionEntity.setCurrentFlowElement(cancelBoundaryEvent);
    // end all executions in the scope of the transaction
    deleteChildExecutions(parentScopeExecution, executionEntity, commandContext, DeleteReason.TRANSACTION_CANCELED);
    commandContext.getHistoryManager().recordActivityEnd(parentScopeExecution, DeleteReason.TRANSACTION_CANCELED);
    Context.getAgenda().planTriggerExecutionOperation(executionEntity);
}
Also used : SubProcess(org.activiti.bpmn.model.SubProcess) ActivitiException(org.activiti.engine.ActivitiException) ExecutionEntity(org.activiti.engine.impl.persistence.entity.ExecutionEntity) CommandContext(org.activiti.engine.impl.interceptor.CommandContext) BoundaryEvent(org.activiti.bpmn.model.BoundaryEvent) FlowElement(org.activiti.bpmn.model.FlowElement) ArrayList(java.util.ArrayList) CancelEventDefinition(org.activiti.bpmn.model.CancelEventDefinition) ExecutionEntityManager(org.activiti.engine.impl.persistence.entity.ExecutionEntityManager)

Example 29 with ExecutionEntityManager

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

the class EventSubProcessMessageStartEventActivityBehavior method trigger.

@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())) {
                executionEntityManager.cancelExecutionAndRelatedData(childExecution, DeleteReason.EVENT_SUBPROCESS_INTERRUPTING + "(" + startEvent.getId() + ")");
            }
        }
    }
    // Should we use triggerName and triggerData, because message name expression can change?
    String messageName = messageExecutionContext.getMessageName(execution);
    EventSubscriptionEntityManager eventSubscriptionEntityManager = Context.getCommandContext().getEventSubscriptionEntityManager();
    List<EventSubscriptionEntity> eventSubscriptions = executionEntity.getEventSubscriptions();
    for (EventSubscriptionEntity eventSubscription : eventSubscriptions) {
        if (eventSubscription instanceof MessageEventSubscriptionEntity && eventSubscription.getEventName().equals(messageName)) {
            eventSubscriptionEntityManager.delete(eventSubscription);
        }
    }
    executionEntity.setCurrentFlowElement((SubProcess) executionEntity.getCurrentFlowElement().getParentContainer());
    executionEntity.setScope(true);
    ExecutionEntity outgoingFlowExecution = executionEntityManager.createChildExecution(executionEntity);
    outgoingFlowExecution.setCurrentFlowElement(startEvent);
    leave(outgoingFlowExecution);
}
Also used : CommandContext(org.activiti.engine.impl.interceptor.CommandContext) ExecutionEntity(org.activiti.engine.impl.persistence.entity.ExecutionEntity) EventSubscriptionEntityManager(org.activiti.engine.impl.persistence.entity.EventSubscriptionEntityManager) StartEvent(org.activiti.bpmn.model.StartEvent) MessageEventSubscriptionEntity(org.activiti.engine.impl.persistence.entity.MessageEventSubscriptionEntity) ExecutionEntityManager(org.activiti.engine.impl.persistence.entity.ExecutionEntityManager) MessageEventSubscriptionEntity(org.activiti.engine.impl.persistence.entity.MessageEventSubscriptionEntity) EventSubscriptionEntity(org.activiti.engine.impl.persistence.entity.EventSubscriptionEntity)

Example 30 with ExecutionEntityManager

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

the class InclusiveGatewayActivityBehavior method executeInclusiveGatewayLogic.

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);
            }
        }
        // Leave
        commandContext.getAgenda().planTakeOutgoingSequenceFlowsOperation(execution, true);
    }
}
Also used : CommandContext(org.activiti.engine.impl.interceptor.CommandContext) ExecutionEntity(org.activiti.engine.impl.persistence.entity.ExecutionEntity) ExecutionEntityManager(org.activiti.engine.impl.persistence.entity.ExecutionEntityManager)

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