// ========================================================================
// 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 FolderInfoEvent(object sender);
	/// 
	///	FolderInfo Generated by MyGeneration using the CSLA Object Mapping template
	/// 
	[Serializable()]
	[TypeConverter(typeof(FolderInfoConverter))]
	public partial class FolderInfo : ReadOnlyBase, IDisposable
	{
		public event FolderInfoEvent 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 (FolderInfo tmp in _AllList)
			{
				_AllByPrimaryKey[tmp.FolderID.ToString()]=tmp; // Primary Key
				remove.Add(tmp);
			}
			foreach (FolderInfo tmp in remove)
				_AllList.Remove(tmp);
		}
		internal static void AddList(FolderInfoList lst)
		{
			foreach (FolderInfo item in lst) _AllList.Add(item);
		}
		public static FolderInfo GetExistingByPrimaryKey(int folderID)
		{
			ConvertListToDictionary();
			string key = folderID.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 Folder _Editable;
		private IVEHasBrokenRules HasBrokenRules
		{
			get
			{
				IVEHasBrokenRules hasBrokenRules = null;
				if (_Editable != null)
					hasBrokenRules = _Editable.HasBrokenRules;
				return hasBrokenRules;
			}
		}
		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 FolderInfo _MyParent;
		public FolderInfo MyParent
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("MyParent",true);
				if (_MyParent == null && _ParentID != _FolderID) _MyParent = FolderInfo.Get(_ParentID);
				return _MyParent;
			}
		}
		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 ConnectionInfo _MyConnection;
		public ConnectionInfo MyConnection
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("MyConnection",true);
				if (_MyConnection == null && _DBID != 0) _MyConnection = ConnectionInfo.Get(_DBID);
				return _MyConnection;
			}
		}
		private string _Name = string.Empty;
		public string Name
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("Name",true);
				return _Name;
			}
		}
		private string _Title = string.Empty;
		public string Title
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("Title",true);
				return _Title;
			}
		}
		private string _ShortName = string.Empty;
		public string ShortName
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("ShortName",true);
				return _ShortName;
			}
		}
		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 FormatInfo _MyFormat;
		public FormatInfo MyFormat
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("MyFormat",true);
				if (_MyFormat == null && _FormatID != null) _MyFormat = FormatInfo.Get((int)_FormatID);
				return _MyFormat;
			}
		}
		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 _UsrID = string.Empty;
		public string UsrID
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("UsrID",true);
				return _UsrID;
			}
		}
		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 AssignmentInfoList _FolderAssignments = null;
		[TypeConverter(typeof(AssignmentInfoListConverter))]
		public AssignmentInfoList FolderAssignments
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("FolderAssignments",true);
				if (_FolderAssignmentCount > 0 && _FolderAssignments == null)
					_FolderAssignments = AssignmentInfoList.GetByFolderID(_FolderID);
				return _FolderAssignments;
			}
		}
		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 DocVersionInfoList _FolderDocVersions = null;
		[TypeConverter(typeof(DocVersionInfoListConverter))]
		public DocVersionInfoList FolderDocVersions
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("FolderDocVersions",true);
				if (_FolderDocVersionCount > 0 && _FolderDocVersions == null)
					_FolderDocVersions = DocVersionInfoList.GetByFolderID(_FolderID);
				return _FolderDocVersions;
			}
		}
		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 FolderInfoList _ChildFolders = null;
		[TypeConverter(typeof(FolderInfoListConverter))]
		public FolderInfoList ChildFolders
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("ChildFolders",true);
				if (_ChildFolderCount > 0 && _ChildFolders == null)
					_ChildFolders = FolderInfoList.GetChildren(_FolderID);
				return _ChildFolders;
			}
		}
		// TODO: Replace base FolderInfo.ToString function as necessary
		/// 
		/// Overrides Base ToString
		/// 
		/// A string representation of current FolderInfo
		//public override string ToString()
		//{
		//  return base.ToString();
		//}
		// TODO: Check FolderInfo.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 FolderInfo
		protected override object GetIdValue()
		{
			return _FolderID;
		}
		#endregion
		#region Factory Methods
		private FolderInfo()
		{/* require use of factory methods */
			_AllList.Add(this);
		}
		public void Dispose()
		{
			_AllList.Remove(this);
			_AllByPrimaryKey.Remove(FolderID.ToString());
		}
		public virtual Folder Get()
		{
			return _Editable = Folder.Get(_FolderID);
		}
		public static void Refresh(Folder tmp)
		{
			FolderInfo tmpInfo = GetExistingByPrimaryKey(tmp.FolderID);
			if (tmpInfo == null) return;
			tmpInfo.RefreshFields(tmp);
		}
		private void RefreshFields(Folder tmp)
		{
			_ParentID = tmp.ParentID;
			_DBID = tmp.DBID;
			_Name = tmp.Name;
			_Title = tmp.Title;
			_ShortName = tmp.ShortName;
			_FormatID = tmp.FormatID;
			_Config = tmp.Config;
			_DTS = tmp.DTS;
			_UsrID = tmp.UsrID;
			_FolderInfoExtension.Refresh(this);
			_MyParent = null;
			_MyConnection = null;
			_MyFormat = null;
			OnChange();// raise an event
		}
		public static void Refresh(ConnectionFolder tmp)
		{
			FolderInfo tmpInfo = GetExistingByPrimaryKey(tmp.FolderID);
			if (tmpInfo == null) return;
			tmpInfo.RefreshFields(tmp);
		}
		private void RefreshFields(ConnectionFolder tmp)
		{
			_ParentID = tmp.ParentID;
			_Name = tmp.Name;
			_Title = tmp.Title;
			_ShortName = tmp.ShortName;
			_FormatID = tmp.FormatID;
			_Config = tmp.Config;
			_DTS = tmp.DTS;
			_UsrID = tmp.UsrID;
			_FolderInfoExtension.Refresh(this);
			_MyParent = null;
			_MyConnection = null;
			_MyFormat = null;
			OnChange();// raise an event
		}
		public static void Refresh(FormatFolder tmp)
		{
			FolderInfo tmpInfo = GetExistingByPrimaryKey(tmp.FolderID);
			if (tmpInfo == null) return;
			tmpInfo.RefreshFields(tmp);
		}
		private void RefreshFields(FormatFolder tmp)
		{
			_ParentID = tmp.ParentID;
			_DBID = tmp.DBID;
			_Name = tmp.Name;
			_Title = tmp.Title;
			_ShortName = tmp.ShortName;
			_Config = tmp.Config;
			_DTS = tmp.DTS;
			_UsrID = tmp.UsrID;
			_FolderInfoExtension.Refresh(this);
			_MyParent = null;
			_MyConnection = null;
			_MyFormat = null;
			OnChange();// raise an event
		}
		public static FolderInfo Get(int folderID)
		{
			//if (!CanGetObject())
			//  throw new System.Security.SecurityException("User not authorized to view a Folder");
			try
			{
				FolderInfo tmp = GetExistingByPrimaryKey(folderID);
				if (tmp == null)
				{
					tmp = DataPortal.Fetch(new PKCriteria(folderID));
					_AllList.Add(tmp);
				}
				if (tmp.ErrorMessage == "No Record Found") tmp = null;
				return tmp;
			}
			catch (Exception ex)
			{
				throw new DbCslaException("Error on FolderInfo.Get", ex);
			}
		}
		#endregion
		#region Data Access Portal
		internal FolderInfo(SafeDataReader dr)
		{
			if(_MyLog.IsDebugEnabled)_MyLog.DebugFormat("[{0}] FolderInfo.Constructor", GetHashCode());
			try
			{
				ReadData(dr);
			}
			catch (Exception ex)
			{
				if(_MyLog.IsErrorEnabled)_MyLog.Error("FolderInfo.Constructor", ex);
				throw new DbCslaException("FolderInfo.Constructor", ex);
			}
		}
		[Serializable()]
		protected class PKCriteria
		{
			private int _FolderID;
			public int FolderID
			{ get { return _FolderID; } }
			public PKCriteria(int folderID)
			{
				_FolderID = folderID;
			}
		}
		private void ReadData(SafeDataReader dr)
		{
			if(_MyLog.IsDebugEnabled)_MyLog.DebugFormat("[{0}] FolderInfo.ReadData", GetHashCode());
			try
			{
				_FolderID = dr.GetInt32("FolderID");
				_ParentID = dr.GetInt32("ParentID");
				_DBID = dr.GetInt32("DBID");
				_Name = dr.GetString("Name");
				_Title = dr.GetString("Title");
				_ShortName = dr.GetString("ShortName");
				_FormatID = (int?)dr.GetValue("FormatID");
				_Config = dr.GetString("Config");
				_DTS = dr.GetDateTime("DTS");
				_UsrID = dr.GetString("UsrID");
				_FolderAssignmentCount = dr.GetInt32("AssignmentCount");
				_FolderDocVersionCount = dr.GetInt32("DocVersionCount");
				_ChildFolderCount = dr.GetInt32("ChildCount");
			}
			catch (Exception ex)
			{
				if(_MyLog.IsErrorEnabled)_MyLog.Error("FolderInfo.ReadData", ex);
				_ErrorMessage = ex.Message;
				throw new DbCslaException("FolderInfo.ReadData", ex);
			}
		}
		private void DataPortal_Fetch(PKCriteria criteria)
		{
			if(_MyLog.IsDebugEnabled)_MyLog.DebugFormat("[{0}] FolderInfo.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 = "getFolder";
						cm.Parameters.AddWithValue("@FolderID", criteria.FolderID);
						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("FolderInfo.DataPortal_Fetch", ex);
				_ErrorMessage = ex.Message;
				throw new DbCslaException("FolderInfo.DataPortal_Fetch", ex);
			}
		}
		#endregion
		// Standard Refresh
		#region extension
		FolderInfoExtension _FolderInfoExtension = new FolderInfoExtension();
		[Serializable()]
		partial class FolderInfoExtension : extensionBase {}
		[Serializable()]
		class extensionBase
		{
			// Default Refresh
			public virtual void Refresh(FolderInfo tmp) { }
		}
		#endregion
	} // Class
	#region Converter
	internal class FolderInfoConverter : ExpandableObjectConverter
	{
		public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destType)
		{
			if (destType == typeof(string) && value is FolderInfo)
			{
				// Return the ToString value
				return ((FolderInfo)value).ToString();
			}
			return base.ConvertTo(context, culture, value, destType);
		}
	}
	#endregion
} // Namespace