// ========================================================================
// 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;
namespace VEPROMS.CSLA.Library
{
    public delegate void RODbInfoEvent(object sender);
    /// 
    ///	RODbInfo Generated by MyGeneration using the CSLA Object Mapping template
    /// 
    [Serializable()]
    [TypeConverter(typeof(RODbInfoConverter))]
    public partial class RODbInfo : ReadOnlyBase, IDisposable
    {
        public event RODbInfoEvent Changed;
        private void OnChange()
        {
            if (Changed != null) Changed(this);
        }
        #region Log4Net
        private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        #endregion
        #region Collection
        private static List _CacheList = new List();
        protected static void AddToCache(RODbInfo rODbInfo)
        {
            if (!_CacheList.Contains(rODbInfo)) _CacheList.Add(rODbInfo); // In AddToCache
        }
        protected static void RemoveFromCache(RODbInfo rODbInfo)
        {
            while (_CacheList.Contains(rODbInfo)) _CacheList.Remove(rODbInfo); // In RemoveFromCache
        }
        private static Dictionary> _CacheByPrimaryKey = new Dictionary>();
        private static void ConvertListToDictionary()
        {
            while (_CacheList.Count > 0) // Move RODbInfo(s) from temporary _CacheList to _CacheByPrimaryKey
            {
                RODbInfo tmp = _CacheList[0]; // Get the first RODbInfo
                string pKey = tmp.RODbID.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 RODbInfo
            }
        }
        internal static void AddList(RODbInfoList lst)
        {
            foreach (RODbInfo item in lst) AddToCache(item);
        }
        protected static RODbInfo GetCachedByPrimaryKey(int rODbID)
        {
            ConvertListToDictionary();
            string key = rODbID.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; }
        }
        protected RODb _Editable;
        private IVEHasBrokenRules HasBrokenRules
        {
            get
            {
                IVEHasBrokenRules hasBrokenRules = null;
                if (_Editable != null)
                    hasBrokenRules = _Editable.HasBrokenRules;
                return hasBrokenRules;
            }
        }
        private int _RODbID;
        [System.ComponentModel.DataObjectField(true, true)]
        public int RODbID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _RODbID;
            }
        }
        private string _ROName = string.Empty;
        /// 
        /// Hook for future - to allow the user to select multiple RO Databases assocaiated with on DocVersion
        /// 
        public string ROName
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _ROName;
            }
        }
        private string _FolderPath = string.Empty;
        /// 
        /// Path to the RO database
        /// 
        public string FolderPath
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _FolderPath;
            }
        }
        private string _DBConnectionString = string.Empty;
        /// 
        /// Connection String - Default could just be the full path and name of the database
        /// 
        public string DBConnectionString
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _DBConnectionString;
            }
        }
        private string _Config = string.Empty;
        public string Config
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _Config;
            }
        }
        private DateTime _DTS = new DateTime();
        public DateTime DTS
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _DTS;
            }
        }
        private string _UserID = string.Empty;
        public string UserID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _UserID;
            }
        }
        private int _RODbDROUsageCount = 0;
        /// 
        /// Count of RODbDROUsages for this RODb
        /// 
        public int RODbDROUsageCount
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_RODbDROUsageCount < 0)
                    _RODbDROUsageCount = RODbDROUsages.Count;
                return _RODbDROUsageCount;
            }
        }
        private DROUsageInfoList _RODbDROUsages = null;
        [TypeConverter(typeof(DROUsageInfoListConverter))]
        public DROUsageInfoList RODbDROUsages
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_RODbDROUsageCount < 0 || (_RODbDROUsageCount > 0 && _RODbDROUsages == null))
                    _RODbDROUsages = DROUsageInfoList.GetByRODbID(_RODbID);
                if (_RODbDROUsageCount < 0)
                    _RODbDROUsageCount = _RODbDROUsages.Count;
                return _RODbDROUsages;
            }
        }
        public void RefreshRODbDROUsages()
        {
            _RODbDROUsageCount = -1;
            ConvertListToDictionary();
            if (_CacheByPrimaryKey.ContainsKey(_RODbID.ToString()))
                foreach (RODbInfo tmp in _CacheByPrimaryKey[_RODbID.ToString()])
                    tmp._RODbDROUsageCount = -1; // This will cause the data to be requeried
        }
        private int _RODbROFstCount = 0;
        /// 
        /// Count of RODbROFsts for this RODb
        /// 
        public int RODbROFstCount
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_RODbROFstCount < 0)
                    _RODbROFstCount = RODbROFsts.Count;
                return _RODbROFstCount;
            }
        }
        private ROFstInfoList _RODbROFsts = null;
        [TypeConverter(typeof(ROFstInfoListConverter))]
        public ROFstInfoList RODbROFsts
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_RODbROFstCount < 0 || (_RODbROFstCount > 0 && _RODbROFsts == null))
                    _RODbROFsts = ROFstInfoList.GetByRODbID(_RODbID);
                if (_RODbROFstCount < 0)
                    _RODbROFstCount = _RODbROFsts.Count;
                return _RODbROFsts;
            }
        }
        public void RefreshRODbROFsts()
        {
            _RODbROFstCount = -1;
            ConvertListToDictionary();
            if (_CacheByPrimaryKey.ContainsKey(_RODbID.ToString()))
                foreach (RODbInfo tmp in _CacheByPrimaryKey[_RODbID.ToString()])
                    tmp._RODbROFstCount = -1; // This will cause the data to be requeried
        }
        private int _RODbROImageCount = 0;
        /// 
        /// Count of RODbROImages for this RODb
        /// 
        public int RODbROImageCount
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_RODbROImageCount < 0)
                    _RODbROImageCount = RODbROImages.Count;
                return _RODbROImageCount;
            }
        }
        private ROImageInfoList _RODbROImages = null;
        [TypeConverter(typeof(ROImageInfoListConverter))]
        public ROImageInfoList RODbROImages
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_RODbROImageCount < 0 || (_RODbROImageCount > 0 && _RODbROImages == null))
                    _RODbROImages = ROImageInfoList.GetByRODbID(_RODbID);
                if (_RODbROImageCount < 0)
                    _RODbROImageCount = _RODbROImages.Count;
                return _RODbROImages;
            }
        }
        public void RefreshRODbROImages()
        {
            _RODbROImageCount = -1;
            ConvertListToDictionary();
            if (_CacheByPrimaryKey.ContainsKey(_RODbID.ToString()))
                foreach (RODbInfo tmp in _CacheByPrimaryKey[_RODbID.ToString()])
                    tmp._RODbROImageCount = -1; // This will cause the data to be requeried
        }
        private int _RODbRoUsageCount = 0;
        /// 
        /// Count of RODbRoUsages for this RODb
        /// 
        public int RODbRoUsageCount
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_RODbRoUsageCount < 0)
                    _RODbRoUsageCount = RODbRoUsages.Count;
                return _RODbRoUsageCount;
            }
        }
        private RoUsageInfoList _RODbRoUsages = null;
        [TypeConverter(typeof(RoUsageInfoListConverter))]
        public RoUsageInfoList RODbRoUsages
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_RODbRoUsageCount < 0 || (_RODbRoUsageCount > 0 && _RODbRoUsages == null))
                    _RODbRoUsages = RoUsageInfoList.GetByRODbID(_RODbID);
                if (_RODbRoUsageCount < 0)
                    _RODbRoUsageCount = _RODbRoUsages.Count;
                return _RODbRoUsages;
            }
        }
        public void RefreshRODbRoUsages()
        {
            _RODbRoUsageCount = -1;
            ConvertListToDictionary();
            if (_CacheByPrimaryKey.ContainsKey(_RODbID.ToString()))
                foreach (RODbInfo tmp in _CacheByPrimaryKey[_RODbID.ToString()])
                    tmp._RODbRoUsageCount = -1; // This will cause the data to be requeried
        }
        // CSLATODO: Replace base RODbInfo.ToString function as necessary
        /// 
        /// Overrides Base ToString
        /// 
        /// A string representation of current RODbInfo
        //public override string ToString()
        //{
        //  return base.ToString();
        //}
        // CSLATODO: Check RODbInfo.GetIdValue to assure that the ID returned is unique
        /// 
        /// Overrides Base GetIdValue - Used internally by CSLA to determine equality
        /// 
        /// A Unique ID for the current RODbInfo
        protected override object GetIdValue()
        {
            return MyRODbInfoUnique; // Absolutely Unique ID
        }
        #endregion
        #region Factory Methods
        private static int _RODbInfoUnique = 0;
        private static int RODbInfoUnique
        { get { return ++_RODbInfoUnique; } }
        private int _MyRODbInfoUnique = RODbInfoUnique;
        public int MyRODbInfoUnique // Absolutely Unique ID - Info
        { get { return _MyRODbInfoUnique; } }
        protected RODbInfo()
        {/* require use of factory methods */
            AddToCache(this);
        }
        private bool _Disposed = false;
        private static int _CountCreated = 0;
        private static int _CountDisposed = 0;
        private static int _CountFinalized = 0;
        private static int IncrementCountCreated
        { get { return ++_CountCreated; } }
        private int _CountWhenCreated = IncrementCountCreated;
        public static int CountCreated
        { get { return _CountCreated; } }
        public static int CountNotDisposed
        { get { return _CountCreated - _CountDisposed; } }
        public static int CountNotFinalized
        { get { return _CountCreated - _CountFinalized; } }
        ~RODbInfo()
        {
            _CountFinalized++;
        }
        public void Dispose()
        {
            if (_Disposed) return;
            _CountDisposed++;
            _Disposed = true;
            RemoveFromCache(this);
            if (!_CacheByPrimaryKey.ContainsKey(RODbID.ToString())) return;
            List listRODbInfo = _CacheByPrimaryKey[RODbID.ToString()]; // Get the list of items
            while (listRODbInfo.Contains(this)) listRODbInfo.Remove(this); // Remove the item from the list
            if (listRODbInfo.Count == 0) // If there are no items left in the list
                _CacheByPrimaryKey.Remove(RODbID.ToString()); // remove the list
        }
        public virtual RODb Get()
        {
            return _Editable = RODb.Get(_RODbID);
        }
        public static void Refresh(RODb tmp)
        {
            string key = tmp.RODbID.ToString();
            ConvertListToDictionary();
            if (_CacheByPrimaryKey.ContainsKey(key))
                foreach (RODbInfo tmpInfo in _CacheByPrimaryKey[key])
                    tmpInfo.RefreshFields(tmp);
        }
        protected virtual void RefreshFields(RODb tmp)
        {
            _ROName = tmp.ROName;
            _FolderPath = tmp.FolderPath;
            _DBConnectionString = tmp.DBConnectionString;
            _Config = tmp.Config;
            _DTS = tmp.DTS;
            _UserID = tmp.UserID;
            _RODbInfoExtension.Refresh(this);
            OnChange();// raise an event
        }
        public static RODbInfo Get(int rODbID)
        {
            //if (!CanGetObject())
            //  throw new System.Security.SecurityException("User not authorized to view a RODb");
            try
            {
                RODbInfo tmp = GetCachedByPrimaryKey(rODbID);
                if (tmp == null)
                {
                    tmp = DataPortal.Fetch(new PKCriteria(rODbID));
                    AddToCache(tmp);
                }
                if (tmp.ErrorMessage == "No Record Found")
                {
                    tmp.Dispose(); // Clean-up RODbInfo
                    tmp = null;
                }
                return tmp;
            }
            catch (Exception ex)
            {
                throw new DbCslaException("Error on RODbInfo.Get", ex);
            }
        }
        #endregion
        #region Data Access Portal
        internal RODbInfo(SafeDataReader dr)
        {
            if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] RODbInfo.Constructor", GetHashCode());
            try
            {
                ReadData(dr);
            }
            catch (Exception ex)
            {
                if (_MyLog.IsErrorEnabled) _MyLog.Error("RODbInfo.Constructor", ex);
                throw new DbCslaException("RODbInfo.Constructor", ex);
            }
        }
        [Serializable()]
        protected class PKCriteria
        {
            private int _RODbID;
            public int RODbID
            { get { return _RODbID; } }
            public PKCriteria(int rODbID)
            {
                _RODbID = rODbID;
            }
        }
        private void ReadData(SafeDataReader dr)
        {
            if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] RODbInfo.ReadData", GetHashCode());
            try
            {
                _RODbID = dr.GetInt32("RODbID");
                _ROName = dr.GetString("ROName");
                _FolderPath = dr.GetString("FolderPath");
                _DBConnectionString = dr.GetString("DBConnectionString");
                _Config = dr.GetString("Config");
                _DTS = dr.GetDateTime("DTS");
                _UserID = dr.GetString("UserID");
                _RODbDROUsageCount = dr.GetInt32("DROUsageCount");
                _RODbROFstCount = dr.GetInt32("ROFstCount");
                _RODbROImageCount = dr.GetInt32("ROImageCount");
                _RODbRoUsageCount = dr.GetInt32("RoUsageCount");
            }
            catch (Exception ex)
            {
                if (_MyLog.IsErrorEnabled) _MyLog.Error("RODbInfo.ReadData", ex);
                _ErrorMessage = ex.Message;
                throw new DbCslaException("RODbInfo.ReadData", ex);
            }
        }
        private void DataPortal_Fetch(PKCriteria criteria)
        {
            if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] RODbInfo.DataPortal_Fetch", GetHashCode());
            try
            {
                using (SqlConnection cn = Database.VEPROMS_SqlConnection)
                {
                    ApplicationContext.LocalContext["cn"] = cn;
                    using (SqlCommand cm = cn.CreateCommand())
                    {
                        cm.CommandType = CommandType.StoredProcedure;
                        cm.CommandText = "getRODb";
                        cm.Parameters.AddWithValue("@RODbID", criteria.RODbID);
                        cm.CommandTimeout = Database.DefaultTimeout;
                        using (SafeDataReader dr = new SafeDataReader(cm.ExecuteReader()))
                        {
                            if (!dr.Read())
                            {
                                _ErrorMessage = "No Record Found";
                                return;
                            }
                            ReadData(dr);
                        }
                    }
                    // removing of item only needed for local data portal
                    if (ApplicationContext.ExecutionLocation == ApplicationContext.ExecutionLocations.Client)
                        ApplicationContext.LocalContext.Remove("cn");
                }
            }
            catch (Exception ex)
            {
                if (_MyLog.IsErrorEnabled) _MyLog.Error("RODbInfo.DataPortal_Fetch", ex);
                _ErrorMessage = ex.Message;
                throw new DbCslaException("RODbInfo.DataPortal_Fetch", ex);
            }
        }
        #endregion
        // Standard Refresh
        #region extension
        RODbInfoExtension _RODbInfoExtension = new RODbInfoExtension();
        [Serializable()]
        partial class RODbInfoExtension : extensionBase { }
        [Serializable()]
        class extensionBase
        {
            // Default Refresh
            public virtual void Refresh(RODbInfo tmp) { }
        }
        #endregion
    } // Class
    #region Converter
    internal class RODbInfoConverter : ExpandableObjectConverter
    {
        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destType)
        {
            if (destType == typeof(string) && value is RODbInfo)
            {
                // Return the ToString value
                return ((RODbInfo)value).ToString();
            }
            return base.ConvertTo(context, culture, value, destType);
        }
    }
    #endregion
} // Namespace