use of org.kie.internal.process.CorrelationKey in project jbpm by kiegroup.
the class CaseRuntimeDataServiceImpl method internalGetCaseStages.
/*
* Helper methods to parse process and extract case related information
*/
public List<CaseStageInstance> internalGetCaseStages(CaseDefinition caseDef, String caseId, boolean activeOnly, QueryContext queryContext) {
CorrelationKey correlationKey = correlationKeyFactory.newCorrelationKey(caseId);
Collection<org.jbpm.services.api.model.NodeInstanceDesc> nodes = runtimeDataService.getNodeInstancesByCorrelationKeyNodeType(correlationKey, Arrays.asList(ProcessInstance.STATE_ACTIVE), Arrays.asList("DynamicNode"), queryContext);
Collection<Long> completedNodes = nodes.stream().filter(n -> ((NodeInstanceDesc) n).getType() == 1).map(n -> n.getId()).collect(toList());
Map<String, CaseStage> stagesByName = caseDef.getCaseStages().stream().collect(toMap(CaseStage::getId, c -> c));
Predicate<org.jbpm.services.api.model.NodeInstanceDesc> filterNodes = null;
if (activeOnly) {
filterNodes = n -> ((NodeInstanceDesc) n).getType() == 0 && !completedNodes.contains(((NodeInstanceDesc) n).getId());
} else {
filterNodes = n -> ((NodeInstanceDesc) n).getType() == 0;
}
List<String> triggeredStages = new ArrayList<>();
List<CaseStageInstance> stages = new ArrayList<>();
nodes.stream().filter(filterNodes).map(n -> {
StageStatus status = StageStatus.Active;
if (completedNodes.contains(((NodeInstanceDesc) n).getId())) {
status = StageStatus.Completed;
}
Collection<org.jbpm.services.api.model.NodeInstanceDesc> activeNodes = getActiveNodesForCaseAndStage(caseId, n.getNodeId(), new QueryContext(0, 100));
return new CaseStageInstanceImpl(n.getNodeId(), n.getName(), stagesByName.get(n.getNodeId()).getAdHocFragments(), activeNodes, status);
}).forEach(csi -> {
stages.add(csi);
triggeredStages.add(csi.getName());
});
if (!activeOnly) {
// add other stages that are present in the definition
caseDef.getCaseStages().stream().filter(cs -> !triggeredStages.contains(cs.getName())).map(cs -> new CaseStageInstanceImpl(cs.getId(), cs.getName(), cs.getAdHocFragments(), Collections.emptyList(), StageStatus.Available)).forEach(csi -> stages.add(csi));
}
return stages;
}
use of org.kie.internal.process.CorrelationKey in project jbpm by kiegroup.
the class CaseServiceImpl method internalTriggerAdHocFragment.
protected void internalTriggerAdHocFragment(ProcessInstanceDesc pi, String fragmentName, Object data) throws CaseNotFoundException {
CorrelationKey key = CorrelationKeyXmlAdapter.unmarshalCorrelationKey(pi.getCorrelationKey());
String caseId = (String) key.getProperties().get(0).getValue();
authorizationManager.checkAuthorization(caseId);
CaseDefinition caseDef = caseRuntimeDataService.getCase(pi.getDeploymentId(), pi.getProcessId());
List<AdHocFragment> allFragments = new ArrayList<>();
if (caseDef.getAdHocFragments() != null) {
allFragments.addAll(caseDef.getAdHocFragments());
}
caseDef.getCaseStages().forEach(stage -> {
if (stage.getAdHocFragments() != null) {
allFragments.addAll(stage.getAdHocFragments());
}
});
allFragments.stream().filter(fragment -> fragment.getName().equals(fragmentName)).findFirst().orElseThrow(() -> new AdHocFragmentNotFoundException("AdHoc fragment '" + fragmentName + "' not found in case " + pi.getCorrelationKey()));
processService.signalProcessInstance(pi.getId(), fragmentName, data);
}
use of org.kie.internal.process.CorrelationKey in project jbpm by kiegroup.
the class CancelCaseCommand method execute.
@Override
public Void execute(Context context) {
CorrelationKey correlationKey = correlationKeyFactory.newCorrelationKey(caseId);
Collection<ProcessInstanceDesc> caseProcesses = runtimeDataService.getProcessInstancesByCorrelationKey(correlationKey, new QueryContext(0, 1000));
if (caseProcesses.isEmpty()) {
throw new CaseNotFoundException("Case with id " + caseId + " was not found");
}
List<Long> processInstanceIds = caseProcesses.stream().filter(pi -> pi.getState().equals(ProcessInstance.STATE_ACTIVE)).sorted((ProcessInstanceDesc o1, ProcessInstanceDesc o2) -> {
return Long.valueOf(o2.getParentId()).compareTo(Long.valueOf(o1.getParentId()));
}).map(pi -> pi.getId()).collect(toList());
CaseEventSupport caseEventSupport = getCaseEventSupport(context);
KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
CaseFileInstance caseFile = getCaseFile(ksession, caseId);
caseEventSupport.fireBeforeCaseCancelled(caseId, caseFile, processInstanceIds);
logger.debug("Case {} consists of following process instances (ids) {}", caseId, processInstanceIds);
processService.abortProcessInstances(processInstanceIds);
caseEventSupport.fireAfterCaseCancelled(caseId, caseFile, processInstanceIds);
if (destroy) {
RuntimeManager runtimeManager = getRuntimeManager(context);
if (runtimeManager instanceof PerCaseRuntimeManager) {
caseEventSupport.fireBeforeCaseDestroyed(caseId, caseFile, processInstanceIds);
logger.debug("Case {} aborted, destroying case data including per case runtime engine (including working memory)", caseId);
((PerCaseRuntimeManager) runtimeManager).destroyCase(CaseContext.get(caseId));
caseEventSupport.fireAfterCaseDestroyed(caseId, caseFile, processInstanceIds);
}
}
return null;
}
use of org.kie.internal.process.CorrelationKey in project jbpm by kiegroup.
the class ReopenCaseCommand method execute.
@Override
public Void execute(Context context) {
CaseEventSupport caseEventSupport = getCaseEventSupport(context);
KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
CaseFileInstance caseFile = getCaseFile(ksession, caseId);
caseEventSupport.fireBeforeCaseReopened(caseId, caseFile, deploymentId, caseDefinitionId, data);
logger.debug("Updating case file in working memory");
FactHandle factHandle = ksession.getFactHandle(caseFile);
((CaseFileInstanceImpl) caseFile).setCaseReopenDate(new Date());
if (data != null && !data.isEmpty()) {
caseFile.addAll(data);
}
ksession.update(factHandle, caseFile);
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);
long processInstanceId = processService.startProcess(deploymentId, caseDefinitionId, correlationKey, params);
logger.debug("Case {} successfully reopened (process instance id {})", caseId, processInstanceId);
caseEventSupport.fireAfterCaseReopened(caseId, caseFile, deploymentId, caseDefinitionId, data, processInstanceId);
return null;
}
use of org.kie.internal.process.CorrelationKey in project jbpm by kiegroup.
the class PerProcessInstanceRuntimeManagerTest method testCreationOfSessionWithPersistenceByCorrelationKey.
@Test
public void testCreationOfSessionWithPersistenceByCorrelationKey() {
RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().userGroupCallback(userGroupCallback).addAsset(ResourceFactory.newClassPathResource("BPMN2-ScriptTask.bpmn2"), ResourceType.BPMN2).addAsset(ResourceFactory.newClassPathResource("BPMN2-UserTask.bpmn2"), ResourceType.BPMN2).get();
CorrelationKeyFactory keyFactory = KieInternalServices.Factory.get().newCorrelationKeyFactory();
manager = RuntimeManagerFactory.Factory.get().newPerProcessInstanceRuntimeManager(environment);
assertNotNull(manager);
// ksession for process instance #1
// since there is no process instance yet we need to get new session
CorrelationKey key = keyFactory.newCorrelationKey("first");
RuntimeEngine runtime = manager.getRuntimeEngine(CorrelationKeyContext.get());
KieSession ksession = runtime.getKieSession();
assertNotNull(ksession);
long ksession1Id = ksession.getIdentifier();
assertTrue(ksession1Id == 2);
// ksession for process instance #2
// since there is no process instance yet we need to get new session
CorrelationKey key2 = keyFactory.newCorrelationKey("second");
RuntimeEngine runtime2 = manager.getRuntimeEngine(CorrelationKeyContext.get());
KieSession ksession2 = runtime2.getKieSession();
assertNotNull(ksession2);
long ksession2Id = ksession2.getIdentifier();
assertTrue(ksession2Id == 3);
ProcessInstance pi1 = ((CorrelationAwareProcessRuntime) ksession).startProcess("UserTask", key, null);
ProcessInstance pi2 = ((CorrelationAwareProcessRuntime) ksession2).startProcess("UserTask", key2, null);
// both processes started
assertEquals(ProcessInstance.STATE_ACTIVE, pi1.getState());
assertEquals(ProcessInstance.STATE_ACTIVE, pi2.getState());
runtime = manager.getRuntimeEngine(CorrelationKeyContext.get(key));
ksession = runtime.getKieSession();
assertEquals(ksession1Id, ksession.getIdentifier());
ksession.getWorkItemManager().completeWorkItem(1, null);
// since process is completed now session should not be there any more
try {
manager.getRuntimeEngine(CorrelationKeyContext.get(key)).getKieSession();
fail("Session for this (" + pi1.getId() + ") process instance is no more accessible");
} catch (RuntimeException e) {
}
runtime2 = manager.getRuntimeEngine(CorrelationKeyContext.get(key2));
ksession2 = runtime2.getKieSession();
assertEquals(ksession2Id, ksession2.getIdentifier());
ksession2.getWorkItemManager().completeWorkItem(2, null);
// since process is completed now session should not be there any more
try {
manager.getRuntimeEngine(CorrelationKeyContext.get(key2)).getKieSession();
fail("Session for this (" + pi2.getId() + ") process instance is no more accessible");
} catch (RuntimeException e) {
}
manager.close();
}
Aggregations