JBPM4基础篇06-IdentityService实现组织机构

使用IdentityService实现用户和用户组之间的关系对应。
package org.wxp;

import java.util.List;

import junit.framework.TestCase;

import org.jbpm.api.Configuration;
import org.jbpm.api.ExecutionService;
import org.jbpm.api.IdentityService;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.ProcessInstance;
import org.jbpm.api.RepositoryService;
import org.jbpm.api.TaskService;
import org.jbpm.api.task.Task;

/**
 * 用户
 * 用户组
 * 关系
 * @author Champion.Wong
 *
 */
public class TaskTest extends TestCase {
	
	ProcessEngine processEngine; // 创建流程引擎
	
	public TaskTest() {
		processEngine = Configuration.getProcessEngine();
		IdentityService identityService = processEngine.getIdentityService(); // 身份验证
		identityService.createGroup("dev"); // 创建一个组,需要在XML中指定一个task的type为candidate-groups,expression为"dev"
		identityService.createUser("user1", "user1", "user1"); // 创建用户1
		identityService.createUser("user2", "user2", "user2"); // 创建用户2
		identityService.createMembership("user1", "dev"); // 绑定用户1和组之间的关系
		identityService.createMembership("user2", "dev"); // 绑定用户2和组之间的关系
	}
	
	public void test() {
		RepositoryService repositoryService = processEngine.getRepositoryService();
		TaskService taskService = processEngine.getTaskService();
		ExecutionService executionService = processEngine.getExecutionService();
		String deploymentId = repositoryService.createDeployment().addResourceFromClasspath("task.jpdl.xml").deploy(); // 加载jpdl文件
		ProcessInstance processInstance = executionService.startProcessInstanceByKey("task"); // 开始执行一个流程
		assertEquals(1, taskService.findGroupTasks("user1").size()); // 判断某一个用户的任务列表中任务的个数
		assertEquals(1, taskService.findGroupTasks("user2").size());
		
		List<Task> taskList = taskService.findGroupTasks("user1"); // 获取user1的任务列表
		Task task = taskList.get(0);
		taskService.takeTask(task.getId(), "user1"); // 执行任务
		taskService.completeTask(task.getId()); // 完成任务
	}
}

你可能感兴趣的:(JBPM4基础篇06-IdentityService实现组织机构)