// ========================================================================
// 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 _RefreshDocumentEntries = new List();
		private void AddToRefreshList(List refreshDocuments, List refreshDocumentEntries)
		{
			if (IsDirty)
				refreshDocuments.Add(this);
			if (_DocumentEntries != null && _DocumentEntries.IsDirty)
			{
				foreach (DocumentEntry tmp in _DocumentEntries)
				{
					if (tmp.IsDirty) refreshDocumentEntries.Add(tmp);
				}
			}
		}
		private void BuildRefreshList()
		{
			_RefreshDocuments = new List();
			_RefreshDocumentEntries = new List();
			AddToRefreshList(_RefreshDocuments, _RefreshDocumentEntries);
		}
		private void ProcessRefreshList()
		{
			foreach (Document tmp in _RefreshDocuments)
			{
				DocumentInfo.Refresh(tmp);
			}
			foreach (DocumentEntry tmp in _RefreshDocumentEntries)
			{
				EntryInfo.Refresh(tmp);
			}
		}
		#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()
		{
			List remove = new List();
			foreach (Document tmp in _CacheList)
			{
				if (!_CacheByPrimaryKey.ContainsKey(tmp.DocID.ToString()))
				{
					_CacheByPrimaryKey[tmp.DocID.ToString()] = new List(); // Add new list for PrimaryKey
				}
				_CacheByPrimaryKey[tmp.DocID.ToString()].Add(tmp); // Add to Primary Key list
				remove.Add(tmp);
			}
			foreach (Document tmp in remove)
				RemoveFromCache(tmp);
		}
		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
			{
				CanReadProperty("DocID", true);
				return _DocID;
			}
		}
		private string _LibTitle = string.Empty;
		public string LibTitle
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("LibTitle", true);
				return _LibTitle;
			}
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			set
			{
				CanWriteProperty("LibTitle", true);
				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
			{
				CanReadProperty("DocContent", true);
				return _DocContent;
			}
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			set
			{
				CanWriteProperty("DocContent", true);
				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
			{
				CanReadProperty("DocAscii", true);
				return _DocAscii;
			}
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			set
			{
				CanWriteProperty("DocAscii", true);
				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
			{
				CanReadProperty("Config", true);
				return _Config;
			}
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			set
			{
				CanWriteProperty("Config", true);
				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
			{
				CanReadProperty("DTS", true);
				return _DTS;
			}
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			set
			{
				CanWriteProperty("DTS", true);
				if (_DTS != value)
				{
					_DTS = value;
					PropertyHasChanged();
				}
			}
		}
		private string _UserID = string.Empty;
		public string UserID
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("UserID", true);
				return _UserID;
			}
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			set
			{
				CanWriteProperty("UserID", true);
				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
			{
				CanReadProperty("FileExtension", true);
				return _FileExtension;
			}
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			set
			{
				CanWriteProperty("FileExtension", true);
				if (value == null) value = string.Empty;
				if (_FileExtension != value)
				{
					_FileExtension = value;
					PropertyHasChanged();
				}
			}
		}
		private int _DocumentEntryCount = 0;
		/// 
		/// Count of DocumentEntries for this Document
		/// 
		public int DocumentEntryCount
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("DocumentEntryCount", true);
				return _DocumentEntryCount;
			}
		}
		private DocumentEntries _DocumentEntries = null;
		/// 
		/// Related Field
		/// 
		[TypeConverter(typeof(DocumentEntriesConverter))]
		public DocumentEntries DocumentEntries
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("DocumentEntries", true);
				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;
		}
		public override bool IsDirty
		{
			get 
			{
				if ( base.IsDirty ) 
					return true;
				return IsDirtyList(new List