activiti 设置变量(四)

package diagrams;

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

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricVariableInstance;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Test;

/**
 * 设置变量
 * 
 * @author ZHEN.L
 * 
 */
public class ProcessVariablesTest {

	ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

	/**
	 * 部署流程定义<br>
	 * select * from act_re_deployment ;
	 * 
	 */
	@Test
	public void deploymentProcessInstance() {
		InputStream bpmn = this.getClass().getResourceAsStream(
				"/diagrams/LeaveProcess.bpmn");
		InputStream png = this.getClass().getResourceAsStream(
				"/diagrams/LeaveProcess.png");
		Deployment deployment = processEngine.getRepositoryService()
				.createDeployment().addInputStream("LeaveProcess.bpmn", bpmn)
				.addInputStream("LeaveProcess.png", png).name("汇信请假流程")
				.deploy();
		System.out.println(deployment.getId());
		System.out.println(deployment.getName());
	}

	/**
	 * 启动流程实例<br>
	 * select * from act_ru_task;
	 */
	@Test
	public void startProcessInstance() {
		List<ProcessInstance> list = processEngine.getRuntimeService()
				.createProcessInstanceQuery().processDefinitionKey("myProcess")
				.list();
		if (list != null && list.size() > 0) {
			for (ProcessInstance p : list) {
				System.out.println(p.getId() + "  " + p.getProcessInstanceId());
			}
		}
	}

	/**
	 * 设置参数
	 */
	@Test
	public void setVariables() {
		String taskId = "2002";
		TaskService taskService = processEngine.getTaskService();
		taskService.setVariableLocal(taskId, "请假天数", 3);
		taskService.setVariable(taskId, "请假原因", "世界这么大,想去看看。");
		taskService.setVariable(taskId, "请假日期", new Date());

		// 设置javabean
		Person p = new Person.decorate().name("东方不败").age(133).address("黑木崖").birthday(new Date()).build();
		taskService.setVariable(taskId, "人员信息", p);
	}

	/**
	 * 获取参数
	 */
	@Test
	public void getVariables() {
		String taskId = "2002";
		TaskService taskService = processEngine.getTaskService();
		Integer day = (Integer) taskService.getVariable(taskId, "请假天数");
		Date date = (Date) taskService.getVariable(taskId, "请假日期");
		String reason = (String) taskService.getVariable(taskId, "请假原因");
		System.out.println(date + "" + reason + " " + day);
		// 获取javabean
		Person p = (Person)taskService.getVariable(taskId, "人员信息");
		System.out.println(p.getName()+"; "+p.getAge()+"; "+p.getAddress()+"; "+p.getBirthday());
	}

	/**
	 * 完成任务
	 */
	@Test
	public void completeTask(){
		String taskId = "2002";
		processEngine.getTaskService().complete(taskId);
	}
	
	/**
	 * 查询完成历史
	 */
	@Test
	public void findHistoricVariables(){
		List<HistoricVariableInstance> list = processEngine.getHistoryService().createHistoricVariableInstanceQuery().variableName("人员信息").list();
		if(list != null && list.size() > 0){
			for(HistoricVariableInstance h:list){
				System.out.println(h.getVariableName() +"; "+h.getVariableTypeName()+"; "+h.getValue());
			}
		}
	}
}

class Person implements Serializable {

	private static final long serialVersionUID = -8977772771107623992L;

	private String name;
	private int age;
	private String address;
	private Date birthday;

	private Person() {
	}

	public static class decorate {

		Person p = null;

		public decorate() {
			this.p = new Person();
		}

		public decorate name(String name) {
			this.p.setName(name);
			return this;
		}

		public decorate address(String address) {
			this.p.setAddress(address);
			return this;
		}

		public decorate age(int age) {
			this.p.setAge(age);
			return this;
		}

		public decorate birthday(Date birthday) {
			this.p.setBirthday(birthday);
			return this;
		}

		public Person build() {
			return this.p;
		}
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
}

你可能感兴趣的:(activiti 设置变量(四))