activiti 사용자 정의 함수 해석 주엘 표현식

10622 단어
activiti는 주엘 표현식을 지원하는데 이것도 사용하기 좋으나 실제 과정에서 지원 클래스 방법과 변수가 필요합니다.원래 항목에서 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);
    }


이런 방법은 문제가 없지만 변수 집행에만 국한된다.하지만 방법류 호출을 늘리면 문제가 있습니다!
현재 주리 변수 표현식이 있습니다. 표현식은 ${fn test.getJiebaoPost (",", ")}입니다.이 표현식은 다음과 같은 종류를 호출해야 한다
public class FunctionOrgService {

    
    public String getJiebaoPost(String id,String nodeFlag,String exParams){
        
        return "82301111111";
    }
}


원래 해석한 코드에 문제가 생겼다. 왜냐하면 그는 이 함수를 식별할 방법이 없기 때문이다.
해결 방법은 다음과 같다.
  • 프로세스 Engine을 초기화할 때 함수 추가 도입
  •         Map map=new HashMap();
            map.put("fn_test", new FunctionOrgService());
            processEngineConfiguration.setBeans(map);
    
  • 구조 변수 클래스인 VariableScope는 특히 이 프로세스가 끝나지 않으면 프로세스 실례 대상인 ExecutionEntity로 대체할 수 있다. 왜냐하면 이 클래스도 VariableScope를 계승했기 때문이다.이쪽 프로젝트는 절차가 끝나도 주엘 표현식을 계산해야 하기 때문에 이 대상을 스스로 구성할 수 밖에 없다
  • 
    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 variables=new HashMap();
        public TestVariableScope(Map variables){
            this.variables=variables;
        }
        @Override
        public Map getVariables() {
            // TODO Auto-generated method stub
            return variables;
        }
    
        @Override
        public Map getVariableInstances() {
            /*// TODO Auto-generated method stub
             Map variableInstanceMap=new HashMap();
             System.out.println("  Map.entrySet  key value");
              for (Map.Entry entry : variables.entrySet()) {
                  VariableInstanceEntity variableInstance=VariableInstanceEntity.create( entry.getKey(), type, entry.getValue())
              
              }*/
            return null;
        }
    
        @Override
        public Map getVariables(Collection variableNames) {
            // TODO Auto-generated method stub
            return variables;
        }
    
        @Override
        public Map getVariableInstances(Collection variableNames) {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public Map getVariables(Collection variableNames, boolean fetchAllVariables) {
            // TODO Auto-generated method stub
            return variables;
        }
    
        @Override
        public Map getVariableInstances(Collection variableNames,
                boolean fetchAllVariables) {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public Map getVariablesLocal() {
            // TODO Auto-generated method stub
            return variables;
        }
    
        @Override
        public Map getVariableInstancesLocal() {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public Map getVariablesLocal(Collection variableNames) {
            // TODO Auto-generated method stub
            return variables;
        }
    
        @Override
        public Map getVariableInstancesLocal(Collection variableNames) {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public Map getVariablesLocal(Collection variableNames, boolean fetchAllVariables) {
            // TODO Auto-generated method stub
            return variables;
        }
    
        @Override
        public Map getVariableInstancesLocal(Collection 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 getVariable(String variableName, Class variableClass) {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public  T getVariableLocal(String variableName, Class variableClass) {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public Set getVariableNames() {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public Set 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 variables) {
            // TODO Auto-generated method stub
            this.variables=(Map) variables;
        }
    
        @Override
        public void setVariablesLocal(Map variables) {
            // TODO Auto-generated method stub
            this.variables=(Map) 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 variableNames) {
            // TODO Auto-generated method stub
            
        }
    
        @Override
        public void removeVariablesLocal(Collection variableNames) {
            // TODO Auto-generated method stub
            
        }
    
        @Override
        public void removeVariables() {
            // TODO Auto-generated method stub
            
        }
    
        @Override
        public void removeVariablesLocal() {
            // TODO Auto-generated method stub
            
        }
    
    }
    
    

    상기 코드는 맵 하나만 전송하면 되며, 많은 방법은 실현할 필요가 없다.
    3. 주엘의 표현식 실행하기
    
    public class JuelExpressonCommand implements Command {
        private static Logger logger = LoggerFactory.getLogger(JuelExpressonCommand.class);
     
        /**
         *     
         */
        private String el;
    
        
    
        /**
         *     
         */
        private Map formData;
        public JuelExpressonCommand(String el, Map 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!

    좋은 웹페이지 즐겨찾기