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;
}
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;
}
Aggregations