Skip to content

Commit 8322438

Browse files
committed
Dispatch business status updated events from the entity managers
This makes the events fire for the process and case instance update builders as well.
1 parent 95668dd commit 8322438

6 files changed

Lines changed: 107 additions & 19 deletions

File tree

modules/flowable-cmmn-engine/src/main/java/org/flowable/cmmn/engine/impl/cmd/SetCaseInstanceBusinessStatusCmd.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,13 @@
1515
import java.io.Serializable;
1616

1717
import org.flowable.cmmn.api.runtime.CaseInstance;
18-
import org.flowable.cmmn.engine.impl.event.FlowableCmmnEventBuilder;
1918
import org.flowable.cmmn.engine.impl.persistence.entity.CaseInstanceEntity;
2019
import org.flowable.cmmn.engine.impl.persistence.entity.CaseInstanceEntityManager;
2120
import org.flowable.cmmn.engine.impl.util.CommandContextUtil;
2221
import org.flowable.common.engine.api.FlowableIllegalArgumentException;
2322
import org.flowable.common.engine.api.FlowableObjectNotFoundException;
24-
import org.flowable.common.engine.api.delegate.event.FlowableEventDispatcher;
2523
import org.flowable.common.engine.impl.interceptor.Command;
2624
import org.flowable.common.engine.impl.interceptor.CommandContext;
27-
import org.flowable.common.engine.impl.interceptor.EngineConfigurationConstants;
2825

2926
public class SetCaseInstanceBusinessStatusCmd implements Command<Void>, Serializable {
3027

@@ -50,14 +47,8 @@ public Void execute(CommandContext commandContext) {
5047
throw new FlowableObjectNotFoundException("No case instance found for id = '" + caseInstanceId + "'.", CaseInstance.class);
5148
}
5249

53-
String oldBusinessStatus = caseInstanceEntity.getBusinessStatus();
5450
caseInstanceEntityManager.updateCaseInstanceBusinessStatus(caseInstanceEntity, businessStatus);
5551

56-
FlowableEventDispatcher eventDispatcher = CommandContextUtil.getEventDispatcher(commandContext);
57-
if (eventDispatcher != null && eventDispatcher.isEnabled()) {
58-
eventDispatcher.dispatchEvent(FlowableCmmnEventBuilder.createCaseBusinessStatusUpdatedEvent(caseInstanceEntity, oldBusinessStatus, businessStatus), EngineConfigurationConstants.KEY_CMMN_ENGINE_CONFIG);
59-
}
60-
6152
return null;
6253
}
6354
}

