<%@ WebHandler Language="C#" Class="PassportService" %>

using System;
using System.Data;
using System.Web;
using System.Text;
using System.Web.Security;
using System.Xml;

public class PassportService : IHttpHandler {
    private int m_UserID=0;
    public void ProcessRequest (HttpContext context) 
    {
        context.Response.ContentType = "text/xml";
        string action = context.Request.Params["Action"];
        if (action == "CheckReg")
        {
            CheckReg(context);

        }
        else if (action =="GetAnnounce")
        {
            GetAnnounce(context);

        }
        else
        {
            HowaveFuncion _chk = new HowaveFuncion();
            if (_chk.CheckUserLogined())
            {
                m_UserID = Convert.ToInt32(_chk.UserID);
                switch (action)
                {
                    case "AddRss":
                        AddRss(context);
                        break;
                    case "GetMyRssList":
                        GetMyRssList(context, m_UserID);
                        break;
                    case "GetRssContent":
                        GetRssContent(context);
                        break;
                    default:
                        break;

                }

                context.Response.Flush();
                context.Response.Close();
            }
            else
            {
                context.Response.Write("当前用户未登录，请先登录");
                context.Response.Close();
            }
        }
    }

    private void AddRss(HttpContext context)
    {
        string RssURL = context.Request.Params["RssUrl"];
        string RssName = context.Request.Params["RssName"];
        string result="";
        if (RssName != "" && RssURL != "")
        {
            MySpace _rss = new MySpace();
            if (_rss.AddRss(m_UserID, RssURL, RssName))            
                result = "<rss><url>" + RssURL + "</url><name>" + RssName + "</name></rss>";           
            else
                result = "<rss><url>err</url><name>err</name></rss>";         
            context.Response.Write(result);
            

        }
    }

    private void GetMyRssList(HttpContext context,int UserID)
    {
        if (UserID > 0)
        {
            MySpace _rss = new MySpace();
            DataTable dt = _rss.GetMyRss(UserID);
            if (dt.Rows.Count > 0)
            {
                StringBuilder _result = new StringBuilder("<rss>",300);                
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    _result.Append("<item><url>" + dt.Rows[i]["RssUrl"].ToString() + "</url><name>"+dt.Rows[i]["ClassName"].ToString()+"</name></item>");

                }
                _result.Append("</rss>");
                context.Response.Write(_result.ToString());

            }
        }

    }

    private void CheckReg(HttpContext context)
    {
        string result="";
        string RegName = context.Request.Params["UserName"];
        UserBLL _user = new UserBLL();
        DataTable dt = _user.GetUserByUserName(RegName);
        if (dt.Rows.Count > 0)
            result = "该用户名已经被注册";
        else
            result = "该用户名可以注册";
        context.Response.ContentType = "text/plain";
        context.Response.Write(result);

    }

    private void GetRssContent(HttpContext context)
    {
        XmlDocument doc = new XmlDocument();
        string m_RssUrl=context.Request.Params["rssurl"];
        doc.Load(m_RssUrl);
        context.Response.ContentType = "text/xml";
        if (doc.InnerXml.Contains("encoding=\"gbk\""))
            doc.InnerXml = doc.InnerXml.Replace("encoding=\"gbk\"", "");
        context.Response.Write(doc.InnerXml);

    }
  
    public bool IsReusable {
        get {
            return false;
        }
    }
    private void GetAnnounce(HttpContext context)
    {
        AnnounceBLL announce = new AnnounceBLL();
        DataTable dt = announce.GetAnnounces();
        if (dt.Rows.Count > 0)
        {
            StringBuilder _result = new StringBuilder("<rss>", 300);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if(Convert.ToDateTime(dt.Rows[i]["AnnounceEnd"])>DateTime.Now)
                    _result.Append("<item><url>ShowResource.aspx?DefaultValue=" + dt.Rows[i]["AnnounceID"].ToString() + "</url><name>" + dt.Rows[i]["AnnounceTitle"].ToString() + "</name></item>");

            }
            _result.Append("</rss>");
            context.Response.Write(_result.ToString());

        }

    }

}