Skip to content

Commit

Permalink
Call lifecycle listener for stage child plan item instances on termin…
Browse files Browse the repository at this point in the history
…ate and exit
  • Loading branch information
tijsrademakers committed Aug 26, 2024
1 parent c7904de commit be6593b
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import org.apache.commons.lang3.StringUtils;
import org.flowable.cmmn.api.delegate.DelegatePlanItemInstance;
import org.flowable.cmmn.api.runtime.PlanItemInstanceState;
import org.flowable.cmmn.engine.impl.behavior.CoreCmmnTriggerableActivityBehavior;
import org.flowable.cmmn.engine.impl.behavior.PlanItemActivityBehavior;
import org.flowable.cmmn.engine.impl.persistence.entity.PlanItemInstanceEntity;
Expand Down Expand Up @@ -92,6 +93,10 @@ protected void handleChildPlanItemInstances(CommandContext commandContext, Deleg
} else if (PlanItemTransition.EXIT.equals(transition)) {
CommandContextUtil.getAgenda(commandContext).planExitPlanItemInstanceOperation(childPlanItemInstance, null, null, null);
}

} else if (PlanItemTransition.TERMINATE.equals(transition) || PlanItemTransition.EXIT.equals(transition)) {
CommandContextUtil.getCmmnEngineConfiguration(commandContext).getListenerNotificationHelper()
.executeLifecycleListeners(commandContext, childPlanItemInstance, childPlanItemInstance.getState(), PlanItemInstanceState.TERMINATED);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,43 @@ public void testUserEventListenerRepetitionWithCondition() {

assertThat(cmmnRuntimeService.createUserEventListenerInstanceQuery().caseInstanceId(caseInstance.getId()).count()).isZero();
}

@Test
@CmmnDeployment
public void testTerminateCaseInstance() {
setTestLifeCycleListener(null, new TestTerminateLifecycleListener());

// Start case instance
CaseInstance caseInstance = cmmnRuntimeService.createCaseInstanceBuilder()
.caseDefinitionKey("testManualActivation")
.start();

assertThat(testLifeCycleListener.getEvents()).isEmpty();

List<PlanItemInstance> planItemInstances = cmmnRuntimeService.createPlanItemInstanceQuery().caseInstanceId(caseInstance.getId()).list();
assertThat(planItemInstances).hasSize(3);

PlanItemInstance planItemInstance = cmmnRuntimeService.createPlanItemInstanceQuery().caseInstanceId(caseInstance.getId()).planItemInstanceElementId("planItemTaskA").singleResult();
assertThat(planItemInstance.getState()).isEqualTo(PlanItemInstanceState.ENABLED);

cmmnRuntimeService.terminateCaseInstance(caseInstance.getId());

List<TestLifeCycleEvent> lifeCycleEvents = testLifeCycleListener.getEvents();
assertThat(lifeCycleEvents).hasSize(2);

for (TestLifeCycleEvent lifeCycleEvent : lifeCycleEvents) {
if ("planItemTaskA".equals(lifeCycleEvent.getPlanItemInstance().getElementId())) {
assertThat(lifeCycleEvent.getPlanItemInstance().getElementId()).isEqualTo("planItemTaskA");
assertThat(lifeCycleEvent.getOldState()).isEqualTo(PlanItemInstanceState.ENABLED);
assertThat(lifeCycleEvent.getNewState()).isEqualTo(PlanItemInstanceState.TERMINATED);

} else {
assertThat(lifeCycleEvent.getPlanItemInstance().getElementId()).isEqualTo("planItemTaskB");
assertThat(lifeCycleEvent.getOldState()).isEqualTo(PlanItemInstanceState.ENABLED);
assertThat(lifeCycleEvent.getNewState()).isEqualTo(PlanItemInstanceState.TERMINATED);
}
}
}

private void setTestLifeCycleListener(String planItemDefinitionType, AbstractTestLifecycleListener testLifeCycleListener) {
cmmnEngineConfiguration.addPlanItemInstanceLifeCycleListener(planItemDefinitionType, testLifeCycleListener);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.flowable.cmmn.test.listener;

import org.flowable.cmmn.api.runtime.PlanItemInstanceState;

public class TestTerminateLifecycleListener extends AbstractTestLifecycleListener {

@Override
public String getSourceState() {
return PlanItemInstanceState.ENABLED;
}

@Override
public String getTargetState() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/CMMN/20151109/MODEL"
targetNamespace="http://flowable.org/cmmn">
<case id="testManualActivation" name="testManualActivation">
<casePlanModel id="casePlanModel">

<planItem id="planItemStage" definitionRef="stage1"/>
<planItem id="planItemTaskA" definitionRef="taskA">
<itemControl>
<manualActivationRule></manualActivationRule>
</itemControl>
</planItem>

<stage id="stage1">
<planItem id="planItemTaskB" name="Task B" definitionRef="taskB">
<itemControl>
<manualActivationRule></manualActivationRule>
</itemControl>
</planItem>

<humanTask id="taskB" name="Task B"/>
</stage>

<humanTask id="taskA" name="Task A" />
</casePlanModel>
</case>
</definitions>

0 comments on commit be6593b

Please sign in to comment.