// ========================================================================
// 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
{
	/// 
	///	ROFst Generated by MyGeneration using the CSLA Object Mapping template
	/// 
	[Serializable()]
	[TypeConverter(typeof(ROFstConverter))]
	public partial class ROFst : BusinessBase, IDisposable, IVEHasBrokenRules
	{
		#region Log4Net
		private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
		#endregion
		#region Refresh
		private List _RefreshROFsts = new List();
		private List _RefreshROFstAssociations = new List();
		private List _RefreshROFstFigures = new List();
		private void AddToRefreshList(List refreshROFsts, List refreshROFstAssociations, List refreshROFstFigures)
		{
			if (IsDirty)
				refreshROFsts.Add(this);
			if (_ROFstAssociations != null && _ROFstAssociations.IsDirty)
			{
				foreach (ROFstAssociation tmp in _ROFstAssociations)
				{
					if (tmp.IsDirty) refreshROFstAssociations.Add(tmp);
				}
			}
			if (_ROFstFigures != null && _ROFstFigures.IsDirty)
			{
				foreach (ROFstFigure tmp in _ROFstFigures)
				{
					if (tmp.IsDirty) refreshROFstFigures.Add(tmp);
				}
			}
		}
		private void BuildRefreshList()
		{
			_RefreshROFsts = new List();
			_RefreshROFstAssociations = new List();
			_RefreshROFstFigures = new List();
			AddToRefreshList(_RefreshROFsts, _RefreshROFstAssociations, _RefreshROFstFigures);
		}
		private void ProcessRefreshList()
		{
			foreach (ROFst tmp in _RefreshROFsts)
			{
				ROFstInfo.Refresh(tmp);
				if (tmp._MyRODb != null) RODbInfo.Refresh(tmp._MyRODb);
			}
			foreach (ROFstAssociation tmp in _RefreshROFstAssociations)
			{
				AssociationInfo.Refresh(tmp);
			}
			foreach (ROFstFigure tmp in _RefreshROFstFigures)
			{
				FigureInfo.Refresh(tmp);
			}
		}
		#endregion
		#region Collection
		private static List _CacheList = new List();
		protected static void AddToCache(ROFst rOFst)
		{
			if (!_CacheList.Contains(rOFst)) _CacheList.Add(rOFst); // In AddToCache
		}
		protected static void RemoveFromCache(ROFst rOFst)
		{
			while (_CacheList.Contains(rOFst)) _CacheList.Remove(rOFst); // In RemoveFromCache
		}
		private static Dictionary> _CacheByPrimaryKey = new Dictionary>();
		private static Dictionary> _CacheByRODbID_DTS = new Dictionary>();
		private static void ConvertListToDictionary()
		{
			List remove = new List();
			foreach (ROFst tmp in _CacheList)
			{
				if (!_CacheByPrimaryKey.ContainsKey(tmp.ROFstID.ToString()))
				{
					_CacheByPrimaryKey[tmp.ROFstID.ToString()] = new List(); // Add new list for PrimaryKey
					_CacheByRODbID_DTS[tmp.RODbID.ToString() + "_" + tmp.DTS.ToString()] = new List(); // Add new list for RODbID_DTS
				}
				_CacheByPrimaryKey[tmp.ROFstID.ToString()].Add(tmp); // Add to Primary Key list
				_CacheByRODbID_DTS[tmp.RODbID.ToString() + "_" + tmp.DTS.ToString()].Add(tmp); // Unique Index
				remove.Add(tmp);
			}
			foreach (ROFst tmp in remove)
				RemoveFromCache(tmp);
		}
		protected static ROFst GetCachedByPrimaryKey(int rOFstID)
		{
			ConvertListToDictionary();
			string key = rOFstID.ToString();
			if (_CacheByPrimaryKey.ContainsKey(key)) return _CacheByPrimaryKey[key][0];
			return null;
		}
		protected static ROFst GetCachedByRODbID_DTS(int rODbID, DateTime dts)
		{
			ConvertListToDictionary();
			string key = rODbID.ToString() + "_" + dts.ToString();
			if (_CacheByRODbID_DTS.ContainsKey(key)) return _CacheByRODbID_DTS[key][0];
			return null;
		}
		#endregion
		#region Business Methods
		private string _ErrorMessage = string.Empty;
		public string ErrorMessage
		{
			get { return _ErrorMessage; }
		}
		private static int _nextROFstID = -1;
		public static int NextROFstID
		{
			get { return _nextROFstID--; }
		}
		private int _ROFstID;
		[System.ComponentModel.DataObjectField(true, true)]
		public int ROFstID
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("ROFstID", true);
				return _ROFstID;
			}
		}
		private int _RODbID;
		public int RODbID
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("RODbID", true);
				if (_MyRODb != null) _RODbID = _MyRODb.RODbID;
				return _RODbID;
			}
		}
		private RODb _MyRODb;
		public RODb MyRODb
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("MyRODb", true);
				if (_MyRODb == null && _RODbID != 0) _MyRODb = RODb.Get(_RODbID);
				return _MyRODb;
			}
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			set
			{
				CanWriteProperty("MyRODb", true);
				if (_MyRODb != value)
				{
					_MyRODb = value;
					PropertyHasChanged();
				}
			}
		}
		private byte[] _ROLookup;
		public byte[] ROLookup
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("ROLookup", true);
				return _ROLookup;
			}
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			set
			{
				CanWriteProperty("ROLookup", true);
				if (_ROLookup != value)
				{
					_ROLookup = 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 _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 byte[] _LastChanged = new byte[8];//timestamp
		private int _ROFstAssociationCount = 0;
		/// 
		/// Count of ROFstAssociations for this ROFst
		/// 
		public int ROFstAssociationCount
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("ROFstAssociationCount", true);
				return _ROFstAssociationCount;
			}
		}
		private ROFstAssociations _ROFstAssociations = null;
		/// 
		/// Related Field
		/// 
		[TypeConverter(typeof(ROFstAssociationsConverter))]
		public ROFstAssociations ROFstAssociations
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("ROFstAssociations", true);
				if (_ROFstAssociationCount < 0 || (_ROFstAssociationCount > 0 && _ROFstAssociations == null))
					_ROFstAssociations = ROFstAssociations.GetByROFstID(ROFstID);
				if (_ROFstAssociationCount < 0 )
					_ROFstAssociationCount = _ROFstAssociations == null ? 0 : _ROFstAssociations.Count;
				if (_ROFstAssociations == null)
					_ROFstAssociations = ROFstAssociations.New();
				return _ROFstAssociations;
			}
		}
		public void Reset_ROFstAssociations()
		{
			_ROFstAssociationCount = -1;
		}
		private int _ROFstFigureCount = 0;
		/// 
		/// Count of ROFstFigures for this ROFst
		/// 
		public int ROFstFigureCount
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("ROFstFigureCount", true);
				return _ROFstFigureCount;
			}
		}
		private ROFstFigures _ROFstFigures = null;
		/// 
		/// Related Field
		/// 
		[TypeConverter(typeof(ROFstFiguresConverter))]
		public ROFstFigures ROFstFigures
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("ROFstFigures", true);
				if (_ROFstFigureCount < 0 || (_ROFstFigureCount > 0 && _ROFstFigures == null))
					_ROFstFigures = ROFstFigures.GetByROFstID(ROFstID);
				if (_ROFstFigureCount < 0 )
					_ROFstFigureCount = _ROFstFigures == null ? 0 : _ROFstFigures.Count;
				if (_ROFstFigures == null)
					_ROFstFigures = ROFstFigures.New();
				return _ROFstFigures;
			}
		}
		public void Reset_ROFstFigures()
		{
			_ROFstFigureCount = -1;
		}
		public override bool IsDirty
		{
			get 
			{
				if ( base.IsDirty ) 
					return true;
				return IsDirtyList(new List