Skip to content

Commit c307f4d

Browse files
murselturkfiliphr
authored andcommitted
Consider available conditions when creating variable event listeners (#4037)
1 parent 0bd2407 commit c307f4d

File tree

3 files changed

+57
-9
lines changed

3 files changed

+57
-9
lines changed

modules/flowable-cmmn-engine/src/main/java/org/flowable/cmmn/engine/impl/behavior/impl/VariableEventListenerActivityBehaviour.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,10 @@
4242
*/
4343
public class VariableEventListenerActivityBehaviour extends CoreCmmnTriggerableActivityBehavior implements PlanItemActivityBehavior {
4444

45-
protected String variableName;
46-
protected String variableChangeType;
45+
protected VariableEventListener variableEventListener;
4746

4847
public VariableEventListenerActivityBehaviour(VariableEventListener variableEventListener) {
49-
this.variableName = variableEventListener.getVariableName();
50-
this.variableChangeType = variableEventListener.getVariableChangeType();
48+
this.variableEventListener = variableEventListener;
5149
}
5250

5351
@Override
@@ -62,17 +60,18 @@ public void onStateTransition(CommandContext commandContext, DelegatePlanItemIns
6260
eventSubscriptionService.deleteEventSubscription(eventSubscription);
6361
}
6462

65-
} else if (PlanItemTransition.CREATE.equals(transition)) {
63+
} else if ((PlanItemTransition.CREATE.equals(transition) && StringUtils.isEmpty(variableEventListener.getAvailableConditionExpression()))
64+
|| PlanItemTransition.INITIATE.equals(transition)) {
6665
String configuration = null;
67-
if (StringUtils.isNotEmpty(variableChangeType)) {
66+
if (StringUtils.isNotEmpty(variableEventListener.getVariableChangeType())) {
6867
ObjectNode configurationNode = cmmnEngineConfiguration.getObjectMapper().createObjectNode();
69-
configurationNode.put(VariableListenerEventDefinition.CHANGE_TYPE_PROPERTY, variableChangeType);
68+
configurationNode.put(VariableListenerEventDefinition.CHANGE_TYPE_PROPERTY, variableEventListener.getVariableChangeType());
7069
configuration = configurationNode.toString();
7170
}
7271

7372
cmmnEngineConfiguration.getEventSubscriptionServiceConfiguration().getEventSubscriptionService().createEventSubscriptionBuilder()
7473
.eventType("variable")
75-
.eventName(variableName)
74+
.eventName(variableEventListener.getVariableName())
7675
.configuration(configuration)
7776
.subScopeId(planItemInstance.getId())
7877
.scopeId(planItemInstance.getCaseInstanceId())
@@ -107,7 +106,7 @@ public void trigger(CommandContext commandContext, PlanItemInstanceEntity planIt
107106

108107
List<EventSubscriptionEntity> eventSubscriptions = eventSubscriptionService.findEventSubscriptionsBySubScopeId(planItemInstanceEntity.getId());
109108
for (EventSubscriptionEntity eventSubscription : eventSubscriptions) {
110-
if ("variable".equals(eventSubscription.getEventType()) && variableName.equals(eventSubscription.getEventName())) {
109+
if ("variable".equals(eventSubscription.getEventType()) && variableEventListener.getVariableName().equals(eventSubscription.getEventName())) {
111110
eventSubscriptionService.deleteEventSubscription(eventSubscription);
112111
}
113112
}

modules/flowable-cmmn-engine/src/test/java/org/flowable/cmmn/test/eventlistener/VariableEventListenerTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,32 @@
2929

3030
public class VariableEventListenerTest extends FlowableCmmnTestCase {
3131

32+
@Test
33+
@CmmnDeployment
34+
public void testVariableEventListenerWithAvailableCondition() {
35+
CaseInstance caseInstance = cmmnRuntimeService.createCaseInstanceBuilder().caseDefinitionKey("variableListener").variable("availableConditionVar", false).start();
36+
37+
PlanItemInstance listenerInstance = cmmnRuntimeService.createPlanItemInstanceQuery().planItemDefinitionType(PlanItemDefinitionType.VARIABLE_EVENT_LISTENER).singleResult();
38+
assertThat(listenerInstance.getState()).isEqualTo(PlanItemInstanceState.UNAVAILABLE);
39+
40+
// create var1 variable
41+
cmmnRuntimeService.setVariable(caseInstance.getId(), "var1", "test1");
42+
// should not trigger variable event listener
43+
assertThat(cmmnRuntimeService.createPlanItemInstanceQuery().planItemDefinitionType(PlanItemDefinitionType.VARIABLE_EVENT_LISTENER).count()).isEqualTo(1);
44+
assertThat(cmmnRuntimeService.createPlanItemInstanceQuery().planItemDefinitionId("taskB").planItemInstanceStateActive().count()).isZero();
45+
46+
// setting the variable should make the variable event listener available
47+
cmmnRuntimeService.setVariable(caseInstance.getId(), "availableConditionVar", true);
48+
listenerInstance = cmmnRuntimeService.createPlanItemInstanceQuery().planItemDefinitionType(PlanItemDefinitionType.VARIABLE_EVENT_LISTENER).singleResult();
49+
assertThat(listenerInstance.getState()).isEqualTo(PlanItemInstanceState.AVAILABLE);
50+
51+
// update var1 variable to trigger variable event listener
52+
cmmnRuntimeService.setVariable(caseInstance.getId(), "var1", "test2");
53+
// variable event listener should be completed
54+
assertThat(cmmnRuntimeService.createPlanItemInstanceQuery().planItemDefinitionType(PlanItemDefinitionType.VARIABLE_EVENT_LISTENER).count()).isZero();
55+
assertThat(cmmnRuntimeService.createPlanItemInstanceQuery().planItemDefinitionId("taskB").planItemInstanceStateActive().count()).isEqualTo(1);
56+
}
57+
3258
@Test
3359
@CmmnDeployment
3460
public void testTriggerVariableEventListener() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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="variableListener" name="Variable listener">
6+
<casePlanModel id="casePlanModel">
7+
<planItem id="planItemA" name="A" definitionRef="taskA"/>
8+
<planItem id="variableEventListenerPlanItem" definitionRef="variableEventListener"/>
9+
<planItem id="planItemB" name="B" definitionRef="taskB">
10+
<entryCriterion id="entryTaskB" sentryRef="sentryOnVariableEventListener"/>
11+
</planItem>
12+
<sentry id="sentryOnVariableEventListener">
13+
<planItemOnPart id="sentryOnvariableEvent" sourceRef="variableEventListenerPlanItem">
14+
<standardEvent>occur</standardEvent>
15+
</planItemOnPart>
16+
</sentry>
17+
<humanTask id="taskA" name="A"/>
18+
<eventListener id="variableEventListener" flowable:eventType="variable" flowable:variableName="var1"
19+
flowable:availableCondition="${availableConditionVar}" />
20+
<humanTask id="taskB" name="B"/>
21+
</casePlanModel>
22+
</case>
23+
</definitions>

0 commit comments

Comments
 (0)