Search in sources :

Example 1 with CorrelationKey

use of org.kie.internal.process.CorrelationKey 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;
}
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) CaseStage(org.jbpm.casemgmt.api.model.CaseStage) ArrayList(java.util.ArrayList) CaseStageInstance(org.jbpm.casemgmt.api.model.instance.CaseStageInstance) QueryContext(org.kie.api.runtime.query.QueryContext) StageStatus(org.jbpm.casemgmt.api.model.instance.StageStatus) CorrelationKey(org.kie.internal.process.CorrelationKey) Collection(java.util.Collection) NodeInstanceDesc(org.jbpm.kie.services.impl.model.NodeInstanceDesc) CaseStageInstanceImpl(org.jbpm.casemgmt.impl.model.instance.CaseStageInstanceImpl)

Example 2 with CorrelationKey

use of org.kie.internal.process.CorrelationKey in project jbpm by kiegroup.

the class CaseServiceImpl method internalTriggerAdHocFragment.

protected void internalTriggerAdHocFragment(ProcessInstanceDesc pi, String fragmentName, Object data) throws CaseNotFoundException {
    CorrelationKey key = CorrelationKeyXmlAdapter.unmarshalCorrelationKey(pi.getCorrelationKey());
    String caseId = (String) key.getProperties().get(0).getValue();
    authorizationManager.checkAuthorization(caseId);
    CaseDefinition caseDef = caseRuntimeDataService.getCase(pi.getDeploymentId(), pi.getProcessId());
    List<AdHocFragment> allFragments = new ArrayList<>();
    if (caseDef.getAdHocFragments() != null) {
        allFragments.addAll(caseDef.getAdHocFragments());
    }
    caseDef.getCaseStages().forEach(stage -> {
        if (stage.getAdHocFragments() != null) {
            allFragments.addAll(stage.getAdHocFragments());
        }
    });
    allFragments.stream().filter(fragment -> fragment.getName().equals(fragmentName)).findFirst().orElseThrow(() -> new AdHocFragmentNotFoundException("AdHoc fragment '" + fragmentName + "' not found in case " + pi.getCorrelationKey()));
    processService.signalProcessInstance(pi.getId(), fragmentName, data);
}
Also used : CorrelationKey(org.kie.internal.process.CorrelationKey) CaseDefinition(org.jbpm.casemgmt.api.model.CaseDefinition) ArrayList(java.util.ArrayList) AdHocFragmentNotFoundException(org.jbpm.casemgmt.api.AdHocFragmentNotFoundException) AdHocFragment(org.jbpm.casemgmt.api.model.AdHocFragment)

Example 3 with CorrelationKey

use of org.kie.internal.process.CorrelationKey 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 4 with CorrelationKey

use of org.kie.internal.process.CorrelationKey in project jbpm by kiegroup.

the class ReopenCaseCommand method execute.

@Override
public Void execute(Context context) {
    CaseEventSupport caseEventSupport = getCaseEventSupport(context);
    KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
    CaseFileInstance caseFile = getCaseFile(ksession, caseId);
    caseEventSupport.fireBeforeCaseReopened(caseId, caseFile, deploymentId, caseDefinitionId, data);
    logger.debug("Updating case file in working memory");
    FactHandle factHandle = ksession.getFactHandle(caseFile);
    ((CaseFileInstanceImpl) caseFile).setCaseReopenDate(new Date());
    if (data != null && !data.isEmpty()) {
        caseFile.addAll(data);
    }
    ksession.update(factHandle, caseFile);
    logger.debug("Starting process instance for case {} and case definition {}", caseId, caseDefinitionId);
    CorrelationKey correlationKey = correlationKeyFactory.newCorrelationKey(caseId);
    Map<String, Object> params = new HashMap<>();
    // set case id to allow it to use CaseContext when creating runtime engine
    params.put(EnvironmentName.CASE_ID, caseId);
    long processInstanceId = processService.startProcess(deploymentId, caseDefinitionId, correlationKey, params);
    logger.debug("Case {} successfully reopened (process instance id {})", caseId, processInstanceId);
    caseEventSupport.fireAfterCaseReopened(caseId, caseFile, deploymentId, caseDefinitionId, data, processInstanceId);
    return null;
}
Also used : CaseEventSupport(org.jbpm.casemgmt.impl.event.CaseEventSupport) FactHandle(org.kie.api.runtime.rule.FactHandle) HashMap(java.util.HashMap) CaseFileInstanceImpl(org.jbpm.casemgmt.impl.model.instance.CaseFileInstanceImpl) RegistryContext(org.drools.core.command.impl.RegistryContext) Date(java.util.Date) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) CorrelationKey(org.kie.internal.process.CorrelationKey) KieSession(org.kie.api.runtime.KieSession)

Example 5 with CorrelationKey

use of org.kie.internal.process.CorrelationKey in project jbpm by kiegroup.

