Skip to content

Commit

Permalink
Fix issue with repetition counter variables not getting created for c…
Browse files Browse the repository at this point in the history
…ase migration
  • Loading branch information
tijsrademakers committed Mar 22, 2024
1 parent 19f1fa3 commit 0cd0140
Show file tree
Hide file tree
Showing 6 changed files with 251 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ protected void executeActivatePlanItemInstances(CaseInstanceChangeState caseInst
if (evaluateRepetitionRule(newPlanItemInstance, commandContext)) {
createPlanItemInstanceDuplicateForRepetition(newPlanItemInstance, commandContext);
}

} else if (hasRepetitionRule(newPlanItemInstance)) {
setRepetitionCounter(newPlanItemInstance, 1);
}
}
}
Expand Down Expand Up @@ -316,6 +319,10 @@ protected void executeChangePlanItemInstancesToAvailableState(CaseInstanceChange
caseInstanceChangeState.addCreatedStageInstance(planItemDefinitionMapping.getPlanItemDefinitionId(), availablePlanItemInstance);
}

if (hasRepetitionRule(availablePlanItemInstance)) {
setRepetitionCounter(availablePlanItemInstance, 1);
}

CmmnHistoryManager cmmnHistoryManager = cmmnEngineConfiguration.getCmmnHistoryManager();
cmmnHistoryManager.recordPlanItemInstanceCreated(availablePlanItemInstance);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2969,6 +2969,150 @@ void withChangedTaskName() {
assertThat(task.getDescription()).isEqualTo("Description: bar");
}

@Test
void withAdditionalReptitionTask() {
// Arrange
CaseDefinition originalDefinition = deployCaseDefinition("test1", "org/flowable/cmmn/test/migration/repetition-task.cmmn.xml");

CaseInstance caseInstance = cmmnRuntimeService.createCaseInstanceBuilder()
.caseDefinitionId(originalDefinition.getId())
.start();

// Assert
Task task = cmmnTaskService.createTaskQuery().caseInstanceId(caseInstance.getId()).singleResult();
assertThat(task.getName()).isEqualTo("repeatableTask");

List<PlanItemInstance> planItemInstances = cmmnRuntimeService.createPlanItemInstanceQuery().caseInstanceId(caseInstance.getId()).planItemDefinitionId("repeatableTask").list();
assertThat(planItemInstances.size()).isEqualTo(1);

Map<String, Object> localVarMap = cmmnRuntimeService.getLocalVariables(planItemInstances.get(0).getId());
assertThat(localVarMap.size()).isEqualTo(1);
assertThat(localVarMap.get("repetitionCounter")).isEqualTo(1);

CaseDefinition destinationDefinition = deployCaseDefinition("test1", "org/flowable/cmmn/test/migration/repetition-task-new-repetition-task.cmmn.xml");

// Act
cmmnMigrationService.createCaseInstanceMigrationBuilder()
.migrateToCaseDefinition(destinationDefinition.getId())
.addActivatePlanItemDefinitionMapping(PlanItemDefinitionMappingBuilder.createActivatePlanItemDefinitionMappingFor("repeatableTask2"))
.migrate(caseInstance.getId());

task = cmmnTaskService.createTaskQuery().caseInstanceId(caseInstance.getId()).taskDefinitionKey("repeatableTask2").singleResult();
assertThat(task.getName()).isEqualTo("repeatableTask2");

planItemInstances = cmmnRuntimeService.createPlanItemInstanceQuery().caseInstanceId(caseInstance.getId()).planItemDefinitionId("repeatableTask2").list();
assertThat(planItemInstances.size()).isEqualTo(1);

localVarMap = cmmnRuntimeService.getLocalVariables(planItemInstances.get(0).getId());
assertThat(localVarMap.size()).isEqualTo(1);
assertThat(localVarMap.get("repetitionCounter2")).isEqualTo(1);

// Assert
cmmnTaskService.complete(task.getId());
task = cmmnTaskService.createTaskQuery().caseInstanceId(caseInstance.getId()).taskDefinitionKey("repeatableTask2").singleResult();
assertThat(task.getName()).isEqualTo("repeatableTask2");

planItemInstances = cmmnRuntimeService.createPlanItemInstanceQuery().caseInstanceId(caseInstance.getId()).planItemDefinitionId("repeatableTask2").list();
assertThat(planItemInstances.size()).isEqualTo(1);

localVarMap = cmmnRuntimeService.getLocalVariables(planItemInstances.get(0).getId());
assertThat(localVarMap.size()).isEqualTo(1);
assertThat(localVarMap.get("repetitionCounter2")).isEqualTo(2);
}

