// ========================================================================
// 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
{
    /// 
    ///	DocVersion Generated by MyGeneration using the CSLA Object Mapping template
    /// 
    [Serializable()]
    [TypeConverter(typeof(DocVersionConverter))]
    public partial class DocVersion : BusinessBase, IDisposable, IVEHasBrokenRules
    {
        #region Log4Net
        private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        #endregion
        #region Refresh
        private List _RefreshDocVersions = new List();
        private List _RefreshDocVersionAssociations = new List();
        private void AddToRefreshList(List refreshDocVersions, List refreshDocVersionAssociations)
        {
            if (IsDirty)
                refreshDocVersions.Add(this);
            if (_DocVersionAssociations != null && _DocVersionAssociations.IsDirty)
            {
                foreach (DocVersionAssociation tmp in _DocVersionAssociations)
                {
                    if (tmp.IsDirty) refreshDocVersionAssociations.Add(tmp);
                }
            }
        }
        private void ClearRefreshList()
        {
            _RefreshDocVersions = new List();
            _RefreshDocVersionAssociations = new List();
        }
        private void BuildRefreshList()
        {
            ClearRefreshList();
            AddToRefreshList(_RefreshDocVersions, _RefreshDocVersionAssociations);
        }
        private void ProcessRefreshList()
        {
            foreach (DocVersion tmp in _RefreshDocVersions)
            {
                DocVersionInfo.Refresh(tmp);
                if (tmp._MyFolder != null) FolderInfo.Refresh(tmp._MyFolder);
                if (tmp._MyFormat != null) FormatInfo.Refresh(tmp._MyFormat);
                if (tmp._MyItem != null) ItemInfo.Refresh(tmp._MyItem);
            }
            foreach (DocVersionAssociation tmp in _RefreshDocVersionAssociations)
            {
                AssociationInfo.Refresh(tmp);
            }
            ClearRefreshList();
        }
        #endregion
        #region Collection
        private static List _CacheList = new List();
        protected static void AddToCache(DocVersion docVersion)
        {
            if (!_CacheList.Contains(docVersion)) _CacheList.Add(docVersion); // In AddToCache
        }
        protected static void RemoveFromCache(DocVersion docVersion)
        {
            while (_CacheList.Contains(docVersion)) _CacheList.Remove(docVersion); // In RemoveFromCache
        }
        private static Dictionary> _CacheByPrimaryKey = new Dictionary>();
        private static void ConvertListToDictionary()
        {
            while (_CacheList.Count > 0) // Move DocVersion(s) from temporary _CacheList to _CacheByPrimaryKey
            {
                DocVersion tmp = _CacheList[0]; // Get the first DocVersion
                string pKey = tmp.VersionID.ToString();
                if (!_CacheByPrimaryKey.ContainsKey(pKey))
                {
                    _CacheByPrimaryKey[pKey] = new List(); // Add new list for PrimaryKey
                }
                _CacheByPrimaryKey[pKey].Add(tmp); // Add to Primary Key list
                _CacheList.RemoveAt(0); // Remove the first DocVersion
            }
        }
        protected static DocVersion GetCachedByPrimaryKey(int versionID)
        {
            ConvertListToDictionary();
            string key = versionID.ToString();
            if (_CacheByPrimaryKey.ContainsKey(key)) return _CacheByPrimaryKey[key][0];
            return null;
        }
        #endregion
        #region Business Methods
        private string _ErrorMessage = string.Empty;
        public string ErrorMessage
        {
            get { return _ErrorMessage; }
        }
        private static int _nextVersionID = -1;
        public static int NextVersionID
        {
            get { return _nextVersionID--; }
        }
        private int _VersionID;
        [System.ComponentModel.DataObjectField(true, true)]
        public int VersionID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _VersionID;
            }
        }
        private int _FolderID;
        public int FolderID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_MyFolder != null) _FolderID = _MyFolder.FolderID;
                return _FolderID;
            }
        }
        private Folder _MyFolder;
        public Folder MyFolder
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_MyFolder == null && _FolderID != 0) _MyFolder = Folder.Get(_FolderID);
                return _MyFolder;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (_MyFolder != value)
                {
                    _MyFolder = value;
                    _FolderID = value.FolderID;// Update underlying data field
                    PropertyHasChanged();
                }
            }
        }
        private int _VersionType;
        /// 
        /// 0 Working Draft, 1 Temporary, 128 Revision, 129 Approved (Greater than 127  - non editable)
        /// 
        public int VersionType
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _VersionType;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (_VersionType != value)
                {
                    _VersionType = value;
                    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 _Title = string.Empty;
        public string Title
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _Title;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (value == null) value = string.Empty;
                if (_Title != value)
                {
                    _Title = value;
                    PropertyHasChanged();
                }
            }
        }
        private int? _ItemID;
        public int? ItemID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_MyItem != null) _ItemID = _MyItem.ItemID;
                return _ItemID;
            }
        }
        private Item _MyItem;
        public Item MyItem
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_MyItem == null && _ItemID != null) _MyItem = Item.Get((int)_ItemID);
                return _MyItem;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if ((_MyItem == null ? _ItemID : (int?)_MyItem.ItemID) != (value == null ? null : (int?)value.ItemID))
                {
                    _MyItem = value;
                    _ItemID = (value == null ? null : (int?)value.ItemID);
                    PropertyHasChanged();
                }
            }
        }
        private int? _FormatID;
        public int? FormatID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_MyFormat != null) _FormatID = _MyFormat.FormatID;
                return _FormatID;
            }
        }
        private Format _MyFormat;
        public Format MyFormat
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_MyFormat == null && _FormatID != null) _MyFormat = Format.Get((int)_FormatID);
                return _MyFormat;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if ((_MyFormat == null ? _FormatID : (int?)_MyFormat.FormatID) != (value == null ? null : (int?)value.FormatID))
                {
                    _MyFormat = value;
                    _FormatID = (value == null ? null : (int?)value.FormatID);
                    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 _DocVersionAssociationCount = 0;
        /// 
        /// Count of DocVersionAssociations for this DocVersion
        /// 
        public int DocVersionAssociationCount
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _DocVersionAssociationCount;
            }
        }
        private DocVersionAssociations _DocVersionAssociations = null;
        /// 
        /// Related Field
        /// 
        [TypeConverter(typeof(DocVersionAssociationsConverter))]
        public DocVersionAssociations DocVersionAssociations
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_DocVersionAssociationCount < 0 || (_DocVersionAssociationCount > 0 && _DocVersionAssociations == null))
                    _DocVersionAssociations = DocVersionAssociations.GetByVersionID(VersionID);
                if (_DocVersionAssociationCount < 0)
                    _DocVersionAssociationCount = _DocVersionAssociations == null ? 0 : _DocVersionAssociations.Count;
                if (_DocVersionAssociations == null)
                    _DocVersionAssociations = DocVersionAssociations.New();
                return _DocVersionAssociations;
            }
        }
        public void Reset_DocVersionAssociations()
        {
            _DocVersionAssociationCount = -1;
        }
        public override bool IsDirty
        {
            get
            {
                if (base.IsDirty)
                    return true;
                return IsDirtyList(new List