SheetManager.cs
上传用户:li2971742
上传日期:2021-11-18
资源大小:39096k
文件大小:10k
源码类别:

OA系统

开发平台:

C#

  1. using System;
  2. using System.Collections;
  3. using System.Web.UI;
  4. using System.Web.UI.WebControls;
  5. using System.ComponentModel;
  6. using System.Drawing;
  7. using System.Drawing.Design;
  8. [assembly: TagPrefix("OThinker.H3.WorkSheet", "SheetControls")]
  9. namespace OThinker.H3.WorkSheet
  10. {
  11.     /// <summary>
  12.     /// 这个控件用于进行汇签,填写大家的意见
  13.     /// </summary>
  14.     [ToolboxBitmap(typeof(SheetManager), "OThinker.H3.WorkSheet.SheetManager.bmp")]
  15.     [DefaultProperty("Text"), ToolboxData("<{0}:SheetManager runat=server></{0}:SheetManager>")]
  16.     public class SheetManager : System.Web.UI.WebControls.WebControl
  17.     {
  18.         /// <summary>
  19.         /// 工作项头
  20.         /// </summary>
  21.         protected SheetHeader SheetHeader = new SheetHeader();
  22.         /// <summary>
  23.         /// 添加操作面板
  24.         /// </summary>
  25.         protected OThinker.H3.WorkSheet.SheetActionPane ActionPane = new SheetActionPane();
  26.         /// <summary>
  27.         /// 构造函数
  28.         /// </summary>
  29.         public SheetManager()
  30.         {
  31.             // 工作项的头
  32.             this.Controls.Add(this.SheetHeader);
  33.             // 操作面板
  34.             this.ActionPane.Submit += new SheetSubmitEventHandler(this.Submit);
  35.             this.Controls.Add(this.ActionPane);
  36.             this.Load += new EventHandler(SheetManager_Load);
  37.         }
  38.         private void SheetManager_Load(object sender, EventArgs e)
  39.         {
  40.             this.SheetHeader.Enviroment = this.Enviroment;
  41.             this.ActionPane.Enviroment = this.Enviroment;
  42.             // 验证权限
  43.             if (!this.SheetPage.ValidateAuthorization())
  44.             {
  45.                 this.Enviroment.NotifyMessage("当前用户没有权限操作该工作项");
  46.             }
  47.             switch (this.Enviroment.SheetMode)
  48.             {
  49.                 case SheetMode.Work:
  50.                     // 工作模式
  51.                     if (this.Enviroment.WorkItem == null)
  52.                     {
  53.                         this.Enviroment.NotifyMessage("工作项不存在");
  54.                     }
  55.                     else if (this.Enviroment.WorkItem.State == OThinker.H3.WorkItem.WorkItemState.Finished)
  56.                     {
  57.                         this.Enviroment.NotifyMessage("该工作项已经完成");
  58.                     }
  59.                     else if (this.Enviroment.WorkItem.State == OThinker.H3.WorkItem.WorkItemState.Canceled)
  60.                     {
  61.                         this.Enviroment.NotifyMessage("该工作项已经取消");
  62.                     }
  63.                     else
  64.                     {
  65.                         // 开始做该工作项
  66.                         this.Enviroment.WorkItemManager.DoWorkItem(this.Enviroment.WorkItemID);
  67.                     }
  68.                     break;
  69.                 case SheetMode.View:
  70.                     break;
  71.                 case SheetMode.Originate:
  72.                     break;
  73.                 case SheetMode.Unspecified:
  74.                     break;
  75.                 default:
  76.                     throw new NotImplementedException();
  77.             }
  78.             // 加载数据
  79.             this.SheetPage.LoadDataFields();
  80.         }
  81.         private ISheetPage SheetPage
  82.         {
  83.             get
  84.             {
  85.                 if (!(this.Page is ISheetPage))
  86.                 {
  87.                     throw new Exception("SheetManager控件必须添加到实现了OThinker.H3.WorkShet.ISheetPage的页面中。");
  88.                 }
  89.                 return (ISheetPage)this.Page;
  90.             }
  91.         }
  92.         /// <summary>
  93.         /// 环境
  94.         /// </summary>
  95.         [Browsable(false)]
  96.         public virtual SheetEnviroment Enviroment
  97.         {
  98.             get
  99.             {
  100.                 return this.SheetPage.Enviroment;
  101.             }
  102.         }
  103.         /// <summary>
  104.         /// 提交
  105.         /// </summary>
  106.         /// <param name="sender">发送参数的对象</param>
  107.         /// <param name="e">事件参数</param>
  108.         public virtual void Submit(object sender, SheetSubmitEventArgs e)
  109.         {
  110.             // 检查当前登录用户是否存在
  111.             if (this.Enviroment.UserValidator == null)
  112.             {
  113.                 throw new NotImplementedException("User not login.");
  114.             }
  115.             // 验证输入是否合法
  116.             System.Collections.Generic.List<string> errors = new System.Collections.Generic.List<string>();
  117.             if (this.Page is ISheetPage && !((ISheetPage)this.Page).ValidateDataFields(e, errors))
  118.             {
  119.                 System.Text.StringBuilder builder = new System.Text.StringBuilder();
  120.                 foreach (string error in errors)
  121.                 {
  122.                     builder.Append(error);
  123.                 }
  124.                 this.Enviroment.NotifyMessage(builder.ToString());
  125.                 return;
  126.             }
  127.             // 检查是否需要发起
  128.             if (this.Enviroment.SheetMode == SheetMode.Originate)
  129.             {
  130.                 this.OriginateInstance(this.ActionPane.GroupSelector.SelectedValue);
  131.             }
  132.             //无论是否是更新和完成任务,都要执行此任务,对各个控件的数据进行提交
  133.             if (this.Page is ISheetPage)
  134.             {
  135.                 ((ISheetPage)this.Page).SaveDataFields(e);
  136.             }
  137.             string workItemId = this.Enviroment.WorkItem.WorkItemID;
  138.             if (e.Finish)
  139.             {
  140.                 OThinker.Data.BoolMatchValue approval = OThinker.Data.BoolMatchValue.Unspecified;
  141.                 if (this.Page is ISheetPage)
  142.                 {
  143.                     approval = ((ISheetPage)this.Page).Approval;
  144.                 }
  145.                 string comment = null;
  146.                 if (this.Page is ISheetPage)
  147.                 {
  148.                     comment = ((ISheetPage)this.Page).Comment;
  149.                 }
  150.                 // 进入到完成界面上
  151.                 this.Enviroment.FinishWorkItem(
  152.                     workItemId, 
  153.                     e.DestActivityName, 
  154.                     this.ActionPane.GroupSelector.SelectedValue, 
  155.                     approval, 
  156.                     comment, 
  157.                     e.Description);
  158.             }
  159.         }
  160.         #region 发起流程
  161.         /// <summary>
  162.         /// 发起流程的事件
  163.         /// </summary>
  164.         [Browsable(true), Category("WorkSheet")]
  165.         public event EventHandler<OriginateInstanceEventArgs> OriginatingInstance;
  166.         /// <summary>
  167.         /// 创建一个新的流程实例
  168.         /// </summary>
  169.         public void OriginateInstance(string OriginateGroup)
  170.         {
  171.             // 创建流程实例
  172.             string instanceID = this.Enviroment.InstanceManager.CreateInstance(
  173.                 this.Enviroment.WorkflowPackage,
  174.                 this.Enviroment.WorkflowName,
  175.                 this.Enviroment.WorkflowVersion,
  176.                 null,
  177.                 this.Enviroment.Participant,
  178.                 OriginateGroup,
  179.                 this.Enviroment.Organization.GetUserDept(this.Enviroment.Participant),
  180.                 false,
  181.                 Instance.InstanceContext.UnspecifiedID,
  182.                 null,
  183.                 Instance.Token.UnspecifiedID);
  184.             this.Enviroment.InstanceId = instanceID;
  185.             // 设置紧急程度为普通
  186.             OThinker.H3.Messages.MessageEmergencyType emergency = Messages.MessageEmergencyType.Normal;
  187.             // 如果是发起后需要用户填写表单的模式,则紧急程度为高
  188.             if (this.Enviroment.Workflow.StartWithSheet)
  189.             {
  190.                 emergency = OThinker.H3.Messages.MessageEmergencyType.High;
  191.             }
  192.             // 调用发起事件
  193.             OriginateInstanceEventArgs originateArgs = null;
  194.             if (this.OriginatingInstance != null)
  195.             {
  196.                 originateArgs = new OriginateInstanceEventArgs(instanceID);
  197.                 this.OriginatingInstance(this, originateArgs);
  198.             }
  199.             // 启动流程的消息
  200.             OThinker.H3.Messages.StartInstanceMessage startInstanceMessage
  201.                 = new OThinker.H3.Messages.StartInstanceMessage(
  202.                 emergency,
  203.                 instanceID,
  204.                 originateArgs == null ? null : originateArgs.InstanceParameterTable.Count == 0 ? null : originateArgs.InstanceParameterTable,
  205.                 Instance.PriorityType.Normal,
  206.                 false,
  207.                 0,
  208.                 OThinker.H3.Instance.Token.UnspecifiedID,
  209.                 null);
  210.             this.Enviroment.InstanceManager.SendMessage(startInstanceMessage);
  211.             if (!this.Enviroment.Workflow.StartWithSheet)
  212.             {
  213.                 // 返回工作项为空
  214.                 this.Enviroment.WorkItemID = H3.WorkItem.WorkItem.NullWorkItemID;
  215.                 return;
  216.             }
  217.             // 查找新创建的工作项
  218.             string[] jobs = null;
  219.             for (int triedTimes = 0; triedTimes < 10; triedTimes++)
  220.             {
  221.                 System.Threading.Thread.Sleep(1500);
  222.                 jobs = this.Enviroment.WorkItemManager.Query(
  223.                     null,
  224.                     new string[] { instanceID },
  225.                     new string[] { this.Enviroment.Participant },
  226.                     System.DateTime.MinValue,
  227.                     System.DateTime.MaxValue,
  228.                     OThinker.H3.WorkItem.WorkItemState.Unfinished);
  229.                 if (jobs != null && jobs.Length != 0)
  230.                 {
  231.                     break;
  232.                 }
  233.             }
  234.             if (jobs == null || jobs.Length == 0)
  235.             {
  236.                 this.Enviroment.NotifyMessage("发起流程失败");
  237.                 this.Enviroment.WorkItemID = OThinker.H3.WorkItem.WorkItem.NullWorkItemID;
  238.             }
  239.             else
  240.             {
  241.                 // 返回新创建的工作项
  242.                 this.Enviroment.WorkItemID = jobs[0];
  243.             }
  244.         }
  245.         #endregion
  246.     }
  247. }