@Test
void withAdditionalRepetitionWithTwoTasks() {
// Arrange
CaseDefinition originalDefinition = deployCaseDefinition("test1", "org/flowable/cmmn/test/migration/repetition-2tasks.cmmn.xml");

CaseInstance caseInstance = cmmnRuntimeService.createCaseInstanceBuilder()
.caseDefinitionId(originalDefinition.getId())
.start();

// Assert
Task task = cmmnTaskService.createTaskQuery().caseInstanceId(caseInstance.getId()).singleResult();
assertThat(task.getName()).isEqualTo("initialTask");

List<PlanItemInstance> planItemInstances = cmmnRuntimeService.createPlanItemInstanceQuery().caseInstanceId(caseInstance.getId()).planItemDefinitionId("initialTask").list();
assertThat(planItemInstances.size()).isEqualTo(1);

Map<String, Object> localVarMap = cmmnRuntimeService.getLocalVariables(planItemInstances.get(0).getId());
assertThat(localVarMap.size()).isEqualTo(1);
assertThat(localVarMap.get("initialCounter")).isEqualTo(1);

planItemInstances = cmmnRuntimeService.createPlanItemInstanceQuery().caseInstanceId(caseInstance.getId()).planItemDefinitionId("dependingTask").list();
assertThat(planItemInstances.size()).isEqualTo(1);
assertThat(planItemInstances.get(0).getState()).isEqualTo(PlanItemInstanceState.AVAILABLE);
localVarMap = cmmnRuntimeService.getLocalVariables(planItemInstances.get(0).getId());
assertThat(localVarMap.size()).isEqualTo(1);
assertThat(localVarMap.get("dependingCounter")).isEqualTo(1);

cmmnTaskService.complete(task.getId());

task = cmmnTaskService.createTaskQuery().caseInstanceId(caseInstance.getId()).taskDefinitionKey("dependingTask").singleResult();
assertThat(task.getName()).isEqualTo("dependingTask");

planItemInstances = cmmnRuntimeService.createPlanItemInstanceQuery().caseInstanceId(caseInstance.getId()).planItemDefinitionId("dependingTask").list();
assertThat(planItemInstances.size()).isEqualTo(2);

planItemInstances = cmmnRuntimeService.createPlanItemInstanceQuery().caseInstanceId(caseInstance.getId()).planItemDefinitionId("dependingTask").planItemInstanceStateActive().list();
assertThat(planItemInstances.size()).isEqualTo(1);

localVarMap = cmmnRuntimeService.getLocalVariables(planItemInstances.get(0).getId());
assertThat(localVarMap.size()).isEqualTo(1);
assertThat(localVarMap.get("dependingCounter")).isEqualTo(1);

planItemInstances = cmmnRuntimeService.createPlanItemInstanceQuery().caseInstanceId(caseInstance.getId()).planItemDefinitionId("dependingTask").planItemInstanceStateWaitingForRepetition().list();
assertThat(planItemInstances.size()).isEqualTo(1);

localVarMap = cmmnRuntimeService.getLocalVariables(planItemInstances.get(0).getId());
assertThat(localVarMap.size()).isEqualTo(1);
assertThat(localVarMap.get("dependingCounter")).isEqualTo(2);

CaseDefinition destinationDefinition = deployCaseDefinition("test1", "org/flowable/cmmn/test/migration/repetition-2tasks-new-repetition-2tasks.cmmn.xml");

// Act
cmmnMigrationService.createCaseInstanceMigrationBuilder()
.migrateToCaseDefinition(destinationDefinition.getId())
.addActivatePlanItemDefinitionMapping(PlanItemDefinitionMappingBuilder.createActivatePlanItemDefinitionMappingFor("extraInitialTask"))
.addMoveToAvailablePlanItemDefinitionMapping(PlanItemDefinitionMappingBuilder.createMoveToAvailablePlanItemDefinitionMappingFor("extraDependingTask"))
.migrate(caseInstance.getId());

task = cmmnTaskService.createTaskQuery().caseInstanceId(caseInstance.getId()).taskDefinitionKey("extraInitialTask").singleResult();
assertThat(task.getName()).isEqualTo("extraInitialTask");

planItemInstances = cmmnRuntimeService.createPlanItemInstanceQuery().caseInstanceId(caseInstance.getId()).planItemDefinitionId("extraInitialTask").list();
assertThat(planItemInstances.size()).isEqualTo(1);

localVarMap = cmmnRuntimeService.getLocalVariables(planItemInstances.get(0).getId());
assertThat(localVarMap.size()).isEqualTo(1);
assertThat(localVarMap.get("extraInitialCounter")).isEqualTo(1);

assertThat(cmmnTaskService.createTaskQuery().caseInstanceId(caseInstance.getId()).taskDefinitionKey("extraDependingTask").count()).isEqualTo(0);

// Assert
cmmnTaskService.complete(task.getId());
task = cmmnTaskService.createTaskQuery().caseInstanceId(caseInstance.getId()).taskDefinitionKey("extraDependingTask").singleResult();
assertThat(task.getName()).isEqualTo("extraDependingTask");

planItemInstances = cmmnRuntimeService.createPlanItemInstanceQuery().caseInstanceId(caseInstance.getId()).planItemDefinitionId("extraDependingTask").list();
assertThat(planItemInstances.size()).isEqualTo(2);

planItemInstances = cmmnRuntimeService.createPlanItemInstanceQuery().caseInstanceId(caseInstance.getId()).planItemDefinitionId("extraDependingTask").planItemInstanceStateActive().list();
assertThat(planItemInstances.size()).isEqualTo(1);

localVarMap = cmmnRuntimeService.getLocalVariables(planItemInstances.get(0).getId());
assertThat(localVarMap.size()).isEqualTo(1);
assertThat(localVarMap.get("extraDependingCounter")).isEqualTo(1);

planItemInstances = cmmnRuntimeService.createPlanItemInstanceQuery().caseInstanceId(caseInstance.getId()).planItemDefinitionId("extraDependingTask").planItemInstanceStateWaitingForRepetition().list();
assertThat(planItemInstances.size()).isEqualTo(1);

localVarMap = cmmnRuntimeService.getLocalVariables(planItemInstances.get(0).getId());
assertThat(localVarMap.size()).isEqualTo(1);
assertThat(localVarMap.get("extraDependingCounter")).isEqualTo(2);
}

