// ========================================================================
// 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
{
    /// 
    ///	ROFst Generated by MyGeneration using the CSLA Object Mapping template
    /// 
    [Serializable()]
    [TypeConverter(typeof(ROFstConverter))]
    public partial class ROFst : BusinessBase, IDisposable, IVEHasBrokenRules
    {
        #region Log4Net
        private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        #endregion
        #region Refresh
        private List _RefreshROFsts = new List();
        private List _RefreshROFstAssociations = new List();
        private List _RefreshROFstFigures = new List();
        private void AddToRefreshList(List refreshROFsts, List refreshROFstAssociations, List refreshROFstFigures)
        {
            if (IsDirty)
                refreshROFsts.Add(this);
            if (_ROFstAssociations != null && _ROFstAssociations.IsDirty)
            {
                foreach (ROFstAssociation tmp in _ROFstAssociations)
                {
                    if (tmp.IsDirty) refreshROFstAssociations.Add(tmp);
                }
            }
            if (_ROFstFigures != null && _ROFstFigures.IsDirty)
            {
                foreach (ROFstFigure tmp in _ROFstFigures)
                {
                    if (tmp.IsDirty) refreshROFstFigures.Add(tmp);
                }
            }
        }
        private void ClearRefreshList()
        {
            _RefreshROFsts = new List();
            _RefreshROFstAssociations = new List();
            _RefreshROFstFigures = new List();
        }
        private void BuildRefreshList()
        {
            ClearRefreshList();
            AddToRefreshList(_RefreshROFsts, _RefreshROFstAssociations, _RefreshROFstFigures);
        }
        private void ProcessRefreshList()
        {
            foreach (ROFst tmp in _RefreshROFsts)
            {
                ROFstInfo.Refresh(tmp);
                if (tmp._MyRODb != null) RODbInfo.Refresh(tmp._MyRODb);
            }
            foreach (ROFstAssociation tmp in _RefreshROFstAssociations)
            {
                AssociationInfo.Refresh(tmp);
            }
            foreach (ROFstFigure tmp in _RefreshROFstFigures)
            {
                FigureInfo.Refresh(tmp);
            }
            ClearRefreshList();
        }
        #endregion
        #region Collection
        private static List _CacheList = new List();
        protected static void AddToCache(ROFst rOFst)
        {
            if (!_CacheList.Contains(rOFst))
            {
                rOFst.ROLookup = null; // B2022-026 RO Memory reduction
                _CacheList.Add(rOFst); // In AddToCache
            }
        }
        protected static void RemoveFromCache(ROFst rOFst)
        {
            while (_CacheList.Contains(rOFst)) _CacheList.Remove(rOFst); // In RemoveFromCache
        }
        private static Dictionary> _CacheByPrimaryKey = new Dictionary>();
        private static Dictionary> _CacheByRODbID_DTS = new Dictionary>();
        private static void ConvertListToDictionary()
        {
            while (_CacheList.Count > 0) // Move ROFst(s) from temporary _CacheList to _CacheByPrimaryKey
            {
                ROFst tmp = _CacheList[0]; // Get the first ROFst
                string pKey = tmp.ROFstID.ToString();
                if (!_CacheByPrimaryKey.ContainsKey(pKey))
                {
                    _CacheByPrimaryKey[pKey] = new List(); // Add new list for PrimaryKey
                    _CacheByRODbID_DTS[tmp.RODbID.ToString() + "_" + tmp.DTS.ToString()] = new List(); // Add new list for RODbID_DTS
                }
                tmp.ROLookup = null; // B2022-026 RO Memory reduction
                _CacheByPrimaryKey[pKey].Add(tmp); // Add to Primary Key list
                _CacheByRODbID_DTS[tmp.RODbID.ToString() + "_" + tmp.DTS.ToString()].Add(tmp); // Unique Index
                _CacheList.RemoveAt(0); // Remove the first ROFst
            }
        }
        protected static ROFst GetCachedByPrimaryKey(int rOFstID)
        {
            ConvertListToDictionary();
            string key = rOFstID.ToString();
            if (_CacheByPrimaryKey.ContainsKey(key)) return _CacheByPrimaryKey[key][0];
            return null;
        }
        protected static ROFst GetCachedByRODbID_DTS(int rODbID, DateTime dts)
        {
            ConvertListToDictionary();
            string key = rODbID.ToString() + "_" + dts.ToString();
            if (_CacheByRODbID_DTS.ContainsKey(key)) return _CacheByRODbID_DTS[key][0];
            return null;
        }
        #endregion
        #region Business Methods
        private string _ErrorMessage = string.Empty;
        public string ErrorMessage
        {
            get { return _ErrorMessage; }
        }
        private static int _nextROFstID = -1;
        public static int NextROFstID
        {
            get { return _nextROFstID--; }
        }
        private int _ROFstID;
        [System.ComponentModel.DataObjectField(true, true)]
        public int ROFstID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _ROFstID;
            }
        }
        private int _RODbID;
        public int RODbID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_MyRODb != null) _RODbID = _MyRODb.RODbID;
                return _RODbID;
            }
        }
        private RODb _MyRODb;
        public RODb MyRODb
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_MyRODb == null && _RODbID != 0) _MyRODb = RODb.Get(_RODbID);
                return _MyRODb;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (_MyRODb != value)
                {
                    _MyRODb = value;
                    _RODbID = value.RODbID;// Update underlying data field
                    PropertyHasChanged();
                }
            }
        }
        private byte[] _ROLookup;
        public byte[] ROLookup
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return null; // B2022-026 RO Memory reduction
                             //if (_ROLookup == null)
                             //{
                             //	_ROLookup = ROFSTLookup.GetRofstLookupBytes(_ROFstID);
                             //}
                             //return _ROLookup;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (_ROLookup != value)
                {
                    _ROLookup = 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 _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
        private int _ROFstAssociationCount = 0;
        /// 
        /// Count of ROFstAssociations for this ROFst
        /// 
        public int ROFstAssociationCount
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _ROFstAssociationCount;
            }
        }
        private ROFstAssociations _ROFstAssociations = null;
        /// 
        /// Related Field
        /// 
        [TypeConverter(typeof(ROFstAssociationsConverter))]
        public ROFstAssociations ROFstAssociations
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_ROFstAssociationCount < 0 || (_ROFstAssociationCount > 0 && _ROFstAssociations == null))
                    _ROFstAssociations = ROFstAssociations.GetByROFstID(ROFstID);
                if (_ROFstAssociationCount < 0)
                    _ROFstAssociationCount = _ROFstAssociations == null ? 0 : _ROFstAssociations.Count;
                if (_ROFstAssociations == null)
                    _ROFstAssociations = ROFstAssociations.New();
                return _ROFstAssociations;
            }
        }
        public void Reset_ROFstAssociations()
        {
            _ROFstAssociationCount = -1;
        }
        private int _ROFstFigureCount = 0;
        /// 
        /// Count of ROFstFigures for this ROFst
        /// 
        public int ROFstFigureCount
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _ROFstFigureCount;
            }
        }
        private ROFstFigures _ROFstFigures = null;
        /// 
        /// Related Field
        /// 
        [TypeConverter(typeof(ROFstFiguresConverter))]
        public ROFstFigures ROFstFigures
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_ROFstFigureCount < 0 || (_ROFstFigureCount > 0 && _ROFstFigures == null))
                    _ROFstFigures = ROFstFigures.GetByROFstID(ROFstID);
                if (_ROFstFigureCount < 0)
                    _ROFstFigureCount = _ROFstFigures == null ? 0 : _ROFstFigures.Count;
                if (_ROFstFigures == null)
                    _ROFstFigures = ROFstFigures.New();
                return _ROFstFigures;
            }
        }
        public void Reset_ROFstFigures()
        {
            _ROFstFigureCount = -1;
        }
        public override bool IsDirty
        {
            get
            {
                if (base.IsDirty)
                    return true;
                return IsDirtyList(new List