定制netbpm的decision

      今天根据Holiday设计了一个自己的工作流程,在流程中有一个活动是二选一的决策。根据nPdl的规范先做一个activity-state,并在该activity-state中设置transition to一个decision的节点,然后在decision设置二选一的属性及属性值,并设置decision的两个transition to各自的下一活动。在测试的时候发现了两个问题:
      一个是decision中有个handler的性质,根据Holiday中的情况,选了handler="NetBpm.Workflow.Delegation.Impl.Decision.EvaluationDecision, NetBpm"。根据nPdl文档EvaluationDecision是个接口,如果我想修改该接口为自己的处理接口该怎么做nPdl中没有说明。
      另外一个是如果使用EvaluationDecision接口,那么在decision中就必须设置性质:<transition name="approve" to="nextaction" />,其中name必须为“approve”或“disapprove”。
      通过查看Netbpm源代码,发现EvaluationDecision中想让界面上显示“是”/“否”等字样都不行。
      如果修改EvaluationDecision固然可以做到使用自己想要的字符串,但是那样并不规范。
      可以通过添加新的接口来实现,但是如何才能做到不去重新编译netbpm的内核代码,二只是用实现在NetBpm.Workflow.Delegation.Impl.Decision.命名空间下的自己的接口来实现呢?
      nPdl没有介绍,对委托又不是很熟悉,很是困惑。
 
      后来做了一些尝试,配合阅读源代码,发现了解决办法,虽然不是很正规,但至少能满足目前自己的需要了。具体的解决方法如下:
      第一步:新建一个类项目,取名为NetBPMEx,然后在里面实现两个接口,一个是NetBpm.Workflow.Delegation.Impl.Decision.EvaluationDecision,另外自己再定义一个NetBpm.Workflow.Delegation.Impl.Htmlformatter.ChoiceInput。第一个接口是处理decision分支问题的接口。第二个接口是生成界面显示元素的接口,也可以命名为与netbpm内置EvaluationInput相同名字。
      第二步:修改第一步新建的两个接口的代码,其实基本上就是netbpm原来相应接口的代码,现在只是做了一些汉化的改动。当然要记得添加对netbpm,system.web的引用,否则编译过不去哦。把编译好的netbpmex.dll复制到NetBPM的bin目录下。
 1using System;
 2using System.Web;
 3using NetBpm.Workflow.Definition.Attr;
 4using NetBpm.Workflow.Delegation;
 5
 6namespace NetBpm.Workflow.Delegation.Impl.Decision
 7{
 8    /// <summary>
 9    /// EvaluationDecision 的摘要说明。
10    /// </summary>

11    public class EvaluationDecision : IDecisionHandler
12    {
13        public String Decide(IDecisionContext decisionContext)
14        {
15            String transitionName = "";
16
17            String attributeName = (String) decisionContext.GetConfiguration()["attribute"];
18            Object attributeValue = decisionContext.GetAttribute(attributeName);
19
20            if (attributeValue == Evaluation.APPROVE)
21            {
22                transitionName = "";
23            }

24
25            return transitionName;
26        }

27    }

28}

29
30namespace NetBpm.Workflow.Delegation.Impl.Htmlformatter
31{
32    
33    public class ChoiceInput:AbstractConfigurable, IHtmlFormatter
34    {
35        
36        public String ObjectToHtml(System.Object valueObject, System.String parameterName, System.Web.HttpRequest request)
37        {
38            System.Text.StringBuilder htmlBuffer = new System.Text.StringBuilder();
39            htmlBuffer.Append("<table border=0 cellspacing=0 cellpadding=0><tr><td nowrap style=\"background-color:transparent;\">");
40            htmlBuffer.Append("<input type=radio name=\"");
41            htmlBuffer.Append(parameterName);
42            htmlBuffer.Append("\" value=\"");
43            htmlBuffer.Append(Evaluation.APPROVE);
44            htmlBuffer.Append("\">");
45            htmlBuffer.Append("&nbsp; 是");
46            htmlBuffer.Append("</td></tr><tr><td nowrap style=\"background-color:transparent;\">");
47            htmlBuffer.Append("<input type=radio name=\"");
48            htmlBuffer.Append(parameterName);
49            htmlBuffer.Append("\" value=\"");
50            htmlBuffer.Append(Evaluation.DISAPPROVE);
51            htmlBuffer.Append("\">");
52            htmlBuffer.Append("&nbsp; 否");
53            htmlBuffer.Append("</td></tr></table>");
54            
55            return htmlBuffer.ToString();
56        }

57        
58        public Object ParseHttpParameter(System.String text, System.Web.HttpRequest request)
59        {
60            System.Object evaluationResult = null;
61            
62            try
63            {
64                evaluationResult = Evaluation.ParseEvaluation(text);
65            }

66            catch (System.ArgumentException e)
67            {
68                throw new System.FormatException("couldn't parse the Evaluation from value " + text +" exception message: "+e.Message);
69            }

70            
71            return evaluationResult;
72        }

73    }

74}

      第三步:修改processdefinition.xml,注意到3、4行的name就是自定义的EvaluationDecision接口的45、52行的汉字:
1  <decision name="提交确认" handler="NetBpm.Workflow.Delegation.Impl.Decision.EvaluationDecision, NetBPMEx">
2    <parameter name="attribute">确认</parameter>
3    <transition name="是" to="关闭" />
4    <transition name="否" to="下一步" />
5  </decision>
      以及webinterface.xml,其中第4行的ChoiceInput就是自定义的能显示“是”、“否”汉字的页面控件了:
1  <field attribute="确认">
2   <name>确认</name>
3   <description>确认</description>
4   <htmlformatter class="NetBpm.Workflow.Delegation.Impl.Htmlformatter.ChoiceInput,NetBPMEx" />
5  </field>
      最后就是重新生成过程定义包、上传并使用。

      总结:如果有时间和心情,完全可以扩展Decision下的接口,只是我这里因为只是研究,就容我偷个懒吧。
posted @ 2007-06-07 17:59  badwood  阅读(911)  评论(2编辑  收藏  举报
Badwood's Blog