Skip to content

Commit

Permalink
add new component to show workflow tasks assigned to user
Browse files Browse the repository at this point in the history
  • Loading branch information
WangLiNaruto committed Jan 29, 2024
1 parent db97e40 commit 255d4aa
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* SPDX-License-Identifier: Apache-2.0
* SPDX-FileCopyrightText: Huawei Inc.
*/

package org.eclipse.xpanse.api.config;

import org.eclipse.xpanse.modules.models.workflow.TaskStatus;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

/**
* Bean for serializing string in request parameters to TaskStatus enum.
*/
@Component
public class TaskStatusEnumConverter implements Converter<String, TaskStatus> {

@Override
public TaskStatus convert(String status) {
return TaskStatus.getByValue(status);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.xpanse.modules.deployment.migration.consts.MigrateConstants;
import org.eclipse.xpanse.modules.models.workflow.TaskStatus;
import org.eclipse.xpanse.modules.models.workflow.WorkFlowTask;
import org.eclipse.xpanse.modules.security.IdentityProviderManager;
import org.eclipse.xpanse.modules.workflow.utils.WorkflowUtils;
Expand All @@ -31,6 +32,7 @@
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

Expand All @@ -51,27 +53,17 @@ public class WorkFlowApi {
private IdentityProviderManager identityProviderManager;

/**
* Query the tasks that need to be handled by the user.
* Query tasks of the given user by status.
*/
@Tag(name = "Workflow", description = "APIs to manage the Workflow")
@Operation(description = "Query the tasks that need to be handled by the user")
@GetMapping(value = "/workflow/task/todo", produces = MediaType.APPLICATION_JSON_VALUE)
@Operation(description = "Query all tasks of the given user")
@GetMapping(value = "/workflow/tasks", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public List<WorkFlowTask> queryTodoTasks() {
public List<WorkFlowTask> queryTasks(
@Parameter(name = "status", description = "the status of task")
@RequestParam(name = "status", required = false) TaskStatus status) {
Optional<String> userIdOptional = identityProviderManager.getCurrentLoginUserId();
return workflowUtils.todoTasks(userIdOptional.orElse(null));
}

/**
* Query the tasks the given has completed.
*/
@Tag(name = "Workflow", description = "APIs to manage the Workflow")
@Operation(description = "Query the tasks the given user has completed")
@GetMapping(value = "/workflow/task/done", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public List<WorkFlowTask> queryDoneTasks() {
Optional<String> userIdOptional = identityProviderManager.getCurrentLoginUserId();
return workflowUtils.doneTasks(userIdOptional.orElse(null));
return workflowUtils.queryAllTasks(status, userIdOptional.orElse(null));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* SPDX-License-Identifier: Apache-2.0
* SPDX-FileCopyrightText: Huawei Inc.
*/

package org.eclipse.xpanse.modules.models.workflow;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.xpanse.modules.models.common.exceptions.UnsupportedEnumValueException;

/**
* Task Status enums.
*/
public enum TaskStatus {
DONE("done"),
FAILED("failed");

private final String status;

TaskStatus(String status) {
this.status = status;
}

/**
* For Category deserialize.
*/
@JsonCreator
public static TaskStatus getByValue(String status) {
for (TaskStatus taskStatus : values()) {
if (StringUtils.equalsIgnoreCase(taskStatus.status, status)) {
return taskStatus;
}
}
throw new UnsupportedEnumValueException(
String.format("Category value %s is not supported.", status));
}

/**
* For Category serialize.
*/
@JsonValue
public String toValue() {
return this.status;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public class WorkFlowTask {
@Schema(description = "The businessKey of the Process")
private String businessKey;

@NotNull
@Schema(description = "The status of the Task")
private TaskStatus status;

@NotNull
@Schema(description = "The create time of the task")
private Date createTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ void testHashCode() {
void testToString() {
String result = "WorkFlowTask(processInstanceId=null, processInstanceName=null,"
+ " processDefinitionId=null, processDefinitionName=null, executionId=null,"
+ " taskId=null, taskName=null, businessKey=null, createTime=mockCreateTime)";
+ " taskId=null, taskName=null, businessKey=null, status=null, "
+ "createTime=mockCreateTime)";

final WorkFlowTask workFlowTaskUnderTest = new WorkFlowTask();
workFlowTaskUnderTest.setCreateTime(mockCreateTime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package org.eclipse.xpanse.modules.workflow.utils;

import jakarta.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand All @@ -22,6 +23,7 @@
import org.activiti.engine.task.TaskInfo;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.xpanse.modules.models.service.deploy.exceptions.ServiceNotDeployedException;
import org.eclipse.xpanse.modules.models.workflow.TaskStatus;
import org.eclipse.xpanse.modules.models.workflow.WorkFlowTask;
import org.springframework.stereotype.Component;

Expand Down Expand Up @@ -84,6 +86,26 @@ public List<WorkFlowTask> doneTasks(String userId) {
return transHistoricTaskInstanceToWorkFlowTask(list);
}

/**
* Query all tasks of the given user.
*
* @param userId userId the ID of the currently logged in user.
*/
public List<WorkFlowTask> queryAllTasks(TaskStatus status, String userId) {
List<WorkFlowTask> workFlowTasks = new ArrayList<>();
List<WorkFlowTask> todoTasks = todoTasks(userId);
List<WorkFlowTask> doneTasks = doneTasks(userId);
if (Objects.isNull(status)) {
workFlowTasks.addAll(todoTasks.stream().map(this::setTodoTaskStatus).toList());
workFlowTasks.addAll(doneTasks.stream().map(this::setDoneTaskStatus).toList());
} else if (status == TaskStatus.DONE) {
workFlowTasks.addAll(doneTasks.stream().map(this::setDoneTaskStatus).toList());
} else if (status == TaskStatus.FAILED) {
workFlowTasks.addAll(todoTasks.stream().map(this::setTodoTaskStatus).toList());
}
return workFlowTasks;
}

/**
* Complete tasks based on task ID and set global process variables.
*
Expand Down Expand Up @@ -147,4 +169,14 @@ private void validateTaskId(String taskId) {
+ "taskId: " + taskId);
}
}

private WorkFlowTask setTodoTaskStatus(WorkFlowTask workFlowTask) {
workFlowTask.setStatus(TaskStatus.FAILED);
return workFlowTask;
}

private WorkFlowTask setDoneTaskStatus(WorkFlowTask workFlowTask) {
workFlowTask.setStatus(TaskStatus.DONE);
return workFlowTask;
}
}

0 comments on commit 255d4aa

Please sign in to comment.