modules/flowable-cmmn-engine/src/main/java/org/flowable/cmmn/engine/impl/persistence/entity/CaseInstanceEntityManagerImpl.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@
2525
import org.flowable.cmmn.api.runtime.PlanItemInstanceState;
2626
import org.flowable.cmmn.engine.CmmnEngineConfiguration;
2727
import org.flowable.cmmn.engine.impl.behavior.impl.ChildTaskActivityBehavior;
28+
import org.flowable.cmmn.engine.impl.event.FlowableCmmnEventBuilder;
2829
import org.flowable.cmmn.engine.impl.persistence.entity.data.CaseInstanceDataManager;
2930
import org.flowable.cmmn.engine.impl.runtime.CaseInstanceQueryImpl;
3031
import org.flowable.cmmn.engine.impl.task.TaskHelper;
3132
import org.flowable.cmmn.engine.impl.util.CommandContextUtil;
33+
import org.flowable.common.engine.api.delegate.event.FlowableEventDispatcher;
3234
import org.flowable.common.engine.api.scope.ScopeTypes;
3335
import org.flowable.common.engine.impl.interceptor.CommandContext;
3436
import org.flowable.common.engine.impl.persistence.entity.AbstractEngineEntityManager;
@@ -263,8 +265,16 @@ public void updateCaseInstanceBusinessKey(CaseInstanceEntity caseInstanceEntity,
263265
@Override
264266
public void updateCaseInstanceBusinessStatus(CaseInstanceEntity caseInstanceEntity, String businessStatus) {
265267
if (businessStatus != null) {
268+
String oldBusinessStatus = caseInstanceEntity.getBusinessStatus();
266269
caseInstanceEntity.setBusinessStatus(businessStatus);
267270
engineConfiguration.getCmmnHistoryManager().recordUpdateBusinessStatus(caseInstanceEntity, businessStatus);
271+
272+
FlowableEventDispatcher eventDispatcher = getEventDispatcher();
273+
if (eventDispatcher != null && eventDispatcher.isEnabled()) {
274+
eventDispatcher.dispatchEvent(
275+
FlowableCmmnEventBuilder.createCaseBusinessStatusUpdatedEvent(caseInstanceEntity, oldBusinessStatus, businessStatus),
276+
engineConfiguration.getEngineCfgKey());
277+
}
268278
}
269279
}
270280

modules/flowable-cmmn-engine/src/test/java/org/flowable/cmmn/test/event/CaseBusinessStatusUpdatedEventTest.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,51 @@ public void testBusinessStatusUpdatedEvent() {
100100
assertThat(events).hasSize(1);
101101
}
102102

103+
@Test
104+
@CmmnDeployment(resources = "org/flowable/cmmn/test/runtime/oneTaskCase.cmmn")
105+
public void testBusinessStatusUpdatedEventWithUpdateBuilder() {
106+
List<FlowableEvent> events = new ArrayList<>();
107+
businessStatusUpdatedEventListener.eventConsumer = (flowableEvent) -> {
108+
if (flowableEvent instanceof FlowableCaseBusinessStatusUpdatedEvent caseBusinessStatusUpdatedEvent) {
109+
CaseInstance eventCaseInstance = (CaseInstance) caseBusinessStatusUpdatedEvent.getEntity();
110+
assertThat(caseBusinessStatusUpdatedEvent.getScopeType()).isEqualTo(ScopeTypes.CMMN);
111+
assertThat(caseBusinessStatusUpdatedEvent.getScopeId()).isNotNull().isEqualTo(eventCaseInstance.getId());
112+
assertThat(caseBusinessStatusUpdatedEvent.getOldBusinessStatus()).isEqualTo("oldStatus");
113+
assertThat(caseBusinessStatusUpdatedEvent.getNewBusinessStatus()).isEqualTo("newStatus");
114+
events.add(flowableEvent);
115+
}
116+
};
117+
118+
CaseInstance caseInstance = cmmnRuntimeService.createCaseInstanceBuilder()
119+
.caseDefinitionKey("oneTaskCase")
120+
.businessStatus("oldStatus")
121+
.start();
122+
123+
cmmnRuntimeService.createCaseInstanceUpdateBuilder(caseInstance.getId())
124+
.businessStatus("newStatus")
125+
.update();
126+
127+
assertThat(events).hasSize(1);
128+
}
129+
130+
@Test
131+
@CmmnDeployment(resources = "org/flowable/cmmn/test/runtime/oneTaskCase.cmmn")
132+
public void testNoBusinessStatusUpdatedEventWhenUpdateBuilderDoesNotSetBusinessStatus() {
133+
List<FlowableEvent> events = new ArrayList<>();
134+
businessStatusUpdatedEventListener.eventConsumer = events::add;
135+
136+
CaseInstance caseInstance = cmmnRuntimeService.createCaseInstanceBuilder()
137+
.caseDefinitionKey("oneTaskCase")
138+
.businessStatus("oldStatus")
139+
.start();
140+
141+
cmmnRuntimeService.createCaseInstanceUpdateBuilder(caseInstance.getId())
142+
.name("newName")
143+
.update();
144+
145+
assertThat(events).isEmpty();
146+
}
147+
103148
public static class CustomEventListener extends AbstractFlowableEventListener {
104149

105150
private Consumer<FlowableEvent> eventConsumer;

modules/flowable-engine/src/main/java/org/flowable/engine/impl/cmd/SetProcessInstanceBusinessStatusCmd.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,8 @@
1818

1919
import org.flowable.common.engine.api.FlowableIllegalArgumentException;
2020
import org.flowable.common.engine.api.FlowableObjectNotFoundException;
21-
import org.flowable.common.engine.api.delegate.event.FlowableEventDispatcher;
2221
import org.flowable.common.engine.impl.interceptor.Command;
2322
import org.flowable.common.engine.impl.interceptor.CommandContext;
24-
import org.flowable.common.engine.impl.interceptor.EngineConfigurationConstants;
25-
import org.flowable.engine.delegate.event.impl.FlowableEventBuilder;
2623
import org.flowable.engine.impl.persistence.entity.ExecutionEntity;
2724
import org.flowable.engine.impl.persistence.entity.ExecutionEntityManager;
2825
import org.flowable.engine.impl.util.CommandContextUtil;
@@ -64,13 +61,8 @@ public Void execute(CommandContext commandContext) {
6461
+ processInstance.getProcessInstanceId() + "'. " + "Please invoke the " + getClass().getSimpleName() + " with a root execution id.");
6562
}
6663

67-
String oldBusinessStatus = processInstance.getBusinessStatus();
6864
executionManager.updateProcessInstanceBusinessStatus(processInstance, businessStatus);
6965

70-
FlowableEventDispatcher eventDispatcher = CommandContextUtil.getEventDispatcher(commandContext);
71-
if (eventDispatcher != null && eventDispatcher.isEnabled()) {
72-
eventDispatcher.dispatchEvent(FlowableEventBuilder.createProcessBusinessStatusUpdatedEvent(processInstance, oldBusinessStatus, businessStatus), EngineConfigurationConstants.KEY_PROCESS_ENGINE_CONFIG);
73-
}
7466
return null;
7567
}
7668
}

