// ======================================================================== // 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; using System.Configuration; using System.IO; namespace Volian.CSLA.Library { /// /// Assignment Generated by MyGeneration using the CSLA Object Mapping template /// [Serializable()] public partial class Assignment : BusinessBase { #region Business Methods private string _errorMessage = string.Empty; public string ErrorMessage { get { return _errorMessage; } } private static int _nextAID = -1; public static int NextAID { get { return _nextAID--; } } private int _AID; [System.ComponentModel.DataObjectField(true, true)] public int AID { [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] get { CanReadProperty(true); return _AID; } } private int _GID; public int GID { [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] get { CanReadProperty(true); return _GID; } [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] set { CanWriteProperty(true); if (_GID != value) { _GID = value; PropertyHasChanged(); } } } private int _RID; public int RID { [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] get { CanReadProperty(true); return _RID; } [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] set { CanWriteProperty(true); if (_RID != value) { _RID = value; PropertyHasChanged(); } } } private int _FolderID; public int FolderID { [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] get { CanReadProperty(true); return _FolderID; } [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] set { CanWriteProperty(true); if (_FolderID != value) { _FolderID = value; PropertyHasChanged(); } } } private string _StartDate = string.Empty; public string StartDate { [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] get { CanReadProperty(true); return _StartDate; } [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] set { CanWriteProperty(true); if (value == null) value = string.Empty; _StartDate = value; try { SmartDate tmp = new SmartDate(value); if (_StartDate != tmp.ToString()) { _StartDate = tmp.ToString(); // TODO: Any Cross Property Validation } } catch { } PropertyHasChanged(); } } private string _EndDate = string.Empty; public string EndDate { [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] get { CanReadProperty(true); return _EndDate; } [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] set { CanWriteProperty(true); if (value == null) value = string.Empty; _EndDate = value; try { SmartDate tmp = new SmartDate(value); if (_EndDate != tmp.ToString()) { _EndDate = tmp.ToString(); // TODO: Any Cross Property Validation } } catch { } PropertyHasChanged(); } } private DateTime _DTS = new DateTime(); public DateTime DTS { [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] get { CanReadProperty(true); return _DTS; } [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] set { CanWriteProperty(true); if (_DTS != value) { _DTS = value; PropertyHasChanged(); } } } private string _UsrID = string.Empty; public string UsrID { [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] get { CanReadProperty(true); return _UsrID; } [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] set { CanWriteProperty(true); if (value == null) value = string.Empty; if (_UsrID != value) { _UsrID = value; PropertyHasChanged(); } } } private byte[] _LastChanged = new byte[8];//timestamp // TODO: Replace base Assignment.ToString function as necessary /// /// Overrides Base ToString /// /// A string representation of current Assignment //public override string ToString() //{ // return base.ToString(); //} // TODO: Check Assignment.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 Assignment protected override object GetIdValue() { return _AID; } #endregion #region ValidationRules protected override void AddBusinessRules() { ValidationRules.AddRule( Csla.Validation.CommonRules.StringRequired, "StartDate"); ValidationRules.AddRule(StartDateValid, "StartDate"); ValidationRules.AddRule(EndDateValid, "EndDate"); ValidationRules.AddRule( Csla.Validation.CommonRules.StringRequired, "UsrID"); 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(AID, ""); //AuthorizationRules.AllowRead(GID, ""); //AuthorizationRules.AllowRead(RID, ""); //AuthorizationRules.AllowRead(FolderID, ""); //AuthorizationRules.AllowRead(StartDate, ""); //AuthorizationRules.AllowRead(EndDate, ""); //AuthorizationRules.AllowRead(DTS, ""); //AuthorizationRules.AllowRead(UsrID, ""); //AuthorizationRules.AllowWrite(GID, ""); //AuthorizationRules.AllowWrite(RID, ""); //AuthorizationRules.AllowWrite(FolderID, ""); //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 public int CurrentEditLevel { get { return EditLevel; } } private Assignment() {/* require use of factory methods */} public static Assignment New() { if (!CanAddObject()) throw new System.Security.SecurityException("User not authorized to add a Assignment"); return DataPortal.Create(); } public static Assignment New(int gid, int rid, int folderID, string startDate, string endDate) { Assignment tmp = Assignment.New(); tmp.GID = gid; tmp.RID = rid; tmp.FolderID = folderID; tmp.StartDate = startDate; tmp.EndDate = endDate; return tmp; } public static Assignment MakeAssignment(int gid, int rid, int folderID, string startDate, string endDate) { Assignment tmp = Assignment.New(gid, rid, folderID, startDate, endDate); tmp.Save(); return tmp; } public static Assignment MakeAssignment(int gid, int rid, int folderID, string startDate, string endDate, DateTime dts, string usrID) { Assignment tmp = Assignment.New(gid, rid, folderID, startDate, endDate); if (dts >= new DateTime(1753, 1, 1) && dts <= new DateTime(9999, 12, 31)) tmp.DTS = dts; if (usrID != null && usrID != string.Empty) tmp.UsrID = usrID; tmp.Save(); return tmp; } public static Assignment Get(int aid) { if (!CanGetObject()) throw new System.Security.SecurityException("User not authorized to view a Assignment"); return DataPortal.Fetch(new PKCriteria(aid)); } public static Assignment Get(SafeDataReader dr) { if (dr.Read()) return new Assignment(dr); return null; } private Assignment(SafeDataReader dr) { _AID = dr.GetInt32("AID"); _GID = dr.GetInt32("GID"); _RID = dr.GetInt32("RID"); _FolderID = dr.GetInt32("FolderID"); _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); } public static void Delete(int aid) { if (!CanDeleteObject()) throw new System.Security.SecurityException("User not authorized to remove a Assignment"); DataPortal.Delete(new PKCriteria(aid)); } public override Assignment Save() { if (IsDeleted && !CanDeleteObject()) throw new System.Security.SecurityException("User not authorized to remove a Assignment"); else if (IsNew && !CanAddObject()) throw new System.Security.SecurityException("User not authorized to add a Assignment"); else if (!CanEditObject()) throw new System.Security.SecurityException("User not authorized to update a Assignment"); return base.Save(); } #endregion #region Data Access Portal [Serializable()] private class PKCriteria { private int _AID; public int AID { get { return _AID; } } public PKCriteria(int aid) { _AID = aid; } } // TODO: If Create needs to access DB - It should not be marked RunLocal [RunLocal()] private new void DataPortal_Create(object criteria) { _AID = NextAID; // Database Defaults _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 = "getAssignment"; cm.Parameters.AddWithValue("@AID", criteria.AID); using (SafeDataReader dr = new SafeDataReader(cm.ExecuteReader())) { dr.Read(); _AID = dr.GetInt32("AID"); _GID = dr.GetInt32("GID"); _RID = dr.GetInt32("RID"); _FolderID = dr.GetInt32("FolderID"); _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("Assignment.DataPortal_Fetch", ex); _errorMessage = ex.Message; throw new DbCslaException("Assignment.DataPortal_Fetch", 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 = "addAssignment"; // Input All Fields - Except Calculated Columns cm.Parameters.AddWithValue("@GID", _GID); cm.Parameters.AddWithValue("@RID", _RID); cm.Parameters.AddWithValue("@FolderID", _FolderID); 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_AID = new SqlParameter("@newAID", SqlDbType.Int); param_AID.Direction = ParameterDirection.Output; cm.Parameters.Add(param_AID); 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 _AID = (int)cm.Parameters["@newAID"].Value; _LastChanged = (byte[])cm.Parameters["@newLastChanged"].Value; } // update child objects } } catch (Exception ex) { Database.LogException("Assignment.DataPortal_Insert", ex); _errorMessage = ex.Message; throw new DbCslaException("Assignment.DataPortal_Fetch", ex); } } [Transactional(TransactionalTypes.TransactionScope)] public static byte[] Add(SqlConnection cn, ref int aid, int gid, int rid, int folderID, SmartDate startDate, SmartDate endDate, DateTime dts, string usrID) { try { using (SqlCommand cm = cn.CreateCommand()) { cm.CommandType = CommandType.StoredProcedure; cm.CommandText = "addAssignment"; // Input All Fields - Except Calculated Columns cm.Parameters.AddWithValue("@GID", gid); cm.Parameters.AddWithValue("@RID", rid); cm.Parameters.AddWithValue("@FolderID", folderID); 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_AID = new SqlParameter("@newAID", SqlDbType.Int); param_AID.Direction = ParameterDirection.Output; cm.Parameters.Add(param_AID); 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 aid = (int)cm.Parameters["@newAID"].Value; return (byte[])cm.Parameters["@newLastChanged"].Value; } } catch (Exception ex) { Database.LogException("Assignment.Add", ex); throw new DbCslaException("Assignment.Add", ex); } } [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 = "updateAssignment"; // All Fields including Calculated Fields cm.Parameters.AddWithValue("@AID", _AID); cm.Parameters.AddWithValue("@GID", _GID); cm.Parameters.AddWithValue("@RID", _RID); cm.Parameters.AddWithValue("@FolderID", _FolderID); 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("Assignment.DataPortal_Update", ex); _errorMessage = ex.Message; if (!ex.Message.EndsWith("has been edited by another user."))throw ex; } } } [Transactional(TransactionalTypes.TransactionScope)] public static byte[] Update(SqlConnection cn, ref int aid, int gid, int rid, int folderID, SmartDate startDate, SmartDate endDate, DateTime dts, string usrID, ref byte[] lastChanged) { try { using (SqlCommand cm = cn.CreateCommand()) { cm.CommandType = CommandType.StoredProcedure; cm.CommandText = "updateAssignment"; // Input All Fields - Except Calculated Columns cm.Parameters.AddWithValue("@AID", aid); cm.Parameters.AddWithValue("@GID", gid); cm.Parameters.AddWithValue("@RID", rid); cm.Parameters.AddWithValue("@FolderID", folderID); 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("Assignment.Update", ex); throw new DbCslaException("Assignment.Update", ex); } } [Transactional(TransactionalTypes.TransactionScope)] protected override void DataPortal_DeleteSelf() { DataPortal_Delete(new PKCriteria(_AID)); } [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 = "deleteAssignment"; cm.Parameters.AddWithValue("@AID", criteria.AID); cm.ExecuteNonQuery(); } } } catch (Exception ex) { Database.LogException("Assignment.DataPortal_Delete", ex); _errorMessage = ex.Message; throw new DbCslaException("Assignment.DataPortal_Delete", ex); } } [Transactional(TransactionalTypes.TransactionScope)] public static void Remove(SqlConnection cn, int aid) { try { using (SqlCommand cm = cn.CreateCommand()) { cm.CommandType = CommandType.StoredProcedure; cm.CommandText = "deleteAssignment"; // Input PK Fields cm.Parameters.AddWithValue("@AID", aid); // TODO: Define any additional output parameters cm.ExecuteNonQuery(); } } catch (Exception ex) { Database.LogException("Assignment.Remove", ex); throw new DbCslaException("Assignment.Remove", ex); } } #endregion #region Exists public static bool Exists(int aid) { ExistsCommand result; result = DataPortal.Execute (new ExistsCommand(aid)); return result.Exists; } [Serializable()] private class ExistsCommand : CommandBase { private int _AID; private bool _exists; public bool Exists { get { return _exists; } } public ExistsCommand(int aid) { _AID = aid; } 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 = "existsAssignment"; cm.Parameters.AddWithValue("@AID", _AID); int count = (int)cm.ExecuteScalar(); _exists = (count > 0); } } } catch (Exception ex) { Database.LogException("Assignment.DataPortal_Execute", ex); throw new DbCslaException("Assignment.DataPortal_Execute", ex); } } } #endregion // Standard Default Code #region extension Extension ext = new Extension(); [Serializable()] partial class Extension : extensionBase { } [Serializable()] class extensionBase { // Default Values 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 } // Class } // Namespace //// The following is a sample Extension File. You can use it to create AssignmentExt.cs //using System; //using System.Collections.Generic; //using System.Text; //using Csla; //namespace Volian.CSLA.Library //{ // public partial class Assignment // { // partial class Extension : extensionBase // { // // TODO: Override automatic defaults // 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)); // } // } // } //}