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);
}
這種辦法沒有問題蒋情,但只局限于變量執(zhí)行埠况。但如果增加方法類調用就有問題!
如現(xiàn)在我有一個juely變量表達式棵癣,表達式為${fn_test.getJiebaoPost('','','')}辕翰。
該表達式需要調用以下類
public class FunctionOrgService {
public String getJiebaoPost(String id,String nodeFlag,String exParams){
return "82301111111";
}
}
原來解析的代碼就有問題了,因為他沒辦法識別該函數(shù).
解決辦法如下
- 先在processEngine初始化時狈谊,增加函數(shù)的引入
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即可,很多方法不需要實現(xiàn).
3.執(zhí)行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!