学习资料:《Activiti实战》
第五章 用户与组及部署管理(一)用户与组
内容概览:讲解activiti中内置的一套用户、组的关系,以及如何通过API添加、删除、查询。
5.1 用户与组
5.1.1 用户
1 public class IdentityServiceTest{ 2 @Rule 3 public ActivitiRule ar = new ActivitiRule();//使用默认的acitiviti.cfg.xml作为参数,当创建activitirule时,会自动创建引擎对象processEngine。 4 5 @Test 6 public void testUser() throws Exception(){ 7 IdentityService is = ar.getIdentityService(); 8 9 User user = is.newUser("henryyan"); 10 user.setFirstName("Henry"); 11 user.setLastName("yan"); 12 user.setEmail("yanhonglei@gamil.com"); 13 14 is.saveUser(user); 15 16 User userInDb = is.createUserQuery().userId("henryyan").singleResult(); 17 assertNotNull(userInDb); 18 19 is.deleteUser("henryyan"); 20 userInDb = is.createUserQuery().userId("henryyan").singleResult(); 21 assertNull(userInDb); 22 } 23 }
5.1.2 组
1 public class IdentityServiceTest{ 2 @Rule 3 public ActivitiRule ar = new ActivitiRule();//使用默认的acitiviti.cfg.xml作为参数,当创建activitirule时,会自动创建引擎对象processEngine。 4 5 @Test 6 public void testGroup() throws Exception(){ 7 IdentityService is = ar.getIdentityService(); 8 9 Group group = is.newGroup("deptLeader"); 10 group .setName("部门领导"); 11 group .setType("assignment"); 12 13 is.saveGroup(group); 14 15 List<Group> groupList = is.createGroupQuery().groupId("deptLeader").list(); 16 assertEquals(1,groupList.size()); 17 18 is.deleteGroup("deptLeader"); 19 groupList = is.createGroupQuery().groupId("deptLeader").list(); 20 assertEquals(0,groupList.size()); 21 } 22 }
5.1.3 用户与组的关系
1 public class IdentityServiceTest{ 2 @Rule 3 public ActivitiRule ar = new ActivitiRule();//使用默认的acitiviti.cfg.xml作为参数 4 5 @Test 6 public void testUserAndGroupMembership() throws Exception(){ 7 IdentityService is = ar.getIdentityService(); 8 9 Group group = is.newGroup("deptLeader"); 10 group .setName("部门领导"); 11 group .setType("assignment"); 12 is.saveGroup(group); 13 14 User user = is.newUser("henryyan"); 15 user.setFirstName("Henry"); 16 user.setLastName("yan"); 17 user.setEmail("yanhonglei@gamil.com"); 18 is.saveUser(user); 19 20 is.createMembership("henryyan","deptLeader"); 21 22 //查询组deptLeader所拥有的用户 23 User userInGroup = is.createUserQuery().memberOfGroup("deptLeader").singleResult(); 24 assertNotNull(userInGroup); 25 assertEquals("henryyan",userInGroup.getId()); 26 27 //查询用户henryyan所属的组 28 Group groupContainsHenryyan = is.createGroupQuery().groupMember("henryyan").singleResult(); 29 assertNotNull(groupContainsHenryyan); 30 assertEquals("deptLeader",groupContainsHenryyan.getId()); 31 } 32 }
5.1.4 UserTask中的用户与组
(1)测试场景及测试模板
假设一个流程图示意图为:开始--->用户任务--->结束
共用的xml文件模板如下:
1 <process id="userAndGroupInUserTask" name="userAndGroupInUserTask"> 2 <startEvent id="startEvent1" name="Start"></startEvent> 3 <userTask id="studyUserAndGroupInUserTask" name="学习用户与组在用户任务中的应用"></userTask> 4 <sequenceFlow id="flow1" name="" sourceRef="startEvent1" targetRef="studyUserAndGroupInUserTask"></sequenceFlow> 5 <endEvent id="endEvent1" name="End"></endEvent> 6 <sequenceFlow id="flow2" name="" sourceRef="studyUserAndGroupInUserTask" targetRef="endEvent1"></sequenceFlow> 7 </process>
共用的测试代码模板如下:即此时有两个用户,位于组deptLeader里。
1 public class UserAndGroupInUserTaskTest{ 2 @Before 3 public void setup() throws Exception(){ 4 //初始化7个Service 5 super.setUp(); 6 7 Group group = identityService.newGroup("deptLeader"); 8 group .setName("部门领导"); 9 group .setType("assignment"); 10 identityService.saveGroup(group); 11 //henryyan示例,jackchen的略 12 User user = identityService.newUser("henryyan"); 13 user.setFirstName("Henry"); 14 user.setLastName("yan"); 15 user.setEmail("yanhonglei@gmail.com"); 16 identityService.saveUser(user); 17 //henryyan示例,jackchen的略 18 identityService.createMembership("henryyan","deptLeader"); 19 } 20 21 @After 22 public void afterInvokeTestMethod() throws Exception(){ 23 identityService.deleteMembership("henryyan","deptLeader"); 24 identityService.deleteGroup("deptLeader"); 25 identityService.deleteUser("henryyan"); 26 } 27 ... 28 }
(2)候选组
修改上面的UserTask如下:属于这个组的用户都可以签收任务。一旦有一个人签收,其他人的用户任务里就没有这个任务了。
1 <userTask id="studyUserAndGroupInUserTask" 2 name="学习用户与组在用户任务中的应用" 3 activiti:candidateGroups="deptLeader"> 4 </userTask>
(3)候选人
修改上面的UserTask如下:列出来的用户都可以签收任务。一旦有一个人签收,其他人的用户任务里就没有这个任务了。
1 <userTask id="studyUserAndGroupInUserTask" 2 name="学习用户与组在用户任务中的应用" 3 activiti:candidateUsers="jackchen,henryyan"> 4 </userTask>
(4)共用测试代码
在类userAndGroupTestInUserTask添加方法如下:
1 public class UserAndGroupInUserTaskTest{
2 @Test
3 @Deployment(resources={"chapter5/userAndGroupInUserTask.bpmn"})
4 public void testUserAndGroupInUserTask() throws Exception{
5 ProcessInstance pi = runtimeService.startProcessInstanceByKey("userAndGroupInUserTask");
6 assertNotNull(pi);
7
8 //该任务属于所有位于候选组的用户/ 该任务属于所有列出来的候选人
9 Task jackchenTask = taskService.createTaskQuery().taskCandidateUser("jackchen").singleResult();
10 assertNotNull(jackchenTask);
11 Task henryyanTask = taskService.createTaskQuery().taskCandidateUser("henryyan").singleResult();
12 assertNotNull(henryyanTask);
13
14 taskService.claim(jackchenTask.getId(),"jackchen");//jackchen签收任务
15 //taskService.complete(jackchenTask.getId());//jackchen完成任务
16
17 //被jackchen签收后,henryyan不再拥有该任务
18 henryyanTask = taskService.createTaskQuery().taskCandidateUser("henryyan").singleResult();
19 assertNull(henryyanTask);
20 }
21 }
综上,主要涉及的方法:
1 主要涉及到的方法: 2 //创建、保存和删除用户/组 3 User user/Group group = identityService.newUser("userid")/newGroup("groupid"); 4 identityService.saveUser("userid")/saveGroup("groupid"); 5 identityService.deleteUser("userid")/deleteGroup("groupid"); 6 7 //查询User 8 User user = identityService.createUserQuery().userId("userid").singleResult(); 9 List<User> userList = identityService.createUserQuery().userId("userid").list(); 10 //查询Group 11 Group group = identityService.createGroupQuery().groupId("groupid").singleResult(); 12 List<Group> groupList = identityService.createGroupQuery().groupId("groupid").list(); 13 14 //创建、查询关系 15 identityService.createMembership("userid",groupid); 16 User userInGroup = identityService.createUserQuery().memberOfGroup("deptLeader").singleResult(); 17 Group groupContainsHenryyan = identityService.createGroupQuery().groupMember("henryyan").singleResult(); 18 19 //查询用户拥有的任务,签收及完成 20 Task task = taskService.createTaskQuery().taskCandidateUser("userid").singleResult(); 21 List<Task> taskList = taskService.createTaskQuery().taskCandidateUser("userid").list(); 22 taskService.claim(task.getId(),"userid"); 23 taskService.complete(task.getId());