Skip to content

Commit

Permalink
Remove not needed update methods from VariableService and polish Vari…
Browse files Browse the repository at this point in the history
…ableInstanceValueModifier
  • Loading branch information
filiphr committed May 15, 2023
1 parent aedf85e commit 9c4d155
Show file tree
Hide file tree
Showing 12 changed files with 308 additions and 205 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import org.flowable.task.api.history.HistoricTaskLogEntryBuilder;
import org.flowable.task.api.history.HistoricTaskLogEntryType;
import org.flowable.variable.api.history.HistoricVariableInstance;
import org.flowable.variable.service.VariableServiceConfiguration;
import org.flowable.variable.service.impl.persistence.entity.VariableInstanceEntity;
import org.junit.Assert;
import org.junit.Test;
Expand Down Expand Up @@ -1579,7 +1580,12 @@ public void testVariableChanges() {

VariableInstanceEntity variableInstanceEntity = variablesInstances.get(0);
variableInstanceEntity.setMetaInfo("test meta info");
cmmnEngineConfiguration.getVariableServiceConfiguration().getVariableInstanceEntityManager().updateWithHistoricVariableSync(variableInstanceEntity);
VariableServiceConfiguration variableServiceConfiguration = cmmnEngineConfiguration.getVariableServiceConfiguration();
variableServiceConfiguration.getVariableInstanceEntityManager().update(variableInstanceEntity);
if (variableServiceConfiguration.getInternalHistoryVariableManager() != null) {
variableServiceConfiguration.getInternalHistoryVariableManager()
.recordVariableUpdate(variableInstanceEntity, commandContext.getClock().getCurrentTime());
}
return null;
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.flowable.common.engine.api.FlowableIllegalArgumentException;
import org.flowable.task.api.Task;
import org.flowable.variable.api.persistence.entity.VariableInstance;
import org.flowable.variable.service.VariableServiceConfiguration;
import org.flowable.variable.service.impl.DefaultVariableInstanceValueModifier;
import org.flowable.variable.service.impl.VariableInstanceValueModifier;
import org.junit.After;
Expand Down Expand Up @@ -53,19 +54,8 @@ public void tearDown() {
@Test
@CmmnDeployment(resources = { "org/flowable/cmmn/test/one-human-task-model.cmmn" })
public void testCompleteTaskWithExceptionInPostSetVariable() {
DefaultVariableInstanceValueModifier modifier = new DefaultVariableInstanceValueModifier(cmmnEngineConfiguration.getVariableServiceConfiguration()) {

@Override
protected void setOrUpdateValue(VariableInstance variableInstance, Object value, String tenantId) {
if (variableInstance.getName().equals("orderId")) {
if (((Number) value).longValue() < 0) {
throw new FlowableIllegalArgumentException("Invalid type: value should be larger than zero");
}
}
super.setOrUpdateValue(variableInstance, value, tenantId);
}
};
cmmnEngineConfiguration.getVariableServiceConfiguration().setVariableInstanceValueModifier(modifier);
cmmnEngineConfiguration.getVariableServiceConfiguration()
.setVariableInstanceValueModifier(new TestOrderIdValidatingValueModifier(cmmnEngineConfiguration.getVariableServiceConfiguration()));

Map<String, Object> variables = new HashMap<>();
variables.put("orderId", 1L);
Expand All @@ -87,18 +77,8 @@ protected void setOrUpdateValue(VariableInstance variableInstance, Object value,
@Test
@CmmnDeployment(resources = { "org/flowable/cmmn/test/one-human-task-model.cmmn" })
public void testTransientVariables() {
DefaultVariableInstanceValueModifier modifier = new DefaultVariableInstanceValueModifier(cmmnEngineConfiguration.getVariableServiceConfiguration()) {

@Override
protected void setOrUpdateValue(VariableInstance variableInstance, Object value, String tenantId) {
if (variableInstance.getName().equals("orderId")) {
if (((Number) value).longValue() < 0) {
throw new FlowableIllegalArgumentException("Invalid type: value should be larger than zero");
}
}
}
};
cmmnEngineConfiguration.getVariableServiceConfiguration().setVariableInstanceValueModifier(modifier);
cmmnEngineConfiguration.getVariableServiceConfiguration()
.setVariableInstanceValueModifier(new TestOrderIdValidatingValueModifier(cmmnEngineConfiguration.getVariableServiceConfiguration()));

Map<String, Object> variables = new HashMap<>();
variables.put("orderId", -1L);
Expand All @@ -108,4 +88,31 @@ protected void setOrUpdateValue(VariableInstance variableInstance, Object value,
cmmnRuntimeService.createCaseInstanceBuilder().transientVariables(variables).caseDefinitionKey("oneTaskCase").start();
}).isInstanceOf(FlowableIllegalArgumentException.class).hasMessage("Invalid type: value should be larger than zero");
}

static class TestOrderIdValidatingValueModifier extends DefaultVariableInstanceValueModifier {

public TestOrderIdValidatingValueModifier(VariableServiceConfiguration serviceConfiguration) {
super(serviceConfiguration);
}

@Override
public void setVariableValue(VariableInstance variableInstance, Object value, String tenantId) {
validateValue(variableInstance, value);
super.setVariableValue(variableInstance, value, tenantId);
}

@Override
public void updateVariableValue(VariableInstance variableInstance, Object value, String tenantId) {
validateValue(variableInstance, value);
super.updateVariableValue(variableInstance, value, tenantId);
}

protected void validateValue(VariableInstance variableInstance, Object value) {
if (variableInstance.getName().equals("orderId")) {
if (((Number) value).longValue() < 0) {
throw new FlowableIllegalArgumentException("Invalid type: value should be larger than zero");
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.flowable.variable.api.persistence.entity.VariableInstance;
import org.flowable.variable.api.types.ValueFields;
import org.flowable.variable.api.types.VariableType;
import org.flowable.variable.service.VariableServiceConfiguration;
import org.flowable.variable.service.impl.persistence.entity.VariableInstanceEntity;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -811,7 +812,12 @@ public void testUpdateMetaInfo() {

VariableInstanceEntity variableInstanceEntity = variablesInstances.get(0);
variableInstanceEntity.setMetaInfo("test meta info");
cmmnEngineConfiguration.getVariableServiceConfiguration().getVariableInstanceEntityManager().updateWithHistoricVariableSync(variableInstanceEntity);
VariableServiceConfiguration variableServiceConfiguration = cmmnEngineConfiguration.getVariableServiceConfiguration();
variableServiceConfiguration.getVariableInstanceEntityManager().update(variableInstanceEntity);
if (variableServiceConfiguration.getInternalHistoryVariableManager() != null) {
variableServiceConfiguration.getInternalHistoryVariableManager()
.recordVariableUpdate(variableInstanceEntity, commandContext.getClock().getCurrentTime());
}

return null;
});
Expand Down
Loading

0 comments on commit 9c4d155

Please sign in to comment.