Exemplo n.º 1
0
        //找到下个节点
        private string prepareNextNode(string processInstanceID, string lastUserID, int OrganizationUnitID, ref string email)
        {
            WFManualWorkItem nextNode = getNextNode(processInstanceID);

            if (nextNode == null)
            {
                return(null); //process finished
            }
            else
            {
                string participant      = "";
                string participantEmail = "";
                nextNode.UserID = nextNode.UserID.Split('\\')[1].ToString();

                //translate participant(防止多个流程角色)
                string[] ids = nextNode.OriginalUserID.Split('\\')[1].ToString().Split("|".ToCharArray());
                for (int i = 0; i < ids.Length; i++)
                {
                    string uid = string.Empty;

                    string PositionTypeCode = ids[i].ToString();
                    getUserIDByIdentifier(PositionTypeCode, OrganizationUnitID, ref uid, ref email);

                    //当前角色下找不到人
                    if (uid == "none")
                    {
                        continue;
                    }
                    else
                    {
                        participant      += uid + "&";
                        participantEmail += email;
                    }
                }
                participant = participant.TrimEnd('&');
                // update workitem
                APWorkFlow.NodeStatusDataTable dt = new APWorkFlow.NodeStatusDataTable();
                APWorkFlow.NodeStatusRow       dr = dt.NewNodeStatusRow();
                if (!String.IsNullOrEmpty(participant) && participant != "none")   //user found
                {
                    dr.STATUS      = FlowNodeStatus.ASSIGNED;
                    dr.PARTICIPANT = participant;
                    //dr.AcceptChanges();
                    dt.AddNodeStatusRow(dr);
                    dt.AcceptChanges();
                    StringWriter sw = new StringWriter();
                    dt.WriteXml(sw);
                    string clientData = sw.ToString();

                    System.Collections.ArrayList attrList = new System.Collections.ArrayList();
                    attrList.Add(new NameValue("UserID", dr.PARTICIPANT));//更新该步骤的userid
                    attrList.Add(new NameValue("CLIENT_DATA", clientData));
                    NameValue[] attributes = (NameValue[])attrList.ToArray(typeof(NameValue));
                    api.UpdateWorkItem(nextNode.WorkItemID, attributes);
                    //update email
                    if (!String.IsNullOrEmpty(participantEmail))
                    {
                        email = participantEmail;
                    }
                    return(dr.PARTICIPANT);
                }
                else     // 没有找到人员
                {
                    dr.STATUS      = FlowNodeStatus.ONERROR;
                    dr.PARTICIPANT = "P" + lastUserID + "P";
                    //dr.ERROR_MSG = "错误代码:Invalid UserId (" + nextNode.ProcInstName + "," + nextNode.OriginalUserID + "," + nextNode.UserID + ")";
                    dr.ERROR_MSG = "未能找到下一步审批人!";
                    //dr.AcceptChanges();
                    dt.AddNodeStatusRow(dr);
                    dt.AcceptChanges();
                    StringWriter sw = new StringWriter();
                    dt.WriteXml(sw);
                    string clientData = sw.ToString();

                    System.Collections.ArrayList attrList = new System.Collections.ArrayList();
                    attrList.Add(new NameValue("UserID", dr.PARTICIPANT));
                    attrList.Add(new NameValue("CLIENT_DATA", clientData));
                    NameValue[] attributes = (NameValue[])attrList.ToArray(typeof(NameValue));
                    api.UpdateWorkItem(nextNode.WorkItemID, attributes);

                    // sendMail(getUserEmailByID(int.Parse(lastUserID)), "", "您提交的单据发生错误", "单据编号为:" + nextNode.ProcInstName);

                    throw new ApplicationException(dr.ERROR_MSG);
                }
            }
        }
