// ========================================================================
// 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
{
	/// 
	///	Item Generated by MyGeneration using the CSLA Object Mapping template
	/// 
	[Serializable()]
	[TypeConverter(typeof(ItemConverter))]
	public partial class Item : BusinessBase- , IDisposable, IVEHasBrokenRules
	{
		#region Log4Net
		private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
		#endregion
		#region Refresh
		private List-  _RefreshItems = new List- ();
		private List _RefreshItemAnnotations = new List();
		private List _RefreshItemDocVersions = new List();
		private List _RefreshItemParts = new List();
		private List _RefreshItemTransitions_RangeID = new List();
		private List _RefreshItemTransitions_ToID = new List();
		private void AddToRefreshList(List-  refreshItems, List refreshItemAnnotations, List refreshItemDocVersions, List refreshItemParts, List refreshItemTransitions_RangeID, List refreshItemTransitions_ToID)
		{
			if (IsDirty)
				refreshItems.Add(this);
			if (_ItemAnnotations != null && _ItemAnnotations.IsDirty)
			{
				foreach (ItemAnnotation tmp in _ItemAnnotations)
				{
					if (tmp.IsDirty) refreshItemAnnotations.Add(tmp);
				}
			}
			if (_ItemDocVersions != null && _ItemDocVersions.IsDirty)
			{
				foreach (ItemDocVersion tmp in _ItemDocVersions)
				{
					if (tmp.IsDirty) refreshItemDocVersions.Add(tmp);
				}
			}
			if (_NextItems != null && _NextItems.IsDirty)
			{
				foreach (Item tmp in _NextItems)
				{
					tmp.AddToRefreshList(refreshItems, refreshItemAnnotations, refreshItemDocVersions, refreshItemParts, refreshItemTransitions_RangeID, refreshItemTransitions_ToID);
				}
			}
			if (_ItemParts != null && _ItemParts.IsDirty)
			{
				foreach (ItemPart tmp in _ItemParts)
				{
					if (tmp.IsDirty) refreshItemParts.Add(tmp);
				}
			}
			if (_ItemTransitions_RangeID != null && _ItemTransitions_RangeID.IsDirty)
			{
				foreach (ItemTransition_RangeID tmp in _ItemTransitions_RangeID)
				{
					if (tmp.IsDirty) refreshItemTransitions_RangeID.Add(tmp);
				}
			}
			if (_ItemTransitions_ToID != null && _ItemTransitions_ToID.IsDirty)
			{
				foreach (ItemTransition_ToID tmp in _ItemTransitions_ToID)
				{
					if (tmp.IsDirty) refreshItemTransitions_ToID.Add(tmp);
				}
			}
		}
		private void ClearRefreshList()
		{
			_RefreshItems = new List- ();
			_RefreshItemAnnotations = new List();
			_RefreshItemDocVersions = new List();
			_RefreshItemParts = new List();
			_RefreshItemTransitions_RangeID = new List();
			_RefreshItemTransitions_ToID = new List();
		}
		private void BuildRefreshList()
		{
			ClearRefreshList();
			AddToRefreshList(_RefreshItems, _RefreshItemAnnotations, _RefreshItemDocVersions, _RefreshItemParts, _RefreshItemTransitions_RangeID, _RefreshItemTransitions_ToID);
		}
		private void ProcessRefreshList()
		{
			foreach (Item tmp in _RefreshItems)
			{
				if (tmp._MyContent != null) ContentInfo.Refresh(tmp._MyContent);
				ItemInfo.Refresh(tmp);
			}
			foreach (ItemAnnotation tmp in _RefreshItemAnnotations)
			{
				AnnotationInfo.Refresh(tmp);
			}
			foreach (ItemDocVersion tmp in _RefreshItemDocVersions)
			{
				DocVersionInfo.Refresh(tmp);
			}
			foreach (ItemPart tmp in _RefreshItemParts)
			{
				PartInfo.Refresh(tmp);
			}
			foreach (ItemTransition_RangeID tmp in _RefreshItemTransitions_RangeID)
			{
				TransitionInfo.Refresh(tmp);
			}
			foreach (ItemTransition_ToID tmp in _RefreshItemTransitions_ToID)
			{
				TransitionInfo.Refresh(tmp);
			}
			ClearRefreshList();
		}
		#endregion
		#region Collection
		private static List-  _CacheList = new List- ();
		protected static void AddToCache(Item item)
		{
			if (!_CacheList.Contains(item)) _CacheList.Add(item); // In AddToCache
		}
		protected static void RemoveFromCache(Item item)
		{
			while (_CacheList.Contains(item)) _CacheList.Remove(item); // In RemoveFromCache
		}
		private static Dictionary> _CacheByPrimaryKey = new Dictionary>();
		private static void ConvertListToDictionary()
		{
			while (_CacheList.Count > 0) // Move Item(s) from temporary _CacheList to _CacheByPrimaryKey
			{
				Item tmp = _CacheList[0]; // Get the first Item
				string pKey = tmp.ItemID.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 Item
			}
		}
		protected static Item GetCachedByPrimaryKey(int itemID)
		{
			ConvertListToDictionary();
			string key = itemID.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; }
		}
		private static int _nextItemID = -1;
		public static int NextItemID
		{
			get { return _nextItemID--; }
		}
		private int _ItemID;
		[System.ComponentModel.DataObjectField(true, true)]
		public int ItemID
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("ItemID", true);
				return _ItemID;
			}
		}
		private int? _PreviousID;
		public int? PreviousID
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("PreviousID", true);
				if (_MyPrevious != null) _PreviousID = _MyPrevious.ItemID;
				return _PreviousID;
			}
		}
		private Item _MyPrevious;
		public Item MyPrevious
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("MyPrevious", true);
				if (_MyPrevious == null && _PreviousID != null) _MyPrevious = Item.Get((int)_PreviousID);
				return _MyPrevious;
			}
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			set
			{
				CanWriteProperty("MyPrevious", true);
				if ((_MyPrevious == null ? _PreviousID : (int?)_MyPrevious.ItemID) != (value == null ? null : (int?)value.ItemID))
				{
					_MyPrevious = value;
					_PreviousID = (value == null ? null : (int?)value.ItemID);
					PropertyHasChanged();
				}
			}
		}
		private int _ContentID;
		public int ContentID
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("ContentID", true);
				if (_MyContent != null) _ContentID = _MyContent.ContentID;
				return _ContentID;
			}
		}
		private Content _MyContent;
		public Content MyContent
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("MyContent", true);
				if (_MyContent == null && _ContentID != 0) _MyContent = Content.Get(_ContentID);
				return _MyContent;
			}
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			set
			{
				CanWriteProperty("MyContent", true);
				if (_MyContent != value)
				{
					_MyContent = value;
					_ContentID = value.ContentID;// Update underlying data field
					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 _ItemAnnotationCount = 0;
		/// 
		/// Count of ItemAnnotations for this Item
		/// 
		public int ItemAnnotationCount
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("ItemAnnotationCount", true);
				return _ItemAnnotationCount;
			}
		}
		private ItemAnnotations _ItemAnnotations = null;
		/// 
		/// Related Field
		/// 
		[TypeConverter(typeof(ItemAnnotationsConverter))]
		public ItemAnnotations ItemAnnotations
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("ItemAnnotations", true);
				if (_ItemAnnotationCount < 0 || (_ItemAnnotationCount > 0 && _ItemAnnotations == null))
					_ItemAnnotations = ItemAnnotations.GetByItemID(ItemID);
				if (_ItemAnnotationCount < 0 )
					_ItemAnnotationCount = _ItemAnnotations == null ? 0 : _ItemAnnotations.Count;
				if (_ItemAnnotations == null)
					_ItemAnnotations = ItemAnnotations.New();
				return _ItemAnnotations;
			}
		}
		public void Reset_ItemAnnotations()
		{
			_ItemAnnotationCount = -1;
		}
		private int _ItemDocVersionCount = 0;
		/// 
		/// Count of ItemDocVersions for this Item
		/// 
		public int ItemDocVersionCount
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("ItemDocVersionCount", true);
				return _ItemDocVersionCount;
			}
		}
		private ItemDocVersions _ItemDocVersions = null;
		/// 
		/// Related Field
		/// 
		[TypeConverter(typeof(ItemDocVersionsConverter))]
		public ItemDocVersions ItemDocVersions
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("ItemDocVersions", true);
				if (_ItemDocVersionCount < 0 || (_ItemDocVersionCount > 0 && _ItemDocVersions == null))
					_ItemDocVersions = ItemDocVersions.GetByItemID(ItemID);
				if (_ItemDocVersionCount < 0 )
					_ItemDocVersionCount = _ItemDocVersions == null ? 0 : _ItemDocVersions.Count;
				if (_ItemDocVersions == null)
					_ItemDocVersions = ItemDocVersions.New();
				return _ItemDocVersions;
			}
		}
		public void Reset_ItemDocVersions()
		{
			_ItemDocVersionCount = -1;
		}
		private int _NextItemCount = 0;
		/// 
		/// Count of NextItems for this Item
		/// 
		public int NextItemCount
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("NextItemCount", true);
				return _NextItemCount;
			}
		}
		private NextItems _NextItems = null;
		/// 
		/// Related Field
		/// 
		[TypeConverter(typeof(NextItemsConverter))]
		public NextItems NextItems
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("NextItems", true);
				if (_NextItemCount < 0 || (_NextItemCount > 0 && _NextItems == null))
					_NextItems = NextItems.GetByPreviousID(ItemID);
				if (_NextItemCount < 0 )
					_NextItemCount = _NextItems == null ? 0 : _NextItems.Count;
				if (_NextItems == null)
					_NextItems = NextItems.New();
				return _NextItems;
			}
		}
		public void Reset_NextItems()
		{
			_NextItemCount = -1;
		}
		private int _ItemPartCount = 0;
		/// 
		/// Count of ItemParts for this Item
		/// 
		public int ItemPartCount
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("ItemPartCount", true);
				return _ItemPartCount;
			}
		}
		private ItemParts _ItemParts = null;
		/// 
		/// Related Field
		/// 
		[TypeConverter(typeof(ItemPartsConverter))]
		public ItemParts ItemParts
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("ItemParts", true);
				if (_ItemPartCount < 0 || (_ItemPartCount > 0 && _ItemParts == null))
					_ItemParts = ItemParts.GetByItemID(ItemID);
				if (_ItemPartCount < 0 )
					_ItemPartCount = _ItemParts == null ? 0 : _ItemParts.Count;
				if (_ItemParts == null)
					_ItemParts = ItemParts.New();
				return _ItemParts;
			}
		}
		public void Reset_ItemParts()
		{
			_ItemPartCount = -1;
		}
		private int _ItemTransition_RangeIDCount = 0;
		/// 
		/// Count of ItemTransitions for this Item
		/// 
		public int ItemTransition_RangeIDCount
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("ItemTransition_RangeIDCount", true);
				return _ItemTransition_RangeIDCount;
			}
		}
		private ItemTransitions_RangeID _ItemTransitions_RangeID = null;
		/// 
		/// Related Field
		/// 
		[TypeConverter(typeof(ItemTransitions_RangeIDConverter))]
		public ItemTransitions_RangeID ItemTransitions_RangeID
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("ItemTransitions_RangeID", true);
				if (_ItemTransition_RangeIDCount < 0 || (_ItemTransition_RangeIDCount > 0 && _ItemTransitions_RangeID == null))
					_ItemTransitions_RangeID = ItemTransitions_RangeID.GetByRangeID(ItemID);
				if (_ItemTransition_RangeIDCount < 0 )
					_ItemTransition_RangeIDCount = _ItemTransitions_RangeID == null ? 0 : _ItemTransitions_RangeID.Count;
				if (_ItemTransitions_RangeID == null)
					_ItemTransitions_RangeID = ItemTransitions_RangeID.New();
				return _ItemTransitions_RangeID;
			}
		}
		public void Reset_ItemTransitions_RangeID()
		{
			_ItemTransition_RangeIDCount = -1;
		}
		private int _ItemTransition_ToIDCount = 0;
		/// 
		/// Count of ItemTransitions for this Item
		/// 
		public int ItemTransition_ToIDCount
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("ItemTransition_ToIDCount", true);
				return _ItemTransition_ToIDCount;
			}
		}
		private ItemTransitions_ToID _ItemTransitions_ToID = null;
		/// 
		/// Related Field
		/// 
		[TypeConverter(typeof(ItemTransitions_ToIDConverter))]
		public ItemTransitions_ToID ItemTransitions_ToID
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty("ItemTransitions_ToID", true);
				if (_ItemTransition_ToIDCount < 0 || (_ItemTransition_ToIDCount > 0 && _ItemTransitions_ToID == null))
					_ItemTransitions_ToID = ItemTransitions_ToID.GetByToID(ItemID);
				if (_ItemTransition_ToIDCount < 0 )
					_ItemTransition_ToIDCount = _ItemTransitions_ToID == null ? 0 : _ItemTransitions_ToID.Count;
				if (_ItemTransitions_ToID == null)
					_ItemTransitions_ToID = ItemTransitions_ToID.New();
				return _ItemTransitions_ToID;
			}
		}
		public void Reset_ItemTransitions_ToID()
		{
			_ItemTransition_ToIDCount = -1;
		}
		public override bool IsDirty
		{
			get 
			{
				if ( base.IsDirty ) 
					return true;
				return IsDirtyList(new List