Skip to content

Commit

Permalink
Always fetch a variable value when returning it after a creation or u…
Browse files Browse the repository at this point in the history
…pdate + add support for fetching specific case instance and plan item instance variables
  • Loading branch information
filiphr committed Jun 28, 2023
1 parent b0ac246 commit f21e9bc
Show file tree
Hide file tree
Showing 25 changed files with 312 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ public interface CmmnRuntimeService {
* when no case instance is found for the given caseInstanceId.
*/
Map<String, Object> getVariables(String caseInstanceId);

/**
* The variable values for all given variableNames.
*
* @param caseInstanceId id of execution, cannot be null.
* @param variableNames the collection of variable names that should be retrieved.
* @return the variables or an empty map if no such variables are found.
* @throws FlowableObjectNotFoundException when no case instance is found for the given caseInstanceId.
*/
Map<String, Object> getVariables(String caseInstanceId, Collection<String> variableNames);

/**
* All variables visible from the given case instance scope.
Expand All @@ -111,6 +121,17 @@ public interface CmmnRuntimeService {
*/
Map<String, Object> getLocalVariables(String planItemInstanceId);

/**
* All variable values for all given variableNames that are defined in the plan item instance scope,
* without taking outer scopes into account.
*
* @param planItemInstanceId id of plan item instance, cannot be null.
* @param variableNames the collection of variable names that should be retrieved.
* @return the variables or an empty map if no such variables are found.
* @throws FlowableObjectNotFoundException when no plan item instance is found for the given planItemInstanceId.
*/
Map<String, Object> getLocalVariables(String planItemInstanceId, Collection<String> variableNames);

/**
* All variable values that are defined in the plan item instance scope, without taking outer scopes into account.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package org.flowable.cmmn.engine.impl.cmd;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -30,9 +31,11 @@
public class GetLocalVariablesCmd implements Command<Map<String, Object>> {

protected String planItemInstanceId;

public GetLocalVariablesCmd(String planItemInstanceId) {
protected Collection<String> variableNames;

public GetLocalVariablesCmd(String planItemInstanceId, Collection<String> variableNames) {
this.planItemInstanceId = planItemInstanceId;
this.variableNames = variableNames;
}

@Override
Expand All @@ -42,8 +45,18 @@ public Map<String, Object> execute(CommandContext commandContext) {
}

CmmnEngineConfiguration cmmnEngineConfiguration = CommandContextUtil.getCmmnEngineConfiguration(commandContext);
List<VariableInstanceEntity> variableInstanceEntities = cmmnEngineConfiguration.getVariableServiceConfiguration().getVariableService()
.findVariableInstanceBySubScopeIdAndScopeType(planItemInstanceId, ScopeTypes.CMMN);
List<VariableInstanceEntity> variableInstanceEntities;
if (variableNames == null || variableNames.isEmpty()) {
variableInstanceEntities = cmmnEngineConfiguration.getVariableServiceConfiguration().getVariableService()
.findVariableInstanceBySubScopeIdAndScopeType(planItemInstanceId, ScopeTypes.CMMN);
} else {
variableInstanceEntities = cmmnEngineConfiguration.getVariableServiceConfiguration().getVariableService()
.createInternalVariableInstanceQuery()
.subScopeId(planItemInstanceId)
.scopeType(ScopeTypes.CMMN)
.names(variableNames)
.list();
}
Map<String, Object> variables = new HashMap<>(variableInstanceEntities.size());
for (VariableInstanceEntity variableInstanceEntity : variableInstanceEntities) {
variables.put(variableInstanceEntity.getName(), variableInstanceEntity.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package org.flowable.cmmn.engine.impl.cmd;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -30,9 +31,11 @@
public class GetVariablesCmd implements Command<Map<String, Object>> {

protected String caseInstanceId;

public GetVariablesCmd(String caseInstanceId) {
protected Collection<String> variableNames;

public GetVariablesCmd(String caseInstanceId, Collection<String> variableNames) {
this.caseInstanceId = caseInstanceId;
this.variableNames = variableNames;
}

@Override
Expand All @@ -42,8 +45,20 @@ public Map<String, Object> execute(CommandContext commandContext) {
}

CmmnEngineConfiguration cmmnEngineConfiguration = CommandContextUtil.getCmmnEngineConfiguration(commandContext);
List<VariableInstanceEntity> variableInstanceEntities = cmmnEngineConfiguration.getVariableServiceConfiguration().getVariableService()
.findVariableInstanceByScopeIdAndScopeType(caseInstanceId, ScopeTypes.CMMN);
List<VariableInstanceEntity> variableInstanceEntities;

if (variableNames == null || variableNames.isEmpty()) {
variableInstanceEntities = cmmnEngineConfiguration.getVariableServiceConfiguration().getVariableService()
.findVariableInstanceByScopeIdAndScopeType(caseInstanceId, ScopeTypes.CMMN);
} else {
variableInstanceEntities = cmmnEngineConfiguration.getVariableServiceConfiguration().getVariableService()
.createInternalVariableInstanceQuery()
.scopeId(caseInstanceId)
.withoutSubScopeId()
.scopeType(ScopeTypes.CMMN)
.names(variableNames)
.list();
}
Map<String, Object> variables = new HashMap<>(variableInstanceEntities.size());
for (VariableInstanceEntity variableInstanceEntity : variableInstanceEntities) {
variables.put(variableInstanceEntity.getName(), variableInstanceEntity.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.flowable.cmmn.engine.impl.runtime;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -202,17 +203,27 @@ public void completeUserEventListenerInstance(String userEventListenerInstanceId

@Override
public Map<String, Object> getVariables(String caseInstanceId) {
return commandExecutor.execute(new GetVariablesCmd(caseInstanceId));
return commandExecutor.execute(new GetVariablesCmd(caseInstanceId, Collections.emptyList()));
}

@Override
public Map<String, Object> getVariables(String caseInstanceId, Collection<String> variableNames) {
return commandExecutor.execute(new GetVariablesCmd(caseInstanceId, variableNames));
}

@Override
public Map<String, VariableInstance> getVariableInstances(String caseInstanceId) {
return commandExecutor.execute(new GetCaseVariableInstancesCmd(caseInstanceId));
}

@Override
public Map<String, Object> getLocalVariables(String planItemInstanceId) {
return commandExecutor.execute(new GetLocalVariablesCmd(planItemInstanceId));
return commandExecutor.execute(new GetLocalVariablesCmd(planItemInstanceId, Collections.emptyList()));
}

@Override
public Map<String, Object> getLocalVariables(String planItemInstanceId, Collection<String> variableNames) {
return commandExecutor.execute(new GetLocalVariablesCmd(planItemInstanceId, variableNames));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@

import static java.util.stream.Collectors.toMap;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -102,6 +104,30 @@ public void testGetVariables() {
assertThat(variableInstance).isNull();
}

@Test
@CmmnDeployment(resources = "org/flowable/cmmn/test/runtime/oneHumanTaskCase.cmmn")
public void testGetSpecificVariablesOnly() {
CaseInstance caseInstance = cmmnRuntimeService.createCaseInstanceBuilder()
.caseDefinitionKey("oneHumanTaskCase")
.variable("stringVar", "Hello World")
.variable("intVar", 42)
.variable("doubleVar", 10.5)
.start();

assertThat(cmmnRuntimeService.getVariables(caseInstance.getId()))
.containsOnly(
entry("stringVar", "Hello World"),
entry("intVar", 42),
entry("doubleVar", 10.5)
);

assertThat(cmmnRuntimeService.getVariables(caseInstance.getId(), Arrays.asList("stringVar", "doubleVar", "dummyVar")))
.containsOnly(
entry("stringVar", "Hello World"),
entry("doubleVar", 10.5)
);
}

@Test
@CmmnDeployment
public void testGetLocalVariables() {
Expand Down Expand Up @@ -148,6 +174,35 @@ public void testGetLocalVariables() {
assertThat(variableInstance.getTypeName()).isEqualTo("integer");
}

@Test
@CmmnDeployment(resources = "org/flowable/cmmn/test/runtime/oneHumanTaskCase.cmmn")
public void testGetSpecificLocalVariables() {
cmmnRuntimeService.createCaseInstanceBuilder()
.caseDefinitionKey("oneHumanTaskCase")
.variable("stringVar", "Hello World")
.variable("intVar", 42)
.variable("doubleVar", 10.5)
.start();

PlanItemInstance planItemInstance = cmmnRuntimeService.createPlanItemInstanceQuery().singleResult();
cmmnRuntimeService.setLocalVariable(planItemInstance.getId(), "stringVar", "Changed value");
cmmnRuntimeService.setLocalVariable(planItemInstance.getId(), "intVar", 21);
cmmnRuntimeService.setLocalVariable(planItemInstance.getId(), "doubleVar", 12.6);

assertThat(cmmnRuntimeService.getLocalVariables(planItemInstance.getId()))
.containsOnly(
entry("stringVar", "Changed value"),
entry("intVar", 21),
entry("doubleVar", 12.6)
);

assertThat(cmmnRuntimeService.getLocalVariables(planItemInstance.getId(), Arrays.asList("stringVar", "doubleVar", "dummyVar")))
.containsOnly(
entry("stringVar", "Changed value"),
entry("doubleVar", 12.6)
);
}

@Test
@CmmnDeployment
public void testSetVariables() {
Expand Down
Loading

0 comments on commit f21e9bc

Please sign in to comment.