activiti是支持juel表达式的,这个也很好用,但实际过程中需要支持类方法及变量。
原来项目中解析juel,这边有自定义一个方法的
代码如下:
public String getStringByELAndFormData(String el, Map formData) throws Exception {
ExpressionFactory factory = new ExpressionFactoryImpl();
SimpleContext context = new SimpleContext();
for (Object k : formData.keySet()) {
if (formData.get(k) != null) {
context.setVariable(k.toString(),
factory.createValueExpression(formData.get(k), formData.get(k).getClass()));
}
}
ValueExpression e = factory.createValueExpression(context, el, String.class);
return (String) e.getValue(context);
}
这种办法没有问题,但只局限于变量执行。但如果增加方法类调用就有问题!
如现在我有一个juely变量表达式,表达式为${fn_test.getJiebaoPost('','','')}。
该表达式需要调用以下类
public class FunctionOrgService {
public String getJiebaoPost(String id,String nodeFlag,String exParams){
return "82301111111";
}
}
原来解析的代码就有问题了,因为他没办法识别该函数.
解决办法如下
- 先在processEngine初始化时,增加函数的引入
Map map=new HashMap();
map.put("fn_test", new FunctionOrgService());
processEngineConfiguration.setBeans(map);
- 构造变量类VariableScope
特别说明一下,这个如果流程未结束,可以直接用流程实例对象ExecutionEntity代替,因为该类也继承了VariableScope。 这边项目需要流程结束也要计算juel表达式,所以只能自己构造该对象
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.activiti.engine.delegate.VariableScope;
import org.activiti.engine.impl.persistence.entity.VariableInstance;
public class TestVariableScope implements VariableScope{
private Map<String, Object> variables=new HashMap<String, Object>();
public TestVariableScope(Map<String, Object> variables){
this.variables=variables;
}
@Override
public Map<String, Object> getVariables() {
// TODO Auto-generated method stub
return variables;
}
@Override
public Map<String, VariableInstance> getVariableInstances() {
/*// TODO Auto-generated method stub
Map<String, VariableInstance> variableInstanceMap=new HashMap();
System.out.println("通过Map.entrySet遍历key和value");
for (Map.Entry<String, Object> entry : variables.entrySet()) {
VariableInstanceEntity variableInstance=VariableInstanceEntity.create( entry.getKey(), type, entry.getValue())
}*/
return null;
}
@Override
public Map<String, Object> getVariables(Collection<String> variableNames) {
// TODO Auto-generated method stub
return variables;
}
@Override
public Map<String, VariableInstance> getVariableInstances(Collection<String> variableNames) {
// TODO Auto-generated method stub
return null;
}
@Override
public Map<String, Object> getVariables(Collection<String> variableNames, boolean fetchAllVariables) {
// TODO Auto-generated method stub
return variables;
}
@Override
public Map<String, VariableInstance> getVariableInstances(Collection<String> variableNames,
boolean fetchAllVariables) {
// TODO Auto-generated method stub
return null;
}
@Override
public Map<String, Object> getVariablesLocal() {
// TODO Auto-generated method stub
return variables;
}
@Override
public Map<String, VariableInstance> getVariableInstancesLocal() {
// TODO Auto-generated method stub
return null;
}
@Override
public Map<String, Object> getVariablesLocal(Collection<String> variableNames) {
// TODO Auto-generated method stub
return variables;
}
@Override
public Map<String, VariableInstance> getVariableInstancesLocal(Collection<String> variableNames) {
// TODO Auto-generated method stub
return null;
}
@Override
public Map<String, Object> getVariablesLocal(Collection<String> variableNames, boolean fetchAllVariables) {
// TODO Auto-generated method stub
return variables;
}
@Override
public Map<String, VariableInstance> getVariableInstancesLocal(Collection<String> variableNames,
boolean fetchAllVariables) {
// TODO Auto-generated method stub
return null;
}
@Override
public Object getVariable(String variableName) {
// TODO Auto-generated method stub
return variables.get(variableName);
}
@Override
public VariableInstance getVariableInstance(String variableName) {
// TODO Auto-generated method stub
return null;
}
@Override
public Object getVariable(String variableName, boolean fetchAllVariables) {
// TODO Auto-generated method stub
return this.getVariable(variableName);
}
@Override
public VariableInstance getVariableInstance(String variableName, boolean fetchAllVariables) {
// TODO Auto-generated method stub
return null;
}
@Override
public Object getVariableLocal(String variableName) {
// TODO Auto-generated method stub
return this.getVariable(variableName);
}
@Override
public VariableInstance getVariableInstanceLocal(String variableName) {
// TODO Auto-generated method stub
return null;
}
@Override
public Object getVariableLocal(String variableName, boolean fetchAllVariables) {
// TODO Auto-generated method stub
return this.getVariable(variableName);
}
@Override
public VariableInstance getVariableInstanceLocal(String variableName, boolean fetchAllVariables) {
// TODO Auto-generated method stub
return null;
}
@Override
public <T> T getVariable(String variableName, Class<T> variableClass) {
// TODO Auto-generated method stub
return null;
}
@Override
public <T> T getVariableLocal(String variableName, Class<T> variableClass) {
// TODO Auto-generated method stub
return null;
}
@Override
public Set<String> getVariableNames() {
// TODO Auto-generated method stub
return null;
}
@Override
public Set<String> getVariableNamesLocal() {
// TODO Auto-generated method stub
return null;
}
@Override
public void setVariable(String variableName, Object value) {
// TODO Auto-generated method stub
variables.put(variableName,value);
}
@Override
public void setVariable(String variableName, Object value, boolean fetchAllVariables) {
// TODO Auto-generated method stub
this.setVariable(variableName, value);
}
@Override
public Object setVariableLocal(String variableName, Object value) {
// TODO Auto-generated method stub
return null;
}
@Override
public Object setVariableLocal(String variableName, Object value, boolean fetchAllVariables) {
// TODO Auto-generated method stub
return null;
}
@Override
public void setVariables(Map<String, ? extends Object> variables) {
// TODO Auto-generated method stub
this.variables=(Map<String, Object>) variables;
}
@Override
public void setVariablesLocal(Map<String, ? extends Object> variables) {
// TODO Auto-generated method stub
this.variables=(Map<String, Object>) variables;
}
@Override
public boolean hasVariables() {
// TODO Auto-generated method stub
return variables.size()>0;
}
@Override
public boolean hasVariablesLocal() {
// TODO Auto-generated method stub
return variables.size()>0;
}
@Override
public boolean hasVariable(String variableName) {
// TODO Auto-generated method stub
return variables.containsKey(variableName);
}
@Override
public boolean hasVariableLocal(String variableName) {
// TODO Auto-generated method stub
return variables.containsKey(variableName);
}
@Override
public void createVariableLocal(String variableName, Object value) {
// TODO Auto-generated method stub
}
@Override
public void removeVariable(String variableName) {
// TODO Auto-generated method stub
}
@Override
public void removeVariableLocal(String variableName) {
// TODO Auto-generated method stub
}
@Override
public void removeVariables(Collection<String> variableNames) {
// TODO Auto-generated method stub
}
@Override
public void removeVariablesLocal(Collection<String> variableNames) {
// TODO Auto-generated method stub
}
@Override
public void removeVariables() {
// TODO Auto-generated method stub
}
@Override
public void removeVariablesLocal() {
// TODO Auto-generated method stub
}
}
以上代码只传入一个map即可,很多方法不需要实现.
3.执行juel的表达式
public class JuelExpressonCommand implements Command<Object> {
private static Logger logger = LoggerFactory.getLogger(JuelExpressonCommand.class);
/**
* 取消沟通
*/
private String el;
/**
* 流程变量
*/
private Map<String, Object> formData;
public JuelExpressonCommand(String el, Map<String, Object> formData) {
super();
this.el = el;
this.formData = formData;
}
@Override
public Object execute(CommandContext commandContext) {
ProcessEngineConfigurationImpl processEngineConfiguation=commandContext.getProcessEngineConfiguration();
ExpressionManager expressionManager=processEngineConfiguation.getExpressionManager();
Expression e = expressionManager.createExpression(el);
CdpVariableScope variableScope=new CdpVariableScope(formData);
return e.getValue(variableScope);
}
}
至此,该表达式解析OK!
网友评论