// ========================================================================
// 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
{
    /// 
    ///	Document Generated by MyGeneration using the CSLA Object Mapping template
    /// 
    [Serializable()]
    [TypeConverter(typeof(DocumentConverter))]
    public partial class Document : BusinessBase, IDisposable, IVEHasBrokenRules
    {
        #region Log4Net
        private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        #endregion
        #region Refresh
        private List _RefreshDocuments = new List();
        private List _RefreshDocumentDROUsages = new List();
        private List _RefreshDocumentEntries = new List();
        private List _RefreshDocumentPdfs = new List();
        private void AddToRefreshList(List refreshDocuments, List refreshDocumentDROUsages, List refreshDocumentEntries, List refreshDocumentPdfs)
        {
            if (IsDirty)
                refreshDocuments.Add(this);
            if (_DocumentDROUsages != null && _DocumentDROUsages.IsDirty)
            {
                foreach (DocumentDROUsage tmp in _DocumentDROUsages)
                {
                    if (tmp.IsDirty) refreshDocumentDROUsages.Add(tmp);
                }
            }
            if (_DocumentEntries != null && _DocumentEntries.IsDirty)
            {
                foreach (DocumentEntry tmp in _DocumentEntries)
                {
                    if (tmp.IsDirty) refreshDocumentEntries.Add(tmp);
                }
            }
            if (_DocumentPdfs != null && _DocumentPdfs.IsDirty)
            {
                foreach (DocumentPdf tmp in _DocumentPdfs)
                {
                    if (tmp.IsDirty) refreshDocumentPdfs.Add(tmp);
                }
            }
        }
        private void ClearRefreshList()
        {
            _RefreshDocuments = new List();
            _RefreshDocumentDROUsages = new List();
            _RefreshDocumentEntries = new List();
            _RefreshDocumentPdfs = new List();
        }
        private void BuildRefreshList()
        {
            ClearRefreshList();
            AddToRefreshList(_RefreshDocuments, _RefreshDocumentDROUsages, _RefreshDocumentEntries, _RefreshDocumentPdfs);
        }
        private void ProcessRefreshList()
        {
            foreach (Document tmp in _RefreshDocuments)
            {
                DocumentInfo.Refresh(tmp);
            }
            foreach (DocumentDROUsage tmp in _RefreshDocumentDROUsages)
            {
                DROUsageInfo.Refresh(tmp);
            }
            foreach (DocumentEntry tmp in _RefreshDocumentEntries)
            {
                EntryInfo.Refresh(tmp);
            }
            foreach (DocumentPdf tmp in _RefreshDocumentPdfs)
            {
                PdfInfo.Refresh(this, tmp);
            }
            ClearRefreshList();
        }
        #endregion
        #region Collection
        private static List _CacheList = new List();
        protected static void AddToCache(Document document)
        {
            if (!_CacheList.Contains(document)) _CacheList.Add(document); // In AddToCache
        }
        protected static void RemoveFromCache(Document document)
        {
            while (_CacheList.Contains(document)) _CacheList.Remove(document); // In RemoveFromCache
        }
        private static Dictionary> _CacheByPrimaryKey = new Dictionary>();
        private static void ConvertListToDictionary()
        {
            while (_CacheList.Count > 0) // Move Document(s) from temporary _CacheList to _CacheByPrimaryKey
            {
                Document tmp = _CacheList[0]; // Get the first Document
                string pKey = tmp.DocID.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 Document
            }
        }
        protected static Document GetCachedByPrimaryKey(int docID)
        {
            ConvertListToDictionary();
            string key = docID.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 _nextDocID = -1;
        public static int NextDocID
        {
            get { return _nextDocID--; }
        }
        private int _DocID;
        [System.ComponentModel.DataObjectField(true, true)]
        public int DocID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _DocID;
            }
        }
        private string _LibTitle = string.Empty;
        public string LibTitle
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _LibTitle;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (value == null) value = string.Empty;
                if (_LibTitle != value)
                {
                    _LibTitle = value;
                    PropertyHasChanged();
                }
            }
        }
        private byte[] _DocContent;
        /// 
        /// Actual content of a Word Document (RTF, DOC or XML Format)
        /// 
        public byte[] DocContent
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _DocContent;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (_DocContent != value)
                {
                    _DocContent = value;
                    PropertyHasChanged();
                }
            }
        }
        private string _DocAscii = string.Empty;
        /// 
        /// Used for searching
        /// 
        public string DocAscii
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _DocAscii;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (value == null) value = string.Empty;
                if (_DocAscii != value)
                {
                    _DocAscii = 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 string _FileExtension = string.Empty;
        public string FileExtension
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _FileExtension;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (value == null) value = string.Empty;
                if (_FileExtension != value)
                {
                    _FileExtension = value;
                    PropertyHasChanged();
                }
            }
        }
        private int _DocumentDROUsageCount = 0;
        /// 
        /// Count of DocumentDROUsages for this Document
        /// 
        public int DocumentDROUsageCount
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _DocumentDROUsageCount;
            }
        }
        private DocumentDROUsages _DocumentDROUsages = null;
        /// 
        /// Related Field
        /// 
        [TypeConverter(typeof(DocumentDROUsagesConverter))]
        public DocumentDROUsages DocumentDROUsages
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_DocumentDROUsageCount < 0 || (_DocumentDROUsageCount > 0 && _DocumentDROUsages == null))
                    _DocumentDROUsages = DocumentDROUsages.GetByDocID(DocID);
                if (_DocumentDROUsageCount < 0)
                    _DocumentDROUsageCount = _DocumentDROUsages == null ? 0 : _DocumentDROUsages.Count;
                if (_DocumentDROUsages == null)
                    _DocumentDROUsages = DocumentDROUsages.New();
                return _DocumentDROUsages;
            }
        }
        public void Reset_DocumentDROUsages()
        {
            _DocumentDROUsageCount = -1;
        }
        private int _DocumentEntryCount = 0;
        /// 
        /// Count of DocumentEntries for this Document
        /// 
        public int DocumentEntryCount
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_DocumentEntryCount < 0)
                    _DocumentEntryCount = DocumentEntries.Count;
                return _DocumentEntryCount;
            }
        }
        private DocumentEntries _DocumentEntries = null;
        /// 
        /// Related Field
        /// 
        [TypeConverter(typeof(DocumentEntriesConverter))]
        public DocumentEntries DocumentEntries
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_DocumentEntryCount < 0 || (_DocumentEntryCount > 0 && _DocumentEntries == null))
                    _DocumentEntries = DocumentEntries.GetByDocID(DocID);
                if (_DocumentEntryCount < 0)
                    _DocumentEntryCount = _DocumentEntries == null ? 0 : _DocumentEntries.Count;
                if (_DocumentEntries == null)
                    _DocumentEntries = DocumentEntries.New();
                return _DocumentEntries;
            }
        }
        public void Reset_DocumentEntries()
        {
            _DocumentEntryCount = -1;
        }
        private int _DocumentPdfCount = 0;
        /// 
        /// Count of DocumentPdfs for this Document
        /// 
        public int DocumentPdfCount
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _DocumentPdfCount;
            }
        }
        private DocumentPdfs _DocumentPdfs = null;
        /// 
        /// Related Field
        /// 
        [TypeConverter(typeof(DocumentPdfsConverter))]
        public DocumentPdfs DocumentPdfs
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_DocumentPdfCount < 0 || (_DocumentPdfCount > 0 && _DocumentPdfs == null))
                    _DocumentPdfs = DocumentPdfs.GetByDocID(DocID);
                if (_DocumentPdfCount < 0)
                    _DocumentPdfCount = _DocumentPdfs == null ? 0 : _DocumentPdfs.Count;
                if (_DocumentPdfs == null)
                    _DocumentPdfs = DocumentPdfs.New();
                return _DocumentPdfs;
            }
        }
        public void Reset_DocumentPdfs()
        {
            _DocumentPdfCount = -1;
        }
        public override bool IsDirty
        {
            get
            {
                if (base.IsDirty)
                    return true;
                return IsDirtyList(new List