Search in sources :

Example 51 with CaseFileInstance

use of org.jbpm.casemgmt.api.model.instance.CaseFileInstance 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);
        }
    }
}
Also used : TaskSummary(org.kie.api.task.model.TaskSummary) Arrays(java.util.Arrays) UserImpl(org.jbpm.services.task.impl.model.UserImpl) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) QueryFilter(org.kie.internal.query.QueryFilter) ArrayList(java.util.ArrayList) OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) Collectors.toMap(java.util.stream.Collectors.toMap) ProcessInstance(org.jbpm.process.instance.ProcessInstance) CaseInstance(org.jbpm.casemgmt.api.model.instance.CaseInstance) Map(java.util.Map) ProcessInstanceDesc(org.jbpm.services.api.model.ProcessInstanceDesc) TaskSpecification(org.jbpm.casemgmt.api.dynamic.TaskSpecification) Logger(org.slf4j.Logger) Assert.assertNotNull(org.junit.Assert.assertNotNull) Collection(java.util.Collection) AbstractCaseServicesBaseTest(org.jbpm.casemgmt.impl.util.AbstractCaseServicesBaseTest) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) QueryContext(org.kie.api.runtime.query.QueryContext) Consumer(java.util.function.Consumer) List(java.util.List) Assertions.fail(org.assertj.core.api.Assertions.fail) Assert.assertNull(org.junit.Assert.assertNull) CaseStatus(org.jbpm.casemgmt.api.model.CaseStatus) Assert.assertEquals(org.junit.Assert.assertEquals) HashMap(java.util.HashMap) QueryContext(org.kie.api.runtime.query.QueryContext) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) CaseInstance(org.jbpm.casemgmt.api.model.instance.CaseInstance) TaskSpecification(org.jbpm.casemgmt.api.dynamic.TaskSpecification) TaskSummary(org.kie.api.task.model.TaskSummary)

Example 52 with CaseFileInstance

use of org.jbpm.casemgmt.api.model.instance.CaseFileInstance in project jbpm by kiegroup.

the class AddDynamicTaskToStageCommand method execute.

@Override
public Void execute(Context context) {
    KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
    ProcessInstance processInstance = ksession.getProcessInstance(processInstanceId);
    DynamicNodeInstance dynamicContext = (DynamicNodeInstance) ((WorkflowProcessInstanceImpl) processInstance).getNodeInstances(true).stream().filter(ni -> (ni instanceof DynamicNodeInstance) && stageId.equals(ni.getNode().getMetaData().get("UniqueId"))).findFirst().orElse(null);
    if (dynamicContext == null) {
        throw new StageNotFoundException("No stage found with id " + stageId);
    }
    CaseFileInstance caseFile = getCaseFile(ksession, caseId);
    CaseEventSupport caseEventSupport = getCaseEventSupport(context);
    caseEventSupport.fireBeforeDynamicTaskAdded(caseId, caseFile, processInstanceId, nodeType, parameters);
    DynamicUtils.addDynamicWorkItem(dynamicContext, ksession, nodeType, parameters);
    caseEventSupport.fireAfterDynamicTaskAdded(caseId, caseFile, processInstanceId, nodeType, parameters);
    return null;
}
Also used : IdentityProvider(org.kie.internal.identity.IdentityProvider) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) RegistryContext(org.drools.core.command.impl.RegistryContext) WorkflowProcessInstanceImpl(org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl) Context(org.kie.api.runtime.Context) CaseEventSupport(org.jbpm.casemgmt.impl.event.CaseEventSupport) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) Map(java.util.Map) DynamicNodeInstance(org.jbpm.workflow.instance.node.DynamicNodeInstance) KieSession(org.kie.api.runtime.KieSession) StageNotFoundException(org.jbpm.casemgmt.api.StageNotFoundException) DynamicUtils(org.jbpm.workflow.instance.node.DynamicUtils) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) CaseEventSupport(org.jbpm.casemgmt.impl.event.CaseEventSupport) StageNotFoundException(org.jbpm.casemgmt.api.StageNotFoundException) DynamicNodeInstance(org.jbpm.workflow.instance.node.DynamicNodeInstance) KieSession(org.kie.api.runtime.KieSession) RegistryContext(org.drools.core.command.impl.RegistryContext) ProcessInstance(org.kie.api.runtime.process.ProcessInstance)

Example 53 with CaseFileInstance

