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

Feature/git issue #4487 fetch task variables feature #4754

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
adding variable response for list of tasks get and post
  • Loading branch information
prakashpalanisamy committed Oct 28, 2024
commit a686db9b980f196b961a7b7912f41d35c377668d
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ public class TaskQueryDto extends AbstractQueryDto<TaskQuery> {

private Boolean withCommentAttachmentInfo;

private Boolean withTaskVariablesInReturn;

private Boolean withTaskLocalVariablesInReturn;

public TaskQueryDto() {

}
Expand Down Expand Up @@ -721,6 +725,16 @@ public void setWithCommentAttachmentInfo(Boolean withCommentAttachmentInfo) {
this.withCommentAttachmentInfo = withCommentAttachmentInfo;
}

@CamundaQueryParam(value = "withTaskVariablesInReturn", converter = BooleanConverter.class)
public void setWithTaskVariablesInReturn(Boolean withTaskVariablesInReturn) {
this.withTaskVariablesInReturn = withTaskVariablesInReturn;
}

@CamundaQueryParam(value = "withTaskLocalVariablesInReturn", converter = BooleanConverter.class)
public void setWithTaskLocalVariablesInReturn(Boolean withTaskLocalVariablesInReturn) {
this.withTaskLocalVariablesInReturn = withTaskLocalVariablesInReturn;
}

@Override
protected boolean isValidSortByValue(String value) {
return VALID_SORT_BY_VALUES.contains(value);
Expand Down Expand Up @@ -1097,6 +1111,14 @@ public Boolean isVariableValuesIgnoreCase() {

public Boolean getWithCommentAttachmentInfo() { return withCommentAttachmentInfo;}

public Boolean getWithTaskVariablesInReturn() {
return withTaskVariablesInReturn;
}

public Boolean getWithTaskLocalVariablesInReturn() {
return withTaskLocalVariablesInReturn;
}

@Override
protected void applyFilters(TaskQuery query) {
if (orQueries != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
Expand All @@ -34,6 +35,8 @@
import org.camunda.bpm.engine.rest.dto.task.TaskDto;
import org.camunda.bpm.engine.rest.dto.task.TaskQueryDto;
import org.camunda.bpm.engine.rest.dto.task.TaskWithAttachmentAndCommentDto;
import org.camunda.bpm.engine.rest.dto.task.TaskWithVariablesDto;
import org.camunda.bpm.engine.rest.dto.VariableValueDto;
import org.camunda.bpm.engine.rest.exception.InvalidRequestException;
import org.camunda.bpm.engine.rest.hal.Hal;
import org.camunda.bpm.engine.rest.hal.task.HalTaskList;
Expand All @@ -44,6 +47,7 @@
import org.camunda.bpm.engine.rest.util.QueryUtil;
import org.camunda.bpm.engine.task.Task;
import org.camunda.bpm.engine.task.TaskQuery;
import org.camunda.bpm.engine.variable.VariableMap;

public class TaskRestServiceImpl extends AbstractRestProcessEngineAware implements TaskRestService {

Expand Down Expand Up @@ -96,8 +100,19 @@ public List<TaskDto> queryTasks(TaskQueryDto queryDto, Integer firstResult,
TaskQuery query = queryDto.toQuery(engine);

List<Task> matchingTasks = executeTaskQuery(firstResult, maxResults, query);

List<TaskDto> tasks = new ArrayList<TaskDto>();

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 104 - 115 seems unececcerly complicated. I think this would mean the same:

    boolean withTaskVars = Boolean.TRUE.equals(queryDto.getWithTaskVariablesInReturn());
    boolean withTaskLocalVars = Boolean.TRUE.equals(queryDto.getWithTaskLocalVariablesInReturn());
    boolean withCommentInfo = Boolean.TRUE.equals(queryDto.getWithCommentAttachmentInfo());

    if (withTaskVars || withTaskLocalVars){
        return getVariablesForTasks(engine, matchingTasks, withTaskVars, withCommentInfo);
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@venetrius I agree with your comments. Ill push the updates shortly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have pushed the changes. Can you please take a look?

if ((Boolean.TRUE.equals(queryDto.getWithTaskVariablesInReturn()) || Boolean.TRUE.equals(
queryDto.getWithTaskLocalVariablesInReturn())) && Boolean.TRUE.equals(
queryDto.getWithCommentAttachmentInfo())) {
return getVariablesForTasks(engine, matchingTasks, Boolean.TRUE.equals(queryDto.getWithTaskVariablesInReturn()),
true);
}
if (Boolean.TRUE.equals(queryDto.getWithTaskVariablesInReturn()) || Boolean.TRUE.equals(
queryDto.getWithTaskLocalVariablesInReturn())) {
return getVariablesForTasks(engine, matchingTasks, Boolean.TRUE.equals(queryDto.getWithTaskVariablesInReturn()),
false);
}
if (Boolean.TRUE.equals(queryDto.getWithCommentAttachmentInfo())) {
tasks = matchingTasks.stream().map(TaskWithAttachmentAndCommentDto::fromEntity).collect(Collectors.toList());
}
Expand Down Expand Up @@ -163,4 +178,27 @@ public void createTask(TaskDto taskDto) {
public TaskReportResource getTaskReportResource() {
return new TaskReportResourceImpl(getProcessEngine());
}

private List<TaskDto> getVariablesForTasks(ProcessEngine engine,
List<Task> matchingTasks,
boolean withTaskVariablesInReturn,
boolean withCommentAndAttachments) {
TaskService taskService = engine.getTaskService();
List<TaskDto> tasks = new ArrayList<TaskDto>();
for (Task task : matchingTasks) {
VariableMap taskVariables;
if (withTaskVariablesInReturn) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This loop can introduce 2 extra DB query per task instance. One to get Variables and one for the Comments.
Did you consider if you could do it with fewer DB calls?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @venetrius , Here one call is being made for each task in the loop - either taskVars or taskLocalVars. The comments and attachment info is already obtained and just used to populate the DTO accordingly. Please let me know your thoughts.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@venetrius , I understand that there is a DB call made to get the comment and attachment info additional to getAllTask, while getting the list of tasks (matchingTasks). I understand in the above loop, one additional call is being made to get either taskVariables or taskLocalVariables to be added to the response. OpenAPI documentation is updated with exercising caution while using the comment/attachment request. Do you think its apt to add another cautionary note in the documentation for the variablesInReturn query as well? Please provide your thoughts and direction on how to proceed further.

taskVariables = taskService.getVariablesTyped(task.getId(), true);
} else {
taskVariables = taskService.getVariablesLocalTyped(task.getId(), true);
}
Map<String, VariableValueDto> taskVariablesDto = VariableValueDto.fromMap(taskVariables);
if (withCommentAndAttachments) {
tasks.add(TaskWithAttachmentAndCommentDto.fromEntity(task, taskVariablesDto));
} else {
tasks.add(TaskWithVariablesDto.fromEntity(task, taskVariablesDto));
}
}
return tasks;
}
}