1 流程:
设计流程图:
其中:
2 代码:
controller代码,包含基本的任务部署、启动、查询、办理等
package com.v.activiti.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.activiti.engine.FormService;
import org.activiti.engine.HistoryService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.ManagementService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.v.activiti.bean.TaskView;
import com.v.common.bean.OperationResult;
import com.v.common.utils.StringUtil;
@RestController
@RequestMapping("activiti")
public class ActivitiController {
@Autowired
ProcessEngine processEngine;
@Autowired
RepositoryService repositoryService;
@Autowired
RuntimeService runtimeService;
@Autowired
TaskService taskService;
@Autowired
IdentityService identityService;
@Autowired
FormService formService;
@Autowired
HistoryService historyService;
@Autowired
ManagementService managementService;
@RequestMapping(value = "deploy", method = RequestMethod.POST)
public OperationResult<Object> deploy() {
repositoryService.createDeployment().addClasspathResource("activiti/v.bpmn").deploy();
return new OperationResult<>(true, "200");
}
@RequestMapping(value = "start/{key}/{role}", method = RequestMethod.POST)
public OperationResult<Object> start(@PathVariable String key, String role) {
Map<String, Object> variables = new HashMap<>();
variables.put("role", role);// 设置角色组或者用户组,可用任务监听器的create动态设置当前办理人
runtimeService.startProcessInstanceByKey(key, variables);
return new OperationResult<>(true, "200");
}
@RequestMapping(value = "tasks/{userId}", method = RequestMethod.GET)
public OperationResult<Object> queryTask(@PathVariable String userId) {
List<Task> tasks = taskService.createTaskQuery().taskAssignee(userId).list();
List<TaskView> views = new ArrayList<>();
if (!StringUtil.isEmpty(tasks)) {
for (Task task : tasks) {
TaskView taskView = new TaskView(task.getId(), task.getName());
views.add(taskView);
}
}
return new OperationResult<Object>(true, "200", views);
}
@RequestMapping(value = "takeTask/{taskId}", method = RequestMethod.POST)
public OperationResult<Object> takeTask(@PathVariable String taskId) {
taskService.claim(taskId, "user");
return new OperationResult<>(true, "200");
}
@RequestMapping(value = "returnTask/{taskId}", method = RequestMethod.POST)
public OperationResult<Object> returnTask(@PathVariable String taskId) {
taskService.claim(taskId, null);
return new OperationResult<>(true, "200");
}
@RequestMapping(value = "complete/{taskId}/{param}", method = RequestMethod.POST)
public OperationResult<Object> complete(@PathVariable String taskId, @PathVariable String param) {
taskService.setVariable(taskId, "i", param);
taskService.complete(taskId);
return new OperationResult<>(true, "200");
}
@RequestMapping(value = "deleteTask/{taskId}/{param}", method = RequestMethod.DELETE)
public OperationResult<Object> deleteTask(@PathVariable String taskId) {
taskService.deleteTask(taskId, true);
return new OperationResult<>(true, "200");
}
}