Skip to content

Commit

Permalink
Migration: Allow to configure a pre/post expression which is executed…
Browse files Browse the repository at this point in the history
… before/after the migration
  • Loading branch information
amporsim committed Sep 12, 2023
1 parent 2a6445e commit 846f5d6
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,22 @@ public interface CaseInstanceMigrationBuilder {
*/
CaseInstanceMigrationBuilder addChangePlanItemIdWithDefinitionIdMapping(ChangePlanItemIdWithDefinitionIdMapping mapping);

/**
* Specifies an expression which is executed before the migration starts.
*
* @param preUpgradeExpression the expression e.g. ${mySpringBean.doSomething()}
* @return Returns the builder
*/
CaseInstanceMigrationBuilder withPreUpgradeExpression(String preUpgradeExpression);

/**
* Specifies an expression which is executed after the migration is finished.
*
* @param postUpgradeExpression the expression e.g. ${mySpringBean.doSomething()}
* @return Returns the builder
*/
CaseInstanceMigrationBuilder withPostUpgradeExpression(String postUpgradeExpression);

/**
* Specifies a case instance variable that will also be available during the case migration
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public interface CaseInstanceMigrationDocument {

List<ChangePlanItemIdWithDefinitionIdMapping> getChangePlanItemIdWithDefinitionIdMappings();

String getPreUpgradeExpression();

String getPostUpgradeExpression();

Map<String, Map<String, Object>> getPlanItemLocalVariables();

Map<String, Object> getCaseInstanceVariables();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public interface CaseInstanceMigrationDocumentBuilder {

CaseInstanceMigrationDocumentBuilder addCaseInstanceVariables(Map<String, Object> caseInstanceVariables);

CaseInstanceMigrationDocumentBuilder preUpgradeExpression(String preUpgradeExpression);

CaseInstanceMigrationDocumentBuilder postUpgradeExpression(String postUpgradeExpression);

CaseInstanceMigrationDocument build();

}
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,18 @@ public CaseInstanceMigrationBuilder addChangePlanItemIdWithDefinitionIdMapping(C
return this;
}

@Override
public CaseInstanceMigrationBuilder withPreUpgradeExpression(String preUpgradeExpression) {
this.caseInstanceMigrationDocumentDocumentBuilder.preUpgradeExpression(preUpgradeExpression);
return this;
}

@Override
public CaseInstanceMigrationBuilder withPostUpgradeExpression(String postUpgradeExpression) {
this.caseInstanceMigrationDocumentDocumentBuilder.postUpgradeExpression(postUpgradeExpression);
return this;
}

@Override
public CaseInstanceMigrationBuilder withCaseInstanceVariable(String variableName, Object variableValue) {
this.caseInstanceMigrationDocumentDocumentBuilder.addCaseInstanceVariable(variableName, variableValue);
Expand All @@ -139,6 +151,8 @@ public CaseInstanceMigrationDocument getCaseInstanceMigrationDocument() {
return this.caseInstanceMigrationDocumentDocumentBuilder.build();
}



@Override
public void migrate(String caseInstanceId) {
getCmmnMigrationService().migrateCaseInstance(caseInstanceId, getCaseInstanceMigrationDocument());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public class CaseInstanceMigrationDocumentBuilderImpl implements CaseInstanceMig
protected List<WaitingForRepetitionPlanItemDefinitionMapping> waitingForRepetitionPlanItemDefinitionMappings = new ArrayList<>();
protected List<RemoveWaitingForRepetitionPlanItemDefinitionMapping> removeWaitingForRepetitionPlanItemDefinitionMappings = new ArrayList<>();
protected List<ChangePlanItemIdMapping> changePlanItemIdMappings = new ArrayList<>();
protected String preUpgradeExpression;
protected String postUpgradeExpression;
protected List<ChangePlanItemIdWithDefinitionIdMapping> changePlanItemIdWithDefinitionIdMappings = new ArrayList<>();
protected Map<String, Object> caseInstanceVariables = new HashMap<>();

Expand Down Expand Up @@ -149,6 +151,18 @@ public CaseInstanceMigrationDocumentBuilder addCaseInstanceVariables(Map<String,
return this;
}

@Override
public CaseInstanceMigrationDocumentBuilder preUpgradeExpression(String preUpgradeExpression) {
this.preUpgradeExpression = preUpgradeExpression;
return this;
}

@Override
public CaseInstanceMigrationDocumentBuilder postUpgradeExpression(String postUpgradeExpression) {
this.postUpgradeExpression = postUpgradeExpression;
return this;
}

@Override
public CaseInstanceMigrationDocument build() {
CaseInstanceMigrationDocumentImpl caseInstanceMigrationDocument = new CaseInstanceMigrationDocumentImpl();
Expand All @@ -162,6 +176,8 @@ public CaseInstanceMigrationDocument build() {
caseInstanceMigrationDocument.setChangePlanItemIdMappings(this.changePlanItemIdMappings);
caseInstanceMigrationDocument.setChangePlanItemIdWithDefinitionIdMappings(this.changePlanItemIdWithDefinitionIdMappings);
caseInstanceMigrationDocument.setCaseInstanceVariables(this.caseInstanceVariables);
caseInstanceMigrationDocument.setPreUpgradeExpression(this.preUpgradeExpression);
caseInstanceMigrationDocument.setPostUpgradeExpression(this.postUpgradeExpression);
return caseInstanceMigrationDocument;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public interface CaseInstanceMigrationDocumentConstants {
String REMOVE_WAITING_FOR_REPETITION_PLAN_ITEM_DEFINITIONS_JSON_SECTION = "removeWaitingForRepetitionPlanItemDefinitions";
String CHANGE_PLAN_ITEM_IDS_JSON_SECTION = "changePlanItemIds";
String CHANGE_PLAN_ITEM_IDS_WITH_DEFINITION_ID_JSON_SECTION = "changePlanItemIdsWithDefinitionId";
String PRE_UPGRADE_EXPRESSION_KEY_JSON_PROPERTY = "preUpgradeExpression";
String POST_UPGRADE_EXPRESSION_KEY_JSON_PROPERTY = "postUpgradeExpression";
String LOCAL_VARIABLES_JSON_SECTION = "localVariables";
String CASE_INSTANCE_VARIABLES_JSON_SECTION = "caseInstanceVariables";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ public static JsonNode convertToJson(CaseInstanceMigrationDocument caseInstanceM
if (changePlanItemIdWithDefinitionIdMappingNodes != null && !changePlanItemIdWithDefinitionIdMappingNodes.isNull()) {
documentNode.set(CHANGE_PLAN_ITEM_IDS_WITH_DEFINITION_ID_JSON_SECTION, changePlanItemIdWithDefinitionIdMappingNodes);
}

if (caseInstanceMigrationDocument.getPreUpgradeExpression() != null) {
documentNode.put(PRE_UPGRADE_EXPRESSION_KEY_JSON_PROPERTY, caseInstanceMigrationDocument.getPreUpgradeExpression());
}

if (caseInstanceMigrationDocument.getPostUpgradeExpression() != null) {
documentNode.put(POST_UPGRADE_EXPRESSION_KEY_JSON_PROPERTY, caseInstanceMigrationDocument.getPostUpgradeExpression());
}

JsonNode caseInstanceVariablesNode = convertToJsonCaseInstanceVariables(caseInstanceMigrationDocument, objectMapper);
if (caseInstanceVariablesNode != null && !caseInstanceVariablesNode.isNull()) {
Expand Down Expand Up @@ -292,6 +300,13 @@ public static CaseInstanceMigrationDocument convertFromJson(String jsonCaseInsta
Map<String, Object> caseInstanceVariables = convertFromJsonNodeToObject(caseInstanceVariablesNode, objectMapper);
documentBuilder.addCaseInstanceVariables(caseInstanceVariables);
}

String preUpgradeExpression = getJsonProperty(PRE_UPGRADE_EXPRESSION_KEY_JSON_PROPERTY, rootNode);
documentBuilder.preUpgradeExpression(preUpgradeExpression);

String postUpgradeExpression = getJsonProperty(POST_UPGRADE_EXPRESSION_KEY_JSON_PROPERTY, rootNode);
documentBuilder.postUpgradeExpression(postUpgradeExpression);

return documentBuilder.build();

} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public class CaseInstanceMigrationDocumentImpl implements CaseInstanceMigrationD
protected List<RemoveWaitingForRepetitionPlanItemDefinitionMapping> removeWaitingForRepetitionPlanItemDefinitionMappings = new ArrayList<>();
protected List<ChangePlanItemIdMapping> changePlanItemIdMappings = new ArrayList<>();
protected List<ChangePlanItemIdWithDefinitionIdMapping> changePlanItemIdWithDefinitionIdMappings = new ArrayList<>();
protected String preUpgradeExpression;
protected String postUpgradeExpression;
protected Map<String, Object> caseInstanceVariables = new HashMap<>();
protected Map<String, Map<String, Object>> planItemLocalVariables = new HashMap<>();

Expand Down Expand Up @@ -97,6 +99,14 @@ public void setCaseInstanceVariables(Map<String, Object> caseInstanceVariables)
this.caseInstanceVariables = caseInstanceVariables;
}

public void setPreUpgradeExpression(String preUpgradeExpression) {
this.preUpgradeExpression = preUpgradeExpression;
}

public void setPostUpgradeExpression(String postUpgradeExpression) {
this.postUpgradeExpression = postUpgradeExpression;
}

@Override
public String getMigrateToCaseDefinitionId() {
return this.migrateToCaseDefinitionId;
Expand Down Expand Up @@ -152,6 +162,16 @@ public List<ChangePlanItemIdWithDefinitionIdMapping> getChangePlanItemIdWithDefi
return changePlanItemIdWithDefinitionIdMappings;
}

@Override
public String getPreUpgradeExpression() {
return preUpgradeExpression;
}

@Override
public String getPostUpgradeExpression() {
return postUpgradeExpression;
}

@Override
public Map<String, Map<String, Object>> getPlanItemLocalVariables() {
return this.planItemLocalVariables;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,10 @@ public void migrateHistoricCaseInstancesOfCaseDefinition(String caseDefinitionId

protected void doMigrateCaseInstance(CaseInstanceEntity caseInstance, CaseDefinition caseDefinitionToMigrateTo, CaseInstanceMigrationDocument document, CommandContext commandContext) {
LOGGER.debug("Start migration of case instance with Id:'{}' to case definition identified by {}", caseInstance.getId(), printCaseDefinitionIdentifierMessage(document));
if (document.getPreUpgradeExpression() != null && !document.getPreUpgradeExpression().isEmpty()) {
cmmnEngineConfiguration.getExpressionManager().createExpression(document.getPreUpgradeExpression()).getValue(caseInstance);
}

ChangePlanItemStateBuilderImpl changePlanItemStateBuilder = prepareChangeStateBuilder(caseInstance, caseDefinitionToMigrateTo, document, commandContext);

LOGGER.debug("Updating case definition reference of case root execution with id:'{}' to '{}'", caseInstance.getId(), caseDefinitionToMigrateTo.getId());
Expand Down Expand Up @@ -293,6 +297,10 @@ protected void doMigrateCaseInstance(CaseInstanceEntity caseInstance, CaseDefini
caseInstanceMigrationCallback.caseInstanceMigrated(caseInstance, caseDefinitionToMigrateTo, document);
}
}

if (document.getPostUpgradeExpression() != null && !document.getPostUpgradeExpression().isEmpty()) {
cmmnEngineConfiguration.getExpressionManager().createExpression(document.getPostUpgradeExpression()).getValue(caseInstance);
}
}

protected void doMigrateHistoricCaseInstance(HistoricCaseInstanceEntity historicCaseInstance, CaseDefinition caseDefinitionToMigrateTo, HistoricCaseInstanceMigrationDocument document, CommandContext commandContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4242,6 +4242,56 @@ void withSentryIfPartEventDeferred() {
}
}

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

// Act
cmmnMigrationService.createCaseInstanceMigrationBuilder()
.migrateToCaseDefinition(definition2.getId())
.withPreUpgradeExpression("${variableContainer.setVariable('preUpgradeExpressionExecuted', true)}")
.migrate(caseInstance.getId());

// Assert
CaseInstance caseInstanceAfterMigration = cmmnRuntimeService.createCaseInstanceQuery()
.caseInstanceId(caseInstance.getId())
.includeCaseVariables()
.singleResult();
assertThat(caseInstanceAfterMigration.getCaseDefinitionId()).isEqualTo(definition2.getId());
assertThat(caseInstanceAfterMigration.getCaseDefinitionVersion()).isEqualTo(2);
assertThat(caseInstanceAfterMigration.getCaseDefinitionDeploymentId()).isEqualTo(definition2.getDeploymentId());

assertThat((Boolean) caseInstanceAfterMigration.getCaseVariables().getOrDefault("preUpgradeExpressionExecuted", false)).isTrue();
}

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

// Act
cmmnMigrationService.createCaseInstanceMigrationBuilder()
.migrateToCaseDefinition(definition2.getId())
.withPostUpgradeExpression("${variableContainer.setVariable('postUpgradeExpressionExecuted', true)}")
.migrate(caseInstance.getId());

// Assert
CaseInstance caseInstanceAfterMigration = cmmnRuntimeService.createCaseInstanceQuery()
.caseInstanceId(caseInstance.getId())
.includeCaseVariables()
.singleResult();
assertThat(caseInstanceAfterMigration.getCaseDefinitionId()).isEqualTo(definition2.getId());
assertThat(caseInstanceAfterMigration.getCaseDefinitionVersion()).isEqualTo(2);
assertThat(caseInstanceAfterMigration.getCaseDefinitionDeploymentId()).isEqualTo(definition2.getDeploymentId());

assertThat((Boolean) caseInstanceAfterMigration.getCaseVariables().getOrDefault("postUpgradeExpressionExecuted", false)).isTrue();
}

// with sentries
// with stages
// with new expected case variables
Expand Down

0 comments on commit 846f5d6

Please sign in to comment.