// ========================================================================
// 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
{
	/// 
	///	User Generated by MyGeneration using the CSLA Object Mapping template
	/// 
	[Serializable()]
	[TypeConverter(typeof(UserConverter))]
	public partial class User : BusinessBase, IDisposable, IVEHasBrokenRules
	{
		#region Log4Net
		private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
		#endregion
		#region Refresh
		private List _RefreshUsers = new List();
		private List _RefreshUserMemberships = new List();
		private void AddToRefreshList(List refreshUsers, List refreshUserMemberships)
		{
			if (IsDirty)
				refreshUsers.Add(this);
			if (_UserMemberships != null && _UserMemberships.IsDirty)
			{
				foreach (UserMembership tmp in _UserMemberships)
				{
					if (tmp.IsDirty) refreshUserMemberships.Add(tmp);
				}
			}
		}
		private void ClearRefreshList()
		{
			_RefreshUsers = new List();
			_RefreshUserMemberships = new List();
		}
		private void BuildRefreshList()
		{
			ClearRefreshList();
			AddToRefreshList(_RefreshUsers, _RefreshUserMemberships);
		}
		private void ProcessRefreshList()
		{
			foreach (User tmp in _RefreshUsers)
			{
				UserInfo.Refresh(tmp);
			}
			foreach (UserMembership tmp in _RefreshUserMemberships)
			{
				MembershipInfo.Refresh(tmp);
			}
			ClearRefreshList();
		}
		#endregion
		#region Collection
		private static List _CacheList = new List();
		protected static void AddToCache(User user)
		{
			if (!_CacheList.Contains(user)) _CacheList.Add(user); // In AddToCache
		}
		protected static void RemoveFromCache(User user)
		{
			while (_CacheList.Contains(user)) _CacheList.Remove(user); // In RemoveFromCache
		}
		private static Dictionary> _CacheByPrimaryKey = new Dictionary>();
		private static void ConvertListToDictionary()
		{
			while (_CacheList.Count > 0) // Move User(s) from temporary _CacheList to _CacheByPrimaryKey
			{
				User tmp = _CacheList[0]; // Get the first User
				string pKey = tmp.UID.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 User
			}
		}
		protected static User GetCachedByPrimaryKey(int uid)
		{
			ConvertListToDictionary();
			string key = uid.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 _nextUID = -1;
		public static int NextUID
		{
			get { return _nextUID--; }
		}
		private int _UID;
		[System.ComponentModel.DataObjectField(true, true)]
		public int UID
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("UID", true);
				return _UID;
			}
		}
		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 string _FirstName = string.Empty;
		public string FirstName
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("FirstName", true);
				return _FirstName;
			}
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			set
			{
				CanWriteProperty("FirstName", true);
				if (value == null) value = string.Empty;
				if (_FirstName != value)
				{
					_FirstName = value;
					PropertyHasChanged();
				}
			}
		}
		private string _MiddleName = string.Empty;
		public string MiddleName
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("MiddleName", true);
				return _MiddleName;
			}
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			set
			{
				CanWriteProperty("MiddleName", true);
				if (value == null) value = string.Empty;
				if (_MiddleName != value)
				{
					_MiddleName = value;
					PropertyHasChanged();
				}
			}
		}
		private string _LastName = string.Empty;
		public string LastName
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("LastName", true);
				return _LastName;
			}
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			set
			{
				CanWriteProperty("LastName", true);
				if (value == null) value = string.Empty;
				if (_LastName != value)
				{
					_LastName = value;
					PropertyHasChanged();
				}
			}
		}
		private string _Suffix = string.Empty;
		public string Suffix
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("Suffix", true);
				return _Suffix;
			}
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			set
			{
				CanWriteProperty("Suffix", true);
				if (value == null) value = string.Empty;
				if (_Suffix != value)
				{
					_Suffix = value;
					PropertyHasChanged();
				}
			}
		}
		private string _CourtesyTitle = string.Empty;
		public string CourtesyTitle
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("CourtesyTitle", true);
				return _CourtesyTitle;
			}
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			set
			{
				CanWriteProperty("CourtesyTitle", true);
				if (value == null) value = string.Empty;
				if (_CourtesyTitle != value)
				{
					_CourtesyTitle = value;
					PropertyHasChanged();
				}
			}
		}
		private string _PhoneNumber = string.Empty;
		public string PhoneNumber
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("PhoneNumber", true);
				return _PhoneNumber;
			}
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			set
			{
				CanWriteProperty("PhoneNumber", true);
				if (value == null) value = string.Empty;
				if (_PhoneNumber != value)
				{
					_PhoneNumber = value;
					PropertyHasChanged();
				}
			}
		}
		private string _CFGName = string.Empty;
		public string CFGName
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("CFGName", true);
				return _CFGName;
			}
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			set
			{
				CanWriteProperty("CFGName", true);
				if (value == null) value = string.Empty;
				if (_CFGName != value)
				{
					_CFGName = value;
					PropertyHasChanged();
				}
			}
		}
		private string _UserLogin = string.Empty;
		public string UserLogin
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("UserLogin", true);
				return _UserLogin;
			}
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			set
			{
				CanWriteProperty("UserLogin", true);
				if (value == null) value = string.Empty;
				if (_UserLogin != value)
				{
					_UserLogin = value;
					PropertyHasChanged();
				}
			}
		}
		private string _UserName = string.Empty;
		public string UserName
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("UserName", true);
				return _UserName;
			}
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			set
			{
				CanWriteProperty("UserName", true);
				if (value == null) value = string.Empty;
				if (_UserName != value)
				{
					_UserName = 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 _UserMembershipCount = 0;
		/// 
		/// Count of UserMemberships for this User
		/// 
		public int UserMembershipCount
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("UserMembershipCount", true);
				return _UserMembershipCount;
			}
		}
		private UserMemberships _UserMemberships = null;
		/// 
		/// Related Field
		/// 
		[TypeConverter(typeof(UserMembershipsConverter))]
		public UserMemberships UserMemberships
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("UserMemberships", true);
				if (_UserMembershipCount < 0 || (_UserMembershipCount > 0 && _UserMemberships == null))
					_UserMemberships = UserMemberships.GetByUID(UID);
				if (_UserMembershipCount < 0 )
					_UserMembershipCount = _UserMemberships == null ? 0 : _UserMemberships.Count;
				if (_UserMemberships == null)
					_UserMemberships = UserMemberships.New();
				return _UserMemberships;
			}
		}
		public void Reset_UserMemberships()
		{
			_UserMembershipCount = -1;
		}
		public override bool IsDirty
		{
			get 
			{
				if ( base.IsDirty ) 
					return true;
				return IsDirtyList(new List