1099 lines
46 KiB
C#
1099 lines
46 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.ComponentModel;
|
|
using System.Collections.Generic;
|
|
using Csla.Validation;
|
|
namespace VEPROMS.CSLA.Library
|
|
{
|
|
/// <summary>
|
|
/// Role Generated by MyGeneration using the CSLA Object Mapping template
|
|
/// </summary>
|
|
[Serializable()]
|
|
[TypeConverter(typeof(RoleConverter))]
|
|
public partial class Role : BusinessBase<Role>, IDisposable, IVEHasBrokenRules
|
|
{
|
|
#region Log4Net
|
|
private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
|
|
#endregion
|
|
#region Refresh
|
|
private List<Role> _RefreshRoles = new List<Role>();
|
|
private List<RoleAssignment> _RefreshRoleAssignments = new List<RoleAssignment>();
|
|
private List<RolePermission> _RefreshRolePermissions = new List<RolePermission>();
|
|
private void AddToRefreshList(List<Role> refreshRoles, List<RoleAssignment> refreshRoleAssignments, List<RolePermission> refreshRolePermissions)
|
|
{
|
|
if (IsDirty)
|
|
refreshRoles.Add(this);
|
|
if (_RoleAssignments != null && _RoleAssignments.IsDirty)
|
|
{
|
|
foreach (RoleAssignment tmp in _RoleAssignments)
|
|
{
|
|
if (tmp.IsDirty) refreshRoleAssignments.Add(tmp);
|
|
}
|
|
}
|
|
if (_RolePermissions != null && _RolePermissions.IsDirty)
|
|
{
|
|
foreach (RolePermission tmp in _RolePermissions)
|
|
{
|
|
if (tmp.IsDirty) refreshRolePermissions.Add(tmp);
|
|
}
|
|
}
|
|
}
|
|
private void ClearRefreshList()
|
|
{
|
|
_RefreshRoles = new List<Role>();
|
|
_RefreshRoleAssignments = new List<RoleAssignment>();
|
|
_RefreshRolePermissions = new List<RolePermission>();
|
|
}
|
|
private void BuildRefreshList()
|
|
{
|
|
ClearRefreshList();
|
|
AddToRefreshList(_RefreshRoles, _RefreshRoleAssignments, _RefreshRolePermissions);
|
|
}
|
|
private void ProcessRefreshList()
|
|
{
|
|
foreach (Role tmp in _RefreshRoles)
|
|
{
|
|
RoleInfo.Refresh(tmp);
|
|
}
|
|
foreach (RoleAssignment tmp in _RefreshRoleAssignments)
|
|
{
|
|
AssignmentInfo.Refresh(tmp);
|
|
}
|
|
foreach (RolePermission tmp in _RefreshRolePermissions)
|
|
{
|
|
PermissionInfo.Refresh(tmp);
|
|
}
|
|
ClearRefreshList();
|
|
}
|
|
#endregion
|
|
#region Collection
|
|
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0044:Add readonly modifier", Justification = "Keeping Collections Not ReadOnly")]
|
|
private static List<Role> _CacheList = new List<Role>();
|
|
protected static void AddToCache(Role role)
|
|
{
|
|
if (!_CacheList.Contains(role)) _CacheList.Add(role); // In AddToCache
|
|
}
|
|
protected static void RemoveFromCache(Role role)
|
|
{
|
|
while (_CacheList.Contains(role)) _CacheList.Remove(role); // In RemoveFromCache
|
|
}
|
|
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0044:Add readonly modifier", Justification = "Keeping Collections Not ReadOnly")]
|
|
private static Dictionary<string, List<Role>> _CacheByPrimaryKey = new Dictionary<string, List<Role>>();
|
|
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0044:Add readonly modifier", Justification = "Keeping Collections Not ReadOnly")]
|
|
private static Dictionary<string, List<Role>> _CacheByName = new Dictionary<string, List<Role>>();
|
|
private static void ConvertListToDictionary()
|
|
{
|
|
while (_CacheList.Count > 0) // Move Role(s) from temporary _CacheList to _CacheByPrimaryKey
|
|
{
|
|
Role tmp = _CacheList[0]; // Get the first Role
|
|
string pKey = tmp.RID.ToString();
|
|
if (!_CacheByPrimaryKey.ContainsKey(pKey))
|
|
{
|
|
_CacheByPrimaryKey[pKey] = new List<Role>(); // Add new list for PrimaryKey
|
|
_CacheByName[tmp.Name.ToString()] = new List<Role>(); // Add new list for Name
|
|
}
|
|
_CacheByPrimaryKey[pKey].Add(tmp); // Add to Primary Key list
|
|
_CacheByName[tmp.Name.ToString()].Add(tmp); // Unique Index
|
|
_CacheList.RemoveAt(0); // Remove the first Role
|
|
}
|
|
}
|
|
protected static Role GetCachedByPrimaryKey(int rid)
|
|
{
|
|
ConvertListToDictionary();
|
|
string key = rid.ToString();
|
|
if (_CacheByPrimaryKey.ContainsKey(key)) return _CacheByPrimaryKey[key][0];
|
|
return null;
|
|
}
|
|
protected static Role GetCachedByName(string name)
|
|
{
|
|
ConvertListToDictionary();
|
|
string key = name.ToString();
|
|
if (_CacheByName.ContainsKey(key)) return _CacheByName[key][0];
|
|
return null;
|
|
}
|
|
#endregion
|
|
#region Business Methods
|
|
private string _ErrorMessage = string.Empty;
|
|
public string ErrorMessage => _ErrorMessage;
|
|
private static int _nextRID = -1;
|
|
public static int NextRID => _nextRID--;
|
|
private int _RID;
|
|
[System.ComponentModel.DataObjectField(true, true)]
|
|
public int RID
|
|
{
|
|
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
|
|
get
|
|
{
|
|
return _RID;
|
|
}
|
|
}
|
|
private string _Name = string.Empty;
|
|
public string Name
|
|
{
|
|
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
|
|
get
|
|
{
|
|
return _Name;
|
|
}
|
|
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
|
|
set
|
|
{
|
|
if (value == null) value = string.Empty;
|
|
if (_Name != value)
|
|
{
|
|
_Name = value;
|
|
PropertyHasChanged();
|
|
}
|
|
}
|
|
}
|
|
private string _Title = string.Empty;
|
|
public string Title
|
|
{
|
|
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
|
|
get
|
|
{
|
|
return _Title;
|
|
}
|
|
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
|
|
set
|
|
{
|
|
if (value == null) value = string.Empty;
|
|
if (_Title != value)
|
|
{
|
|
_Title = 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 _UsrID = string.Empty;
|
|
public string UsrID
|
|
{
|
|
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
|
|
get
|
|
{
|
|
return _UsrID;
|
|
}
|
|
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
|
|
set
|
|
{
|
|
if (value == null) value = string.Empty;
|
|
if (_UsrID != value)
|
|
{
|
|
_UsrID = value;
|
|
PropertyHasChanged();
|
|
}
|
|
}
|
|
}
|
|
private byte[] _LastChanged = new byte[8];//timestamp
|
|
private int _RoleAssignmentCount = 0;
|
|
/// <summary>
|
|
/// Count of RoleAssignments for this Role
|
|
/// </summary>
|
|
public int RoleAssignmentCount
|
|
{
|
|
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
|
|
get
|
|
{
|
|
return _RoleAssignmentCount;
|
|
}
|
|
}
|
|
private RoleAssignments _RoleAssignments = null;
|
|
/// <summary>
|
|
/// Related Field
|
|
/// </summary>
|
|
[TypeConverter(typeof(RoleAssignmentsConverter))]
|
|
public RoleAssignments RoleAssignments
|
|
{
|
|
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
|
|
get
|
|
{
|
|
if (_RoleAssignmentCount < 0 || (_RoleAssignmentCount > 0 && _RoleAssignments == null))
|
|
_RoleAssignments = RoleAssignments.GetByRID(RID);
|
|
if (_RoleAssignmentCount < 0)
|
|
_RoleAssignmentCount = _RoleAssignments == null ? 0 : _RoleAssignments.Count;
|
|
if (_RoleAssignments == null)
|
|
_RoleAssignments = RoleAssignments.New();
|
|
return _RoleAssignments;
|
|
}
|
|
}
|
|
public void Reset_RoleAssignments() => _RoleAssignmentCount = -1;
|
|
private int _RolePermissionCount = 0;
|
|
/// <summary>
|
|
/// Count of RolePermissions for this Role
|
|
/// </summary>
|
|
public int RolePermissionCount
|
|
{
|
|
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
|
|
get
|
|
{
|
|
return _RolePermissionCount;
|
|
}
|
|
}
|
|
private RolePermissions _RolePermissions = null;
|
|
/// <summary>
|
|
/// Related Field
|
|
/// </summary>
|
|
[TypeConverter(typeof(RolePermissionsConverter))]
|
|
public RolePermissions RolePermissions
|
|
{
|
|
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
|
|
get
|
|
{
|
|
if (_RolePermissionCount < 0 || (_RolePermissionCount > 0 && _RolePermissions == null))
|
|
_RolePermissions = RolePermissions.GetByRID(RID);
|
|
if (_RolePermissionCount < 0)
|
|
_RolePermissionCount = _RolePermissions == null ? 0 : _RolePermissions.Count;
|
|
if (_RolePermissions == null)
|
|
_RolePermissions = RolePermissions.New();
|
|
return _RolePermissions;
|
|
}
|
|
}
|
|
public void Reset_RolePermissions() => _RolePermissionCount = -1;
|
|
public override bool IsDirty
|
|
{
|
|
get
|
|
{
|
|
if (base.IsDirty)
|
|
return true;
|
|
return IsDirtyList(new List<object>());
|
|
}
|
|
}
|
|
public bool IsDirtyList(List<object> list)
|
|
{
|
|
if (base.IsDirty || list.Contains(this))
|
|
return base.IsDirty;
|
|
list.Add(this);
|
|
return base.IsDirty || (_RoleAssignments != null && _RoleAssignments.IsDirtyList(list)) || (_RolePermissions != null && _RolePermissions.IsDirtyList(list));
|
|
}
|
|
public override bool IsValid => IsValidList(new List<object>());
|
|
public bool IsValidList(List<object> list)
|
|
{
|
|
if (list.Contains(this))
|
|
return (IsNew && !IsDirty) || base.IsValid;
|
|
list.Add(this);
|
|
return ((IsNew && !IsDirty) || base.IsValid) && (_RoleAssignments == null || _RoleAssignments.IsValidList(list)) && (_RolePermissions == null || _RolePermissions.IsValidList(list));
|
|
}
|
|
// CSLATODO: Check Role.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 Role</returns>
|
|
protected override object GetIdValue() => MyRoleUnique; // Absolutely Unique ID
|
|
#endregion
|
|
#region ValidationRules
|
|
[NonSerialized]
|
|
private bool _CheckingBrokenRules = false;
|
|
public IVEHasBrokenRules HasBrokenRules
|
|
{
|
|
get
|
|
{
|
|
if (_CheckingBrokenRules) return null;
|
|
if ((IsDirty || !IsNew) && BrokenRulesCollection.Count > 0) return this;
|
|
try
|
|
{
|
|
_CheckingBrokenRules = true;
|
|
IVEHasBrokenRules hasBrokenRules = null;
|
|
if (_RoleAssignments != null && (hasBrokenRules = _RoleAssignments.HasBrokenRules) != null) return hasBrokenRules;
|
|
if (_RolePermissions != null && (hasBrokenRules = _RolePermissions.HasBrokenRules) != null) return hasBrokenRules;
|
|
return hasBrokenRules;
|
|
}
|
|
finally
|
|
{
|
|
_CheckingBrokenRules = false;
|
|
}
|
|
}
|
|
}
|
|
public BrokenRulesCollection BrokenRules
|
|
{
|
|
get
|
|
{
|
|
IVEHasBrokenRules hasBrokenRules = HasBrokenRules;
|
|
if (Equals(hasBrokenRules)) return BrokenRulesCollection;
|
|
return hasBrokenRules?.BrokenRules;
|
|
}
|
|
}
|
|
protected override void AddBusinessRules()
|
|
{
|
|
ValidationRules.AddRule(
|
|
Csla.Validation.CommonRules.StringRequired, "Name");
|
|
ValidationRules.AddRule(
|
|
Csla.Validation.CommonRules.StringMaxLength,
|
|
new Csla.Validation.CommonRules.MaxLengthRuleArgs("Name", 50));
|
|
ValidationRules.AddRule(
|
|
Csla.Validation.CommonRules.StringRequired, "Title");
|
|
ValidationRules.AddRule(
|
|
Csla.Validation.CommonRules.StringMaxLength,
|
|
new Csla.Validation.CommonRules.MaxLengthRuleArgs("Title", 250));
|
|
ValidationRules.AddRule(
|
|
Csla.Validation.CommonRules.StringRequired, "UsrID");
|
|
ValidationRules.AddRule(
|
|
Csla.Validation.CommonRules.StringMaxLength,
|
|
new Csla.Validation.CommonRules.MaxLengthRuleArgs("UsrID", 100));
|
|
//ValidationRules.AddDependantProperty("x", "y");
|
|
_RoleExtension.AddValidationRules(ValidationRules);
|
|
// CSLATODO: Add other validation rules
|
|
}
|
|
protected override void AddInstanceBusinessRules()
|
|
{
|
|
_RoleExtension.AddInstanceValidationRules(ValidationRules);
|
|
// CSLATODO: Add other validation rules
|
|
}
|
|
#endregion
|
|
#region Authorization Rules
|
|
protected override void AddAuthorizationRules()
|
|
{
|
|
//CSLATODO: Who can read/write which fields
|
|
_RoleExtension.AddAuthorizationRules(AuthorizationRules);
|
|
}
|
|
protected override void AddInstanceAuthorizationRules()
|
|
{
|
|
//CSLATODO: Who can read/write which fields
|
|
_RoleExtension.AddInstanceAuthorizationRules(AuthorizationRules);
|
|
}
|
|
#endregion
|
|
#region Factory Methods
|
|
public int CurrentEditLevel => EditLevel;
|
|
private static int _RoleUnique = 0;
|
|
protected static int RoleUnique => ++_RoleUnique;
|
|
private readonly int _MyRoleUnique = RoleUnique;
|
|
// Absolutely Unique ID - Editable
|
|
public int MyRoleUnique => _MyRoleUnique;
|
|
protected Role()
|
|
{/* require use of factory methods */
|
|
AddToCache(this);
|
|
}
|
|
private bool _Disposed = false;
|
|
private static int _CountCreated = 0;
|
|
private static int _CountDisposed = 0;
|
|
private static int _CountFinalized = 0;
|
|
private static int IncrementCountCreated => ++_CountCreated;
|
|
private readonly int _CountWhenCreated = IncrementCountCreated;
|
|
public static int CountCreated => _CountCreated;
|
|
public static int CountNotDisposed => _CountCreated - _CountDisposed;
|
|
public static int CountNotFinalized => _CountCreated - _CountFinalized;
|
|
~Role()
|
|
{
|
|
_CountFinalized++;
|
|
}
|
|
public void Dispose()
|
|
{
|
|
if (_Disposed) return;
|
|
_CountDisposed++;
|
|
_Disposed = true;
|
|
RemoveFromDictionaries();
|
|
}
|
|
private void RemoveFromDictionaries()
|
|
{
|
|
RemoveFromCache(this);
|
|
if (_CacheByPrimaryKey.ContainsKey(RID.ToString()))
|
|
{
|
|
List<Role> listRole = _CacheByPrimaryKey[RID.ToString()]; // Get the list of items
|
|
while (listRole.Contains(this)) listRole.Remove(this); // Remove the item from the list
|
|
if (listRole.Count == 0) //If there are no items left in the list
|
|
_CacheByPrimaryKey.Remove(RID.ToString()); // remove the list
|
|
}
|
|
string myKey;
|
|
myKey = null;
|
|
foreach (string key in _CacheByName.Keys)
|
|
if (_CacheByName[key].Contains(this))
|
|
myKey = key;
|
|
if (myKey != null)
|
|
{
|
|
List<Role> listRole = _CacheByName[myKey]; // Get the list of items
|
|
listRole.Remove(this); // Remove the item from the list
|
|
if (listRole.Count == 0) //If there are no items left in the list
|
|
_CacheByName.Remove(myKey); // remove the list
|
|
}
|
|
}
|
|
public static Role New()
|
|
{
|
|
try
|
|
{
|
|
return DataPortal.Create<Role>();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new DbCslaException("Error on Role.New", ex);
|
|
}
|
|
}
|
|
public static Role New(string name, string title)
|
|
{
|
|
Role tmp = Role.New();
|
|
tmp.Name = name;
|
|
tmp.Title = title;
|
|
return tmp;
|
|
}
|
|
public static Role MakeRole(string name, string title)
|
|
{
|
|
Role tmp = Role.New(name, title);
|
|
if (tmp.IsSavable)
|
|
tmp = tmp.Save();
|
|
else
|
|
{
|
|
Csla.Validation.BrokenRulesCollection brc = tmp.ValidationRules.GetBrokenRules();
|
|
tmp._ErrorMessage = "Failed Validation:";
|
|
foreach (Csla.Validation.BrokenRule br in brc)
|
|
{
|
|
tmp._ErrorMessage += "\r\n\tFailure: " + br.RuleName;
|
|
}
|
|
}
|
|
return tmp;
|
|
}
|
|
public static Role New(string name, string title, DateTime dts, string usrID)
|
|
{
|
|
Role tmp = Role.New();
|
|
tmp.Name = name;
|
|
tmp.Title = title;
|
|
tmp.DTS = dts;
|
|
tmp.UsrID = usrID;
|
|
return tmp;
|
|
}
|
|
public static Role MakeRole(string name, string title, DateTime dts, string usrID)
|
|
{
|
|
Role tmp = Role.New(name, title, dts, usrID);
|
|
if (tmp.IsSavable)
|
|
tmp = tmp.Save();
|
|
else
|
|
{
|
|
Csla.Validation.BrokenRulesCollection brc = tmp.ValidationRules.GetBrokenRules();
|
|
tmp._ErrorMessage = "Failed Validation:";
|
|
foreach (Csla.Validation.BrokenRule br in brc)
|
|
{
|
|
tmp._ErrorMessage += "\r\n\tFailure: " + br.RuleName;
|
|
}
|
|
}
|
|
return tmp;
|
|
}
|
|
public static Role Get(int rid)
|
|
{
|
|
try
|
|
{
|
|
Role tmp = GetCachedByPrimaryKey(rid);
|
|
if (tmp == null)
|
|
{
|
|
tmp = DataPortal.Fetch<Role>(new PKCriteria(rid));
|
|
AddToCache(tmp);
|
|
}
|
|
if (tmp.ErrorMessage == "No Record Found")
|
|
{
|
|
tmp.Dispose(); // Clean-up Role
|
|
tmp = null;
|
|
}
|
|
return tmp;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new DbCslaException("Error on Role.Get", ex);
|
|
}
|
|
}
|
|
public static Role GetByName(string name)
|
|
{
|
|
try
|
|
{
|
|
Role tmp = GetCachedByName(name);
|
|
if (tmp == null)
|
|
{
|
|
tmp = DataPortal.Fetch<Role>(new NameCriteria(name));
|
|
AddToCache(tmp);
|
|
}
|
|
if (tmp.ErrorMessage == "No Record Found")
|
|
{
|
|
tmp.Dispose(); // Clean-up Role
|
|
tmp = null;
|
|
}
|
|
return tmp;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new DbCslaException("Error on Role.GetByName", ex);
|
|
}
|
|
}
|
|
public static Role Get(SafeDataReader dr)
|
|
{
|
|
if (dr.Read()) return new Role(dr);
|
|
return null;
|
|
}
|
|
internal Role(SafeDataReader dr) => ReadData(dr);
|
|
public static void Delete(int rid)
|
|
{
|
|
try
|
|
{
|
|
DataPortal.Delete(new PKCriteria(rid));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new DbCslaException("Error on Role.Delete", ex);
|
|
}
|
|
}
|
|
public override Role Save()
|
|
{
|
|
try
|
|
{
|
|
BuildRefreshList();
|
|
Role role = base.Save();
|
|
RemoveFromDictionaries(); // if save is successful remove the previous Folder from the cache
|
|
AddToCache(role);//Refresh the item in AllList
|
|
ProcessRefreshList();
|
|
return role;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new DbCslaException("Error on CSLA Save", ex);
|
|
}
|
|
}
|
|
#endregion
|
|
#region Data Access Portal
|
|
[Serializable()]
|
|
protected class PKCriteria
|
|
{
|
|
private readonly int _RID;
|
|
public int RID => _RID;
|
|
public PKCriteria(int rid) => _RID = rid;
|
|
}
|
|
[Serializable()]
|
|
private class NameCriteria
|
|
{
|
|
private readonly string _Name;
|
|
public string Name => _Name;
|
|
public NameCriteria(string name) => _Name = name;
|
|
}
|
|
// CSLATODO: If Create needs to access DB - It should not be marked RunLocal
|
|
[RunLocal()]
|
|
private new void DataPortal_Create()
|
|
{
|
|
_RID = NextRID;
|
|
// Database Defaults
|
|
_DTS = _RoleExtension.DefaultDTS;
|
|
_UsrID = _RoleExtension.DefaultUsrID;
|
|
// CSLATODO: Add any defaults that are necessary
|
|
ValidationRules.CheckRules();
|
|
}
|
|
private void ReadData(SafeDataReader dr)
|
|
{
|
|
if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] Role.ReadData", GetHashCode());
|
|
try
|
|
{
|
|
_RID = dr.GetInt32("RID");
|
|
_Name = dr.GetString("Name");
|
|
_Title = dr.GetString("Title");
|
|
_DTS = dr.GetDateTime("DTS");
|
|
_UsrID = dr.GetString("UsrID");
|
|
dr.GetBytes("LastChanged", 0, _LastChanged, 0, 8);
|
|
_RoleAssignmentCount = dr.GetInt32("AssignmentCount");
|
|
_RolePermissionCount = dr.GetInt32("PermissionCount");
|
|
MarkOld();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (_MyLog.IsErrorEnabled) _MyLog.Error("Role.ReadData", ex);
|
|
_ErrorMessage = ex.Message;
|
|
throw new DbCslaException("Role.ReadData", ex);
|
|
}
|
|
}
|
|
private void DataPortal_Fetch(PKCriteria criteria)
|
|
{
|
|
if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] Role.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 = "getRole";
|
|
cm.Parameters.AddWithValue("@RID", criteria.RID);
|
|
cm.CommandTimeout = Database.DefaultTimeout;
|
|
using (SafeDataReader dr = new SafeDataReader(cm.ExecuteReader()))
|
|
{
|
|
if (!dr.Read())
|
|
{
|
|
_ErrorMessage = "No Record Found";
|
|
return;
|
|
}
|
|
ReadData(dr);
|
|
// load child objects
|
|
dr.NextResult();
|
|
_RoleAssignments = RoleAssignments.Get(dr);
|
|
// load child objects
|
|
dr.NextResult();
|
|
_RolePermissions = RolePermissions.Get(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("Role.DataPortal_Fetch", ex);
|
|
_ErrorMessage = ex.Message;
|
|
throw new DbCslaException("Role.DataPortal_Fetch", ex);
|
|
}
|
|
}
|
|
private void DataPortal_Fetch(NameCriteria criteria)
|
|
{
|
|
if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] Role.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 = "getRoleByName";
|
|
cm.Parameters.AddWithValue("@Name", criteria.Name);
|
|
cm.CommandTimeout = Database.DefaultTimeout;
|
|
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("Role.DataPortal_Fetch", ex);
|
|
_ErrorMessage = ex.Message;
|
|
throw new DbCslaException("Role.DataPortal_Fetch", ex);
|
|
}
|
|
}
|
|
[Transactional(TransactionalTypes.TransactionScope)]
|
|
protected override void DataPortal_Insert()
|
|
{
|
|
try
|
|
{
|
|
using (SqlConnection cn = Database.VEPROMS_SqlConnection)
|
|
{
|
|
ApplicationContext.LocalContext["cn"] = cn;
|
|
SQLInsert();
|
|
// 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("Role.DataPortal_Insert", ex);
|
|
_ErrorMessage = ex.Message;
|
|
throw new DbCslaException("Role.DataPortal_Insert", ex);
|
|
}
|
|
finally
|
|
{
|
|
if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] Role.DataPortal_Insert", GetHashCode());
|
|
}
|
|
}
|
|
[Transactional(TransactionalTypes.TransactionScope)]
|
|
internal void SQLInsert()
|
|
{
|
|
if (!IsDirty) return;
|
|
try
|
|
{
|
|
using (SqlConnection cn = Database.VEPROMS_SqlConnection)
|
|
{
|
|
if (cn.State == System.Data.ConnectionState.Closed) cn.Open();
|
|
using (SqlCommand cm = cn.CreateCommand())
|
|
{
|
|
cm.CommandType = CommandType.StoredProcedure;
|
|
cm.CommandTimeout = Database.SQLTimeout;
|
|
cm.CommandText = "addRole";
|
|
// Input All Fields - Except Calculated Columns
|
|
cm.Parameters.AddWithValue("@Name", _Name);
|
|
cm.Parameters.AddWithValue("@Title", _Title);
|
|
if (_DTS.Year >= 1753 && _DTS.Year <= 9999) cm.Parameters.AddWithValue("@DTS", _DTS);
|
|
cm.Parameters.AddWithValue("@UsrID", _UsrID);
|
|
// Output Calculated Columns
|
|
SqlParameter param_RID = new SqlParameter("@newRID", SqlDbType.Int)
|
|
{
|
|
Direction = ParameterDirection.Output
|
|
};
|
|
cm.Parameters.Add(param_RID);
|
|
SqlParameter param_LastChanged = new SqlParameter("@newLastChanged", SqlDbType.Timestamp)
|
|
{
|
|
Direction = ParameterDirection.Output
|
|
};
|
|
cm.Parameters.Add(param_LastChanged);
|
|
// CSLATODO: Define any additional output parameters
|
|
cm.ExecuteNonQuery();
|
|
// Save all values being returned from the Procedure
|
|
_RID = (int)cm.Parameters["@newRID"].Value;
|
|
_LastChanged = (byte[])cm.Parameters["@newLastChanged"].Value;
|
|
}
|
|
}
|
|
|
|
MarkOld();
|
|
// update child objects
|
|
_RoleAssignments?.Update(this);
|
|
_RolePermissions?.Update(this);
|
|
if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] Role.SQLInsert", GetHashCode());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (_MyLog.IsErrorEnabled) _MyLog.Error("Role.SQLInsert", ex);
|
|
_ErrorMessage = ex.Message;
|
|
throw new DbCslaException("Role.SQLInsert", ex);
|
|
}
|
|
}
|
|
[Transactional(TransactionalTypes.TransactionScope)]
|
|
public static byte[] Add(SqlConnection cn, ref int rid, string name, string title, DateTime dts, string usrID)
|
|
{
|
|
if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] Role.Add", 0);
|
|
try
|
|
{
|
|
using (SqlCommand cm = cn.CreateCommand())
|
|
{
|
|
cm.CommandType = CommandType.StoredProcedure;
|
|
cm.CommandTimeout = Database.SQLTimeout;
|
|
cm.CommandText = "addRole";
|
|
// Input All Fields - Except Calculated Columns
|
|
cm.Parameters.AddWithValue("@Name", name);
|
|
cm.Parameters.AddWithValue("@Title", title);
|
|
if (dts.Year >= 1753 && dts.Year <= 9999) cm.Parameters.AddWithValue("@DTS", dts);
|
|
cm.Parameters.AddWithValue("@UsrID", usrID);
|
|
// Output Calculated Columns
|
|
SqlParameter param_RID = new SqlParameter("@newRID", SqlDbType.Int)
|
|
{
|
|
Direction = ParameterDirection.Output
|
|
};
|
|
cm.Parameters.Add(param_RID);
|
|
SqlParameter param_LastChanged = new SqlParameter("@newLastChanged", SqlDbType.Timestamp)
|
|
{
|
|
Direction = ParameterDirection.Output
|
|
};
|
|
cm.Parameters.Add(param_LastChanged);
|
|
// CSLATODO: Define any additional output parameters
|
|
cm.ExecuteNonQuery();
|
|
// Save all values being returned from the Procedure
|
|
rid = (int)cm.Parameters["@newRID"].Value;
|
|
return (byte[])cm.Parameters["@newLastChanged"].Value;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (_MyLog.IsErrorEnabled) _MyLog.Error("Role.Add", ex);
|
|
throw new DbCslaException("Role.Add", ex);
|
|
}
|
|
}
|
|
[Transactional(TransactionalTypes.TransactionScope)]
|
|
protected override void DataPortal_Update()
|
|
{
|
|
if (!IsDirty) return; // If not dirty - nothing to do
|
|
if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] Role.DataPortal_Update", GetHashCode());
|
|
try
|
|
{
|
|
using (SqlConnection cn = Database.VEPROMS_SqlConnection)
|
|
{
|
|
ApplicationContext.LocalContext["cn"] = cn;
|
|
SQLUpdate();
|
|
// 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("Role.DataPortal_Update", ex);
|
|
_ErrorMessage = ex.Message;
|
|
if (!ex.Message.EndsWith("has been edited by another user.")) throw ex;
|
|
}
|
|
}
|
|
[Transactional(TransactionalTypes.TransactionScope)]
|
|
internal void SQLUpdate()
|
|
{
|
|
if (!IsDirty) return; // If not dirty - nothing to do
|
|
if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] Role.SQLUpdate", GetHashCode());
|
|
try
|
|
{
|
|
using (SqlConnection cn = Database.VEPROMS_SqlConnection)
|
|
{
|
|
if (cn.State == System.Data.ConnectionState.Closed) cn.Open();
|
|
if (base.IsDirty)
|
|
{
|
|
using (SqlCommand cm = cn.CreateCommand())
|
|
{
|
|
cm.CommandType = CommandType.StoredProcedure;
|
|
cm.CommandTimeout = Database.SQLTimeout;
|
|
cm.CommandText = "updateRole";
|
|
// All Fields including Calculated Fields
|
|
cm.Parameters.AddWithValue("@RID", _RID);
|
|
cm.Parameters.AddWithValue("@Name", _Name);
|
|
cm.Parameters.AddWithValue("@Title", _Title);
|
|
if (_DTS.Year >= 1753 && _DTS.Year <= 9999) cm.Parameters.AddWithValue("@DTS", _DTS);
|
|
cm.Parameters.AddWithValue("@UsrID", _UsrID);
|
|
cm.Parameters.AddWithValue("@LastChanged", _LastChanged);
|
|
// Output Calculated Columns
|
|
SqlParameter param_LastChanged = new SqlParameter("@newLastChanged", SqlDbType.Timestamp)
|
|
{
|
|
Direction = ParameterDirection.Output
|
|
};
|
|
cm.Parameters.Add(param_LastChanged);
|
|
// CSLATODO: Define any additional output parameters
|
|
cm.ExecuteNonQuery();
|
|
// Save all values being returned from the Procedure
|
|
_LastChanged = (byte[])cm.Parameters["@newLastChanged"].Value;
|
|
}
|
|
}
|
|
}
|
|
|
|
MarkOld();
|
|
// use the open connection to update child objects
|
|
_RoleAssignments?.Update(this);
|
|
_RolePermissions?.Update(this);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (_MyLog.IsErrorEnabled) _MyLog.Error("Role.SQLUpdate", ex);
|
|
_ErrorMessage = ex.Message;
|
|
if (!ex.Message.EndsWith("has been edited by another user.")) throw ex;
|
|
}
|
|
}
|
|
internal void Update()
|
|
{
|
|
if (!IsDirty) return;
|
|
if (base.IsDirty)
|
|
{
|
|
using (SqlConnection cn = Database.VEPROMS_SqlConnection)
|
|
{
|
|
if (cn.State == System.Data.ConnectionState.Closed) cn.Open();
|
|
if (IsNew)
|
|
_LastChanged = Role.Add(cn, ref _RID, _Name, _Title, _DTS, _UsrID);
|
|
else
|
|
_LastChanged = Role.Update(cn, ref _RID, _Name, _Title, _DTS, _UsrID, ref _LastChanged);
|
|
}
|
|
|
|
MarkOld();
|
|
}
|
|
_RoleAssignments?.Update(this);
|
|
_RolePermissions?.Update(this);
|
|
}
|
|
[Transactional(TransactionalTypes.TransactionScope)]
|
|
public static byte[] Update(SqlConnection cn, ref int rid, string name, string title, DateTime dts, string usrID, ref byte[] lastChanged)
|
|
{
|
|
if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] Role.Update", 0);
|
|
try
|
|
{
|
|
using (SqlCommand cm = cn.CreateCommand())
|
|
{
|
|
cm.CommandType = CommandType.StoredProcedure;
|
|
cm.CommandTimeout = Database.SQLTimeout;
|
|
cm.CommandText = "updateRole";
|
|
// Input All Fields - Except Calculated Columns
|
|
cm.Parameters.AddWithValue("@RID", rid);
|
|
cm.Parameters.AddWithValue("@Name", name);
|
|
cm.Parameters.AddWithValue("@Title", title);
|
|
if (dts.Year >= 1753 && dts.Year <= 9999) cm.Parameters.AddWithValue("@DTS", dts);
|
|
cm.Parameters.AddWithValue("@UsrID", usrID);
|
|
cm.Parameters.AddWithValue("@LastChanged", lastChanged);
|
|
// Output Calculated Columns
|
|
SqlParameter param_LastChanged = new SqlParameter("@newLastChanged", SqlDbType.Timestamp)
|
|
{
|
|
Direction = ParameterDirection.Output
|
|
};
|
|
cm.Parameters.Add(param_LastChanged);
|
|
// CSLATODO: Define any additional output parameters
|
|
cm.ExecuteNonQuery();
|
|
// Save all values being returned from the Procedure
|
|
return (byte[])cm.Parameters["@newLastChanged"].Value;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (_MyLog.IsErrorEnabled) _MyLog.Error("Role.Update", ex);
|
|
throw new DbCslaException("Role.Update", ex);
|
|
}
|
|
}
|
|
[Transactional(TransactionalTypes.TransactionScope)]
|
|
protected override void DataPortal_DeleteSelf()
|
|
{
|
|
DataPortal_Delete(new PKCriteria(_RID));
|
|
}
|
|
[Transactional(TransactionalTypes.TransactionScope)]
|
|
private void DataPortal_Delete(PKCriteria criteria)
|
|
{
|
|
if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] Role.DataPortal_Delete", GetHashCode());
|
|
try
|
|
{
|
|
using (SqlConnection cn = Database.VEPROMS_SqlConnection)
|
|
{
|
|
using (SqlCommand cm = cn.CreateCommand())
|
|
{
|
|
cm.CommandType = CommandType.StoredProcedure;
|
|
cm.CommandTimeout = Database.SQLTimeout;
|
|
cm.CommandText = "deleteRole";
|
|
cm.Parameters.AddWithValue("@RID", criteria.RID);
|
|
cm.ExecuteNonQuery();
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (_MyLog.IsErrorEnabled) _MyLog.Error("Role.DataPortal_Delete", ex);
|
|
_ErrorMessage = ex.Message;
|
|
throw new DbCslaException("Role.DataPortal_Delete", ex);
|
|
}
|
|
}
|
|
[Transactional(TransactionalTypes.TransactionScope)]
|
|
public static void Remove(SqlConnection cn, int rid)
|
|
{
|
|
if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] Role.Remove", 0);
|
|
try
|
|
{
|
|
using (SqlCommand cm = cn.CreateCommand())
|
|
{
|
|
cm.CommandType = CommandType.StoredProcedure;
|
|
cm.CommandTimeout = Database.SQLTimeout;
|
|
cm.CommandText = "deleteRole";
|
|
// Input PK Fields
|
|
cm.Parameters.AddWithValue("@RID", rid);
|
|
// CSLATODO: Define any additional output parameters
|
|
cm.ExecuteNonQuery();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (_MyLog.IsErrorEnabled) _MyLog.Error("Role.Remove", ex);
|
|
throw new DbCslaException("Role.Remove", ex);
|
|
}
|
|
}
|
|
#endregion
|
|
#region Exists
|
|
public static bool Exists(int rid)
|
|
{
|
|
ExistsCommand result;
|
|
try
|
|
{
|
|
result = DataPortal.Execute<ExistsCommand>(new ExistsCommand(rid));
|
|
return result.Exists;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new DbCslaException("Error on Role.Exists", ex);
|
|
}
|
|
}
|
|
[Serializable()]
|
|
private class ExistsCommand : CommandBase
|
|
{
|
|
private readonly int _RID;
|
|
private bool _exists;
|
|
public bool Exists => _exists;
|
|
public ExistsCommand(int rid) => _RID = rid;
|
|
protected override void DataPortal_Execute()
|
|
{
|
|
if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] Role.DataPortal_Execute", GetHashCode());
|
|
try
|
|
{
|
|
using (SqlConnection cn = Database.VEPROMS_SqlConnection)
|
|
{
|
|
if (cn.State == System.Data.ConnectionState.Closed) cn.Open();
|
|
using (SqlCommand cm = cn.CreateCommand())
|
|
{
|
|
cm.CommandType = CommandType.StoredProcedure;
|
|
cm.CommandTimeout = Database.SQLTimeout;
|
|
cm.CommandText = "existsRole";
|
|
cm.Parameters.AddWithValue("@RID", _RID);
|
|
int count = (int)cm.ExecuteScalar();
|
|
_exists = (count > 0);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (_MyLog.IsErrorEnabled) _MyLog.Error("Role.DataPortal_Execute", ex);
|
|
throw new DbCslaException("Role.DataPortal_Execute", ex);
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
// Standard Default Code
|
|
#region extension
|
|
readonly RoleExtension _RoleExtension = new RoleExtension();
|
|
[Serializable()]
|
|
partial class RoleExtension : extensionBase
|
|
{
|
|
}
|
|
[Serializable()]
|
|
class extensionBase
|
|
{
|
|
// Default Values
|
|
public virtual DateTime DefaultDTS => DateTime.Now;
|
|
public virtual string DefaultUsrID => Volian.Base.Library.VlnSettings.UserID;
|
|
// Authorization Rules
|
|
public virtual void AddAuthorizationRules(Csla.Security.AuthorizationRules rules)
|
|
{
|
|
// Needs to be overriden to add new authorization rules
|
|
}
|
|
// Instance Authorization Rules
|
|
public virtual void AddInstanceAuthorizationRules(Csla.Security.AuthorizationRules rules)
|
|
{
|
|
// Needs to be overriden to add new authorization rules
|
|
}
|
|
// Validation Rules
|
|
public virtual void AddValidationRules(Csla.Validation.ValidationRules rules)
|
|
{
|
|
// Needs to be overriden to add new validation rules
|
|
}
|
|
// InstanceValidation Rules
|
|
public virtual void AddInstanceValidationRules(Csla.Validation.ValidationRules rules)
|
|
{
|
|
// Needs to be overriden to add new validation rules
|
|
}
|
|
}
|
|
#endregion
|
|
} // Class
|
|
#region Converter
|
|
internal class RoleConverter : ExpandableObjectConverter
|
|
{
|
|
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destType)
|
|
{
|
|
if (destType == typeof(string) && value is Role role)
|
|
{
|
|
// Return the ToString value
|
|
return role.ToString();
|
|
}
|
|
return base.ConvertTo(context, culture, value, destType);
|
|
}
|
|
}
|
|
#endregion
|
|
} // Namespace
|