activiti工作流代码抽取
package base;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.runtime.ExecutionQuery;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.runtime.ProcessInstanceQuery;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
public class BaseBpmn {
// 获取核心对象 服务大管家
protected ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
// 获取运行的service服务
protected RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();
// 获取TaskService
protected TaskService taskService = defaultProcessEngine.getTaskService();
/**
* 部署文件
*
* @param resource
* @param name
*/
public void deploy(String resource, String name) {
// 获取核心对象 服务大管家
// 获取服务
RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();
// 部署对象
DeploymentBuilder createDeployment = repositoryService.createDeployment();
// 上传资源
InputStream inputStream = this.getClass().getResourceAsStream(resource + ".bpmn");
InputStream inputStream2 = this.getClass().getResourceAsStream(resource + ".png");
createDeployment.addInputStream(resource + ".bpmn", inputStream);
createDeployment.addInputStream(resource + ".png", inputStream2);
createDeployment.name(name);
// 部署
createDeployment.deploy();
}
/**
* 启动实例带流程变量
*/
public ProcessInstance start(String key, Map<String, Object> variables) {
// 启动
// 设置一下变量
ProcessInstance startProcessInstanceByKey = runtimeService.startProcessInstanceByKey(key, variables);
return startProcessInstanceByKey;
}
/**
* 启动实例带流程变量
*/
public ProcessInstance start(String key) {
// 启动
// 设置一下变量
ProcessInstance startProcessInstanceByKey = runtimeService.startProcessInstanceByKey(key);
return startProcessInstanceByKey;
}
/**
* 完成任务 带流程变量 方便流转到下以个节点
*/
public void compleTask(String taskId, Map<String, Object> variables) throws Exception {
taskService.complete(taskId, variables);
}
/**
* 完成任务
*/
public void compleTask(String taskId) throws Exception {
taskService.complete(taskId);
}
/**
* 完成任务 实例id 和 人的名称
*/
public void compleTaskByPIdAndName(String pid, String name) throws Exception {
// 获取查询对象
TaskQuery createTaskQuery = taskService.createTaskQuery();
// 获取任务id
Task task = createTaskQuery.processInstanceId(pid).taskAssignee(name).singleResult();
// 完成
taskService.complete(task.getId());
}
/**
* 完成任务 实例id 和 人的名称
*/
public void compleTaskByPIdAndName(String pid, String name, Map<String, Object> variables) throws Exception {
// 获取查询对象
TaskQuery createTaskQuery = taskService.createTaskQuery();
// 获取任务id
Task task = createTaskQuery.processInstanceId(pid).taskAssignee(name).singleResult();
// 完成
taskService.complete(task.getId(), variables);
}
/**
* 查询某个人的任务列表
*/
public List<Task> queryTaskList(String name) throws Exception {
// 获取查询对象
TaskQuery createTaskQuery = taskService.createTaskQuery();
// 设置查询条件
createTaskQuery.taskAssignee(name);
// 查询列表
List<Task> list = createTaskQuery.list();
return list;
}
/**
* 查询某个人的当前任务
*/
public Task queryTaskByName(String pid, String name) throws Exception {
// 获取查询对象
TaskQuery createTaskQuery = taskService.createTaskQuery();
// 设置查询条件
createTaskQuery.taskAssignee(name);
createTaskQuery.processInstanceId(pid);
//
Task task = createTaskQuery.singleResult();
return task;
}
/**
* 查询当前执行对象(当前活动节点)
*/
public Execution queryExcution(String pid, String actid) throws Exception {
// 获取查询对象
ExecutionQuery createExecutionQuery = runtimeService.createExecutionQuery();
// 设置查询条件
Execution execution = createExecutionQuery.processInstanceId(pid).activityId(actid).singleResult();
// 返回
return execution;
}
// 查询流程是否结束
public void isEnd(String pid) {
// 查询对象
ProcessInstanceQuery createProcessInstanceQuery = runtimeService.createProcessInstanceQuery();
// 根据id
ProcessInstance singleResult = createProcessInstanceQuery.processInstanceId(pid).singleResult();
// 如果singleResult找到说明流程还没有结束
if (singleResult != null) {
System.out.println("流程未结束或不存在");
} else {
System.out.println("流程结束");
}
}
}