自定義JPPM的配置及操作


1、創建工作流,在某個節點加入自定義配置項,配置方法如下:

       

<task name="node" g="323,87,110,50" key="node">
      <transition name="to end" to="end" g="437,85" key="to end"/>
      <config type="json" value="{name:'test',value:'result'}"/>
</task>

 

其中config元素即是需要加入的配置項標識,它的參數如下:

 

參數名稱

可選參數值

說明

示例

 

 

 

 

 

type

 

json

 

表示此時value格式必須爲json格式

{name:'test',value:'result'}其中name爲參數名稱,value爲參數值

 

jsonArray

 

表示此時value格式爲jsonArray格式

 

[{name:'test',value:'hh'},{name:'test1',value:'xx'}]

其中name爲參數名稱,value爲參數值

 

customer

用戶自定義解析方法,使用時必須要添加class元素,指向的是一個實現了FlowConfigImpl接口的類

<config type="customer" value="this is test " class="com.jht.workflow.configImpl.FlowConfigIns"/>

value

name,value爲必選參數

name爲參數名稱,value爲參數值

name:'test',value:'result'

 

 

 

 

 

 

2、對於自定義的工作流節點,解析方式有兩種:

 

方法一:自動解析

 

 

import com.jht.workflow.model.ConfigXML;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: DS
 * Date: 13-1-10
 * Time: 上午10:39
 * To change this template use File | Settings | File Templates.
 */
public class FlowXml {
    //得到流內容
    public static String getStringInputStream(InputStream is,String code) {
        String str=null;
        try{
            InputStreamReader isr = new InputStreamReader(is,code);
            StringBuffer sb=new StringBuffer();
            int ch;
            while((ch = isr.read())!=-1 ){
                sb.append((char)ch);
            }
            isr.close();
            is.close();
            str=sb.toString();
            str = str.replace("&lt;","<");
            str = str.replace("&gt;",">");
            str = str.replace("'","\\'");
            str = str.replace("\r","");
            str = str.replace("\n","");
            str = str.replace("\t", "");
        }catch (Exception e){
            e.printStackTrace();
        }
        return str;
    }

    //得到xml接點內容
    public static List getXMLNode(InputStream inputStream){
        List<String> result = new ArrayList<String>();
        try {
            SAXReader reader = new SAXReader();
            Document doc = reader.read(inputStream);
            Element rootElement = doc.getRootElement();
            List<Element> elements = (List<Element>) rootElement.elements();
            for (Element element : elements) {
                String tagName = element.getName();
                if(tagName.equals("decision") || tagName.equals("task") || tagName.equals("state") || tagName.equals("sub") || tagName.equals("sign")){
                    Attribute nameAttribute = element.attribute("name");
                    if(null != nameAttribute)
                        result.add(nameAttribute.getValue());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    //得到xml節點的配置內容
    public static List<ConfigXML> getConfigInfoFromXML(InputStream stream) throws Exception{
        List<ConfigXML> result = new ArrayList<ConfigXML>();
        try{
            SAXReader reader = new SAXReader();
            Document document = reader.read(stream);
            Element element = document.getRootElement();
            Iterator<Element> iterator = element.elementIterator();
            while (iterator.hasNext()){
                Element ele = iterator.next();
                Attribute temp = ele.attribute("name");
                if(ele.hasContent()){
                    Iterator<Element> tempIt = ele.elementIterator();
                    while (tempIt.hasNext()){
                        Element elem = tempIt.next();
                        if("config".equals(elem.getName())){
                            Attribute attr = elem.attribute("type");
                            if(attr==null){
                                throw new Exception("流程的xml的config配置中缺少type屬性");
                            }else {
                                String type = attr.getValue();
                                Attribute attri = elem.attribute("value");
                                if("json".compareToIgnoreCase(type)==0){
                                    ConfigXML xml = new ConfigXML();
                                    xml.setParamNodeName(temp.getValue());
                                    String json = attri.getValue();
                                    JSONObject object = JSONObject.fromObject(json);
                                    xml.setParamName(object.get("name").toString());
                                    xml.setParamValue(object.get("value").toString());
                                    result.add(xml);
                                }else if("jsonArray".compareToIgnoreCase(type)==0){
                                    JSONArray array = JSONArray.fromObject(attri.getValue());
                                    int len = array.size();
                                    for (int i=0;i<len;i++){
                                        JSONObject obj = (JSONObject)array.get(i);
                                        ConfigXML xml = new ConfigXML(obj.get("name").toString(),obj.get("value").toString(),temp.getValue());
                                        result.add(xml);
                                    }
                                }else if("customer".compareToIgnoreCase(type)==0){   //用戶自定義
                                    Attribute clsAttr = elem.attribute("class");
                                    if(clsAttr!=null){
                                        String val = elem.attribute("value").getValue();
                                        String clsName = clsAttr.getValue();
                                        Class cls = Class.forName(clsName);
                                        Object instance = cls.newInstance();
                                        Method method = cls.getMethod("resolve",new Class[]{String.class});
                                        List<ConfigXML> xmlList  = (List<ConfigXML>)method.invoke(instance,val);
                                        for(ConfigXML xml : xmlList){
                                            result.add(xml);
                                        }
                                    }else {
                                        throw new Exception("請指定自定義解析的類名!");
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }catch (DocumentException e) {
            e.printStackTrace();
        }
        return result;
    }
}

 

 

 

方法二,用戶自定義解析:

 

import com.jht.common.jbpm.FlowConfigImpl;
import com.jht.workflow.model.ConfigXML;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: DS
 * Date: 13-5-13
 * Time: 下午4:38
 * To change this template use File | Settings | File Templates.
 */
public class FlowConfigIns implements FlowConfigImpl {

    @Override
    public List<ConfigXML> resolve(String value) {
        List<ConfigXML> list = new ArrayList<ConfigXML>();
        ConfigXML xml = new ConfigXML();
        xml.setParamName("xml said : "+value);
        list.add(xml);
        return list;
    }
}

 

 

3、保存配置信息

 

 

public int saveFlowConfig(String configXml,String processId) throws Exception {
        InputStream stream = new ByteArrayInputStream(configXml.getBytes("utf-8"));
        List<ConfigXML> xmlList = FlowXml.getConfigInfoFromXML(stream);
        for (ConfigXML xml : xmlList){
            JBPM4FlowConfig flowConfig = new JBPM4FlowConfig();
            flowConfig.setFlowId(processId);
            flowConfig.setNodeId(xml.getParamNodeName());
            flowConfig.setParamName(xml.getParamName());
            flowConfig.setParamValue(xml.getParamValue());
            flowConfig.setParamType("0");
            saveObject(flowConfig);
        }
        return 1;
    }

 

 

發佈了19 篇原創文章 · 獲贊 1 · 訪問量 2579
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章