activiti 根据角色指定执行人(十二)

1. 流程图

activiti 根据角色指定执行人(十二)_第1张图片


2. 实现类

package com.hxzq.workflow.role;

import java.io.InputStream;
import java.util.List;

import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.impl.persistence.entity.GroupEntity;
import org.activiti.engine.impl.persistence.entity.UserEntity;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

/**
 * 根据角色查询 select * from act_id_group ; <br>
 * select * from act_id_user;<br>
 * select * from act_id_membership;<br>
 * 
 * @author HXZQ-SW
 * 
 */
public class RoleTask {

	ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

	@Test
	public void deploymentProcessDefinition() {
		InputStream bpmn = this.getClass().getResourceAsStream("RoleTask.bpmn");
		InputStream png = this.getClass().getResourceAsStream("RoleTask.png");
		processEngine.getRepositoryService().createDeployment()
				.addInputStream("RoleTask.bpmn", bpmn)
				.addInputStream("RoleTask.png", png).name("组任务").deploy();

		IdentityService identityService = processEngine.getIdentityService();
		// 添加角色
		identityService.saveGroup(new GroupEntity("M1"));
		identityService.saveGroup(new GroupEntity("M2"));
		// 添加用户
		identityService.saveUser(new UserEntity("胡一"));
		identityService.saveUser(new UserEntity("胡二"));
		identityService.saveUser(new UserEntity("胡三"));
		// 建立用户角色关系
		identityService.createMembership("胡一", "M1");
		identityService.createMembership("胡二", "M2");
		identityService.createMembership("胡三", "M1");

	}

	@Test
	public void startProcessInstance() {
		String processInstanceKey = "RoleTask";
		ProcessInstance processInstance = processEngine.getRuntimeService()
				.startProcessInstanceByKey(processInstanceKey);
		System.out.println(processInstance.getId());
	}

	/**
	 * 查询当前人的组任务
	 */
	@Test
	public void findGroupTask() {
		String user = "胡一";
		List<Task> list = processEngine.getTaskService().createTaskQuery()
				.taskCandidateUser(user).list();
		if (list != null && list.size() > 0) {
			for (Task t : list) {
				System.out.println(t.getId() + ", " + t.getName() + ", "
						+ t.getCreateTime());
			}
		}
	}

	/**
	 * 完成任务
	 */
	@Test
	public void completeTask() {
		String taskId = "5004";
		processEngine.getTaskService().complete(taskId);
	}

}


你可能感兴趣的:(activiti 根据角色指定执行人(十二))