Search in sources :

Example 11 with CaseEventSupport

use of org.jbpm.casemgmt.impl.event.CaseEventSupport in project jbpm by kiegroup.

the class StartCaseCommand method execute.

@SuppressWarnings("unchecked")
@Override
public Void execute(Context context) {
    CaseEventSupport caseEventSupport = getCaseEventSupport(context);
    caseEventSupport.fireBeforeCaseStarted(caseId, deploymentId, caseDefinitionId, caseFile);
    logger.debug("Inserting case file into working memory");
    List<Command<?>> commands = new ArrayList<>();
    commands.add(commandsFactory.newInsert(caseFile));
    commands.add(commandsFactory.newFireAllRules());
    BatchExecutionCommand batch = commandsFactory.newBatchExecution(commands);
    processService.execute(deploymentId, CaseContext.get(caseId), batch);
    logger.debug("Starting process instance for case {} and case definition {}", caseId, caseDefinitionId);
    CorrelationKey correlationKey = correlationKeyFactory.newCorrelationKey(caseId);
    Map<String, Object> params = new HashMap<>();
    // set case id to allow it to use CaseContext when creating runtime engine
    params.put(EnvironmentName.CASE_ID, caseId);
    final long processInstanceId = processService.startProcess(deploymentId, caseDefinitionId, correlationKey, params);
    logger.debug("Case {} successfully started (process instance id {})", caseId, processInstanceId);
    final Map<String, Object> caseData = caseFile.getData();
    if (caseData != null && !caseData.isEmpty()) {
        processService.execute(deploymentId, CaseContext.get(caseId), new ExecutableCommand<Void>() {

            private static final long serialVersionUID = -7093369406457484236L;

            @Override
            public Void execute(Context context) {
                KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
                ProcessInstance pi = (ProcessInstance) ksession.getProcessInstance(processInstanceId);
                if (pi != null) {
                    ProcessEventSupport processEventSupport = ((InternalProcessRuntime) ((InternalKnowledgeRuntime) ksession).getProcessRuntime()).getProcessEventSupport();
                    for (Entry<String, Object> entry : caseData.entrySet()) {
                        String name = "caseFile_" + entry.getKey();
                        processEventSupport.fireAfterVariableChanged(name, name, null, entry.getValue(), pi, (KieRuntime) ksession);
                    }
                }
                return null;
            }
        });
    }
    caseEventSupport.fireAfterCaseStarted(caseId, deploymentId, caseDefinitionId, caseFile, processInstanceId);
    return null;
}
Also used : CaseContext(org.kie.internal.runtime.manager.context.CaseContext) RegistryContext(org.drools.core.command.impl.RegistryContext) Context(org.kie.api.runtime.Context) CaseEventSupport(org.jbpm.casemgmt.impl.event.CaseEventSupport) HashMap(java.util.HashMap) KieRuntime(org.kie.api.runtime.KieRuntime) ArrayList(java.util.ArrayList) ProcessEventSupport(org.drools.core.event.ProcessEventSupport) InternalKnowledgeRuntime(org.drools.core.common.InternalKnowledgeRuntime) Entry(java.util.Map.Entry) ExecutableCommand(org.drools.core.command.impl.ExecutableCommand) Command(org.kie.api.command.Command) BatchExecutionCommand(org.kie.api.command.BatchExecutionCommand) CorrelationKey(org.kie.internal.process.CorrelationKey) BatchExecutionCommand(org.kie.api.command.BatchExecutionCommand) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.jbpm.process.instance.ProcessInstance)

Example 12 with CaseEventSupport

use of org.jbpm.casemgmt.impl.event.CaseEventSupport in project jbpm by kiegroup.

the class AddDynamicProcessCommand method execute.

@Override
public Long execute(Context context) {
    KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
    ProcessInstance processInstance = ksession.getProcessInstance(processInstanceId);
    if (processInstance == null) {
        throw new ProcessInstanceNotFoundException("No process instance found with id " + processInstanceId);
    }
    try {
        CaseFileInstance caseFile = getCaseFile(ksession, caseId);
        FactHandle factHandle = ksession.getFactHandle(caseFile);
        CaseEventSupport caseEventSupport = getCaseEventSupport(context);
        caseEventSupport.fireBeforeDynamicProcessAdded(caseId, caseFile, processInstanceId, processId, parameters);
        long subProcessInstanceId = DynamicUtils.addDynamicSubProcess(processInstance, ksession, processId, parameters);
        ksession.update(factHandle, caseFile);
        triggerRules(ksession);
        caseEventSupport.fireAfterDynamicProcessAdded(caseId, caseFile, processInstanceId, processId, parameters, subProcessInstanceId);
        return subProcessInstanceId;
    } catch (IllegalArgumentException e) {
        throw new ProcessDefinitionNotFoundException(e.getMessage());
    }
}
Also used : CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) CaseEventSupport(org.jbpm.casemgmt.impl.event.CaseEventSupport) FactHandle(org.kie.api.runtime.rule.FactHandle) KieSession(org.kie.api.runtime.KieSession) RegistryContext(org.drools.core.command.impl.RegistryContext) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) ProcessDefinitionNotFoundException(org.jbpm.services.api.ProcessDefinitionNotFoundException) ProcessInstanceNotFoundException(org.jbpm.services.api.ProcessInstanceNotFoundException)

