// ========================================================================
// 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
{
    /// 
    ///	Group Generated by MyGeneration using the CSLA Object Mapping template
    /// 
    [Serializable()]
    [TypeConverter(typeof(GroupConverter))]
    public partial class Group : BusinessBase, IDisposable, IVEHasBrokenRules
    {
        #region Log4Net
        private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        #endregion
        #region Refresh
        private List _RefreshGroups = new List();
        private List _RefreshGroupAssignments = new List();
        private List _RefreshGroupMemberships = new List();
        private void AddToRefreshList(List refreshGroups, List refreshGroupAssignments, List refreshGroupMemberships)
        {
            if (IsDirty)
                refreshGroups.Add(this);
            if (_GroupAssignments != null && _GroupAssignments.IsDirty)
            {
                foreach (GroupAssignment tmp in _GroupAssignments)
                {
                    if (tmp.IsDirty) refreshGroupAssignments.Add(tmp);
                }
            }
            if (_GroupMemberships != null && _GroupMemberships.IsDirty)
            {
                foreach (GroupMembership tmp in _GroupMemberships)
                {
                    if (tmp.IsDirty) refreshGroupMemberships.Add(tmp);
                }
            }
        }
        private void ClearRefreshList()
        {
            _RefreshGroups = new List();
            _RefreshGroupAssignments = new List();
            _RefreshGroupMemberships = new List();
        }
        private void BuildRefreshList()
        {
            ClearRefreshList();
            AddToRefreshList(_RefreshGroups, _RefreshGroupAssignments, _RefreshGroupMemberships);
        }
        private void ProcessRefreshList()
        {
            foreach (Group tmp in _RefreshGroups)
            {
                GroupInfo.Refresh(tmp);
            }
            foreach (GroupAssignment tmp in _RefreshGroupAssignments)
            {
                AssignmentInfo.Refresh(tmp);
            }
            foreach (GroupMembership tmp in _RefreshGroupMemberships)
            {
                MembershipInfo.Refresh(tmp);
            }
            ClearRefreshList();
        }
        #endregion
        #region Collection
        private static List _CacheList = new List();
        protected static void AddToCache(Group group)
        {
            if (!_CacheList.Contains(group)) _CacheList.Add(group); // In AddToCache
        }
        protected static void RemoveFromCache(Group group)
        {
            while (_CacheList.Contains(group)) _CacheList.Remove(group); // In RemoveFromCache
        }
        private static Dictionary> _CacheByPrimaryKey = new Dictionary>();
        private static Dictionary> _CacheByGroupName = new Dictionary>();
        private static void ConvertListToDictionary()
        {
            while (_CacheList.Count > 0) // Move Group(s) from temporary _CacheList to _CacheByPrimaryKey
            {
                Group tmp = _CacheList[0]; // Get the first Group
                string pKey = tmp.GID.ToString();
                if (!_CacheByPrimaryKey.ContainsKey(pKey))
                {
                    _CacheByPrimaryKey[pKey] = new List(); // Add new list for PrimaryKey
                    _CacheByGroupName[tmp.GroupName.ToString()] = new List(); // Add new list for GroupName
                }
                _CacheByPrimaryKey[pKey].Add(tmp); // Add to Primary Key list
                _CacheByGroupName[tmp.GroupName.ToString()].Add(tmp); // Unique Index
                _CacheList.RemoveAt(0); // Remove the first Group
            }
        }
        protected static Group GetCachedByPrimaryKey(int gid)
        {
            ConvertListToDictionary();
            string key = gid.ToString();
            if (_CacheByPrimaryKey.ContainsKey(key)) return _CacheByPrimaryKey[key][0];
            return null;
        }
        protected static Group GetCachedByGroupName(string groupName)
        {
            ConvertListToDictionary();
            string key = groupName.ToString();
            if (_CacheByGroupName.ContainsKey(key)) return _CacheByGroupName[key][0];
            return null;
        }
        #endregion
        #region Business Methods
        private string _ErrorMessage = string.Empty;
        public string ErrorMessage
        {
            get { return _ErrorMessage; }
        }
        private static int _nextGID = -1;
        public static int NextGID
        {
            get { return _nextGID--; }
        }
        private int _GID;
        [System.ComponentModel.DataObjectField(true, true)]
        public int GID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _GID;
            }
        }
        private string _GroupName = string.Empty;
        public string GroupName
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _GroupName;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (value == null) value = string.Empty;
                if (_GroupName != value)
                {
                    _GroupName = value;
                    PropertyHasChanged();
                }
            }
        }
        private int? _GroupType;
        public int? GroupType
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _GroupType;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (_GroupType != value)
                {
                    _GroupType = 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 _GroupAssignmentCount = 0;
        /// 
        /// Count of GroupAssignments for this Group
        /// 
        public int GroupAssignmentCount
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _GroupAssignmentCount;
            }
        }
        private GroupAssignments _GroupAssignments = null;
        /// 
        /// Related Field
        /// 
        [TypeConverter(typeof(GroupAssignmentsConverter))]
        public GroupAssignments GroupAssignments
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_GroupAssignmentCount < 0 || (_GroupAssignmentCount > 0 && _GroupAssignments == null))
                    _GroupAssignments = GroupAssignments.GetByGID(GID);
                if (_GroupAssignmentCount < 0)
                    _GroupAssignmentCount = _GroupAssignments == null ? 0 : _GroupAssignments.Count;
                if (_GroupAssignments == null)
                    _GroupAssignments = GroupAssignments.New();
                return _GroupAssignments;
            }
        }
        public void Reset_GroupAssignments()
        {
            _GroupAssignmentCount = -1;
        }
        private int _GroupMembershipCount = 0;
        /// 
        /// Count of GroupMemberships for this Group
        /// 
        public int GroupMembershipCount
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _GroupMembershipCount;
            }
        }
        private GroupMemberships _GroupMemberships = null;
        /// 
        /// Related Field
        /// 
        [TypeConverter(typeof(GroupMembershipsConverter))]
        public GroupMemberships GroupMemberships
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_GroupMembershipCount < 0 || (_GroupMembershipCount > 0 && _GroupMemberships == null))
                    _GroupMemberships = GroupMemberships.GetByGID(GID);
                if (_GroupMembershipCount < 0)
                    _GroupMembershipCount = _GroupMemberships == null ? 0 : _GroupMemberships.Count;
                if (_GroupMemberships == null)
                    _GroupMemberships = GroupMemberships.New();
                return _GroupMemberships;
            }
        }
        public void Reset_GroupMemberships()
        {
            _GroupMembershipCount = -1;
        }
        public override bool IsDirty
        {
            get
            {
                if (base.IsDirty)
                    return true;
                return IsDirtyList(new List