// ========================================================================
// 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
{
	/// 
	///	Version Generated by MyGeneration using the CSLA Object Mapping template
	/// 
	[Serializable()]
	[TypeConverter(typeof(VersionConverter))]
	public partial class Version : BusinessBase, IDisposable, IVEHasBrokenRules
	{
		#region Log4Net
		private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
		#endregion
		#region Refresh
		private List _RefreshVersions = new List();
		private void AddToRefreshList(List refreshVersions)
		{
			if (IsDirty)
				refreshVersions.Add(this);
		}
		private void ClearRefreshList()
		{
			_RefreshVersions = new List();
		}
		private void BuildRefreshList()
		{
			ClearRefreshList();
			AddToRefreshList(_RefreshVersions);
		}
		private void ProcessRefreshList()
		{
			foreach (Version tmp in _RefreshVersions)
			{
				VersionInfo.Refresh(tmp);
				if (tmp._MyRevision != null) RevisionInfo.Refresh(tmp._MyRevision);
				if (tmp._MyStage != null) StageInfo.Refresh(tmp._MyStage);
			}
			ClearRefreshList();
		}
		#endregion
		#region Collection
		private static List _CacheList = new List();
		protected static void AddToCache(Version version)
		{
			if (!_CacheList.Contains(version)) _CacheList.Add(version); // In AddToCache
		}
		protected static void RemoveFromCache(Version version)
		{
			while (_CacheList.Contains(version)) _CacheList.Remove(version); // In RemoveFromCache
		}
		private static Dictionary> _CacheByPrimaryKey = new Dictionary>();
		private static void ConvertListToDictionary()
		{
			while (_CacheList.Count > 0) // Move Version(s) from temporary _CacheList to _CacheByPrimaryKey
			{
				Version tmp = _CacheList[0]; // Get the first Version
				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 Version
			}
		}
		protected static Version 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
			{
				CanReadProperty("VersionID", true);
				return _VersionID;
			}
		}
		private int _RevisionID;
		public int RevisionID
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("RevisionID", true);
				if (_MyRevision != null) _RevisionID = _MyRevision.RevisionID;
				return _RevisionID;
			}
		}
		private Revision _MyRevision;
		public Revision MyRevision
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("MyRevision", true);
				if (_MyRevision == null && _RevisionID != 0) _MyRevision = Revision.Get(_RevisionID);
				return _MyRevision;
			}
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			set
			{
				CanWriteProperty("MyRevision", true);
				if (_MyRevision != value)
				{
					_MyRevision = value;
					_RevisionID = value.RevisionID;// Update underlying data field
					PropertyHasChanged();
				}
			}
		}
		private int _StageID;
		public int StageID
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("StageID", true);
				if (_MyStage != null) _StageID = _MyStage.StageID;
				return _StageID;
			}
		}
		private Stage _MyStage;
		public Stage MyStage
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("MyStage", true);
				if (_MyStage == null && _StageID != 0) _MyStage = Stage.GetJustStage(_StageID);
				return _MyStage;
			}
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			set
			{
				CanWriteProperty("MyStage", true);
				if (_MyStage != value)
				{
					_MyStage = value;
					_StageID = value.StageID;// Update underlying data field
					PropertyHasChanged();
				}
			}
		}
		private byte[] _PDF;
		public byte[] PDF
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("PDF", true);
				return _PDF;
			}
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			set
			{
				CanWriteProperty("PDF", true);
				if (_PDF != value)
				{
					_PDF = value;
					PropertyHasChanged();
				}
			}
		}
		private byte[] _SummaryPDF;
		public byte[] SummaryPDF
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("SummaryPDF", true);
				return _SummaryPDF;
			}
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			set
			{
				CanWriteProperty("SummaryPDF", true);
				if (_SummaryPDF != value)
				{
					_SummaryPDF = value;
					PropertyHasChanged();
				}
			}
		}
		private string _ApprovedXML;
		public string ApprovedXML
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("ApprovedXML", true);
				return _ApprovedXML;
			}
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			set
			{
				CanWriteProperty("ApprovedXML", true);
				if (_ApprovedXML != value)
				{
					_ApprovedXML = 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
		public override bool IsDirty
		{
			get 
			{
				if ( base.IsDirty ) 
					return true;
				return IsDirtyList(new List