modules/flowable-engine/src/main/java/org/flowable/engine/impl/persistence/entity/ExecutionEntityManagerImpl.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,11 +1114,16 @@ public String updateProcessInstanceBusinessKey(ExecutionEntity executionEntity,
11141114
@Override
11151115
public String updateProcessInstanceBusinessStatus(ExecutionEntity executionEntity, String businessStatus) {
11161116
if (executionEntity.isProcessInstanceType() && businessStatus != null) {
1117+
String oldBusinessStatus = executionEntity.getBusinessStatus();
11171118
executionEntity.setBusinessStatus(businessStatus);
11181119
getHistoryManager().updateProcessBusinessStatusInHistory(executionEntity);
11191120

1120-
if (getEventDispatcher() != null && getEventDispatcher().isEnabled()) {
1121-
getEventDispatcher().dispatchEvent(FlowableEventBuilder.createEntityEvent(FlowableEngineEventType.ENTITY_UPDATED, executionEntity),
1121+
FlowableEventDispatcher eventDispatcher = getEventDispatcher();
1122+
if (eventDispatcher != null && eventDispatcher.isEnabled()) {
1123+
eventDispatcher.dispatchEvent(FlowableEventBuilder.createEntityEvent(FlowableEngineEventType.ENTITY_UPDATED, executionEntity),
1124+
engineConfiguration.getEngineCfgKey());
1125+
eventDispatcher.dispatchEvent(
1126+
FlowableEventBuilder.createProcessBusinessStatusUpdatedEvent(executionEntity, oldBusinessStatus, businessStatus),
11221127
engineConfiguration.getEngineCfgKey());
11231128
}
11241129

modules/flowable-engine/src/test/java/org/flowable/engine/test/api/event/FlowableProcessBusinessStatusUpdatedEventTest.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,51 @@ public void testBusinessStatusUpdatedEvent() {
8888
assertThat(events).hasSize(1);
8989
}
9090

91+
@Test
92+
@Deployment(resources = "org/flowable/engine/test/api/runtime/oneTaskProcess.bpmn20.xml")
93+
public void testBusinessStatusUpdatedEventWithUpdateBuilder() {
94+
List<FlowableEvent> events = new ArrayList<>();
95+
businessStatusUpdatedEventListener.eventConsumer = (flowableEvent) -> {
96+
if (flowableEvent instanceof FlowableProcessBusinessStatusUpdatedEvent processBusinessStatusUpdatedEvent) {
97+
Execution execution = (Execution) processBusinessStatusUpdatedEvent.getEntity();
98+
assertThat(processBusinessStatusUpdatedEvent.getScopeType()).isEqualTo(ScopeTypes.BPMN);
99+
assertThat(processBusinessStatusUpdatedEvent.getScopeId()).isNotNull().isEqualTo(execution.getId());
100+
assertThat(processBusinessStatusUpdatedEvent.getOldBusinessStatus()).isEqualTo("oldStatus");
101+
assertThat(processBusinessStatusUpdatedEvent.getNewBusinessStatus()).isEqualTo("newStatus");
102+
events.add(flowableEvent);
103+
}
104+
};
105+
106+
ProcessInstance processInstance = runtimeService.createProcessInstanceBuilder()
107+
.processDefinitionKey("oneTaskProcess")
108+
.businessStatus("oldStatus")
109+
.start();
110+
111+
runtimeService.createProcessInstanceUpdateBuilder(processInstance.getId())
112+
.businessStatus("newStatus")
113+
.update();
114+
115+
assertThat(events).hasSize(1);
116+
}
117+
118+
@Test
119+
@Deployment(resources = "org/flowable/engine/test/api/runtime/oneTaskProcess.bpmn20.xml")
120+
public void testNoBusinessStatusUpdatedEventWhenUpdateBuilderDoesNotSetBusinessStatus() {
121+
List<FlowableEvent> events = new ArrayList<>();
122+
businessStatusUpdatedEventListener.eventConsumer = events::add;
123+
124+
ProcessInstance processInstance = runtimeService.createProcessInstanceBuilder()
125+
.processDefinitionKey("oneTaskProcess")
126+
.businessStatus("oldStatus")
127+
.start();
128+
129+
runtimeService.createProcessInstanceUpdateBuilder(processInstance.getId())
130+
.name("newName")
131+
.update();
132+
133+
assertThat(events).isEmpty();
134+
}
135+
91136
public static class CustomEventListener extends AbstractFlowableEventListener {
92137

93138
private Consumer<FlowableEvent> eventConsumer;

0 commit comments

Comments
 (0)