activiti流程变量的设置和获取

流程变量的设置通过map键值对设置

package com.liuzm.activiti.day2;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

/**
 * 流程变量
 *  1.启动流程
 *	2.设置流程变量
 *	3.获取流程变量
 */
public class ProcessVarTest {
	private ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
	// 1.启动流程
	@Test
	public void startProcess() throws Exception {
		//获取服务对象
		RuntimeService runtimeService = processEngine.getRuntimeService();
		//通过流程定义ID开启流程实例,同时添加流程变量
		String processDefinitionId = "myProcess:3:604";
		Map<String,Object> variables = new HashMap<String,Object>();
		variables.put("提示信息", "开启请假流程!!!");
		runtimeService.startProcessInstanceById(processDefinitionId, variables);
	}
	
	// 2.设置流程变量
	@Test
	public void setVar() throws Exception {
		//获取服务对象
		TaskService taskService = processEngine.getTaskService();
		//查询任务
		String assignee = "范爷";
		List<Task> list = taskService.createTaskQuery().taskAssignee(assignee).list();
		//获取到第一个任务
		Task task = list.get(0);
		String taskId = task.getId();
		//设置流程变量--->通过taskService可以多次设置流程变量,并且可通过Map批量设置
		//taskService.setVariable(taskId, "请假人", assignee);  //存储到Text1列
		//taskService.setVariable(taskId , "请假时间", new Date());//转化为时间戳,存储到Long列
		taskService.setVariable(taskId, "是否要扣薪资", true);//存储到Long列,存1
		taskService.setVariable(taskId, "要扣薪资", 333.33);//存储到DOUBLE列
	}
	
	// 3.获取流程变量
	@Test
	public void getVar() throws Exception {
		//获取服务对象
		TaskService taskService = processEngine.getTaskService();
		//获取任务,从而得到任务ID
		String assignee = "范爷";
		List<Task> list = taskService.createTaskQuery().taskAssignee(assignee).list();
		Task task = list.get(0);
		String taskId = task.getId();
		//获取流程变量
		Map<String, Object> variables = taskService.getVariables(taskId );
		System.out.println(variables);
	}

}

 

 

你可能感兴趣的:(Activiti)