Exemplo n.º 2
0
        public string approve(bool isApproved, string approvalComment, string userID, string userName, string processInstanceID, string proxyUserName, int OrganizationUnitID, ref string email)
        {
            try {
                if (String.IsNullOrEmpty(processInstanceID))
                {
                    throw new Exception("approve - processInstanceID is empty");
                }
                if (String.IsNullOrEmpty(userID))
                {
                    throw new Exception("approve - userID is empty");
                }
                if (String.IsNullOrEmpty(userName))
                {
                    throw new Exception("approve - userName is empty");
                }

                string where = "WF_MANUAL_WORKITEMS.PROC_INST_ID='" + processInstanceID + "' and WF_MANUAL_WORKITEMS.STATUS in ('" + WFManualWorkItem.ASSIGNED + "','" + WFManualWorkItem.OVERDUE + "','" + WFManualWorkItem.REASSIGNED + "','" + WFManualWorkItem.PSEUDO + "') and CHARINDEX('P" + userID + "P',WF_MANUAL_WORKITEMS.USER_ID)>0";
                WFManualWorkItem[] wis = api.QueryWorkListEx(where);

                if (wis.Length > 0)
                {
                    WFManualWorkItem wi = wis[0];
                    APWorkFlow.NodeStatusDataTable dt = new APWorkFlow.NodeStatusDataTable();
                    StringReader sr = new StringReader(wi.ClientData);
                    dt.ReadXml(sr);
                    APWorkFlow.NodeStatusRow dr = dt[0];
                    if (String.IsNullOrEmpty(proxyUserName))
                    {
                        dr.APPROVED_BY = userName;
                    }
                    else
                    {
                        dr.APPROVED_BY = proxyUserName + " 代理 " + userName;
                    }
                    if (String.IsNullOrEmpty(approvalComment))
                    {
                        dr.COMMENTS = "";
                    }
                    else
                    {
                        dr.COMMENTS = approvalComment;
                    }
                    dr.COMPLETED_DATE = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
                    if (isApproved)//若状态为通过
                    {
                        //  首先判断是否为尝试修复的
                        if (dr.STATUS.Equals(FlowNodeStatus.ONERROR))
                        {
                            // recover this node
                            System.Collections.ArrayList attrList = new System.Collections.ArrayList();
                            attrList.Add(new NameValue("UserID", wi.OriginalUserID));
                            NameValue[] attributes = (NameValue[])attrList.ToArray(typeof(NameValue));
                            api.UpdateWorkItem(wi.WorkItemID, attributes);
                        }
                        else
                        {
                            dr.STATUS = FlowNodeStatus.APPROVED;//通过
                            //dr.AcceptChanges();
                            dt.AcceptChanges();
                            StringWriter sw = new StringWriter();
                            dt.WriteXml(sw);
                            string clientData = sw.ToString();                               //将信息写入XML并保存

                            WFEvent evt = api.CompleteWorkItemEx(wi.WorkItemID, clientData); //完成当前步骤
                            evt = getFullEvent(evt);
                            if (!String.IsNullOrEmpty(evt.Error))
                            {
                                throw new Exception("approve - " + evt.Error);
                            }
                        }
                    }
                    else
                    {
                        //直接取消流程

                        if (dr.STATUS.Equals(FlowNodeStatus.ONERROR))
                        {
                            dr.COMMENTS = dr.ERROR_MSG;
                        }
                        dr.STATUS = FlowNodeStatus.CANCELLED;
                        //dr.AcceptChanges();
                        dt.AcceptChanges();
                        StringWriter sw = new StringWriter();
                        dt.WriteXml(sw);
                        string clientData = sw.ToString();

                        ArrayList attrList = new ArrayList();
                        attrList.Add(new NameValue("CLIENT_DATA", clientData));
                        NameValue[] nv = (NameValue[])attrList.ToArray(typeof(NameValue));
                        api.UpdateWorkItem(wi.WorkItemID, nv);
                        WFEvent evt = api.CancelProcInst(processInstanceID);
                        evt = getFullEvent(evt);
                        if (!String.IsNullOrEmpty(evt.Error))
                        {
                            throw new Exception("approve - " + evt.Error);
                        }
                    }

                    //prepare next node

                    string nextUser = prepareNextNode(processInstanceID, userID, OrganizationUnitID, ref email);
                    //if (!String.IsNullOrEmpty(nextUser) && nextUser == "P" + userID + "P") // 如果下一步包有且只有同一个人就自动审批(后面需要在审批历史里加入自动通过的标识)
                    //    return approve(isApproved, approvalComment, userID, userName, processInstanceID, proxyUserName, OrganizationUnitID, ref email);
                    //else
                    return(nextUser);
                }
                else
                {
                    throw new Exception("approve - 用户不能操作当前流程");
                }
            } catch (Exception e) {
                if (e is ApplicationException)
                {
                    throw e;
                }
                throw new Exception(errorMSGPrefix + e.Message, e);
            }
        }