// ========================================================================
// 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
{
    /// 
    ///	RODb Generated by MyGeneration using the CSLA Object Mapping template
    /// 
    [Serializable()]
    [TypeConverter(typeof(RODbConverter))]
    public partial class RODb : BusinessBase, IDisposable, IVEHasBrokenRules
    {
        #region Log4Net
        private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        #endregion
        #region Refresh
        private List _RefreshRODbs = new List();
        private List _RefreshRODbDROUsages = new List();
        private List _RefreshRODbROFsts = new List();
        private List _RefreshRODbROImages = new List();
        private List _RefreshRODbRoUsages = new List();
        private void AddToRefreshList(List refreshRODbs, List refreshRODbDROUsages, List refreshRODbROFsts, List refreshRODbROImages, List refreshRODbRoUsages)
        {
            if (IsDirty)
                refreshRODbs.Add(this);
            if (_RODbDROUsages != null && _RODbDROUsages.IsDirty)
            {
                foreach (RODbDROUsage tmp in _RODbDROUsages)
                {
                    if (tmp.IsDirty) refreshRODbDROUsages.Add(tmp);
                }
            }
            if (_RODbROFsts != null && _RODbROFsts.IsDirty)
            {
                foreach (RODbROFst tmp in _RODbROFsts)
                {
                    if (tmp.IsDirty) refreshRODbROFsts.Add(tmp);
                }
            }
            if (_RODbROImages != null && _RODbROImages.IsDirty)
            {
                foreach (RODbROImage tmp in _RODbROImages)
                {
                    if (tmp.IsDirty) refreshRODbROImages.Add(tmp);
                }
            }
            if (_RODbRoUsages != null && _RODbRoUsages.IsDirty)
            {
                foreach (RODbRoUsage tmp in _RODbRoUsages)
                {
                    if (tmp.IsDirty) refreshRODbRoUsages.Add(tmp);
                }
            }
        }
        private void ClearRefreshList()
        {
            _RefreshRODbs = new List();
            _RefreshRODbDROUsages = new List();
            _RefreshRODbROFsts = new List();
            _RefreshRODbROImages = new List();
            _RefreshRODbRoUsages = new List();
        }
        private void BuildRefreshList()
        {
            ClearRefreshList();
            AddToRefreshList(_RefreshRODbs, _RefreshRODbDROUsages, _RefreshRODbROFsts, _RefreshRODbROImages, _RefreshRODbRoUsages);
        }
        private void ProcessRefreshList()
        {
            foreach (RODb tmp in _RefreshRODbs)
            {
                RODbInfo.Refresh(tmp);
            }
            foreach (RODbDROUsage tmp in _RefreshRODbDROUsages)
            {
                DROUsageInfo.Refresh(tmp);
            }
            foreach (RODbROFst tmp in _RefreshRODbROFsts)
            {
                ROFstInfo.Refresh(tmp);
            }
            foreach (RODbROImage tmp in _RefreshRODbROImages)
            {
                ROImageInfo.Refresh(tmp);
            }
            foreach (RODbRoUsage tmp in _RefreshRODbRoUsages)
            {
                RoUsageInfo.Refresh(tmp);
            }
            ClearRefreshList();
        }
        #endregion
        #region Collection
        private static List _CacheList = new List();
        protected static void AddToCache(RODb rODb)
        {
            if (!_CacheList.Contains(rODb)) _CacheList.Add(rODb); // In AddToCache
        }
        protected static void RemoveFromCache(RODb rODb)
        {
            while (_CacheList.Contains(rODb)) _CacheList.Remove(rODb); // In RemoveFromCache
        }
        private static Dictionary> _CacheByPrimaryKey = new Dictionary>();
        private static Dictionary> _CacheByFolderPath = new Dictionary>();
        private static void ConvertListToDictionary()
        {
            while (_CacheList.Count > 0) // Move RODb(s) from temporary _CacheList to _CacheByPrimaryKey
            {
                RODb tmp = _CacheList[0]; // Get the first RODb
                string pKey = tmp.RODbID.ToString();
                if (!_CacheByPrimaryKey.ContainsKey(pKey))
                {
                    _CacheByPrimaryKey[pKey] = new List(); // Add new list for PrimaryKey
                    _CacheByFolderPath[tmp.FolderPath.ToString()] = new List(); // Add new list for FolderPath
                }
                _CacheByPrimaryKey[pKey].Add(tmp); // Add to Primary Key list
                _CacheByFolderPath[tmp.FolderPath.ToString()].Add(tmp); // Unique Index
                _CacheList.RemoveAt(0); // Remove the first RODb
            }
        }
        protected static RODb GetCachedByPrimaryKey(int rODbID)
        {
            ConvertListToDictionary();
            string key = rODbID.ToString();
            if (_CacheByPrimaryKey.ContainsKey(key)) return _CacheByPrimaryKey[key][0];
            return null;
        }
        protected static RODb GetCachedByFolderPath(string folderPath)
        {
            ConvertListToDictionary();
            string key = folderPath.ToString();
            if (_CacheByFolderPath.ContainsKey(key)) return _CacheByFolderPath[key][0];
            return null;
        }
        #endregion
        #region Business Methods
        private string _ErrorMessage = string.Empty;
        public string ErrorMessage
        {
            get { return _ErrorMessage; }
        }
        private static int _nextRODbID = -1;
        public static int NextRODbID
        {
            get { return _nextRODbID--; }
        }
        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;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (value == null) value = string.Empty;
                if (_ROName != value)
                {
                    _ROName = value;
                    PropertyHasChanged();
                }
            }
        }
        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;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (value == null) value = string.Empty;
                if (_FolderPath != value)
                {
                    _FolderPath = value;
                    PropertyHasChanged();
                }
            }
        }
        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;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (value == null) value = string.Empty;
                if (_DBConnectionString != value)
                {
                    _DBConnectionString = 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 _RODbDROUsageCount = 0;
        /// 
        /// Count of RODbDROUsages for this RODb
        /// 
        public int RODbDROUsageCount
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _RODbDROUsageCount;
            }
        }
        private RODbDROUsages _RODbDROUsages = null;
        /// 
        /// Related Field
        /// 
        [TypeConverter(typeof(RODbDROUsagesConverter))]
        public RODbDROUsages RODbDROUsages
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_RODbDROUsageCount < 0 || (_RODbDROUsageCount > 0 && _RODbDROUsages == null))
                    _RODbDROUsages = RODbDROUsages.GetByRODbID(RODbID);
                if (_RODbDROUsageCount < 0)
                    _RODbDROUsageCount = _RODbDROUsages == null ? 0 : _RODbDROUsages.Count;
                if (_RODbDROUsages == null)
                    _RODbDROUsages = RODbDROUsages.New();
                return _RODbDROUsages;
            }
        }
        public void Reset_RODbDROUsages()
        {
            _RODbDROUsageCount = -1;
        }
        private int _RODbROFstCount = 0;
        /// 
        /// Count of RODbROFsts for this RODb
        /// 
        public int RODbROFstCount
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _RODbROFstCount;
            }
        }
        private RODbROFsts _RODbROFsts = null;
        /// 
        /// Related Field
        /// 
        [TypeConverter(typeof(RODbROFstsConverter))]
        public RODbROFsts RODbROFsts
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_RODbROFstCount < 0 || (_RODbROFstCount > 0 && _RODbROFsts == null))
                    _RODbROFsts = RODbROFsts.GetByRODbID(RODbID);
                if (_RODbROFstCount < 0)
                    _RODbROFstCount = _RODbROFsts == null ? 0 : _RODbROFsts.Count;
                if (_RODbROFsts == null)
                    _RODbROFsts = RODbROFsts.New();
                return _RODbROFsts;
            }
        }
        public void Reset_RODbROFsts()
        {
            _RODbROFstCount = -1;
        }
        private int _RODbROImageCount = 0;
        /// 
        /// Count of RODbROImages for this RODb
        /// 
        public int RODbROImageCount
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _RODbROImageCount;
            }
        }
        private RODbROImages _RODbROImages = null;
        /// 
        /// Related Field
        /// 
        [TypeConverter(typeof(RODbROImagesConverter))]
        public RODbROImages RODbROImages
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_RODbROImageCount < 0 || (_RODbROImageCount > 0 && _RODbROImages == null))
                    _RODbROImages = RODbROImages.GetByRODbID(RODbID);
                if (_RODbROImageCount < 0)
                    _RODbROImageCount = _RODbROImages == null ? 0 : _RODbROImages.Count;
                if (_RODbROImages == null)
                    _RODbROImages = RODbROImages.New();
                return _RODbROImages;
            }
        }
        public void Reset_RODbROImages()
        {
            _RODbROImageCount = -1;
        }
        private int _RODbRoUsageCount = 0;
        /// 
        /// Count of RODbRoUsages for this RODb
        /// 
        public int RODbRoUsageCount
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _RODbRoUsageCount;
            }
        }
        private RODbRoUsages _RODbRoUsages = null;
        /// 
        /// Related Field
        /// 
        [TypeConverter(typeof(RODbRoUsagesConverter))]
        public RODbRoUsages RODbRoUsages
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_RODbRoUsageCount < 0 || (_RODbRoUsageCount > 0 && _RODbRoUsages == null))
                    _RODbRoUsages = RODbRoUsages.GetByRODbID(RODbID);
                if (_RODbRoUsageCount < 0)
                    _RODbRoUsageCount = _RODbRoUsages == null ? 0 : _RODbRoUsages.Count;
                if (_RODbRoUsages == null)
                    _RODbRoUsages = RODbRoUsages.New();
                return _RODbRoUsages;
            }
        }
        public void Reset_RODbRoUsages()
        {
            _RODbRoUsageCount = -1;
        }
        public override bool IsDirty
        {
            get
            {
                if (base.IsDirty)
                    return true;
                return IsDirtyList(new List