Search in sources :

Example 1 with CaseMilestoneInstance

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;
}
Also used : CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) CaseInstanceImpl(org.jbpm.casemgmt.impl.model.instance.CaseInstanceImpl) CaseMilestoneInstance(org.jbpm.casemgmt.api.model.instance.CaseMilestoneInstance) CaseStageInstance(org.jbpm.casemgmt.api.model.instance.CaseStageInstance) CaseRoleInstance(org.jbpm.casemgmt.api.model.instance.CaseRoleInstance)

Example 2 with CaseMilestoneInstance

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);
        }
    }
}
Also used : CaseInstance(org.jbpm.casemgmt.api.model.instance.CaseInstance) HashMap(java.util.HashMap) CaseMilestoneInstance(org.jbpm.casemgmt.api.model.instance.CaseMilestoneInstance) QueryContext(org.kie.api.runtime.query.QueryContext) CaseCommentNotFoundException(org.jbpm.casemgmt.api.CaseCommentNotFoundException) AdHocFragmentNotFoundException(org.jbpm.casemgmt.api.AdHocFragmentNotFoundException) CaseNotFoundException(org.jbpm.casemgmt.api.CaseNotFoundException) TaskNotFoundException(org.jbpm.services.api.TaskNotFoundException) CaseActiveException(org.jbpm.casemgmt.api.CaseActiveException) AbstractCaseServicesBaseTest(org.jbpm.casemgmt.impl.util.AbstractCaseServicesBaseTest) Test(org.junit.Test)

Example 3 with CaseMilestoneInstance

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);
        }
    }
}
Also used : OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) HashMap(java.util.HashMap) CaseMilestoneInstance(org.jbpm.casemgmt.api.model.instance.CaseMilestoneInstance) QueryContext(org.kie.api.runtime.query.QueryContext) AdHocFragment(org.jbpm.casemgmt.api.model.AdHocFragment) CaseCommentNotFoundException(org.jbpm.casemgmt.api.CaseCommentNotFoundException) AdHocFragmentNotFoundException(org.jbpm.casemgmt.api.AdHocFragmentNotFoundException) CaseNotFoundException(org.jbpm.casemgmt.api.CaseNotFoundException) TaskNotFoundException(org.jbpm.services.api.TaskNotFoundException) CaseActiveException(org.jbpm.casemgmt.api.CaseActiveException) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) CaseInstance(org.jbpm.casemgmt.api.model.instance.CaseInstance) QueryFilter(org.kie.internal.query.QueryFilter) CaseDefinition(org.jbpm.casemgmt.api.model.CaseDefinition) UserImpl(org.jbpm.services.task.impl.model.UserImpl) TaskSummary(org.kie.api.task.model.TaskSummary) AbstractCaseServicesBaseTest(org.jbpm.casemgmt.impl.util.AbstractCaseServicesBaseTest) Test(org.junit.Test)

