// ======================================================================== // 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 { /// /// Structure Generated by MyGeneration using the CSLA Object Mapping template /// [Serializable()] public partial class Structure : BusinessBase { #region Business Methods private string _errorMessage = string.Empty; public string ErrorMessage { get { return _errorMessage; } } private static int _nextStructureID = -1; public static int NextStructureID { get { return _nextStructureID--; } } private int _StructureID; [System.ComponentModel.DataObjectField(true, true)] public int StructureID { [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] get { CanReadProperty(true); return _StructureID; } } private int _FromType; /// /// 0 - Parent, 1 - Previous /// public int FromType { [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] get { CanReadProperty(true); return _FromType; } [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] set { CanWriteProperty(true); if (_FromType != value) { _FromType = value; PropertyHasChanged(); } } } private int _FromID; public int FromID { [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] get { CanReadProperty(true); return _FromID; } [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] set { CanWriteProperty(true); if (_FromID != value) { _FromID = value; PropertyHasChanged(); } } } private int _ContentType; /// /// 0 - Structure, 1 - Procedure, 2 - Section, 3 - Step, 4 - Branch /// public int ContentType { [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] get { CanReadProperty(true); return _ContentType; } [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] set { CanWriteProperty(true); if (_ContentType != value) { _ContentType = value; PropertyHasChanged(); } } } private int _ContentID; public int ContentID { [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] get { CanReadProperty(true); return _ContentID; } [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] set { CanWriteProperty(true); if (_ContentID != value) { _ContentID = value; 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 _UserID = string.Empty; public string UserID { [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] get { CanReadProperty(true); return _UserID; } [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] set { CanWriteProperty(true); if (value == null) value = string.Empty; if (_UserID != value) { _UserID = value; PropertyHasChanged(); } } } private byte[] _LastChanged = new byte[8];//timestamp private StructureRoUsages _StructureRoUsages = StructureRoUsages.New(); /// /// Related Field /// public StructureRoUsages StructureRoUsages { [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] get { CanReadProperty(true); return _StructureRoUsages; } } private StructureTransitions _StructureTransitions = StructureTransitions.New(); /// /// Related Field /// public StructureTransitions StructureTransitions { [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] get { CanReadProperty(true); return _StructureTransitions; } } public override bool IsDirty { get { return base.IsDirty || _StructureRoUsages.IsDirty || _StructureTransitions.IsDirty; } } public override bool IsValid { get { return base.IsValid && _StructureRoUsages.IsValid && _StructureTransitions.IsValid; } } // TODO: Replace base Structure.ToString function as necessary /// /// Overrides Base ToString /// /// A string representation of current Structure //public override string ToString() //{ // return base.ToString(); //} // TODO: Check Structure.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 Structure protected override object GetIdValue() { return _StructureID; } #endregion #region ValidationRules protected override void AddBusinessRules() { ValidationRules.AddRule( Csla.Validation.CommonRules.StringRequired, "UserID"); ValidationRules.AddRule( Csla.Validation.CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("UserID", 100)); ext.AddValidationRules(ValidationRules); // TODO: Add other validation rules // ValidationRules.AddRule(StartDateGTEndDate, "Started"); } // 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(StructureID, ""); //AuthorizationRules.AllowRead(FromType, ""); //AuthorizationRules.AllowRead(FromID, ""); //AuthorizationRules.AllowRead(ContentType, ""); //AuthorizationRules.AllowRead(ContentID, ""); //AuthorizationRules.AllowRead(DTS, ""); //AuthorizationRules.AllowRead(UserID, ""); //AuthorizationRules.AllowWrite(FromType, ""); //AuthorizationRules.AllowWrite(FromID, ""); //AuthorizationRules.AllowWrite(ContentType, ""); //AuthorizationRules.AllowWrite(ContentID, ""); //AuthorizationRules.AllowWrite(DTS, ""); //AuthorizationRules.AllowWrite(UserID, ""); 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 Structure() {/* require use of factory methods */} public static Structure New() { if (!CanAddObject()) throw new System.Security.SecurityException("User not authorized to add a Structure"); return DataPortal.Create(); } public static Structure New(int fromType, int fromID, int contentType, int contentID) { Structure tmp = Structure.New(); tmp.FromType = fromType; tmp.FromID = fromID; tmp.ContentType = contentType; tmp.ContentID = contentID; return tmp; } public static Structure MakeStructure(int fromType, int fromID, int contentType, int contentID) { Structure tmp = Structure.New(fromType, fromID, contentType, contentID); tmp.Save(); return tmp; } public static Structure MakeStructure(int fromType, int fromID, int contentType, int contentID, DateTime dts, string userID) { Structure tmp = Structure.New(fromType, fromID, contentType, contentID); if (dts >= new DateTime(1753, 1, 1) && dts <= new DateTime(9999, 12, 31)) tmp.DTS = dts; if (userID != null && userID != string.Empty) tmp.UserID = userID; tmp.Save(); return tmp; } public static Structure Get(int structureID) { if (!CanGetObject()) throw new System.Security.SecurityException("User not authorized to view a Structure"); return DataPortal.Fetch(new PKCriteria(structureID)); } public static Structure Get(SafeDataReader dr) { if (dr.Read()) return new Structure(dr); return null; } private Structure(SafeDataReader dr) { _StructureID = dr.GetInt32("StructureID"); _FromType = dr.GetInt32("FromType"); _FromID = dr.GetInt32("FromID"); _ContentType = dr.GetInt32("ContentType"); _ContentID = dr.GetInt32("ContentID"); _DTS = dr.GetDateTime("DTS"); _UserID = dr.GetString("UserID"); dr.GetBytes("LastChanged", 0, _LastChanged, 0, 8); } public static void Delete(int structureID) { if (!CanDeleteObject()) throw new System.Security.SecurityException("User not authorized to remove a Structure"); DataPortal.Delete(new PKCriteria(structureID)); } public override Structure Save() { if (IsDeleted && !CanDeleteObject()) throw new System.Security.SecurityException("User not authorized to remove a Structure"); else if (IsNew && !CanAddObject()) throw new System.Security.SecurityException("User not authorized to add a Structure"); else if (!CanEditObject()) throw new System.Security.SecurityException("User not authorized to update a Structure"); return base.Save(); } #endregion #region Data Access Portal [Serializable()] private class PKCriteria { private int _StructureID; public int StructureID { get { return _StructureID; } } public PKCriteria(int structureID) { _StructureID = structureID; } } // TODO: If Create needs to access DB - It should not be marked RunLocal [RunLocal()] private new void DataPortal_Create(object criteria) { _StructureID = NextStructureID; // Database Defaults _DTS = ext.DefaultDTS; _UserID = ext.DefaultUserID; // 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 = "getStructure"; cm.Parameters.AddWithValue("@StructureID", criteria.StructureID); using (SafeDataReader dr = new SafeDataReader(cm.ExecuteReader())) { dr.Read(); _StructureID = dr.GetInt32("StructureID"); _FromType = dr.GetInt32("FromType"); _FromID = dr.GetInt32("FromID"); _ContentType = dr.GetInt32("ContentType"); _ContentID = dr.GetInt32("ContentID"); _DTS = dr.GetDateTime("DTS"); _UserID = dr.GetString("UserID"); dr.GetBytes("LastChanged", 0, _LastChanged, 0, 8); // load child objects dr.NextResult(); _StructureRoUsages = StructureRoUsages.Get(dr); // load child objects dr.NextResult(); _StructureTransitions = StructureTransitions.Get(dr); } } } } catch (Exception ex) { Database.LogException("Structure.DataPortal_Fetch", ex); _errorMessage = ex.Message; throw new DbCslaException("Structure.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 = "addStructure"; // Input All Fields - Except Calculated Columns cm.Parameters.AddWithValue("@FromType", _FromType); cm.Parameters.AddWithValue("@FromID", _FromID); cm.Parameters.AddWithValue("@ContentType", _ContentType); cm.Parameters.AddWithValue("@ContentID", _ContentID); cm.Parameters.AddWithValue("@DTS", _DTS); cm.Parameters.AddWithValue("@UserID", _UserID); // Output Calculated Columns SqlParameter param_StructureID = new SqlParameter("@newStructureID", SqlDbType.Int); param_StructureID.Direction = ParameterDirection.Output; cm.Parameters.Add(param_StructureID); 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 _StructureID = (int)cm.Parameters["@newStructureID"].Value; _LastChanged = (byte[])cm.Parameters["@newLastChanged"].Value; } // update child objects _StructureRoUsages.Update(this, cn); _StructureTransitions.Update(this, cn); } } catch (Exception ex) { Database.LogException("Structure.DataPortal_Insert", ex); _errorMessage = ex.Message; throw new DbCslaException("Structure.DataPortal_Fetch", ex); } } [Transactional(TransactionalTypes.TransactionScope)] public static byte[] Add(SqlConnection cn, ref int structureID, int fromType, int fromID, int contentType, int contentID, DateTime dts, string userID) { try { using (SqlCommand cm = cn.CreateCommand()) { cm.CommandType = CommandType.StoredProcedure; cm.CommandText = "addStructure"; // Input All Fields - Except Calculated Columns cm.Parameters.AddWithValue("@FromType", fromType); cm.Parameters.AddWithValue("@FromID", fromID); cm.Parameters.AddWithValue("@ContentType", contentType); cm.Parameters.AddWithValue("@ContentID", contentID); cm.Parameters.AddWithValue("@DTS", dts); cm.Parameters.AddWithValue("@UserID", userID); // Output Calculated Columns SqlParameter param_StructureID = new SqlParameter("@newStructureID", SqlDbType.Int); param_StructureID.Direction = ParameterDirection.Output; cm.Parameters.Add(param_StructureID); 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 structureID = (int)cm.Parameters["@newStructureID"].Value; return (byte[])cm.Parameters["@newLastChanged"].Value; } } catch (Exception ex) { Database.LogException("Structure.Add", ex); throw new DbCslaException("Structure.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 = "updateStructure"; // All Fields including Calculated Fields cm.Parameters.AddWithValue("@StructureID", _StructureID); cm.Parameters.AddWithValue("@FromType", _FromType); cm.Parameters.AddWithValue("@FromID", _FromID); cm.Parameters.AddWithValue("@ContentType", _ContentType); cm.Parameters.AddWithValue("@ContentID", _ContentID); cm.Parameters.AddWithValue("@DTS", _DTS); cm.Parameters.AddWithValue("@UserID", _UserID); 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 _StructureRoUsages.Update(this, cn); _StructureTransitions.Update(this, cn); } } catch (Exception ex) { Database.LogException("Structure.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 structureID, int fromType, int fromID, int contentType, int contentID, DateTime dts, string userID, ref byte[] lastChanged) { try { using (SqlCommand cm = cn.CreateCommand()) { cm.CommandType = CommandType.StoredProcedure; cm.CommandText = "updateStructure"; // Input All Fields - Except Calculated Columns cm.Parameters.AddWithValue("@StructureID", structureID); cm.Parameters.AddWithValue("@FromType", fromType); cm.Parameters.AddWithValue("@FromID", fromID); cm.Parameters.AddWithValue("@ContentType", contentType); cm.Parameters.AddWithValue("@ContentID", contentID); cm.Parameters.AddWithValue("@DTS", dts); cm.Parameters.AddWithValue("@UserID", userID); 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("Structure.Update", ex); throw new DbCslaException("Structure.Update", ex); } } [Transactional(TransactionalTypes.TransactionScope)] protected override void DataPortal_DeleteSelf() { DataPortal_Delete(new PKCriteria(_StructureID)); } [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 = "deleteStructure"; cm.Parameters.AddWithValue("@StructureID", criteria.StructureID); cm.ExecuteNonQuery(); } } } catch (Exception ex) { Database.LogException("Structure.DataPortal_Delete", ex); _errorMessage = ex.Message; throw new DbCslaException("Structure.DataPortal_Delete", ex); } } [Transactional(TransactionalTypes.TransactionScope)] public static void Remove(SqlConnection cn, int structureID) { try { using (SqlCommand cm = cn.CreateCommand()) { cm.CommandType = CommandType.StoredProcedure; cm.CommandText = "deleteStructure"; // Input PK Fields cm.Parameters.AddWithValue("@StructureID", structureID); // TODO: Define any additional output parameters cm.ExecuteNonQuery(); } } catch (Exception ex) { Database.LogException("Structure.Remove", ex); throw new DbCslaException("Structure.Remove", ex); } } #endregion #region Exists public static bool Exists(int structureID) { ExistsCommand result; result = DataPortal.Execute (new ExistsCommand(structureID)); return result.Exists; } [Serializable()] private class ExistsCommand : CommandBase { private int _StructureID; private bool _exists; public bool Exists { get { return _exists; } } public ExistsCommand(int structureID) { _StructureID = structureID; } 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 = "existsStructure"; cm.Parameters.AddWithValue("@StructureID", _StructureID); int count = (int)cm.ExecuteScalar(); _exists = (count > 0); } } } catch (Exception ex) { Database.LogException("Structure.DataPortal_Execute", ex); throw new DbCslaException("Structure.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 DateTime DefaultDTS { get { return DateTime.Now; } } public virtual string DefaultUserID { 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 StructureExt.cs //using System; //using System.Collections.Generic; //using System.Text; //using Csla; //namespace Volian.CSLA.Library //{ // public partial class Structure // { // partial class Extension : extensionBase // { // // TODO: Override automatic defaults // public virtual DateTime DefaultDTS // { // get { return DateTime.Now; } // } // public virtual string DefaultUserID // { // 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)); // } // } // } //}