Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added expression resolver for activity name #3821

Merged
merged 8 commits into from
Jan 25, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ public StageActivityBehavior(Stage stage) {
public void execute(CommandContext commandContext, PlanItemInstanceEntity planItemInstanceEntity) {
if (planItemInstanceEntity.getName() == null && planItemInstanceEntity.getPlanItem().getName() != null) {
Expression nameExpression = CommandContextUtil.getExpressionManager(commandContext).createExpression(planItemInstanceEntity.getPlanItem().getName());
planItemInstanceEntity.setName(nameExpression.getValue(planItemInstanceEntity).toString());
Object nameExpressionValue = nameExpression.getValue(planItemInstanceEntity);
if (nameExpressionValue != null) {
planItemInstanceEntity.setName(nameExpressionValue.toString());
}
}

if (StringUtils.isNotEmpty(stage.getBusinessStatus())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ public interface DelegateExecution extends VariableScope {
*/
String getCurrentActivityId();

/**
* Gets the name of the current activity.
*/
String getCurrentActivityName();

/**
* Returns the tenant id, if any is set before on the process definition or process instance.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ public void execute(DelegateExecution execution, MigrationContext migrationConte
activeTaskCandidateGroups = userTask.getCandidateGroups();
activeTaskIdVariableName = userTask.getTaskIdVariableName();
}
if (execution.getCurrentActivityName() != null) {
activeTaskName = execution.getCurrentActivityName();
}

CreateUserTaskBeforeContext beforeContext = new CreateUserTaskBeforeContext(userTask, execution, activeTaskName, activeTaskDescription, activeTaskDueDate,
activeTaskPriority, activeTaskCategory, activeTaskFormKey, activeTaskSkipExpression, activeTaskAssignee, activeTaskOwner,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
import java.util.Map;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.flowable.bpmn.model.FlowElement;
import org.flowable.bpmn.model.FlowNode;
import org.flowable.bpmn.model.SequenceFlow;
import org.flowable.common.engine.api.delegate.Expression;
import org.flowable.common.engine.impl.cfg.IdGenerator;
import org.flowable.engine.history.HistoricActivityInstance;
import org.flowable.engine.impl.ActivityInstanceQueryImpl;
Expand Down Expand Up @@ -350,9 +352,25 @@ protected ActivityInstanceEntity createActivityInstanceEntity(ExecutionEntity ex
}

if (execution.getCurrentFlowElement() != null) {
activityInstanceEntity.setActivityName(execution.getCurrentFlowElement().getName());
if (StringUtils.isNotEmpty(execution.getCurrentFlowElement().getName())
&& (execution.getCurrentFlowElement().getName().contains("${")
|| execution.getCurrentFlowElement().getName().contains("#{"))) {
Expression activityNameExpression = CommandContextUtil.getProcessEngineConfiguration().getExpressionManager()
.createExpression(execution.getCurrentFlowElement().getName());

Object expressionValue = activityNameExpression.getValue(execution);
if (expressionValue != null) {
String nameValue = expressionValue.toString();
execution.setCurrentActivityName(nameValue);
activityInstanceEntity.setActivityName(nameValue);
}
} else {
activityInstanceEntity.setActivityName(execution.getCurrentFlowElement().getName());
}

activityInstanceEntity.setActivityType(parseActivityType(execution.getCurrentFlowElement()));
}

Date now = getClock().getCurrentTime();
activityInstanceEntity.setStartTime(now);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,6 @@ public interface ExecutionEntity extends DelegateExecution, Execution, ProcessIn
void setOriginatingCurrentFlowElement(FlowElement flowElement);

List<VariableInstanceEntity> getQueryVariables();

void setCurrentActivityName(String activityName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -1294,6 +1294,10 @@ public String getCurrentActivityName() {
return activityName;
}

@Override
public void setCurrentActivityName(String activityName) {
this.activityName = activityName;
}
@Override
public String getStartActivityId() {
return startActivityId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -520,4 +520,61 @@ public void upgradeFromHistoryToRuntimeActivities_changeOwner() {
assertProcessEnded(processInstance.getId());
}

@Test
@Deployment(resources = { "org/flowable/engine/test/api/oneTaskProcessWithExpression.bpmn20.xml" })
public void testActivityNameIsResolved() {
ProcessInstance processInstance = runtimeService.createProcessInstanceBuilder().processDefinitionKey("oneTaskProcess")
.variable("testVar", "someTestValue").start();
ActivityInstance taskActivity = runtimeService.createActivityInstanceQuery().activityId("theTask").processInstanceId(processInstance.getId())
.singleResult();
assertThat(taskActivity.getActivityName()).isEqualTo("someTestValue");

ActivityInstance flowActivityInstance = runtimeService.createActivityInstanceQuery().processInstanceId(processInstance.getId()).activityId("flow1")
.singleResult();

assertThat(flowActivityInstance.getActivityName()).isEqualTo("someTestValue");

}

@Test
@Deployment(resources = { "org/flowable/engine/test/api/runtime/ServiceTaskWithNameExpression.bpmn20.xml" })
public void testServiceTaskWithNameExpression() {
ProcessInstance processInstance = runtimeService.createProcessInstanceBuilder().processDefinitionKey("noopProcess")
.variable("testVar", "someTestValue").start();
ActivityInstance taskActivity = runtimeService.createActivityInstanceQuery().activityId("noop").processInstanceId(processInstance.getId())
.singleResult();
assertThat(taskActivity.getActivityName()).isEqualTo("someTestValue");


}

@Test
@Deployment(resources = "org/flowable/engine/test/api/oneTaskProcessWithBeanExpression.bpmn20.xml")
public void testTaskNameExpressionIsNotResolvedTwice() {
NameProvider nameProvider = new NameProvider();
ProcessInstance processInstance = runtimeService.createProcessInstanceBuilder().transientVariable("nameProvider", nameProvider)
.processDefinitionKey("oneTaskProcess").start();
ActivityInstance taskActivity = runtimeService.createActivityInstanceQuery().activityId("theTask").processInstanceId(processInstance.getId())
.singleResult();
Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();

assertThat(taskActivity.getActivityName()).isEqualTo("someName");
assertThat(task.getName()).isEqualTo("someName");
assertThat(nameProvider.getCounter()).isEqualTo(1);

}

public static class NameProvider {

int counter = 0;

public String getName() {
counter++;
return "someName";
}

public int getCounter() {
return counter;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<definitions
xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
xmlns:activiti="http://activiti.org/bpmn"
targetNamespace="ExamplesCategory">

<process id="oneTaskProcess" name="The One Task Process">
<documentation>This is a process for testing purposes</documentation>

<startEvent id="theStart" />
<sequenceFlow id="flow1" sourceRef="theStart" targetRef="theTask" />
<userTask id="theTask" name="${nameProvider.getName()}" />
<sequenceFlow id="flow2" sourceRef="theTask" targetRef="theEnd" />
<endEvent id="theEnd" />

</process>

</definitions>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<definitions
xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
xmlns:activiti="http://activiti.org/bpmn"
targetNamespace="ExamplesCategory">

<process id="oneTaskProcess" name="The One Task Process">
<documentation>This is a process for testing purposes</documentation>

<startEvent id="theStart" />
<sequenceFlow id="flow1" name="${testVar}" sourceRef="theStart" targetRef="theTask" />
<userTask id="theTask" name="${testVar}" />
<sequenceFlow id="flow2" sourceRef="theTask" targetRef="theEnd" />
<endEvent id="theEnd" />

</process>

</definitions>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>

<definitions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
xmlns:flowable="http://flowable.org/bpmn"
targetNamespace="Examples">

<process id="noopProcess">

<startEvent id="start" />
<sequenceFlow id="flow1" sourceRef="start" targetRef="noop" />

<serviceTask id="noop" name="${testVar}" flowable:class="org.flowable.engine.test.history.Noop" />
<sequenceFlow id="flow2" sourceRef="noop" targetRef="waitTask" />

<receiveTask id="waitTask"/>
<sequenceFlow sourceRef="waitTask" targetRef="end"/>

<endEvent id="end" />

</process>

</definitions>