8.2 启动与执行业务流程
如图8.3所示,当用户登录系统后,根据用户的角色权限,系统页面左侧将显示与其权限相符的工作菜单。对于参与部门业务的用户,“我的任务列表”是默认权限,点击“我的任务列表”,将打开工作列表页面。工作列表页面包括“启动业务流程”列表和任务实例列表。根据用户的角色权限,如果用户具有某项业务的首任务(如用地审批业务的首任务“接受用地申请”)权限,在“启动业务流程”列表中将显示该项业务,输入具体业务描述(用于流程监控中区别不同业务内容),点击启动按钮,将触发系统创建该项业务的新的过程实例,以及业务的首任务实例,并转到任务处理页面,由该用户完成对手任务的处理;任务实例列表则包含用户有权处理的属于某些业务过程的任务环节(任务实例),其中包括新创建的任务实例(业务过程一个环节的结束会触发工作流引擎生成该环节的下一任务实例)、流程退回的任务实例(因业务数据等错误需要重新完成)、临时保存的任务实例(用户在任务实例处理页面点击了“临时保存”按钮)等。
图8.3 启动与执行业务过程
8.2.1 启动业务过程的TaskInstanceList.aspx程序代码
......
<form id="Form1" method="post" runat="server">
<table height="60" cellSpacing="0" cellPadding="0" width="100%" border="0">
<tr><td><asp:Label id="Label1" runat="server"></asp:Label></td></tr>
</table>
<table cellSpacing="0" cellPadding="0" width="100%" align="center" border="0">
<tr><td>
<asp:datagrid id="DataGrid2" runat="server" Width="100%"
DataKeyField="TaskInstanceID" AllowPaging="True">
<ItemStyle Font-Size="13px" HorizontalAlign="Center" Height="28px"
VerticalAlign="Bottom"></ItemStyle>
<HeaderStyle Font-Size="16px" Font-Bold="True" HorizontalAlign="Center"
Height="30px" VerticalAlign="Middle" BackColor="#EEEEEE"></HeaderStyle>
<Columns>
<asp:BoundColumn DataField="taskname" HeaderText="正在处理的任务">
<HeaderStyle HorizontalAlign="Left" ForeColor="#FF9900"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="processdesc" HeaderText="业务内容"></asp:BoundColumn>
<asp:BoundColumn DataField="startdate" HeaderText="任务开始时间"></asp:BoundColumn>
<asp:BoundColumn DataField="IsPostedBack" HeaderText="是否流程回退的任务">
</asp:BoundColumn>
<asp:ButtonColumn Text="启动" CommandName="Delete"></asp:ButtonColumn>
<asp:BoundColumn Visible="False" DataField="ProcessInstanceID"></asp:BoundColumn>
</Columns>
<PagerStyle Font-Size="9pt" HorizontalAlign="Right" ForeColor="#3366FF"
Mode="NumericPages"></PagerStyle>
</asp:datagrid></td>
</tr>
</table>
......
<table cellSpacing="0" cellPadding="0" width="100%" align="center" border="0">
<tr>
<td style="FONT-SIZE: 15px" align="left"><FONT face="宋体"></FONT></td>
</tr>
<tr><td>
<asp:datagrid id="DataGrid1" runat="server" Width="100%"
DataKeyField="ProcessID" AllowPaging="True">
<ItemStyle Font-Size="13px" HorizontalAlign="Center" Height="28px"
VerticalAlign="Bottom"></ItemStyle>
<HeaderStyle Font-Size="16px" Font-Bold="True" HorizontalAlign="Left" Height="30px"
ForeColor="#FF9900" VerticalAlign="Middle" BackColor="#EEEEEE"></HeaderStyle>
<Columns>
<asp:BoundColumn DataField="ProcessName" HeaderText="启动业务流程">
</asp:BoundColumn>
<asp:BoundColumn Visible="False" DataField="RelatedTable" ReadOnly="True">
</asp:BoundColumn>
<asp:BoundColumn Visible="False" DataField="StartTaskID"></asp:BoundColumn>
<asp:BoundColumn Visible="False" DataField="IdentifiedField"></asp:BoundColumn>
<asp:TemplateColumn>
<ItemTemplate>
<FONT face="宋体">
<asp:Label id="Label2" runat="server">业务内容概述:</asp:Label>
</FONT>
<FONT face="宋体">
<asp:TextBox id="TextBox1" runat="server" Columns="60"></asp:TextBox>
</FONT>
</ItemTemplate>
</asp:TemplateColumn>
<asp:ButtonColumn Text="启动" CommandName="Delete"></asp:ButtonColumn>
</Columns>
<PagerStyle Font-Size="9pt" HorizontalAlign="Right" ForeColor="#3366FF"
Mode="NumericPages"></PagerStyle>
</asp:datagrid></td>
</tr>
</table>
</form>
......
TaskInstanceList.aspx.cs文件代码:
......
using System.Text.RegularExpressions;
using DataAccess;
using CommonTools;
using WFEngine;
using UserResourceManagement;
namespace workflow.Process
{
public class TaskInstanceList : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.DataGrid DataGrid2;
protected System.Web.UI.WebControls.Label Label1;
DataSet rolesds;//用户所拥有的角色列表
private void Page_Load(object sender, System.EventArgs e)
{
if(! IsPostBack)
{
//验证用户是否登录
if(Session["userid"] == null)
Response.Redirect("../Message.aspx");
rolesds=new UserManagement().GetRoles(Session["userid"].ToString());
if(rolesds == null || rolesds.Tables[0].Rows.Count<1)
{
Response.Write("用户未被赋予任何角色权限,请与管理员联系!");
return;
}
Label1.Text="";
BindGrid2();
BindGrid1();
}
}
void BindGrid1()
{
//有首任务执行权限的用户显示相关流程,以启动业务。
//调用过程定义类(参考前面相关章节)方法获取过程定义信息及开始任务编号(根据迁移定
//义得到)等
DataSet ds=new ProcessDefinitionClass()
.getProcessListByUser(Session["userid"].ToString());
if(ds !=null)//如果用户没有任务就不显示
{
if(ds.Tables[0].Rows.Count>=1)
{
DataGrid1.DataSource=ds;
DataGrid1.DataBind();
}
}
}
void BindGrid2()
{
DataSet ds=new TaskInstanceClass()
.getTaskListByUserID(Session["userid"].ToString());
if(ds !=null & ds.Tables[0].Rows.Count>=1)
{
DataGrid2.DataSource=ds;
DataGrid2.DataBind();
}
else
Label1.Text="呵呵,目前暂时没有要处理的业务。您可以喘口气儿、歇一会儿......";
}
//启动按钮所借用的删除事件处理函数(创建业务过程实例)
private void CreateProcessInstance(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//获取流程定义相关内容
string processid=DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
string description=((TextBox)e.Item.FindControl("TextBox1")).Text.Trim();
if(description == "")
{
((Label)e.Item.FindControl("Label2")).Text="业务实例描述不能为空";
((Label)e.Item.FindControl("Label2")).ForeColor=Color.Red;
return;
}
Regex reg=
new Regex(@"^[\u4e00-\u9fa5]*《*[\u4e00-\u9fa5]*》*[\u4e00-\u9fa5]*\w*$");
if(! reg.IsMatch(description))
{
((Label)e.Item.FindControl("Label2")).Text="只能输入汉字或字母或下划线";
((Label)e.Item.FindControl("Label2")).ForeColor=Color.Red;
return;
}
string relatedtable=e.Item.Cells[1].Text.Trim();
string identityname=e.Item.Cells[3].Text.Trim();
string starttaskid=e.Item.Cells[2].Text.Trim();
string starttime=DateTime.Now.ToString();
//创建流程实例新记录,获得流程实例新记录编号。
ProcessInstanceClass processins=new ProcessInstanceClass();
processins.RelatedTable=relatedtable;
processins.ProcessDefinitionID=int.Parse(processid);
processins.Description=description;
processins.StartTime=starttime;
int processinsid=processins.CreateProcessInstance();
if(processinsid == -1)
{
Response.Write(processins.ProcessInstancesErrDes);
return;
}
string processinstanceid=processinsid.ToString();
//创建首任务实例,获得任务实例编号
TaskInstanceClass taskins=new TaskInstanceClass();
int taskinsid=taskins.CreateTaskInstance(processinstanceid,starttaskid,starttime);
if(taskinsid == -1)
{
((Label)e.Item.FindControl("Label2")).Text=
taskins.TaskInstancesErrDes+starttaskid;
((Label)e.Item.FindControl("Label2")).ForeColor=Color.Red;
return;
}
string taskinstanceid=taskinsid.ToString();
//创建过程关联的业务表新记录并获得标识字段的值
Base basecode=new Base();
string identityfieldvalue=basecode.InserBlankRecord(relatedtable,identityname);
//修改流程实例的相应字段
processins=new ProcessInstanceClass();
if(! processins
.UpdateProcessInstance(processinstanceid,taskinstanceid,identityfieldvalue))
{
((Label)e.Item.FindControl("Label2")).Text=processins.ProcessInstancesErrDes;
((Label)e.Item.FindControl("Label2")).ForeColor=Color.Red;
return;
}
//转到任务处理页面,由当前用户处理过程首任务
Response.Redirect("TaskInstance.aspx?id="+taskinstanceid);
}
//任务实例列表中的启动按钮借用的删除事件处理函数(启动任务实例)
private void TaskInstanceHdndler(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//判断流程是否已经被冻结(防止在此时管理员刚进行了冻结操作),如果已经冻结则不再执行
//后续操作。
ProcessInstanceClass processins=new ProcessInstanceClass();
if(processins.Isfreezed(e.Item.Cells[5].Text))//e.Item.Cells[5].Text为流程实例ID
{
RegisterStartupScript("alert","<script>alert('该流程已经被冻结,
请在解冻后继续处理或与管理员联系。');</script>");
return;
}
//转到任务实例处理页面
Response.Redirect("TaskInstance.aspx?id="
+DataGrid2.DataKeys[e.Item.ItemIndex].ToString());
}
private void DataGrid2_PageIndexChanged(object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid2.CurrentPageIndex=e.NewPageIndex;
BindGrid2();
}
private void DataGrid1_PageIndexChanged(object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex=e.NewPageIndex;
BindGrid1();
}
}
}