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;

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

        public static ConfigurationList GetConfigurationList()
        {
            ConfigurationList loConfigurationList = new ConfigurationList();
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                Configuration loConfiguration;
                DataTable loConfigTable;

                string lsSQL = "SELECT * FROM tblConfig;";

                loDB.openConnection();
                loConfigTable = loDB.QueryDBDataset(lsSQL);
                loDB.closeConnection();

                foreach (DataRow loDR in loConfigTable.Rows)
                {
                    loConfiguration = new Configuration();
                    loConfiguration.ID = (int)loDR["id"];
                    loConfiguration.KeyName = (string)loDR["key_name"];
                    loConfiguration.KeyValue = (string)loDR["key_value"];
                    loConfiguration.Removable = (string)loDR["removable"];
                    loConfigurationList.Add(loConfiguration);
                }
            }
            else
            {
                IDataNonRelational loDB = DataFactoryNonRelational.GetDataObject(coPortalSettings);
                loDB.openEntity();
                IEnumerable loObjs = loDB.GetEntity("Configuration");                
                foreach (object obj in loObjs)
                {
                    loConfigurationList.Add((Configuration)obj);
                }
                loDB.closeEntity();
            }
            return loConfigurationList;
        }


        public static bool SaveConfiguration(Configuration poConfiguration, string psMode)
        {
            bool lbResult = false;
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                string lsSQL = String.Empty;

                if (psMode == "New")
                {
                    lsSQL = "EXEC spInsertConfiguration "
                            + "@key_name = '" + poConfiguration.KeyName + "',"
                            + "@key_value = '" + poConfiguration.KeyValue + "',"
                            + "@removable = '" + poConfiguration.Removable + "';";

                    loDB.openConnection();
                    loDB.ExecDB(lsSQL);
                    loDB.closeConnection();
                    lbResult = true;
                }
                else if (psMode == "Update")
                {
                    lsSQL = "EXEC spUpdateConfiguration "
                            + "@key_id = " + poConfiguration.ID + ","
                            + "@key_name = '" + poConfiguration.KeyName + "',"
                            + "@key_value = '" + poConfiguration.KeyValue + "',"
                            + "@removable = '" + poConfiguration.Removable + "';";

                    loDB.openConnection();
                    loDB.ExecDB(lsSQL);
                    loDB.closeConnection();
                    lbResult = true;
                }
            }
            else
            {
                digioz.XML xml = new XML(coPortalSettings);
                xml.SaveEntity(poConfiguration);
            }
            return lbResult;
        }

        public static bool DeleteConfiguration(Configuration poConfiguration)
        {
            bool lbResult = false;
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                string lsSQL = String.Empty;

                lsSQL = "EXEC spDeleteConfiguration "
                        + "@key_id = " + poConfiguration.ID + ";";

                loDB.openConnection();
                loDB.ExecDB(lsSQL);
                loDB.closeConnection();
                lbResult = true;
            }
            else
            {
                digioz.XML xml = new XML(coPortalSettings);
                xml.DeleteEntity(poConfiguration);
            }
            return lbResult;
        }
    }
}



