using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace ASPNetPortal {

    //*********************************************************************
    //
    // ModuleItem Class
    //
    // This class encapsulates the basic attributes of a Module, and is used
    // by the administration pages when manipulating modules.  ModuleItem implements 
    // the IComparable interface so that an ArrayList of ModuleItems may be sorted
    // by ModuleOrder, using the ArrayList's Sort() method.
    //
    //*********************************************************************

    public class ModuleItem : IComparable {

        private int      _moduleOrder;
        private String   _title;
        private String   _pane;
        private int      _id;
        private int      _defId;

        public int ModuleOrder {

            get {
                return _moduleOrder;
            }
            set {
                _moduleOrder = value;
            }
        }    

        public String ModuleTitle {

            get {
                return _title;
            }
            set {
                _title = value;
            }
        }

        public String PaneName {

            get {
                return _pane;
            }
            set {
                _pane = value;
            }
        }
        
        public int ModuleId {

            get {
                return _id;
            }
            set {
                _id = value;
            }
        }  
  
        public int ModuleDefId {

            get {
                return _defId;
            }
            set {
                _defId = value;
            }
        } 
   
        public int CompareTo(object value) {

            if (value == null) return 1;

            int compareOrder = ((ModuleItem)value).ModuleOrder;
            
            if (this.ModuleOrder == compareOrder) return 0;
            if (this.ModuleOrder < compareOrder) return -1;
            if (this.ModuleOrder > compareOrder) return 1;
            return 0;
        }
    }
    
    //*********************************************************************
    //
    // TabItem Class
    //
    // This class encapsulates the basic attributes of a Tab, and is used
    // by the administration pages when manipulating tabs.  TabItem implements 
    // the IComparable interface so that an ArrayList of TabItems may be sorted
    // by TabOrder, using the ArrayList's Sort() method.
    //
    //*********************************************************************

    public class TabItem : IComparable {

        private int      _tabOrder;
        private String   _name;
        private int      _id;

        public int TabOrder {

            get {
                return _tabOrder;
            }
            set {
                _tabOrder = value;
            }
        }    

        public String TabName {

            get {
                return _name;
            }
            set {
                _name = value;
            }
        }

        public int TabId {

            get {
                return _id;
            }
            set {
                _id = value;
            }
        }  
  
        public int CompareTo(object value) {

            if (value == null) return 1;

            int compareOrder = ((TabItem)value).TabOrder;
            
            if (this.TabOrder == compareOrder) return 0;
            if (this.TabOrder < compareOrder) return -1;
            if (this.TabOrder > compareOrder) return 1;
            return 0;
        }
    }
    
    //*********************************************************************
    //
    // AdminDB Class
    //
    // Class that encapsulates all data logic necessary to add/query/delete
    // configuration, layout and security settings values within the Portal database.
    //
    //*********************************************************************

    public class AdminDB {

        //
        // ROLES
        //

        //*********************************************************************
        //
        // GetPortalRoles() Method 
        //
        // The GetPortalRoles method returns a list of all role names for the 
        // specified portal.
        //
        // Other relevant sources:
        //     + GetPortalRoles Stored Procedure
        //
        //*********************************************************************

        public SqlDataReader GetPortalRoles(int portalId) {

            // Create Instance of Connection and Command Object
            SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
            SqlCommand myCommand = new SqlCommand("GetPortalRoles", myConnection);

            // Mark the Command as a SPROC
            myCommand.CommandType = CommandType.StoredProcedure;

            // Add Parameters to SPROC
            SqlParameter parameterPortalID = new SqlParameter("@PortalID", SqlDbType.Int, 4);
            parameterPortalID.Value = portalId;
            myCommand.Parameters.Add(parameterPortalID);

            // Open the database connection and execute the command
            myConnection.Open();
            SqlDataReader dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection);

            // Return the datareader
            return dr;
        }

        //*********************************************************************
        //
        // AddRole() Method 
        //
        // The AddRole method creates a new security role for the specified portal,
        // and returns the new RoleID value.
        //
        // Other relevant sources:
        //     + AddRole Stored Procedure
        //
        //*********************************************************************

        public int AddRole(int portalId, String roleName) {

            // Create Instance of Connection and Command Object
            SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
            SqlCommand myCommand = new SqlCommand("AddRole", myConnection);

            // Mark the Command as a SPROC
            myCommand.CommandType = CommandType.StoredProcedure;

            // Add Parameters to SPROC
            SqlParameter parameterPortalID = new SqlParameter("@PortalID", SqlDbType.Int, 4);
            parameterPortalID.Value = portalId;
            myCommand.Parameters.Add(parameterPortalID);

            SqlParameter parameterRoleName = new SqlParameter("@RoleName", SqlDbType.NVarChar, 50);
            parameterRoleName.Value = roleName;
            myCommand.Parameters.Add(parameterRoleName);

            SqlParameter parameterRoleID = new SqlParameter("@RoleID", SqlDbType.Int, 4);
            parameterRoleID.Direction = ParameterDirection.Output;
            myCommand.Parameters.Add(parameterRoleID);

            // Open the database connection and execute the command
            myConnection.Open();
            myCommand.ExecuteNonQuery();
            myConnection.Close();

            // return the role id 
            return (int) parameterRoleID.Value;
        }

        //*********************************************************************
        //
        // DeleteRole() Method 
        //
        // The DeleteRole deletes the specified role from the portal database.
        //
        // Other relevant sources:
        //     + DeleteRole Stored Procedure
        //
        //*********************************************************************

        public void DeleteRole(int roleId) {

            // Create Instance of Connection and Command Object
            SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
            SqlCommand myCommand = new SqlCommand("DeleteRole", myConnection);

            // Mark the Command as a SPROC
            myCommand.CommandType = CommandType.StoredProcedure;

            // Add Parameters to SPROC
            SqlParameter parameterRoleID = new SqlParameter("@RoleID", SqlDbType.Int, 4);
            parameterRoleID.Value = roleId;
            myCommand.Parameters.Add(parameterRoleID);

            // Open the database connection and execute the command
            myConnection.Open();
            myCommand.ExecuteNonQuery();
            myConnection.Close();
        }
       
        //*********************************************************************
        //
        // UpdateRole() Method 
        //
        // The UpdateRole method updates the friendly name of the specified role.
        //
        // Other relevant sources:
        //     + UpdateRole Stored Procedure
        //
        //*********************************************************************

        public void UpdateRole(int roleId, String roleName) {

            // Create Instance of Connection and Command Object
            SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
            SqlCommand myCommand = new SqlCommand("UpdateRole", myConnection);

            // Mark the Command as a SPROC
            myCommand.CommandType = CommandType.StoredProcedure;

            // Add Parameters to SPROC
            SqlParameter parameterRoleID = new SqlParameter("@RoleID", SqlDbType.Int, 4);
            parameterRoleID.Value = roleId;
            myCommand.Parameters.Add(parameterRoleID);

            SqlParameter parameterRoleName = new SqlParameter("@RoleName", SqlDbType.NVarChar, 50);
            parameterRoleName.Value = roleName;
            myCommand.Parameters.Add(parameterRoleName);

            // Open the database connection and execute the command
            myConnection.Open();
            myCommand.ExecuteNonQuery();
            myConnection.Close();
        }

        
        //
        // USER ROLES
        //

        //*********************************************************************
        //
        // GetRoleMembers() Method 
        //
        // The GetRoleMembers method returns a list of all members in the specified
        // security role.
        //
        // Other relevant sources:
        //     + GetRoleMembers Stored Procedure
        //
        //*********************************************************************

        public SqlDataReader GetRoleMembers(int roleId) {

            // Create Instance of Connection and Command Object
            SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
            SqlCommand myCommand = new SqlCommand("GetRoleMembership", myConnection);

            // Mark the Command as a SPROC
            myCommand.CommandType = CommandType.StoredProcedure;

            SqlParameter parameterRoleID = new SqlParameter("@RoleID", SqlDbType.Int, 4);
            parameterRoleID.Value = roleId;
            myCommand.Parameters.Add(parameterRoleID);

            // Open the database connection and execute the command
            myConnection.Open();
            SqlDataReader dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection);

            // Return the datareader
            return dr;
        }

        //*********************************************************************
        //
        // AddUserRole() Method 
        //
        // The AddUserRole method adds the user to the specified security role.
        //
        // Other relevant sources:
        //     + AddUserRole Stored Procedure
        //
        //*********************************************************************

        public void AddUserRole(int roleId, int userId) {

            // Create Instance of Connection and Command Object
            SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
            SqlCommand myCommand = new SqlCommand("AddUserRole", myConnection);

            // Mark the Command as a SPROC
            myCommand.CommandType = CommandType.StoredProcedure;

            // Add Parameters to SPROC
            SqlParameter parameterRoleID = new SqlParameter("@RoleID", SqlDbType.Int, 4);
            parameterRoleID.Value = roleId;
            myCommand.Parameters.Add(parameterRoleID);

            SqlParameter parameterUserID = new SqlParameter("@UserID", SqlDbType.Int, 4);
            parameterUserID.Value = userId;
            myCommand.Parameters.Add(parameterUserID);

            // Open the database connection and execute the command
            myConnection.Open();
            myCommand.ExecuteNonQuery();
            myConnection.Close();
        }

        //*********************************************************************
        //
        // DeleteUserRole() Method 
        //
        // The DeleteUserRole method deletes the user from the specified role.
        //
        // Other relevant sources:
        //     + DeleteUserRole Stored Procedure
        //
        //*********************************************************************

        public void DeleteUserRole(int roleId, int userId) {

            // Create Instance of Connection and Command Object
            SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
            SqlCommand myCommand = new SqlCommand("DeleteUserRole", myConnection);

            // Mark the Command as a SPROC
            myCommand.CommandType = CommandType.StoredProcedure;

            // Add Parameters to SPROC
            SqlParameter parameterRoleID = new SqlParameter("@RoleID", SqlDbType.Int, 4);
            parameterRoleID.Value = roleId;
            myCommand.Parameters.Add(parameterRoleID);

            SqlParameter parameterUserID = new SqlParameter("@UserID", SqlDbType.Int, 4);
            parameterUserID.Value = userId;
            myCommand.Parameters.Add(parameterUserID);

            // Open the database connection and execute the command
            myConnection.Open();
            myCommand.ExecuteNonQuery();
            myConnection.Close();
        }
       
        
        //
        // USERS
        //

        //*********************************************************************
        //
        // GetUsers() Method 
        //
        // The GetUsers method returns returns the UserID, Name and Email for 
        // all registered users.
        //
        // Other relevant sources:
        //     + GetUsers Stored Procedure
        //
        //*********************************************************************

        public SqlDataReader GetUsers() {

            // Create Instance of Connection and Command Object
            SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
            SqlCommand myCommand = new SqlCommand("GetUsers", myConnection);

            // Mark the Command as a SPROC
            myCommand.CommandType = CommandType.StoredProcedure;

            // Open the database connection and execute the command
            myConnection.Open();
            SqlDataReader dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection);

            // Return the datareader
            return dr;
        }


        //
        // PORTAL
        //

        //*********************************************************************
        //
        // UpdatePortalInfo() Method 
        //
        // The UpdatePortalInfo method updates the name and access settings for the portal.
        //
        // Other relevant sources:
        //     + UpdatePortalInfo Stored Procedure
        //
        //*********************************************************************

        public void UpdatePortalInfo (int portalId, String portalName, bool alwaysShow) {

            // Create Instance of Connection and Command Object
            SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
            SqlCommand myCommand = new SqlCommand("UpdatePortalInfo", myConnection);

            // Mark the Command as a SPROC
            myCommand.CommandType = CommandType.StoredProcedure;

            // Add Parameters to SPROC
            SqlParameter parameterPortalId = new SqlParameter("@PortalId", SqlDbType.Int, 4);
            parameterPortalId.Value = portalId;
            myCommand.Parameters.Add(parameterPortalId);

            SqlParameter parameterPortalName = new SqlParameter("@PortalName", SqlDbType.NVarChar, 128);
            parameterPortalName.Value = portalName;
            myCommand.Parameters.Add(parameterPortalName);

            SqlParameter parameterAlwaysShow = new SqlParameter("@AlwaysShowEditButton", SqlDbType.Bit, 1);
            parameterAlwaysShow.Value = alwaysShow;
            myCommand.Parameters.Add(parameterAlwaysShow);

            myConnection.Open();
            myCommand.ExecuteNonQuery();
            myConnection.Close();
        }
        
        //
        // TABS
        //

        //*********************************************************************
        //
        // AddTab Method
        //
        // The AddTab method adds a new tab to the portal.
        //
        // Other relevant sources:
        //     + AddTab Stored Procedure
        //
        //*********************************************************************

        public int AddTab (int portalId, String tabName, int tabOrder) {

            // Create Instance of Connection and Command Object
            SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
            SqlCommand myCommand = new SqlCommand("AddTab", myConnection);

            // Mark the Command as a SPROC
            myCommand.CommandType = CommandType.StoredProcedure;

            // Add Parameters to SPROC
            SqlParameter parameterPortalId = new SqlParameter("@PortalId", SqlDbType.Int, 4);
            parameterPortalId.Value = portalId;
            myCommand.Parameters.Add(parameterPortalId);

            SqlParameter parameterTabName = new SqlParameter("@TabName", SqlDbType.NVarChar, 50);
            parameterTabName.Value = tabName;
            myCommand.Parameters.Add(parameterTabName);

            SqlParameter parameterTabOrder = new SqlParameter("@TabOrder", SqlDbType.Int, 4);
            parameterTabOrder.Value = tabOrder;
            myCommand.Parameters.Add(parameterTabOrder);

            SqlParameter parameterAuthRoles = new SqlParameter("@AuthorizedRoles", SqlDbType.NVarChar, 256);
            parameterAuthRoles.Value = "All Users";
            myCommand.Parameters.Add(parameterAuthRoles);

            SqlParameter parameterMobileTabName = new SqlParameter("@MobileTabName", SqlDbType.NVarChar, 50);
            parameterMobileTabName.Value = "";
            myCommand.Parameters.Add(parameterMobileTabName);

            SqlParameter parameterTabId = new SqlParameter("@TabId", SqlDbType.Int, 4);
            parameterTabId.Direction = ParameterDirection.Output;
            myCommand.Parameters.Add(parameterTabId);

            myConnection.Open();
            myCommand.ExecuteNonQuery();
            myConnection.Close();

            return (int) parameterTabId.Value;
        }        
        
        
        //*********************************************************************
        //
        // UpdateTab Method
        //
        // The UpdateTab method updates the settings for the specified tab.
        //
        // Other relevant sources:
        //     + UpdateTab Stored Procedure
        //
        //*********************************************************************

        public void UpdateTab (int portalId, int tabId, String tabName, int tabOrder, String authorizedRoles, String mobileTabName, bool showMobile) {

            // Create Instance of Connection and Command Object
            SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
            SqlCommand myCommand = new SqlCommand("UpdateTab", myConnection);

            // Mark the Command as a SPROC
            myCommand.CommandType = CommandType.StoredProcedure;

            // Add Parameters to SPROC
            SqlParameter parameterPortalId = new SqlParameter("@PortalId", SqlDbType.Int, 4);
            parameterPortalId.Value = portalId;
            myCommand.Parameters.Add(parameterPortalId);
            
            SqlParameter parameterTabId = new SqlParameter("@TabId", SqlDbType.Int, 4);
            parameterTabId.Value = tabId;
            myCommand.Parameters.Add(parameterTabId);

            SqlParameter parameterTabName = new SqlParameter("@TabName", SqlDbType.NVarChar, 50);
            parameterTabName.Value = tabName;
            myCommand.Parameters.Add(parameterTabName);

            SqlParameter parameterTabOrder = new SqlParameter("@TabOrder", SqlDbType.Int, 4);
            parameterTabOrder.Value = tabOrder;
            myCommand.Parameters.Add(parameterTabOrder);

            SqlParameter parameterAuthRoles = new SqlParameter("@AuthorizedRoles", SqlDbType.NVarChar, 256);
            parameterAuthRoles.Value = authorizedRoles;
            myCommand.Parameters.Add(parameterAuthRoles);

            SqlParameter parameterMobileTabName = new SqlParameter("@MobileTabName", SqlDbType.NVarChar, 50);
            parameterMobileTabName.Value = mobileTabName;
            myCommand.Parameters.Add(parameterMobileTabName);

            SqlParameter parameterShowMobile = new SqlParameter("@ShowMobile", SqlDbType.Bit, 1);
            parameterShowMobile.Value = showMobile;
            myCommand.Parameters.Add(parameterShowMobile);

            myConnection.Open();
            myCommand.ExecuteNonQuery();
            myConnection.Close();
        }        
        
        //*********************************************************************
        //
        // UpdateTabOrder Method
        //
        // The UpdateTabOrder method changes the position of the tab with respect
        // to other tabs in the portal.
        //
        // Other relevant sources:
        //     + UpdateTabOrder Stored Procedure
        //
        //*********************************************************************

        public void UpdateTabOrder (int tabId, int tabOrder) {

            // Create Instance of Connection and Command Object
            SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
            SqlCommand myCommand = new SqlCommand("UpdateTabOrder", myConnection);

            // Mark the Command as a SPROC
            myCommand.CommandType = CommandType.StoredProcedure;

            // Add Parameters to SPROC
            SqlParameter parameterTabId = new SqlParameter("@TabId", SqlDbType.Int, 4);
            parameterTabId.Value = tabId;
            myCommand.Parameters.Add(parameterTabId);

            SqlParameter parameterTabOrder = new SqlParameter("@TabOrder", SqlDbType.Int, 4);
            parameterTabOrder.Value = tabOrder;
            myCommand.Parameters.Add(parameterTabOrder);

            myConnection.Open();
            myCommand.ExecuteNonQuery();
            myConnection.Close();
        }
        
        //*********************************************************************
        //
        // DeleteTab() Method 
        //
        // The DeleteTab method deletes the selected tab from the portal.
        //
        // Other relevant sources:
        //     + DeleteTab Stored Procedure
        //
        //*********************************************************************

        public void DeleteTab(int tabId) {

            // Create Instance of Connection and Command Object
            SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
            SqlCommand myCommand = new SqlCommand("DeleteTab", myConnection);

            // Mark the Command as a SPROC
            myCommand.CommandType = CommandType.StoredProcedure;

            // Add Parameters to SPROC
            SqlParameter parameterTabID = new SqlParameter("@TabID", SqlDbType.Int, 4);
            parameterTabID.Value = tabId;
            myCommand.Parameters.Add(parameterTabID);

            // Open the database connection and execute the command
            myConnection.Open();
            myCommand.ExecuteNonQuery();
            myConnection.Close();
        }
       
        //
        // MODULES
        //

        //*********************************************************************
        //
        // UpdateModuleOrder Method
        //
        // The AddUserRole method adds the user to the specified security role.
        //
        // Other relevant sources:
        //     + UpdateModuleOrder Stored Procedure
        //
        //*********************************************************************

        public void UpdateModuleOrder (int ModuleId, int ModuleOrder, String pane) {

            // Create Instance of Connection and Command Object
            SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
            SqlCommand myCommand = new SqlCommand("UpdateModuleOrder", myConnection);

            // Mark the Command as a SPROC
            myCommand.CommandType = CommandType.StoredProcedure;

            // Add Parameters to SPROC
            SqlParameter parameterModuleId = new SqlParameter("@ModuleId", SqlDbType.Int, 4);
            parameterModuleId.Value = ModuleId;
            myCommand.Parameters.Add(parameterModuleId);

            SqlParameter parameterModuleOrder = new SqlParameter("@ModuleOrder", SqlDbType.Int, 4);
            parameterModuleOrder.Value = ModuleOrder;
            myCommand.Parameters.Add(parameterModuleOrder);

            SqlParameter parameterPaneName = new SqlParameter("@PaneName", SqlDbType.NVarChar, 256);
            parameterPaneName.Value = pane;
            myCommand.Parameters.Add(parameterPaneName);

            myConnection.Open();
            myCommand.ExecuteNonQuery();
            myConnection.Close();
        }
        
        
        //*********************************************************************
        //
        // AddModule Method
        //
        // The AddModule method updates a specified Module within
        // the Modules database table.  If the module does not yet exist,
        // the stored procedure adds it.
        //
        // Other relevant sources:
        //     + AddModule Stored Procedure
        //
        //*********************************************************************

        public int AddModule(int tabId, int moduleOrder, String paneName, String title, int moduleDefId, int cacheTime, String editRoles, bool showMobile) {

            // Create Instance of Connection and Command Object
            SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
            SqlCommand myCommand = new SqlCommand("AddModule", myConnection);

            // Mark the Command as a SPROC
            myCommand.CommandType = CommandType.StoredProcedure;

            // Add Parameters to SPROC
            SqlParameter parameterModuleId = new SqlParameter("@ModuleId", SqlDbType.Int, 4);
            parameterModuleId.Direction = ParameterDirection.Output;
            myCommand.Parameters.Add(parameterModuleId);

            SqlParameter parameterModuleDefinitionId = new SqlParameter("@ModuleDefId", SqlDbType.Int, 4);
            parameterModuleDefinitionId.Value = moduleDefId;
            myCommand.Parameters.Add(parameterModuleDefinitionId);

            SqlParameter parameterTabId = new SqlParameter("@TabId", SqlDbType.Int, 4);
            parameterTabId.Value = tabId;
            myCommand.Parameters.Add(parameterTabId);

            SqlParameter parameterModuleOrder = new SqlParameter("@ModuleOrder", SqlDbType.Int, 4);
            parameterModuleOrder.Value = moduleOrder;
            myCommand.Parameters.Add(parameterModuleOrder);

            SqlParameter parameterTitle = new SqlParameter("@ModuleTitle", SqlDbType.NVarChar, 256);
            parameterTitle.Value = title;
            myCommand.Parameters.Add(parameterTitle);

            SqlParameter parameterPaneName = new SqlParameter("@PaneName", SqlDbType.NVarChar, 256);
            parameterPaneName.Value = paneName;
            myCommand.Parameters.Add(parameterPaneName);

            SqlParameter parameterCacheTime = new SqlParameter("@CacheTime", SqlDbType.Int, 4);
            parameterCacheTime.Value = cacheTime;
            myCommand.Parameters.Add(parameterCacheTime);

            SqlParameter parameterEditRoles = new SqlParameter("@EditRoles", SqlDbType.NVarChar, 256);
            parameterEditRoles.Value = editRoles;
            myCommand.Parameters.Add(parameterEditRoles);

            SqlParameter parameterShowMobile = new SqlParameter("@ShowMobile", SqlDbType.Bit, 1);
            parameterShowMobile.Value = showMobile;
            myCommand.Parameters.Add(parameterShowMobile);

            myConnection.Open();
            myCommand.ExecuteNonQuery();
            myConnection.Close();

            return (int) parameterModuleId.Value;
        }

        
        //*********************************************************************
        //
        // UpdateModule Method
        //
        // The UpdateModule method updates a specified Module within
        // the Modules database table.  If the module does not yet exist,
        // the stored procedure adds it.
        //
        // Other relevant sources:
        //     + UpdateModule Stored Procedure
        //
        //*********************************************************************

        public int UpdateModule(int moduleId, int moduleOrder, String paneName, String title, int cacheTime, String editRoles, bool showMobile) {

            // Create Instance of Connection and Command Object
            SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
            SqlCommand myCommand = new SqlCommand("UpdateModule", myConnection);

            // Mark the Command as a SPROC
            myCommand.CommandType = CommandType.StoredProcedure;

            // Add Parameters to SPROC
            SqlParameter parameterModuleId = new SqlParameter("@ModuleID", SqlDbType.Int, 4);
            parameterModuleId.Value = moduleId;
            myCommand.Parameters.Add(parameterModuleId);

            SqlParameter parameterModuleOrder = new SqlParameter("@ModuleOrder", SqlDbType.Int, 4);
            parameterModuleOrder.Value = moduleOrder;
            myCommand.Parameters.Add(parameterModuleOrder);

            SqlParameter parameterTitle = new SqlParameter("@ModuleTitle", SqlDbType.NVarChar, 256);
            parameterTitle.Value = title;
            myCommand.Parameters.Add(parameterTitle);

            SqlParameter parameterPaneName = new SqlParameter("@PaneName", SqlDbType.NVarChar, 256);
            parameterPaneName.Value = paneName;
            myCommand.Parameters.Add(parameterPaneName);

            SqlParameter parameterCacheTime = new SqlParameter("@CacheTime", SqlDbType.Int, 4);
            parameterCacheTime.Value = cacheTime;
            myCommand.Parameters.Add(parameterCacheTime);

            SqlParameter parameterEditRoles = new SqlParameter("@EditRoles", SqlDbType.NVarChar, 256);
            parameterEditRoles.Value = editRoles;
            myCommand.Parameters.Add(parameterEditRoles);

            SqlParameter parameterShowMobile = new SqlParameter("@ShowMobile", SqlDbType.Bit, 1);
            parameterShowMobile.Value = showMobile;
            myCommand.Parameters.Add(parameterShowMobile);

            myConnection.Open();
            myCommand.ExecuteNonQuery();
            myConnection.Close();

            return (int) parameterModuleId.Value;
        }

        //*********************************************************************
        //
        // DeleteModule Method
        //
        // The DeleteModule method deletes a specified Module from
        // the Modules database table.
        //
        // Other relevant sources:
        //     + DeleteModule Stored Procedure
        //
        //*********************************************************************

        public void DeleteModule(int moduleId) {

            // Create Instance of Connection and Command Object
            SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
            SqlCommand myCommand = new SqlCommand("DeleteModule", myConnection);

            // Mark the Command as a SPROC
            myCommand.CommandType = CommandType.StoredProcedure;

            // Add Parameters to SPROC
            SqlParameter parameterModuleId = new SqlParameter("@ModuleID", SqlDbType.Int, 4);
            parameterModuleId.Value = moduleId;
            myCommand.Parameters.Add(parameterModuleId);

            myConnection.Open();
            myCommand.ExecuteNonQuery();
            myConnection.Close();
        }


        //*********************************************************************
        //
        // UpdateModuleSetting Method
        //
        // The UpdateModuleSetting Method updates a single module setting 
        // in the ModuleSettings database table.
        //
        //*********************************************************************

        public void UpdateModuleSetting(int moduleId, String key, String value) {

            // Create Instance of Connection and Command Object
            SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
            SqlCommand myCommand = new SqlCommand("UpdateModuleSetting", myConnection);

            // Mark the Command as a SPROC
            myCommand.CommandType = CommandType.StoredProcedure;

            // Add Parameters to SPROC
            SqlParameter parameterModuleId = new SqlParameter("@ModuleID", SqlDbType.Int, 4);
            parameterModuleId.Value = moduleId;
            myCommand.Parameters.Add(parameterModuleId);

            SqlParameter parameterKey = new SqlParameter("@SettingName", SqlDbType.NVarChar, 50);
            parameterKey.Value = key;
            myCommand.Parameters.Add(parameterKey);
            
            SqlParameter parameterValue = new SqlParameter("@SettingValue", SqlDbType.NVarChar, 256);
            parameterValue.Value = value;
            myCommand.Parameters.Add(parameterValue);
            
            // Execute the command
            myConnection.Open();
            myCommand.ExecuteNonQuery();
            myConnection.Close();

        }

        //
        // MODULE DEFINITIONS
        //

        //*********************************************************************
        //
        // GetModuleDefinitions() Method 
        //
        // The GetModuleDefinitions method returns a list of all module type 
        // definitions for the portal.
        //
        // Other relevant sources:
        //     + GetModuleDefinitions Stored Procedure
        //
        //*********************************************************************

        public SqlDataReader GetModuleDefinitions(int portalId) {

            // Create Instance of Connection and Command Object
            SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
            SqlCommand myCommand = new SqlCommand("GetModuleDefinitions", myConnection);

            // Mark the Command as a SPROC
            myCommand.CommandType = CommandType.StoredProcedure;

            // Add Parameters to SPROC
            SqlParameter parameterPortalId = new SqlParameter("@PortalId", SqlDbType.Int, 4);
            parameterPortalId.Value = portalId;
            myCommand.Parameters.Add(parameterPortalId);

            // Open the database connection and execute the command
            myConnection.Open();
            SqlDataReader dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection);

            // Return the datareader
            return dr;
        }

        //*********************************************************************
        //
        // AddModuleDefinition() Method 
        //
        // The AddModuleDefinition add the definition for a new module type
        // to the portal.
        //
        // Other relevant sources:
        //     + AddModuleDefinition Stored Procedure
        //
        //*********************************************************************

        public int AddModuleDefinition(int portalId, String name, String desktopSrc, String mobileSrc) {

            // Create Instance of Connection and Command Object
            SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
            SqlCommand myCommand = new SqlCommand("AddModuleDefinition", myConnection);

            // Mark the Command as a SPROC
            myCommand.CommandType = CommandType.StoredProcedure;

            // Add Parameters to SPROC
            SqlParameter parameterPortalID = new SqlParameter("@PortalID", SqlDbType.Int, 4);
            parameterPortalID.Value = portalId;
            myCommand.Parameters.Add(parameterPortalID);

            SqlParameter parameterFriendlyName = new SqlParameter("@FriendlyName", SqlDbType.NVarChar, 128);
            parameterFriendlyName.Value = name;
            myCommand.Parameters.Add(parameterFriendlyName);

            SqlParameter parameterDesktopSrc = new SqlParameter("@DesktopSrc", SqlDbType.NVarChar, 256);
            parameterDesktopSrc.Value = desktopSrc;
            myCommand.Parameters.Add(parameterDesktopSrc);

            SqlParameter parameterMobileSrc = new SqlParameter("@MobileSrc", SqlDbType.NVarChar, 256);
            parameterMobileSrc.Value = mobileSrc;
            myCommand.Parameters.Add(parameterMobileSrc);

            SqlParameter parameterModuleDefID = new SqlParameter("@ModuleDefID", SqlDbType.Int, 4);
            parameterModuleDefID.Direction = ParameterDirection.Output;
            myCommand.Parameters.Add(parameterModuleDefID);

            // Open the database connection and execute the command
            myConnection.Open();
            myCommand.ExecuteNonQuery();
            myConnection.Close();

            // return the role id 
            return (int) parameterModuleDefID.Value;
        }

        //*********************************************************************
        //
        // DeleteModuleDefinition() Method 
        //
        // The DeleteModuleDefinition method deletes the specified module type 
        // definition from the portal.
        //
        // Other relevant sources:
        //     + DeleteModuleDefinition Stored Procedure
        //
        //*********************************************************************

        public void DeleteModuleDefinition(int defId) {

            // Create Instance of Connection and Command Object
            SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
            SqlCommand myCommand = new SqlCommand("DeleteModuleDefinition", myConnection);

            // Mark the Command as a SPROC
            myCommand.CommandType = CommandType.StoredProcedure;

            // Add Parameters to SPROC
            SqlParameter parameterModuleDefID = new SqlParameter("@ModuleDefID", SqlDbType.Int, 4);
            parameterModuleDefID.Value = defId;
            myCommand.Parameters.Add(parameterModuleDefID);

            // Open the database connection and execute the command
            myConnection.Open();
            myCommand.ExecuteNonQuery();
            myConnection.Close();
        }
       
        //*********************************************************************
        //
        // UpdateModuleDefinition() Method 
        //
        // The UpdateModuleDefinition method updates the settings for the 
        // specified module type definition.
        //
        // Other relevant sources:
        //     + UpdateModuleDefinition Stored Procedure
        //
        //*********************************************************************

        public void UpdateModuleDefinition(int defId, String name, String desktopSrc, String mobileSrc) {

            // Create Instance of Connection and Command Object
            SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
            SqlCommand myCommand = new SqlCommand("UpdateModuleDefinition", myConnection);

            // Mark the Command as a SPROC
            myCommand.CommandType = CommandType.StoredProcedure;

            // Add Parameters to SPROC
            SqlParameter parameterModuleDefID = new SqlParameter("@ModuleDefID", SqlDbType.Int, 4);
            parameterModuleDefID.Value = defId;
            myCommand.Parameters.Add(parameterModuleDefID);

            SqlParameter parameterFriendlyName = new SqlParameter("@FriendlyName", SqlDbType.NVarChar, 128);
            parameterFriendlyName.Value = name;
            myCommand.Parameters.Add(parameterFriendlyName);

            SqlParameter parameterDesktopSrc = new SqlParameter("@DesktopSrc", SqlDbType.NVarChar, 256);
            parameterDesktopSrc.Value = desktopSrc;
            myCommand.Parameters.Add(parameterDesktopSrc);

            SqlParameter parameterMobileSrc = new SqlParameter("@MobileSrc", SqlDbType.NVarChar, 256);
            parameterMobileSrc.Value = mobileSrc;
            myCommand.Parameters.Add(parameterMobileSrc);

            // Open the database connection and execute the command
            myConnection.Open();
            myCommand.ExecuteNonQuery();
            myConnection.Close();
        }

        //*********************************************************************
        //
        // GetSingleModuleDefinition Method
        //
        // The GetSingleModuleDefinition method returns a SqlDataReader containing details
        // about a specific module definition from the ModuleDefinitions table.
        //
        // Other relevant sources:
        //     + GetSingleModuleDefinition Stored Procedure
        //
        //*********************************************************************

        public SqlDataReader GetSingleModuleDefinition(int defId) {

            // Create Instance of Connection and Command Object
            SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
            SqlCommand myCommand = new SqlCommand("GetSingleModuleDefinition", myConnection);

            // Mark the Command as a SPROC
            myCommand.CommandType = CommandType.StoredProcedure;

            // Add Parameters to SPROC
            SqlParameter parameterModuleDefID = new SqlParameter("@ModuleDefID", SqlDbType.Int, 4);
            parameterModuleDefID.Value = defId;
            myCommand.Parameters.Add(parameterModuleDefID);

            // Execute the command
            myConnection.Open();
            SqlDataReader result = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            
            // Return the datareader 
            return result;
        }
    }
}