activiti流程实例

ProcessInstance代表流程定义的执行实例。如范冰冰请了一天的假,她就必须发出一个流程实例的申请。一个流程实例包括了所有的运行节点。我们可以利用这个对象来了解当前流程实例的进度等信息。

package com.liuzm.activiti.day1;

import java.util.List;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.runtime.ProcessInstanceQuery;
import org.activiti.engine.task.Task;
import org.junit.Test;

//ProcessInstance(流程实例)    : 按照某规则,一次实际的执行
public class ProcessInstanceTest {
	// 获取核心ProcessEngine类
	private ProcessEngine processEngine = ProcessEngines
			.getDefaultProcessEngine();

	// 1. 启动流程
	@Test
	public void startProcess() throws Exception {
		// 获取服务对象
		RuntimeService runtimeService = processEngine.getRuntimeService();
		// 通过ID启动流程,可以指定启动一个流程
		// runtimeService.startProcessInstanceById(processDefinitionId)
		// 通过key启动流程,可以自动启动最新版本的那一个规则
		String processDefinitionKey = "myProcess";
		ProcessInstance processInstance = runtimeService
				.startProcessInstanceByKey(processDefinitionKey);
		System.out.println(processInstance);
	}

	// 2. 查看任务(私有)
	@Test
	public void queryPersonalTask() throws Exception {
		// 获取服务
		TaskService taskService = processEngine.getTaskService();
		//

		String assignee = "范爷";
		List<Task> list = taskService.createTaskQuery().taskAssignee(assignee)
				.list();
		System.out.println("=================【" + assignee
				+ "】的私有任务列表======================================");
		for (Task task : list) {
			System.out.println("id:" + task.getId() + ",name:" + task.getName()
					+ ",assignee:" + task.getAssignee() + ",createTime:"
					+ task.getCreateTime());
		}
	}

	// 共有任务查看
	@Test
	public void queryCommonTask() throws Exception {
		TaskService taskService = processEngine.getTaskService();
		// 查询
		String candidateUser = "范爷助理";
		List<Task> list = taskService.createTaskQuery()
				.taskCandidateUser(candidateUser).list();
		System.out.println("=================【" + candidateUser
				+ "】的公有任务列表======================================");
		for (Task task : list) {
			System.out.println("id:" + task.getId() + ",name:" + task.getName()
					+ ",assignee:" + task.getAssignee() + ",createTime:"
					+ task.getCreateTime());
		}
	}

	// 3. 认领任务
	@Test
	public void claimTask() throws Exception {
		TaskService taskService = processEngine.getTaskService();
		//

		String taskId = "504";
		String userId = "范爷经纪人";
		taskService.claim(taskId, userId);
	}

	// 4. 办理任务
	@Test
	public void completeTask() throws Exception {
		TaskService taskService = processEngine.getTaskService();
		// 办理任务
		String taskId = "504";
		taskService.complete(taskId);
	}

	// 5. 查看流程的状态
	@Test
	public void viewProcessState() throws Exception {
		// 获取服务
		RuntimeService runtimeService = processEngine.getRuntimeService();
		// 获得流程实例
		ProcessInstanceQuery query = runtimeService
				.createProcessInstanceQuery();
		// 添加查询条件
		String processDefinitionId = "1701";
		query.processDefinitionId(processDefinitionId);
		// 查询
		ProcessInstance singleResult = query.singleResult();
		// 显示流程状态
		if (singleResult != null) {
			System.out.println("当前流程在: " + singleResult.getActivityId());
		} else {
			System.out.println("流程已经结束!");
		}
	}

}

 

 

 

你可能感兴趣的:(Activiti)