485 lines
15 KiB
C#

// ========================================================================
// 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;
namespace VEPROMS.CSLA.Library
{
public delegate void RODbInfoEvent(object sender);
/// <summary>
/// RODbInfo Generated by MyGeneration using the CSLA Object Mapping template
/// </summary>
[Serializable()]
[TypeConverter(typeof(RODbInfoConverter))]
public partial class RODbInfo : ReadOnlyBase<RODbInfo>, IDisposable
{
public event RODbInfoEvent Changed;
private void OnChange()
{
if (Changed != null) Changed(this);
}
#region Log4Net
private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
#endregion
#region Collection
private static List<RODbInfo> _CacheList = new List<RODbInfo>();
protected static void AddToCache(RODbInfo rODbInfo)
{
if (!_CacheList.Contains(rODbInfo)) _CacheList.Add(rODbInfo); // In AddToCache
}
protected static void RemoveFromCache(RODbInfo rODbInfo)
{
while (_CacheList.Contains(rODbInfo)) _CacheList.Remove(rODbInfo); // In RemoveFromCache
}
private static Dictionary<string, List<RODbInfo>> _CacheByPrimaryKey = new Dictionary<string, List<RODbInfo>>();
private static void ConvertListToDictionary()
{
while (_CacheList.Count > 0) // Move RODbInfo(s) from temporary _CacheList to _CacheByPrimaryKey
{
RODbInfo tmp = _CacheList[0]; // Get the first RODbInfo
string pKey = tmp.RODbID.ToString();
if (!_CacheByPrimaryKey.ContainsKey(pKey))
{
_CacheByPrimaryKey[pKey] = new List<RODbInfo>(); // Add new list for PrimaryKey
}
_CacheByPrimaryKey[pKey].Add(tmp); // Add to Primary Key list
_CacheList.RemoveAt(0); // Remove the first RODbInfo
}
}
internal static void AddList(RODbInfoList lst)
{
foreach (RODbInfo item in lst) AddToCache(item);
}
protected static RODbInfo GetCachedByPrimaryKey(int rODbID)
{
ConvertListToDictionary();
string key = rODbID.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; }
}
protected RODb _Editable;
private IVEHasBrokenRules HasBrokenRules
{
get
{
IVEHasBrokenRules hasBrokenRules = null;
if (_Editable != null)
hasBrokenRules = _Editable.HasBrokenRules;
return hasBrokenRules;
}
}
private int _RODbID;
[System.ComponentModel.DataObjectField(true, true)]
public int RODbID
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
get
{
CanReadProperty("RODbID", true);
return _RODbID;
}
}
private string _ROName = string.Empty;
/// <summary>
/// Hook for future - to allow the user to select multiple RO Databases assocaiated with on DocVersion
/// </summary>
public string ROName
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
get
{
CanReadProperty("ROName", true);
return _ROName;
}
}
private string _FolderPath = string.Empty;
/// <summary>
/// Path to the RO database
/// </summary>
public string FolderPath
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
get
{
CanReadProperty("FolderPath", true);
return _FolderPath;
}
}
private string _DBConnectionString = string.Empty;
/// <summary>
/// Connection String - Default could just be the full path and name of the database
/// </summary>
public string DBConnectionString
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
get
{
CanReadProperty("DBConnectionString", true);
return _DBConnectionString;
}
}
private string _Config = string.Empty;
public string Config
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
get
{
CanReadProperty("Config", true);
return _Config;
}
}
private DateTime _DTS = new DateTime();
public DateTime DTS
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
get
{
CanReadProperty("DTS", true);
return _DTS;
}
}
private string _UserID = string.Empty;
public string UserID
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
get
{
CanReadProperty("UserID", true);
return _UserID;
}
}
private int _RODbROFstCount = 0;
/// <summary>
/// Count of RODbROFsts for this RODb
/// </summary>
public int RODbROFstCount
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
get
{
CanReadProperty("RODbROFstCount", true);
if (_RODbROFstCount < 0)
_RODbROFstCount = RODbROFsts.Count;
return _RODbROFstCount;
}
}
private ROFstInfoList _RODbROFsts = null;
[TypeConverter(typeof(ROFstInfoListConverter))]
public ROFstInfoList RODbROFsts
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
get
{
CanReadProperty("RODbROFsts", true);
if (_RODbROFstCount < 0 || (_RODbROFstCount > 0 && _RODbROFsts == null))
_RODbROFsts = ROFstInfoList.GetByRODbID(_RODbID);
if (_RODbROFstCount < 0)
_RODbROFstCount = _RODbROFsts.Count;
return _RODbROFsts;
}
}
public void RefreshRODbROFsts()
{
_RODbROFstCount = -1;
ConvertListToDictionary();
if (_CacheByPrimaryKey.ContainsKey(_RODbID.ToString()))
foreach (RODbInfo tmp in _CacheByPrimaryKey[_RODbID.ToString()])
tmp._RODbROFstCount = -1; // This will cause the data to be requeried
}
private int _RODbROImageCount = 0;
/// <summary>
/// Count of RODbROImages for this RODb
/// </summary>
public int RODbROImageCount
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
get
{
CanReadProperty("RODbROImageCount", true);
if (_RODbROImageCount < 0)
_RODbROImageCount = RODbROImages.Count;
return _RODbROImageCount;
}
}
private ROImageInfoList _RODbROImages = null;
[TypeConverter(typeof(ROImageInfoListConverter))]
public ROImageInfoList RODbROImages
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
get
{
CanReadProperty("RODbROImages", true);
if (_RODbROImageCount < 0 || (_RODbROImageCount > 0 && _RODbROImages == null))
_RODbROImages = ROImageInfoList.GetByRODbID(_RODbID);
if (_RODbROImageCount < 0)
_RODbROImageCount = _RODbROImages.Count;
return _RODbROImages;
}
}
public void RefreshRODbROImages()
{
_RODbROImageCount = -1;
ConvertListToDictionary();
if (_CacheByPrimaryKey.ContainsKey(_RODbID.ToString()))
foreach (RODbInfo tmp in _CacheByPrimaryKey[_RODbID.ToString()])
tmp._RODbROImageCount = -1; // This will cause the data to be requeried
}
private int _RODbRoUsageCount = 0;
/// <summary>
/// Count of RODbRoUsages for this RODb
/// </summary>
public int RODbRoUsageCount
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
get
{
CanReadProperty("RODbRoUsageCount", true);
if (_RODbRoUsageCount < 0)
_RODbRoUsageCount = RODbRoUsages.Count;
return _RODbRoUsageCount;
}
}
private RoUsageInfoList _RODbRoUsages = null;
[TypeConverter(typeof(RoUsageInfoListConverter))]
public RoUsageInfoList RODbRoUsages
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
get
{
CanReadProperty("RODbRoUsages", true);
if (_RODbRoUsageCount < 0 || (_RODbRoUsageCount > 0 && _RODbRoUsages == null))
_RODbRoUsages = RoUsageInfoList.GetByRODbID(_RODbID);
if (_RODbRoUsageCount < 0)
_RODbRoUsageCount = _RODbRoUsages.Count;
return _RODbRoUsages;
}
}
public void RefreshRODbRoUsages()
{
_RODbRoUsageCount = -1;
ConvertListToDictionary();
if (_CacheByPrimaryKey.ContainsKey(_RODbID.ToString()))
foreach (RODbInfo tmp in _CacheByPrimaryKey[_RODbID.ToString()])
tmp._RODbRoUsageCount = -1; // This will cause the data to be requeried
}
// CSLATODO: Replace base RODbInfo.ToString function as necessary
/// <summary>
/// Overrides Base ToString
/// </summary>
/// <returns>A string representation of current RODbInfo</returns>
//public override string ToString()
//{
// return base.ToString();
//}
// CSLATODO: Check RODbInfo.GetIdValue to assure that the ID returned is unique
/// <summary>
/// Overrides Base GetIdValue - Used internally by CSLA to determine equality
/// </summary>
/// <returns>A Unique ID for the current RODbInfo</returns>
protected override object GetIdValue()
{
return MyRODbInfoUnique; // Absolutely Unique ID
}
#endregion
#region Factory Methods
private static int _RODbInfoUnique = 0;
private static int RODbInfoUnique
{ get { return ++_RODbInfoUnique; } }
private int _MyRODbInfoUnique = RODbInfoUnique;
public int MyRODbInfoUnique // Absolutely Unique ID - Info
{ get { return _MyRODbInfoUnique; } }
protected RODbInfo()
{/* require use of factory methods */
AddToCache(this);
}
public void Dispose()
{
RemoveFromCache(this);
if (!_CacheByPrimaryKey.ContainsKey(RODbID.ToString())) return;
List<RODbInfo> listRODbInfo = _CacheByPrimaryKey[RODbID.ToString()]; // Get the list of items
while (listRODbInfo.Contains(this)) listRODbInfo.Remove(this); // Remove the item from the list
if (listRODbInfo.Count == 0) // If there are no items left in the list
_CacheByPrimaryKey.Remove(RODbID.ToString()); // remove the list
}
public virtual RODb Get()
{
return _Editable = RODb.Get(_RODbID);
}
public static void Refresh(RODb tmp)
{
string key = tmp.RODbID.ToString();
ConvertListToDictionary();
if (_CacheByPrimaryKey.ContainsKey(key))
foreach (RODbInfo tmpInfo in _CacheByPrimaryKey[key])
tmpInfo.RefreshFields(tmp);
}
protected virtual void RefreshFields(RODb tmp)
{
_ROName = tmp.ROName;
_FolderPath = tmp.FolderPath;
_DBConnectionString = tmp.DBConnectionString;
_Config = tmp.Config;
_DTS = tmp.DTS;
_UserID = tmp.UserID;
_RODbInfoExtension.Refresh(this);
OnChange();// raise an event
}
public static RODbInfo Get(int rODbID)
{
//if (!CanGetObject())
// throw new System.Security.SecurityException("User not authorized to view a RODb");
try
{
RODbInfo tmp = GetCachedByPrimaryKey(rODbID);
if (tmp == null)
{
tmp = DataPortal.Fetch<RODbInfo>(new PKCriteria(rODbID));
AddToCache(tmp);
}
if (tmp.ErrorMessage == "No Record Found")
{
tmp.Dispose(); // Clean-up RODbInfo
tmp = null;
}
return tmp;
}
catch (Exception ex)
{
throw new DbCslaException("Error on RODbInfo.Get", ex);
}
}
#endregion
#region Data Access Portal
internal RODbInfo(SafeDataReader dr)
{
if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] RODbInfo.Constructor", GetHashCode());
try
{
ReadData(dr);
}
catch (Exception ex)
{
if (_MyLog.IsErrorEnabled) _MyLog.Error("RODbInfo.Constructor", ex);
throw new DbCslaException("RODbInfo.Constructor", ex);
}
}
[Serializable()]
protected class PKCriteria
{
private int _RODbID;
public int RODbID
{ get { return _RODbID; } }
public PKCriteria(int rODbID)
{
_RODbID = rODbID;
}
}
private void ReadData(SafeDataReader dr)
{
if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] RODbInfo.ReadData", GetHashCode());
try
{
_RODbID = dr.GetInt32("RODbID");
_ROName = dr.GetString("ROName");
_FolderPath = dr.GetString("FolderPath");
_DBConnectionString = dr.GetString("DBConnectionString");
_Config = dr.GetString("Config");
_DTS = dr.GetDateTime("DTS");
_UserID = dr.GetString("UserID");
_RODbROFstCount = dr.GetInt32("ROFstCount");
_RODbROImageCount = dr.GetInt32("ROImageCount");
_RODbRoUsageCount = dr.GetInt32("RoUsageCount");
}
catch (Exception ex)
{
if (_MyLog.IsErrorEnabled) _MyLog.Error("RODbInfo.ReadData", ex);
_ErrorMessage = ex.Message;
throw new DbCslaException("RODbInfo.ReadData", ex);
}
}
private void DataPortal_Fetch(PKCriteria criteria)
{
if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] RODbInfo.DataPortal_Fetch", GetHashCode());
try
{
using (SqlConnection cn = Database.VEPROMS_SqlConnection)
{
ApplicationContext.LocalContext["cn"] = cn;
using (SqlCommand cm = cn.CreateCommand())
{
cm.CommandType = CommandType.StoredProcedure;
cm.CommandText = "getRODb";
cm.Parameters.AddWithValue("@RODbID", criteria.RODbID);
using (SafeDataReader dr = new SafeDataReader(cm.ExecuteReader()))
{
if (!dr.Read())
{
_ErrorMessage = "No Record Found";
return;
}
ReadData(dr);
}
}
// removing of item only needed for local data portal
if (ApplicationContext.ExecutionLocation == ApplicationContext.ExecutionLocations.Client)
ApplicationContext.LocalContext.Remove("cn");
}
}
catch (Exception ex)
{
if (_MyLog.IsErrorEnabled) _MyLog.Error("RODbInfo.DataPortal_Fetch", ex);
_ErrorMessage = ex.Message;
throw new DbCslaException("RODbInfo.DataPortal_Fetch", ex);
}
}
#endregion
// Standard Refresh
#region extension
RODbInfoExtension _RODbInfoExtension = new RODbInfoExtension();
[Serializable()]
partial class RODbInfoExtension : extensionBase { }
[Serializable()]
class extensionBase
{
// Default Refresh
public virtual void Refresh(RODbInfo tmp) { }
}
#endregion
} // Class
#region Converter
internal class RODbInfoConverter : ExpandableObjectConverter
{
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destType)
{
if (destType == typeof(string) && value is RODbInfo)
{
// Return the ToString value
return ((RODbInfo)value).ToString();
}
return base.ConvertTo(context, culture, value, destType);
}
}
#endregion
} // Namespace