Example 13 with CaseEventSupport

use of org.jbpm.casemgmt.impl.event.CaseEventSupport in project jbpm by kiegroup.

the class AddDynamicProcessToStageCommand method execute.

@Override
public Long execute(Context context) {
    KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
    ProcessInstance processInstance = ksession.getProcessInstance(processInstanceId);
    if (processInstance == null) {
        throw new ProcessInstanceNotFoundException("No process instance found with id " + processInstanceId);
    }
    DynamicNodeInstance dynamicContext = (DynamicNodeInstance) ((WorkflowProcessInstanceImpl) processInstance).getNodeInstances(true).stream().filter(ni -> (ni instanceof DynamicNodeInstance) && stageId.equals(ni.getNode().getMetaData().get("UniqueId"))).findFirst().orElse(null);
    if (dynamicContext == null) {
        throw new StageNotFoundException("No stage found with id " + stageId);
    }
    try {
        CaseFileInstance caseFile = getCaseFile(ksession, caseId);
        CaseEventSupport caseEventSupport = getCaseEventSupport(context);
        caseEventSupport.fireBeforeDynamicProcessAdded(caseId, caseFile, processInstanceId, processId, parameters);
        long subProcessInstanceId = DynamicUtils.addDynamicSubProcess(dynamicContext, ksession, processId, parameters);
        if (subProcessInstanceId < 0) {
            throw new ProcessDefinitionNotFoundException("No process definition found with id: " + processId);
        }
        caseEventSupport.fireAfterDynamicProcessAdded(caseId, caseFile, processInstanceId, processId, parameters, subProcessInstanceId);
        return subProcessInstanceId;
    } catch (IllegalArgumentException e) {
        throw new ProcessDefinitionNotFoundException(e.getMessage());
    }
}
Also used : IdentityProvider(org.kie.internal.identity.IdentityProvider) RegistryContext(org.drools.core.command.impl.RegistryContext) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) ProcessDefinitionNotFoundException(org.jbpm.services.api.ProcessDefinitionNotFoundException) ProcessInstanceNotFoundException(org.jbpm.services.api.ProcessInstanceNotFoundException) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) WorkflowProcessInstanceImpl(org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl) Context(org.kie.api.runtime.Context) CaseEventSupport(org.jbpm.casemgmt.impl.event.CaseEventSupport) Map(java.util.Map) DynamicNodeInstance(org.jbpm.workflow.instance.node.DynamicNodeInstance) KieSession(org.kie.api.runtime.KieSession) StageNotFoundException(org.jbpm.casemgmt.api.StageNotFoundException) DynamicUtils(org.jbpm.workflow.instance.node.DynamicUtils) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) CaseEventSupport(org.jbpm.casemgmt.impl.event.CaseEventSupport) StageNotFoundException(org.jbpm.casemgmt.api.StageNotFoundException) DynamicNodeInstance(org.jbpm.workflow.instance.node.DynamicNodeInstance) KieSession(org.kie.api.runtime.KieSession) RegistryContext(org.drools.core.command.impl.RegistryContext) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) ProcessDefinitionNotFoundException(org.jbpm.services.api.ProcessDefinitionNotFoundException) ProcessInstanceNotFoundException(org.jbpm.services.api.ProcessInstanceNotFoundException)

Aggregations

CaseEventSupport (org.jbpm.casemgmt.impl.event.CaseEventSupport)13 RegistryContext (org.drools.core.command.impl.RegistryContext)12 KieSession (org.kie.api.runtime.KieSession)12 CaseFileInstance (org.jbpm.casemgmt.api.model.instance.CaseFileInstance)11 Context (org.kie.api.runtime.Context)6 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)6 FactHandle (org.kie.api.runtime.rule.FactHandle)6 IdentityProvider (org.kie.internal.identity.IdentityProvider)5 ClassObjectFilter (org.drools.core.ClassObjectFilter)4 CorrelationKey (org.kie.internal.process.CorrelationKey)4 Collection (java.util.Collection)3 HashMap (java.util.HashMap)3 List (java.util.List)3 CaseFileInstanceImpl (org.jbpm.casemgmt.impl.model.instance.CaseFileInstanceImpl)3 ProcessInstanceNotFoundException (org.jbpm.services.api.ProcessInstanceNotFoundException)3 CaseContext (org.kie.internal.runtime.manager.context.CaseContext)3 Map (java.util.Map)2 Collectors.toList (java.util.stream.Collectors.toList)2 ExecutableCommand (org.drools.core.command.impl.ExecutableCommand)2 CaseNotFoundException (org.jbpm.casemgmt.api.CaseNotFoundException)2