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 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ public interface TaskRestService {
public static final String PATH = "/task";

@Path("/{id}")
TaskResource getTask(@PathParam("id") String id, @QueryParam("withCommentAttachmentInfo") boolean withCommentAttachmentInfo);
TaskResource getTask(@PathParam("id") String id,
@QueryParam("withCommentAttachmentInfo") boolean withCommentAttachmentInfo,
@QueryParam("withTaskVariablesInReturn") boolean withTaskVariablesInReturn,
@QueryParam("withTaskLocalVariablesInReturn") boolean withTaskLocalVariablesInReturn);

@GET
@Produces({MediaType.APPLICATION_JSON, Hal.APPLICATION_HAL_JSON})
Expand Down
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 @@ -16,16 +16,22 @@
*/
package org.camunda.bpm.engine.rest.dto.task;

import java.util.Map;
import org.camunda.bpm.engine.rest.dto.VariableValueDto;
import org.camunda.bpm.engine.task.Task;

public class TaskWithAttachmentAndCommentDto extends TaskDto {
public class TaskWithAttachmentAndCommentDto extends TaskWithVariablesDto {

private boolean hasAttachment;
private boolean hasComment;

public TaskWithAttachmentAndCommentDto() {
}

public TaskWithAttachmentAndCommentDto(Task task, Map<String, VariableValueDto> variables) {
super(task, variables);
}

public TaskWithAttachmentAndCommentDto(Task task) {
super(task);
}
Expand All @@ -44,11 +50,15 @@ public void setComment(boolean hasComment) {
this.hasComment = hasComment;
}

public static TaskDto fromEntity(Task task) {
TaskWithAttachmentAndCommentDto result = new TaskWithAttachmentAndCommentDto(task);

public static TaskDto fromEntity(Task task, Map<String, VariableValueDto> variables) {
TaskWithAttachmentAndCommentDto result = new TaskWithAttachmentAndCommentDto(task, variables);
result.hasAttachment = task.hasAttachment();
result.hasComment = task.hasComment();
return result;
}

public static TaskDto fromEntity(Task task) {
return fromEntity(task, null);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
* under one or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. Camunda licenses this file to you under the Apache License,
* Version 2.0; you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.camunda.bpm.engine.rest.dto.task;

import com.fasterxml.jackson.annotation.JsonInclude;
import java.util.Map;
import org.camunda.bpm.engine.rest.dto.VariableValueDto;
import org.camunda.bpm.engine.task.Task;

public class TaskWithVariablesDto extends TaskDto {
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
private Map<String, VariableValueDto> variables;

public TaskWithVariablesDto() {
}

public TaskWithVariablesDto(Task task, Map<String, VariableValueDto> variables) {
super(task);
this.variables = variables;
}

public TaskWithVariablesDto(Task task) {
super(task);
}

public Map<String, VariableValueDto> getVariables() {
return variables;
}

public void setVariables(Map<String, VariableValueDto> variables) {
this.variables = variables;
}

public static TaskDto fromEntity(Task task, Map<String, VariableValueDto> variables) {
TaskWithVariablesDto result = new TaskWithVariablesDto(task);
result.setVariables(variables);
return result;
}
}
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>();

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 @@ -134,8 +149,12 @@ public CountResultDto queryTasksCount(TaskQueryDto queryDto) {
}

@Override
public TaskResource getTask(String id, boolean withCommentAttachmentInfo) {
return new TaskResourceImpl(getProcessEngine(), id, relativeRootResourcePath, getObjectMapper(), withCommentAttachmentInfo);
public TaskResource getTask(String id,
boolean withCommentAttachmentInfo,
boolean withTaskVariablesInReturn,
boolean withTaskLocalVariablesInReturn) {
return new TaskResourceImpl(getProcessEngine(), id, relativeRootResourcePath, getObjectMapper(),
withCommentAttachmentInfo, withTaskVariablesInReturn, withTaskLocalVariablesInReturn);
}

@Override
Expand All @@ -159,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) {
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,23 @@ public class TaskResourceImpl implements TaskResource {
protected String rootResourcePath;
protected ObjectMapper objectMapper;
protected boolean withCommentAttachmentInfo;

public TaskResourceImpl(ProcessEngine engine, String taskId, String rootResourcePath, ObjectMapper objectMapper, boolean withCommentAttachmentInfo) {
protected boolean withTaskVariablesInReturn;
protected boolean withTaskLocalVariablesInReturn;

public TaskResourceImpl(ProcessEngine engine,
String taskId,
String rootResourcePath,
ObjectMapper objectMapper,
boolean withCommentAttachmentInfo,
boolean withTaskVariablesInReturn,
boolean withTaskLocalVariablesInReturn) {
this.engine = engine;
this.taskId = taskId;
this.rootResourcePath = rootResourcePath;
this.objectMapper = objectMapper;
this.withCommentAttachmentInfo = withCommentAttachmentInfo;
this.withTaskVariablesInReturn = withTaskVariablesInReturn;
this.withTaskLocalVariablesInReturn = withTaskLocalVariablesInReturn;
}

@Override
Expand Down Expand Up @@ -193,10 +203,17 @@ public TaskDto getJsonTask() {
if (task == null) {
throw new InvalidRequestException(Status.NOT_FOUND, "No matching task with id " + taskId);
}
if ((withTaskVariablesInReturn || withTaskLocalVariablesInReturn) && withCommentAttachmentInfo) {
Map<String, VariableValueDto> taskVariables = getTaskVariables(withTaskVariablesInReturn);
return TaskWithAttachmentAndCommentDto.fromEntity(task, taskVariables);
}
if (withCommentAttachmentInfo) {
return TaskWithAttachmentAndCommentDto.fromEntity(task);
return TaskWithAttachmentAndCommentDto.fromEntity(task, null);
}
else {
if (withTaskVariablesInReturn || withTaskLocalVariablesInReturn) {
Map<String, VariableValueDto> taskVariables = getTaskVariables(withTaskVariablesInReturn);
return TaskWithVariablesDto.fromEntity(task, taskVariables);
} else {
return TaskDto.fromEntity(task);
}
}
Expand Down Expand Up @@ -468,4 +485,14 @@ protected <V extends Object> V runWithoutAuthorization(Supplier<V> action) {
}
}

private Map<String, VariableValueDto> getTaskVariables(boolean withTaskVariablesInReturn) {
VariableResource variableResource;
if (withTaskVariablesInReturn) {
variableResource = this.getVariables();
} else {
variableResource = this.getLocalVariables();
}
return variableResource.getVariables(true);
}

}
Loading