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

OA系统

开发平台:

C#

  1. using System;
  2. using System.Web.UI;
  3. using System.Web.UI.WebControls;
  4. using System.ComponentModel;
  5. namespace OThinker.H3.WorkSheet
  6. {
  7.     /// <summary>
  8.     /// Summary description for SheetWorkItemActionPane.
  9.     /// </summary>
  10.     [DefaultProperty("Text"), ToolboxData("<{0}:SheetActionPane runat=server></{0}:SheetActionPane>")]
  11.     public class SheetActionPane : System.Web.UI.WebControls.WebControl
  12.     {
  13.         private SheetEnviroment _Enviroment;
  14.         /// <summary>
  15.         /// 环境
  16.         /// </summary>
  17.         public SheetEnviroment Enviroment
  18.         {
  19.             get
  20.             {
  21.                 return this._Enviroment;
  22.             }
  23.             set
  24.             {
  25.                 this._Enviroment = value;
  26.                 if (this._Enviroment == null)
  27.                 {
  28.                 }
  29.                 else if (this._Enviroment.IsWorkMode || this._Enviroment.IsOriginateMode)
  30.                 {
  31.                     // 操作面板
  32.                     this.InitAsWorkMode();
  33.                 }
  34.                 else if (this._Enviroment.IsRetrieveMode)
  35.                 {
  36.                     // 操作面板
  37.                     this.InitAsRetreiveMode();
  38.                 }
  39.             }
  40.         }
  41.         #region 按钮
  42.         private DropDownList _GroupSelector = new DropDownList();
  43.         public DropDownList GroupSelector
  44.         {
  45.             get
  46.             {
  47.                 return this._GroupSelector;
  48.             }
  49.         }
  50.         // 保存按钮
  51.         protected LinkButton _Save = new LinkButton();
  52.         public LinkButton Save
  53.         {
  54.             get
  55.             {
  56.                 return this._Save;
  57.             }
  58.         }
  59.         // 查看状态
  60.         protected HyperLink _ViewInstanceState = new HyperLink();
  61.         public HyperLink ViewInstanceState
  62.         {
  63.             get
  64.             {
  65.                 return this._ViewInstanceState;
  66.             }
  67.         }
  68.         // 征询意见
  69.         protected HyperLink _Consult = new HyperLink();
  70.         public HyperLink Consult
  71.         {
  72.             get
  73.             {
  74.                 return this._Consult;
  75.             }
  76.         }
  77.         // 转发
  78.         protected HyperLink _Forward = new HyperLink();
  79.         public HyperLink Forward
  80.         {
  81.             get
  82.             {
  83.                 return this._Forward;
  84.             }
  85.         }
  86.         // 取回
  87.         protected HyperLink _Retrieve = new HyperLink();
  88.         public HyperLink Retrieve
  89.         {
  90.             get
  91.             {
  92.                 return this._Retrieve;
  93.             }
  94.         }
  95.         // 打回
  96.         protected LinkButton _Rollback = new LinkButton();
  97.         public LinkButton Rollback
  98.         {
  99.             get
  100.             {
  101.                 return this._Rollback;
  102.             }
  103.         }
  104.         // 跳转按钮
  105.         protected LinkButton _Jump = new LinkButton();
  106.         public LinkButton Jump
  107.         {
  108.             get
  109.             {
  110.                 return this._Jump;
  111.             }
  112.         }
  113.         // 可选择的任务
  114.         protected DropDownList _JumpDests = new DropDownList();
  115.         public DropDownList JumpDests
  116.         {
  117.             get
  118.             {
  119.                 return this._JumpDests;
  120.             }
  121.         }
  122.         // 取消
  123.         protected LinkButton _Cancel = new LinkButton();
  124.         public LinkButton Cancel
  125.         {
  126.             get
  127.             {
  128.                 return this._Cancel;
  129.             }
  130.         }
  131.         // 挂起
  132.         protected LinkButton _Suspend = new LinkButton();
  133.         public LinkButton Suspend
  134.         {
  135.             get
  136.             {
  137.                 return this._Suspend;
  138.             }
  139.         }
  140.         #endregion
  141.         public event SheetSubmitEventHandler Submit;
  142.         public SheetActionPane()
  143.         {
  144.         }
  145.         public void InitAsRetreiveMode()
  146.         {            
  147.             this.Retrieve.Text = "取回";
  148.             this.Retrieve.NavigateUrl = this.Enviroment.GetRetrieveUrl(this.Enviroment.WorkItemID);
  149.             this.Controls.Add(this.Retrieve);
  150.         }
  151.         public void InitAsWorkMode()
  152.         {
  153.             // 保存按钮
  154.             if (this.Enviroment.SheetMode == SheetMode.Work || this.Enviroment.SheetMode == SheetMode.Originate)
  155.             {
  156.                 this.AddSplit();
  157.                 this.Save.Text = "保存";
  158.                 this.Save.Click += new EventHandler(this.Save_Click);
  159.                 this.Controls.Add(this.Save);
  160.             }
  161.             // 查看流程状态
  162.             if (
  163.                 this.Enviroment.SheetMode == SheetMode.Work &&
  164.                 this.Enviroment.WorkItem.PermittedActions.ViewInstanceState)
  165.             {
  166.                 this.AddSplit();
  167.                 this.Controls.Add(this.ViewInstanceState);
  168.                 this.ViewInstanceState.Text = "流程状态";
  169.                 this.ViewInstanceState.Target = "_blank";
  170.                 this.ViewInstanceState.NavigateUrl = this.Enviroment.GetViewInstanceStateUrl(this.Enviroment.InstanceId);
  171.             }
  172.             // 征询意见
  173.             if (
  174.                 this.Enviroment.SheetMode == SheetMode.Work &&
  175.                 this.Enviroment.WorkItem.ItemType == OThinker.H3.WorkItem.WorkItemType.Normal && 
  176.                 this.Enviroment.WorkItem.PermittedActions.Consult)
  177.             {
  178.                 this.AddSplit();
  179.                 this.Controls.Add(this.Consult);
  180.                 this.Consult.Text = "征询意见";
  181.                 this.Consult.NavigateUrl = this.Enviroment.GetConsultUrl(this.Enviroment.WorkItemID);
  182.             }
  183.             // 转发
  184.             if (
  185.                 this.Enviroment.SheetMode == SheetMode.Work &&
  186.                 this.Enviroment.WorkItem.ItemType == OThinker.H3.WorkItem.WorkItemType.Normal && 
  187.                 this.Enviroment.WorkItem.PermittedActions.Forward)
  188.             {
  189.                 this.AddSplit();
  190.                 this.Controls.Add(this.Forward);
  191.                 this.Forward.Text = "转发";
  192.                 this.Forward.NavigateUrl = this.Enviroment.GetForwardUrl(this.Enviroment.WorkItemID);
  193.             }
  194.             // 反向流转
  195.             if (
  196.                 this.Enviroment.SheetMode == SheetMode.Work &&
  197.                 this.Enviroment.WorkItem.ItemType == OThinker.H3.WorkItem.WorkItemType.Normal && 
  198.                 this.Enviroment.WorkItem.PermittedActions.Backward)
  199.             {
  200.                 // 允许回退,所以要获得前驱
  201.                 OThinker.H3.WorkflowTemplate.ActivityTemplate[] preActivitys = this.Enviroment.Workflow.GetActivityPreActivity(this.Enviroment.ActivityName);
  202.                 if (preActivitys != null)
  203.                 {
  204.                     foreach (OThinker.H3.WorkflowTemplate.ActivityTemplate preActivity in preActivitys)
  205.                     {
  206.                         this.AddSplit();
  207.                         this.AddText("↑");
  208.                         LinkButton backward = new LinkButton();
  209.                         this.Controls.Add(backward);
  210.                         backward.Text = preActivity.Name;
  211.                         backward.Click += new EventHandler(this.JmpButton_Click);
  212.                     }
  213.                 }
  214.             }
  215.             // 提交
  216.             OThinker.H3.WorkflowTemplate.ActivityTemplate[] postActivitys = this.Enviroment.Workflow.GetActivityPostActivitys(this.Enviroment.ActivityName);
  217.             if (
  218.                 postActivitys == null || postActivitys.Length == 0 ||
  219.                 (this.Enviroment.SheetMode == SheetMode.Originate && !this.Enviroment.Workflow.GetActivity(this.Enviroment.ActivityName).PermittedActions.Choose) ||
  220.                 (this.Enviroment.SheetMode == SheetMode.Work && 
  221.                     (this.Enviroment.WorkItem.ItemType == OThinker.H3.WorkItem.WorkItemType.Consultancy || 
  222.                     !this.Enviroment.WorkItem.PermittedActions.Choose)))
  223.             {
  224.                 this.AddSplit();
  225.                 this.AddText("↓");
  226.                 // 只有零个或一个后继
  227.                 LinkButton submit = new LinkButton();
  228.                 submit.Text = "提交";
  229.                 this.Controls.Add(submit);
  230.                 submit.Click += new EventHandler(this.Submit_Click);
  231.             }
  232.             else
  233.             {
  234.                 foreach (OThinker.H3.WorkflowTemplate.ActivityTemplate postActivity in postActivitys)
  235.                 {
  236.                     this.AddSplit();
  237.                     this.AddText("↓");
  238.                     LinkButton post = new LinkButton();
  239.                     this.Controls.Add(post);
  240.                     post.Text = postActivity.Name;
  241.                     post.Click += new EventHandler(this.JmpButton_Click);
  242.                 }
  243.             }
  244.             // 打回
  245.             if (
  246.                 this.Enviroment.SheetMode == SheetMode.Work &&
  247.                 this.Enviroment.WorkItem.ItemType == OThinker.H3.WorkItem.WorkItemType.Normal && 
  248.                 this.Enviroment.WorkItem.PermittedActions.Rollback)
  249.             {
  250.                 this.AddSplit();
  251.                 this.AddText("↑");
  252.                 this.Rollback.Text = "打回";
  253.                 this.Rollback.Click += new EventHandler(this.Rollback_Click);
  254.                 this.Controls.Add(this.Rollback);
  255.             }
  256.             // 跳转
  257.             if (
  258.                 (
  259.                     this.Enviroment.SheetMode == SheetMode.Originate && 
  260.                     this.Enviroment.Workflow.GetActivity(this.Enviroment.ActivityName).PermittedActions.Jump) ||
  261.                 (
  262.                     this.Enviroment.SheetMode == SheetMode.Work && 
  263.                     this.Enviroment.WorkItem.ItemType == OThinker.H3.WorkItem.WorkItemType.Normal && 
  264.                     this.Enviroment.WorkItem.PermittedActions.Jump))
  265.             {
  266.                 this.AddSplit();
  267.                 foreach (OThinker.H3.WorkflowTemplate.ActivityTemplate processActivity in this.Enviroment.Workflow.Activities)
  268.                 {
  269.                     if (processActivity.Name != this.Enviroment.ActivityName)
  270.                     {
  271.                         this.JumpDests.Items.Add(processActivity.Name);
  272.                     }
  273.                 }
  274.                 this.Controls.Add(this.JumpDests);
  275.                 this.Jump.Text = "跳转";
  276.                 this.Jump.Click += new EventHandler(Jump_Click);
  277.                 this.Controls.Add(this.Jump);
  278.             }
  279.             // 取消/终止
  280.             if (
  281.                 this.Enviroment.SheetMode == SheetMode.Work &&
  282.                 this.Enviroment.WorkItem.ItemType == OThinker.H3.WorkItem.WorkItemType.Normal && 
  283.                 (
  284.                     this.Enviroment.WorkItem.PermittedActions.Terminate || 
  285.                     this.Enviroment.WorkItem.TokenId == Instance.Token.InitialTokenId))
  286.             {
  287.                 this.AddSplit();
  288.                 this.Cancel.Text = this.Enviroment.WorkItem.TokenId == Instance.Token.InitialTokenId ? "取消" : "终止";
  289.                 this.Cancel.Click += new EventHandler(this.Cancel_Click);
  290.                 this.Controls.Add(this.Cancel);
  291.             }
  292.             // 挂起和反挂起
  293.             if (
  294.                 this.Enviroment.SheetMode == SheetMode.Work &&
  295.                 this.Enviroment.WorkItem.ItemType == OThinker.H3.WorkItem.WorkItemType.Normal && 
  296.                 this.Enviroment.WorkItem.PermittedActions.Suspend)
  297.             {
  298.                 this.AddSplit();
  299.                 string instanceId = this.Enviroment.WorkItem.InstanceId;
  300.                 OThinker.H3.Instance.InstanceContext context = OThinker.H3.Server.Engine.InstanceManager.GetInstanceContext(instanceId);
  301.                 if (context != null)
  302.                 {
  303.                     bool suspended = context.Suspended;
  304.                     this.Suspend.Text = suspended ? "继续" : "挂起";
  305.                     this.Suspend.Click += new EventHandler(this.Suspend_Click);
  306.                     this.Controls.Add(this.Suspend);
  307.                 }
  308.             }
  309.             // 操作的身份
  310.             if (this.Enviroment.WorkflowActivity.RequireGroup)
  311.             {
  312.                 string[] groups = null;
  313.                 if (this.Enviroment.SheetMode == SheetMode.Originate)
  314.                 {
  315.                     groups = this.Enviroment.UserValidator.Groups;
  316.                 }
  317.                 else if (
  318.                     this.Enviroment.SheetMode == SheetMode.Work && 
  319.                     this.Enviroment.WorkItem.ItemType == OThinker.H3.WorkItem.WorkItemType.Normal)
  320.                 {
  321.                     // 获得普通型的组
  322.                     groups =
  323.                         this.Enviroment.WorkItem == null ?
  324.                             this.Enviroment.UserValidator.NormalGroups :
  325.                             this.Enviroment.Organization.GetParentGroups(this.Enviroment.WorkItem.Participant, OThinker.Organization.VisibleType.Normal);
  326.                 }
  327.                 if (groups != null && groups.Length != 0)
  328.                 {
  329.                     foreach (string group in groups)
  330.                     {
  331.                         string groupName = this.Enviroment.Organization.GetName(group);
  332.                         this.GroupSelector.Items.Add(new ListItem(groupName, group));
  333.                     }
  334.                     this.GroupSelector.SelectedIndex = 0;
  335.                 }
  336.                 else
  337.                 {
  338.                     this.GroupSelector.Visible = false;
  339.                 }
  340.                 if (this.GroupSelector.Visible)
  341.                 {
  342.                     this.AddSplit();
  343.                 }
  344.                 this.Controls.Add(this.GroupSelector);
  345.             }
  346.         }
  347.         private void Rollback_Click(object sender, EventArgs e)
  348.         {
  349.             // 保存表单
  350.             if (this.Submit != null)
  351.             {
  352.                 this.Submit(this, new SheetSubmitEventArgs(false, null, null));
  353.             }
  354.             // 检查是否允许打回
  355.             H3.Instance.IToken token = this.Enviroment.TokenPool.GetToken(this.Enviroment.WorkItem.InstanceId, this.Enviroment.WorkItem.TokenId);
  356.             bool rollbackable = true;
  357.             // 如果是回滚,那么需要检查是否允许回滚
  358.             if (token == null || token.PreTokens == null)
  359.             {
  360.                 rollbackable = false;
  361.             }
  362.             else
  363.             {
  364.                 foreach (long preTokenId in token.PreTokens)
  365.                 {
  366.                     long[] branches = this.Enviroment.TokenPool.QueryPostToken(this.Enviroment.WorkItem.InstanceId, preTokenId);
  367.                     if (branches != null && branches.Length > 1)
  368.                     {
  369.                         rollbackable = false;
  370.                         break;
  371.                     }
  372.                 }
  373.             }
  374.             // 发送打回消息
  375.             if (rollbackable == false)
  376.             {
  377.                 this.Enviroment.NotifyMessage("不具备打回的条件");
  378.             }
  379.             else
  380.             {
  381.                 // 完成该任务
  382.                 OThinker.H3.Server.Engine.WorkItemManager.FinishWorkItem(this.Enviroment.WorkItem.WorkItemID, this.Enviroment.Participant, this.GroupSelector.SelectedValue, OThinker.Data.BoolMatchValue.Unspecified, null);
  383.                 // 标记为打回
  384.                 OThinker.H3.Server.Engine.TokenPool.AppendDescription(this.Enviroment.WorkItem.InstanceId, this.Enviroment.WorkItem.TokenId, "打回");
  385.                 OThinker.H3.Messages.CancelActivityMessage rollbackMessage
  386.                     = new OThinker.H3.Messages.CancelActivityMessage(
  387.                         OThinker.H3.Messages.MessageEmergencyType.Normal,
  388.                         this.Enviroment.WorkItem.InstanceId,
  389.                         this.Enviroment.WorkItem.ActivityName,
  390.                         true,
  391.                         null);
  392.                 OThinker.H3.Server.Engine.InstanceManager.SendMessage(rollbackMessage);
  393.                 this.Enviroment.NotifyMessage("工作项已经打回");
  394.             }
  395.         }
  396.         private int ControlCount = 0;
  397.         private void AddSplit()
  398.         {
  399.             this.ControlCount++;
  400.             if (this.ControlCount > 1)
  401.             {
  402.                 Label split = new Label();
  403.                 split.Text = "&nbsp;|&nbsp;";
  404.                 this.Controls.Add(split);
  405.             }
  406.         }
  407.         private void AddText(string Text)
  408.         {
  409.             Label label = new Label();
  410.             label.Text = Text;
  411.             this.Controls.Add(label);
  412.         }
  413.         private void JmpButton_Click(object sender, EventArgs e)
  414.         {
  415.             // 跳转
  416.             // 获得目的任务名称
  417.             string destActivityName = ((LinkButton)sender).Text;
  418.             if (this.Submit != null)
  419.             {
  420.                 this.Submit(sender, new SheetSubmitEventArgs(true, destActivityName, "跳转到活动:" + destActivityName));
  421.             }
  422.         }
  423.         private void Submit_Click(object sender, EventArgs e)
  424.         {
  425.             // 按照默认的方法进行流转
  426.             if (this.Submit != null)
  427.             {
  428.                 this.Submit(sender, new SheetSubmitEventArgs(true, null, "提交"));
  429.             }
  430.         }
  431.         private void Jump_Click(object sender, EventArgs e)
  432.         {
  433.             if (this.Submit != null && this.JumpDests != null && this.JumpDests.SelectedValue != null && this.JumpDests.SelectedValue != "")
  434.             {
  435.                 this.Submit(sender, new SheetSubmitEventArgs(true, this.JumpDests.SelectedValue, "跳转到活动:" + this.JumpDests.SelectedValue));
  436.             }
  437.         }
  438.         private void Save_Click(object sender, EventArgs e)
  439.         {
  440.             if (this.Submit != null)
  441.             {
  442.                 this.Submit(sender, new SheetSubmitEventArgs(false, null, null));
  443.                 this.Enviroment.NotifyMessage("表单保存完毕!");
  444.             }
  445.         }
  446.         private void Cancel_Click(object sender, EventArgs e)
  447.         {
  448.             string instanceId = this.Enviroment.WorkItem.InstanceId;
  449.             // 取消
  450.             this.Enviroment.CancelInstance(instanceId);
  451.         }
  452.         private void Suspend_Click(object sender, EventArgs e)
  453.         {
  454.             string instanceId = this.Enviroment.WorkItem.InstanceId;
  455.             LinkButton suspend = (LinkButton)sender;
  456.             // 要发送的消息
  457.             OThinker.H3.Messages.Message message = null;
  458.             if (suspend.Text == "挂起")
  459.             {
  460.                 message = new OThinker.H3.Messages.SuspendMessage(OThinker.H3.Messages.MessageEmergencyType.Normal, instanceId);
  461.             }
  462.             else
  463.             {
  464.                 message = new OThinker.H3.Messages.ResumeMessage(OThinker.H3.Messages.MessageEmergencyType.Normal, instanceId);
  465.             }
  466.             OThinker.H3.Server.Engine.InstanceManager.SendMessage(message);
  467.             this.Enviroment.NotifyMessage("挂起流程成功");
  468.         }
  469.         protected override void Render(HtmlTextWriter writer)
  470.         {
  471.             // 添加禁止重复提交的JS
  472.             foreach (System.Web.UI.Control control in this.Controls)
  473.             {
  474.                 if (control is LinkButton)
  475.                 {
  476.                     bool confirm = false;
  477.                     if (control != this.Save)
  478.                     {
  479.                         confirm = true;
  480.                     }
  481.                     this.ProhibitReSubmit((LinkButton)control, true, confirm);
  482.                 }
  483.             }
  484.             writer.Write("<table border="0" cellpadding="0" cellspacing="0" width="100%">");
  485.             writer.Write("<tr>");
  486.             writer.Write("<td bgcolor="#e0e6f6">");
  487.             writer.Write("<div id="" + CheckSubmit_ButtonDiv + "">");
  488.             if (this.DesignMode)
  489.             {
  490.                 writer.Write("保存 | 流程状态 | 提交");
  491.             }
  492.             else
  493.             {
  494.                 base.Render(writer);
  495.             }
  496.             writer.Write("</div>");
  497.             writer.Write("<div id="" + CheckSubmit_HintID + "" style="display:none;">正在处理您的请求,请稍等……</div>");
  498.             writer.Write("</td></tr></table>");
  499.             // 输出检查重复提交的JS
  500.             writer.Write("<script>" + CheckSubmit_FunctionDef + "</script>");
  501.         }
  502.         #region 避免重复提交的JS
  503.         /// <summary>
  504.         /// 包含按钮的DIV的ID
  505.         /// </summary>
  506.         public const string CheckSubmit_ButtonDiv = "_H3_Submit_ButtonDiv";
  507.         /// <summary>
  508.         /// 包含提示的DIV的ID
  509.         /// </summary>
  510.         public const string CheckSubmit_HintID = "_H3_Submit_Hint";
  511.         /// <summary>
  512.         /// 禁止重复提交的函数名称
  513.         /// </summary>
  514.         public const string CheckSubmit_FunctionName = "_H3_Submit_HideButtion";
  515.         /// <summary>
  516.         /// 函数定义
  517.         /// </summary>
  518.         public const string CheckSubmit_FunctionDef =
  519.             "function " + CheckSubmit_FunctionName + "(){" +
  520.             CheckSubmit_ButtonDiv + ".style.display='none';" +
  521.             CheckSubmit_HintID + ".style.display='block';" + 
  522.             "}";
  523.         private void ProhibitReSubmit(LinkButton Control, bool DisableActionPane, bool Confirm)
  524.         {
  525.             string js_Disable = CheckSubmit_FunctionName + "();";
  526.             string js_PostBack = "__doPostBack('" + Control.ClientID + "', '');";
  527.             string js_Confirm = "if(confirm('确定要进行该操作“" + Control.Text + "”?'))";
  528.             string js = null;
  529.             if (Confirm)
  530.             {
  531.                 if (DisableActionPane)
  532.                 {
  533.                     js = js_Confirm + "{" + js_Disable + js_PostBack + "}";
  534.                 }
  535.                 else
  536.                 {
  537.                     js = js_Confirm + "{" + js_PostBack + "}";
  538.                 }
  539.             }
  540.             else
  541.             {
  542.                 if (DisableActionPane)
  543.                 {
  544.                     js = js_Disable + js_PostBack;
  545.                 }
  546.                 else
  547.                 {
  548.                     js = js_PostBack;
  549.                 }
  550.             }
  551.             if (js != null && js != "")
  552.             {
  553.                     Control.Attributes["href"] = "javascript:" + js;
  554.             }
  555.         }
  556.         #endregion
  557.     }
  558. }