use of org.kie.api.runtime.query.QueryContext in project jbpm by kiegroup.
the class CaseDynamicNodesTest method testAddDynamicTaskToNotExistingStageByProcessInstanceId.
@Test
public void testAddDynamicTaskToNotExistingStageByProcessInstanceId() {
String caseId = caseService.startCase(deploymentUnit.getIdentifier(), EMPTY_CASE_P_ID);
assertThat(caseId).isNotNull().isEqualTo(FIRST_CASE_ID);
Collection<ProcessInstanceDesc> caseProcessInstances = caseRuntimeDataService.getProcessInstancesForCase(caseId, new QueryContext());
assertThat(caseProcessInstances).isNotNull().hasSize(1);
ProcessInstanceDesc processInstance = caseProcessInstances.iterator().next();
assertThat(processInstance).isNotNull();
assertThat(processInstance.getCorrelationKey()).isEqualTo(FIRST_CASE_ID);
assertThatThrownBy(() -> caseService.addDynamicTaskToStage(processInstance.getId(), NOT_EXISTING_STAGE, createUserTask())).isInstanceOf(StageNotFoundException.class);
}
use of org.kie.api.runtime.query.QueryContext in project jbpm by kiegroup.
the class SubCaseServiceImplTest method testStartCaseWithIndependentDynamicSubCase.
private void testStartCaseWithIndependentDynamicSubCase(String idependent, Consumer<String> finishCase) {
Map<String, Object> data = new HashMap<>();
data.put("name", "John Doe");
CaseFileInstance caseFile = caseService.newCaseFileInstance(deploymentUnit.getIdentifier(), EMPTY_CASE_P_ID, data);
String caseId = caseService.startCase(deploymentUnit.getIdentifier(), EMPTY_CASE_P_ID, caseFile);
assertNotNull(caseId);
assertEquals(FIRST_CASE_ID, caseId);
try {
CaseInstance cInstance = caseService.getCaseInstance(caseId);
assertNotNull(cInstance);
assertEquals(deploymentUnit.getIdentifier(), cInstance.getDeploymentId());
Map<String, Object> parameters = new HashMap<>();
parameters.put("CaseDefinitionId", "UserTaskCase");
parameters.put("DeploymentId", deploymentUnit.getIdentifier());
parameters.put("UserRole_owner", "john");
parameters.put("Data_s", "#{name}");
parameters.put("Independent", idependent);
TaskSpecification taskSpecification = caseService.newTaskSpec("StartCaseInstance", "Sub Case", parameters);
caseService.addDynamicTask(caseId, taskSpecification);
Collection<CaseInstance> caseInstances = caseRuntimeDataService.getCaseInstances(new QueryContext());
assertNotNull(caseInstances);
assertEquals(2, caseInstances.size());
Map<String, CaseInstance> byCaseId = caseInstances.stream().collect(toMap(CaseInstance::getCaseId, c -> c));
assertTrue(byCaseId.containsKey(FIRST_CASE_ID));
assertTrue(byCaseId.containsKey(HR_CASE_ID));
List<TaskSummary> tasks = caseRuntimeDataService.getCaseTasksAssignedAsPotentialOwner(HR_CASE_ID, "john", null, new QueryContext());
assertNotNull(tasks);
assertEquals(1, tasks.size());
assertEquals("Hello1", tasks.get(0).getName());
CaseFileInstance mainCaseFile = caseService.getCaseFileInstance(caseId);
assertNotNull(mainCaseFile);
assertEquals(HR_CASE_ID, mainCaseFile.getData("CaseId"));
assertEquals("John Doe", mainCaseFile.getData("s"));
finishCase.accept(HR_CASE_ID);
mainCaseFile = caseService.getCaseFileInstance(caseId);
assertNotNull(mainCaseFile);
assertEquals(HR_CASE_ID, mainCaseFile.getData("CaseId"));
assertEquals("John Doe", mainCaseFile.getData("s"));
} 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.api.runtime.query.QueryContext 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.api.runtime.query.QueryContext in project jbpm by kiegroup.
the class QueryRunCommand method execute.
public ExecutionResults execute(CommandContext ctx) {
BeanManager manager = CDIUtils.lookUpBeanManager(ctx);
String clazz = QueryService.class.getName();
try {
QueryService cdiBean = (QueryService) CDIUtils.createBean(Class.forName(clazz), manager);
logger.info("CDI bean created {}", cdiBean);
String mapperClass = (String) ctx.getData("mapper");
if (mapperClass == null) {
mapperClass = "org.jbpm.kie.services.impl.query.mapper.ProcessInstanceQueryMapper";
}
Method m = Class.forName(mapperClass).getMethod("get", new Class[0]);
QueryResultMapper<?> mapper = (QueryResultMapper<?>) m.invoke(null, new Object[0]);
Object queryR = cdiBean.query((String) ctx.getData("query"), mapper, new QueryContext());
logger.info("Result of the query is " + queryR);
} catch (Exception e) {
logger.error("Error while creating CDI bean from jbpm executor", e);
}
logger.info("Command executed on executor with data {}", ctx.getData());
ExecutionResults executionResults = new ExecutionResults();
return executionResults;
}
use of org.kie.api.runtime.query.QueryContext in project jbpm by kiegroup.
the class ExecutorLogCleanTest method deleteInfoLogsByStatus.
@Test
public void deleteInfoLogsByStatus() throws Exception {
CountDownAsyncJobListener countDownListener = new CountDownAsyncJobListener(1);
((ExecutorServiceImpl) getExecutorService()).addAsyncJobListener(countDownListener);
KieSession kieSession = createKSession(ASYNC_DATA_EXEC);
WorkItemManager wim = kieSession.getWorkItemManager();
wim.registerWorkItemHandler("async", new AsyncWorkItemHandler(getExecutorService()));
Map<String, Object> pm = new HashMap<String, Object>();
pm.put("command", "org.jbpm.test.jobexec.UserCommand");
ProcessInstance pi = kieSession.startProcess(ASYNC_DATA_EXEC_ID, pm);
// Wait for the job to complete
countDownListener.waitTillCompleted();
// Assert completion of the job
Assertions.assertThat(getExecutorService().getCompletedRequests(new QueryContext())).hasSize(1);
// Delete a record
int resultCount = auditService.requestInfoLogDeleteBuilder().status(STATUS.DONE).build().execute();
Assertions.assertThat(resultCount).isEqualTo(1);
// Assert remaining records
Assertions.assertThat(getExecutorService().getCompletedRequests(new QueryContext())).hasSize(0);
}
Aggregations