Search in sources :

Example 1 with ProcessInstanceHelper

use of org.activiti.engine.impl.util.ProcessInstanceHelper in project Activiti by Activiti.

the class StartCreatedProcessInstanceCmd method execute.

@Override
public ProcessInstance execute(CommandContext commandContext) {
    if (this.internalProcessInstance.getStartTime() != null) {
        throw new ActivitiIllegalArgumentException("Process instance " + this.internalProcessInstance.getProcessInstanceId() + " has already been started");
    }
    ExecutionEntity processExecution = (ExecutionEntity) internalProcessInstance;
    ProcessInstanceHelper processInstanceHelper = commandContext.getProcessEngineConfiguration().getProcessInstanceHelper();
    Process process = ProcessDefinitionUtil.getProcess(internalProcessInstance.getProcessDefinitionId());
    processInstanceHelper.startProcessInstance(processExecution, commandContext, variables, process.getInitialFlowElement(), Collections.emptyMap());
    return processExecution;
}
Also used : ProcessInstanceHelper(org.activiti.engine.impl.util.ProcessInstanceHelper) ExecutionEntity(org.activiti.engine.impl.persistence.entity.ExecutionEntity) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) Process(org.activiti.bpmn.model.Process)

Example 2 with ProcessInstanceHelper

use of org.activiti.engine.impl.util.ProcessInstanceHelper in project Activiti by Activiti.

the class TimerStartEventJobHandler method execute.

public void execute(JobEntity job, String configuration, ExecutionEntity execution, CommandContext commandContext) {
    ProcessDefinitionEntity processDefinitionEntity = ProcessDefinitionUtil.getProcessDefinitionFromDatabase(// From DB -> need to get latest suspended state
    job.getProcessDefinitionId());
    if (processDefinitionEntity == null) {
        throw new ActivitiException("Could not find process definition needed for timer start event");
    }
    try {
        if (!processDefinitionEntity.isSuspended()) {
            if (commandContext.getEventDispatcher().isEnabled()) {
                commandContext.getEventDispatcher().dispatchEvent(ActivitiEventBuilder.createEntityEvent(ActivitiEventType.TIMER_FIRED, job));
            }
            // Find initial flow element matching the signal start event
            org.activiti.bpmn.model.Process process = ProcessDefinitionUtil.getProcess(job.getProcessDefinitionId());
            String activityId = TimerEventHandler.getActivityIdFromConfiguration(configuration);
            if (activityId != null) {
                FlowElement flowElement = process.getFlowElement(activityId, true);
                if (flowElement == null) {
                    throw new ActivitiException("Could not find matching FlowElement for activityId " + activityId);
                }
                ProcessInstanceHelper processInstanceHelper = commandContext.getProcessEngineConfiguration().getProcessInstanceHelper();
                processInstanceHelper.createAndStartProcessInstanceWithInitialFlowElement(processDefinitionEntity, null, null, flowElement, process, null, null, true);
            } else {
                new StartProcessInstanceCmd(processDefinitionEntity.getKey(), null, null, null, job.getTenantId()).execute(commandContext);
            }
        } else {
            log.debug("ignoring timer of suspended process definition {}", processDefinitionEntity.getName());
        }
    } catch (RuntimeException e) {
        log.error("exception during timer execution", e);
        throw e;
    } catch (Exception e) {
        log.error("exception during timer execution", e);
        throw new ActivitiException("exception during timer execution: " + e.getMessage(), e);
    }
}
Also used : ProcessInstanceHelper(org.activiti.engine.impl.util.ProcessInstanceHelper) ActivitiException(org.activiti.engine.ActivitiException) FlowElement(org.activiti.bpmn.model.FlowElement) ProcessDefinitionEntity(org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity) StartProcessInstanceCmd(org.activiti.engine.impl.cmd.StartProcessInstanceCmd) ActivitiException(org.activiti.engine.ActivitiException)

Example 3 with ProcessInstanceHelper

use of org.activiti.engine.impl.util.ProcessInstanceHelper in project Activiti by Activiti.

the class SignalEventHandler method handleEvent.

