// ========================================================================
// 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
{
    /// 
    ///	Content Generated by MyGeneration using the CSLA Object Mapping template
    /// 
    [Serializable()]
    [TypeConverter(typeof(ContentConverter))]
    public partial class Content : BusinessBase, IDisposable, IVEHasBrokenRules
    {
        #region Log4Net
        private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        #endregion
        #region Refresh
        private List _RefreshContents = new List();
        private List _RefreshContentDetails = new List();
        private List _RefreshContentItems = new List();
        private List _RefreshContentParts = new List();
        private List _RefreshContentRoUsages = new List();
        private List _RefreshContentTransitions = new List();
        private void AddToRefreshList(List refreshContents, List refreshContentDetails, List refreshContentItems, List refreshContentParts, List refreshContentRoUsages, List refreshContentTransitions)
        {
            if (IsDirty)
                refreshContents.Add(this);
            if (_ContentDetails != null && _ContentDetails.IsDirty)
            {
                foreach (ContentDetail tmp in _ContentDetails)
                {
                    if (tmp.IsDirty) refreshContentDetails.Add(tmp);
                }
            }
            if (_ContentItems != null && _ContentItems.IsDirty)
            {
                foreach (ContentItem tmp in _ContentItems)
                {
                    if (tmp.IsDirty) refreshContentItems.Add(tmp);
                }
            }
            if (_ContentParts != null && _ContentParts.IsDirty)
            {
                foreach (ContentPart tmp in _ContentParts)
                {
                    if (tmp.IsDirty) refreshContentParts.Add(tmp);
                }
            }
            if (_ContentRoUsages != null && _ContentRoUsages.IsDirty)
            {
                foreach (ContentRoUsage tmp in _ContentRoUsages)
                {
                    if (tmp.IsDirty) refreshContentRoUsages.Add(tmp);
                }
            }
            if (_ContentTransitions != null && _ContentTransitions.IsDirty)
            {
                foreach (ContentTransition tmp in _ContentTransitions)
                {
                    if (tmp.IsDirty) refreshContentTransitions.Add(tmp);
                }
            }
        }
        private void ClearRefreshList()
        {
            _RefreshContents = new List();
            _RefreshContentDetails = new List();
            _RefreshContentItems = new List();
            _RefreshContentParts = new List();
            _RefreshContentRoUsages = new List();
            _RefreshContentTransitions = new List();
        }
        private void BuildRefreshList()
        {
            ClearRefreshList();
            AddToRefreshList(_RefreshContents, _RefreshContentDetails, _RefreshContentItems, _RefreshContentParts, _RefreshContentRoUsages, _RefreshContentTransitions);
        }
        private void ProcessRefreshList()
        {
            foreach (Content tmp in _RefreshContents)
            {
                ContentInfo.Refresh(tmp);
                if (tmp._MyFormat != null) FormatInfo.Refresh(tmp._MyFormat);
            }
            foreach (ContentDetail tmp in _RefreshContentDetails)
            {
                DetailInfo.Refresh(tmp);
            }
            foreach (ContentItem tmp in _RefreshContentItems)
            {
                ItemInfo.Refresh(tmp);
            }
            foreach (ContentPart tmp in _RefreshContentParts)
            {
                PartInfo.Refresh(this, tmp);
            }
            foreach (ContentRoUsage tmp in _RefreshContentRoUsages)
            {
                RoUsageInfo.Refresh(tmp);
            }
            foreach (ContentTransition tmp in _RefreshContentTransitions)
            {
                TransitionInfo.Refresh(tmp);
            }
            ClearRefreshList();
        }
        #endregion
        #region Collection
        private static List _CacheList = new List();
        protected static void AddToCache(Content content)
        {
            if (!_CacheList.Contains(content)) _CacheList.Add(content); // In AddToCache
        }
        protected static void RemoveFromCache(Content content)
        {
            while (_CacheList.Contains(content)) _CacheList.Remove(content); // In RemoveFromCache
        }
        private static Dictionary> _CacheByPrimaryKey = new Dictionary>();
        private static void ConvertListToDictionary()
        {
            while (_CacheList.Count > 0) // Move Content(s) from temporary _CacheList to _CacheByPrimaryKey
            {
                Content tmp = _CacheList[0]; // Get the first Content
                if (!tmp.Disposed)
                {
                    string pKey = tmp.ContentID.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 Content
            }
        }
        protected static Content GetCachedByPrimaryKey(int contentID)
        {
            ConvertListToDictionary();
            string key = contentID.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 _nextContentID = -1;
        public static int NextContentID
        {
            get { return _nextContentID--; }
        }
        private int _ContentID;
        [System.ComponentModel.DataObjectField(true, true)]
        public int ContentID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _ContentID;
            }
        }
        private string _Number = string.Empty;
        /// 
        /// Increased from 30 to 256 to support RTF symbols
        /// 
        public string Number
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _Number;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (value == null) value = string.Empty;
                if (_Number != value)
                {
                    _Number = value;
                    PropertyHasChanged();
                }
            }
        }
        private string _Text = string.Empty;
        public string Text
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (Disposed) throw new Exception("Attempt to access Disposed Object");
                return _Text;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (Disposed) throw new Exception("Attempt to access Disposed Object");
                if (value == null) value = string.Empty;
                if (_Text != value)
                {
                    _Text = value;
                    PropertyHasChanged();
                }
            }
        }
        private int? _Type;
        /// 
        /// 0 - Procedure, 10000 - Section, 20000 Step
        /// 
        public int? Type
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _Type;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if (_Type != value)
                {
                    _Type = value;
                    PropertyHasChanged();
                }
            }
        }
        private int? _FormatID;
        public int? FormatID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_MyFormat != null) _FormatID = _MyFormat.FormatID;
                return _FormatID;
            }
        }
        private Format _MyFormat;
        public Format MyFormat
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_MyFormat == null && _FormatID != null) _MyFormat = Format.Get((int)_FormatID);
                return _MyFormat;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                if ((_MyFormat == null ? _FormatID : (int?)_MyFormat.FormatID) != (value == null ? null : (int?)value.FormatID))
                {
                    _MyFormat = value;
                    _FormatID = (value == null ? null : (int?)value.FormatID);
                    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 byte[] LastChanged
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _LastChanged;
            }
            set { _LastChanged = value; }
        }
        private int _ContentDetailCount = 0;
        /// 
        /// Count of ContentDetails for this Content
        /// 
        public int ContentDetailCount
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _ContentDetailCount;
            }
        }
        private ContentDetails _ContentDetails = null;
        /// 
        /// Related Field
        /// 
        [TypeConverter(typeof(ContentDetailsConverter))]
        public ContentDetails ContentDetails
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_ContentDetailCount < 0 || (_ContentDetailCount > 0 && _ContentDetails == null))
                    _ContentDetails = ContentDetails.GetByContentID(ContentID);
                if (_ContentDetailCount < 0)
                    _ContentDetailCount = _ContentDetails == null ? 0 : _ContentDetails.Count;
                if (_ContentDetails == null)
                    _ContentDetails = ContentDetails.New();
                return _ContentDetails;
            }
        }
        public void Reset_ContentDetails()
        {
            _ContentDetailCount = -1;
        }
        private int _ContentEntryCount = 0;
        /// 
        /// Count of ContentEntries for this Content
        /// 
        public int ContentEntryCount
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _ContentEntryCount;
            }
        }
        private Entry _MyEntry = null;
        /// 
        /// Related Field
        /// 
        [TypeConverter(typeof(EntryConverter))]
        public Entry MyEntry
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_MyEntry == null) _MyEntry = Entry.New(this);
                return _MyEntry;
            }
        }
        [TypeConverter(typeof(EntryConverter))]
        public Entry LocalEntry
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get { return _MyEntry; }
        }
        private int _ContentGridCount = 0;
        /// 
        /// Count of ContentGrids for this Content
        /// 
        public int ContentGridCount
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _ContentGridCount;
            }
        }
        private Grid _MyGrid = null;
        /// 
        /// Related Field
        /// 
        [TypeConverter(typeof(GridConverter))]
        public Grid MyGrid
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_MyGrid == null) _MyGrid = Grid.New(this);
                return _MyGrid;
            }
            set
            {
                _MyGrid = value;
            }
        }
        private int _ContentImageCount = 0;
        /// 
        /// Count of ContentImages for this Content
        /// 
        public int ContentImageCount
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _ContentImageCount;
            }
        }
        private Image _MyImage = null;
        /// 
        /// Related Field
        /// 
        [TypeConverter(typeof(ImageConverter))]
        public Image MyImage
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_MyImage == null) _MyImage = Image.New(this);
                return _MyImage;
            }
        }
        private int _ContentItemCount = 0;
        /// 
        /// Count of ContentItems for this Content
        /// 
        public int ContentItemCount
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _ContentItemCount;
            }
        }
        private ContentItems _ContentItems = null;
        /// 
        /// Related Field
        /// 
        [TypeConverter(typeof(ContentItemsConverter))]
        public ContentItems ContentItems
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_ContentItemCount < 0 || (_ContentItemCount > 0 && _ContentItems == null))
                    _ContentItems = ContentItems.GetByContentID(ContentID);
                if (_ContentItemCount < 0)
                    _ContentItemCount = _ContentItems == null ? 0 : _ContentItems.Count;
                if (_ContentItems == null)
                    _ContentItems = ContentItems.New();
                return _ContentItems;
            }
        }
        public void Reset_ContentItems()
        {
            _ContentItemCount = -1;
        }
        private int _ContentPartCount = 0;
        /// 
        /// Count of ContentParts for this Content
        /// 
        public int ContentPartCount
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _ContentPartCount;
            }
        }
        private ContentParts _ContentParts = null;
        /// 
        /// Related Field
        /// 
        [TypeConverter(typeof(ContentPartsConverter))]
        public ContentParts ContentParts
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_ContentPartCount < 0 || (_ContentPartCount > 0 && _ContentParts == null))
                    _ContentParts = ContentParts.GetByContentID(ContentID);
                if (_ContentPartCount < 0)
                    _ContentPartCount = _ContentParts == null ? 0 : _ContentParts.Count;
                if (_ContentParts == null)
                    _ContentParts = ContentParts.New();
                return _ContentParts;
            }
        }
        public void Reset_ContentParts()
        {
            _ContentPartCount = -1;
        }
        private int _ContentRoUsageCount = 0;
        /// 
        /// Count of ContentRoUsages for this Content
        /// 
        public int ContentRoUsageCount
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_ContentRoUsageCount < 0)
                    _ContentRoUsageCount = ContentRoUsages.Count;
                return _ContentRoUsageCount;
            }
        }
        private ContentRoUsages _ContentRoUsages = null;
        /// 
        /// Related Field
        /// 
        [TypeConverter(typeof(ContentRoUsagesConverter))]
        public ContentRoUsages ContentRoUsages
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_ContentRoUsageCount < 0 || (_ContentRoUsageCount > 0 && _ContentRoUsages == null))
                    _ContentRoUsages = ContentRoUsages.GetByContentID(ContentID);
                if (_ContentRoUsageCount < 0)
                    _ContentRoUsageCount = _ContentRoUsages == null ? 0 : _ContentRoUsages.Count;
                if (_ContentRoUsages == null)
                    _ContentRoUsages = ContentRoUsages.New();
                return _ContentRoUsages;
            }
        }
        public void Reset_ContentRoUsages()
        {
            _ContentRoUsageCount = -1;
        }
        private int _ContentTransitionCount = 0;
        /// 
        /// Count of ContentTransitions for this Content
        /// 
        public int ContentTransitionCount
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_ContentTransitionCount < 0)
                    _ContentTransitionCount = ContentTransitions.Count;
                return _ContentTransitionCount;
            }
        }
        private ContentTransitions _ContentTransitions = null;
        /// 
        /// Related Field
        /// 
        [TypeConverter(typeof(ContentTransitionsConverter))]
        public ContentTransitions ContentTransitions
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_ContentTransitionCount < 0 || (_ContentTransitionCount > 0 && _ContentTransitions == null))
                    _ContentTransitions = ContentTransitions.GetByFromID(ContentID);
                if (_ContentTransitionCount < 0)
                    _ContentTransitionCount = _ContentTransitions == null ? 0 : _ContentTransitions.Count;
                if (_ContentTransitions == null)
                    _ContentTransitions = ContentTransitions.New();
                return _ContentTransitions;
            }
        }
        public void Reset_ContentTransitions()
        {
            _ContentTransitionCount = -1;
        }
        private int _ContentZContentCount = 0;
        /// 
        /// Count of ContentZContents for this Content
        /// 
        public int ContentZContentCount
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                return _ContentZContentCount;
            }
        }
        private ZContent _MyZContent = null;
        /// 
        /// Related Field
        /// 
        [TypeConverter(typeof(ZContentConverter))]
        public ZContent MyZContent
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                if (_MyZContent == null) _MyZContent = ZContent.New(this);
                return _MyZContent;
            }
        }
        public override bool IsDirty
        {
            get
            {
                if (base.IsDirty)
                    return true;
                return IsDirtyList(new List