// ========================================================================
// 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;
namespace VEPROMS.CSLA.Library
{
	public delegate void PartAuditInfoEvent(object sender);
	/// 
	///	PartAuditInfo Generated by MyGeneration using the CSLA Object Mapping template
	/// 
	[Serializable()]
	[TypeConverter(typeof(PartAuditInfoConverter))]
	public partial class PartAuditInfo : ReadOnlyBase, IDisposable
	{
		public event PartAuditInfoEvent Changed;
		private void OnChange()
		{
			if (Changed != null) Changed(this);
		}
		#region Log4Net
		private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
		#endregion
		#region Collection
		private static List _CacheList = new List();
		protected static void AddToCache(PartAuditInfo partAuditInfo)
		{
			if (!_CacheList.Contains(partAuditInfo)) _CacheList.Add(partAuditInfo); // In AddToCache
		}
		protected static void RemoveFromCache(PartAuditInfo partAuditInfo)
		{
			while (_CacheList.Contains(partAuditInfo)) _CacheList.Remove(partAuditInfo); // In RemoveFromCache
		}
		private static Dictionary> _CacheByPrimaryKey = new Dictionary>();
		private static void ConvertListToDictionary()
		{
			while (_CacheList.Count > 0) // Move PartAuditInfo(s) from temporary _CacheList to _CacheByPrimaryKey
			{
				PartAuditInfo tmp = _CacheList[0]; // Get the first PartAuditInfo
				string pKey = tmp.AuditID.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 PartAuditInfo
			}
		}
		internal static void AddList(PartAuditInfoList lst)
		{
			foreach (PartAuditInfo item in lst) AddToCache(item);
		}
		protected static PartAuditInfo GetCachedByPrimaryKey(long auditID)
		{
			ConvertListToDictionary();
			string key = auditID.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; }
		}
		protected PartAudit _Editable;
		private IVEHasBrokenRules HasBrokenRules
		{
			get
			{
				IVEHasBrokenRules hasBrokenRules = null;
				if (_Editable != null)
					hasBrokenRules = _Editable.HasBrokenRules;
				return hasBrokenRules;
			}
		}
		private long _AuditID;
		[System.ComponentModel.DataObjectField(true, true)]
		public long AuditID
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("AuditID", true);
				return _AuditID;
			}
		}
		private int _ContentID;
		public int ContentID
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("ContentID", true);
				return _ContentID;
			}
		}
		private int _FromType;
		public int FromType
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("FromType", true);
				return _FromType;
			}
		}
		private int _ItemID;
		public int ItemID
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("ItemID", true);
				return _ItemID;
			}
		}
		private DateTime _DTS = new DateTime();
		public DateTime DTS
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("DTS", true);
				return _DTS;
			}
		}
		private string _UserID = string.Empty;
		public string UserID
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("UserID", true);
				return _UserID;
			}
		}
		private int _DeleteStatus;
		public int DeleteStatus
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("DeleteStatus", true);
				return _DeleteStatus;
			}
		}
		// CSLATODO: Replace base PartAuditInfo.ToString function as necessary
		/// 
		/// Overrides Base ToString
		/// 
		/// A string representation of current PartAuditInfo
		//public override string ToString()
		//{
		//  return base.ToString();
		//}
		// CSLATODO: Check PartAuditInfo.GetIdValue to assure that the ID returned is unique
		/// 
		/// Overrides Base GetIdValue - Used internally by CSLA to determine equality
		/// 
		/// A Unique ID for the current PartAuditInfo
		protected override object GetIdValue()
		{
			return MyPartAuditInfoUnique; // Absolutely Unique ID
		}
		#endregion
		#region Factory Methods
		private static int _PartAuditInfoUnique = 0;
		private static int PartAuditInfoUnique
		{ get { return ++_PartAuditInfoUnique; } }
		private int _MyPartAuditInfoUnique = PartAuditInfoUnique;
		public int MyPartAuditInfoUnique // Absolutely Unique ID - Info
		{ get { return _MyPartAuditInfoUnique; } }
		protected PartAuditInfo()
		{/* require use of factory methods */
			AddToCache(this);
		}
		private bool _Disposed = false;
		private static int _CountCreated = 0;
		private static int _CountDisposed = 0;
		private static int _CountFinalized = 0;
		private static int IncrementCountCreated
		{ get { return ++_CountCreated; } }
		private int _CountWhenCreated = IncrementCountCreated;
		public static int CountCreated
		{ get { return _CountCreated; } }
		public static int CountNotDisposed
		{ get { return _CountCreated - _CountDisposed; } }
		public static int CountNotFinalized
		{ get { return _CountCreated - _CountFinalized; } }
		~PartAuditInfo()
		{
			_CountFinalized++;
		}
		public void Dispose()
		{
			if (_Disposed) return;
			_CountDisposed++;
			_Disposed = true;
			RemoveFromCache(this);
			if (!_CacheByPrimaryKey.ContainsKey(AuditID.ToString())) return;
			List listPartAuditInfo = _CacheByPrimaryKey[AuditID.ToString()]; // Get the list of items
			while (listPartAuditInfo.Contains(this)) listPartAuditInfo.Remove(this); // Remove the item from the list
			if (listPartAuditInfo.Count == 0) // If there are no items left in the list
				_CacheByPrimaryKey.Remove(AuditID.ToString()); // remove the list
		}
		public virtual PartAudit Get()
		{
			return _Editable = PartAudit.Get(_AuditID);
		}
		public static void Refresh(PartAudit tmp)
		{
			string key = tmp.AuditID.ToString();
			ConvertListToDictionary();
			if (_CacheByPrimaryKey.ContainsKey(key))
				foreach (PartAuditInfo tmpInfo in _CacheByPrimaryKey[key])
					tmpInfo.RefreshFields(tmp);
		}
		protected virtual void RefreshFields(PartAudit tmp)
		{
			_ContentID = tmp.ContentID;
			_FromType = tmp.FromType;
			_ItemID = tmp.ItemID;
			_DTS = tmp.DTS;
			_UserID = tmp.UserID;
			_DeleteStatus = tmp.DeleteStatus;
			_PartAuditInfoExtension.Refresh(this);
			OnChange();// raise an event
		}
		public static PartAuditInfo Get(long auditID)
		{
			//if (!CanGetObject())
			//  throw new System.Security.SecurityException("User not authorized to view a PartAudit");
			try
			{
				PartAuditInfo tmp = GetCachedByPrimaryKey(auditID);
				if (tmp == null)
				{
					tmp = DataPortal.Fetch(new PKCriteria(auditID));
					AddToCache(tmp);
				}
				if (tmp.ErrorMessage == "No Record Found")
				{
					tmp.Dispose(); // Clean-up PartAuditInfo
					tmp = null;
				}
				return tmp;
			}
			catch (Exception ex)
			{
				throw new DbCslaException("Error on PartAuditInfo.Get", ex);
			}
		}
		#endregion
		#region Data Access Portal
		internal PartAuditInfo(SafeDataReader dr)
		{
			if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] PartAuditInfo.Constructor", GetHashCode());
			try
			{
				ReadData(dr);
			}
			catch (Exception ex)
			{
				if (_MyLog.IsErrorEnabled) _MyLog.Error("PartAuditInfo.Constructor", ex);
				throw new DbCslaException("PartAuditInfo.Constructor", ex);
			}
		}
		[Serializable()]
		protected class PKCriteria
		{
			private long _AuditID;
			public long AuditID
			{ get { return _AuditID; } }
			public PKCriteria(long auditID)
			{
				_AuditID = auditID;
			}
		}
		private void ReadData(SafeDataReader dr)
		{
			if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] PartAuditInfo.ReadData", GetHashCode());
			try
			{
				_AuditID = dr.GetInt64("AuditID");
				_ContentID = dr.GetInt32("ContentID");
				_FromType = dr.GetInt32("FromType");
				_ItemID = dr.GetInt32("ItemID");
				_DTS = dr.GetDateTime("DTS");
				_UserID = dr.GetString("UserID");
				_DeleteStatus = dr.GetInt32("DeleteStatus");
			}
			catch (Exception ex)
			{
				if (_MyLog.IsErrorEnabled) _MyLog.Error("PartAuditInfo.ReadData", ex);
				_ErrorMessage = ex.Message;
				throw new DbCslaException("PartAuditInfo.ReadData", ex);
			}
		}
		private void DataPortal_Fetch(PKCriteria criteria)
		{
			if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] PartAuditInfo.DataPortal_Fetch", GetHashCode());
			try
			{
				using (SqlConnection cn = Database.VEPROMS_SqlConnection)
				{
					ApplicationContext.LocalContext["cn"] = cn;
					using (SqlCommand cm = cn.CreateCommand())
					{
						cm.CommandType = CommandType.StoredProcedure;
						cm.CommandText = "getPartAudit";
						cm.Parameters.AddWithValue("@AuditID", criteria.AuditID);
						cm.CommandTimeout = Database.DefaultTimeout;
						using (SafeDataReader dr = new SafeDataReader(cm.ExecuteReader()))
						{
							if (!dr.Read())
							{
								_ErrorMessage = "No Record Found";
								return;
							}
							ReadData(dr);
						}
					}
					// removing of item only needed for local data portal
					if (ApplicationContext.ExecutionLocation == ApplicationContext.ExecutionLocations.Client)
						ApplicationContext.LocalContext.Remove("cn");
				}
			}
			catch (Exception ex)
			{
				if (_MyLog.IsErrorEnabled) _MyLog.Error("PartAuditInfo.DataPortal_Fetch", ex);
				_ErrorMessage = ex.Message;
				throw new DbCslaException("PartAuditInfo.DataPortal_Fetch", ex);
			}
		}
		#endregion
		// Standard Refresh
		#region extension
		PartAuditInfoExtension _PartAuditInfoExtension = new PartAuditInfoExtension();
		[Serializable()]
		partial class PartAuditInfoExtension : extensionBase { }
		[Serializable()]
		class extensionBase
		{
			// Default Refresh
			public virtual void Refresh(PartAuditInfo tmp) { }
		}
		#endregion
	} // Class
	#region Converter
	internal class PartAuditInfoConverter : ExpandableObjectConverter
	{
		public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destType)
		{
			if (destType == typeof(string) && value is PartAuditInfo)
			{
				// Return the ToString value
				return ((PartAuditInfo)value).ToString();
			}
			return base.ConvertTo(context, culture, value, destType);
		}
	}
	#endregion
} // Namespace