// ========================================================================
// 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
{
	/// 
	///	Folder Generated by MyGeneration using the CSLA Object Mapping template
	/// 
	[Serializable()]
	[TypeConverter(typeof(FolderConverter))]
	public partial class Folder : BusinessBase, IDisposable, IVEHasBrokenRules
	{
		#region Log4Net
		private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
		#endregion
		#region Refresh
		private List _RefreshFolders = new List();
		private List _RefreshFolderAssignments = new List();
		private List _RefreshFolderDocVersions = new List();
		private void AddToRefreshList(List refreshFolders, List refreshFolderAssignments, List refreshFolderDocVersions)
		{
			if (IsDirty)
				refreshFolders.Add(this);
			if (_FolderAssignments != null && _FolderAssignments.IsDirty)
			{
				foreach (FolderAssignment tmp in _FolderAssignments)
				{
					if (tmp.IsDirty) refreshFolderAssignments.Add(tmp);
				}
			}
			if (_FolderDocVersions != null && _FolderDocVersions.IsDirty)
			{
				foreach (FolderDocVersion tmp in _FolderDocVersions)
				{
					if (tmp.IsDirty) refreshFolderDocVersions.Add(tmp);
				}
			}
			if (_ChildFolders != null && _ChildFolders.IsDirty)
			{
				foreach (Folder tmp in _ChildFolders)
				{
					tmp.AddToRefreshList(refreshFolders, refreshFolderAssignments, refreshFolderDocVersions);
				}
			}
		}
		private void ClearRefreshList()
		{
			_RefreshFolders = new List();
			_RefreshFolderAssignments = new List();
			_RefreshFolderDocVersions = new List();
		}
		private void BuildRefreshList()
		{
			ClearRefreshList();
			AddToRefreshList(_RefreshFolders, _RefreshFolderAssignments, _RefreshFolderDocVersions);
		}
		private void ProcessRefreshList()
		{
			foreach (Folder tmp in _RefreshFolders)
			{
				FolderInfo.Refresh(tmp);
				if (tmp._MyConnection != null) ConnectionInfo.Refresh(tmp._MyConnection);
				if (tmp._MyFormat != null) FormatInfo.Refresh(tmp._MyFormat);
			}
			foreach (FolderAssignment tmp in _RefreshFolderAssignments)
			{
				AssignmentInfo.Refresh(tmp);
			}
			foreach (FolderDocVersion tmp in _RefreshFolderDocVersions)
			{
				DocVersionInfo.Refresh(tmp);
			}
			ClearRefreshList();
		}
		#endregion
		#region Collection
		private static List _CacheList = new List();
		protected static void AddToCache(Folder folder)
		{
			if (!_CacheList.Contains(folder)) _CacheList.Add(folder); // In AddToCache
		}
		protected static void RemoveFromCache(Folder folder)
		{
			while (_CacheList.Contains(folder)) _CacheList.Remove(folder); // In RemoveFromCache
		}
		private static Dictionary> _CacheByPrimaryKey = new Dictionary>();
		private static Dictionary> _CacheByParentID_Name = new Dictionary>();
		private static void ConvertListToDictionary()
		{
			while (_CacheList.Count > 0) // Move Folder(s) from temporary _CacheList to _CacheByPrimaryKey
			{
				Folder tmp = _CacheList[0]; // Get the first Folder
				string pKey = tmp.FolderID.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 Folder
			}
		}
		protected static Folder GetCachedByPrimaryKey(int folderID)
		{
			ConvertListToDictionary();
			string key = folderID.ToString();
			if (_CacheByPrimaryKey.ContainsKey(key)) return _CacheByPrimaryKey[key][0];
			return null;
		}
		protected static Folder 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 _nextFolderID = -1;
		public static int NextFolderID
		{
			get { return _nextFolderID--; }
		}
		private int _FolderID;
		[System.ComponentModel.DataObjectField(true, true)]
		public int FolderID
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("FolderID", true);
				return _FolderID;
			}
		}
		private int _ParentID;
		public int ParentID
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("ParentID", true);
				if (_MyParent != null) _ParentID = _MyParent.FolderID;
				return _ParentID;
			}
		}
		private Folder _MyParent;
		public Folder MyParent
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("MyParent", true);
				if (_MyParent == null && _ParentID != _FolderID) _MyParent = Folder.Get(_ParentID);
				return _MyParent;
			}
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			set
			{
				CanWriteProperty("MyParent", true);
				if (_MyParent != value)
				{
					_MyParent = value;
					_ParentID = value.FolderID;// Update underlying data field
					PropertyHasChanged();
				}
			}
		}
		private int _DBID;
		public int DBID
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("DBID", true);
				if (_MyConnection != null) _DBID = _MyConnection.DBID;
				return _DBID;
			}
		}
		private Connection _MyConnection;
		public Connection MyConnection
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("MyConnection", true);
				if (_MyConnection == null && _DBID != 0) _MyConnection = Connection.Get(_DBID);
				return _MyConnection;
			}
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			set
			{
				CanWriteProperty("MyConnection", true);
				if (_MyConnection != value)
				{
					_MyConnection = value;
					_DBID = value.DBID;// Update underlying data field
					PropertyHasChanged();
				}
			}
		}
		private string _Name = string.Empty;
		public string Name
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("Name", true);
				return _Name;
			}
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			set
			{
				CanWriteProperty("Name", true);
				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
			{
				CanReadProperty("Title", true);
				return _Title;
			}
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			set
			{
				CanWriteProperty("Title", true);
				if (value == null) value = string.Empty;
				if (_Title != value)
				{
					_Title = value;
					PropertyHasChanged();
				}
			}
		}
		private string _ShortName = string.Empty;
		public string ShortName
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("ShortName", true);
				return _ShortName;
			}
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			set
			{
				CanWriteProperty("ShortName", true);
				if (value == null) value = string.Empty;
				if (_ShortName != value)
				{
					_ShortName = value;
					PropertyHasChanged();
				}
			}
		}
		private int? _FormatID;
		public int? FormatID
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("FormatID", true);
				if (_MyFormat != null) _FormatID = _MyFormat.FormatID;
				return _FormatID;
			}
		}
		private Format _MyFormat;
		public Format MyFormat
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("MyFormat", true);
				if (_MyFormat == null && _FormatID != null) _MyFormat = Format.Get((int)_FormatID);
				return _MyFormat;
			}
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			set
			{
				CanWriteProperty("MyFormat", true);
				if ((_MyFormat == null ? _FormatID : (int?)_MyFormat.FormatID) != (value == null ? null : (int?)value.FormatID))
				{
					_MyFormat = value;
					_FormatID = (value == null ? null : (int?)value.FormatID);
					PropertyHasChanged();
				}
			}
		}
		private double? _ManualOrder;
		public double? ManualOrder
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("ManualOrder", true);
				return _ManualOrder;
			}
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			set
			{
				CanWriteProperty("ManualOrder", true);
				if (_ManualOrder != value)
				{
					_ManualOrder = 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 _UsrID = string.Empty;
		public string UsrID
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("UsrID", true);
				return _UsrID;
			}
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			set
			{
				CanWriteProperty("UsrID", true);
				if (value == null) value = string.Empty;
				if (_UsrID != value)
				{
					_UsrID = value;
					PropertyHasChanged();
				}
			}
		}
		private byte[] _LastChanged = new byte[8];//timestamp
		private int _FolderAssignmentCount = 0;
		/// 
		/// Count of FolderAssignments for this Folder
		/// 
		public int FolderAssignmentCount
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("FolderAssignmentCount", true);
				return _FolderAssignmentCount;
			}
		}
		private FolderAssignments _FolderAssignments = null;
		/// 
		/// Related Field
		/// 
		[TypeConverter(typeof(FolderAssignmentsConverter))]
		public FolderAssignments FolderAssignments
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("FolderAssignments", true);
				if (_FolderAssignmentCount < 0 || (_FolderAssignmentCount > 0 && _FolderAssignments == null))
					_FolderAssignments = FolderAssignments.GetByFolderID(FolderID);
				if (_FolderAssignmentCount < 0 )
					_FolderAssignmentCount = _FolderAssignments == null ? 0 : _FolderAssignments.Count;
				if (_FolderAssignments == null)
					_FolderAssignments = FolderAssignments.New();
				return _FolderAssignments;
			}
		}
		public void Reset_FolderAssignments()
		{
			_FolderAssignmentCount = -1;
		}
		private int _FolderDocVersionCount = 0;
		/// 
		/// Count of FolderDocVersions for this Folder
		/// 
		public int FolderDocVersionCount
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("FolderDocVersionCount", true);
				return _FolderDocVersionCount;
			}
		}
		private FolderDocVersions _FolderDocVersions = null;
		/// 
		/// Related Field
		/// 
		[TypeConverter(typeof(FolderDocVersionsConverter))]
		public FolderDocVersions FolderDocVersions
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("FolderDocVersions", true);
				if (_FolderDocVersionCount < 0 || (_FolderDocVersionCount > 0 && _FolderDocVersions == null))
					_FolderDocVersions = FolderDocVersions.GetByFolderID(FolderID);
				if (_FolderDocVersionCount < 0 )
					_FolderDocVersionCount = _FolderDocVersions == null ? 0 : _FolderDocVersions.Count;
				if (_FolderDocVersions == null)
					_FolderDocVersions = FolderDocVersions.New();
				return _FolderDocVersions;
			}
		}
		public void Reset_FolderDocVersions()
		{
			_FolderDocVersionCount = -1;
		}
		private int _ChildFolderCount = 0;
		/// 
		/// Count of ChildFolders for this Folder
		/// 
		public int ChildFolderCount
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("ChildFolderCount", true);
				return _ChildFolderCount;
			}
		}
		private ChildFolders _ChildFolders = null;
		/// 
		/// Related Field
		/// 
		[TypeConverter(typeof(ChildFoldersConverter))]
		public ChildFolders ChildFolders
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("ChildFolders", true);
				if (_ChildFolderCount < 0 || (_ChildFolderCount > 0 && _ChildFolders == null))
					_ChildFolders = ChildFolders.GetByParentID(FolderID);
				if (_ChildFolderCount < 0 )
					_ChildFolderCount = _ChildFolders == null ? 0 : _ChildFolders.Count;
				if (_ChildFolders == null)
					_ChildFolders = ChildFolders.New();
				return _ChildFolders;
			}
		}
		public void Reset_ChildFolders()
		{
			_ChildFolderCount = -1;
		}
		public override bool IsDirty
		{
			get 
			{
				if ( base.IsDirty ) 
					return true;
				return IsDirtyList(new List