Skip to content

Commit

Permalink
Fix issue with case definition id not updated for milestone and sentr…
Browse files Browse the repository at this point in the history
…y part instances in migration
  • Loading branch information
tijsrademakers committed May 24, 2024
1 parent e22cf89 commit cb45b1b
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,12 @@ protected void doMigrateCaseInstance(CaseInstanceEntity caseInstance, CaseDefini

LOGGER.debug("Updating case definition reference in plan item instances");
CommandContextUtil.getPlanItemInstanceEntityManager(commandContext).updatePlanItemInstancesCaseDefinitionId(caseInstance.getId(), caseDefinitionToMigrateTo.getId());

LOGGER.debug("Updating case definition reference in milestone instances");
CommandContextUtil.getMilestoneInstanceEntityManager(commandContext).updateMilestoneInstancesCaseDefinitionId(caseInstance.getId(), caseDefinitionToMigrateTo.getId());

LOGGER.debug("Updating case definition reference in sentry part instances");
CommandContextUtil.getSentryPartInstanceEntityManager(commandContext).updateSentryPartInstancesCaseDefinitionId(caseInstance.getId(), caseDefinitionToMigrateTo.getId());

LOGGER.debug("Updating case definition reference in history");
changeCaseDefinitionReferenceOfHistory(caseInstance, caseDefinitionToMigrateTo, commandContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public interface MilestoneInstanceEntityManager extends EntityManager<MilestoneI

long findMilestoneInstanceCountByQueryCriteria(MilestoneInstanceQuery query);

void updateMilestoneInstancesCaseDefinitionId(String caseInstanceId, String caseDefinitionId);

void deleteByCaseDefinitionId(String caseDefinitionId);

void deleteByCaseInstanceId(String caseInstanceId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ public long findMilestoneInstanceCountByQueryCriteria(MilestoneInstanceQuery que
return dataManager.findMilestoneInstancesCountByQueryCriteria((MilestoneInstanceQueryImpl) query);
}

@Override
public void updateMilestoneInstancesCaseDefinitionId(String caseInstanceId, String caseDefinitionId) {
MilestoneInstanceQuery milestoneQuery = new MilestoneInstanceQueryImpl()
.milestoneInstanceCaseInstanceId(caseInstanceId);
List<MilestoneInstance> milestoneInstances = findMilestoneInstancesByQueryCriteria(milestoneQuery);
if (milestoneInstances != null && !milestoneInstances.isEmpty()) {
for (MilestoneInstance milestoneInstance : milestoneInstances) {
MilestoneInstanceEntity milestoneInstanceEntity = (MilestoneInstanceEntity) milestoneInstance;
milestoneInstanceEntity.setCaseDefinitionId(caseDefinitionId);
update(milestoneInstanceEntity);
}
}
}

@Override
public void deleteByCaseDefinitionId(String caseDefinitionId) {
dataManager.deleteByCaseDefinitionId(caseDefinitionId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public interface PlanItemInstanceEntityManager extends EntityManager<PlanItemIns
List<PlanItemInstanceEntity> findByCaseInstanceIdAndPlanItemId(String caseInstanceId, String planItemId);

List<PlanItemInstanceEntity> findByStageInstanceIdAndPlanItemId(String stageInstanceId, String planItemId);

void updatePlanItemInstancesCaseDefinitionId(String caseInstanceId, String caseDefinitionId);

void deleteSentryRelatedData(String planItemId);

Expand All @@ -53,6 +55,4 @@ public interface PlanItemInstanceEntityManager extends EntityManager<PlanItemIns
void deleteByStageInstanceId(String stageInstanceId);

void deleteByCaseInstanceId(String caseInstanceId);

void updatePlanItemInstancesCaseDefinitionId(String caseInstanceId, String caseDefinitionId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,34 @@ public List<PlanItemInstanceEntity> findByCaseInstanceIdAndPlanItemId(String cas
public List<PlanItemInstanceEntity> findByStageInstanceIdAndPlanItemId(String stageInstanceId, String planItemId) {
return dataManager.findByStageInstanceIdAndPlanItemId(stageInstanceId, planItemId);
}

@Override
public void updatePlanItemInstancesCaseDefinitionId(String caseInstanceId, String caseDefinitionId) {
CommandContext commandContext = Context.getCommandContext();
PlanItemInstanceQuery planItemQuery = new PlanItemInstanceQueryImpl(commandContext, engineConfiguration)
.caseInstanceId(caseInstanceId)
.includeEnded();
List<PlanItemInstance> planItemInstances = findByCriteria(planItemQuery);
if (planItemInstances != null && !planItemInstances.isEmpty()) {
List<String> endStates = Arrays.asList(PlanItemInstanceState.UNAVAILABLE, PlanItemInstanceState.DISABLED, PlanItemInstanceState.COMPLETED, PlanItemInstanceState.TERMINATED, PlanItemInstanceState.FAILED);
CmmnModel cmmnModel = CaseDefinitionUtil.getCmmnModel(caseDefinitionId);
for (PlanItemInstance planItemInstance : planItemInstances) {
if (!endStates.contains(planItemInstance.getState())) {
if (cmmnModel.findPlanItemByPlanItemDefinitionId(planItemInstance.getPlanItemDefinitionId()) == null) {
PlanItemInstanceEntity planItemInstanceEntity = (PlanItemInstanceEntity) planItemInstance;
planItemInstanceEntity.setState(PlanItemInstanceState.TERMINATED);
planItemInstanceEntity.setEndedTime(engineConfiguration.getClock().getCurrentTime());
planItemInstanceEntity.setTerminatedTime(planItemInstanceEntity.getEndedTime());
CommandContextUtil.getCmmnHistoryManager(commandContext).recordPlanItemInstanceTerminated(planItemInstanceEntity);
}
}

PlanItemInstanceEntity planItemInstanceEntity = (PlanItemInstanceEntity) planItemInstance;
planItemInstanceEntity.setCaseDefinitionId(caseDefinitionId);
update(planItemInstanceEntity);
}
}
}

@Override
public void delete(PlanItemInstanceEntity planItemInstanceEntity, boolean fireEvent) {
Expand Down Expand Up @@ -476,34 +504,6 @@ public void delete(PlanItemInstanceEntity planItemInstanceEntity, boolean fireEv
getDataManager().delete(planItemInstanceEntity);
}

@Override
public void updatePlanItemInstancesCaseDefinitionId(String caseInstanceId, String caseDefinitionId) {
CommandContext commandContext = Context.getCommandContext();
PlanItemInstanceQuery planItemQuery = new PlanItemInstanceQueryImpl(commandContext, engineConfiguration)
.caseInstanceId(caseInstanceId)
.includeEnded();
List<PlanItemInstance> planItemInstances = findByCriteria(planItemQuery);
if (planItemInstances != null && !planItemInstances.isEmpty()) {
List<String> endStates = Arrays.asList(PlanItemInstanceState.UNAVAILABLE, PlanItemInstanceState.DISABLED, PlanItemInstanceState.COMPLETED, PlanItemInstanceState.TERMINATED, PlanItemInstanceState.FAILED);
CmmnModel cmmnModel = CaseDefinitionUtil.getCmmnModel(caseDefinitionId);
for (PlanItemInstance planItemInstance : planItemInstances) {
if (!endStates.contains(planItemInstance.getState())) {
if (cmmnModel.findPlanItemByPlanItemDefinitionId(planItemInstance.getPlanItemDefinitionId()) == null) {
PlanItemInstanceEntity planItemInstanceEntity = (PlanItemInstanceEntity) planItemInstance;
planItemInstanceEntity.setState(PlanItemInstanceState.TERMINATED);
planItemInstanceEntity.setEndedTime(engineConfiguration.getClock().getCurrentTime());
planItemInstanceEntity.setTerminatedTime(planItemInstanceEntity.getEndedTime());
CommandContextUtil.getCmmnHistoryManager(commandContext).recordPlanItemInstanceTerminated(planItemInstanceEntity);
}
}

PlanItemInstanceEntity planItemInstanceEntity = (PlanItemInstanceEntity) planItemInstance;
planItemInstanceEntity.setCaseDefinitionId(caseDefinitionId);
update(planItemInstanceEntity);
}
}
}

protected CaseInstanceEntityManager getCaseInstanceEntityManager() {
return engineConfiguration.getCaseInstanceEntityManager();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public interface SentryPartInstanceEntityManager extends EntityManager<SentryPar

List<SentryPartInstanceEntity> findSentryPartInstancesByPlanItemInstanceId(String planItemInstanceId);

void updateSentryPartInstancesCaseDefinitionId(String caseInstanceId, String caseDefinitionId);

void deleteByCaseInstanceId(String caseInstanceId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ public List<SentryPartInstanceEntity> findSentryPartInstancesByPlanItemInstanceI
return dataManager.findSentryPartInstancesByPlanItemInstanceId(planItemId);
}

@Override
public void updateSentryPartInstancesCaseDefinitionId(String caseInstanceId, String caseDefinitionId) {
List<SentryPartInstanceEntity> sentryPartInstances = findSentryPartInstancesByCaseInstanceId(caseInstanceId);
if (sentryPartInstances != null && !sentryPartInstances.isEmpty()) {
for (SentryPartInstanceEntity sentryPartInstanceEntity : sentryPartInstances) {
sentryPartInstanceEntity.setCaseDefinitionId(caseDefinitionId);
update(sentryPartInstanceEntity);
}
}
}

@Override
public void deleteByCaseInstanceId(String caseInstanceId) {
dataManager.deleteByCaseInstanceId(caseInstanceId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.flowable.cmmn.converter.CmmnXmlConstants.ELEMENT_STAGE;
import static org.junit.Assert.assertNotNull;

import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -1006,6 +1007,92 @@ void withChangingPlanItemIdAndTerminateDefinition() {
}
}

@Test
void withMilestone() {
// Arrange
deployCaseDefinition("test1", "org/flowable/cmmn/test/migration/one-task-milestone.cmmn.xml");
CaseInstance caseInstance = cmmnRuntimeService.createCaseInstanceBuilder().caseDefinitionKey("testCase").start();
CaseDefinition destinationDefinition = deployCaseDefinition("test1", "org/flowable/cmmn/test/migration/one-task-milestone.cmmn.xml");

if (CmmnHistoryTestHelper.isHistoryLevelAtLeast(HistoryLevel.ACTIVITY, cmmnEngineConfiguration)) {
assertThat(cmmnHistoryService.createHistoricCaseInstanceQuery().caseInstanceId(caseInstance.getId()).singleResult().getCaseDefinitionId())
.isNotEqualTo(destinationDefinition.getId());
}

MilestoneInstance milestoneInstance = cmmnRuntimeService.createMilestoneInstanceQuery().milestoneInstanceCaseInstanceId(caseInstance.getId()).singleResult();
assertNotNull(milestoneInstance);

// Act
cmmnMigrationService.createCaseInstanceMigrationBuilder()
.migrateToCaseDefinition(destinationDefinition.getId())
.migrate(caseInstance.getId());

// Assert
CaseInstance caseInstanceAfterMigration = cmmnRuntimeService.createCaseInstanceQuery()
.caseInstanceId(caseInstance.getId())
.singleResult();
assertThat(caseInstanceAfterMigration.getCaseDefinitionId()).isEqualTo(destinationDefinition.getId());
List<PlanItemInstance> planItemInstances = cmmnRuntimeService.createPlanItemInstanceQuery()
.caseInstanceId(caseInstance.getId())
.list();
assertThat(planItemInstances).hasSize(1);
PlanItemInstance task1PlanItemInstance = planItemInstances.get(0);
assertThat(task1PlanItemInstance).isNotNull();
assertThat(task1PlanItemInstance.getState()).isEqualTo(PlanItemInstanceState.ACTIVE);
assertThat(task1PlanItemInstance.getCaseDefinitionId()).isEqualTo(destinationDefinition.getId());
assertThat(task1PlanItemInstance.getElementId()).isEqualTo("planItem1");

Task task = cmmnTaskService.createTaskQuery().caseInstanceId(caseInstance.getId()).singleResult();
assertThat(task.getTaskDefinitionKey()).isEqualTo("humanTask1");
assertThat(task.getScopeDefinitionId()).isEqualTo(destinationDefinition.getId());

List<MilestoneInstance> milestoneInstances = cmmnRuntimeService.createMilestoneInstanceQuery()
.milestoneInstanceCaseInstanceId(caseInstance.getId())
.list();
assertThat(milestoneInstances).hasSize(1);
MilestoneInstance milestonePlanItemInstance = milestoneInstances.get(0);
assertThat(milestonePlanItemInstance).isNotNull();
assertThat(milestonePlanItemInstance.getCaseDefinitionId()).isEqualTo(destinationDefinition.getId());
assertThat(milestonePlanItemInstance.getElementId()).isEqualTo("planItem2");

cmmnTaskService.complete(task.getId());

assertThat(cmmnRuntimeService.createCaseInstanceQuery().caseInstanceId(caseInstance.getId()).count()).isZero();

if (CmmnHistoryTestHelper.isHistoryLevelAtLeast(HistoryLevel.ACTIVITY, cmmnEngineConfiguration)) {
assertThat(cmmnHistoryService.createHistoricCaseInstanceQuery().caseInstanceId(caseInstance.getId()).count()).isEqualTo(1);
assertThat(cmmnHistoryService.createHistoricCaseInstanceQuery().caseInstanceId(caseInstance.getId()).singleResult().getCaseDefinitionId())
.isEqualTo(destinationDefinition.getId());

List<HistoricPlanItemInstance> historicPlanItemInstances = cmmnHistoryService.createHistoricPlanItemInstanceQuery()
.planItemInstanceCaseInstanceId(caseInstance.getId())
.orderByName()
.desc()
.list();
assertThat(historicPlanItemInstances).hasSize(2);
HistoricPlanItemInstance historicPlanItemInstance = historicPlanItemInstances.get(0);
assertThat(historicPlanItemInstance.getCaseDefinitionId()).isEqualTo(destinationDefinition.getId());
assertThat(historicPlanItemInstance.getElementId()).isEqualTo("planItem1");

historicPlanItemInstance = historicPlanItemInstances.get(1);
assertThat(historicPlanItemInstance.getCaseDefinitionId()).isEqualTo(destinationDefinition.getId());
assertThat(historicPlanItemInstance.getElementId()).isEqualTo("planItem2");

List<HistoricMilestoneInstance> historicMilestoneInstances = cmmnHistoryService.createHistoricMilestoneInstanceQuery()
.milestoneInstanceCaseInstanceId(caseInstance.getId()).list();
assertThat(historicMilestoneInstances).hasSize(1);
HistoricMilestoneInstance historicMilestoneInstance = historicMilestoneInstances.get(0);
assertThat(historicMilestoneInstance.getCaseDefinitionId()).isEqualTo(destinationDefinition.getId());
assertThat(historicMilestoneInstance.getElementId()).isEqualTo("planItem2");

List<HistoricTaskInstance> historicTasks = cmmnHistoryService.createHistoricTaskInstanceQuery().caseInstanceId(caseInstance.getId()).list();
assertThat(historicTasks).hasSize(1);
HistoricTaskInstance historicTask = historicTasks.get(0);
assertThat(historicTask.getScopeDefinitionId()).isEqualTo(destinationDefinition.getId());
assertThat(historicTask.getTaskDefinitionKey()).isEqualTo("humanTask1");
}
}

@Test
void withMilestoneAndChangingPlanItemId() {
// Arrange
Expand Down Expand Up @@ -3424,6 +3511,7 @@ public List<SentryPartInstanceEntity> execute(CommandContext commandContext) {

assertThat(sentryPartInstances).hasSize(1);
assertThat(sentryPartInstances.get(0).getOnPartId()).isEqualTo("sentryOnPartcmmnEntrySentry_2");
assertThat(sentryPartInstances.get(0).getCaseDefinitionId()).isEqualTo(caseInstance.getCaseDefinitionId());

// Act
cmmnMigrationService.createCaseInstanceMigrationBuilder()
Expand Down Expand Up @@ -3452,6 +3540,7 @@ public List<SentryPartInstanceEntity> execute(CommandContext commandContext) {

assertThat(sentryPartInstances).hasSize(1);
assertThat(sentryPartInstances.get(0).getOnPartId()).isEqualTo("sentryOnPartcmmnEntrySentry_2");
assertThat(sentryPartInstances.get(0).getCaseDefinitionId()).isEqualTo(destinationDefinition.getId());

List<PlanItemInstance> planItemInstances = cmmnRuntimeService.createPlanItemInstanceQuery()
.caseInstanceId(caseInstance.getId())
Expand Down

0 comments on commit cb45b1b

Please sign in to comment.