acitivi 流程实例(三)

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


import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Test;


/**
 * 流程实例
 * 
 * @author ZHEN.L
 * 
 */
public class ProcessInstanceTest {


	ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();


	/**
	 * 部署
	 */
	@Test
	public void deploymentProcess() {
		InputStream in = this.getClass().getClassLoader()
				.getResourceAsStream("diagrams/LeaveProcess.zip");
		Deployment deployment = processEngine.getRepositoryService()
				.createDeployment().name("汇信请假流程")
				.addZipInputStream(new ZipInputStream(in)).deploy();
		System.out.println(deployment.getId());
		System.out.println(deployment.getName());
	}


	/**
	 * 启动流程实例
	 */
	@Test
	public void startProcessInstance() {
		String processInstanceKey = "myProcess";
		ProcessInstance pi = processEngine.getRuntimeService()
				.startProcessInstanceByKey(processInstanceKey);
		System.out.println(pi.getId());
		System.out.println(pi.getProcessInstanceId());
		System.out.println(pi.getProcessDefinitionId());
	}


	/**
	 * 查询流程实例状态
	 */
	@Test
	public void isCompleted() {
		String processInstanceId = "1501";
		ProcessInstance pi = processEngine.getRuntimeService()
				.createProcessInstanceQuery()
				.processInstanceBusinessKey(processInstanceId).singleResult();
		if (pi == null) {
			System.out.println("流程已经结束");
		} else {
			System.out.println("未结束");
		}
	}


	/**
	 * 完成流程
	 */
	@Test
	public void completeTask() {
		String processInstanceId = "1501";
		processEngine.getTaskService().complete(processInstanceId);
	}
	
	/**
	 * 查询历史流程实例
	 */
	@Test
	public void findHistoricProcessInstance(){
		String processInstanceId = "1501";
		HistoricProcessInstance h = processEngine.getHistoryService().createHistoricProcessInstanceQuery().processDefinitionId(processInstanceId).singleResult();
		System.out.println(h.getId());
		System.out.println(h.getProcessDefinitionId());
		System.out.println(h.getStartTime());
		System.out.println(h.getEndTime());
	}


	/**
	 * 查询历史任务
	 */
	@Test
	public void findHistoricTask() {
		String assignee = "张三";
		List<HistoricTaskInstance> list = processEngine.getHistoryService()
				.createHistoricTaskInstanceQuery().taskAssignee(assignee)
				.list();
		if (list != null && list.size() > 0) {
			for (HistoricTaskInstance h : list) {
				System.out.println(h.getAssignee() + " " + h.getName() + " "
						+ h.getStartTime() + " " + h.getEndTime());
				System.out.println("###################");
			}
		}
	}
	
}

你可能感兴趣的:(acitivi 流程实例(三))