概述
一、任务操作
1、创建任务
package org.crazyit.activiti.TaskService;
/**
* taskService 的 newTask 和 saveTask
*/
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.task.Task;
public class TaskNew {
public static void main(String[] args) {
// 创建流程引擎
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
//获取任务服务组件
TaskService taskService = engine.getTaskService();
//保存第一个Task,不设置ID
Task task1 = taskService.newTask();
taskService.saveTask(task1);
//保存第二个Task,设置ID
Task task2 = taskService.newTask("审核任务");
taskService.saveTask(task2);
}
}
2、任务提醒 /分配
package org.crazyit.activiti.TaskService;
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.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
/**
* 任务提醒 /分配
* @author mz
*
*/
public class TaskClaim {
public static void main(String[] args) {
// 获取流程引擎实例
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
// 获取任务服务组件
TaskService taskService = engine.getTaskService();
// 获取运行服务组件
RuntimeService runtimeService = engine.getRuntimeService();
// 流程存储服务组件
RepositoryService repositoryService = engine.getRepositoryService();
// 部署流程描述文件
Deployment dep = repositoryService.createDeployment()
.addClasspathResource("bpmn/vacation.bpmn").deploy();
// 查找流程定义
ProcessDefinition pd = repositoryService.createProcessDefinitionQuery()
.deploymentId(dep.getId()).singleResult();
// 启动流程
ProcessInstance pi = runtimeService
.startProcessInstanceById(pd.getId());
// 查找任务
Task task = taskService.createTaskQuery().processInstanceId(pi.getId())
.singleResult();
// 调用claim方法
taskService.claim(task.getId(), "1");
// 此处将会抛出异常
taskService.claim(task.getId(), "2");
}
}
3、任务完成
package org.crazyit.activiti.TaskService;
import java.util.HashMap;
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.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
/**
* 任务完成
* @author mz
*
*/
public class TaskComplete {
public static void main(String[] args) {
// 获取流程引擎实例
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
// 获取任务服务组件
TaskService taskService = engine.getTaskService();
// 获取运行服务组件
RuntimeService runtimeService = engine.getRuntimeService();
// 流程存储服务组件
RepositoryService repositoryService = engine.getRepositoryService();
// 部署流程描述文件
Deployment dep = repositoryService.createDeployment()
.addClasspathResource("bpmn/vacation2.bpmn").deploy();
// 查找流程定义
ProcessDefinition pd = repositoryService.createProcessDefinitionQuery()
.deploymentId(dep.getId()).singleResult();
// 启动流程
ProcessInstance pi = runtimeService
.startProcessInstanceById(pd.getId());
// 查找任务
Task task = taskService.createTaskQuery().processInstanceId(pi.getId())
.singleResult();
// 调用complete方法完成任务,传入参数
Map<String, Object> vars = new HashMap<String, Object>();
vars.put("days", 2);
// 设置临时的参数
Map<String, Object> vars2 = new HashMap<String, Object>();
vars2.put("temp", "temp var");
taskService.complete(task.getId(), vars, vars2);
// 再次查找任务
task = taskService.createTaskQuery().processInstanceId(pi.getId())
.singleResult();
// 无法查询临时参数
String tempVar = (String)taskService.getVariable(task.getId(), "temp");
System.out.println("查询临时参数:" + tempVar);
//得到参数
Integer days = (Integer)taskService.getVariable(task.getId(), "days");
if (days > 5) {
System.out.println("大于5天,不批");
} else {
System.out.println("小于5天,完成任务,流程结束");
taskService.complete(task.getId());
}
}
}
4、任务删除
package org.crazyit.activiti.TaskService;
import java.util.ArrayList;
import java.util.List;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
/**
* taskService 的 DeleteTask
*/
public class TaskDelete {
public static void main(String[] args) {
// 创建流程引擎
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
//获取任务服务组件
TaskService taskService = engine.getTaskService();
// 保存若干个Task
for (int i = 1; i < 10; i++) {
saveTask(taskService, String.valueOf(i));
}
// 单个删除
// 删除task(不包括历史数据和子任务)
taskService.deleteTask("1");
// 删除task(包括历史数据和子任务)
taskService.deleteTask("2", true);
// 多个删除
// 删除多个task(不包括历史数据和子任务)
List<String> ids = new ArrayList<String>();
ids.add("3");
ids.add("4");
taskService.deleteTasks(ids);
// 删除多个task(包括历史数据和子任务)
ids = new ArrayList<String>();
ids.add("5");
ids.add("6");
taskService.deleteTasks(ids, true);
// 再删除ID为3的task,此时3的历史数据也会被删除(如果有的话)
taskService.deleteTask("3", true);
}
//保存一个task
static void saveTask(TaskService taskService, String id) {
Task task1 = taskService.newTask(id);
taskService.saveTask(task1);
}
}
二、任务附件操作
1、创建附件
package org.crazyit.activiti.TaskService;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
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.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
/**
* 创建任务附件
* @author mz
*
*/
public class AttachmentCreate {
public static void main(String[] args) throws Exception {
// 获取流程引擎实例
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
// 获取任务服务组件
TaskService taskService = engine.getTaskService();
// 获取运行服务组件
RuntimeService runtimeService = engine.getRuntimeService();
// 流程存储服务组件
RepositoryService repositoryService = engine.getRepositoryService();
// 部署流程描述文件
Deployment dep = repositoryService.createDeployment()
.addClasspathResource("bpmn/vacation.bpmn").deploy();
// 查找流程定义
ProcessDefinition pd = repositoryService.createProcessDefinitionQuery()
.deploymentId(dep.getId()).singleResult();
// 启动流程
ProcessInstance pi = runtimeService
.startProcessInstanceById(pd.getId());
// 查找任务
Task task = taskService.createTaskQuery().processInstanceId(pi.getId())
.singleResult();
// 设置任务附件
taskService.createAttachment("web url", task.getId(), pi.getId(), "163.com",
"163 web page", "http://www.163.com");
// 创建图片输入流
InputStream is = new FileInputStream(new File("resource/artifact/result.png"));
// 设置输入流为任务附件
taskService.createAttachment("web url", task.getId(), pi.getId(), "163.com",
"163 web page", is);
}
}
2、查询附件
package org.crazyit.activiti.TaskService;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
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.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Attachment;
import org.activiti.engine.task.Task;
/**
* 附件查询
* @author mz
*
*/
public class AttachmentGet {
public static void main(String[] args) throws Exception {
// 获取流程引擎实例
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
// 获取任务服务组件
TaskService taskService = engine.getTaskService();
// 获取运行服务组件
RuntimeService runtimeService = engine.getRuntimeService();
// 流程存储服务组件
RepositoryService repositoryService = engine.getRepositoryService();
// 部署流程描述文件
Deployment dep = repositoryService.createDeployment()
.addClasspathResource("bpmn/vacation.bpmn").deploy();
// 查找流程定义
ProcessDefinition pd = repositoryService.createProcessDefinitionQuery()
.deploymentId(dep.getId()).singleResult();
// 启动流程
ProcessInstance pi = runtimeService
.startProcessInstanceById(pd.getId());
// 查找任务
Task task = taskService.createTaskQuery().processInstanceId(pi.getId())
.singleResult();
// 设置任务附件
Attachment att1 = taskService.createAttachment("web url", task.getId(), pi.getId(), "Attachement1",
"163 web page", "http://www.163.com");
// 创建图片输入流
InputStream is = new FileInputStream(new File("resource/artifact/result.png"));
// 设置输入流为任务附件
Attachment att2 = taskService.createAttachment("web url", task.getId(), pi.getId(), "Attachement2",
"Image InputStream", is);
// 根据流程实例ID查询附件
List<Attachment> attas1 = taskService.getProcessInstanceAttachments(pi.getId());
System.out.println("流程附件数量:" + attas1.size());
// 根据任务ID查询附件
List<Attachment> attas2 = taskService.getTaskAttachments(task.getId());
System.out.println("任务附件数量:" + attas2.size());
// 根据附件ID查询附件
Attachment attResult = taskService.getAttachment(att1.getId());
System.out.println("附件1名称:" + attResult.getName());
// 根据附件ID查询附件内容
InputStream stream1 = taskService.getAttachmentContent(att1.getId());
System.out.println("附件1的输入流:" + stream1);
InputStream stream2 = taskService.getAttachmentContent(att2.getId());
System.out.println("附件2的输入流:" + stream2);
}
}
3、删除附件
package org.crazyit.activiti.TaskService;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
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.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Attachment;
import org.activiti.engine.task.Task;
/**
* 创建任务附件
* @author mz
*
*/
public class AttachmentDelete {
public static void main(String[] args) throws Exception {
// 获取流程引擎实例
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
// 获取任务服务组件
TaskService taskService = engine.getTaskService();
// 获取运行服务组件
RuntimeService runtimeService = engine.getRuntimeService();
// 流程存储服务组件
RepositoryService repositoryService = engine.getRepositoryService();
// 部署流程描述文件
Deployment dep = repositoryService.createDeployment()
.addClasspathResource("bpmn/vacation.bpmn").deploy();
// 查找流程定义
ProcessDefinition pd = repositoryService.createProcessDefinitionQuery()
.deploymentId(dep.getId()).singleResult();
// 启动流程
ProcessInstance pi = runtimeService
.startProcessInstanceById(pd.getId());
// 查找任务
Task task = taskService.createTaskQuery().processInstanceId(pi.getId())
.singleResult();
// 设置任务附件
Attachment att1 = taskService.createAttachment("web url", task.getId(), pi.getId(), "Attachement1",
"163 web page", "http://www.163.com");
// 创建图片输入流
InputStream is = new FileInputStream(new File("resource/artifact/result.png"));
// 设置输入流为任务附件
Attachment att2 = taskService.createAttachment("web url", task.getId(), pi.getId(), "Attachement2",
"Image InputStream", is);
System.out.println("删除前数量:" + taskService.getTaskAttachments(task.getId()).size());
taskService.deleteAttachment(att2.getId());
System.out.println("删除后数量:" + taskService.getTaskAttachments(task.getId()).size());
}
}
三、任务评论与事件记录操作
1、事件记录
package org.crazyit.activiti.TaskService;
import java.util.List;
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.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Attachment;
import org.activiti.engine.task.Comment;
import org.activiti.engine.task.Event;
import org.activiti.engine.task.Task;
/**
* 事件记录
* @author mz
*
*/
public class CommentEventAdd {
public static void main(String[] args) {
// 获取流程引擎实例
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
// 获取任务服务组件
TaskService taskService = engine.getTaskService();
// 获取运行服务组件
RuntimeService runtimeService = engine.getRuntimeService();
// 流程存储服务组件
RepositoryService repositoryService = engine.getRepositoryService();
// 部署流程描述文件
Deployment dep = repositoryService.createDeployment()
.addClasspathResource("bpmn/vacation.bpmn").deploy();
// 查找流程定义
ProcessDefinition pd = repositoryService.createProcessDefinitionQuery()
.deploymentId(dep.getId()).singleResult();
// 启动流程
ProcessInstance pi = runtimeService
.startProcessInstanceById(pd.getId());
// 查找任务
Task task = taskService.createTaskQuery().processInstanceId(pi.getId())
.singleResult();
// 调用各个记录事件的方法
taskService.addComment(task.getId(), pi.getId(), "this is comment message");
taskService.addUserIdentityLink(task.getId(), "1", "user");
taskService.deleteUserIdentityLink(task.getId(), "1", "user");
taskService.addGroupIdentityLink(task.getId(), "1", "group");
taskService.deleteGroupIdentityLink(task.getId(), "1", "group");
Attachment atta = taskService.createAttachment("test", task.getId(), pi.getId(), "test", "test", "");
taskService.deleteAttachment(atta.getId());
// 查询Comment和Event
List<Comment> comments = taskService.getTaskComments(task.getId());
System.out.println("总共的评论数量:" + comments.size());
List<Event> events = taskService.getTaskEvents(task.getId());
System.out.println("总共的事件数量:" + events.size());
}
}
2、任务备注/评论
package org.crazyit.activiti.TaskService;
import java.util.List;
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.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Comment;
import org.activiti.engine.task.Task;
/**
* 任务备注/评论
* @author mz
*
*/
public class CommentRecordAdd {
/**
* @param args
*/
public static void main(String[] args) {
// 获取流程引擎实例
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
// 获取任务服务组件
TaskService taskService = engine.getTaskService();
// 获取运行服务组件
RuntimeService runtimeService = engine.getRuntimeService();
// 流程存储服务组件
RepositoryService repositoryService = engine.getRepositoryService();
// 部署流程描述文件
Deployment dep = repositoryService.createDeployment()
.addClasspathResource("bpmn/vacation.bpmn").deploy();
// 查找流程定义
ProcessDefinition pd = repositoryService.createProcessDefinitionQuery()
.deploymentId(dep.getId()).singleResult();
// 启动流程
ProcessInstance pi = runtimeService
.startProcessInstanceById(pd.getId());
// 查找任务
Task task = taskService.createTaskQuery().processInstanceId(pi.getId())
.singleResult();
// 添加任务评论
taskService.addComment(task.getId(), pi.getId(), "this is comment message");
// 查询评论
List<Comment> comments = taskService.getTaskComments(task.getId());
System.out.println("评论数量:" + comments.size());
}
}
3、备注查询
package org.crazyit.activiti.TaskService;
import java.util.List;
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.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Attachment;
import org.activiti.engine.task.Comment;
import org.activiti.engine.task.Event;
import org.activiti.engine.task.Task;
/**
* ACT_HI_COMMENT表数据查询
* @author mz
*
*/
public class CommentRecordQuery {
public static void main(String[] args) {
// 获取流程引擎实例
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
// 获取任务服务组件
TaskService taskService = engine.getTaskService();
// 获取运行服务组件
RuntimeService runtimeService = engine.getRuntimeService();
// 流程存储服务组件
RepositoryService repositoryService = engine.getRepositoryService();
// 部署流程描述文件
Deployment dep = repositoryService.createDeployment()
.addClasspathResource("bpmn/vacation.bpmn").deploy();
// 查找流程定义
ProcessDefinition pd = repositoryService.createProcessDefinitionQuery()
.deploymentId(dep.getId()).singleResult();
// 启动流程
ProcessInstance pi = runtimeService
.startProcessInstanceById(pd.getId());
// 查找任务
Task task = taskService.createTaskQuery().processInstanceId(pi.getId())
.singleResult();
// 调用各个记录事件的方法
taskService.addComment(task.getId(), pi.getId(), "this is comment message");
taskService.addUserIdentityLink(task.getId(), "1", "user");
taskService.deleteUserIdentityLink(task.getId(), "1", "user");
taskService.addGroupIdentityLink(task.getId(), "1", "group");
taskService.deleteGroupIdentityLink(task.getId(), "1", "group");
Attachment atta = taskService.createAttachment("test", task.getId(), pi.getId(), "test", "test", "");
taskService.deleteAttachment(atta.getId());
// 查询事件与评论
List<Comment> commonts1 = taskService.getProcessInstanceComments(pi.getId());
System.out.println("流程评论(事件)数量:" + commonts1.size());
commonts1 = taskService.getTaskComments(task.getId());
System.out.println("任务评论数量:" + commonts1.size());
List<Event> events = taskService.getTaskEvents(task.getId());
System.out.println("事件数量:" + events.size());
}
}
四、任务权限操作
1、绑定用户组和任务关系
package org.crazyit.activiti.TaskService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.TaskService;
import org.activiti.engine.identity.Group;
import org.activiti.engine.task.Task;
/**
* 使用addCandidateGroup方法
* @author mz
*
*/
public class PermissionAddCandidateGroup {
public static void main(String[] args) {
//获取流程引擎实例
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
// 获取身份服务组件
IdentityService identityService = engine.getIdentityService();
// 新建用户组
Group groupA = createGroup(identityService, "groupOK", "经理组OK", "managerOK");
// 获取任务服务组件
TaskService taskService = engine.getTaskService();
//保存第一个Task
Task task1 = taskService.newTask("taskOK1");
taskService.saveTask(task1);
//保存第二个Task
Task task2 = taskService.newTask("taskOK2");
taskService.saveTask(task2);
//绑定用户组和任务关系
taskService.addCandidateGroup("taskOK1", groupA.getId());
taskService.addCandidateGroup("taskOK2", groupA.getId());
}
// 将用户组数据保存到数据库中
static Group createGroup(IdentityService identityService, String id,
String name, String type) {
// 调用newGroup方法创建Group实例
Group group = identityService.newGroup(id);
group.setName(name);
group.setType(type);
identityService.saveGroup(group);
return identityService.createGroupQuery().groupId(id).singleResult();
}
}
2、绑定用户与任务关系
package org.crazyit.activiti.TaskService;
import java.util.UUID;
import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.TaskService;
import org.activiti.engine.identity.User;
import org.activiti.engine.task.Task;
/**
* 使用addCandidateUser方法
*
* @author mz
*
*/
public class PermissionAddCandidateUser {
public static void main(String[] args) {
// 获取流程引擎实例
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
// 获取身份服务组件
IdentityService identityService = engine.getIdentityService();
// 新建用户
User user = creatUser(identityService, UUID.randomUUID().toString(),
"张三", "lastname", "abc@163.com", "123");
User user2 = creatUser(identityService, UUID.randomUUID().toString(),
"李四", "lastname", "abc@163.com", "123");
// 获取任务服务组件
TaskService taskService = engine.getTaskService();
// 保存一个Task
Task task1 = taskService.newTask("task1");
taskService.saveTask(task1);
// 绑定用户与任务关系
taskService.addCandidateUser("task1", user.getId());
taskService.addCandidateUser("task1", user2.getId());
}
// 创建用户方法
static User creatUser(IdentityService identityService, String id,
String first, String last, String email, String passwd) {
// 使用newUser方法创建User实例
User user = identityService.newUser(id);
// 设置用户的各个属性
user.setFirstName(first);
user.setLastName(last);
user.setEmail(email);
user.setPassword(passwd);
// 使用saveUser方法保存用户
identityService.saveUser(user);
return identityService.createUserQuery().userId(id).singleResult();
}
}
3、 添加任务权限数据
package org.crazyit.activiti.TaskService;
import java.util.List;
import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.TaskService;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.User;
import org.activiti.engine.task.IdentityLink;
import org.activiti.engine.task.IdentityLinkType;
import org.activiti.engine.task.Task;
/**
* 添加任务权限数据
* @author mz
*
*/
public class PermissionAddIdentityLink {
public static void main(String[] args) {
//获取流程引擎实例
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
// 获取身份服务组件
IdentityService identityService = engine.getIdentityService();
// 获取任务服务组件
TaskService taskService = engine.getTaskService();
// 新建用户组并保存
Group groupA = createGroup(identityService, "group1", "经理组", "manager");
// 新建用户并保存
User user = creatUser(identityService, "user1", "张三", "last", "abc@163.com", "123");
//保存第一个Task
Task task1 = taskService.newTask("task1");
task1.setName("申请任务");
taskService.saveTask(task1);
//调用addGroupIdentityLink方法
taskService.addGroupIdentityLink(task1.getId(), groupA.getId(), IdentityLinkType.CANDIDATE);
taskService.addGroupIdentityLink(task1.getId(), groupA.getId(), IdentityLinkType.OWNER);
taskService.addGroupIdentityLink(task1.getId(), groupA.getId(), IdentityLinkType.ASSIGNEE);
//调用addUserIdentityLink方法
Task task2 = taskService.newTask("task2");
task2.setName("申请任务2");
taskService.saveTask(task2);
taskService.addUserIdentityLink(task2.getId(), user.getId(), IdentityLinkType.CANDIDATE);
taskService.addUserIdentityLink(task2.getId(), user.getId(), IdentityLinkType.OWNER);
taskService.addUserIdentityLink(task2.getId(), user.getId(), IdentityLinkType.ASSIGNEE);
}
//创建用户方法
static User creatUser(IdentityService identityService, String id, String first,
String last, String email, String passwd) {
// 使用newUser方法创建User实例
User user = identityService.newUser(id);
// 设置用户的各个属性
user.setFirstName(first);
user.setLastName(last);
user.setEmail(email);
user.setPassword(passwd);
// 使用saveUser方法保存用户
identityService.saveUser(user);
return identityService.createUserQuery().userId(id).singleResult();
}
// 将用户组数据保存到数据库中
static Group createGroup(IdentityService identityService, String id,
String name, String type) {
// 调用newGroup方法创建Group实例
Group group = identityService.newGroup(id);
group.setName(name);
group.setType(type);
identityService.saveGroup(group);
return identityService.createGroupQuery().groupId(id).singleResult();
}
}
4、删除用户组权限数据
package org.crazyit.activiti.TaskService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.TaskService;
import org.activiti.engine.identity.Group;
import org.activiti.engine.task.IdentityLinkType;
import org.activiti.engine.task.Task;
/**
* 删除用户组权限数据
*
* @author mz
*
*/
public class PermissionDeleteGroupIdentity {
public static void main(String[] args) {
// 获取流程引擎实例
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
// 获取身份服务组件
IdentityService identityService = engine.getIdentityService();
// 新建用户组
Group groupA = createGroup(identityService, "group1", "经理组", "manager");
// 获取任务服务组件
TaskService taskService = engine.getTaskService();
// 保存第一个Task
Task task1 = taskService.newTask("task1");
task1.setName("审批任务");
taskService.saveTask(task1);
// 调用addGroupIdentityLink方法
taskService.addGroupIdentityLink(task1.getId(), groupA.getId(),
IdentityLinkType.CANDIDATE);
taskService.addGroupIdentityLink(task1.getId(), groupA.getId(),
IdentityLinkType.OWNER);
taskService.addGroupIdentityLink(task1.getId(), groupA.getId(),
IdentityLinkType.ASSIGNEE);
// 调用delete方法
taskService.deleteCandidateGroup(task1.getId(), groupA.getId());
// 以下两个s方法将抛出异常
taskService.deleteGroupIdentityLink(task1.getId(), groupA.getId(),
IdentityLinkType.OWNER);
taskService.deleteGroupIdentityLink(task1.getId(), groupA.getId(),
IdentityLinkType.ASSIGNEE);
}
// 将用户组数据保存到数据库中
static Group createGroup(IdentityService identityService, String id,
String name, String type) {
// 调用newGroup方法创建Group实例
Group group = identityService.newGroup(id);
group.setName(name);
group.setType(type);
identityService.saveGroup(group);
return identityService.createGroupQuery().groupId(id).singleResult();
}
}
5、删除用户组权限数据
package org.crazyit.activiti.TaskService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.TaskService;
import org.activiti.engine.identity.User;
import org.activiti.engine.task.IdentityLinkType;
import org.activiti.engine.task.Task;
/**
* 删除用户组权限数据
* @author mz
*
*/
public class PermissionDeleteUserIdentity {
public static void main(String[] args) {
//获取流程引擎实例
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
// 获取身份服务组件
IdentityService identityService = engine.getIdentityService();
// 新建用户
User user = creatUser(identityService, "user1", "first", "last", "abc@163.com", "123");
// 获取任务服务组件
TaskService taskService = engine.getTaskService();
//保存第一个Task
Task task1 = taskService.newTask("task1");
taskService.saveTask(task1);
//添加用户权限
taskService.addCandidateUser(task1.getId(), user.getId());
long count = taskService.createTaskQuery().taskCandidateUser(user.getId()).count();
System.out.println("调用addCandidateUser方法后,用户的候选任务数量:" + count);
//删除用户权限
taskService.deleteCandidateUser(task1.getId(), user.getId());
count = taskService.createTaskQuery().taskCandidateUser(user.getId()).count();
System.out.println("调用deleteCandidateUser方法后,用户的候选任务数量:" + count);
//添加用户权限
taskService.addUserIdentityLink(task1.getId(), user.getId(), IdentityLinkType.OWNER);
count = taskService.createTaskQuery().taskOwner(user.getId()).count();
System.out.println("调用addUserIdentityLink方法后,用户的候选任务数量:" + count);
//删除用户权限
taskService.deleteUserIdentityLink(task1.getId(), user.getId(), IdentityLinkType.OWNER);
count = taskService.createTaskQuery().taskOwner(user.getId()).count();
System.out.println("调用deleteUserIdentityLink方法后,用户的候选任务数量:" + count);
}
//创建用户方法
static User creatUser(IdentityService identityService, String id, String first,
String last, String email, String passwd) {
// 使用newUser方法创建User实例
User user = identityService.newUser(id);
// 设置用户的各个属性
user.setFirstName(first);
user.setLastName(last);
user.setEmail(email);
user.setPassword(passwd);
// 使用saveUser方法保存用户
identityService.saveUser(user);
return identityService.createUserQuery().userId(id).singleResult();
}
}
6、权限数据查询
package org.crazyit.activiti.TaskService;
import java.util.ArrayList;
import java.util.List;
import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.TaskService;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.User;
import org.activiti.engine.task.IdentityLink;
import org.activiti.engine.task.Task;
/**
* 权限数据查询
* @author mz
*
*/
public class PermissionQuery {
public static void main(String[] args) {
//获取流程引擎实例
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
// 获取身份服务组件
IdentityService identityService = engine.getIdentityService();
// 新建用户
User user = creatUser(identityService, "user1", "张三", "last", "abc@163.com", "123");
// 新建用户组
Group groupA = createGroup(identityService, "group1", "经理组", "manager");
Group groupB = createGroup(identityService, "group2", "员工组", "employee");
// 获取任务服务组件
TaskService taskService = engine.getTaskService();
//保存第一个Task
Task task1 = taskService.newTask("task1");
task1.setName("申请假期");
taskService.saveTask(task1);
//保存第二个Task
Task task2 = taskService.newTask("task2");
task2.setName("审批假期");
taskService.saveTask(task2);
//绑定权限
taskService.addCandidateGroup("task1", groupA.getId());
taskService.addCandidateGroup("task2", groupB.getId());
taskService.addCandidateUser("task2", user.getId());
//根据用户组查询任务
List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup(groupA.getId()).list();
System.out.println("经理组的候选任务有:");
for (Task task : tasks) {
System.out.println(" " + task.getName());
}
//根据用户查询任务
tasks = taskService.createTaskQuery().taskCandidateUser(user.getId()).list();
System.out.println("张三的候选任务有");
for (Task task : tasks) {
System.out.println(" " + task.getName());
}
//调用taskCandidateGroupIn
List<String> groupIds = new ArrayList<String>();
groupIds.add(groupA.getId());
groupIds.add(groupB.getId());
tasks = taskService.createTaskQuery().taskCandidateGroupIn(groupIds).list();
System.out.println("经理组与员工组的任务有:");
for (Task task : tasks) {
System.out.println(" " + task.getName());
}
//查询权限数据
List<IdentityLink> links = taskService.getIdentityLinksForTask(tasks.get(0).getId());
System.out.println("关系数据量: " + links.size());
}
// 将用户组数据保存到数据库中
static Group createGroup(IdentityService identityService, String id,
String name, String type) {
// 调用newGroup方法创建Group实例
Group group = identityService.newGroup(id);
group.setName(name);
group.setType(type);
identityService.saveGroup(group);
return identityService.createGroupQuery().groupId(id).singleResult();
}
//创建用户方法
static User creatUser(IdentityService identityService, String id, String first,
String last, String email, String passwd) {
// 使用newUser方法创建User实例
User user = identityService.newUser(id);
// 设置用户的各个属性
user.setFirstName(first);
user.setLastName(last);
user.setEmail(email);
user.setPassword(passwd);
// 使用saveUser方法保存用户
identityService.saveUser(user);
return identityService.createUserQuery().userId(id).singleResult();
}
}
7、设置任务受理人
package org.crazyit.activiti.TaskService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.TaskService;
import org.activiti.engine.identity.User;
import org.activiti.engine.task.Task;
/**
* 设置任务受理人
*
* @author mz
*
*/
public class PermissionSetAssignee {
public static void main(String[] args) {
// 获取流程引擎实例
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
// 获取身份服务组件
IdentityService identityService = engine.getIdentityService();
// 新建用户
User user = creatUser(identityService, "user1", "张三", "last",
"abc@163.com", "123");
// 获取任务服务组件
TaskService taskService = engine.getTaskService();
// 保存一个Task
Task task1 = taskService.newTask("task1");
task1.setName("申请任务");
taskService.saveTask(task1);
// 设置任务持有人
taskService.setAssignee(task1.getId(), user.getId());
System.out.println("用户张三受理的任务数量:"
+ taskService.createTaskQuery().taskAssignee(user.getId())
.count());
}
// 创建用户方法
static User creatUser(IdentityService identityService, String id,
String first, String last, String email, String passwd) {
// 使用newUser方法创建User实例
User user = identityService.newUser(id);
// 设置用户的各个属性
user.setFirstName(first);
user.setLastName(last);
user.setEmail(email);
user.setPassword(passwd);
// 使用saveUser方法保存用户
identityService.saveUser(user);
return identityService.createUserQuery().userId(id).singleResult();
}
}
8、设置任务持有人
package org.crazyit.activiti.TaskService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.TaskService;
import org.activiti.engine.identity.User;
import org.activiti.engine.task.Task;
/**
* 设置任务持有人
*
* @author mz
*
*/
public class PermissionSetOwner {
public static void main(String[] args) {
// 获取流程引擎实例
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
// 获取身份服务组件
IdentityService identityService = engine.getIdentityService();
// 新建用户
User user = creatUser(identityService, "user1", "张三", "last",
"abc@163.com", "123");
// 获取任务服务组件
TaskService taskService = engine.getTaskService();
// 保存一个Task
Task task1 = taskService.newTask("task1 ");
task1.setName("申请任务");
taskService.saveTask(task1);
// 设置任务持有人
taskService.setOwner(task1.getId(), user.getId());
System.out
.println("用户张三持有任务数量:"
+ taskService.createTaskQuery().taskOwner(user.getId())
.count());
}
// 创建用户方法
static User creatUser(IdentityService identityService, String id,
String first, String last, String email, String passwd) {
// 使用newUser方法创建User实例
User user = identityService.newUser(id);
// 设置用户的各个属性
user.setFirstName(first);
user.setLastName(last);
user.setEmail(email);
user.setPassword(passwd);
// 使用saveUser方法保存用户
identityService.saveUser(user);
return identityService.createUserQuery().userId(id).singleResult();
}
}
最后
以上就是明亮母鸡为你收集整理的五、流程任务操作 - TaskService的全部内容,希望文章能够帮你解决五、流程任务操作 - TaskService所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复