// ========================================================================
// 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
{
    /// 
    ///	Check Generated by MyGeneration using the CSLA Object Mapping template
    /// 
    [Serializable()]
    [TypeConverter(typeof(CheckConverter))]
    public partial class Check : BusinessBase, IDisposable, IVEHasBrokenRules
    {
        #region Log4Net
        private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        #endregion
        #region Refresh
        private List _RefreshChecks = new List();
        private void AddToRefreshList(List refreshChecks)
        {
            if (IsDirty)
                refreshChecks.Add(this);
        }
        private void ClearRefreshList()
        {
            _RefreshChecks = new List();
        }
        private void BuildRefreshList()
        {
            ClearRefreshList();
            AddToRefreshList(_RefreshChecks);
        }
        private void ProcessRefreshList()
        {
            foreach (Check tmp in _RefreshChecks)
            {
                CheckInfo.Refresh(tmp);
                if (tmp._MyRevision != null) RevisionInfo.Refresh(tmp._MyRevision);
                if (tmp._MyStage != null) StageInfo.Refresh(tmp._MyStage);
            }
            ClearRefreshList();
        }
        #endregion
        #region Collection
        private static List _CacheList = new List();
        protected static void AddToCache(Check check)
        {
            if (!_CacheList.Contains(check)) _CacheList.Add(check); // In AddToCache
        }
        protected static void RemoveFromCache(Check check)
        {
            while (_CacheList.Contains(check)) _CacheList.Remove(check); // In RemoveFromCache
        }
        private static Dictionary> _CacheByPrimaryKey = new Dictionary>();
        private static void ConvertListToDictionary()
        {
            while (_CacheList.Count > 0) // Move Check(s) from temporary _CacheList to _CacheByPrimaryKey
            {
                Check tmp = _CacheList[0]; // Get the first Check
                string pKey = tmp.CheckID.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 Check
            }
        }
        protected static Check GetCachedByPrimaryKey(int checkID)
        {
            ConvertListToDictionary();
            string key = checkID.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 _nextCheckID = -1;
        public static int NextCheckID
        {
            get { return _nextCheckID--; }
        }
        private int _CheckID;
        [System.ComponentModel.DataObjectField(true, true)]
        public int CheckID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _CheckID;
            }
        }
        private int _RevisionID;
        public int RevisionID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_MyRevision != null) _RevisionID = _MyRevision.RevisionID;
                return _RevisionID;
            }
        }
        private Revision _MyRevision;
        public Revision MyRevision
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_MyRevision == null && _RevisionID != 0) _MyRevision = Revision.Get(_RevisionID);
                return _MyRevision;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (_MyRevision != value)
                {
                    _MyRevision = value;
                    _RevisionID = value.RevisionID;// Update underlying data field
                    PropertyHasChanged();
                }
            }
        }
        private int _StageID;
        public int StageID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_MyStage != null) _StageID = _MyStage.StageID;
                return _StageID;
            }
        }
        private Stage _MyStage;
        public Stage MyStage
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_MyStage == null && _StageID != 0) _MyStage = Stage.GetJustStage(_StageID);
                return _MyStage;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (_MyStage != value)
                {
                    _MyStage = value;
                    _StageID = value.StageID;// Update underlying data field
                    PropertyHasChanged();
                }
            }
        }
        private string _ConsistencyChecks = string.Empty;
        public string ConsistencyChecks
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _ConsistencyChecks;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (value == null) value = string.Empty;
                if (_ConsistencyChecks != value)
                {
                    _ConsistencyChecks = 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
        public override bool IsDirty
        {
            get
            {
                if (base.IsDirty)
                    return true;
                return IsDirtyList(new List