activiti 自定义函数解析juel表达式

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";
    }
}

原来解析的代码就有问题了,因为他没办法识别该函数.

解决办法如下

  1. 先在processEngine初始化时,增加函数的引入
        Map map=new HashMap();
        map.put("fn_test", new FunctionOrgService());
        processEngineConfiguration.setBeans(map);
  1. 构造变量类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!

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏封碎

Android开发减少方法数的建议 博客分类: Android小技巧 方法数android

     现在的Android应用越来越庞大,开发者越来越能感受到方法数限制带来的影响。这里简单讲几个减少方法数的小建议。

13550
来自专栏码匠的流水账

聊聊storm WindowTridentProcessor的FreshCollector

本文主要研究一下storm WindowTridentProcessor的FreshCollector

10920
来自专栏技术之路

Qt学习笔记常用容器

主要说Qt的以下几种容器 1.QList<T> 2.QLinkedList<T> 3.Map<T> 和一些常用的容器方法的使用 qSort qCopy qFin...

226100
来自专栏曾大稳的博客

Android OpenGLES 绘制三角形 ,四边形

OpenGL ES Shader的三种变量类型uniform,attribute和varying

32330
来自专栏Android原创

Android OpenGL ES滤镜开发设计

按照正常的Android OpenGL开发,一般只需引入两个“主角”:GLSurfaceView和Renderer。

25840
来自专栏Android相关

Kotlin---类与构造函数

Kotlin中的类定义与Java中无异,只是在Kotlin中,再也没有static所声明的静态内部类。

10120
来自专栏码匠的流水账

聊聊storm WindowTridentProcessor的FreshCollector

本文主要研究一下storm WindowTridentProcessor的FreshCollector

9530
来自专栏懒人开发

鸿洋AutoLayout代码分析(五):Attr相关类

上一节,我们分析了很多类, 其中,最重要的是 AutoLayoutHelper 类的2个方法 adjustChildren() 和 getAutoLayou...

13730
来自专栏java达人

数字的陷阱

Java中对数字的处理,如四舍五入,如加减乘除,貌似是一个很基础很简单的知识点,但是如果你没有对他进行充分了解,很容易掉进它的陷阱里。 1、浮点数运算 先来看一...

19880
来自专栏一个会写诗的程序员的博客

Frida JAVA API 文档JavaWeakRef

a boolean specifying whether the current process has the a Java VM loaded, i.e. ...

25530

扫码关注云+社区

领取腾讯云代金券