流程的变量有多重方式进行设置与获取
/**
* setValiable 与 setLocalValiable作用域不同
*/
@Test
public void deploeyFlow() {
String folderPath = "study/activiti/diagrams/sequenceFlow";
HashMap map = new HashMap<>();
map.put("name", "连线流程");// 名称
map.put("id", "sequenceFlow");// id
map.put("category", "办公流程");// 类别
activitiService.devlopActiviti(folderPath, map);
}
@Test
public void startActiviti() {
String processDefinitionKey = "sequenceFlow";
String orgCode = "070067801";
// 设置变量
HashMap map = new HashMap<>();
map.put("createLoginName", "0003");
map.put("orgCode", orgCode);
activitiService.startActiviti(processDefinitionKey, map);
}
此时没有完成第一个任务。
@Test
public void setVariable() {
// String excutionId = "25001";
String taskId = "10008";
HashMap<String, Object> map = new HashMap<>();
map.put("message", "重要");
boolean isLocal = true;
// activitiService.setVariableByExcutionId(excutionId, isLocal, map);
activitiService.setVariableByTaskId(taskId, isLocal, map);
}
@Test
public void getVariable() {
// String excutionId = "10001";
String taskId = "10008";
Map<String, Object> map = new HashMap<>();
boolean isLocal = false;
//map = activitiService.getVariableByExcutionId(excutionId, isLocal );
map = activitiService.getVariableByTaskId(taskId, isLocal);
LOGGER.info("message【{}】",(String)map.get("message"));
//SequenceFlow sequenceFlow = activitiService.getVariableByTaskId(taskId, "sequenceFlow", SequenceFlow.class);
// LOGGER.info("创建时间【{}】", sequenceFlow.getDate());
}
@Override
public boolean setVariableByTaskId(String taskId, boolean isLocal, HashMap<String, Object> map) {
/*
* taskService.setVariable(taskId, variableName, value);
*
* 设置本执行对象的变量,该作用域只在当前的executionId中 taskService.setVariableLocal(taskId,
* variableName, value); 可以设置对个变量,放在map中
*/
if (isLocal) {
taskService.setVariablesLocal(taskId, map);
} else {
taskService.setVariables(taskId, map);
}
return true;
}
@Override
public Map<String, Object> getVariableByTaskId(String taskId, boolean isLocal) {
Map<String, Object> variablesMap = new HashMap<String, Object>();
if (isLocal) {
variablesMap = taskService.getVariablesLocal(taskId);
} else {
variablesMap = taskService.getVariables(taskId);
}
return variablesMap;
}
[20:39:24:795] [INFO] - wsylp.log.MyLog.logAfter(MyLog.java:29) - @AfterReturning:wsylp.service.ActivitiService.getVariableByTaskId end.
[20:39:24:795] [INFO] - serviceTest.ActivitiVariableTest.getVariable(ActivitiVariableTest.java:172) - message【重要】
如果变量为对象需要实现序列化接口
public class SequenceFlow implements Serializable {
private static final long serialVersionUID = -1164661902871465047L;
private String message;//信息(重要,不重要)
private String createLoginName;// 申请人
private Date date;// 申请日期
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getCreateLoginName() {
return createLoginName;
}
public void setCreateLoginName(String createLoginName) {
this.createLoginName = createLoginName;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
设置变量
/**
* 设置流程变量值
*/
@Test
public void setVariablesByBean() {
String taskId = "10008";
/**
* 支持的变量类型
*
*
* 简单类型:String boolean Integer double date
* 自定义对象bean
*/
// 传递的是一个自定义的bean对象
SequenceFlow sequenceFlow = new SequenceFlow();
sequenceFlow.setCreateLoginName("0003");
sequenceFlow.setDate(new Date());
sequenceFlow.setMessage("重要");
activitiService.setVariableByTaskId(taskId, "sequenceFlow", sequenceFlow);
}
@Test
public void getVariable() {
// String excutionId = "10001";
String taskId = "10008";
Map map = new HashMap<>();
boolean isLocal = false;
//map = activitiService.getVariableByExcutionId(excutionId, isLocal );
// map = activitiService.getVariableByTaskId(taskId, isLocal);
//LOGGER.info("message【{}】",(String)map.get("message"));
SequenceFlow sequenceFlow = activitiService.getVariableByTaskId(taskId, "sequenceFlow", SequenceFlow.class);
LOGGER.info("创建时间【{}】", sequenceFlow.getDate());
}
[20:45:47:841] [INFO] - wsylp.log.MyLog.logAfter(MyLog.java:29) - @AfterReturning:wsylp.service.ActivitiService.getVariableByTaskId end.
[20:45:47:841] [INFO] - serviceTest.ActivitiVariableTest.getVariable(ActivitiVariableTest.java:175) - 创建时间【2018-06-17T20:45:24.240+0800】
由于此时串的message为重要,那么下一个审批人为总经理
由于变量设置在sequenceFlow上,尽量设置变量为全局的。或者设置在执行对象上。
[20:53:50:698] [INFO] - wsylp.service.impl.ActivitiServiceImpl.finshTask(ActivitiServiceImpl.java:148) - 完成任务【10008】
github地址:https://github.com/wsylp/gms.git