Skip to content

Commit 5beefc4

Browse files
committed
Expose plain values for JSON array elements in multi instance and repetition collections
JSON value nodes (text, number, boolean, null) used as the element variable of a BPMN multi instance or CMMN collection repetition are now set as plain Java values. Jackson value node accessor methods (e.g. asText(), asString(), asInt(), isTextual()) keep working on String, Number and Boolean values through a new EL resolver.
1 parent 2e60dd1 commit 5beefc4

15 files changed

Lines changed: 885 additions & 2 deletions

File tree

‎docs/docusaurus/docs/bpmn/ch07b-BPMN-Constructs.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3940,6 +3940,8 @@ For example, assuming the myService.getCollectionVariableName() returns a String
39403940
</multiInstanceLoopCharacteristics>
39413941
</userTask>
39423942

3943+
The collection can also be a JSON array. Elements of a JSON array that are plain values are exposed as plain Java values in the element variable: a JSON string becomes a String, a JSON number becomes a number (e.g. Integer, Long or Double), a JSON boolean becomes a Boolean and a JSON null becomes null. For example, when assigneeList is the JSON array `["kermit", "gonzo"]`, the assignee variable is the String kermit (respectively gonzo), so it can be used directly for the assignee or compared in an expression such as `${assignee == 'kermit'}`. JSON objects and arrays are exposed as JSON. For compatibility, the Jackson accessor methods (e.g. `${assignee.asText()}`, `${assignee.asString()}`, `${assignee.textValue()}`, `${assignee.asInt()}`, `${assignee.booleanValue()}` or `${assignee.isTextual()}`) can still be used on such element variables in expressions.
3944+
39433945
A multi-instance activity ends when all instances are finished. However, it is possible to specify an expression that is evaluated every time an instance ends. When this expression evaluates to true, all remaining instances are destroyed and the multi-instance activity ends, continuing the process. Such an expression must be defined in the **completionCondition** child element.
39443946

39453947
<userTask id="miTasks" name="My Task" flowable:assignee="${assignee}">

‎docs/docusaurus/docs/cmmn/ch06-cmmn.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,8 @@ Here is an example of a repetition rule combined with a collection variable:
11131113
The example uses a collection variable named *myCollection* and has an element and even element index variable specified. They both get used for the plan item instance name used in its name expression.
11141114
As there is no entry sentry, the collection gets evaluated in each evaluation cycle and as soon as *myCollection* is no longer null, it is used for repetition.
11151115

1116+
The collection can also be a JSON array. Elements of a JSON array that are plain values are exposed as plain Java values in the element variable: a JSON string becomes a String, a JSON number becomes a number (e.g. Integer, Long or Double), a JSON boolean becomes a Boolean and a JSON null becomes null. This means that for a *myCollection* JSON array `["A", "B"]` the names in the example above are *Task (A - 0)* and *Task (B - 1)*. JSON objects and arrays are exposed as JSON. For compatibility, the Jackson accessor methods (e.g. `${item.asText()}`, `${item.asString()}`, `${item.textValue()}`, `${item.asInt()}`, `${item.booleanValue()}` or `${item.isTextual()}`) can still be used on such element variables in expressions.
1117+
11161118
### Item control: Manual Activation Rule
11171119

11181120
Plan items on the case model can have a *manual activation rule*: an expression that can be used to indicate a certain plan item needs to be *manually activated by an end-user*.

‎modules/flowable-cmmn-engine/src/main/java/org/flowable/cmmn/engine/impl/util/PlanItemInstanceUtil.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.flowable.cmmn.model.PlanItem;
2626
import org.flowable.cmmn.model.RepetitionRule;
2727
import org.flowable.common.engine.impl.interceptor.CommandContext;
28+
import org.flowable.common.engine.impl.util.JsonUtil;
2829

