// ======================================================================== // 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 { /// /// DocVersion Generated by MyGeneration using the CSLA Object Mapping template /// [Serializable()] public partial class DocVersion : BusinessBase { #region Business Methods private string _errorMessage = string.Empty; public string ErrorMessage { get { return _errorMessage; } } private static int _nextVersionID = -1; public static int NextVersionID { get { return _nextVersionID--; } } private int _VersionID; [System.ComponentModel.DataObjectField(true, true)] public int VersionID { [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] get { CanReadProperty(true); return _VersionID; } } 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 int _VersionType; /// /// 0 Working Draft, 1 Temporary, 128 Revision, 129 Approved (Greater than 127 - non editable) /// public int VersionType { [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] get { CanReadProperty(true); return _VersionType; } [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] set { CanWriteProperty(true); if (_VersionType != value) { _VersionType = value; PropertyHasChanged(); } } } private string _Name = string.Empty; public string Name { [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] get { CanReadProperty(true); return _Name; } [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] set { CanWriteProperty(true); 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 { CanReadProperty(true); return _Title; } [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] set { CanWriteProperty(true); if (value == null) value = string.Empty; if (_Title != value) { _Title = value; PropertyHasChanged(); } } } private int _StructureID; public int StructureID { [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] get { CanReadProperty(true); return _StructureID; } [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] set { CanWriteProperty(true); if (_StructureID != value) { _StructureID = value; PropertyHasChanged(); } } } private string _Config = string.Empty; public string Config { [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] get { CanReadProperty(true); return _Config; } [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] set { CanWriteProperty(true); if (value == null) value = string.Empty; if (_Config != value) { _Config = 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 // TODO: Replace base DocVersion.ToString function as necessary /// /// Overrides Base ToString /// /// A string representation of current DocVersion //public override string ToString() //{ // return base.ToString(); //} // TODO: Check DocVersion.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 DocVersion protected override object GetIdValue() { return _VersionID; } #endregion #region ValidationRules protected override void AddBusinessRules() { ValidationRules.AddRule( Csla.Validation.CommonRules.StringRequired, "Name"); ValidationRules.AddRule( Csla.Validation.CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("Name", 100)); ValidationRules.AddRule( Csla.Validation.CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("Title", 510)); ValidationRules.AddRule( Csla.Validation.CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("Config", 1073741823)); 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(VersionID, ""); //AuthorizationRules.AllowRead(FolderID, ""); //AuthorizationRules.AllowRead(VersionType, ""); //AuthorizationRules.AllowRead(Name, ""); //AuthorizationRules.AllowRead(Title, ""); //AuthorizationRules.AllowRead(StructureID, ""); //AuthorizationRules.AllowRead(Config, ""); //AuthorizationRules.AllowRead(DTS, ""); //AuthorizationRules.AllowRead(UserID, ""); //AuthorizationRules.AllowWrite(FolderID, ""); //AuthorizationRules.AllowWrite(VersionType, ""); //AuthorizationRules.AllowWrite(Name, ""); //AuthorizationRules.AllowWrite(Title, ""); //AuthorizationRules.AllowWrite(StructureID, ""); //AuthorizationRules.AllowWrite(Config, ""); //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 DocVersion() {/* require use of factory methods */} public static DocVersion New() { if (!CanAddObject()) throw new System.Security.SecurityException("User not authorized to add a DocVersion"); return DataPortal.Create(); } public static DocVersion New(int folderID, int versionType, string name, string title, int structureID, string config) { DocVersion tmp = DocVersion.New(); tmp.FolderID = folderID; tmp.VersionType = versionType; tmp.Name = name; tmp.Title = title; tmp.StructureID = structureID; tmp.Config = config; return tmp; } public static DocVersion MakeDocVersion(int folderID, int versionType, string name, string title, int structureID, string config) { DocVersion tmp = DocVersion.New(folderID, versionType, name, title, structureID, config); tmp.Save(); return tmp; } public static DocVersion MakeDocVersion(int folderID, int versionType, string name, string title, int structureID, string config, DateTime dts, string userID) { DocVersion tmp = DocVersion.New(folderID, versionType, name, title, structureID, config); 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 DocVersion Get(int versionID) { if (!CanGetObject()) throw new System.Security.SecurityException("User not authorized to view a DocVersion"); return DataPortal.Fetch(new PKCriteria(versionID)); } public static DocVersion Get(SafeDataReader dr) { if (dr.Read()) return new DocVersion(dr); return null; } private DocVersion(SafeDataReader dr) { _VersionID = dr.GetInt32("VersionID"); _FolderID = dr.GetInt32("FolderID"); _VersionType = dr.GetInt32("VersionType"); _Name = dr.GetString("Name"); _Title = dr.GetString("Title"); _StructureID = dr.GetInt32("StructureID"); _Config = dr.GetString("Config"); _DTS = dr.GetDateTime("DTS"); _UserID = dr.GetString("UserID"); dr.GetBytes("LastChanged", 0, _LastChanged, 0, 8); } public static void Delete(int versionID) { if (!CanDeleteObject()) throw new System.Security.SecurityException("User not authorized to remove a DocVersion"); DataPortal.Delete(new PKCriteria(versionID)); } public override DocVersion Save() { if (IsDeleted && !CanDeleteObject()) throw new System.Security.SecurityException("User not authorized to remove a DocVersion"); else if (IsNew && !CanAddObject()) throw new System.Security.SecurityException("User not authorized to add a DocVersion"); else if (!CanEditObject()) throw new System.Security.SecurityException("User not authorized to update a DocVersion"); return base.Save(); } #endregion #region Data Access Portal [Serializable()] private class PKCriteria { private int _VersionID; public int VersionID { get { return _VersionID; } } public PKCriteria(int versionID) { _VersionID = versionID; } } // TODO: If Create needs to access DB - It should not be marked RunLocal [RunLocal()] private new void DataPortal_Create(object criteria) { _VersionID = NextVersionID; // Database Defaults _VersionType = ext.DefaultVersionType; _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 = "getDocVersion"; cm.Parameters.AddWithValue("@VersionID", criteria.VersionID); using (SafeDataReader dr = new SafeDataReader(cm.ExecuteReader())) { dr.Read(); _VersionID = dr.GetInt32("VersionID"); _FolderID = dr.GetInt32("FolderID"); _VersionType = dr.GetInt32("VersionType"); _Name = dr.GetString("Name"); _Title = dr.GetString("Title"); _StructureID = dr.GetInt32("StructureID"); _Config = dr.GetString("Config"); _DTS = dr.GetDateTime("DTS"); _UserID = dr.GetString("UserID"); dr.GetBytes("LastChanged", 0, _LastChanged, 0, 8); } } } } catch (Exception ex) { Database.LogException("DocVersion.DataPortal_Fetch", ex); _errorMessage = ex.Message; throw new DbCslaException("DocVersion.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 = "addDocVersion"; // Input All Fields - Except Calculated Columns cm.Parameters.AddWithValue("@FolderID", _FolderID); cm.Parameters.AddWithValue("@VersionType", _VersionType); cm.Parameters.AddWithValue("@Name", _Name); cm.Parameters.AddWithValue("@Title", _Title); cm.Parameters.AddWithValue("@StructureID", _StructureID); cm.Parameters.AddWithValue("@Config", _Config); cm.Parameters.AddWithValue("@DTS", _DTS); cm.Parameters.AddWithValue("@UserID", _UserID); // Output Calculated Columns SqlParameter param_VersionID = new SqlParameter("@newVersionID", SqlDbType.Int); param_VersionID.Direction = ParameterDirection.Output; cm.Parameters.Add(param_VersionID); 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 _VersionID = (int)cm.Parameters["@newVersionID"].Value; _LastChanged = (byte[])cm.Parameters["@newLastChanged"].Value; } // update child objects } } catch (Exception ex) { Database.LogException("DocVersion.DataPortal_Insert", ex); _errorMessage = ex.Message; throw new DbCslaException("DocVersion.DataPortal_Fetch", ex); } } [Transactional(TransactionalTypes.TransactionScope)] public static byte[] Add(SqlConnection cn, ref int versionID, int folderID, int versionType, string name, string title, int structureID, string config, DateTime dts, string userID) { try { using (SqlCommand cm = cn.CreateCommand()) { cm.CommandType = CommandType.StoredProcedure; cm.CommandText = "addDocVersion"; // Input All Fields - Except Calculated Columns cm.Parameters.AddWithValue("@FolderID", folderID); cm.Parameters.AddWithValue("@VersionType", versionType); cm.Parameters.AddWithValue("@Name", name); cm.Parameters.AddWithValue("@Title", title); cm.Parameters.AddWithValue("@StructureID", structureID); cm.Parameters.AddWithValue("@Config", config); cm.Parameters.AddWithValue("@DTS", dts); cm.Parameters.AddWithValue("@UserID", userID); // Output Calculated Columns SqlParameter param_VersionID = new SqlParameter("@newVersionID", SqlDbType.Int); param_VersionID.Direction = ParameterDirection.Output; cm.Parameters.Add(param_VersionID); 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 versionID = (int)cm.Parameters["@newVersionID"].Value; return (byte[])cm.Parameters["@newLastChanged"].Value; } } catch (Exception ex) { Database.LogException("DocVersion.Add", ex); throw new DbCslaException("DocVersion.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 = "updateDocVersion"; // All Fields including Calculated Fields cm.Parameters.AddWithValue("@VersionID", _VersionID); cm.Parameters.AddWithValue("@FolderID", _FolderID); cm.Parameters.AddWithValue("@VersionType", _VersionType); cm.Parameters.AddWithValue("@Name", _Name); cm.Parameters.AddWithValue("@Title", _Title); cm.Parameters.AddWithValue("@StructureID", _StructureID); cm.Parameters.AddWithValue("@Config", _Config); 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 } } catch (Exception ex) { Database.LogException("DocVersion.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 versionID, int folderID, int versionType, string name, string title, int structureID, string config, DateTime dts, string userID, ref byte[] lastChanged) { try { using (SqlCommand cm = cn.CreateCommand()) { cm.CommandType = CommandType.StoredProcedure; cm.CommandText = "updateDocVersion"; // Input All Fields - Except Calculated Columns cm.Parameters.AddWithValue("@VersionID", versionID); cm.Parameters.AddWithValue("@FolderID", folderID); cm.Parameters.AddWithValue("@VersionType", versionType); cm.Parameters.AddWithValue("@Name", name); cm.Parameters.AddWithValue("@Title", title); cm.Parameters.AddWithValue("@StructureID", structureID); cm.Parameters.AddWithValue("@Config", config); 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("DocVersion.Update", ex); throw new DbCslaException("DocVersion.Update", ex); } } [Transactional(TransactionalTypes.TransactionScope)] protected override void DataPortal_DeleteSelf() { DataPortal_Delete(new PKCriteria(_VersionID)); } [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 = "deleteDocVersion"; cm.Parameters.AddWithValue("@VersionID", criteria.VersionID); cm.ExecuteNonQuery(); } } } catch (Exception ex) { Database.LogException("DocVersion.DataPortal_Delete", ex); _errorMessage = ex.Message; throw new DbCslaException("DocVersion.DataPortal_Delete", ex); } } [Transactional(TransactionalTypes.TransactionScope)] public static void Remove(SqlConnection cn, int versionID) { try { using (SqlCommand cm = cn.CreateCommand()) { cm.CommandType = CommandType.StoredProcedure; cm.CommandText = "deleteDocVersion"; // Input PK Fields cm.Parameters.AddWithValue("@VersionID", versionID); // TODO: Define any additional output parameters cm.ExecuteNonQuery(); } } catch (Exception ex) { Database.LogException("DocVersion.Remove", ex); throw new DbCslaException("DocVersion.Remove", ex); } } #endregion #region Exists public static bool Exists(int versionID) { ExistsCommand result; result = DataPortal.Execute (new ExistsCommand(versionID)); return result.Exists; } [Serializable()] private class ExistsCommand : CommandBase { private int _VersionID; private bool _exists; public bool Exists { get { return _exists; } } public ExistsCommand(int versionID) { _VersionID = versionID; } 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 = "existsDocVersion"; cm.Parameters.AddWithValue("@VersionID", _VersionID); int count = (int)cm.ExecuteScalar(); _exists = (count > 0); } } } catch (Exception ex) { Database.LogException("DocVersion.DataPortal_Execute", ex); throw new DbCslaException("DocVersion.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 int DefaultVersionType { get { return 0; } } 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 DocVersionExt.cs //using System; //using System.Collections.Generic; //using System.Text; //using Csla; //namespace Volian.CSLA.Library //{ // public partial class DocVersion // { // partial class Extension : extensionBase // { // // TODO: Override automatic defaults // public virtual int DefaultVersionType // { // get { return 0; } // } // 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)); // } // } // } //}