1. 程式人生 > >activiti 個人任務指定辦理人三種方式(十)

activiti 個人任務指定辦理人三種方式(十)

1.  流程圖


2. 執行人指定類、實現類

package com.hxzq.workflow.taskperson;

import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;

/**
 * 指定任務的辦理人
 * 
 * @author ZHEN.L
 * 
 */
public class TaskListinerImpl implements TaskListener{

	private static final long serialVersionUID = -8328518556439258389L;

	// 指定任務的辦理人
	public void notify(DelegateTask delegateTask) {
		delegateTask.setAssignee("張一");
	}

}
package com.hxzq.workflow.taskperson;


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


import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;


/**
 * 指定辦理人三種方式<br>
 * 1.直接在活動中制定assignee<br>
 * 2.在asssgine中設定變數${person},在啟動時,設定人員<code>startProcessInstanceByKey(
<span style="white-space:pre">				</span>processInstanceKey, assignee)</code>
 * 3.使用類,動態制定代理人<br>
 * 另外:可以將任務分配給另外一個人
 * 
 * @author ZHEN.L
 * 
 */
public class TaskPerson {


<span style="white-space:pre">	</span>ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();


<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span> * 部署流程定義
<span style="white-space:pre">	</span> */
<span style="white-space:pre">	</span>@Test
<span style="white-space:pre">	</span>public void deploymentProcessDefinition() {
<span style="white-space:pre">		</span>InputStream bpmn = this.getClass().getResourceAsStream(
<span style="white-space:pre">				</span>"taskPerson.bpmn");
<span style="white-space:pre">		</span>InputStream png = this.getClass().getResourceAsStream("taskPerson.png");
<span style="white-space:pre">		</span>Deployment deployment = processEngine.getRepositoryService()
<span style="white-space:pre">				</span>.createDeployment().addInputStream("taskPerson.bpmn", bpmn)
<span style="white-space:pre">				</span>.addInputStream("taskPerson.png", png).name("指定辦理人").deploy();
<span style="white-space:pre">		</span>System.out.println(deployment.getId());
<span style="white-space:pre">	</span>}


<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span> * 啟動流程例項
<span style="white-space:pre">	</span> */
<span style="white-space:pre">	</span>@Test
<span style="white-space:pre">	</span>public void startProcessInstance() {
<span style="white-space:pre">		</span>String processInstanceKey = "taskPerson";
<span style="white-space:pre">		</span>ProcessInstance processInstance = processEngine.getRuntimeService()
<span style="white-space:pre">				</span>.startProcessInstanceByKey(processInstanceKey);
<span style="white-space:pre">		</span>System.out.println(processInstance.getId());
<span style="white-space:pre">	</span>}


<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span> * 查詢個人任務
<span style="white-space:pre">	</span> */
<span style="white-space:pre">	</span>@Test
<span style="white-space:pre">	</span>public void findTaskPerson() {
<span style="white-space:pre">		</span>String assignee = "張一";
<span style="white-space:pre">		</span>List<Task> tasks = processEngine.getTaskService().createTaskQuery()
<span style="white-space:pre">				</span>.taskAssignee(assignee).list();
<span style="white-space:pre">		</span>if (tasks != null && tasks.size() > 0) {
<span style="white-space:pre">			</span>for (Task t : tasks) {
<span style="white-space:pre">				</span>System.out.println(t.getId() + ", " + t.getName());
<span style="white-space:pre">			</span>}
<span style="white-space:pre">		</span>}
<span style="white-space:pre">	</span>}


<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span> * 完成任務
<span style="white-space:pre">	</span> */
<span style="white-space:pre">	</span>@Test
<span style="white-space:pre">	</span>public void completeTask() {
<span style="white-space:pre">		</span>String taskId = "3304";
<span style="white-space:pre">		</span>processEngine.getTaskService().complete(taskId);
<span style="white-space:pre">	</span>}


<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span> * 將任務指定給另外一個人
<span style="white-space:pre">	</span> */
<span style="white-space:pre">	</span>@Test
<span style="white-space:pre">	</span>public void setAssignee() {
<span style="white-space:pre">		</span>String taskId = "3304";
<span style="white-space:pre">		</span>String user = "張九九";
<span style="white-space:pre">		</span>processEngine.getTaskService().setAssignee(taskId, user);
<span style="white-space:pre">	</span>}
}