the class PerProcessInstanceRuntimeManagerTest method testCreationOfSessionWithPersistenceByCorrelationKey.

@Test
public void testCreationOfSessionWithPersistenceByCorrelationKey() {
    RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().userGroupCallback(userGroupCallback).addAsset(ResourceFactory.newClassPathResource("BPMN2-ScriptTask.bpmn2"), ResourceType.BPMN2).addAsset(ResourceFactory.newClassPathResource("BPMN2-UserTask.bpmn2"), ResourceType.BPMN2).get();
    CorrelationKeyFactory keyFactory = KieInternalServices.Factory.get().newCorrelationKeyFactory();
    manager = RuntimeManagerFactory.Factory.get().newPerProcessInstanceRuntimeManager(environment);
    assertNotNull(manager);
    // ksession for process instance #1
    // since there is no process instance yet we need to get new session
    CorrelationKey key = keyFactory.newCorrelationKey("first");
    RuntimeEngine runtime = manager.getRuntimeEngine(CorrelationKeyContext.get());
    KieSession ksession = runtime.getKieSession();
    assertNotNull(ksession);
    long ksession1Id = ksession.getIdentifier();
    assertTrue(ksession1Id == 2);
    // ksession for process instance #2
    // since there is no process instance yet we need to get new session
    CorrelationKey key2 = keyFactory.newCorrelationKey("second");
    RuntimeEngine runtime2 = manager.getRuntimeEngine(CorrelationKeyContext.get());
    KieSession ksession2 = runtime2.getKieSession();
    assertNotNull(ksession2);
    long ksession2Id = ksession2.getIdentifier();
    assertTrue(ksession2Id == 3);
    ProcessInstance pi1 = ((CorrelationAwareProcessRuntime) ksession).startProcess("UserTask", key, null);
    ProcessInstance pi2 = ((CorrelationAwareProcessRuntime) ksession2).startProcess("UserTask", key2, null);
    // both processes started
    assertEquals(ProcessInstance.STATE_ACTIVE, pi1.getState());
    assertEquals(ProcessInstance.STATE_ACTIVE, pi2.getState());
    runtime = manager.getRuntimeEngine(CorrelationKeyContext.get(key));
    ksession = runtime.getKieSession();
    assertEquals(ksession1Id, ksession.getIdentifier());
    ksession.getWorkItemManager().completeWorkItem(1, null);
    // since process is completed now session should not be there any more
    try {
        manager.getRuntimeEngine(CorrelationKeyContext.get(key)).getKieSession();
        fail("Session for this (" + pi1.getId() + ") process instance is no more accessible");
    } catch (RuntimeException e) {
    }
    runtime2 = manager.getRuntimeEngine(CorrelationKeyContext.get(key2));
    ksession2 = runtime2.getKieSession();
    assertEquals(ksession2Id, ksession2.getIdentifier());
    ksession2.getWorkItemManager().completeWorkItem(2, null);
    // since process is completed now session should not be there any more
    try {
        manager.getRuntimeEngine(CorrelationKeyContext.get(key2)).getKieSession();
        fail("Session for this (" + pi2.getId() + ") process instance is no more accessible");
    } catch (RuntimeException e) {
    }
    manager.close();
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) CorrelationAwareProcessRuntime(org.kie.internal.process.CorrelationAwareProcessRuntime) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) CorrelationKey(org.kie.internal.process.CorrelationKey) CorrelationKeyFactory(org.kie.internal.process.CorrelationKeyFactory) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest) Test(org.junit.Test)

Aggregations

CorrelationKey (org.kie.internal.process.CorrelationKey)38 Test (org.junit.Test)27 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)21 ProcessInstanceDesc (org.jbpm.services.api.model.ProcessInstanceDesc)12 QueryContext (org.kie.api.runtime.query.QueryContext)12 HashMap (java.util.HashMap)10 ArrayList (java.util.ArrayList)9 AbstractKieServicesBaseTest (org.jbpm.kie.test.util.AbstractKieServicesBaseTest)8 UserTaskInstanceDesc (org.jbpm.services.api.model.UserTaskInstanceDesc)7 CorrelationKeyFactory (org.kie.internal.process.CorrelationKeyFactory)7 KModuleDeploymentUnit (org.jbpm.kie.services.impl.KModuleDeploymentUnit)6 KieSession (org.kie.api.runtime.KieSession)6 CaseNotFoundException (org.jbpm.casemgmt.api.CaseNotFoundException)5 Collection (java.util.Collection)4 List (java.util.List)4 Map (java.util.Map)4 Collectors.toList (java.util.stream.Collectors.toList)4 RegistryContext (org.drools.core.command.impl.RegistryContext)4 CaseEventSupport (org.jbpm.casemgmt.impl.event.CaseEventSupport)4 RuntimeDataService (org.jbpm.services.api.RuntimeDataService)4