// ========================================================================
// Copyright 2006 - 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;
namespace Volian.Object.Library
{
///
/// Permission Generated by MyGeneration using the CSLA Object Mapping template
///
[Serializable()]
public partial class Permission : BusinessBase
{
#region Business Methods
private int _pid;
[System.ComponentModel.DataObjectField(true, true)]
public int Pid
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
get
{
return _pid;
}
}
private int _rid;
[System.ComponentModel.DataObjectField(true, true)]
public int Rid
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
get
{
return _rid;
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
set
{
if (_rid != value)
{
_rid = value;
PropertyHasChanged();
}
}
}
private int _permlevel;
///
/// 0 - Security, 1 - System, 2 - RO, 3 - Procdures, 4 - Sections, 5 - Steps, 6 - Comments
///
[System.ComponentModel.DataObjectField(true, true)]
public int PermLevel
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
get
{
return _permlevel;
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
set
{
if (_permlevel != value)
{
_permlevel = value;
PropertyHasChanged();
}
}
}
private int _versiontype;
///
/// 0 - Working Draft, 1 - Temporary Change, 2 Approved
///
[System.ComponentModel.DataObjectField(true, true)]
public int VersionType
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
get
{
return _versiontype;
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
set
{
if (_versiontype != value)
{
_versiontype = value;
PropertyHasChanged();
}
}
}
private int _permvalue;
///
/// 1 - Read, 2 - Write, 4 - Create, 8 - Delete (15 - All)
///
[System.ComponentModel.DataObjectField(true, true)]
public int PermValue
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
get
{
return _permvalue;
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
set
{
if (_permvalue != value)
{
_permvalue = value;
PropertyHasChanged();
}
}
}
private int _permad;
///
/// 0 - Allow, 1 - Deny
///
[System.ComponentModel.DataObjectField(true, true)]
public int Permad
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
get
{
return _permad;
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
set
{
if (_permad != value)
{
_permad = value;
PropertyHasChanged();
}
}
}
private string _startdate = string.Empty;
[System.ComponentModel.DataObjectField(true, true)]
public string StartDate
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
get
{
return _startdate;
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
set
{
if (value == null) value = string.Empty;
_startdate = value;
try
{
DateTime tmp = SmartDate.StringToDate(value);
if (_startdate != tmp.ToShortDateString())
{
_startdate = tmp.ToShortDateString();
// TODO: Any Cross Property Validation
}
}
catch
{
}
PropertyHasChanged();
}
}
private string _enddate = string.Empty;
[System.ComponentModel.DataObjectField(true, true)]
public string EndDate
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
get
{
return _enddate;
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
set
{
if (value == null) value = string.Empty;
_enddate = value;
try
{
DateTime tmp = SmartDate.StringToDate(value);
if (_enddate != tmp.ToShortDateString())
{
_enddate = tmp.ToShortDateString();
// TODO: Any Cross Property Validation
}
}
catch
{
}
PropertyHasChanged();
}
}
private DateTime _dts = new DateTime();
[System.ComponentModel.DataObjectField(true, true)]
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;
[System.ComponentModel.DataObjectField(true, true)]
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
// TODO: Replace base Permission.ToString function as necessary
///
/// Overrides Base ToString
///
/// A string representation of current Permission
//public override string ToString()
//{
// return base.ToString();
//}
// TODO: Check Permission.GetIdValue to assure that the ID returned is unique
///
/// Overrides Base GetIdValue - Used internally by CSLA to determine equality
///
/// A Unique ID for the current Permission
protected override object GetIdValue()
{
return _pid;
}
#endregion
#region ValidationRules
protected override void AddBusinessRules()
{
ValidationRules.AddRule(StartDateValid, "StartDate");
ValidationRules.AddRule(EndDateValid, "EndDate");
ValidationRules.AddRule(
Csla.Validation.CommonRules.StringMaxLength,
new Csla.Validation.CommonRules.MaxLengthRuleArgs("Usrid", 100));
ext.AddValidationRules(ValidationRules);
// TODO: Add other validation rules
// ValidationRules.AddRule(StartDateGTEndDate, "Started");
}
private bool StartDateValid(object target, Csla.Validation.RuleArgs e)
{
try
{
DateTime tmp = SmartDate.StringToDate(_startdate);
}
catch
{
e.Description = "Invalid Date";
return false;
}
return true;
}
private bool EndDateValid(object target, Csla.Validation.RuleArgs e)
{
try
{
DateTime tmp = SmartDate.StringToDate(_enddate);
}
catch
{
e.Description = "Invalid Date";
return false;
}
return true;
}
// Sample data comparison validation rule
//private bool StartDateGTEndDate(object target, Csla.Validation.RuleArgs e)
//{
// if (_started > _ended)
// {
// e.Description = "Start date can't be after end date";
// return false;
// }
// else
// return true;
//}
#endregion
#region Authorization Rules
protected override void AddAuthorizationRules()
{
//TODO: Who can read/write which fields
//AuthorizationRules.AllowRead(Pid, "");
//AuthorizationRules.AllowRead(Rid, "");
//AuthorizationRules.AllowRead(PermLevel, "");
//AuthorizationRules.AllowRead(VersionType, "");
//AuthorizationRules.AllowRead(PermValue, "");
//AuthorizationRules.AllowRead(Permad, "");
//AuthorizationRules.AllowRead(StartDate, "");
//AuthorizationRules.AllowRead(EndDate, "");
//AuthorizationRules.AllowRead(Dts, "");
//AuthorizationRules.AllowRead(Usrid, "");
//AuthorizationRules.AllowWrite(Rid, "");
//AuthorizationRules.AllowWrite(PermLevel, "");
//AuthorizationRules.AllowWrite(VersionType, "");
//AuthorizationRules.AllowWrite(PermValue, "");
//AuthorizationRules.AllowWrite(Permad, "");
//AuthorizationRules.AllowWrite(StartDate, "");
//AuthorizationRules.AllowWrite(EndDate, "");
//AuthorizationRules.AllowWrite(Dts, "");
//AuthorizationRules.AllowWrite(Usrid, "");
ext.AddAuthorizationRules(AuthorizationRules);
}
public static bool CanAddObject()
{
// TODO: Can Add Authorization
//return Csla.ApplicationContext.User.IsInRole("ProjectManager");
return true;
}
public static bool CanGetObject()
{
// TODO: CanGet Authorization
return true;
}
public static bool CanDeleteObject()
{
// TODO: CanDelete Authorization
//bool result = false;
//if (Csla.ApplicationContext.User.IsInRole("ProjectManager"))result = true;
//if (Csla.ApplicationContext.User.IsInRole("Administrator"))result = true;
//return result;
return true;
}
public static bool CanEditObject()
{
// TODO: CanEdit Authorization
//return Csla.ApplicationContext.User.IsInRole("ProjectManager");
return true;
}
#endregion
#region Factory Methods
private Permission()
{/* require use of factory methods */}
public static Permission New()
{
if (!CanAddObject())
throw new System.Security.SecurityException("User not authorized to add a Permission");
return DataPortal.Create();
}
public static Permission Get(int _pid)
{
if (!CanGetObject())
throw new System.Security.SecurityException("User not authorized to view a Permission");
return DataPortal.Fetch(new PKCriteria(_pid));
}
public static void Delete(int _pid)
{
if (!CanDeleteObject())
throw new System.Security.SecurityException("User not authorized to remove a Permission");
DataPortal.Delete(new PKCriteria(_pid));
}
public override Permission Save()
{
if (IsDeleted && !CanDeleteObject())
throw new System.Security.SecurityException("User not authorized to remove a Permission");
else if (IsNew && !CanAddObject())
throw new System.Security.SecurityException("User not authorized to add a Permission");
else if (!CanEditObject())
throw new System.Security.SecurityException("User not authorized to update a Permission");
return base.Save();
}
#endregion
#region Data Access Portal
[Serializable()]
private class PKCriteria
{
private int _pid;
public int Pid
{ get { return _pid; } }
public PKCriteria(int pid)
{
_pid = pid;
}
}
// If Create needs to access DB - It should not be marked RunLocal
[RunLocal()]
private new void DataPortal_Create(object criteria)
{
// Database Defaults
_permad = ext.DefaultPermad;
_startdate = ext.DefaultStartDate;
_dts = ext.DefaultDts;
_usrid = ext.DefaultUsrid;
// TODO: Add any defaults that are necessary
ValidationRules.CheckRules();
}
private void DataPortal_Fetch(PKCriteria criteria)
{
try
{
using (SqlConnection cn = Database.VEPROMS_SqlConnection)
{
using (SqlCommand cm = cn.CreateCommand())
{
cm.CommandType = CommandType.StoredProcedure;
cm.CommandText = "getPermission";
cm.Parameters.AddWithValue("@pID", criteria.Pid);
using (SafeDataReader dr = new SafeDataReader(cm.ExecuteReader()))
{
dr.Read();
_pid = dr.GetInt32("PID");
_rid = dr.GetInt32("RID");
_permlevel = dr.GetInt32("PermLevel");
_versiontype = dr.GetInt32("VersionType");
_permvalue = dr.GetInt32("PermValue");
_permad = dr.GetInt32("PermAD");
_startdate = dr.GetSmartDate("StartDate").Text;
_enddate = dr.GetSmartDate("EndDate").Text;
_dts = dr.GetDateTime("DTS");
_usrid = dr.GetString("UsrID");
dr.GetBytes("LastChanged", 0, _lastchanged, 0, 8);
}
}
}
}
catch (Exception ex)
{
Database.LogException("Permission", ex);
}
}
[Transactional(TransactionalTypes.TransactionScope)]
protected override void DataPortal_Insert()
{
try
{
using (SqlConnection cn = Database.VEPROMS_SqlConnection)
{
using (SqlCommand cm = cn.CreateCommand())
{
cm.CommandType = CommandType.StoredProcedure;
cm.CommandText = "addPermission";
// Input All Fields - Except Calculated Columns
cm.Parameters.AddWithValue("@rID", _rid);
cm.Parameters.AddWithValue("@permLevel", _permlevel);
cm.Parameters.AddWithValue("@versionType", _versiontype);
cm.Parameters.AddWithValue("@permValue", _permvalue);
cm.Parameters.AddWithValue("@permAD", _permad);
cm.Parameters.AddWithValue("@startDate", new SmartDate(_startdate).DBValue);
cm.Parameters.AddWithValue("@endDate", new SmartDate(_enddate).DBValue);
cm.Parameters.AddWithValue("@dTS", _dts);
cm.Parameters.AddWithValue("@usrID", _usrid);
// Output Calculated Columns
SqlParameter param_pid = new SqlParameter("@newPID", SqlDbType.Int);
param_pid.Direction = ParameterDirection.Output;
cm.Parameters.Add(param_pid);
SqlParameter param_lastchanged = new SqlParameter("@newLastChanged", SqlDbType.Timestamp);
param_lastchanged.Direction = ParameterDirection.Output;
cm.Parameters.Add(param_lastchanged);
// TODO: Define any additional output parameters
cm.ExecuteNonQuery();
// Save all values being returned from the Procedure
_pid = (int)cm.Parameters["@newPID"].Value;
_lastchanged = (byte[])cm.Parameters["@newLastChanged"].Value;
}
// update child objects
}
}
catch (Exception ex)
{
Database.LogException("Permission", ex);
}
}
[Transactional(TransactionalTypes.TransactionScope)]
public static byte[] Add(SqlConnection cn, ref int pid, int rid, int permlevel, int versiontype, int permvalue, int permad, SmartDate startdate, SmartDate enddate, DateTime dts, string usrid)
{
try
{
using (SqlCommand cm = cn.CreateCommand())
{
cm.CommandType = CommandType.StoredProcedure;
cm.CommandText = "addPermission";
// Input All Fields - Except Calculated Columns
cm.Parameters.AddWithValue("@rID", rid);
cm.Parameters.AddWithValue("@permLevel", permlevel);
cm.Parameters.AddWithValue("@versionType", versiontype);
cm.Parameters.AddWithValue("@permValue", permvalue);
cm.Parameters.AddWithValue("@permAD", permad);
cm.Parameters.AddWithValue("@startDate", startdate.DBValue);
cm.Parameters.AddWithValue("@endDate", enddate.DBValue);
cm.Parameters.AddWithValue("@dTS", dts);
cm.Parameters.AddWithValue("@usrID", usrid);
// Output Calculated Columns
SqlParameter param_pid = new SqlParameter("@newPID", SqlDbType.Int);
param_pid.Direction = ParameterDirection.Output;
cm.Parameters.Add(param_pid);
SqlParameter param_lastchanged = new SqlParameter("@newLastChanged", SqlDbType.Timestamp);
param_lastchanged.Direction = ParameterDirection.Output;
cm.Parameters.Add(param_lastchanged);
// TODO: Define any additional output parameters
cm.ExecuteNonQuery();
// Save all values being returned from the Procedure
pid = (int)cm.Parameters["@newPID"].Value;
return (byte[])cm.Parameters["@newLastChanged"].Value;
}
}
catch (Exception ex)
{
Database.LogException("Permission", ex);
return null;
}
}
[Transactional(TransactionalTypes.TransactionScope)]
protected override void DataPortal_Update()
{
if (IsDirty)// If this is dirty - open the connection
{
try
{
using (SqlConnection cn = Database.VEPROMS_SqlConnection)
{
if (base.IsDirty)
{
using (SqlCommand cm = cn.CreateCommand())
{
cm.CommandType = CommandType.StoredProcedure;
cm.CommandText = "updatePermission";
// All Fields including Calculated Fields
cm.Parameters.AddWithValue("@pID", _pid);
cm.Parameters.AddWithValue("@rID", _rid);
cm.Parameters.AddWithValue("@permLevel", _permlevel);
cm.Parameters.AddWithValue("@versionType", _versiontype);
cm.Parameters.AddWithValue("@permValue", _permvalue);
cm.Parameters.AddWithValue("@permAD", _permad);
cm.Parameters.AddWithValue("@startDate", new SmartDate(_startdate).DBValue);
cm.Parameters.AddWithValue("@endDate", new SmartDate(_enddate).DBValue);
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);
param_lastchanged.Direction = ParameterDirection.Output;
cm.Parameters.Add(param_lastchanged);
// TODO: Define any additional output parameters
cm.ExecuteNonQuery();
// Save all values being returned from the Procedure
_lastchanged = (byte[])cm.Parameters["@newLastChanged"].Value;
}
}
// use the open connection to update child objects
}
}
catch (Exception ex)
{
Database.LogException("Permission", ex);
}
}
}
[Transactional(TransactionalTypes.TransactionScope)]
public static byte[] Update(SqlConnection cn, ref int pid, int rid, int permlevel, int versiontype, int permvalue, int permad, SmartDate startdate, SmartDate enddate, DateTime dts, string usrid, ref byte[] lastchanged)
{
try
{
using (SqlCommand cm = cn.CreateCommand())
{
cm.CommandType = CommandType.StoredProcedure;
cm.CommandText = "updatePermission";
// Input All Fields - Except Calculated Columns
cm.Parameters.AddWithValue("@pID", pid);
cm.Parameters.AddWithValue("@rID", rid);
cm.Parameters.AddWithValue("@permLevel", permlevel);
cm.Parameters.AddWithValue("@versionType", versiontype);
cm.Parameters.AddWithValue("@permValue", permvalue);
cm.Parameters.AddWithValue("@permAD", permad);
cm.Parameters.AddWithValue("@startDate", startdate.DBValue);
cm.Parameters.AddWithValue("@endDate", enddate.DBValue);
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);
param_lastchanged.Direction = ParameterDirection.Output;
cm.Parameters.Add(param_lastchanged);
// TODO: Define any additional output parameters
cm.ExecuteNonQuery();
// Save all values being returned from the Procedure
return (byte[])cm.Parameters["@newLastChanged"].Value;
}
}
catch (Exception ex)
{
Database.LogException("Permission", ex);
return null;
}
}
[Transactional(TransactionalTypes.TransactionScope)]
protected override void DataPortal_DeleteSelf()
{
DataPortal_Delete(new PKCriteria(_pid));
}
[Transactional(TransactionalTypes.TransactionScope)]
private void DataPortal_Delete(PKCriteria criteria)
{
try
{
using (SqlConnection cn = Database.VEPROMS_SqlConnection)
{
using (SqlCommand cm = cn.CreateCommand())
{
cm.CommandType = CommandType.StoredProcedure;
cm.CommandText = "deletePermission";
cm.Parameters.AddWithValue("@pID", criteria.Pid);
cm.ExecuteNonQuery();
}
}
}
catch (Exception ex)
{
Database.LogException("Permission", ex);
}
}
[Transactional(TransactionalTypes.TransactionScope)]
public static void Remove(SqlConnection cn, int pid)
{
try
{
using (SqlCommand cm = cn.CreateCommand())
{
cm.CommandType = CommandType.StoredProcedure;
cm.CommandText = "deletePermission";
// Input PK Fields
cm.Parameters.AddWithValue("@pID", pid);
// TODO: Define any additional output parameters
cm.ExecuteNonQuery();
}
}
catch (Exception ex)
{
Database.LogException("Permission", ex);
}
}
#endregion
#region Exists
public static bool Exists(int pid)
{
ExistsCommand result;
result = DataPortal.Execute
(new ExistsCommand(pid));
return result.Exists;
}
[Serializable()]
private class ExistsCommand : CommandBase
{
private int _pid;
private bool _exists;
public bool Exists
{
get { return _exists; }
}
public ExistsCommand(int pid)
{
_pid = pid;
}
protected override void DataPortal_Execute()
{
try
{
using (SqlConnection cn = Database.VEPROMS_SqlConnection)
{
cn.Open();
using (SqlCommand cm = cn.CreateCommand())
{
cm.CommandType = CommandType.StoredProcedure;
cm.CommandText = "existsPermission";
cm.Parameters.AddWithValue("@pID", _pid);
int count = (int)cm.ExecuteScalar();
_exists = (count > 0);
}
}
}
catch (Exception ex)
{
Database.LogException("Permission", ex);
}
}
}
#endregion
// Standard Default Code
// #region extension
Extension ext = new Extension();
[Serializable()]
partial class Extension : extensionBase
{
}
[Serializable()]
class extensionBase
{
// Default Values
public virtual int DefaultPermad
{
get { return 0; }
}
public virtual string DefaultStartDate
{
get { return DateTime.Now.ToShortDateString(); }
}
public virtual DateTime DefaultDts
{
get { return DateTime.Now; }
}
public virtual string DefaultUsrid
{
get { return Environment.UserName.ToUpper(); }
}
// Authorization Rules
public virtual void AddAuthorizationRules(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
}
}
//#endregion
// The follwing is a sample Extension File. You can use it to create PermissionExt.cs
//using System;
//using System.Collections.Generic;
//using System.Text;
//using Csla;
//namespace PatrialClass
//{
// public partial class Permission
// {
// partial class Extension : extensionBase
// {
// TODO: Override automatic defaults
// public virtual int DefaultPermad
// {
// get { return 0; }
// }
// public virtual SmartDate DefaultStartDate
// {
// get { return DateTime.Now.ToShortDateString(); }
// }
// public virtual DateTime DefaultDts
// {
// get { return DateTime.Now; }
// }
// public virtual string DefaultUsrid
// {
// get { return Environment.UserName.ToUpper(); }
// }
// public new void AddAuthorizationRules(Csla.Security.AuthorizationRules rules)
// {
// //rules.AllowRead(Dbid, "");
// }
// public new void AddValidationRules(Csla.Validation.ValidationRules rules)
// {
// rules.AddRule(
// Csla.Validation.CommonRules.StringMaxLength,
// new Csla.Validation.CommonRules.MaxLengthRuleArgs("Name", 100));
// }
// }
// }
//}
} // Class
} // Namespace