// ========================================================================
// 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
{
    /// 
    ///	Format Generated by MyGeneration using the CSLA Object Mapping template
    /// 
    [Serializable()]
    [TypeConverter(typeof(FormatConverter))]
    public partial class Format : BusinessBase, IDisposable, IVEHasBrokenRules
    {
        #region Log4Net
        private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        #endregion
        #region Refresh
        private List _RefreshFormats = new List();
        private List _RefreshFormatContents = new List();
        private List _RefreshFormatDocVersions = new List();
        private List _RefreshFormatFolders = new List();
        private void AddToRefreshList(List refreshFormats, List refreshFormatContents, List refreshFormatDocVersions, List refreshFormatFolders)
        {
            if (IsDirty)
                refreshFormats.Add(this);
            if (_FormatContents != null && _FormatContents.IsDirty)
            {
                foreach (FormatContent tmp in _FormatContents)
                {
                    if (tmp.IsDirty) refreshFormatContents.Add(tmp);
                }
            }
            if (_FormatDocVersions != null && _FormatDocVersions.IsDirty)
            {
                foreach (FormatDocVersion tmp in _FormatDocVersions)
                {
                    if (tmp.IsDirty) refreshFormatDocVersions.Add(tmp);
                }
            }
            if (_FormatFolders != null && _FormatFolders.IsDirty)
            {
                foreach (FormatFolder tmp in _FormatFolders)
                {
                    if (tmp.IsDirty) refreshFormatFolders.Add(tmp);
                }
            }
            if (_ChildFormats != null && _ChildFormats.IsDirty)
            {
                foreach (Format tmp in _ChildFormats)
                {
                    tmp.AddToRefreshList(refreshFormats, refreshFormatContents, refreshFormatDocVersions, refreshFormatFolders);
                }
            }
        }
        private void ClearRefreshList()
        {
            _RefreshFormats = new List();
            _RefreshFormatContents = new List();
            _RefreshFormatDocVersions = new List();
            _RefreshFormatFolders = new List();
        }
        private void BuildRefreshList()
        {
            ClearRefreshList();
            AddToRefreshList(_RefreshFormats, _RefreshFormatContents, _RefreshFormatDocVersions, _RefreshFormatFolders);
        }
        private void ProcessRefreshList()
        {
            foreach (Format tmp in _RefreshFormats)
            {
                FormatInfo.Refresh(tmp);
            }
            foreach (FormatContent tmp in _RefreshFormatContents)
            {
                ContentInfo.Refresh(tmp);
            }
            foreach (FormatDocVersion tmp in _RefreshFormatDocVersions)
            {
                DocVersionInfo.Refresh(tmp);
            }
            foreach (FormatFolder tmp in _RefreshFormatFolders)
            {
                FolderInfo.Refresh(tmp);
            }
            ClearRefreshList();
        }
        #endregion
        #region Collection
        private static List _CacheList = new List();
        protected static void AddToCache(Format format)
        {
            if (!_CacheList.Contains(format)) _CacheList.Add(format); // In AddToCache
        }
        protected static void RemoveFromCache(Format format)
        {
            while (_CacheList.Contains(format)) _CacheList.Remove(format); // In RemoveFromCache
        }
        private static Dictionary> _CacheByPrimaryKey = new Dictionary>();
        private static Dictionary> _CacheByParentID_Name = new Dictionary>();
        private static void ConvertListToDictionary()
        {
            while (_CacheList.Count > 0) // Move Format(s) from temporary _CacheList to _CacheByPrimaryKey
            {
                Format tmp = _CacheList[0]; // Get the first Format
                string pKey = tmp.FormatID.ToString();
                if (!_CacheByPrimaryKey.ContainsKey(pKey))
                {
                    _CacheByPrimaryKey[pKey] = new List(); // Add new list for PrimaryKey
                    _CacheByParentID_Name[tmp.ParentID.ToString() + "_" + tmp.Name.ToString()] = new List(); // Add new list for ParentID_Name
                }
                _CacheByPrimaryKey[pKey].Add(tmp); // Add to Primary Key list
                _CacheByParentID_Name[tmp.ParentID.ToString() + "_" + tmp.Name.ToString()].Add(tmp); // Unique Index
                _CacheList.RemoveAt(0); // Remove the first Format
            }
        }
        protected static Format GetCachedByPrimaryKey(int formatID)
        {
            ConvertListToDictionary();
            string key = formatID.ToString();
            if (_CacheByPrimaryKey.ContainsKey(key)) return _CacheByPrimaryKey[key][0];
            return null;
        }
        protected static Format GetCachedByParentID_Name(int parentID, string name)
        {
            ConvertListToDictionary();
            string key = parentID.ToString() + "_" + name.ToString();
            if (_CacheByParentID_Name.ContainsKey(key)) return _CacheByParentID_Name[key][0];
            return null;
        }
        #endregion
        #region Business Methods
        private string _ErrorMessage = string.Empty;
        public string ErrorMessage
        {
            get { return _ErrorMessage; }
        }
        private static int _nextFormatID = -1;
        public static int NextFormatID
        {
            get { return _nextFormatID--; }
        }
        private int _FormatID;
        [System.ComponentModel.DataObjectField(true, true)]
        public int FormatID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _FormatID;
            }
        }
        private int _ParentID;
        public int ParentID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_MyParent != null) _ParentID = _MyParent.FormatID;
                return _ParentID;
            }
        }
        private Format _MyParent;
        public Format MyParent
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_MyParent == null && _ParentID != _FormatID) _MyParent = Format.Get(_ParentID);
                return _MyParent;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (_MyParent != value)
                {
                    _MyParent = value;
                    _ParentID = value.FormatID;// Update underlying data field
                    PropertyHasChanged();
                }
            }
        }
        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 _Description = string.Empty;
        public string Description
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _Description;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (value == null) value = string.Empty;
                if (_Description != value)
                {
                    _Description = value;
                    PropertyHasChanged();
                }
            }
        }
        private string _Data = string.Empty;
        public string Data
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _Data;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (value == null) value = string.Empty;
                if (_Data != value)
                {
                    _Data = 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 string _GenMac = string.Empty;
        public string GenMac
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _GenMac;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (value == null) value = string.Empty;
                if (_GenMac != value)
                {
                    _GenMac = 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 _FormatContentCount = 0;
        /// 
        /// Count of FormatContents for this Format
        /// 
        public int FormatContentCount
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _FormatContentCount;
            }
        }
        private FormatContents _FormatContents = null;
        /// 
        /// Related Field
        /// 
        [TypeConverter(typeof(FormatContentsConverter))]
        public FormatContents FormatContents
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_FormatContentCount < 0 || (_FormatContentCount > 0 && _FormatContents == null))
                    _FormatContents = FormatContents.GetByFormatID(FormatID);
                if (_FormatContentCount < 0)
                    _FormatContentCount = _FormatContents == null ? 0 : _FormatContents.Count;
                if (_FormatContents == null)
                    _FormatContents = FormatContents.New();
                return _FormatContents;
            }
        }
        public void Reset_FormatContents()
        {
            _FormatContentCount = -1;
        }
        private int _FormatDocVersionCount = 0;
        /// 
        /// Count of FormatDocVersions for this Format
        /// 
        public int FormatDocVersionCount
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _FormatDocVersionCount;
            }
        }
        private FormatDocVersions _FormatDocVersions = null;
        /// 
        /// Related Field
        /// 
        [TypeConverter(typeof(FormatDocVersionsConverter))]
        public FormatDocVersions FormatDocVersions
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_FormatDocVersionCount < 0 || (_FormatDocVersionCount > 0 && _FormatDocVersions == null))
                    _FormatDocVersions = FormatDocVersions.GetByFormatID(FormatID);
                if (_FormatDocVersionCount < 0)
                    _FormatDocVersionCount = _FormatDocVersions == null ? 0 : _FormatDocVersions.Count;
                if (_FormatDocVersions == null)
                    _FormatDocVersions = FormatDocVersions.New();
                return _FormatDocVersions;
            }
        }
        public void Reset_FormatDocVersions()
        {
            _FormatDocVersionCount = -1;
        }
        private int _FormatFolderCount = 0;
        /// 
        /// Count of FormatFolders for this Format
        /// 
        public int FormatFolderCount
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _FormatFolderCount;
            }
        }
        private FormatFolders _FormatFolders = null;
        /// 
        /// Related Field
        /// 
        [TypeConverter(typeof(FormatFoldersConverter))]
        public FormatFolders FormatFolders
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_FormatFolderCount < 0 || (_FormatFolderCount > 0 && _FormatFolders == null))
                    _FormatFolders = FormatFolders.GetByFormatID(FormatID);
                if (_FormatFolderCount < 0)
                    _FormatFolderCount = _FormatFolders == null ? 0 : _FormatFolders.Count;
                if (_FormatFolders == null)
                    _FormatFolders = FormatFolders.New();
                return _FormatFolders;
            }
        }
        public void Reset_FormatFolders()
        {
            _FormatFolderCount = -1;
        }
        private int _ChildFormatCount = 0;
        /// 
        /// Count of ChildFormats for this Format
        /// 
        public int ChildFormatCount
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _ChildFormatCount;
            }
        }
        private ChildFormats _ChildFormats = null;
        /// 
        /// Related Field
        /// 
        [TypeConverter(typeof(ChildFormatsConverter))]
        public ChildFormats ChildFormats
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_ChildFormatCount < 0 || (_ChildFormatCount > 0 && _ChildFormats == null))
                    _ChildFormats = ChildFormats.GetByParentID(FormatID);
                if (_ChildFormatCount < 0)
                    _ChildFormatCount = _ChildFormats == null ? 0 : _ChildFormats.Count;
                if (_ChildFormats == null)
                    _ChildFormats = ChildFormats.New();
                return _ChildFormats;
            }
        }
        public void Reset_ChildFormats()
        {
            _ChildFormatCount = -1;
        }
        public override bool IsDirty
        {
            get
            {
                if (base.IsDirty)
                    return true;
                return IsDirtyList(new List