use of org.jbpm.casemgmt.api.model.instance.CaseFileInstance 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;
}
Also used : IdentityProvider(org.kie.internal.identity.IdentityProvider) RegistryContext(org.drools.core.command.impl.RegistryContext) Logger(org.slf4j.Logger) ProcessService(org.jbpm.services.api.ProcessService) CorrelationKey(org.kie.internal.process.CorrelationKey) PerCaseRuntimeManager(org.jbpm.runtime.manager.impl.PerCaseRuntimeManager) CaseNotFoundException(org.jbpm.casemgmt.api.CaseNotFoundException) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) QueryContext(org.kie.api.runtime.query.QueryContext) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Context(org.kie.api.runtime.Context) CaseEventSupport(org.jbpm.casemgmt.impl.event.CaseEventSupport) RuntimeDataService(org.jbpm.services.api.RuntimeDataService) ProcessInstanceDesc(org.jbpm.services.api.model.ProcessInstanceDesc) KieInternalServices(org.kie.internal.KieInternalServices) KieSession(org.kie.api.runtime.KieSession) CorrelationKeyFactory(org.kie.internal.process.CorrelationKeyFactory) CaseContext(org.kie.internal.runtime.manager.context.CaseContext) CaseEventSupport(org.jbpm.casemgmt.impl.event.CaseEventSupport) PerCaseRuntimeManager(org.jbpm.runtime.manager.impl.PerCaseRuntimeManager) PerCaseRuntimeManager(org.jbpm.runtime.manager.impl.PerCaseRuntimeManager) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager) ProcessInstanceDesc(org.jbpm.services.api.model.ProcessInstanceDesc) RegistryContext(org.drools.core.command.impl.RegistryContext) QueryContext(org.kie.api.runtime.query.QueryContext) CaseNotFoundException(org.jbpm.casemgmt.api.CaseNotFoundException) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) CorrelationKey(org.kie.internal.process.CorrelationKey) KieSession(org.kie.api.runtime.KieSession)

Example 54 with CaseFileInstance

use of org.jbpm.casemgmt.api.model.instance.CaseFileInstance in project jbpm by kiegroup.

the class CaseCommand method getCaseFile.

protected CaseFileInstance getCaseFile(KieSession ksession, String caseId) {
    Collection<? extends Object> caseFiles = ksession.getObjects(new ClassObjectFilter(CaseFileInstance.class));
    if (caseFiles.size() == 0) {
        return null;
    }
    CaseFileInstance caseFile = (CaseFileInstance) caseFiles.iterator().next();
    return caseFile;
}
Also used : CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) ClassObjectFilter(org.drools.core.ClassObjectFilter)

Example 55 with CaseFileInstance

use of org.jbpm.casemgmt.api.model.instance.CaseFileInstance in project jbpm by kiegroup.

the class CaseCommentCommand method execute.

