package com.mossle.bpm.delegate;

import static org.junit.Assert.assertNotNull;

import java.util.List;

import javax.annotation.Resource;

import org.activiti.engine.HistoryService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricIdentityLink;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * 对委托任务进行测试
 * 
 * @author LuZhao
 * 
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/spring/applicationContext*.xml" })
public class DelegateTaskTest {

	@Resource
	private RepositoryService repositoryService;

	@Resource
	private RuntimeService runtimeService;

	@Resource
	private TaskService taskService;

	@Resource
	private IdentityService identityService;

	@Resource
	private HistoryService historyService;

	private Deployment deployment;

	@Test
	public void testDelegateTask01() throws Exception {

		ProcessDefinition processDefinition = findProcessDefinitionByDeploymentId();// 查询最新的流程定义

		assertNotNull(processDefinition);

		ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinition.getKey());
		assertNotNull(processInstance);

		Task task = findPersonTask(processInstance);// 查询办理任务
		String taskId = task.getId();
		assertNotNull(taskId);

		/** 第一次委托 */
		String userId = "leaderuser";
		taskService.delegateTask(taskId, userId);
		taskService.resolveTask(taskId);

		/** 第二次委托 */
		// task.setDelegationState(DelegationState.PENDING);
		userId = "admin";
		taskService.delegateTask(taskId, userId);
		taskService.resolveTask(taskId);

		taskService.complete(taskId);

		/**
		 * 该对象无法查询出委托任务的参与人有那些... 
		 * TODO 解决无法查询出具体的参与人？
		 */
		List<HistoricIdentityLink> historicIdentityLinks = historyService.getHistoricIdentityLinksForTask(taskId);
		for (HistoricIdentityLink historicIdentityLink : historicIdentityLinks) {
			System.err.println(historicIdentityLink.getUserId());
		}

	}

	private Task findPersonTask(ProcessInstance processInstance) {
		return taskService.createTaskQuery()//
				.processInstanceId(processInstance.getId())//
				.taskDefinitionKey("usertask1")//
				.taskAssignee("hruser")//
				.singleResult();
	}

	private ProcessDefinition findProcessDefinitionByDeploymentId() {
		return repositoryService.createProcessDefinitionQuery()//
				.processDefinitionKey("delegateTask")//
				.deploymentId(deployment.getId())//
				.singleResult();
	}

	@Before
	public void setUp() {
		// 部署
		deployment = repositoryService.createDeployment() //
				.addClasspathResource("diagrams/delegate/delegateTask.bpmn") //
				.addClasspathResource("diagrams/delegate/delegateTask.png") //
				.deploy();
		// identityService.saveUser(identityService.newUser("hruser"));
		// identityService.saveUser(identityService.newUser("leaderuser"));
		// identityService.saveUser(identityService.newUser("admin"));
		//
		// identityService.saveGroup(identityService.newGroup("hr"));
		// identityService.saveGroup(identityService.newGroup("deptLeader"));
		// identityService.saveGroup(identityService.newGroup("admin"));
		// identityService.createMembership("hruser", "hr");
		// identityService.createMembership("leaderuser", "deptLeader");
		// identityService.createMembership("admin", "admin");
	}

	@After
	public void tearDown() {
		// identityService.deleteMembership("hruser", "hr");
		// identityService.deleteMembership("leaderuser", "deptLeader");
		// identityService.deleteMembership("admin", "admin");
		//
		// identityService.deleteUser("hruser");
		// identityService.deleteUser("leaderuser");
		// identityService.deleteUser("admin");
		//
		// identityService.deleteGroup("hr");
		// identityService.deleteGroup("deptLeader");
		// identityService.deleteGroup("admin");

		repositoryService.deleteDeployment(deployment.getId(), true);
	}
}

@@ -1903,32 +1903,95 @@
 lveTask(taskId);
+%0A%09%09taskService.addUserIdentityLink(taskId, userId, %22delegate%22);
 %0A%0A%09%09/** %E7%AC%AC%E4%BA%8C%E6%AC%A1%E5%A7%94%E6%89%98 */
@@ -2140,24 +2140,87 @@
 ask(taskId);
+%0A%09%09taskService.addUserIdentityLink(taskId, userId, %22delegate%22);
 %0A%0A%09%09taskServ
@@ -2547,16 +2547,55 @@
 UserId()
+ + %22 %22 + historicIdentityLink.getType()
 );%0A%09%09%7D%0A%0A

use addIdentityLink for delegateTask