use of org.kie.internal.process.CorrelationKey in project jbpm by kiegroup.
the class ProcessServiceEJBIntegrationTest method testStartProcessWithParmsWithCorrelationKey.
@Test
public void testStartProcessWithParmsWithCorrelationKey() {
assertNotNull(deploymentService);
KModuleDeploymentUnit deploymentUnit = new KModuleDeploymentUnit(GROUP_ID, ARTIFACT_ID, VERSION);
deploymentService.deploy(deploymentUnit);
units.add(deploymentUnit);
assertNotNull(processService);
Map<String, Object> params = new HashMap<String, Object>();
params.put("id", "test");
CorrelationKey key = KieInternalServices.Factory.get().newCorrelationKeyFactory().newCorrelationKey("my business key");
long processInstanceId = processService.startProcess(deploymentUnit.getIdentifier(), "customtask", key, params);
assertNotNull(processInstanceId);
ProcessInstance pi = processService.getProcessInstance(key);
assertNull(pi);
}
use of org.kie.internal.process.CorrelationKey in project jbpm by kiegroup.
the class CaseServiceImplTest method testStartEmptyCaseViaProcessService.
@Test
public void testStartEmptyCaseViaProcessService() {
String caseId = FIRST_CASE_ID;
CorrelationKey correlationKey = KieInternalServices.Factory.get().newCorrelationKeyFactory().newCorrelationKey(caseId);
Map<String, Object> params = new HashMap<>();
params.put("name", "my case via process service");
Long processInstanceId = processService.startProcess(deploymentUnit.getIdentifier(), EMPTY_CASE_P_ID, correlationKey, params);
assertNotNull(processInstanceId);
try {
CaseInstance cInstance = caseService.getCaseInstance(caseId);
assertNotNull(cInstance);
assertEquals(deploymentUnit.getIdentifier(), cInstance.getDeploymentId());
assertEquals(caseId, cInstance.getCaseId());
caseService.cancelCase(caseId);
CaseInstance instance = caseService.getCaseInstance(caseId);
Assertions.assertThat(instance.getStatus()).isEqualTo(CaseStatus.CANCELLED.getId());
caseId = null;
} catch (Exception e) {
logger.error("Unexpected error {}", e.getMessage(), e);
fail("Unexpected exception " + e.getMessage());
} finally {
if (caseId != null) {
caseService.cancelCase(caseId);
}
}
}
use of org.kie.internal.process.CorrelationKey in project jbpm by kiegroup.
the class CaseRuntimeDataServiceImpl method getCaseInstanceMilestones.
@Override
public Collection<CaseMilestoneInstance> getCaseInstanceMilestones(String caseId, boolean achievedOnly, QueryContext queryContext) {
ProcessInstanceDesc pi = runtimeDataService.getProcessInstanceByCorrelationKey(correlationKeyFactory.newCorrelationKey(caseId));
if (pi == null || !pi.getState().equals(ProcessInstance.STATE_ACTIVE)) {
throw new CaseNotFoundException("No case instance found with id " + caseId + " or it's not active anymore");
}
CorrelationKey correlationKey = correlationKeyFactory.newCorrelationKey(caseId);
Collection<org.jbpm.services.api.model.NodeInstanceDesc> nodes = runtimeDataService.getNodeInstancesByCorrelationKeyNodeType(correlationKey, Arrays.asList(ProcessInstance.STATE_ACTIVE), Arrays.asList("MilestoneNode"), queryContext);
Collection<Long> completedNodes = nodes.stream().filter(n -> ((NodeInstanceDesc) n).getType() == 1).map(n -> n.getId()).collect(toList());
Predicate<org.jbpm.services.api.model.NodeInstanceDesc> filterNodes = null;
if (achievedOnly) {
filterNodes = n -> ((NodeInstanceDesc) n).getType() == 1;
} else {
filterNodes = n -> ((NodeInstanceDesc) n).getType() == 0;
}
List<String> foundMilestones = new ArrayList<>();
List<CaseMilestoneInstance> milestones = nodes.stream().filter(filterNodes).map(n -> {
foundMilestones.add(n.getName());
return new CaseMilestoneInstanceImpl(String.valueOf(n.getId()), n.getName(), completedNodes.contains(n.getId()), n.getDataTimeStamp());
}).collect(toList());
if (!achievedOnly) {
// add other milestones that are present in the definition
CaseDefinition caseDef = getCase(pi.getDeploymentId(), pi.getProcessId());
caseDef.getCaseMilestones().stream().filter(cm -> !foundMilestones.contains(cm.getName())).map(cm -> new CaseMilestoneInstanceImpl(cm.getId(), cm.getName(), false, null)).forEach(cmi -> milestones.add(cmi));
}
return applyPagination(milestones, queryContext);
}
use of org.kie.internal.process.CorrelationKey in project jbpm by kiegroup.
the class CloseCaseCommand 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");
}
final 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.fireBeforeCaseClosed(caseId, caseFile, comment);
logger.debug("Process instances {} that will be completed as part of the close of the case {}", processInstanceIds, caseId);
processService.execute(deploymentId, CaseContext.get(caseId), new ExecutableCommand<Void>() {
private static final long serialVersionUID = 1L;
@Override
public Void execute(Context context) {
KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
for (Long processInstanceId : processInstanceIds) {
WorkflowProcessInstance processInstance = (WorkflowProcessInstance) ksession.getProcessInstance(processInstanceId);
processInstance.setState(ProcessInstance.STATE_COMPLETED, comment);
logger.debug("Process instance {} set to state completed", processInstanceId);
}
return null;
}
});
caseEventSupport.fireAfterCaseClosed(caseId, caseFile, comment);
return null;
}
use of org.kie.internal.process.CorrelationKey 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;
}
Aggregations