17.3 服务器端代码实现
服务器端的功能是将数据保存到数据库,以及从数据库中取出xml描述文件返回给客户端。本文使用LINQ to Sql Classes的方法对数据库进行操作。如下图所示,增加一个linq的
剩下的工作就是编写更新和获取工作流xml描述的代码了。
l 获取工作流xml描述
/// <summary>
/// 获取工作流xml描述
/// </summary>
/// <param name="workflowID">流程ID</param>
/// <returns></returns>
[WebMethod]
public string GetWorkFlowXML(string workflowID)
{
string xml = "";
using (ShareDesigner.Web.Class.WorkFlowDataContext dataContext = new WorkFlowDataContext())
{
WorkFlow_Process process = dataContext.WorkFlow_Processes.First(p => p.WorkFlowID == workflowID);
if (process != null)
xml = process.WorkFlowXML;
}
return xml;
}
l 更新工作流xml描述
/// <summary>
/// 更新流程xml
/// </summary>
/// <param name="workFlowXml">流程xml描述</param>
[WebMethod]
public void UpdateWorkFlowXML(string workFlowXml) {
using (ShareDesigner.Web.Class.WorkFlowDataContext dataContext = new WorkFlowDataContext()) {
WorkFlow_Process process = new WorkFlow_Process();
Byte[] b = System.Text.UTF8Encoding.UTF8.GetBytes(workFlowXml);
XElement xele = XElement.Load(System.Xml.XmlReader.Create(new MemoryStream(b)));
process.WorkFlowName = xele.Attribute(XName.Get("Name")).Value;
process.WorkFlowID = xele.Attribute(XName.Get("UniqueID")).Value;
process.WorkFlowXML = workFlowXml;
deleteExistsWorkFlow(process.WorkFlowID);
var partNos = from item in xele.Descendants("Activity") select item;
foreach (XElement node in partNos)
{
WorkFlow_Activity activity = new WorkFlow_Activity();
process.WorkFlow_Activities.Add(activity);
activity.ActivityID = node.Attribute(XName.Get("UniqueID")).Value;
activity.ActivityName = node.Attribute(XName.Get("ActivityName")).Value;
activity.ActivityType = node.Attribute(XName.Get("Type")).Value;
}
dataContext.WorkFlow_Processes.InsertOnSubmit(process);
partNos = from item in xele.Descendants("Rule") select item;
WorkFlow_Rule rule = null;
foreach (XElement node in partNos)
{ rule = new WorkFlow_Rule();
rule.BeginActivityID = node.Attribute(XName.Get("BeginActivityUniqueID")).Value;
rule.Condition = node.Attribute(XName.Get("RuleCondition")).Value;
rule.EndActivityID = node.Attribute(XName.Get("EndActivityUniqueID")).Value;
rule.RuleID = node.Attribute(XName.Get("UniqueID")).Value;
rule.RuleName = node.Attribute(XName.Get("RuleName")).Value;
rule.RuleType = node.Attribute(XName.Get("LineType")).Value;
dataContext.WorkFlow_Rules.InsertOnSubmit(rule);
}
dataContext.SubmitChanges();
}
}
有了以上的基础,我们可以在活动属性中设置子流程了。如下图所示:
其中获取子流程的服务方法如下所示:
[WebMethod]
public string GetWorkFlowList()
{
string xml = "";
using (ShareDesigner.Web.Class.WorkFlowDataContext dataContext = new WorkFlowDataContext())
{
try
{
XDocument doc = new XDocument(
new XElement("WorkFlows", from p in dataContext.WorkFlow_Processes
select (new XElement("WorkFlow",
new XAttribute("Name", p.WorkFlowName),
new XAttribute("ID", p.WorkFlowID)))));
xml = doc.ToString();
}
catch (Exception e)
{
xml = "";
}
}
return xml; }
客户端获取流程并显示的方法如下:
Byte[] b = System.Text.UTF8Encoding.UTF8.GetBytes(xml);
XElement xele = XElement.Load(System.Xml.XmlReader.Create(new MemoryStream(b)));
var partNos = from item in xele.Descendants("WorkFlow")
select new WorkflowListItem { Name = item.Attribute("Name").Value, ID = item.Attribute("ID").Value };
cbSubFlowList.ItemsSource = partNos;
cbSubFlowList.SelectedIndex = 0;
其中WorkflowListItem是一个类,定义如下:
public class WorkflowListItem
{
public string Name { get; set; }
public string ID { get; set; }
public WorkflowListItem() { }
public WorkflowListItem(string name, string id)
{
Name = name;
ID = id;
}
}
从上面的代码可以看出,使用.net framework确实可以提高我们的开发效率,并且代码更加简洁。
到此我们的持久化工作已经完成了,但是距离一个可用的工作流设计器还有一定距离。因为还没有关于工作流属性的配置信息,因为工作流系统各家的实现方法都不一样。因此很难有一个统一的标准,后文将根据wfmc提供的工作流参考模型来具体实现剩下的工作。敬请关注。