@Override
public String execute(Context context) {
    KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
    Collection<? extends Object> caseFiles = ksession.getObjects(new ClassObjectFilter(CaseFileInstance.class));
    if (caseFiles.size() != 1) {
        throw new IllegalStateException("Not able to find distinct case file - found case files " + caseFiles.size());
    }
    CaseFileInstance caseFile = (CaseFileInstance) caseFiles.iterator().next();
    FactHandle factHandle = ksession.getFactHandle(caseFile);
    CaseEventSupport caseEventSupport = getCaseEventSupport(context);
    String commentIdentifier = null;
    if (add) {
        CommentInstance commentInstance = new CommentInstanceImpl(author, comment, restrictedTo);
        caseEventSupport.fireBeforeCaseCommentAdded(caseFile.getCaseId(), caseFile, commentInstance);
        ((CaseFileInstanceImpl) caseFile).addComment(commentInstance);
        commentIdentifier = commentInstance.getId();
        caseEventSupport.fireAfterCaseCommentAdded(caseFile.getCaseId(), caseFile, commentInstance);
    } else if (update) {
        CommentInstance toUpdate = ((CaseFileInstanceImpl) caseFile).getComments().stream().filter(c -> c.getId().equals(commentId)).findFirst().orElseThrow(() -> new CaseCommentNotFoundException("Cannot find comment with id " + commentId));
        if (!this.author.equals(toUpdate.getAuthor())) {
            throw new IllegalStateException("Only original author can update comment");
        }
        // apply authorization
        authorizationManager.checkCommentAuthorization(caseFile.getCaseId(), caseFile, toUpdate);
        caseEventSupport.fireBeforeCaseCommentUpdated(caseFile.getCaseId(), caseFile, toUpdate);
        ((CommentInstanceImpl) toUpdate).setComment(updatedText);
        if (restrictedTo != null) {
            ((CommentInstanceImpl) toUpdate).setRestrictedTo(restrictedTo);
        }
        commentIdentifier = toUpdate.getId();
        caseEventSupport.fireAfterCaseCommentUpdated(caseFile.getCaseId(), caseFile, toUpdate);
    } else if (remove) {
        CommentInstance toRemove = ((CaseFileInstanceImpl) caseFile).getComments().stream().filter(c -> c.getId().equals(commentId)).findFirst().orElseThrow(() -> new CaseCommentNotFoundException("Cannot find comment with id " + commentId));
        // apply authorization
        authorizationManager.checkCommentAuthorization(caseFile.getCaseId(), caseFile, toRemove);
        caseEventSupport.fireBeforeCaseCommentRemoved(caseFile.getCaseId(), caseFile, toRemove);
        ((CaseFileInstanceImpl) caseFile).removeComment(toRemove);
        commentIdentifier = toRemove.getId();
        caseEventSupport.fireAfterCaseCommentRemoved(caseFile.getCaseId(), caseFile, toRemove);
    }
    ksession.update(factHandle, caseFile);
    triggerRules(ksession);
    return commentIdentifier;
}
Also used : IdentityProvider(org.kie.internal.identity.IdentityProvider) CaseCommentNotFoundException(org.jbpm.casemgmt.api.CaseCommentNotFoundException) RegistryContext(org.drools.core.command.impl.RegistryContext) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) Collection(java.util.Collection) ClassObjectFilter(org.drools.core.ClassObjectFilter) FactHandle(org.kie.api.runtime.rule.FactHandle) CommentInstance(org.jbpm.casemgmt.api.model.instance.CommentInstance) List(java.util.List) Context(org.kie.api.runtime.Context) CaseEventSupport(org.jbpm.casemgmt.impl.event.CaseEventSupport) CaseFileInstanceImpl(org.jbpm.casemgmt.impl.model.instance.CaseFileInstanceImpl) KieSession(org.kie.api.runtime.KieSession) CommentInstanceImpl(org.jbpm.casemgmt.impl.model.instance.CommentInstanceImpl) AuthorizationManager(org.jbpm.casemgmt.api.auth.AuthorizationManager) CaseEventSupport(org.jbpm.casemgmt.impl.event.CaseEventSupport) FactHandle(org.kie.api.runtime.rule.FactHandle) CommentInstanceImpl(org.jbpm.casemgmt.impl.model.instance.CommentInstanceImpl) CaseFileInstanceImpl(org.jbpm.casemgmt.impl.model.instance.CaseFileInstanceImpl) RegistryContext(org.drools.core.command.impl.RegistryContext) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) ClassObjectFilter(org.drools.core.ClassObjectFilter) CommentInstance(org.jbpm.casemgmt.api.model.instance.CommentInstance) CaseCommentNotFoundException(org.jbpm.casemgmt.api.CaseCommentNotFoundException) KieSession(org.kie.api.runtime.KieSession)

Aggregations

CaseFileInstance (org.jbpm.casemgmt.api.model.instance.CaseFileInstance)121 HashMap (java.util.HashMap)95 Test (org.junit.Test)86 AbstractCaseServicesBaseTest (org.jbpm.casemgmt.impl.util.AbstractCaseServicesBaseTest)81 CaseInstance (org.jbpm.casemgmt.api.model.instance.CaseInstance)66 OrganizationalEntity (org.kie.api.task.model.OrganizationalEntity)61 UserImpl (org.jbpm.services.task.impl.model.UserImpl)54 QueryContext (org.kie.api.runtime.query.QueryContext)46 CaseNotFoundException (org.jbpm.casemgmt.api.CaseNotFoundException)44 CaseCommentNotFoundException (org.jbpm.casemgmt.api.CaseCommentNotFoundException)42 TaskSummary (org.kie.api.task.model.TaskSummary)40 AdHocFragmentNotFoundException (org.jbpm.casemgmt.api.AdHocFragmentNotFoundException)39 CaseActiveException (org.jbpm.casemgmt.api.CaseActiveException)39 TaskNotFoundException (org.jbpm.services.api.TaskNotFoundException)39 QueryFilter (org.kie.internal.query.QueryFilter)26 ArrayList (java.util.ArrayList)21 ProcessInstanceDesc (org.jbpm.services.api.model.ProcessInstanceDesc)20 Collection (java.util.Collection)17 List (java.util.List)17 Map (java.util.Map)16