using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HySoft.Workflow.WFEngine;
using System.Xml;

namespace HySoft.BaseCallCenter.Web.tools
{
    /// <summary>
    ///ParseWorkFlowTask 的摘要说明
    /// </summary>
    public class ParseWorkFlowTask
    {
        public ParseWorkFlowTask()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }

        /// <summary>
        /// 解析
        /// </summary>
        /// <returns></returns>
        public BusineOpr[] ParseRunOpr(string xml)
        {
            BusineOpr[] busineOpr;
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                try
                {
                    xmlDoc.LoadXml(xml);
                }
                catch
                { }
                XmlNode xe = xmlDoc.SelectSingleNode("RunOprList");
                busineOpr = new BusineOpr[xe.ChildNodes.Count];

                for (int i = 0; i < xe.ChildNodes.Count; i++)
                {
                    XmlNode xnNode = xe.ChildNodes[i];
                    if (xnNode.NodeType == XmlNodeType.Element)
                    {//如果是元素

                        XmlElement xeDataInfo = (XmlElement)xnNode;
                        if (xeDataInfo.Name == "RunOpr")
                        {
                            BusineOpr opr = new BusineOpr();
                            opr.ID = xeDataInfo.Attributes["ID"].Value;
                            opr.Name = xeDataInfo.Attributes["Name"].Value;
                            busineOpr[i] = opr;
                        }
                    }
                }
            }
            catch
            {
                return null;
            }
            return busineOpr;
        }

        /// <summary>
        /// 解析
        /// </summary>
        /// <returns></returns>
        public BusineOpr[] ParseRollBackOpr(string xml)
        {
            BusineOpr[] busineOpr;
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                try
                {
                    xmlDoc.LoadXml(xml);
                }
                catch
                { }
                XmlNode xe = xmlDoc.SelectSingleNode("RollBackOprList");
                busineOpr = new BusineOpr[xe.ChildNodes.Count];

                for (int i = 0; i < xe.ChildNodes.Count; i++)
                {
                    XmlNode xnNode = xe.ChildNodes[i];
                    if (xnNode.NodeType == XmlNodeType.Element)
                    {//如果是元素

                        XmlElement xeDataInfo = (XmlElement)xnNode;
                        if (xeDataInfo.Name == "RollBackOpr")
                        {
                            BusineOpr opr = new BusineOpr();
                            opr.ID = xeDataInfo.Attributes["ID"].Value;
                            opr.Name = xeDataInfo.Attributes["Name"].Value;
                            busineOpr[i] = opr;
                        }
                    }
                }
            }
            catch
            {
                return null;
            }
            return busineOpr;
        }

        /// <summary>
        /// 解析
        /// </summary>
        /// <returns></returns>
        public AssignParticipator[] ParseAssignParticipatorOpr(string xml)
        {
            AssignParticipator[] busineOpr;
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                try
                {
                    xmlDoc.LoadXml(xml);
                }
                catch
                { }
                XmlNode xe = xmlDoc.SelectSingleNode("AssignParticipatorList");
                busineOpr = new AssignParticipator[xe.ChildNodes.Count];

                for (int i = 0; i < xe.ChildNodes.Count; i++)
                {
                    XmlNode xnNode = xe.ChildNodes[i];
                    if (xnNode.NodeType == XmlNodeType.Element)
                    {//如果是元素

                        XmlElement xeDataInfo = (XmlElement)xnNode;
                        if (xeDataInfo.Name == "AssignParticipator")
                        {
                            AssignParticipator opr = new AssignParticipator();
                            opr.NodeID = xeDataInfo.Attributes["NodeID"].Value;
                            opr.ParticipatorInfo = xeDataInfo.Attributes["ParticipatorInfo"].Value;
                            opr.NodeName = xeDataInfo.Attributes["NodeName"].Value;
                            busineOpr[i] = opr;
                        }
                    }
                }
            }
            catch
            {
                return null;
            }
            return busineOpr;
        }

        /// <summary>
        /// 解析
        /// </summary>
        /// <returns></returns>
        public AssignParticipator[] ParseAssignParticipatorOprByArrXml(string arrxml)
        {
            AssignParticipator[] busineOpr = null;
            try
            {
                string[] arr = arrxml.Split(new string[] { "%#AssignParticipator#%" }, StringSplitOptions.None);
                if (arr.Length > 0)
                {
                    busineOpr = new AssignParticipator[arr.Length];
                    int i = 0;
                    foreach (string xml in arr)
                    {
                        AssignParticipator opr = new AssignParticipator();
                        string[] arrinfo = xml.Split(new string[] { "%#Info#%" }, StringSplitOptions.None);
                        opr.NodeID = arrinfo[0];
                        opr.ParticipatorInfo = arrinfo[2];
                        opr.NodeName = HttpUtility.UrlDecode(arrinfo[1]);
                        busineOpr[i] = opr;
                        i++;
                    }
                }
                else
                {
                    busineOpr = null;
                }
            }
            catch
            {
                busineOpr = null;
            }
            return busineOpr;
        }
    }
}