Example 4 with CaseMilestoneInstance

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);
}
Also used : TaskSummary(org.kie.api.task.model.TaskSummary) Arrays(java.util.Arrays) CaseDefinition(org.jbpm.casemgmt.api.model.CaseDefinition) WorkflowProcess(org.jbpm.workflow.core.WorkflowProcess) ProcessDefinition(org.jbpm.services.api.model.ProcessDefinition) BoundaryEventNode(org.jbpm.workflow.core.node.BoundaryEventNode) DynamicNode(org.jbpm.workflow.core.node.DynamicNode) StartNode(org.jbpm.workflow.core.node.StartNode) CaseRuntimeDataService(org.jbpm.casemgmt.api.CaseRuntimeDataService) CaseDefinitionComparator(org.jbpm.casemgmt.impl.model.CaseDefinitionComparator) Collectors.toMap(java.util.stream.Collectors.toMap) NodeInstanceDesc(org.jbpm.kie.services.impl.model.NodeInstanceDesc) Map(java.util.Map) ProcessInstanceDesc(org.jbpm.services.api.model.ProcessInstanceDesc) KieInternalServices(org.kie.internal.KieInternalServices) AuthorizationManager(org.jbpm.casemgmt.api.auth.AuthorizationManager) CaseMilestoneInstance(org.jbpm.casemgmt.api.model.instance.CaseMilestoneInstance) CaseMilestone(org.jbpm.casemgmt.api.model.CaseMilestone) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Process(org.kie.api.definition.process.Process) FILTER(org.kie.internal.query.QueryParameterIdentifiers.FILTER) Set(java.util.Set) DeploymentRolesManager(org.jbpm.kie.services.impl.security.DeploymentRolesManager) QueryContext(org.kie.api.runtime.query.QueryContext) Collectors(java.util.stream.Collectors) CaseMilestoneInstanceImpl(org.jbpm.casemgmt.impl.model.instance.CaseMilestoneInstanceImpl) AbstractRuntimeManager(org.jbpm.runtime.manager.impl.AbstractRuntimeManager) ProcessAssetDesc(org.jbpm.kie.services.impl.model.ProcessAssetDesc) CaseFileItem(org.jbpm.casemgmt.api.model.CaseFileItem) List(java.util.List) DeployedAsset(org.jbpm.services.api.model.DeployedAsset) MilestoneNode(org.jbpm.workflow.core.node.MilestoneNode) AdHocFragment(org.jbpm.casemgmt.api.model.AdHocFragment) ServiceRegistry(org.jbpm.services.api.service.ServiceRegistry) CorrelationKeyFactory(org.kie.internal.process.CorrelationKeyFactory) Status(org.kie.api.task.model.Status) NodeContainer(org.kie.api.definition.process.NodeContainer) AdHocFragmentImpl(org.jbpm.casemgmt.impl.model.AdHocFragmentImpl) CaseDefinitionImpl(org.jbpm.casemgmt.impl.model.CaseDefinitionImpl) CaseRole(org.jbpm.casemgmt.api.model.CaseRole) CaseRoleImpl(org.jbpm.casemgmt.impl.model.CaseRoleImpl) CaseStageInstanceImpl(org.jbpm.casemgmt.impl.model.instance.CaseStageInstanceImpl) CaseNotFoundException(org.jbpm.casemgmt.api.CaseNotFoundException) HashMap(java.util.HashMap) DeploymentEventListener(org.jbpm.services.api.DeploymentEventListener) TransactionalCommandService(org.jbpm.shared.services.impl.TransactionalCommandService) ArrayList(java.util.ArrayList) ProcessDefinitionComparator(org.jbpm.casemgmt.impl.model.ProcessDefinitionComparator) HashSet(java.util.HashSet) CaseStageInstance(org.jbpm.casemgmt.api.model.instance.CaseStageInstance) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) DeploymentEvent(org.jbpm.services.api.DeploymentEvent) CaseInstance(org.jbpm.casemgmt.api.model.instance.CaseInstance) StageStatus(org.jbpm.casemgmt.api.model.instance.StageStatus) CaseStage(org.jbpm.casemgmt.api.model.CaseStage) CaseStageImpl(org.jbpm.casemgmt.impl.model.CaseStageImpl) KieBase(org.kie.api.KieBase) IdentityProvider(org.kie.internal.identity.IdentityProvider) QueryManager(org.jbpm.shared.services.impl.QueryManager) CorrelationKey(org.kie.internal.process.CorrelationKey) QueryNameCommand(org.jbpm.shared.services.impl.commands.QueryNameCommand) CaseIdGenerator(org.jbpm.casemgmt.api.generator.CaseIdGenerator) Collectors.toList(java.util.stream.Collectors.toList) Node(org.kie.api.definition.process.Node) RuntimeDataService(org.jbpm.services.api.RuntimeDataService) CaseStatus(org.jbpm.casemgmt.api.model.CaseStatus) CaseMilestoneImpl(org.jbpm.casemgmt.impl.model.CaseMilestoneImpl) CommonUtils.getAuthenticatedUserRoles(org.jbpm.kie.services.impl.CommonUtils.getAuthenticatedUserRoles) Collections(java.util.Collections) CaseMilestoneInstance(org.jbpm.casemgmt.api.model.instance.CaseMilestoneInstance) ArrayList(java.util.ArrayList) ProcessInstanceDesc(org.jbpm.services.api.model.ProcessInstanceDesc) CaseNotFoundException(org.jbpm.casemgmt.api.CaseNotFoundException) CorrelationKey(org.kie.internal.process.CorrelationKey) CaseDefinition(org.jbpm.casemgmt.api.model.CaseDefinition) CaseMilestoneInstanceImpl(org.jbpm.casemgmt.impl.model.instance.CaseMilestoneInstanceImpl) NodeInstanceDesc(org.jbpm.kie.services.impl.model.NodeInstanceDesc)

Aggregations

CaseMilestoneInstance (org.jbpm.casemgmt.api.model.instance.CaseMilestoneInstance)4 HashMap (java.util.HashMap)3 CaseNotFoundException (org.jbpm.casemgmt.api.CaseNotFoundException)3 CaseInstance (org.jbpm.casemgmt.api.model.instance.CaseInstance)3 QueryContext (org.kie.api.runtime.query.QueryContext)3 AdHocFragmentNotFoundException (org.jbpm.casemgmt.api.AdHocFragmentNotFoundException)2 CaseActiveException (org.jbpm.casemgmt.api.CaseActiveException)2 CaseCommentNotFoundException (org.jbpm.casemgmt.api.CaseCommentNotFoundException)2 AdHocFragment (org.jbpm.casemgmt.api.model.AdHocFragment)2 CaseDefinition (org.jbpm.casemgmt.api.model.CaseDefinition)2 CaseFileInstance (org.jbpm.casemgmt.api.model.instance.CaseFileInstance)2 CaseStageInstance (org.jbpm.casemgmt.api.model.instance.CaseStageInstance)2 TaskSummary (org.kie.api.task.model.TaskSummary)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1