// ========================================================================
// 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
{
    /// 
    ///	Association Generated by MyGeneration using the CSLA Object Mapping template
    /// 
    [Serializable()]
    [TypeConverter(typeof(AssociationConverter))]
    public partial class Association : BusinessBase, IDisposable, IVEHasBrokenRules
    {
        #region Log4Net
        private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        #endregion
        #region Refresh
        private List _RefreshAssociations = new List();
        private void AddToRefreshList(List refreshAssociations)
        {
            if (IsDirty)
                refreshAssociations.Add(this);
        }
        private void ClearRefreshList()
        {
            _RefreshAssociations = new List();
        }
        private void BuildRefreshList()
        {
            ClearRefreshList();
            AddToRefreshList(_RefreshAssociations);
        }
        private void ProcessRefreshList()
        {
            foreach (Association tmp in _RefreshAssociations)
            {
                AssociationInfo.Refresh(tmp);
                if (tmp._MyDocVersion != null) DocVersionInfo.Refresh(tmp._MyDocVersion);
                if (tmp._MyROFst != null) ROFstInfo.Refresh(tmp._MyROFst);
            }
            ClearRefreshList();
        }
        #endregion
        #region Collection
        private static List _CacheList = new List();
        protected static void AddToCache(Association association)
        {
            if (!_CacheList.Contains(association)) _CacheList.Add(association); // In AddToCache
        }
        protected static void RemoveFromCache(Association association)
        {
            while (_CacheList.Contains(association)) _CacheList.Remove(association); // In RemoveFromCache
        }
        private static Dictionary> _CacheByPrimaryKey = new Dictionary>();
        private static Dictionary> _CacheByVersionID_ROFstID = new Dictionary>();
        private static void ConvertListToDictionary()
        {
            while (_CacheList.Count > 0) // Move Association(s) from temporary _CacheList to _CacheByPrimaryKey
            {
                Association tmp = _CacheList[0]; // Get the first Association
                string pKey = tmp.AssociationID.ToString();
                if (!_CacheByPrimaryKey.ContainsKey(pKey))
                {
                    _CacheByPrimaryKey[pKey] = new List(); // Add new list for PrimaryKey
                    _CacheByVersionID_ROFstID[tmp.VersionID.ToString() + "_" + tmp.ROFstID.ToString()] = new List(); // Add new list for VersionID_ROFstID
                }
                _CacheByPrimaryKey[pKey].Add(tmp); // Add to Primary Key list
                _CacheByVersionID_ROFstID[tmp.VersionID.ToString() + "_" + tmp.ROFstID.ToString()].Add(tmp); // Unique Index
                _CacheList.RemoveAt(0); // Remove the first Association
            }
        }
        protected static Association GetCachedByPrimaryKey(int associationID)
        {
            ConvertListToDictionary();
            string key = associationID.ToString();
            if (_CacheByPrimaryKey.ContainsKey(key)) return _CacheByPrimaryKey[key][0];
            return null;
        }
        protected static Association GetCachedByVersionID_ROFstID(int versionID, int rOFstID)
        {
            ConvertListToDictionary();
            string key = versionID.ToString() + "_" + rOFstID.ToString();
            if (_CacheByVersionID_ROFstID.ContainsKey(key)) return _CacheByVersionID_ROFstID[key][0];
            return null;
        }
        #endregion
        #region Business Methods
        private string _ErrorMessage = string.Empty;
        public string ErrorMessage
        {
            get { return _ErrorMessage; }
        }
        private static int _nextAssociationID = -1;
        public static int NextAssociationID
        {
            get { return _nextAssociationID--; }
        }
        private int _AssociationID;
        [System.ComponentModel.DataObjectField(true, true)]
        public int AssociationID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _AssociationID;
            }
        }
        private int _VersionID;
        public int VersionID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_MyDocVersion != null) _VersionID = _MyDocVersion.VersionID;
                return _VersionID;
            }
        }
        private DocVersion _MyDocVersion;
        public DocVersion MyDocVersion
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_MyDocVersion == null && _VersionID != 0) _MyDocVersion = DocVersion.Get(_VersionID);
                return _MyDocVersion;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (_MyDocVersion != value)
                {
                    _MyDocVersion = value;
                    _VersionID = value.VersionID;// Update underlying data field
                    PropertyHasChanged();
                }
            }
        }
        private int _ROFstID;
        public int ROFstID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_MyROFst != null) _ROFstID = _MyROFst.ROFstID;
                return _ROFstID;
            }
        }
        private ROFst _MyROFst;
        public ROFst MyROFst
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_MyROFst == null && _ROFstID != 0) _MyROFst = ROFst.Get(_ROFstID);
                return _MyROFst;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (_MyROFst != value)
                {
                    _MyROFst = value;
                    _ROFstID = value.ROFstID;// Update underlying data field
                    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 _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 byte[] _LastChanged = new byte[8];//timestamp
        public override bool IsDirty
        {
            get
            {
                if (base.IsDirty)
                    return true;
                return IsDirtyList(new List