using System;
using System.Data;
using System.Collections;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;

namespace digioz
{
    public class PageDisplayManagement
    {
        private static PortalSettings coPortalSettings;

        public static PageDisplayList GetPageDisplayList(int piMenuID)
        {
            PageDisplayList loPageDisplayList = new PageDisplayList();
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                PageDisplay loPageDisplay;
                DataTable loPageDisplayTable;

                string lsSQL = "SELECT * FROM tblPageDisplay WHERE menu_id='" + piMenuID + "';";

                try
                {
                    loDB.openConnection();
                    loPageDisplayTable = loDB.QueryDBDataset(lsSQL);
                    foreach (DataRow loDR in loPageDisplayTable.Rows)
                    {
                        loPageDisplay = new PageDisplay();
                        loPageDisplay.ID = Convert.ToInt64(loDR["page_id"]);
                        if (loDR["menu_id"] == DBNull.Value)
                        {
                            loPageDisplay.MenuID = -1;
                        }
                        else
                        {
                            loPageDisplay.MenuID = Convert.ToInt32(loDR["menu_id"]);
                        }
                        loPageDisplay.Title = Convert.ToString(loDR["page_title"]);
                        loPageDisplay.Keyword = Convert.ToString(loDR["page_keywords"]);
                        loPageDisplay.Description = Convert.ToString(loDR["page_description"]);
                        loPageDisplay.Content = Convert.ToString(loDR["page_content"]);
                        loPageDisplayList.Add(loPageDisplay);
                    }
                }
                finally
                {
                    loDB.closeConnection();
                }
            }
            else
            {
                IDataNonRelational loDB = DataFactoryNonRelational.GetDataObject(coPortalSettings);
                loDB.openEntity();
                IEnumerable loObjs = loDB.GetEntity("PageDisplay");                
                foreach (object obj in loObjs)
                {
                    PageDisplay loPage = (PageDisplay)obj;
                    if (loPage.MenuID == piMenuID)
                    {
                        loPageDisplayList.Add(loPage);
                    }
                }
                loDB.closeEntity();
            }
            return loPageDisplayList;
        }

