Search in sources :

Example 1 with CaseContext

use of org.kie.internal.runtime.manager.context.CaseContext 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 2 with CaseContext

use of org.kie.internal.runtime.manager.context.CaseContext in project jbpm by kiegroup.

the class PerCaseRuntimeManager method getRuntimeEngine.

@SuppressWarnings("unchecked")
@Override
public RuntimeEngine getRuntimeEngine(Context<?> context) {
    if (isClosed()) {
        throw new IllegalStateException("Runtime manager " + identifier + " is already closed");
    }
    checkPermission();
    RuntimeEngine runtime = null;
    Object contextId = context.getContextId();
    if (!(context instanceof ProcessInstanceIdContext || context instanceof CaseContext)) {
        logger.warn("ProcessInstanceIdContext or CaseContext shall be used when interacting with PerCase runtime manager");
    }
    if (engineInitEager) {
        KieSession ksession = null;
        Long ksessionId = null;
        RuntimeEngine localRuntime = findLocalRuntime(contextId);
        if (localRuntime != null) {
            return localRuntime;
        }
        synchronized (this) {
            ksessionId = mapper.findMapping(context, this.identifier);
            if (ksessionId == null) {
                ksession = factory.newKieSession();
                ksessionId = ksession.getIdentifier();
                if (context instanceof CaseContext) {
                    ksession.execute(new SaveMappingCommand(mapper, context, ksessionId, getIdentifier()));
                }
            } else {
                ksession = factory.findKieSessionById(ksessionId);
            }
        }
        InternalTaskService internalTaskService = newTaskService(taskServiceFactory);
        runtime = new RuntimeEngineImpl(ksession, internalTaskService);
        ((RuntimeEngineImpl) runtime).setManager(this);
        ((RuntimeEngineImpl) runtime).setContext(context);
        configureRuntimeOnTaskService(internalTaskService, runtime);
        registerDisposeCallback(runtime, new DisposeSessionTransactionSynchronization(this, runtime), ksession.getEnvironment());
        registerItems(runtime);
        attachManager(runtime);
        ksession.addEventListener(new MaintainMappingListener(ksessionId, runtime, this.identifier, (String) contextId));
        if (context instanceof CaseContext) {
            ksession.getEnvironment().set("CaseId", context.getContextId());
        } else {
            Object contexts = mapper.findContextId(ksession.getIdentifier(), this.identifier);
            if (contexts instanceof Collection) {
                RuntimeEngine finalRuntimeEngnie = runtime;
                KieSession finalKieSession = ksession;
                ((Collection<Object>) contexts).forEach(o -> {
                    try {
                        saveLocalRuntime(null, Long.parseLong(o.toString()), finalRuntimeEngnie);
                    } catch (NumberFormatException e) {
                        saveLocalRuntime(o.toString(), null, finalRuntimeEngnie);
                        finalKieSession.getEnvironment().set("CaseId", o.toString());
                    }
                });
            }
        }
    } else {
        RuntimeEngine localRuntime = findLocalRuntime(contextId);
        if (localRuntime != null) {
            return localRuntime;
        }
        // lazy initialization of ksession and task service
        runtime = new RuntimeEngineImpl(context, new PerCaseInitializer());
        ((RuntimeEngineImpl) runtime).setManager(this);
    }
    String caseId = null;
    Long processInstanceId = null;
    if (context instanceof CaseContext) {
        caseId = (String) contextId;
    } else if (context instanceof ProcessInstanceIdContext) {
        processInstanceId = (Long) contextId;
    }
    Long ksessionId = mapper.findMapping(context, this.identifier);
    createLockOnGetEngine(ksessionId, runtime);
    saveLocalRuntime(caseId, processInstanceId, runtime);
    return runtime;
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) InternalTaskService(org.kie.internal.task.api.InternalTaskService) CaseContext(org.kie.internal.runtime.manager.context.CaseContext) ProcessInstanceIdContext(org.kie.internal.runtime.manager.context.ProcessInstanceIdContext) DisposeSessionTransactionSynchronization(org.jbpm.runtime.manager.impl.tx.DisposeSessionTransactionSynchronization) Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession)

Aggregations

KieSession (org.kie.api.runtime.KieSession)2 CaseContext (org.kie.internal.runtime.manager.context.CaseContext)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Entry (java.util.Map.Entry)1 ExecutableCommand (org.drools.core.command.impl.ExecutableCommand)1 RegistryContext (org.drools.core.command.impl.RegistryContext)1 InternalKnowledgeRuntime (org.drools.core.common.InternalKnowledgeRuntime)1 ProcessEventSupport (org.drools.core.event.ProcessEventSupport)1 CaseEventSupport (org.jbpm.casemgmt.impl.event.CaseEventSupport)1 ProcessInstance (org.jbpm.process.instance.ProcessInstance)1 DisposeSessionTransactionSynchronization (org.jbpm.runtime.manager.impl.tx.DisposeSessionTransactionSynchronization)1 BatchExecutionCommand (org.kie.api.command.BatchExecutionCommand)1 Command (org.kie.api.command.Command)1 Context (org.kie.api.runtime.Context)1 KieRuntime (org.kie.api.runtime.KieRuntime)1 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)1 CorrelationKey (org.kie.internal.process.CorrelationKey)1 ProcessInstanceIdContext (org.kie.internal.runtime.manager.context.ProcessInstanceIdContext)1