@Test
void migrateCaseInstancesWithSimpleOneTaskCaseWithMappingToSecondNewTask() {
// Arrange
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/CMMN/20151109/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:flowable="http://flowable.org/cmmn" xmlns:cmmndi="http://www.omg.org/spec/CMMN/20151109/CMMNDI" xmlns:dc="http://www.omg.org/spec/CMMN/20151109/DC" xmlns:di="http://www.omg.org/spec/CMMN/20151109/DI" xmlns:design="http://flowable.org/design" targetNamespace="http://flowable.org/cmmn">
<case id="repetitionTaskCase" name="RepetitionTaskCase">
<casePlanModel id="onecaseplanmodel1" name="Case plan model">
<planItem id="planItemInitialTask" name="initialTask" definitionRef="initialTask">
<itemControl>
<repetitionRule flowable:counterVariable="initialCounter" />
</itemControl>
</planItem>
<planItem id="planItemDependingTask" name="dependingTask" definitionRef="dependingTask">
<itemControl>
<repetitionRule flowable:counterVariable="dependingCounter" />
</itemControl>
<entryCriterion id="entryCriterion1" sentryRef="sentryentryCriterion1" />
</planItem>
<planItem id="planItemExtraInitialTask" name="extraInitialTask" definitionRef="extraInitialTask">
<itemControl>
<repetitionRule flowable:counterVariable="extraInitialCounter" />
</itemControl>
</planItem>
<planItem id="planItemExtraDependingTask" name="extraDependingTask" definitionRef="extraDependingTask">
<itemControl>
<repetitionRule flowable:counterVariable="extraDependingCounter" />
</itemControl>
<entryCriterion id="entryCriterion2" sentryRef="sentryentryCriterion2" />
</planItem>
<sentry id="sentryentryCriterion1">
<planItemOnPart id="sentryOnPartentryCriterion1" sourceRef="planItemInitialTask">
<standardEvent>complete</standardEvent>
</planItemOnPart>
</sentry>
<sentry id="sentryentryCriterion2">
<planItemOnPart id="sentryOnPartentryCriterion2" sourceRef="planItemExtraInitialTask">
<standardEvent>complete</standardEvent>
</planItemOnPart>
</sentry>
<humanTask id="initialTask" name="initialTask" />
<humanTask id="dependingTask" name="dependingTask" />
<humanTask id="extraInitialTask" name="extraInitialTask" />
<humanTask id="extraDependingTask" name="extraDependingTask" />
</casePlanModel>
</case>
</definitions>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/CMMN/20151109/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:flowable="http://flowable.org/cmmn" xmlns:cmmndi="http://www.omg.org/spec/CMMN/20151109/CMMNDI" xmlns:dc="http://www.omg.org/spec/CMMN/20151109/DC" xmlns:di="http://www.omg.org/spec/CMMN/20151109/DI" xmlns:design="http://flowable.org/design" targetNamespace="http://flowable.org/cmmn">
<case id="repetitionTaskCase" name="RepetitionTaskCase">
<casePlanModel id="onecaseplanmodel1" name="Case plan model">
<planItem id="planItemInitialTask" name="initialTask" definitionRef="initialTask">
<itemControl>
<repetitionRule flowable:counterVariable="initialCounter" />
</itemControl>
</planItem>
<planItem id="planItemDependingTask" name="dependingTask" definitionRef="dependingTask">
<itemControl>
<repetitionRule flowable:counterVariable="dependingCounter" />
</itemControl>
<entryCriterion id="entryCriterion1" sentryRef="sentryentryCriterion1" />
</planItem>
<sentry id="sentryentryCriterion1">
<planItemOnPart id="sentryOnPartentryCriterion1" sourceRef="planItemInitialTask">
<standardEvent>complete</standardEvent>
</planItemOnPart>
</sentry>
<humanTask id="initialTask" name="initialTask" />
<humanTask id="dependingTask" name="dependingTask" />
</casePlanModel>
</case>
</definitions>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/CMMN/20151109/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:flowable="http://flowable.org/cmmn" xmlns:cmmndi="http://www.omg.org/spec/CMMN/20151109/CMMNDI" xmlns:dc="http://www.omg.org/spec/CMMN/20151109/DC" xmlns:di="http://www.omg.org/spec/CMMN/20151109/DI" xmlns:design="http://flowable.org/design" targetNamespace="http://flowable.org/cmmn">
<case id="repetitionTaskCase" name="RepetitionTaskCase">
<casePlanModel id="onecaseplanmodel1" name="Case plan model">
<planItem id="planItemrepeatableTask" name="repeatableTask" definitionRef="repeatableTask">
<itemControl>
<repetitionRule flowable:counterVariable="repetitionCounter" />
</itemControl>
</planItem>
<planItem id="planItemrepeatableTask2" name="repeatableTask2" definitionRef="repeatableTask2">
<itemControl>
<repetitionRule flowable:counterVariable="repetitionCounter2" />
</itemControl>
</planItem>
<humanTask id="repeatableTask" name="repeatableTask" />
<humanTask id="repeatableTask2" name="repeatableTask2" />
</casePlanModel>
</case>
</definitions>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/CMMN/20151109/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:flowable="http://flowable.org/cmmn" xmlns:cmmndi="http://www.omg.org/spec/CMMN/20151109/CMMNDI" xmlns:dc="http://www.omg.org/spec/CMMN/20151109/DC" xmlns:di="http://www.omg.org/spec/CMMN/20151109/DI" xmlns:design="http://flowable.org/design" targetNamespace="http://flowable.org/cmmn">
<case id="repetitionTaskCase" name="RepetitionTaskCase">
<casePlanModel id="onecaseplanmodel1" name="Case plan model">
<planItem id="planItemrepeatableTask" name="repeatableTask" definitionRef="repeatableTask">
<itemControl>
<repetitionRule flowable:counterVariable="repetitionCounter" />
</itemControl>
</planItem>
<humanTask id="repeatableTask" name="repeatableTask" />
</casePlanModel>
</case>
</definitions>

0 comments on commit 0cd0140

Please sign in to comment.