// ========================================================================
// 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 TransitionInfoEvent(object sender);
	/// 
	///	TransitionInfo Generated by MyGeneration using the CSLA Object Mapping template
	/// 
	[Serializable()]
	[TypeConverter(typeof(TransitionInfoConverter))]
	public partial class TransitionInfo : ReadOnlyBase, IDisposable
	{
		public event TransitionInfoEvent 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
		protected static List _AllList = new List();
		private static Dictionary _AllByPrimaryKey = new Dictionary();
		private static void ConvertListToDictionary()
		{
			List remove = new List();
			foreach (TransitionInfo tmp in _AllList)
			{
				_AllByPrimaryKey[tmp.TransitionID.ToString()]=tmp; // Primary Key
				remove.Add(tmp);
			}
			foreach (TransitionInfo tmp in remove)
				_AllList.Remove(tmp);
		}
		internal static void AddList(TransitionInfoList lst)
		{
			foreach (TransitionInfo item in lst) _AllList.Add(item);
		}
		public static TransitionInfo GetExistingByPrimaryKey(int transitionID)
		{
			ConvertListToDictionary();
			string key = transitionID.ToString();
			if (_AllByPrimaryKey.ContainsKey(key)) return _AllByPrimaryKey[key]; 
			return null;
		}
		#endregion
		#region Business Methods
		private string _ErrorMessage = string.Empty;
		public string ErrorMessage
		{
			get { return _ErrorMessage; }
		}
		protected Transition _Editable;
		private IVEHasBrokenRules HasBrokenRules
		{
			get
			{
				IVEHasBrokenRules hasBrokenRules = null;
				if (_Editable != null)
					hasBrokenRules = _Editable.HasBrokenRules;
				return hasBrokenRules;
			}
		}
		private int _TransitionID;
		[System.ComponentModel.DataObjectField(true, true)]
		public int TransitionID
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("TransitionID",true);
				return _TransitionID;
			}
		}
		private int _FromID;
		public int FromID
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("FromID",true);
				if (_MyContent != null) _FromID = _MyContent.ContentID;
				return _FromID;
			}
		}
		private ContentInfo _MyContent;
		public ContentInfo MyContent
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("MyContent",true);
				if (_MyContent == null && _FromID != 0) _MyContent = ContentInfo.Get(_FromID);
				return _MyContent;
			}
		}
		private int _ToID;
		/// 
		/// StructureID
		/// 
		public int ToID
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("ToID",true);
				if (_MyItemToID != null) _ToID = _MyItemToID.ItemID;
				return _ToID;
			}
		}
		private ItemInfo _MyItemToID;
		public ItemInfo MyItemToID
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("MyItemToID",true);
				if (_MyItemToID == null && _ToID != 0) _MyItemToID = ItemInfo.Get(_ToID);
				return _MyItemToID;
			}
		}
		private int _RangeID;
		public int RangeID
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("RangeID",true);
				if (_MyItemRangeID != null) _RangeID = _MyItemRangeID.ItemID;
				return _RangeID;
			}
		}
		private ItemInfo _MyItemRangeID;
		public ItemInfo MyItemRangeID
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("MyItemRangeID",true);
				if (_MyItemRangeID == null && _RangeID != 0) _MyItemRangeID = ItemInfo.Get(_RangeID);
				return _MyItemRangeID;
			}
		}
		private int _TranType;
		public int TranType
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("TranType",true);
				return _TranType;
			}
		}
		private string _Config = string.Empty;
		public string Config
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("Config",true);
				return _Config;
			}
		}
		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 _TransitionZTransitionCount = 0;
		/// 
		/// Count of TransitionZTransitions for this Transition
		/// 
		public int TransitionZTransitionCount
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("TransitionZTransitionCount",true);
				return _TransitionZTransitionCount;
			}
		}
		private ZTransitionInfo _MyZTransition = null;
		[TypeConverter(typeof(ZTransitionInfoConverter))]
		public ZTransitionInfo MyZTransition
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("MyZTransition",true);
				if (_TransitionZTransitionCount > 0 && _MyZTransition == null)
					_MyZTransition = ZTransitionInfo.Get(_TransitionID);
				return _MyZTransition;
			}
		}
		// TODO: Replace base TransitionInfo.ToString function as necessary
		/// 
		/// Overrides Base ToString
		/// 
		/// A string representation of current TransitionInfo
		//public override string ToString()
		//{
		//  return base.ToString();
		//}
		// TODO: Check TransitionInfo.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 TransitionInfo
		protected override object GetIdValue()
		{
			return _TransitionID;
		}
		#endregion
		#region Factory Methods
		private TransitionInfo()
		{/* require use of factory methods */
			_AllList.Add(this);
		}
		public void Dispose()
		{
			_AllList.Remove(this);
			_AllByPrimaryKey.Remove(TransitionID.ToString());
		}
		public virtual Transition Get()
		{
			return _Editable = Transition.Get(_TransitionID);
		}
		public static void Refresh(Transition tmp)
		{
			TransitionInfo tmpInfo = GetExistingByPrimaryKey(tmp.TransitionID);
			if (tmpInfo == null) return;
			tmpInfo.RefreshFields(tmp);
		}
		private void RefreshFields(Transition tmp)
		{
			_FromID = tmp.FromID;
			_ToID = tmp.ToID;
			_RangeID = tmp.RangeID;
			_TranType = tmp.TranType;
			_Config = tmp.Config;
			_DTS = tmp.DTS;
			_UserID = tmp.UserID;
			_TransitionInfoExtension.Refresh(this);
			_MyContent = null;
			_MyItemToID = null;
			_MyItemRangeID = null;
			_MyZTransition = null;
			OnChange();// raise an event
		}
		public static void Refresh(ContentTransition tmp)
		{
			TransitionInfo tmpInfo = GetExistingByPrimaryKey(tmp.TransitionID);
			if (tmpInfo == null) return;
			tmpInfo.RefreshFields(tmp);
		}
		private void RefreshFields(ContentTransition tmp)
		{
			_ToID = tmp.ToID;
			_RangeID = tmp.RangeID;
			_TranType = tmp.TranType;
			_Config = tmp.Config;
			_DTS = tmp.DTS;
			_UserID = tmp.UserID;
			_TransitionInfoExtension.Refresh(this);
			_MyContent = null;
			_MyItemToID = null;
			_MyItemRangeID = null;
			_MyZTransition = null;
			OnChange();// raise an event
		}
		public static void Refresh(ItemTransition_RangeID tmp)
		{
			TransitionInfo tmpInfo = GetExistingByPrimaryKey(tmp.TransitionID);
			if (tmpInfo == null) return;
			tmpInfo.RefreshFields(tmp);
		}
		private void RefreshFields(ItemTransition_RangeID tmp)
		{
			_FromID = tmp.FromID;
			_ToID = tmp.ToID;
			_TranType = tmp.TranType;
			_Config = tmp.Config;
			_DTS = tmp.DTS;
			_UserID = tmp.UserID;
			_TransitionInfoExtension.Refresh(this);
			_MyContent = null;
			_MyItemToID = null;
			_MyItemRangeID = null;
			_MyZTransition = null;
			OnChange();// raise an event
		}
		public static void Refresh(ItemTransition_ToID tmp)
		{
			TransitionInfo tmpInfo = GetExistingByPrimaryKey(tmp.TransitionID);
			if (tmpInfo == null) return;
			tmpInfo.RefreshFields(tmp);
		}
		private void RefreshFields(ItemTransition_ToID tmp)
		{
			_FromID = tmp.FromID;
			_RangeID = tmp.RangeID;
			_TranType = tmp.TranType;
			_Config = tmp.Config;
			_DTS = tmp.DTS;
			_UserID = tmp.UserID;
			_TransitionInfoExtension.Refresh(this);
			_MyContent = null;
			_MyItemToID = null;
			_MyItemRangeID = null;
			_MyZTransition = null;
			OnChange();// raise an event
		}
		public static TransitionInfo Get(int transitionID)
		{
			//if (!CanGetObject())
			//  throw new System.Security.SecurityException("User not authorized to view a Transition");
			try
			{
				TransitionInfo tmp = GetExistingByPrimaryKey(transitionID);
				if (tmp == null)
				{
					tmp = DataPortal.Fetch(new PKCriteria(transitionID));
					_AllList.Add(tmp);
				}
				if (tmp.ErrorMessage == "No Record Found") tmp = null;
				return tmp;
			}
			catch (Exception ex)
			{
				throw new DbCslaException("Error on TransitionInfo.Get", ex);
			}
		}
		#endregion
		#region Data Access Portal
		internal TransitionInfo(SafeDataReader dr)
		{
			if(_MyLog.IsDebugEnabled)_MyLog.DebugFormat("[{0}] TransitionInfo.Constructor", GetHashCode());
			try
			{
				ReadData(dr);
			}
			catch (Exception ex)
			{
				if(_MyLog.IsErrorEnabled)_MyLog.Error("TransitionInfo.Constructor", ex);
				throw new DbCslaException("TransitionInfo.Constructor", ex);
			}
		}
		[Serializable()]
		protected class PKCriteria
		{
			private int _TransitionID;
			public int TransitionID
			{ get { return _TransitionID; } }
			public PKCriteria(int transitionID)
			{
				_TransitionID = transitionID;
			}
		}
		private void ReadData(SafeDataReader dr)
		{
			if(_MyLog.IsDebugEnabled)_MyLog.DebugFormat("[{0}] TransitionInfo.ReadData", GetHashCode());
			try
			{
				_TransitionID = dr.GetInt32("TransitionID");
				_FromID = dr.GetInt32("FromID");
				_ToID = dr.GetInt32("ToID");
				_RangeID = dr.GetInt32("RangeID");
				_TranType = dr.GetInt32("TranType");
				_Config = dr.GetString("Config");
				_DTS = dr.GetDateTime("DTS");
				_UserID = dr.GetString("UserID");
				_TransitionZTransitionCount = dr.GetInt32("ZTransitionCount");
			}
			catch (Exception ex)
			{
				if(_MyLog.IsErrorEnabled)_MyLog.Error("TransitionInfo.ReadData", ex);
				_ErrorMessage = ex.Message;
				throw new DbCslaException("TransitionInfo.ReadData", ex);
			}
		}
		private void DataPortal_Fetch(PKCriteria criteria)
		{
			if(_MyLog.IsDebugEnabled)_MyLog.DebugFormat("[{0}] TransitionInfo.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 = "getTransition";
						cm.Parameters.AddWithValue("@TransitionID", criteria.TransitionID);
						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("TransitionInfo.DataPortal_Fetch", ex);
				_ErrorMessage = ex.Message;
				throw new DbCslaException("TransitionInfo.DataPortal_Fetch", ex);
			}
		}
		#endregion
		// Standard Refresh
		#region extension
		TransitionInfoExtension _TransitionInfoExtension = new TransitionInfoExtension();
		[Serializable()]
		partial class TransitionInfoExtension : extensionBase {}
		[Serializable()]
		class extensionBase
		{
			// Default Refresh
			public virtual void Refresh(TransitionInfo tmp) { }
		}
		#endregion
	} // Class
	#region Converter
	internal class TransitionInfoConverter : ExpandableObjectConverter
	{
		public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destType)
		{
			if (destType == typeof(string) && value is TransitionInfo)
			{
				// Return the ToString value
				return ((TransitionInfo)value).ToString();
			}
			return base.ConvertTo(context, culture, value, destType);
		}
	}
	#endregion
} // Namespace