use of org.jbpm.casemgmt.api.model.instance.CaseStageInstance in project jbpm by kiegroup.
the class CaseRuntimeDataServiceImpl method getAdHocFragmentsForCase.
@Override
public Collection<AdHocFragment> getAdHocFragmentsForCase(String caseId) {
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");
}
CaseDefinition caseDef = getCase(pi.getDeploymentId(), pi.getProcessId());
List<AdHocFragment> adHocFragments = new ArrayList<>();
adHocFragments.addAll(caseDef.getAdHocFragments());
Collection<CaseStageInstance> activeStages = internalGetCaseStages(caseDef, caseId, true, new QueryContext(0, 100));
activeStages.forEach(stage -> adHocFragments.addAll(stage.getAdHocFragments()));
return adHocFragments;
}
use of org.jbpm.casemgmt.api.model.instance.CaseStageInstance 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.jbpm.casemgmt.api.model.instance.CaseStageInstance 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.CaseStageInstance in project jbpm by kiegroup.
the class CaseRuntimeDataServiceImplTest method testTransitionBetweenStagesWithConditionsInCase.
@Test
public void testTransitionBetweenStagesWithConditionsInCase() {
Map<String, OrganizationalEntity> roleAssignments = new HashMap<>();
roleAssignments.put("owner", new UserImpl(USER));
Map<String, Object> data = new HashMap<>();
data.put("customData", "none");
CaseFileInstance caseFile = caseService.newCaseFileInstance(deploymentUnit.getIdentifier(), TWO_STAGES_CONDITIONS_CASE_P_ID, data, roleAssignments);
String caseId = caseService.startCase(deploymentUnit.getIdentifier(), TWO_STAGES_CONDITIONS_CASE_P_ID, caseFile);
assertNotNull(caseId);
try {
Collection<CaseStageInstance> stage = caseRuntimeDataService.getCaseInstanceStages(caseId, true, new QueryContext(0, 1));
assertNotNull(stage);
assertEquals(1, stage.size());
assertEquals("Stage One", stage.iterator().next().getName());
assertEquals(StageStatus.Active, stage.iterator().next().getStatus());
Collection<AdHocFragment> adhocTasks = caseRuntimeDataService.getAdHocFragmentsForCase(caseId);
assertNotNull(adhocTasks);
assertEquals(1, adhocTasks.size());
assertEquals("Task 1", adhocTasks.iterator().next().getName());
caseService.triggerAdHocFragment(caseId, "Task 1", null);
List<TaskSummary> tasks = runtimeDataService.getTasksAssignedAsPotentialOwner("john", new QueryFilter());
assertNotNull(tasks);
assertEquals(1, tasks.size());
assertTask(tasks.get(0), "john", "Task 1", Status.Reserved);
Map<String, Object> params = new HashMap<>();
params.put("myData", "nextStage");
userTaskService.completeAutoProgress(tasks.get(0).getId(), "john", params);
stage = caseRuntimeDataService.getCaseInstanceStages(caseId, true, new QueryContext(0, 1));
assertNotNull(stage);
assertEquals(1, stage.size());
assertEquals("Stage Two", stage.iterator().next().getName());
assertEquals(StageStatus.Active, stage.iterator().next().getStatus());
adhocTasks = caseRuntimeDataService.getAdHocFragmentsForCase(caseId);
assertNotNull(adhocTasks);
assertEquals(1, adhocTasks.size());
assertEquals("Task 2", adhocTasks.iterator().next().getName());
caseService.triggerAdHocFragment(caseId, "Task 2", null);
tasks = runtimeDataService.getTasksAssignedAsPotentialOwner("john", new QueryFilter());
assertNotNull(tasks);
assertEquals(1, tasks.size());
assertTask(tasks.get(0), "john", "Task 2", Status.Reserved);
params = new HashMap<>();
params.put("myData", "none");
userTaskService.completeAutoProgress(tasks.get(0).getId(), "john", params);
CaseInstance instance = caseService.getCaseInstance(caseId);
Assertions.assertThat(instance.getStatus()).isEqualTo(CaseStatus.CLOSED.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.CaseStageInstance in project jbpm by kiegroup.
the class CaseRuntimeDataServiceImplTest method testTransitionBetweenStagesInCase.
@Test
public void testTransitionBetweenStagesInCase() {
Map<String, OrganizationalEntity> roleAssignments = new HashMap<>();
roleAssignments.put("owner", new UserImpl(USER));
Map<String, Object> data = new HashMap<>();
CaseFileInstance caseFile = caseService.newCaseFileInstance(deploymentUnit.getIdentifier(), TWO_STAGES_CASE_P_ID, data, roleAssignments);
String caseId = caseService.startCase(deploymentUnit.getIdentifier(), TWO_STAGES_CASE_P_ID, caseFile);
assertNotNull(caseId);
try {
Collection<CaseStageInstance> stage = caseRuntimeDataService.getCaseInstanceStages(caseId, true, new QueryContext(0, 1));
assertNotNull(stage);
assertEquals(1, stage.size());
assertEquals("Stage One", stage.iterator().next().getName());
assertEquals(StageStatus.Active, stage.iterator().next().getStatus());
Collection<AdHocFragment> adhocTasks = caseRuntimeDataService.getAdHocFragmentsForCase(caseId);
assertNotNull(adhocTasks);
assertEquals(1, adhocTasks.size());
assertEquals("Task 1", adhocTasks.iterator().next().getName());
Collection<NodeInstanceDesc> activeNodes = caseRuntimeDataService.getActiveNodesForCase(caseId, new QueryContext(0, 10));
assertNotNull(activeNodes);
assertEquals(1, activeNodes.size());
assertEquals("Stage One", activeNodes.iterator().next().getName());
Collection<NodeInstanceDesc> completedNodes = caseRuntimeDataService.getCompletedNodesForCase(caseId, new QueryContext(0, 10));
assertNotNull(completedNodes);
assertEquals(0, completedNodes.size());
caseService.addDataToCaseFile(caseId, "customData", "nextStagePlease");
stage = caseRuntimeDataService.getCaseInstanceStages(caseId, true, new QueryContext(0, 1));
assertNotNull(stage);
assertEquals(1, stage.size());
assertEquals("Stage Two", stage.iterator().next().getName());
assertEquals(StageStatus.Active, stage.iterator().next().getStatus());
adhocTasks = caseRuntimeDataService.getAdHocFragmentsForCase(caseId);
assertNotNull(adhocTasks);
assertEquals(1, adhocTasks.size());
assertEquals("Task 2", adhocTasks.iterator().next().getName());
activeNodes = caseRuntimeDataService.getActiveNodesForCase(caseId, new QueryContext(0, 10));
assertNotNull(activeNodes);
assertEquals(1, activeNodes.size());
assertEquals("Stage Two", activeNodes.iterator().next().getName());
completedNodes = caseRuntimeDataService.getCompletedNodesForCase(caseId, new QueryContext(0, 10));
assertNotNull(completedNodes);
assertEquals(1, completedNodes.size());
assertEquals("Stage One", completedNodes.iterator().next().getName());
} catch (Exception e) {
logger.error("Unexpected error {}", e.getMessage(), e);
fail("Unexpected exception " + e.getMessage());
} finally {
if (caseId != null) {
caseService.cancelCase(caseId);
}
}
}
Aggregations