- 配置多用户
id | GROUP_ID | TYPE | USER_ID | TASK_ID | PROC_INST_ID_ |
---|
35006 | 1 | candidate | xd | 35005 | |
35007 | 1 | participant | xd | 35001 | |
35008 | 1 | candidate | xf | 35005 | |
35009 | 1 | participant | xf | 35001 | |
35010 | 1 | candidate | xe | 35005 | |
35011 | 1 | participant | xe | 35001 | |
35012 | 1 | candidate | xa | 35005 | |
35013 | 1 | participant | xa | 35001 | |
查询组任务列表
@Test
public void findGroupList(){
String userId = "xa";
List<Task> list = proEng.getTaskService()
.createTaskQuery()
.taskCandidateUser(userId)
.list();
for(Task task:list ){
System.out.println("id="+task.getId());
System.out.println("name="+task.getName());
System.out.println("assinee="+task.getAssignee());
System.out.println("createTime ="+task.getCreateTime());
System.out.println("executionId="+task.getExecutionId());
System.out.println("##################################");
}
}
查询组任务成员列表
@Test
public void findGroupUser(){
String taskId = "35005";
List<IdentityLink> list = proEng.getTaskService()
.getIdentityLinksForTask(taskId);
for(IdentityLink identityLink:list ){
System.out.println("userId="+identityLink.getUserId());
System.out.println("taskId="+identityLink.getTaskId());
System.out.println("piId="+identityLink.getProcessInstanceId());
System.out.println("######################");
}
}
查询组任务成员历史
列表
@Test
public void findGroupHisUser(){
String taskId = "35005";
List<HistoricIdentityLink> list = proEng.getHistoryService()
.getHistoricIdentityLinksForTask(taskId);
for(HistoricIdentityLink identityLink:list ){
System.out.println("userId="+identityLink.getUserId());
System.out.println("taskId="+identityLink.getTaskId());
System.out.println("piId="+identityLink.getProcessInstanceId());
System.out.println("######################");
}
}
分配组任务
@Test
public void claim(){
String taskId = "35005";
String userId = "xb";
proEng.getTaskService()
.claim(taskId, userId);
}
@Test
public void claimRollback(){
String taskId = "35005";
proEng.getTaskService()
.claim(taskId, null);
}
说明:
- 小A,小B,小C,小D是组任务的办理人
- 但是这样分配组任务的办理人不够灵活,因为项目开发中任务的办理人不要放置XML文件中。
- act_ru_identitylink表存放任务的办理人,包括个人任务和组任务,表示正在执行的任务
- act_hi_identitylink表存放任务的办理人,包括个人任务和组任务,表示历史任务
- 区别在于:
- 如果是个人任务TYPE的类型表示participant(参与者)
- 如果是组任务TYPE的类型表示candidate(候选者)和participant(参与者)
使用流程变量
- candidate Users
${userIds}
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("userIDs", "大大,小小,中中");
ProcessInstance pi = processEngine.getRuntimeService()
.startProcessInstanceByKey("taskProcess",variables);
System.out.println("pid:" + pi.getId());
使用监听器
public class TaskListenerImpl implements TaskListener {
@Override
public void notify(DelegateTask delegateTask) {
String userId1 = "孙悟空";
String userId2 = "猪八戒";
delegateTask.addCandidateUser(userId1);
delegateTask.addCandidateUser(userId2);
}
}
使用角色组
<process id="gr_0903" name="group_candid" isExecutable="true">
<startEvent id="startevent1" name="Start"></startEvent>
<endEvent id="endevent1" name="End"></endEvent>
<userTask id="usertask1" name="提交申请" activiti:candidateGroups="admin_0903"></userTask>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
<sequenceFlow id="flow2" sourceRef="usertask1" targetRef="endevent1"></sequenceFlow>
</process>
package com.osvue.bpmn.activitiapp.bpmn.identity;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricIdentityLink;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.User;
import org.activiti.engine.impl.persistence.entity.GroupEntity;
import org.activiti.engine.impl.persistence.entity.UserEntity;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.IdentityLink;
import org.activiti.engine.task.Task;
import org.junit.Test;
public class GroupTaskManage {
ProcessEngine proEng = ProcessEngines.getDefaultProcessEngine();
@Test
public void init() {
ProcessEngine proEng = ProcessEngines.getDefaultProcessEngine();
}
@Test
public void deploy() {
Deployment dep = proEng.getRepositoryService()
.createDeployment()
.name("groupName_0903")
.addClasspathResource("diagrams/dentity/groupm.bpmn")
.addClasspathResource("diagrams/dentity/groupm.png").deploy();
System.out.println(dep.getId());
}
@Test
public void createGroupUser() {
IdentityService idse = proEng.getIdentityService();
Group group = new GroupEntity("boss_0903");
idse.saveGroup(group);
idse.saveGroup(new GroupEntity("admin_0903"));
idse.saveGroup(new GroupEntity("user_0903"));
idse.saveGroup(new GroupEntity("cw_0903"));
User user = new UserEntity("1");
idse.saveUser(user);
idse.saveUser(new UserEntity("2"));
idse.saveUser(new UserEntity("3"));
idse.saveUser(new UserEntity("4"));
idse.saveUser(new UserEntity("5"));
idse.saveUser(new UserEntity("6"));
idse.createMembership("1", "boss_0903");
idse.createMembership("2", "admin_0903");
idse.createMembership("3", "admin_0903");
idse.createMembership("4", "admin_0903");
idse.createMembership("5", "cw_0903");
idse.createMembership("6", "user_0903");
}
@Test
public void startProcess() {
String processDefinitionId = "";
Map<String,Object> param = new HashMap<>();
ProcessInstance proces = proEng.getRuntimeService()
.startProcessInstanceByKey("gr_0903" );
System.out.println(proces.getName());
}
@Test
public void findTask() {
TaskService taskService = proEng.getTaskService();
String assignee="xb";
List<Task> list = taskService.createTaskQuery().taskAssignee(assignee).list();
if(null!=list&&list.size()>0) {
for (Task task : list) {
System.out.println("任务ID:"+task.getId());
System.out.println("流程实例ID:"+task.getProcessInstanceId());
System.out.println("执行实例ID:"+task.getExecutionId());
System.out.println("流程定义ID:"+task.getProcessDefinitionId());
System.out.println("任务名称:"+task.getName());
System.out.println("任务办理人:"+task.getAssignee());
System.out.println("################################");
}
}
}
@Test
public void findGroupList(){
String userId = "4";
List<Task> list = proEng.getTaskService()
.createTaskQuery()
.taskCandidateUser(userId)
.list();
for(Task task:list ){
System.out.println("任务id="+task.getId());
System.out.println("任务name="+task.getName());
System.out.println("assinee="+task.getAssignee());
System.out.println("createTime ="+task.getCreateTime());
System.out.println("executionId="+task.getExecutionId());
System.out.println("##################################");
}
}
@Test
public void findGroupUser(){
String taskId = "47504";
List<IdentityLink> list = proEng.getTaskService()
.getIdentityLinksForTask(taskId);
for(IdentityLink identityLink:list ){
System.out.println("userId="+identityLink.getUserId());
System.out.println("taskId="+identityLink.getTaskId());
System.out.println("piId="+identityLink.getProcessInstanceId());
System.out.println("######################");
}
}
@Test
public void findGroupHisUser(){
String taskId = "47504";
List<HistoricIdentityLink> list = proEng.getHistoryService()
.getHistoricIdentityLinksForTask(taskId);
for(HistoricIdentityLink identityLink:list ){
System.out.println("userId="+identityLink.getUserId());
System.out.println("taskId="+identityLink.getTaskId());
System.out.println("piId="+identityLink.getProcessInstanceId());
System.out.println("piId="+identityLink.getGroupId());
System.out.println("######################");
}
}
@Test
public void doTask(){
TaskService taskService = proEng.getTaskService();
String taskId="47504";
taskService.setVariable (taskId, "userid", "sgrdbslocal_17778179517");
taskService.complete(taskId);
System.out.println("任务完成");
}
}