use of org.jbpm.casemgmt.api.model.instance.CaseMilestoneInstance in project jbpm by kiegroup.
the class CaseServiceImpl method getCaseInstance.
@Override
public CaseInstance getCaseInstance(String caseId, boolean withData, boolean withRoles, boolean withMilestones, boolean withStages) throws CaseNotFoundException {
authorizationManager.checkAuthorization(caseId);
CaseInstanceImpl caseInstance = (CaseInstanceImpl) caseRuntimeDataService.getCaseInstanceById(caseId);
if (caseInstance.getStatus().equals(ProcessInstance.STATE_ACTIVE)) {
if (withData) {
CaseFileInstance caseFile = internalGetCaseFileInstance(caseId, caseInstance.getDeploymentId());
caseInstance.setCaseFile(caseFile);
}
if (withMilestones) {
Collection<CaseMilestoneInstance> milestones = caseRuntimeDataService.getCaseInstanceMilestones(caseId, false, new org.kie.internal.query.QueryContext(0, 100));
caseInstance.setCaseMilestones(milestones);
}
if (withRoles) {
Collection<CaseRoleInstance> roles = getCaseRoleAssignments(caseId);
caseInstance.setCaseRoles(roles);
}
if (withStages) {
Collection<CaseStageInstance> stages = caseRuntimeDataService.getCaseInstanceStages(caseId, true, new org.kie.internal.query.QueryContext(0, 100));
caseInstance.setCaseStages(stages);
}
}
return caseInstance;
}
use of org.jbpm.casemgmt.api.model.instance.CaseMilestoneInstance in project jbpm by kiegroup.
the class CaseServiceImplTest method testStartEmptyCaseViaProcessServiceWithoutCorrelationKeyWithMilestones.
@Test
public void testStartEmptyCaseViaProcessServiceWithoutCorrelationKeyWithMilestones() {
Map<String, Object> params = new HashMap<>();
params.put("s", "my case via process service");
Long processInstanceId = processService.startProcess(deploymentUnit.getIdentifier(), USER_TASK_CASE_P_ID, params);
assertNotNull(processInstanceId);
String caseId = processInstanceId.toString();
try {
CaseInstance cInstance = caseService.getCaseInstance(caseId);
assertNotNull(cInstance);
assertEquals(deploymentUnit.getIdentifier(), cInstance.getDeploymentId());
assertEquals(caseId, cInstance.getCaseId());
Collection<CaseMilestoneInstance> milestones = caseRuntimeDataService.getCaseInstanceMilestones(caseId, true, new QueryContext());
assertNotNull(milestones);
assertEquals(0, milestones.size());
// trigger milestone node
caseService.triggerAdHocFragment(caseId, "Milestone1", null);
milestones = caseRuntimeDataService.getCaseInstanceMilestones(caseId, true, new QueryContext());
assertNotNull(milestones);
assertEquals(1, milestones.size());
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.jbpm.casemgmt.api.model.instance.CaseMilestoneInstance in project jbpm by kiegroup.
the class CaseServiceImplTest method testTriggerTaskAndMilestoneInCase.
@Test
public void testTriggerTaskAndMilestoneInCase() {
Map<String, OrganizationalEntity> roleAssignments = new HashMap<>();
roleAssignments.put("owner", new UserImpl("john"));
Map<String, Object> data = new HashMap<>();
data.put("s", "description");
CaseFileInstance caseFile = caseService.newCaseFileInstance(deploymentUnit.getIdentifier(), USER_TASK_CASE_P_ID, data, roleAssignments);
CaseDefinition caseDef = caseRuntimeDataService.getCase(deploymentUnit.getIdentifier(), USER_TASK_CASE_P_ID);
assertNotNull(caseDef);
assertEquals(deploymentUnit.getIdentifier(), caseDef.getDeploymentId());
assertEquals(3, caseDef.getAdHocFragments().size());
Map<String, AdHocFragment> mappedFragments = mapAdHocFragments(caseDef.getAdHocFragments());
assertTrue(mappedFragments.containsKey("Hello2"));
assertTrue(mappedFragments.containsKey("Milestone1"));
assertTrue(mappedFragments.containsKey("Milestone2"));
String caseId = caseService.startCase(deploymentUnit.getIdentifier(), USER_TASK_CASE_P_ID, caseFile);
assertNotNull(caseId);
assertEquals(HR_CASE_ID, caseId);
try {
CaseInstance cInstance = caseService.getCaseInstance(caseId);
assertNotNull(cInstance);
assertEquals(HR_CASE_ID, cInstance.getCaseId());
assertEquals(deploymentUnit.getIdentifier(), cInstance.getDeploymentId());
List<TaskSummary> tasks = runtimeDataService.getTasksAssignedAsPotentialOwner("john", new QueryFilter());
assertNotNull(tasks);
assertEquals(1, tasks.size());
TaskSummary task = tasks.get(0);
assertNotNull(task);
assertEquals("Hello1", task.getName());
assertEquals("john", task.getActualOwnerId());
assertEquals(Status.Reserved, task.getStatus());
// now let's trigger one (human task) fragment
Map<String, Object> taskData = new HashMap<>();
taskData.put("test", "value");
taskData.put("fromVar", "#{s}");
caseService.triggerAdHocFragment(HR_CASE_ID, "Hello2", taskData);
tasks = runtimeDataService.getTasksAssignedAsPotentialOwner("john", new QueryFilter());
assertNotNull(tasks);
assertEquals(2, tasks.size());
task = tasks.get(0);
assertNotNull(task);
assertEquals("Hello2", task.getName());
assertEquals("john", task.getActualOwnerId());
assertEquals(Status.Reserved, task.getStatus());
Map<String, Object> taskInputs = userTaskService.getTaskInputContentByTaskId(task.getId());
assertNotNull(taskInputs);
assertTrue(taskInputs.containsKey("test"));
assertTrue(taskInputs.containsKey("fromVar"));
assertEquals("value", taskInputs.get("test"));
assertEquals("description", taskInputs.get("fromVar"));
task = tasks.get(1);
assertNotNull(task);
assertEquals("Hello1", task.getName());
assertEquals("john", task.getActualOwnerId());
assertEquals(Status.Reserved, task.getStatus());
Collection<CaseMilestoneInstance> milestones = caseRuntimeDataService.getCaseInstanceMilestones(HR_CASE_ID, true, new QueryContext());
assertNotNull(milestones);
assertEquals(0, milestones.size());
milestones = caseRuntimeDataService.getCaseInstanceMilestones(HR_CASE_ID, false, new QueryContext());
assertNotNull(milestones);
assertEquals(2, milestones.size());
List<String> expectedMilestones = Arrays.asList("Milestone1", "Milestone2");
for (CaseMilestoneInstance mi : milestones) {
assertTrue("Expected mile stopne not found", expectedMilestones.contains(mi.getName()));
assertEquals("Wrong milestone status", MilestoneStatus.Available, mi.getStatus());
assertFalse("Should not be achieved", mi.isAchieved());
assertNull("Achieved date should be null", mi.getAchievedAt());
}
// trigger milestone node
caseService.triggerAdHocFragment(HR_CASE_ID, "Milestone1", null);
milestones = caseRuntimeDataService.getCaseInstanceMilestones(HR_CASE_ID, true, new QueryContext());
assertNotNull(milestones);
assertEquals(1, milestones.size());
CaseMilestoneInstance msInstance = milestones.iterator().next();
assertNotNull(msInstance);
assertEquals("Milestone1", msInstance.getName());
assertEquals(true, msInstance.isAchieved());
assertNotNull(msInstance.getAchievedAt());
// trigger another milestone node that has condition so it should not be achieved
caseService.triggerAdHocFragment(HR_CASE_ID, "Milestone2", null);
milestones = caseRuntimeDataService.getCaseInstanceMilestones(HR_CASE_ID, true, new QueryContext());
assertNotNull(milestones);
assertEquals(1, milestones.size());
milestones = caseRuntimeDataService.getCaseInstanceMilestones(HR_CASE_ID, false, new QueryContext());
assertNotNull(milestones);
assertEquals(2, milestones.size());
// add dataComplete to case file to achieve milestone
caseService.addDataToCaseFile(HR_CASE_ID, "dataComplete", true);
milestones = caseRuntimeDataService.getCaseInstanceMilestones(HR_CASE_ID, true, new QueryContext());
assertNotNull(milestones);
assertEquals(2, milestones.size());
} 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.jbpm.casemgmt.api.model.instance.CaseMilestoneInstance 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);
}
Aggregations