// ========================================================================
// Copyright 2007 - Volian Enterprises, Inc. All rights reserved.          
// Volian Enterprises - Proprietary Information - DO NOT COPY OR DISTRIBUTE
// ------------------------------------------------------------------------
// $Workfile: $     $Revision: $                                           
// $Author: $   $Date: $                                                   
//                                                                         
// $History: $                                                             
// ========================================================================
using System;
using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Data;
using System.Configuration;
using System.IO;
using System.ComponentModel;
using System.Collections.Generic;
using Csla.Validation;
namespace VEPROMS.CSLA.Library
{
    /// 
    ///	Session Generated by MyGeneration using the CSLA Object Mapping template
    /// 
    [Serializable()]
    [TypeConverter(typeof(SessionConverter))]
    public partial class Session : BusinessBase, IDisposable, IVEHasBrokenRules
    {
        #region Log4Net
        private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        #endregion
        #region Refresh
        private List _RefreshSessions = new List();
        private void AddToRefreshList(List refreshSessions)
        {
            if (IsDirty)
                refreshSessions.Add(this);
        }
        private void ClearRefreshList()
        {
            _RefreshSessions = new List();
        }
        private void BuildRefreshList()
        {
            ClearRefreshList();
            AddToRefreshList(_RefreshSessions);
        }
        private void ProcessRefreshList()
        {
            foreach (Session tmp in _RefreshSessions)
            {
                SessionInfo.Refresh(tmp);
            }
            ClearRefreshList();
        }
        #endregion
        #region Collection
        private static List _CacheList = new List();
        protected static void AddToCache(Session session)
        {
            if (!_CacheList.Contains(session)) _CacheList.Add(session); // In AddToCache
        }
        protected static void RemoveFromCache(Session session)
        {
            while (_CacheList.Contains(session)) _CacheList.Remove(session); // In RemoveFromCache
        }
        private static Dictionary> _CacheByPrimaryKey = new Dictionary>();
        private static void ConvertListToDictionary()
        {
            while (_CacheList.Count > 0) // Move Session(s) from temporary _CacheList to _CacheByPrimaryKey
            {
                Session tmp = _CacheList[0]; // Get the first Session
                string pKey = tmp.SessionID.ToString();
                if (!_CacheByPrimaryKey.ContainsKey(pKey))
                {
                    _CacheByPrimaryKey[pKey] = new List(); // Add new list for PrimaryKey
                }
                _CacheByPrimaryKey[pKey].Add(tmp); // Add to Primary Key list
                _CacheList.RemoveAt(0); // Remove the first Session
            }
        }
        protected static Session GetCachedByPrimaryKey(int sessionID)
        {
            ConvertListToDictionary();
            string key = sessionID.ToString();
            if (_CacheByPrimaryKey.ContainsKey(key)) return _CacheByPrimaryKey[key][0];
            return null;
        }
        #endregion
        #region Business Methods
        private string _ErrorMessage = string.Empty;
        public string ErrorMessage
        {
            get { return _ErrorMessage; }
        }
        private static int _nextSessionID = -1;
        public static int NextSessionID
        {
            get { return _nextSessionID--; }
        }
        private int _SessionID;
        [System.ComponentModel.DataObjectField(true, true)]
        public int SessionID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _SessionID;
            }
        }
        private string _UserID = string.Empty;
        public string UserID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _UserID;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (value == null) value = string.Empty;
                if (_UserID != value)
                {
                    _UserID = value;
                    PropertyHasChanged();
                }
            }
        }
        private DateTime _DTSDtart = new DateTime();
        public DateTime DTSDtart
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _DTSDtart;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (_DTSDtart != value)
                {
                    _DTSDtart = value;
                    PropertyHasChanged();
                }
            }
        }
        private DateTime? _DTSEnd;
        public DateTime? DTSEnd
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _DTSEnd;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (_DTSEnd != value)
                {
                    _DTSEnd = value;
                    PropertyHasChanged();
                }
            }
        }
        private DateTime _DTSActivity = new DateTime();
        public DateTime DTSActivity
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _DTSActivity;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (_DTSActivity != value)
                {
                    _DTSActivity = value;
                    PropertyHasChanged();
                }
            }
        }
        private byte[] _LastChanged = new byte[8];//timestamp
        private string _MachineName = string.Empty;
        public string MachineName
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _MachineName;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (value == null) value = string.Empty;
                if (_MachineName != value)
                {
                    _MachineName = value;
                    PropertyHasChanged();
                }
            }
        }
        private int _ProcessID;
        public int ProcessID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _ProcessID;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (_ProcessID != value)
                {
                    _ProcessID = value;
                    PropertyHasChanged();
                }
            }
        }
        public override bool IsDirty
        {
            get { return base.IsDirty; }
        }
        public bool IsDirtyList(List