@SuppressWarnings("unchecked")
@Override
public void handleEvent(EventSubscriptionEntity eventSubscription, Object payload, CommandContext commandContext) {
    if (eventSubscription.getExecutionId() != null) {
        dispatchActivitySignalledEvent(eventSubscription.getExecution(), eventSubscription.getEventName(), payload, commandContext);
        super.handleEvent(eventSubscription, payload, commandContext);
    } else if (eventSubscription.getProcessDefinitionId() != null) {
        // Find initial flow element matching the signal start event
        String processDefinitionId = eventSubscription.getProcessDefinitionId();
        ProcessDefinition processDefinition = ProcessDefinitionUtil.getProcessDefinition(processDefinitionId);
        if (processDefinition == null) {
            throw new ActivitiObjectNotFoundException("No process definition found for id '" + processDefinitionId + "'", ProcessDefinition.class);
        }
        if (processDefinition.isSuspended()) {
            throw new ActivitiException("Could not handle signal: process definition with id: " + processDefinitionId + " is suspended");
        }
        org.activiti.bpmn.model.Process process = ProcessDefinitionUtil.getProcess(processDefinitionId);
        FlowElement flowElement = process.getFlowElement(eventSubscription.getActivityId(), true);
        if (flowElement == null) {
            throw new ActivitiException("Could not find matching FlowElement for activityId " + eventSubscription.getActivityId());
        }
        // Start process instance via that flow element
        Map<String, Object> variables = null;
        if (payload instanceof Map) {
            variables = (Map<String, Object>) payload;
        }
        ProcessInstanceHelper processInstanceHelper = commandContext.getProcessEngineConfiguration().getProcessInstanceHelper();
        ExecutionEntity executionEntity = processInstanceHelper.createProcessInstanceWithInitialFlowElement(processDefinition, null, null, flowElement, process);
        DelegateExecution execution = executionEntity.getExecutions().get(0);
        dispatchActivitySignalledEvent(execution, eventSubscription.getEventName(), payload, commandContext);
        processInstanceHelper.startProcessInstance(executionEntity, commandContext, variables, flowElement, null);
    } else {
        throw new ActivitiException("Invalid signal handling: no execution nor process definition set");
    }
}
Also used : ProcessInstanceHelper(org.activiti.engine.impl.util.ProcessInstanceHelper) ActivitiException(org.activiti.engine.ActivitiException) ExecutionEntity(org.activiti.engine.impl.persistence.entity.ExecutionEntity) FlowElement(org.activiti.bpmn.model.FlowElement) ProcessDefinition(org.activiti.engine.repository.ProcessDefinition) DelegateExecution(org.activiti.engine.delegate.DelegateExecution) ActivitiObjectNotFoundException(org.activiti.engine.ActivitiObjectNotFoundException) Map(java.util.Map)

Example 4 with ProcessInstanceHelper

use of org.activiti.engine.impl.util.ProcessInstanceHelper in project Activiti by Activiti.

the class StartProcessInstanceByMessageCmd method execute.

public ProcessInstance execute(CommandContext commandContext) {
    if (messageName == null) {
        throw new ActivitiIllegalArgumentException("Cannot start process instance by message: message name is null");
    }
    MessageEventSubscriptionEntity messageEventSubscription = commandContext.getEventSubscriptionEntityManager().findMessageStartEventSubscriptionByName(messageName, tenantId);
    if (messageEventSubscription == null) {
        throw new ActivitiObjectNotFoundException("Cannot start process instance by message: no subscription to message with name '" + messageName + "' found.", MessageEventSubscriptionEntity.class);
    }
    String processDefinitionId = messageEventSubscription.getConfiguration();
    if (processDefinitionId == null) {
        throw new ActivitiException("Cannot start process instance by message: subscription to message with name '" + messageName + "' is not a message start event.");
    }
    DeploymentManager deploymentCache = commandContext.getProcessEngineConfiguration().getDeploymentManager();
    ProcessDefinition processDefinition = deploymentCache.findDeployedProcessDefinitionById(processDefinitionId);
    if (processDefinition == null) {
        throw new ActivitiObjectNotFoundException("No process definition found for id '" + processDefinitionId + "'", ProcessDefinition.class);
    }
    ProcessInstanceHelper processInstanceHelper = commandContext.getProcessEngineConfiguration().getProcessInstanceHelper();
    ProcessInstance processInstance = processInstanceHelper.createAndStartProcessInstanceByMessage(processDefinition, businessKey, messageName, processVariables, transientVariables);
    return processInstance;
}
Also used : ProcessInstanceHelper(org.activiti.engine.impl.util.ProcessInstanceHelper) ActivitiException(org.activiti.engine.ActivitiException) DeploymentManager(org.activiti.engine.impl.persistence.deploy.DeploymentManager) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) ProcessDefinition(org.activiti.engine.repository.ProcessDefinition) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) ActivitiObjectNotFoundException(org.activiti.engine.ActivitiObjectNotFoundException) MessageEventSubscriptionEntity(org.activiti.engine.impl.persistence.entity.MessageEventSubscriptionEntity)

Aggregations

ProcessInstanceHelper (org.activiti.engine.impl.util.ProcessInstanceHelper)4 ActivitiException (org.activiti.engine.ActivitiException)3 FlowElement (org.activiti.bpmn.model.FlowElement)2 ActivitiIllegalArgumentException (org.activiti.engine.ActivitiIllegalArgumentException)2 ActivitiObjectNotFoundException (org.activiti.engine.ActivitiObjectNotFoundException)2 ExecutionEntity (org.activiti.engine.impl.persistence.entity.ExecutionEntity)2 ProcessDefinition (org.activiti.engine.repository.ProcessDefinition)2 Map (java.util.Map)1 Process (org.activiti.bpmn.model.Process)1 DelegateExecution (org.activiti.engine.delegate.DelegateExecution)1 StartProcessInstanceCmd (org.activiti.engine.impl.cmd.StartProcessInstanceCmd)1 DeploymentManager (org.activiti.engine.impl.persistence.deploy.DeploymentManager)1 MessageEventSubscriptionEntity (org.activiti.engine.impl.persistence.entity.MessageEventSubscriptionEntity)1 ProcessDefinitionEntity (org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity)1 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)1