// ========================================================================
// 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
{
    /// 
    ///	ROImage Generated by MyGeneration using the CSLA Object Mapping template
    /// 
    [Serializable()]
    [TypeConverter(typeof(ROImageConverter))]
    public partial class ROImage : BusinessBase, IDisposable, IVEHasBrokenRules
    {
        #region Log4Net
        private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        #endregion
        #region Refresh
        private List _RefreshROImages = new List();
        private List _RefreshROImageFigures = new List();
        private void AddToRefreshList(List refreshROImages, List refreshROImageFigures)
        {
            if (IsDirty)
                refreshROImages.Add(this);
            if (_ROImageFigures != null && _ROImageFigures.IsDirty)
            {
                foreach (ROImageFigure tmp in _ROImageFigures)
                {
                    if (tmp.IsDirty) refreshROImageFigures.Add(tmp);
                }
            }
        }
        private void ClearRefreshList()
        {
            _RefreshROImages = new List();
            _RefreshROImageFigures = new List();
        }
        private void BuildRefreshList()
        {
            ClearRefreshList();
            AddToRefreshList(_RefreshROImages, _RefreshROImageFigures);
        }
        private void ProcessRefreshList()
        {
            foreach (ROImage tmp in _RefreshROImages)
            {
                ROImageInfo.Refresh(tmp);
                if (tmp._MyRODb != null) RODbInfo.Refresh(tmp._MyRODb);
            }
            foreach (ROImageFigure tmp in _RefreshROImageFigures)
            {
                FigureInfo.Refresh(tmp);
            }
            ClearRefreshList();
        }
        #endregion
        #region Collection
        private static List _CacheList = new List();
        protected static void AddToCache(ROImage rOImage)
        {
            if (!_CacheList.Contains(rOImage)) _CacheList.Add(rOImage); // In AddToCache
        }
        protected static void RemoveFromCache(ROImage rOImage)
        {
            while (_CacheList.Contains(rOImage)) _CacheList.Remove(rOImage); // In RemoveFromCache
        }
        private static Dictionary> _CacheByPrimaryKey = new Dictionary>();
        private static Dictionary> _CacheByRODbID_FileName_DTS = new Dictionary>();
        private static void ConvertListToDictionary()
        {
            while (_CacheList.Count > 0) // Move ROImage(s) from temporary _CacheList to _CacheByPrimaryKey
            {
                ROImage tmp = _CacheList[0]; // Get the first ROImage
                string pKey = tmp.ImageID.ToString();
                if (!_CacheByPrimaryKey.ContainsKey(pKey))
                {
                    _CacheByPrimaryKey[pKey] = new List(); // Add new list for PrimaryKey
                    _CacheByRODbID_FileName_DTS[tmp.RODbID.ToString() + "_" + tmp.FileName.ToString() + "_" + tmp.DTS.ToString()] = new List(); // Add new list for RODbID_FileName_DTS
                }
                _CacheByPrimaryKey[pKey].Add(tmp); // Add to Primary Key list
                _CacheByRODbID_FileName_DTS[tmp.RODbID.ToString() + "_" + tmp.FileName.ToString() + "_" + tmp.DTS.ToString()].Add(tmp); // Unique Index
                _CacheList.RemoveAt(0); // Remove the first ROImage
            }
        }
        protected static ROImage GetCachedByPrimaryKey(int imageID)
        {
            ConvertListToDictionary();
            string key = imageID.ToString();
            if (_CacheByPrimaryKey.ContainsKey(key)) return _CacheByPrimaryKey[key][0];
            return null;
        }
        protected static ROImage GetCachedByRODbID_FileName_DTS(int rODbID, string fileName, DateTime dts)
        {
            ConvertListToDictionary();
            string key = rODbID.ToString() + "_" + fileName.ToString() + "_" + dts.ToString();
            if (_CacheByRODbID_FileName_DTS.ContainsKey(key)) return _CacheByRODbID_FileName_DTS[key][0];
            return null;
        }
        #endregion
        #region Business Methods
        private string _ErrorMessage = string.Empty;
        public string ErrorMessage
        {
            get { return _ErrorMessage; }
        }
        private static int _nextImageID = -1;
        public static int NextImageID
        {
            get { return _nextImageID--; }
        }
        private int _ImageID;
        [System.ComponentModel.DataObjectField(true, true)]
        public int ImageID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _ImageID;
            }
        }
        private int _RODbID;
        public int RODbID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_MyRODb != null) _RODbID = _MyRODb.RODbID;
                return _RODbID;
            }
        }
        private RODb _MyRODb;
        public RODb MyRODb
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_MyRODb == null && _RODbID != 0) _MyRODb = RODb.Get(_RODbID);
                return _MyRODb;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (_MyRODb != value)
                {
                    _MyRODb = value;
                    _RODbID = value.RODbID;// Update underlying data field
                    PropertyHasChanged();
                }
            }
        }
        private string _FileName = string.Empty;
        public string FileName
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _FileName;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (value == null) value = string.Empty;
                if (_FileName != value)
                {
                    _FileName = value;
                    PropertyHasChanged();
                }
            }
        }
        private byte[] _Content;
        public byte[] Content
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _Content;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (_Content != value)
                {
                    _Content = 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 _ROImageFigureCount = 0;
        /// 
        /// Count of ROImageFigures for this ROImage
        /// 
        public int ROImageFigureCount
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _ROImageFigureCount;
            }
        }
        private ROImageFigures _ROImageFigures = null;
        /// 
        /// Related Field
        /// 
        [TypeConverter(typeof(ROImageFiguresConverter))]
        public ROImageFigures ROImageFigures
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_ROImageFigureCount < 0 || (_ROImageFigureCount > 0 && _ROImageFigures == null))
                    _ROImageFigures = ROImageFigures.GetByImageID(ImageID);
                if (_ROImageFigureCount < 0)
                    _ROImageFigureCount = _ROImageFigures == null ? 0 : _ROImageFigures.Count;
                if (_ROImageFigures == null)
                    _ROImageFigures = ROImageFigures.New();
                return _ROImageFigures;
            }
        }
        public void Reset_ROImageFigures()
        {
            _ROImageFigureCount = -1;
        }
        public override bool IsDirty
        {
            get
            {
                if (base.IsDirty)
                    return true;
                return IsDirtyList(new List