2930
public class PlanItemInstanceUtil {
3031

@@ -209,7 +210,8 @@ protected static PlanItemInstanceEntity createPlanItemInstanceDuplicateForCollec
209210
// check, if we need to set local variables as the item or item index
210211
Map<String, Object> localVariables = new HashMap<>(2);
211212
if (repetitionRule.hasElementVariable()) {
212-
localVariables.put(repetitionRule.getElementVariableName(), item);
213+
// JSON value nodes (e.g. elements of a JSON array) are exposed as plain Java values
214+
localVariables.put(repetitionRule.getElementVariableName(), JsonUtil.unwrapJsonValueNode(item));
213215
}
214216
if (repetitionRule.hasElementIndexVariable()) {
215217
localVariables.put(repetitionRule.getElementIndexVariableName(), index);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/* Licensed under the Apache License, Version 2.0 (the "License");
2+
* you may not use this file except in compliance with the License.
3+
* You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*/
13+
package org.flowable.cmmn.test.itemcontrol;
14+
15+
import static org.assertj.core.api.Assertions.assertThat;
16+
import static org.assertj.core.api.Assertions.tuple;
17+
import static org.flowable.cmmn.api.runtime.PlanItemInstanceState.ACTIVE;
18+
19+
import java.util.List;
20+
21+
import org.flowable.cmmn.api.runtime.CaseInstance;
22+
import org.flowable.cmmn.api.runtime.PlanItemInstance;
23+
import org.flowable.cmmn.engine.test.CmmnDeployment;
24+
import org.flowable.cmmn.test.FlowableCmmnTestCase;
25+
import org.flowable.task.api.Task;
26+
import org.junit.jupiter.api.Test;
27+
28+
import tools.jackson.databind.node.ArrayNode;
29+
30+
/**
31+
* @author Filip Hrisafov
32+
*/
33+
public class PlanItemRepetitionWithJsonArrayCollectionTest extends FlowableCmmnTestCase {
34+
35+
@Test
36+
@CmmnDeployment
37+
public void repetitionWithStringItems() {
38+
ArrayNode collection = cmmnEngineConfiguration.getObjectMapper().createArrayNode()
39+
.add("A")
40+
.add("B");
41+
42+
CaseInstance caseInstance = cmmnRuntimeService.createCaseInstanceBuilder()
43+
.caseDefinitionKey("repetitionWithJsonArray")
44+
.variable("myCollection", collection)
45+
.start();
46+
47+
List<PlanItemInstance> planItemInstances = getPlanItemInstances(caseInstance.getId());
48+
assertThat(planItemInstances).hasSize(2);
49+
assertPlanItemInstanceState(planItemInstances, "Task (A)", ACTIVE);
50+
assertPlanItemInstanceState(planItemInstances, "Task (B)", ACTIVE);
51+
52+
for (PlanItemInstance planItemInstance : planItemInstances) {
53+
assertThat(cmmnRuntimeService.getLocalVariable(planItemInstance.getId(), "item")).isInstanceOf(String.class);
54+
}
55+
56+
assertThat(cmmnTaskService.createTaskQuery().caseInstanceId(caseInstance.getId()).list())
57+
.extracting(Task::getName, Task::getAssignee)
58+
.containsExactlyInAnyOrder(
59+
tuple("Task (A)", "A"),
60+
tuple("Task (B)", "B")
61+
);
62+
}
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<definitions xmlns="http://www.omg.org/spec/CMMN/20151109/MODEL"
3+
xmlns:flowable="http://flowable.org/cmmn"
4+
targetNamespace="http://flowable.org/cmmn">
5+
<case id="repetitionWithJsonArray" name="Repetition with JSON array">
6+
<casePlanModel id="casePlanModel" name="Case plan model">
7+
<planItem id="planItem1" name="Task (${vars:getOrDefault('item', 'na')})" definitionRef="humanTask1">
8+
<itemControl>
9+
<repetitionRule flowable:collectionVariable="myCollection" flowable:elementVariable="item" flowable:elementIndexVariable="itemIndex" />
10+
</itemControl>
11+
</planItem>
12+
<humanTask id="humanTask1" name="Task (${vars:getOrDefault('item', 'na')})" flowable:assignee="${item}" />
13+
</casePlanModel>
14+
</case>
15+
</definitions>

‎modules/flowable-engine-common/src/main/java/org/flowable/common/engine/impl/el/DefaultExpressionManager.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ protected List<ELResolver> createDefaultElResolvers() {
141141
if (jsonNodeElResolver != null) {
142142
elResolvers.add(jsonNodeElResolver);
143143
}
144+
ELResolver jsonValueNodeMethodsElResolver = createJsonValueNodeMethodsElResolver();
145+
if (jsonValueNodeMethodsElResolver != null) {
146+
elResolvers.add(jsonValueNodeMethodsElResolver);
147+
}
144148
if (preBeanResolvers != null) {
145149
elResolvers.addAll(preBeanResolvers);
146150
}
@@ -167,6 +171,11 @@ protected ELResolver createJsonNodeElResolver() {
167171
return jsonNodeResolver == null ? new JsonNodeELResolver() : jsonNodeResolver;
168172
}
169173

174+
protected ELResolver createJsonValueNodeMethodsElResolver() {
175+
// Supports invoking JSON value node methods (e.g. asText()) on plain String, Number and Boolean values
176+
return new JsonValueNodeMethodsELResolver();
177+
}
178+
170179
protected ELResolver createBeanElResolver() {
171180
return beanResolver == null ? new BeanELResolver() : beanResolver;
172181
}
Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
1+
/* Licensed under the Apache License, Version 2.0 (the "License");
2+
* you may not use this file except in compliance with the License.
3+
* You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*/
13+
package org.flowable.common.engine.impl.el;
14+
15+
import java.math.BigDecimal;
16+
import java.math.BigInteger;
17+
18+
import org.flowable.common.engine.impl.javax.el.ELContext;
19+
import org.flowable.common.engine.impl.javax.el.ELResolver;
20+
21+
/**
22+
* An {@link ELResolver} that supports invoking the Jackson (2 and 3) value node accessor methods
23+
* (e.g. {@code asText()}, {@code asString()}, {@code textValue()}, {@code asInt()}, {@code isTextual()})
24+
* on plain {@link String}, {@link Number} and {@link Boolean} values.
25+
* <p>
26+
* Plain values are used in places where JSON value nodes used to be exposed
27+
* (e.g. the element variable of a multi instance or repetition collection that is a JSON array).
28+
* This resolver keeps expressions that call such methods working.
29+
* The methods behave like they do on the corresponding Jackson 2 value node,
30+
* which is the lenient superset of the Jackson 3 behavior.
31+
* Methods that are not supported by this resolver are left to the other resolvers.
32+
*
33+
* @author Filip Hrisafov
34+
*/
35+
public class JsonValueNodeMethodsELResolver extends ELResolver {
36+
37+
protected static final Object NOT_RESOLVED = new Object();
38+
39+
@Override
40+
public Object getValue(ELContext context, Object base, Object property) {
41+
return null;
42+
}
43+
44+
@Override
45+
public Class<?> getType(ELContext context, Object base, Object property) {
46+
return null;
47+
}
48+
49+
@Override
50+
public void setValue(ELContext context, Object base, Object property, Object value) {
51+
// Nothing to do, this resolver only supports invoking methods
52+
}
53+
54+
@Override
55+
public boolean isReadOnly(ELContext context, Object base, Object property) {
56+
return false;
57+
}
58+
59+
@Override
60+
public Class<?> getCommonPropertyType(ELContext context, Object base) {
61+
return null;
62+
}
63+
64+
@Override
65+
public Object invoke(ELContext context, Object base, Object method, Class<?>[] paramTypes, Object[] params) {
66+
if (method == null || !isResolvable(base)) {
67+
return null;
68+
}
69+
70+
Object result;
71+
if (params == null || params.length == 0) {
72+
result = invokeWithoutParameters(base, method.toString());
73+
} else if (params.length == 1) {
74+
result = invokeWithDefaultValue(base, method.toString(), params[0]);
75+
} else {
76+
result = NOT_RESOLVED;
77+
}
78+
79+
if (result == NOT_RESOLVED) {
80+
return null;
81+
}
82+
83+
context.setPropertyResolved(true);
84+
return result;
85+
}
86+
87+
protected Object invokeWithoutParameters(Object base, String methodName) {
88+
return switch (methodName) {
89+
case "asText", "asString" -> base.toString();
90+
case "textValue", "stringValue" -> base instanceof String ? base : null;
91+
case "asInt" -> asInt(base, 0);
92+
case "asLong" -> asLong(base, 0L);
93+
case "asDouble" -> asDouble(base, 0.0);
94+
case "asBoolean" -> asBoolean(base, false);
95+
case "intValue" -> base instanceof Number number ? number.intValue() : 0;
96+
case "longValue" -> base instanceof Number number ? number.longValue() : 0L;
97+
case "doubleValue" -> base instanceof Number number ? number.doubleValue() : 0.0;
98+
case "floatValue" -> base instanceof Number number ? number.floatValue() : 0.0f;
99+
case "decimalValue" -> base instanceof Number number ? toBigDecimal(number) : BigDecimal.ZERO;
100+
case "bigIntegerValue" -> base instanceof Number number ? toBigDecimal(number).toBigInteger() : BigInteger.ZERO;
101+
case "numberValue" -> base instanceof Number ? base : null;
102+
case "booleanValue" -> base instanceof Boolean ? base : Boolean.FALSE;
103+
case "isTextual", "isString" -> base instanceof String;
104+
case "isNumber" -> base instanceof Number;
105+
case "isInt" -> base instanceof Integer;
106+
case "isLong" -> base instanceof Long;
107+
case "isDouble" -> base instanceof Double;
108+
case "isBigDecimal" -> base instanceof BigDecimal;
109+
case "isBigInteger" -> base instanceof BigInteger;
110+
case "isBoolean" -> base instanceof Boolean;
111+
case "isValueNode" -> true;
112+
case "isNull", "isMissingNode", "isContainerNode", "isContainer", "isObject", "isArray" -> false;
113+
default -> NOT_RESOLVED;
114+
};
115+
}
116+
117+
protected Object invokeWithDefaultValue(Object base, String methodName, Object defaultValue) {
118+
return switch (methodName) {
119+
// Jackson returns the default value only for null or missing nodes
120+
case "asText", "asString" -> base.toString();
121+
case "asInt" -> defaultValue instanceof Number number ? asInt(base, number.intValue()) : NOT_RESOLVED;
122+
case "asLong" -> defaultValue instanceof Number number ? asLong(base, number.longValue()) : NOT_RESOLVED;
123+
case "asDouble" -> defaultValue instanceof Number number ? asDouble(base, number.doubleValue()) : NOT_RESOLVED;
124+
case "asBoolean" -> defaultValue instanceof Boolean booleanValue ? asBoolean(base, booleanValue) : NOT_RESOLVED;
125+
default -> NOT_RESOLVED;
126+
};
127+
}
128+
129+
protected int asInt(Object base, int defaultValue) {
130+
if (base instanceof Number number) {
131+
return number.intValue();
132+
} else if (base instanceof Boolean booleanValue) {
133+
return booleanValue ? 1 : 0;
134+
}
135+
136+
String value = ((String) base).trim();
137+
if (value.isEmpty()) {
138+
return defaultValue;
139+
}
140+
141+
if (isIntegral(value)) {
142+
try {
143+
return Integer.parseInt(value);
144+
} catch (NumberFormatException e) {
145+
return defaultValue;
146+
}
147+
}
148+
149+
try {
150+
return (int) Double.parseDouble(value);
151+
} catch (NumberFormatException e) {
152+
return defaultValue;
153+
}
154+
}
155+
156+
protected long asLong(Object base, long defaultValue) {
157+
if (base instanceof Number number) {
158+
return number.longValue();
159+
} else if (base instanceof Boolean booleanValue) {
160+
return booleanValue ? 1L : 0L;
161+
}
162+
163+
String value = ((String) base).trim();
164+
if (value.isEmpty()) {
165+
return defaultValue;
166+
}
167+
168+
if (isIntegral(value)) {
169+
try {
170+
return Long.parseLong(value);
171+
} catch (NumberFormatException e) {
172+
return defaultValue;
173+
}
174+
}
175+
176+
try {
177+
return (long) Double.parseDouble(value);
178+
} catch (NumberFormatException e) {
179+
return defaultValue;
180+
}
181+
}
182+
183+
protected double asDouble(Object base, double defaultValue) {
184+
if (base instanceof Number number) {
185+
return number.doubleValue();
186+
} else if (base instanceof Boolean booleanValue) {
187+
return booleanValue ? 1.0 : 0.0;
188+
}
189+
190+
String value = ((String) base).trim();
191+
if (value.isEmpty()) {
192+
return defaultValue;
193+
}
194+
195+
try {
196+
return Double.parseDouble(value);
197+
} catch (NumberFormatException e) {
198+
return defaultValue;
199+
}
200+
}
201+
202+
protected boolean asBoolean(Object base, boolean defaultValue) {
203+
if (base instanceof Boolean booleanValue) {
204+
return booleanValue;
205+
} else if (base instanceof String string) {
206+
String value = string.trim();
207+
if ("true".equals(value)) {
208+
return true;
209+
} else if ("false".equals(value)) {
210+
return false;
211+
}
212+
return defaultValue;
213+
} else if (base instanceof BigInteger bigInteger) {
214+
return bigInteger.signum() != 0;
215+
} else if (base instanceof Integer || base instanceof Long || base instanceof Short || base instanceof Byte) {
216+
return ((Number) base).longValue() != 0;
217+
}
218+
219+
// Jackson returns the default value for floating point numbers
220+
return defaultValue;
221+
}
222+
223+
protected boolean isIntegral(String value) {
224+
int start = value.charAt(0) == '-' || value.charAt(0) == '+' ? 1 : 0;
225+
if (start == value.length()) {
226+
return false;
227+
}
228+
for (int i = start; i < value.length(); i++) {
229+
char c = value.charAt(i);
230+
if (c < '0' || c > '9') {
231+
return false;
232+
}
233+
}
234+
return true;
235+
}
236+
237+
protected BigDecimal toBigDecimal(Number number) {
238+
if (number instanceof BigDecimal bigDecimal) {
239+
return bigDecimal;
240+
} else if (number instanceof BigInteger bigInteger) {
241+
return new BigDecimal(bigInteger);
242+
} else if (number instanceof Double || number instanceof Float) {
243+
return BigDecimal.valueOf(number.doubleValue());
244+
}
245+
return BigDecimal.valueOf(number.longValue());
246+
}
247+
248+
protected boolean isResolvable(Object base) {
249+
return base instanceof String || base instanceof Number || base instanceof Boolean;
250+
}
251+
}

0 commit comments

Comments
 (0)