Search in sources :

Example 6 with CaseNotFoundException

use of org.jbpm.casemgmt.api.CaseNotFoundException 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 7 with CaseNotFoundException

use of org.jbpm.casemgmt.api.CaseNotFoundException in project jbpm by kiegroup.

the class CaseServiceImplTest method testStartThenReopenDestroyedCase.

@Test
public void testStartThenReopenDestroyedCase() {
    Map<String, Object> data = new HashMap<>();
    data.put("name", "my first case");
    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());
        assertEquals("my first case", cInstance.getCaseDescription());
        caseService.destroyCase(caseId);
        try {
            caseService.reopenCase(caseId, deploymentUnit.getIdentifier(), EMPTY_CASE_P_ID);
            fail("Not allowed to reopen destroyed case");
        } catch (CaseNotFoundException e) {
        // expected
        }
        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 : CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) CaseInstance(org.jbpm.casemgmt.api.model.instance.CaseInstance) HashMap(java.util.HashMap) CaseNotFoundException(org.jbpm.casemgmt.api.CaseNotFoundException) 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 8 with CaseNotFoundException

use of org.jbpm.casemgmt.api.CaseNotFoundException in project jbpm by kiegroup.

the class StartCaseWorkItemHandler method executeWorkItem.

@Override
public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
    String deploymentId = (String) workItem.getParameter(DEPLOYMENT_ID);
    if (deploymentId == null) {
        deploymentId = ((org.drools.core.process.instance.WorkItem) workItem).getDeploymentId();
    }
    RuntimeManager targetRuntimeManager = RuntimeManagerRegistry.get().getManager(deploymentId);
    if (targetRuntimeManager == null || !(targetRuntimeManager instanceof PerCaseRuntimeManager)) {
        throw new IllegalArgumentException("Requested target deployment does not exist or is not per case strategy");
    }
    String caseDefinitionId = (String) workItem.getParameter(CASE_DEFINITION_ID);
    if (caseDefinitionId == null || caseDefinitionId.trim().isEmpty()) {
        throw new IllegalArgumentException(CASE_DEFINITION_ID + " is a required parameter for StartCaseWorkItemHandler");
    }
    Map<String, Object> caseFileData = new HashMap<>();
    Map<String, List<String>> accessRestrictions = new HashMap<>();
    Map<String, OrganizationalEntity> roleAssignments = new HashMap<>();
    parseParameters(workItem, caseFileData, roleAssignments, accessRestrictions);
    long processInstanceId = ((WorkItemImpl) workItem).getProcessInstanceId();
    long workItemId = workItem.getId();
    logger.debug("Parent process instance id {} and work item instance id {} for new case instance", processInstanceId, workItemId);
    CaseService caseService = (CaseService) ServiceRegistry.get().service(ServiceRegistry.CASE_SERVICE);
    CaseFileInstance subCaseFile = caseService.newCaseFileInstanceWithRestrictions(deploymentId, caseDefinitionId, caseFileData, roleAssignments, accessRestrictions);
    ((CaseFileInstanceImpl) subCaseFile).setParentInstanceId(processInstanceId);
    ((CaseFileInstanceImpl) subCaseFile).setParentWorkItemId(workItemId);
    String caseId = caseService.startCase(deploymentId, caseDefinitionId, subCaseFile);
    logger.debug("Case with id {} has been successfully started");
    boolean independent = Boolean.parseBoolean((String) workItem.getParameter(INDEPENDENT));
    if (independent) {
        Map<String, Object> results = new HashMap<>();
        results.put(CASE_ID, caseId);
        try {
            CaseFileInstance snapshot = caseService.getCaseFileInstance(caseId);
            results.putAll(snapshot.getData());
        } catch (CaseNotFoundException e) {
            // case is already completed
            logger.debug("Case is already completed, not possible to fetch case file data any more");
        }
        logger.debug("Completing directly (without waiting for case instance {} completion) work item with id {}", caseId, workItem.getId());
        ((CaseFileInstanceImpl) subCaseFile).setParentInstanceId(null);
        ((CaseFileInstanceImpl) subCaseFile).setParentWorkItemId(null);
        manager.completeWorkItem(workItem.getId(), results);
    } else {
        // save case id so the abort work item can abort/destroy the case instance
        ((WorkItemImpl) workItem).setParameter(CASE_ID, caseId);
        logger.debug("Waiting for case instance {} completion before completing work item with id {}", caseId, workItem.getId());
    }
}
Also used : PerCaseRuntimeManager(org.jbpm.runtime.manager.impl.PerCaseRuntimeManager) OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) HashMap(java.util.HashMap) PerCaseRuntimeManager(org.jbpm.runtime.manager.impl.PerCaseRuntimeManager) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager) CaseFileInstanceImpl(org.jbpm.casemgmt.impl.model.instance.CaseFileInstanceImpl) CaseNotFoundException(org.jbpm.casemgmt.api.CaseNotFoundException) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) WorkItemImpl(org.drools.core.process.instance.impl.WorkItemImpl) ArrayList(java.util.ArrayList) List(java.util.List) CaseService(org.jbpm.casemgmt.api.CaseService)

Example 9 with CaseNotFoundException

use of org.jbpm.casemgmt.api.CaseNotFoundException in project jbpm by kiegroup.

the class CaseRuntimeDataServiceImpl method getCaseInstanceStages.

@Override
public Collection<CaseStageInstance> getCaseInstanceStages(String caseId, boolean activeOnly, 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");
    }
    CaseDefinition caseDef = getCase(pi.getDeploymentId(), pi.getProcessId());
    List<CaseStageInstance> stages = internalGetCaseStages(caseDef, caseId, activeOnly, queryContext);
    return applyPagination(stages, queryContext);
}
Also used : CaseDefinition(org.jbpm.casemgmt.api.model.CaseDefinition) CaseStageInstance(org.jbpm.casemgmt.api.model.instance.CaseStageInstance) ProcessInstanceDesc(org.jbpm.services.api.model.ProcessInstanceDesc) CaseNotFoundException(org.jbpm.casemgmt.api.CaseNotFoundException)

Example 10 with CaseNotFoundException

use of org.jbpm.casemgmt.api.CaseNotFoundException 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

CaseNotFoundException (org.jbpm.casemgmt.api.CaseNotFoundException)14 HashMap (java.util.HashMap)6 CaseFileInstance (org.jbpm.casemgmt.api.model.instance.CaseFileInstance)6 ProcessInstanceDesc (org.jbpm.services.api.model.ProcessInstanceDesc)6 List (java.util.List)5 CaseInstance (org.jbpm.casemgmt.api.model.instance.CaseInstance)5 ArrayList (java.util.ArrayList)4 Collection (java.util.Collection)4 CaseDefinition (org.jbpm.casemgmt.api.model.CaseDefinition)4 QueryContext (org.kie.api.runtime.query.QueryContext)4 Collectors.toList (java.util.stream.Collectors.toList)3 CaseActiveException (org.jbpm.casemgmt.api.CaseActiveException)3 CaseStageInstance (org.jbpm.casemgmt.api.model.instance.CaseStageInstance)3 CaseFileInstanceImpl (org.jbpm.casemgmt.impl.model.instance.CaseFileInstanceImpl)3 RuntimeDataService (org.jbpm.services.api.RuntimeDataService)3 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)3 KieInternalServices (org.kie.internal.KieInternalServices)3 IdentityProvider (org.kie.internal.identity.IdentityProvider)3 CorrelationKey (org.kie.internal.process.CorrelationKey)3 CorrelationKeyFactory (org.kie.internal.process.CorrelationKeyFactory)3