I would know if I’m doing it right or if there’s a simpler way to do this. I re-explain it again.
What I want is a simple “Read data” -> “Elaborate data some way” -> “Show the result”.
For this, I created an User Task
, Read, that is connected to a Service Task
, Elaborate, that is connected to another User Task
, Show.
In my concrete implementation, I want simply to sum two numbers and see the result.
In Spring Boot I created the AddNumbersController
, that maps the /start
URI that simply start the process by key (well, I could generalize it in a ProcessController in a “/start/{key}/{redirectPath}” mapping… well, I’ll do it later).
After starting the process, the page will redirect to a form. When the form is submitted, Add2NumbersController.submit()
is invoked. The form data are added to the process variables with runtimeService.setVariable(processId, variableName, variableValue)
.
Next I query the db for getting Read by key:
public Task getByProcessIdAndTaskKey(String processId, String taskKey) {
final TaskQuery taskQuery = this.taskService.createTaskQuery();
return taskQuery
.processInstanceId(processId)
.taskDefinitionKey(taskKey)
.active()
.singleResult();
}
Well, for example, I don’t know if this is the smarter way to do the thing… in theory I should know in some way the current task. I should not query it by key explicitly. No?
Anyway, after that I complete the Read task. This way Elaborate should start. It is linked with an expression to demoTaskService.addNumbers(String processId)
, that cycle over all process variables, converts them to double
and then to BigDecimal
and sums them. Yes, there are only two numbers but this way you can add also N numbers in future if you want. Ok, now I’m thinking that is better to put all the numbers to add inside a single Collection
process variable…
Anyway, after summing all the numbers, I put the result in a process variable. I do not need to complete the task manually I suppose, since it’s a Service Task
, so I didn’t do it.
Next, Show task starts. It has an Execution Listener
that at task start fires the expression demoTaskService.showAddNumbersResult()
, that redirects to AddNumbersController.showAddNumbersResult()
, that gets the result variable, put it on the model and redirect to the HTML template that shows the result. End.
I mean, this simple task musy be done in such a complicated way? Or am I wronging something?