SessionManager.cs 6.58 KB
using System;
using System.Collections.Generic;
using ADAM.AIA50.BusinessLayer.Utility;
using System.Data;
using ADAM.AIAHTML5.API.Models;
namespace ADAM.AIA50.BusinessLayer.Utility
{
    //[RemotingService("Adam Manage User Service")]
    /// <summary>
    /// This class is main resposible to handle the session mechanism.
    /// This class need for handling the user state maintinence.
    /// </summary>
    public class SessionManager
    {
        // This is the static variable to have a singleton instance of the class.
        private static SessionManager _instance = null;

        /// <summary>
        /// private Constructor of the class to make class singleton.
        /// </summary>
        private SessionManager()
        {

        }

        /// <summary>
        /// This function use to add a session value as per the user id, license id, edition id and user session id 
        /// so that this can be check user is logged in or not.
        /// </summary>
        /// <param name="intUserID">user id</param>
        /// <param name="intLicenseID">license id</param>
        /// <param name="intEditionID">eidtion id</param>
        public void AddSession(int intUserID, int intLicenseID, byte byteEditionID)
        {
            // check if session is not created then create new
            string strSessionName = "ADAMSessionID";
            /* below if condition is commented as session check on culster envirnment 
             * can give be false
             * 
            if (FluorineFx.Context.FluorineContext.Current.Session[strSessionName] == null)
            {
                FluorineFx.Context.FluorineContext.Current.Session.Add(strSessionName, intUserID);
                string strSessionID = FluorineFx.Context.FluorineContext.Current.Session.SessionID;
                CacheManager.GetInstance().Add(strSessionID, intUserID, intLicenseID, byteEditionID);
            }
            */
            //FluorineFx.Context.FluorineContext.Current.Session.Add(strSessionName, intUserID);
            //string strSessionID = FluorineFx.Context.FluorineContext.Current.Session.SessionID;
            //CacheManager.GetInstance().Add(strSessionID, intUserID, intLicenseID, byteEditionID);
            
         }

        /// <summary>
        /// This function use to update the session login time
        /// </summary>
        /// <returns> number of rows updated ( Integer value )</returns>
        //public int UpdateSession()
        //{
            
        //     check session is not null the it will update that session id login time
        //    /* below if condition is commented as session check on culster envirnment 
        //     * can give be false
             
        //        string strSessionName = "ADAMSessionID";
        //        if (FluorineFx.Context.FluorineContext.Current.Session[strSessionName] != null)
        //        {
        //            string strSessionID = FluorineFx.Context.FluorineContext.Current.Session.SessionID;
        //            CacheManager.GetInstance().Update(strSessionID);
        //        }
        //     */
        //    string strSessionID = FluorineFx.Context.FluorineContext.Current.Session.SessionID;
        //    int rowUpdated = CacheManager.GetInstance().Update(strSessionID);
        //    return rowUpdated;
        //}

        /// <summary>
        /// This function use to delete a session id entry when user logged out from the system.
        /// </summary>
        public void DeleteSession()
        {
            string strSessionName = "ADAMSessionID";
            // check if session is not null then delete 
            /*  below if condition is commented as session check on culster envirnment 
             * can give be false
            if (FluorineFx.Context.FluorineContext.Current.Session[strSessionName] != null)
            {
                string strSessionID = FluorineFx.Context.FluorineContext.Current.Session.SessionID;

                CacheManager.GetInstance().Delete(strSessionID);
                FluorineFx.Context.FluorineContext.Current.Session.Remove(strSessionName);
            }
             */
            
            //string strSessionID = FluorineFx.Context.FluorineContext.Current.Session.SessionID;
            //CacheManager.GetInstance().Delete(strSessionID);
            //FluorineFx.Context.FluorineContext.Current.Session.Remove(strSessionName);
        }

        /// <summary>
        /// This function count the row from the sessionmanage table on the basis of License id and edition id
        /// </summary>
        /// <param name="intLicenseID">license id</param>
        /// <param name="intEditionID">edition id</param>
        /// <returns>number of row in the session table</returns>
        public int CountSession(int intLicenseID, byte byteEditionID)
        {
            
            DataTable dtblSessionManager = DBModel.GetSessionByLicenseEditionId(intLicenseID, byteEditionID);
            
            //Count only those logged since 20min or less
            //NOTE: GM Changed to 5 minutes because 20 is too long to wait to detect a browser crash
            //NOTE: this could be moved in the Sql Query for more speed
            DateTime currentDateMinutesAgo = DateTime.Now.AddMinutes(-20); // 11.45 =11:25
            int count = 0;
            for (int i = 0; i < dtblSessionManager.Rows.Count; i++)
            {
                DateTime loginTime =  DateTime.Parse(dtblSessionManager.Rows[i][2].ToString()); //LoginTime
                if (currentDateMinutesAgo <= loginTime) // 11:20/11:50
                    count++;
            }
            
            return count;
        }


        public void AddModSession(string exSessionName, string intValue)
        {
            // check if session is not created then create new
            string strSessionName = exSessionName;
           // FluorineFx.Context.FluorineContext.Current.Session.Add(strSessionName, intValue);
        }

        /// <summary>
        /// This function use to delete a session id entry when user logged out from the system.
        /// </summary>
        public void DeleteModSession(string exSessionName)
        {
            string strSessionName = exSessionName;
           // FluorineFx.Context.FluorineContext.Current.Session.Remove(strSessionName);
        }




        /// <summary>
        /// This method use to get the instance of this class
        /// </summary>
        /// <returns>instance on the same class</returns>
        public static SessionManager GetInstance()
        {
            if (_instance == null)
            {
                _instance = new SessionManager();
            }
            return _instance;
        }
    }
}