Skip to content

Commit 1a6055b

Browse files
polish and added afterEnd methods for both interceptors.
also added terminate flag that is used to determine if a process/case ended or terminated.
1 parent da6f618 commit 1a6055b

8 files changed

Lines changed: 88 additions & 20 deletions

File tree

modules/flowable-cmmn-engine/src/main/java/org/flowable/cmmn/engine/impl/agenda/operation/CompleteCaseInstanceOperation.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,14 @@ public CompleteCaseInstanceOperation(CommandContext commandContext, CaseInstance
4141
public void internalExecute() {
4242
CmmnEngineConfiguration cmmnEngineConfiguration = CommandContextUtil.getCmmnEngineConfiguration(commandContext);
4343
if (cmmnEngineConfiguration.getEndCaseInstanceInterceptor() != null) {
44-
cmmnEngineConfiguration.getEndCaseInstanceInterceptor().beforeEndCaseInstance(caseInstanceEntity);
44+
cmmnEngineConfiguration.getEndCaseInstanceInterceptor().beforeEndCaseInstance(caseInstanceEntity, false);
4545
}
4646

4747
super.internalExecute();
48+
49+
if (cmmnEngineConfiguration.getEndCaseInstanceInterceptor() != null) {
50+
cmmnEngineConfiguration.getEndCaseInstanceInterceptor().afterEndCaseInstance(caseInstanceEntity.getId(), false);
51+
}
4852
}
4953

5054
@Override

modules/flowable-cmmn-engine/src/main/java/org/flowable/cmmn/engine/impl/agenda/operation/TerminateCaseInstanceOperation.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.HashMap;
2121

2222
import org.flowable.cmmn.api.runtime.CaseInstanceState;
23+
import org.flowable.cmmn.engine.CmmnEngineConfiguration;
2324
import org.flowable.cmmn.engine.impl.behavior.OnParentEndDependantActivityBehavior;
2425
import org.flowable.cmmn.engine.impl.callback.CallbackConstants;
2526
import org.flowable.cmmn.engine.impl.event.FlowableCmmnEventBuilder;
@@ -54,6 +55,19 @@ public TerminateCaseInstanceOperation(CommandContext commandContext, String case
5455
this.exitEventType = exitEventType;
5556
}
5657

58+
@Override
59+
public void internalExecute() {
60+
CmmnEngineConfiguration cmmnEngineConfiguration = CommandContextUtil.getCmmnEngineConfiguration(commandContext);
61+
if (cmmnEngineConfiguration.getEndCaseInstanceInterceptor() != null) {
62+
cmmnEngineConfiguration.getEndCaseInstanceInterceptor().beforeEndCaseInstance(caseInstanceEntity, true);
63+
}
64+
super.internalExecute();
65+
66+
if (cmmnEngineConfiguration.getEndCaseInstanceInterceptor() != null) {
67+
cmmnEngineConfiguration.getEndCaseInstanceInterceptor().afterEndCaseInstance(caseInstanceEntity.getId(), true);
68+
}
69+
}
70+
5771
/**
5872
* Overridden to check, if the optional exit event type is set to 'complete' and if so, throw an exception, if the case is not yet completable.
5973
*/

modules/flowable-cmmn-engine/src/main/java/org/flowable/cmmn/engine/interceptor/EndCaseInstanceInterceptor.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,7 @@
1616

1717
public interface EndCaseInstanceInterceptor {
1818

19-
void beforeEndCaseInstance(CaseInstanceEntity caseInstance);
19+
void beforeEndCaseInstance(CaseInstanceEntity caseInstance, boolean isTerminated);
20+
21+
void afterEndCaseInstance(String caseInstanceId, boolean isTerminated);
2022
}

modules/flowable-cmmn-engine/src/test/java/org/flowable/cmmn/test/runtime/CaseInstanceEndInterceptorTest.java

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,16 @@ public class CaseInstanceEndInterceptorTest extends FlowableCmmnTestCase {
3131
@CmmnDeployment(resources = "org/flowable/cmmn/test/runtime/oneHumanTaskCase.cmmn")
3232
public void testEndProcessInterceptorIsCalled() {
3333
try {
34-
TestEndCaseInstanceInterceptor testEndProcessInstanceInterceptor = new TestEndCaseInstanceInterceptor();
35-
cmmnEngineConfiguration.setEndCaseInstanceInterceptor(testEndProcessInstanceInterceptor);
34+
TestEndCaseInstanceInterceptor testEndCaseInstanceInterceptor = new TestEndCaseInstanceInterceptor();
35+
cmmnEngineConfiguration.setEndCaseInstanceInterceptor(testEndCaseInstanceInterceptor);
3636

3737
cmmnRuntimeService.createCaseInstanceBuilder().caseDefinitionKey("oneHumanTaskCase").start();
3838
cmmnTaskService.complete(cmmnTaskService.createTaskQuery().singleResult().getId());
39-
assertThat(testEndProcessInstanceInterceptor.isCalled).isTrue();
39+
assertThat(testEndCaseInstanceInterceptor.beforeIsCalled).isTrue();
40+
assertThat(testEndCaseInstanceInterceptor.beforeIsTerminated).isFalse();
41+
42+
assertThat(testEndCaseInstanceInterceptor.afterIsCalled).isTrue();
43+
assertThat(testEndCaseInstanceInterceptor.afterIsTerminated).isFalse();
4044
} finally {
4145
cmmnEngineConfiguration.setEndCaseInstanceInterceptor(null);
4246
}
@@ -46,23 +50,38 @@ public void testEndProcessInterceptorIsCalled() {
4650
@CmmnDeployment(resources = "org/flowable/cmmn/test/runtime/oneHumanTaskCase.cmmn")
4751
public void testEndProcessInterceptorIsNotCalledForTermination() {
4852
try {
49-
TestEndCaseInstanceInterceptor testEndProcessInstanceInterceptor = new TestEndCaseInstanceInterceptor();
50-
cmmnEngineConfiguration.setEndCaseInstanceInterceptor(testEndProcessInstanceInterceptor);
53+
TestEndCaseInstanceInterceptor testEndCaseInstanceInterceptor = new TestEndCaseInstanceInterceptor();
54+
cmmnEngineConfiguration.setEndCaseInstanceInterceptor(testEndCaseInstanceInterceptor);
5155
CaseInstance caseInstance = cmmnRuntimeService.createCaseInstanceBuilder().caseDefinitionKey("oneHumanTaskCase").start();
5256
cmmnRuntimeService.terminateCaseInstance(caseInstance.getId());
53-
assertThat(testEndProcessInstanceInterceptor.isCalled).isFalse();
57+
assertThat(testEndCaseInstanceInterceptor.beforeIsCalled).isTrue();
58+
assertThat(testEndCaseInstanceInterceptor.beforeIsTerminated).isTrue();
59+
60+
assertThat(testEndCaseInstanceInterceptor.afterIsCalled).isTrue();
61+
assertThat(testEndCaseInstanceInterceptor.afterIsTerminated).isTrue();
5462
} finally {
5563
cmmnEngineConfiguration.setEndCaseInstanceInterceptor(null);
5664
}
5765
}
5866

5967
public static class TestEndCaseInstanceInterceptor implements EndCaseInstanceInterceptor {
6068

61-
protected boolean isCalled = false;
69+
protected boolean beforeIsCalled = false;
70+
protected boolean beforeIsTerminated = false;
71+
72+
protected boolean afterIsCalled = false;
73+
protected boolean afterIsTerminated = false;
74+
75+
@Override
76+
public void beforeEndCaseInstance(CaseInstanceEntity caseInstance, boolean isTerminated) {
77+
beforeIsCalled = true;
78+
beforeIsTerminated= isTerminated;
79+
}
6280

6381
@Override
64-
public void beforeEndCaseInstance(CaseInstanceEntity caseInstance) {
65-
isCalled = true;
82+
public void afterEndCaseInstance(String caseInstanceId, boolean isTerminated) {
83+
afterIsCalled = true;
84+
afterIsTerminated = isTerminated;
6685
}
6786
}
6887
}

modules/flowable-engine/src/main/java/org/flowable/engine/impl/agenda/EndExecutionOperation.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ protected void handleRegularExecution() {
184184
ExecutionEntityManager executionEntityManager = processEngineConfiguration.getExecutionEntityManager();
185185

186186
if (processEngineConfiguration.getEndProcessInstanceInterceptor() != null) {
187-
processEngineConfiguration.getEndProcessInstanceInterceptor().beforeProcessProcessInstance(execution);
187+
processEngineConfiguration.getEndProcessInstanceInterceptor().beforeEndProcessInstance(execution, false);
188188
}
189189

190190
// There will be a parent execution (or else we would be in the process instance handling method)
@@ -246,7 +246,6 @@ protected void handleRegularExecution() {
246246

247247
agenda.planEndExecutionOperation(subProcessParentExecution);
248248
}
249-
250249
return;
251250
}
252251
}
@@ -309,6 +308,10 @@ protected void handleRegularExecution() {
309308
}
310309

311310
}
311+
312+
if (processEngineConfiguration.getEndProcessInstanceInterceptor() != null) {
313+
processEngineConfiguration.getEndProcessInstanceInterceptor().afterEndProcessInstance(execution.getId(), false);
314+
}
312315
}
313316

314317
protected ExecutionEntity handleSubProcessEnd(ExecutionEntityManager executionEntityManager, ExecutionEntity parentExecution, SubProcess subProcess) {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,10 @@ public void deleteProcessInstance(String processInstanceId, String deleteReason,
448448
public void deleteProcessInstance(String processInstanceId, String deleteReason, boolean cascade, boolean directDeleteInDatabase) {
449449
ExecutionEntity processInstanceExecution = findById(processInstanceId);
450450

451+
if (engineConfiguration.getEndProcessInstanceInterceptor() != null) {
452+
engineConfiguration.getEndProcessInstanceInterceptor().beforeEndProcessInstance(processInstanceExecution, true);
453+
}
454+
451455
if (processInstanceExecution == null) {
452456
throw new FlowableObjectNotFoundException("No process instance found for id '" + processInstanceId + "'", ProcessInstance.class);
453457
}
@@ -473,6 +477,10 @@ public void deleteProcessInstance(String processInstanceId, String deleteReason,
473477
}
474478
}
475479
}
480+
481+
if (engineConfiguration.getEndProcessInstanceInterceptor() != null) {
482+
engineConfiguration.getEndProcessInstanceInterceptor().afterEndProcessInstance(processInstanceId, true);
483+
}
476484
}
477485

478486
protected void deleteProcessInstanceCascade(ExecutionEntity execution, String state, String deleteReason, boolean deleteHistory, boolean directDeleteInDatabase) {

modules/flowable-engine/src/main/java/org/flowable/engine/interceptor/EndProcessInstanceInterceptor.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,7 @@
1616

1717
public interface EndProcessInstanceInterceptor {
1818

19-
void beforeProcessProcessInstance(ExecutionEntity processInstance);
19+
void beforeEndProcessInstance(ExecutionEntity processInstance, boolean isTerminated);
20+
21+
void afterEndProcessInstance(String processInstanceId, boolean isTerminated);
2022
}

modules/flowable-engine/src/test/java/org/flowable/engine/test/api/runtime/ProcessInstanceEndInterceptorTest.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
* limitations under the License.
1212
*/
1313
package org.flowable.engine.test.api.runtime;
14-
1514
import static org.assertj.core.api.Assertions.assertThat;
1615

1716
import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl;
@@ -37,7 +36,11 @@ public void testEndProcessInterceptorIsCalled() {
3736

3837
runtimeService.startProcessInstanceByKey("oneTaskProcess");
3938
taskService.complete(taskService.createTaskQuery().singleResult().getId());
40-
assertThat(testEndProcessInstanceInterceptor.isCalled).isTrue();
39+
assertThat(testEndProcessInstanceInterceptor.beforeIsCalled).isTrue();
40+
assertThat(testEndProcessInstanceInterceptor.beforeIsTerminated).isFalse();
41+
42+
assertThat(testEndProcessInstanceInterceptor.afterIsCalled).isTrue();
43+
assertThat(testEndProcessInstanceInterceptor.afterIsTerminated).isFalse();
4144
} finally {
4245
processEngineConfiguration.setEndProcessInstanceInterceptor(null);
4346
}
@@ -53,19 +56,32 @@ public void testEndProcessInterceptorIsNotCalledForTermination() {
5356

5457
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("oneTaskProcess");
5558
runtimeService.deleteProcessInstance(processInstance.getId(), "test");
56-
assertThat(testEndProcessInstanceInterceptor.isCalled).isFalse();
59+
assertThat(testEndProcessInstanceInterceptor.beforeIsCalled).isTrue();
60+
assertThat(testEndProcessInstanceInterceptor.beforeIsTerminated).isTrue();
61+
assertThat(testEndProcessInstanceInterceptor.afterIsCalled).isTrue();
62+
assertThat(testEndProcessInstanceInterceptor.afterIsTerminated).isTrue();
5763
} finally {
5864
processEngineConfiguration.setEndProcessInstanceInterceptor(null);
5965
}
6066
}
6167

6268
public static class TestEndProcessInstanceInterceptor implements EndProcessInstanceInterceptor {
6369

64-
protected boolean isCalled = false;
70+
protected boolean beforeIsCalled = false;
71+
protected boolean beforeIsTerminated = false;
72+
73+
protected boolean afterIsCalled = false;
74+
protected boolean afterIsTerminated = false;
75+
@Override
76+
public void beforeEndProcessInstance(ExecutionEntity processInstance, boolean isTerminated) {
77+
beforeIsCalled = true;
78+
beforeIsTerminated= isTerminated;
79+
}
6580

6681
@Override
67-
public void beforeProcessProcessInstance(ExecutionEntity processInstance) {
68-
isCalled=true;
82+
public void afterEndProcessInstance(String processInstanceId, boolean isTerminated) {
83+
afterIsCalled = true;
84+
afterIsTerminated = isTerminated;
6985
}
7086
}
7187
}

0 commit comments

Comments
 (0)