// ========================================================================
// 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
{
    /// 
    ///	Connection Generated by MyGeneration using the CSLA Object Mapping template
    /// 
    [Serializable()]
    [TypeConverter(typeof(ConnectionConverter))]
    public partial class Connection : BusinessBase, IDisposable, IVEHasBrokenRules
    {
        #region Log4Net
        private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        #endregion
        #region Refresh
        private List _RefreshConnections = new List();
        private List _RefreshConnectionFolders = new List();
        private void AddToRefreshList(List refreshConnections, List refreshConnectionFolders)
        {
            if (IsDirty)
                refreshConnections.Add(this);
            if (_ConnectionFolders != null && _ConnectionFolders.IsDirty)
            {
                foreach (ConnectionFolder tmp in _ConnectionFolders)
                {
                    if (tmp.IsDirty) refreshConnectionFolders.Add(tmp);
                }
            }
        }
        private void ClearRefreshList()
        {
            _RefreshConnections = new List();
            _RefreshConnectionFolders = new List();
        }
        private void BuildRefreshList()
        {
            ClearRefreshList();
            AddToRefreshList(_RefreshConnections, _RefreshConnectionFolders);
        }
        private void ProcessRefreshList()
        {
            foreach (Connection tmp in _RefreshConnections)
            {
                ConnectionInfo.Refresh(tmp);
            }
            foreach (ConnectionFolder tmp in _RefreshConnectionFolders)
            {
                FolderInfo.Refresh(tmp);
            }
            ClearRefreshList();
        }
        #endregion
        #region Collection
        private static List _CacheList = new List();
        protected static void AddToCache(Connection connection)
        {
            if (!_CacheList.Contains(connection)) _CacheList.Add(connection); // In AddToCache
        }
        protected static void RemoveFromCache(Connection connection)
        {
            while (_CacheList.Contains(connection)) _CacheList.Remove(connection); // In RemoveFromCache
        }
        private static Dictionary> _CacheByPrimaryKey = new Dictionary>();
        private static Dictionary> _CacheByName = new Dictionary>();
        private static void ConvertListToDictionary()
        {
            while (_CacheList.Count > 0) // Move Connection(s) from temporary _CacheList to _CacheByPrimaryKey
            {
                Connection tmp = _CacheList[0]; // Get the first Connection
                string pKey = tmp.DBID.ToString();
                if (!_CacheByPrimaryKey.ContainsKey(pKey))
                {
                    _CacheByPrimaryKey[pKey] = new List(); // Add new list for PrimaryKey
                    _CacheByName[tmp.Name.ToString()] = new List(); // Add new list for Name
                }
                _CacheByPrimaryKey[pKey].Add(tmp); // Add to Primary Key list
                _CacheByName[tmp.Name.ToString()].Add(tmp); // Unique Index
                _CacheList.RemoveAt(0); // Remove the first Connection
            }
        }
        protected static Connection GetCachedByPrimaryKey(int dbid)
        {
            ConvertListToDictionary();
            string key = dbid.ToString();
            if (_CacheByPrimaryKey.ContainsKey(key)) return _CacheByPrimaryKey[key][0];
            return null;
        }
        protected static Connection GetCachedByName(string name)
        {
            ConvertListToDictionary();
            string key = name.ToString();
            if (_CacheByName.ContainsKey(key)) return _CacheByName[key][0];
            return null;
        }
        #endregion
        #region Business Methods
        private string _ErrorMessage = string.Empty;
        public string ErrorMessage
        {
            get { return _ErrorMessage; }
        }
        private static int _nextDBID = -1;
        public static int NextDBID
        {
            get { return _nextDBID--; }
        }
        private int _DBID;
        [System.ComponentModel.DataObjectField(true, true)]
        public int DBID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _DBID;
            }
        }
        private string _Name = string.Empty;
        public string Name
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _Name;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (value == null) value = string.Empty;
                if (_Name != value)
                {
                    _Name = value;
                    PropertyHasChanged();
                }
            }
        }
        private string _Title = string.Empty;
        public string Title
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _Title;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (value == null) value = string.Empty;
                if (_Title != value)
                {
                    _Title = value;
                    PropertyHasChanged();
                }
            }
        }
        private string _ConnectionString = string.Empty;
        public string ConnectionString
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _ConnectionString;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (value == null) value = string.Empty;
                if (_ConnectionString != value)
                {
                    _ConnectionString = value;
                    PropertyHasChanged();
                }
            }
        }
        private int _ServerType;
        /// 
        /// 0 SQL Server
        /// 
        public int ServerType
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _ServerType;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (_ServerType != value)
                {
                    _ServerType = value;
                    PropertyHasChanged();
                }
            }
        }
        private string _Config = string.Empty;
        public string Config
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _Config;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (value == null) value = string.Empty;
                if (_Config != value)
                {
                    _Config = value;
                    PropertyHasChanged();
                }
            }
        }
        private DateTime _DTS = new DateTime();
        public DateTime DTS
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _DTS;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (_DTS != value)
                {
                    _DTS = value;
                    PropertyHasChanged();
                }
            }
        }
        private string _UsrID = string.Empty;
        public string UsrID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _UsrID;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (value == null) value = string.Empty;
                if (_UsrID != value)
                {
                    _UsrID = value;
                    PropertyHasChanged();
                }
            }
        }
        private byte[] _LastChanged = new byte[8];//timestamp
        private int _ConnectionFolderCount = 0;
        /// 
        /// Count of ConnectionFolders for this Connection
        /// 
        public int ConnectionFolderCount
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _ConnectionFolderCount;
            }
        }
        private ConnectionFolders _ConnectionFolders = null;
        /// 
        /// Related Field
        /// 
        [TypeConverter(typeof(ConnectionFoldersConverter))]
        public ConnectionFolders ConnectionFolders
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_ConnectionFolderCount < 0 || (_ConnectionFolderCount > 0 && _ConnectionFolders == null))
                    _ConnectionFolders = ConnectionFolders.GetByDBID(DBID);
                if (_ConnectionFolderCount < 0)
                    _ConnectionFolderCount = _ConnectionFolders == null ? 0 : _ConnectionFolders.Count;
                if (_ConnectionFolders == null)
                    _ConnectionFolders = ConnectionFolders.New();
                return _ConnectionFolders;
            }
        }
        public void Reset_ConnectionFolders()
        {
            _ConnectionFolderCount = -1;
        }
        public override bool IsDirty
        {
            get
            {
                if (base.IsDirty)
                    return true;
                return IsDirtyList(new List