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("<","<");
str = str.replace(">",">");
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;
}