        /// <summary>
        /// Overload
        /// Get all available PageDisplay records
        /// </summary>
        /// <returns></returns>
        public static PageDisplayList GetPageDisplayList()
        {
            PageDisplayList loPageDisplayList = new PageDisplayList();
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                PageDisplay loPageDisplay;
                DataTable loPageDisplayTable;
                string lsSQL = "SELECT * FROM tblPageDisplay;";

                try
                {
                    loDB.openConnection();
                    loPageDisplayTable = loDB.QueryDBDataset(lsSQL);
                    foreach (DataRow loDR in loPageDisplayTable.Rows)
                    {
                        loPageDisplay = new PageDisplay();
                        loPageDisplay.ID = Convert.ToInt64(loDR["page_id"]);

                        if (loDR["menu_id"] == DBNull.Value)
                        {
                            loPageDisplay.MenuID = -1;
                        }
                        else
                        {
                            loPageDisplay.MenuID = Convert.ToInt32(loDR["menu_id"]);
                        }
                        loPageDisplay.Title = Convert.ToString(loDR["page_title"]);
                        loPageDisplay.Keyword = Convert.ToString(loDR["page_keywords"]);
                        loPageDisplay.Description = Convert.ToString(loDR["page_description"]);
                        loPageDisplay.Content = Convert.ToString(loDR["page_content"]);
                        loPageDisplayList.Add(loPageDisplay);
                    }
                }
                finally
                {
                    loDB.closeConnection();
                }
            }
            else
            {

                IDataNonRelational loDB = DataFactoryNonRelational.GetDataObject(coPortalSettings);
                IEnumerable loObjs = loDB.GetEntity("PageDisplay");
                foreach (object obj in loObjs)
                {
                    loPageDisplayList.Add((PageDisplay)obj);
                }
            }
            return loPageDisplayList;
        }


        /// <summary>
        /// Insert a new Display record or update an existing one
        /// </summary>
        /// <param name="pPage"></param>
        /// <returns></returns>
        public static bool Save(PageDisplay pPage)
        {
            bool lbReturnValue = false;

            if (pPage.ID == -1)
            {
                lbReturnValue = Insert(pPage);
            }
            else
            {
                lbReturnValue = Update(pPage);
            }
            return lbReturnValue;
        }

        /// <summary>
        /// Insert a new Display record
        /// </summary>
        /// <param name="pPage"></param>
        public static bool Insert(PageDisplay pPage)
        {
            bool lbReturnValue = false;

            coPortalSettings = PortalSettingsManagement.GetPortalSettings();
            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                SqlParameter[] loParameters;
                loParameters = new SqlParameter[] {
                             new SqlParameter("@menu_id", DbType.Int32),
                             new SqlParameter("@page_title", DbType.String),
                             new SqlParameter("@page_keywords", DbType.String),
                             new SqlParameter("@page_description", DbType.String),
                             new SqlParameter("@page_content", DbType.String)};

                if (pPage.MenuID != -1)
                {
                    loParameters[0].Value = pPage.MenuID;
                }
                else
                {
                    loParameters[0].Value = DBNull.Value;
                }
                loParameters[1].Value = pPage.Title;
                loParameters[2].Value = pPage.Keyword;
                loParameters[3].Value = pPage.Description;
                loParameters[4].Value = pPage.Content;

                try
                {
                    int liCount = 0;
                    string lsCmd = "spInsertPageDisplay";
                    loDB.openConnection();
                    loDB.ExecDBProc(lsCmd,
                                    out liCount,
                                    loParameters);
                    lbReturnValue = (liCount == 1); // One row inserted
                }
                finally
                {
                    loDB.closeConnection();
                }
            }
            else
            {
                digioz.XML xml = new XML(coPortalSettings);
                xml.SaveEntity(pPage);
            }
            return lbReturnValue;
        }

        /// <summary>
        /// Insert a new Display record
        /// </summary>
        /// <param name="pPage"></param>
        public static bool Update(PageDisplay pPage)
        {
            bool lbReturnValue = false;

            coPortalSettings = PortalSettingsManagement.GetPortalSettings();
            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                SqlParameter[] loParameters;
                loParameters = new SqlParameter[] {
                             new SqlParameter("@page_id", DbType.Int64),
                             new SqlParameter("@menu_id", DbType.UInt32),
                             new SqlParameter("@page_title", DbType.String),
                             new SqlParameter("@page_keywords", DbType.String),
                             new SqlParameter("@page_description", DbType.String),
                             new SqlParameter("@page_content", DbType.String)};

                loParameters[0].Value = pPage.ID;
                if (pPage.MenuID != -1)
                {
                    loParameters[1].Value = pPage.MenuID;
                }
                else
                {
                    loParameters[1].Value = DBNull.Value;
                }
                loParameters[2].Value = pPage.Title;
                loParameters[3].Value = pPage.Keyword;
                loParameters[4].Value = pPage.Description;
                loParameters[5].Value = pPage.Content;

                try
                {
                    int liCount = 0;
                    string lsCmd = "spUpdatePageDisplay";
                    loDB.openConnection();
                    loDB.ExecDBProc(lsCmd,
                                    out liCount,
                                    loParameters);
                    lbReturnValue = (liCount == 1); // One row inserted
                }
                finally
                {
                    loDB.closeConnection();
                }
            }
            else
            {
                digioz.XML xml = new XML(coPortalSettings);
                xml.SaveEntity(pPage);
            }
            return lbReturnValue;
        }


        /// <summary>
        /// Delete a record from the Display table
        /// </summary>
        /// <param name="pPageId"></param>
        public static bool Delete(long pPageId)
        {
            bool lbReturnValue = false;
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();
            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                try
                {
                    string lsCmd = "spDeletePageDisplay";
                    SqlParameter[] loParameters = new SqlParameter[] { 
                                new SqlParameter("@page_id", DbType.Int64) };
                    loParameters[0].Value = pPageId;
                    int liCount = 0;
                    loDB.openConnection();
                    loDB.ExecDBProc(lsCmd, out liCount, loParameters);
                    lbReturnValue = (liCount == 1); // One row deleted
                }
                finally
                {
                    loDB.closeConnection();
                }
            }
            else
            {
                PageDisplay loPage = new PageDisplay();
                loPage.ID = pPageId;
                digioz.XML xml = new XML(coPortalSettings);
                xml.DeleteEntity(loPage);
            }
            return lbReturnValue;
        }

        /// <summary>
        /// Get a Display based on its ID
        /// </summary>
        /// <param name="pPageId"></param>
        /// <returns></returns>
        public static PageDisplay getPageDetail(long pPageId)
        {
            PageDisplay loPage = null;
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();
            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);

                try
                {
                    string lsCmd = "spGetPageDisplay";
                    int liCount = 0;
                    SqlParameter[] loParameters = new SqlParameter[] { 
                                                   new SqlParameter("@page_id", DbType.Int64) };
                    loParameters[0].Value = pPageId;
                    loDB.openConnection();
                    DataTable loDT = loDB.QueryDBDatasetProc(lsCmd, out liCount, loParameters);
                    if (loDT.Rows.Count > 0)
                    {
                        loPage = new PageDisplay();
                        DataRow loDR = loDT.Rows[0] as DataRow;
                        loPage.ID = Convert.ToInt64(loDR["page_id"]);
                        if (loDR["menu_id"] == DBNull.Value)
                        {
                            loPage.MenuID = -1;
                        }
                        else
                        {
                            loPage.MenuID = Convert.ToInt32(loDR["menu_id"]);
                        }
                        loPage.Title = Convert.ToString(loDR["page_title"]);
                        loPage.Keyword = Convert.ToString(loDR["page_keywords"]);
                        loPage.Description = Convert.ToString(loDR["page_description"]);
                        loPage.Content = Convert.ToString(loDR["page_content"]);
                    }
                }
                finally
                {
                    loDB.closeConnection();
                }
            }
            else
            {
                
                digioz.XML xml = new XML(coPortalSettings);
                loPage = (PageDisplay)xml.GetEntity(int.Parse(pPageId.ToString()));
            }
            return loPage;
        }

    }
}


