// ========================================================================
// 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
{
    /// 
    ///	Transition Generated by MyGeneration using the CSLA Object Mapping template
    /// 
    [Serializable()]
    [TypeConverter(typeof(TransitionConverter))]
    public partial class Transition : BusinessBase, IDisposable, IVEHasBrokenRules
    {
        #region Log4Net
        private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        #endregion
        #region Refresh
        private List _RefreshTransitions = new List();
        private void AddToRefreshList(List refreshTransitions)
        {
            if (IsDirty)
                refreshTransitions.Add(this);
        }
        private void ClearRefreshList()
        {
            _RefreshTransitions = new List();
        }
        private void BuildRefreshList()
        {
            ClearRefreshList();
            AddToRefreshList(_RefreshTransitions);
        }
        private void ProcessRefreshList()
        {
            foreach (Transition tmp in _RefreshTransitions)
            {
                TransitionInfo.Refresh(tmp);
                if (tmp._MyContent != null) ContentInfo.Refresh(tmp._MyContent);
                if (tmp._MyItemRangeID != null) ItemInfo.Refresh(tmp._MyItemRangeID);
                if (tmp._MyItemToID != null) ItemInfo.Refresh(tmp._MyItemToID);
            }
            ClearRefreshList();
        }
        #endregion
        #region Collection
        private static List _CacheList = new List();
        protected static void AddToCache(Transition transition)
        {
            if (!_CacheList.Contains(transition)) _CacheList.Add(transition); // In AddToCache
        }
        protected static void RemoveFromCache(Transition transition)
        {
            while (_CacheList.Contains(transition)) _CacheList.Remove(transition); // In RemoveFromCache
        }
        private static Dictionary> _CacheByPrimaryKey = new Dictionary>();
        private static void ConvertListToDictionary()
        {
            while (_CacheList.Count > 0) // Move Transition(s) from temporary _CacheList to _CacheByPrimaryKey
            {
                Transition tmp = _CacheList[0]; // Get the first Transition
                string pKey = tmp.TransitionID.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 Transition
            }
        }
        protected static Transition GetCachedByPrimaryKey(int transitionID)
        {
            ConvertListToDictionary();
            string key = transitionID.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 _nextTransitionID = -1;
        public static int NextTransitionID
        {
            get { return _nextTransitionID--; }
        }
        private int _TransitionID;
        [System.ComponentModel.DataObjectField(true, true)]
        public int TransitionID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _TransitionID;
            }
        }
        private int _FromID;
        public int FromID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_MyContent != null) _FromID = _MyContent.ContentID;
                return _FromID;
            }
        }
        private Content _MyContent;
        public Content MyContent
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_MyContent == null && _FromID != 0) _MyContent = Content.Get(_FromID);
                return _MyContent;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (_MyContent != value)
                {
                    _MyContent = value;
                    _FromID = value.ContentID;// Update underlying data field
                    PropertyHasChanged();
                }
            }
        }
        private int _ToID;
        /// 
        /// StructureID
        /// 
        public int ToID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_MyItemToID != null) _ToID = _MyItemToID.ItemID;
                return _ToID;
            }
        }
        private Item _MyItemToID;
        public Item MyItemToID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_MyItemToID == null && _ToID != 0) _MyItemToID = Item.Get(_ToID);
                return _MyItemToID;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (_MyItemToID != value)
                {
                    _MyItemToID = value;
                    _ToID = value.ItemID;// Update underlying data field
                    PropertyHasChanged();
                }
            }
        }
        private int _RangeID;
        public int RangeID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_MyItemRangeID != null) _RangeID = _MyItemRangeID.ItemID;
                return _RangeID;
            }
        }
        private Item _MyItemRangeID;
        public Item MyItemRangeID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_MyItemRangeID == null && _RangeID != 0) _MyItemRangeID = Item.Get(_RangeID);
                return _MyItemRangeID;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (_MyItemRangeID != value)
                {
                    _MyItemRangeID = value;
                    _RangeID = value.ItemID;// Update underlying data field
                    PropertyHasChanged();
                }
            }
        }
        private int _IsRange;
        /// 
        /// 0 - Not a Range Transition, 1 - Range Transition, 2 - Range Transition - Extends to last Sibling
        /// 
        public int IsRange
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _IsRange;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (_IsRange != value)
                {
                    _IsRange = value;
                    PropertyHasChanged();
                }
            }
        }
        private int _TranType;
        public int TranType
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _TranType;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (_TranType != value)
                {
                    _TranType = value;
                    PropertyHasChanged();
                }
            }
        }
        private string _Config = string.Empty;
        public string Config
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _Config;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                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
            {
                return _DTS;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (_DTS != value)
                {
                    _DTS = value;
                    PropertyHasChanged();
                }
            }
        }
        private string _UserID = string.Empty;
        public string UserID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _UserID;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (value == null) value = string.Empty;
                if (_UserID != value)
                {
                    _UserID = value;
                    PropertyHasChanged();
                }
            }
        }
        private byte[] _LastChanged = new byte[8];//timestamp
        private int _TransitionZTransitionCount = 0;
        /// 
        /// Count of TransitionZTransitions for this Transition
        /// 
        public int TransitionZTransitionCount
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _TransitionZTransitionCount;
            }
        }
        private ZTransition _MyZTransition = null;
        /// 
        /// Related Field
        /// 
        [TypeConverter(typeof(ZTransitionConverter))]
        public ZTransition MyZTransition
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_MyZTransition == null) _MyZTransition = ZTransition.New(this);
                return _MyZTransition;
            }
        }
        public override bool IsDirty
        {
            get
            {
                if (base.IsDirty)
                    return true;
                return IsDirtyList(new List