// ========================================================================
// 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
{
    /// 
    ///	Figure Generated by MyGeneration using the CSLA Object Mapping template
    /// 
    [Serializable()]
    [TypeConverter(typeof(FigureConverter))]
    public partial class Figure : BusinessBase, IDisposable, IVEHasBrokenRules
    {
        #region Log4Net
        private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        #endregion
        #region Refresh
        private List _RefreshFigures = new List();
        private void AddToRefreshList(List refreshFigures)
        {
            if (IsDirty)
                refreshFigures.Add(this);
        }
        private void ClearRefreshList()
        {
            _RefreshFigures = new List();
        }
        private void BuildRefreshList()
        {
            ClearRefreshList();
            AddToRefreshList(_RefreshFigures);
        }
        private void ProcessRefreshList()
        {
            foreach (Figure tmp in _RefreshFigures)
            {
                FigureInfo.Refresh(tmp);
                if (tmp._MyROFst != null) ROFstInfo.Refresh(tmp._MyROFst);
                if (tmp._MyROImage != null) ROImageInfo.Refresh(tmp._MyROImage);
            }
            ClearRefreshList();
        }
        #endregion
        #region Collection
        private static List _CacheList = new List();
        protected static void AddToCache(Figure figure)
        {
            if (!_CacheList.Contains(figure)) _CacheList.Add(figure); // In AddToCache
        }
        protected static void RemoveFromCache(Figure figure)
        {
            while (_CacheList.Contains(figure)) _CacheList.Remove(figure); // In RemoveFromCache
        }
        private static Dictionary> _CacheByPrimaryKey = new Dictionary>();
        private static Dictionary> _CacheByROFstID_ImageID = new Dictionary>();
        private static void ConvertListToDictionary()
        {
            while (_CacheList.Count > 0) // Move Figure(s) from temporary _CacheList to _CacheByPrimaryKey
            {
                Figure tmp = _CacheList[0]; // Get the first Figure
                string pKey = tmp.FigureID.ToString();
                if (!_CacheByPrimaryKey.ContainsKey(pKey))
                {
                    _CacheByPrimaryKey[pKey] = new List(); // Add new list for PrimaryKey
                    _CacheByROFstID_ImageID[tmp.ROFstID.ToString() + "_" + tmp.ImageID.ToString()] = new List(); // Add new list for ROFstID_ImageID
                }
                _CacheByPrimaryKey[pKey].Add(tmp); // Add to Primary Key list
                _CacheByROFstID_ImageID[tmp.ROFstID.ToString() + "_" + tmp.ImageID.ToString()].Add(tmp); // Unique Index
                _CacheList.RemoveAt(0); // Remove the first Figure
            }
        }
        protected static Figure GetCachedByPrimaryKey(int figureID)
        {
            ConvertListToDictionary();
            string key = figureID.ToString();
            if (_CacheByPrimaryKey.ContainsKey(key)) return _CacheByPrimaryKey[key][0];
            return null;
        }
        protected static Figure GetCachedByROFstID_ImageID(int rOFstID, int imageID)
        {
            ConvertListToDictionary();
            string key = rOFstID.ToString() + "_" + imageID.ToString();
            if (_CacheByROFstID_ImageID.ContainsKey(key)) return _CacheByROFstID_ImageID[key][0];
            return null;
        }
        #endregion
        #region Business Methods
        private string _ErrorMessage = string.Empty;
        public string ErrorMessage
        {
            get { return _ErrorMessage; }
        }
        private static int _nextFigureID = -1;
        public static int NextFigureID
        {
            get { return _nextFigureID--; }
        }
        private int _FigureID;
        [System.ComponentModel.DataObjectField(true, true)]
        public int FigureID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _FigureID;
            }
        }
        private int _ROFstID;
        public int ROFstID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_MyROFst != null) _ROFstID = _MyROFst.ROFstID;
                return _ROFstID;
            }
        }
        private ROFst _MyROFst;
        public ROFst MyROFst
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_MyROFst == null && _ROFstID != 0) _MyROFst = ROFst.Get(_ROFstID);
                return _MyROFst;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (_MyROFst != value)
                {
                    _MyROFst = value;
                    _ROFstID = value.ROFstID;// Update underlying data field
                    PropertyHasChanged();
                }
            }
        }
        private int _ImageID;
        public int ImageID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_MyROImage != null) _ImageID = _MyROImage.ImageID;
                return _ImageID;
            }
        }
        private ROImage _MyROImage;
        public ROImage MyROImage
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_MyROImage == null && _ImageID != 0) _MyROImage = ROImage.GetJustROImage(_ImageID);//Don't load figures or ROFST - Causing Memory Crash
                return _MyROImage;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (_MyROImage != value)
                {
                    _MyROImage = value;
                    _ImageID = value.ImageID;// Update underlying data field
                    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
        public override bool IsDirty
        {
            get
            {
                if (base.IsDirty)
                    return true;
                return IsDirtyList(new List