// ========================================================================
// 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 RODbInfoEvent(object sender);
	/// 
	///	RODbInfo Generated by MyGeneration using the CSLA Object Mapping template
	/// 
	[Serializable()]
	[TypeConverter(typeof(RODbInfoConverter))]
	public partial class RODbInfo : ReadOnlyBase, IDisposable
	{
		public event RODbInfoEvent 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(RODbInfo rODbInfo)
		{
			if (!_CacheList.Contains(rODbInfo)) _CacheList.Add(rODbInfo); // In AddToCache
		}
		protected static void RemoveFromCache(RODbInfo rODbInfo)
		{
			while (_CacheList.Contains(rODbInfo)) _CacheList.Remove(rODbInfo); // In RemoveFromCache
		}
		private static Dictionary> _CacheByPrimaryKey = new Dictionary>();
		private static void ConvertListToDictionary()
		{
			List remove = new List();
			foreach (RODbInfo tmp in _CacheList)
			{
				if (!_CacheByPrimaryKey.ContainsKey(tmp.RODbID.ToString()))
				{
					_CacheByPrimaryKey[tmp.RODbID.ToString()] = new List(); // Add new list for PrimaryKey
				}
				_CacheByPrimaryKey[tmp.RODbID.ToString()].Add(tmp); // Add to Primary Key list
				remove.Add(tmp);
			}
			foreach (RODbInfo tmp in remove)
				RemoveFromCache(tmp);
		}
		internal static void AddList(RODbInfoList lst)
		{
			foreach (RODbInfo item in lst) AddToCache(item);
		}
		protected static RODbInfo GetCachedByPrimaryKey(int rODbID)
		{
			ConvertListToDictionary();
			string key = rODbID.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 RODb _Editable;
		private IVEHasBrokenRules HasBrokenRules
		{
			get
			{
				IVEHasBrokenRules hasBrokenRules = null;
				if (_Editable != null)
					hasBrokenRules = _Editable.HasBrokenRules;
				return hasBrokenRules;
			}
		}
		private int _RODbID;
		[System.ComponentModel.DataObjectField(true, true)]
		public int RODbID
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("RODbID", true);
				return _RODbID;
			}
		}
		private string _ROName = string.Empty;
		/// 
		/// Hook for future - to allow the user to select multiple RO Databases assocaiated with on DocVersion
		/// 
		public string ROName
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("ROName", true);
				return _ROName;
			}
		}
		private string _FolderPath = string.Empty;
		/// 
		/// Path to the RO database
		/// 
		public string FolderPath
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("FolderPath", true);
				return _FolderPath;
			}
		}
		private string _DBConnectionString = string.Empty;
		/// 
		/// Connection String - Default could just be the full path and name of the database
		/// 
		public string DBConnectionString
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("DBConnectionString", true);
				return _DBConnectionString;
			}
		}
		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 _RODbROFstCount = 0;
		/// 
		/// Count of RODbROFsts for this RODb
		/// 
		public int RODbROFstCount
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("RODbROFstCount", true);
				return _RODbROFstCount;
			}
		}
		private ROFstInfoList _RODbROFsts = null;
		[TypeConverter(typeof(ROFstInfoListConverter))]
		public ROFstInfoList RODbROFsts
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("RODbROFsts", true);
				if (_RODbROFstCount < 0 || (_RODbROFstCount > 0 && _RODbROFsts == null))
					_RODbROFsts = ROFstInfoList.GetByRODbID(_RODbID);
				if (_RODbROFstCount < 0)
					_RODbROFstCount = _RODbROFsts.Count;
				return _RODbROFsts;
			}
		}
		public void RefreshRODbROFsts()
		{
			ConvertListToDictionary();
			if (_CacheByPrimaryKey.ContainsKey(_RODbID.ToString()))
				foreach (RODbInfo tmp in _CacheByPrimaryKey[_RODbID.ToString()])
					tmp._RODbROFstCount = -1; // This will cause the data to be requeried
		}
		private int _RODbROImageCount = 0;
		/// 
		/// Count of RODbROImages for this RODb
		/// 
		public int RODbROImageCount
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("RODbROImageCount", true);
				return _RODbROImageCount;
			}
		}
		private ROImageInfoList _RODbROImages = null;
		[TypeConverter(typeof(ROImageInfoListConverter))]
		public ROImageInfoList RODbROImages
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("RODbROImages", true);
				if (_RODbROImageCount < 0 || (_RODbROImageCount > 0 && _RODbROImages == null))
					_RODbROImages = ROImageInfoList.GetByRODbID(_RODbID);
				if (_RODbROImageCount < 0)
					_RODbROImageCount = _RODbROImages.Count;
				return _RODbROImages;
			}
		}
		public void RefreshRODbROImages()
		{
			ConvertListToDictionary();
			if (_CacheByPrimaryKey.ContainsKey(_RODbID.ToString()))
				foreach (RODbInfo tmp in _CacheByPrimaryKey[_RODbID.ToString()])
					tmp._RODbROImageCount = -1; // This will cause the data to be requeried
		}
		private int _RODbRoUsageCount = 0;
		/// 
		/// Count of RODbRoUsages for this RODb
		/// 
		public int RODbRoUsageCount
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("RODbRoUsageCount", true);
				return _RODbRoUsageCount;
			}
		}
		private RoUsageInfoList _RODbRoUsages = null;
		[TypeConverter(typeof(RoUsageInfoListConverter))]
		public RoUsageInfoList RODbRoUsages
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("RODbRoUsages", true);
				if (_RODbRoUsageCount < 0 || (_RODbRoUsageCount > 0 && _RODbRoUsages == null))
					_RODbRoUsages = RoUsageInfoList.GetByRODbID(_RODbID);
				if (_RODbRoUsageCount < 0)
					_RODbRoUsageCount = _RODbRoUsages.Count;
				return _RODbRoUsages;
			}
		}
		public void RefreshRODbRoUsages()
		{
			ConvertListToDictionary();
			if (_CacheByPrimaryKey.ContainsKey(_RODbID.ToString()))
				foreach (RODbInfo tmp in _CacheByPrimaryKey[_RODbID.ToString()])
					tmp._RODbRoUsageCount = -1; // This will cause the data to be requeried
		}
		// TODO: Replace base RODbInfo.ToString function as necessary
		/// 
		/// Overrides Base ToString
		/// 
		/// A string representation of current RODbInfo
		//public override string ToString()
		//{
		//  return base.ToString();
		//}
		// TODO: Check RODbInfo.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 RODbInfo
		protected override object GetIdValue()
		{
			return _RODbID;
		}
		#endregion
		#region Factory Methods
		private static int _RODbInfoUnique = 0;
		private static int RODbInfoUnique
		{ get { return ++_RODbInfoUnique; } }
		private int _MyRODbInfoUnique = RODbInfoUnique;
		public int MyRODbInfoUnique
		{ get { return _MyRODbInfoUnique; } }
		protected RODbInfo()
		{/* require use of factory methods */
			AddToCache(this);
		}
		public void Dispose()
		{
			RemoveFromCache(this);
			if (!_CacheByPrimaryKey.ContainsKey(RODbID.ToString())) return;
			List listRODbInfo = _CacheByPrimaryKey[RODbID.ToString()]; // Get the list of items
			while (listRODbInfo.Contains(this)) listRODbInfo.Remove(this); // Remove the item from the list
			if (listRODbInfo.Count == 0) // If there are no items left in the list
				_CacheByPrimaryKey.Remove(RODbID.ToString()); // remove the list
		}
		public virtual RODb Get()
		{
			return _Editable = RODb.Get(_RODbID);
		}
		public static void Refresh(RODb tmp)
		{
			string key = tmp.RODbID.ToString();
			ConvertListToDictionary();
			if (_CacheByPrimaryKey.ContainsKey(key))
				foreach (RODbInfo tmpInfo in _CacheByPrimaryKey[key])
					tmpInfo.RefreshFields(tmp);
		}
		protected virtual void RefreshFields(RODb tmp)
		{
			_ROName = tmp.ROName;
			_FolderPath = tmp.FolderPath;
			_DBConnectionString = tmp.DBConnectionString;
			_DTS = tmp.DTS;
			_UserID = tmp.UserID;
			_RODbInfoExtension.Refresh(this);
			OnChange();// raise an event
		}
		public static RODbInfo Get(int rODbID)
		{
			//if (!CanGetObject())
			//  throw new System.Security.SecurityException("User not authorized to view a RODb");
			try
			{
				RODbInfo tmp = GetCachedByPrimaryKey(rODbID);
				if (tmp == null)
				{
					tmp = DataPortal.Fetch(new PKCriteria(rODbID));
					AddToCache(tmp);
				}
				if (tmp.ErrorMessage == "No Record Found")
				{
					tmp.Dispose(); // Clean-up RODbInfo
					tmp = null;
				}
				return tmp;
			}
			catch (Exception ex)
			{
				throw new DbCslaException("Error on RODbInfo.Get", ex);
			}
		}
		#endregion
		#region Data Access Portal
		internal RODbInfo(SafeDataReader dr)
		{
			if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] RODbInfo.Constructor", GetHashCode());
			try
			{
				ReadData(dr);
			}
			catch (Exception ex)
			{
				if (_MyLog.IsErrorEnabled) _MyLog.Error("RODbInfo.Constructor", ex);
				throw new DbCslaException("RODbInfo.Constructor", ex);
			}
		}
		[Serializable()]
		protected class PKCriteria
		{
			private int _RODbID;
			public int RODbID
			{ get { return _RODbID; } }
			public PKCriteria(int rODbID)
			{
				_RODbID = rODbID;
			}
		}
		private void ReadData(SafeDataReader dr)
		{
			if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] RODbInfo.ReadData", GetHashCode());
			try
			{
				_RODbID = dr.GetInt32("RODbID");
				_ROName = dr.GetString("ROName");
				_FolderPath = dr.GetString("FolderPath");
				_DBConnectionString = dr.GetString("DBConnectionString");
				_DTS = dr.GetDateTime("DTS");
				_UserID = dr.GetString("UserID");
				_RODbROFstCount = dr.GetInt32("ROFstCount");
				_RODbROImageCount = dr.GetInt32("ROImageCount");
				_RODbRoUsageCount = dr.GetInt32("RoUsageCount");
			}
			catch (Exception ex)
			{
				if (_MyLog.IsErrorEnabled) _MyLog.Error("RODbInfo.ReadData", ex);
				_ErrorMessage = ex.Message;
				throw new DbCslaException("RODbInfo.ReadData", ex);
			}
		}
		private void DataPortal_Fetch(PKCriteria criteria)
		{
			if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] RODbInfo.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 = "getRODb";
						cm.Parameters.AddWithValue("@RODbID", criteria.RODbID);
						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("RODbInfo.DataPortal_Fetch", ex);
				_ErrorMessage = ex.Message;
				throw new DbCslaException("RODbInfo.DataPortal_Fetch", ex);
			}
		}
		#endregion
		// Standard Refresh
		#region extension
		RODbInfoExtension _RODbInfoExtension = new RODbInfoExtension();
		[Serializable()]
		partial class RODbInfoExtension : extensionBase { }
		[Serializable()]
		class extensionBase
		{
			// Default Refresh
			public virtual void Refresh(RODbInfo tmp) { }
		}
		#endregion
	} // Class
	#region Converter
	internal class RODbInfoConverter : ExpandableObjectConverter
	{
		public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destType)
		{
			if (destType == typeof(string) && value is RODbInfo)
			{
				// Return the ToString value
				return ((RODbInfo)value).ToString();
			}
			return base.ConvertTo(context, culture, value, destType);
		}
	}
	#endregion
} // Namespace