// ======================================================================== // 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 { /// /// Connection Generated by MyGeneration using the CSLA Object Mapping template /// [Serializable()] public partial class Connection : BusinessBase { #region Business Methods private string _errorMessage = string.Empty; public string ErrorMessage { get { return _errorMessage; } } private static int _nextDBID = -1; public static int NextDBID { get { return _nextDBID--; } } private int _DBID; [System.ComponentModel.DataObjectField(true, true)] public int DBID { [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] get { CanReadProperty(true); return _DBID; } } 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 string _ConnectionString = string.Empty; public string ConnectionString { [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] get { CanReadProperty(true); return _ConnectionString; } [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] set { CanWriteProperty(true); if (value == null) value = string.Empty; if (_ConnectionString != value) { _ConnectionString = value; PropertyHasChanged(); } } } private int _ServerType; /// /// 0 SQL Server /// public int ServerType { [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] get { CanReadProperty(true); return _ServerType; } [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] set { CanWriteProperty(true); if (_ServerType != value) { _ServerType = 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 _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 private ConnectionFolders _ConnectionFolders = ConnectionFolders.New(); /// /// Related Field /// public ConnectionFolders ConnectionFolders { [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] get { CanReadProperty(true); return _ConnectionFolders; } } public override bool IsDirty { get { return base.IsDirty || _ConnectionFolders.IsDirty; } } public override bool IsValid { get { return base.IsValid && _ConnectionFolders.IsValid; } } // TODO: Replace base Connection.ToString function as necessary /// /// Overrides Base ToString /// /// A string representation of current Connection //public override string ToString() //{ // return base.ToString(); //} // TODO: Check Connection.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 Connection protected override object GetIdValue() { return _DBID; } #endregion #region ValidationRules protected override void AddBusinessRules() { 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("ConnectionString", 510)); ValidationRules.AddRule( Csla.Validation.CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("Config", 1073741823)); 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"); } // 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(DBID, ""); //AuthorizationRules.AllowRead(Name, ""); //AuthorizationRules.AllowRead(Title, ""); //AuthorizationRules.AllowRead(ConnectionString, ""); //AuthorizationRules.AllowRead(ServerType, ""); //AuthorizationRules.AllowRead(Config, ""); //AuthorizationRules.AllowRead(DTS, ""); //AuthorizationRules.AllowRead(UsrID, ""); //AuthorizationRules.AllowWrite(Name, ""); //AuthorizationRules.AllowWrite(Title, ""); //AuthorizationRules.AllowWrite(ConnectionString, ""); //AuthorizationRules.AllowWrite(ServerType, ""); //AuthorizationRules.AllowWrite(Config, ""); //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 Connection() {/* require use of factory methods */} public static Connection New() { if (!CanAddObject()) throw new System.Security.SecurityException("User not authorized to add a Connection"); return DataPortal.Create(); } public static Connection New(string name, string title, string connectionString, int serverType, string config) { Connection tmp = Connection.New(); tmp.Name = name; tmp.Title = title; tmp.ConnectionString = connectionString; tmp.ServerType = serverType; tmp.Config = config; return tmp; } public static Connection MakeConnection(string name, string title, string connectionString, int serverType, string config) { Connection tmp = Connection.New(name, title, connectionString, serverType, config); tmp.Save(); return tmp; } public static Connection MakeConnection(string name, string title, string connectionString, int serverType, string config, DateTime dts, string usrID) { Connection tmp = Connection.New(name, title, connectionString, serverType, config); 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 Connection Get(int dbid) { if (!CanGetObject()) throw new System.Security.SecurityException("User not authorized to view a Connection"); return DataPortal.Fetch(new PKCriteria(dbid)); } public static Connection Get(SafeDataReader dr) { if (dr.Read()) return new Connection(dr); return null; } private Connection(SafeDataReader dr) { _DBID = dr.GetInt32("DBID"); _Name = dr.GetString("Name"); _Title = dr.GetString("Title"); _ConnectionString = dr.GetString("ConnectionString"); _ServerType = dr.GetInt32("ServerType"); _Config = dr.GetString("Config"); _DTS = dr.GetDateTime("DTS"); _UsrID = dr.GetString("UsrID"); dr.GetBytes("LastChanged", 0, _LastChanged, 0, 8); } public static void Delete(int dbid) { if (!CanDeleteObject()) throw new System.Security.SecurityException("User not authorized to remove a Connection"); DataPortal.Delete(new PKCriteria(dbid)); } public override Connection Save() { if (IsDeleted && !CanDeleteObject()) throw new System.Security.SecurityException("User not authorized to remove a Connection"); else if (IsNew && !CanAddObject()) throw new System.Security.SecurityException("User not authorized to add a Connection"); else if (!CanEditObject()) throw new System.Security.SecurityException("User not authorized to update a Connection"); return base.Save(); } #endregion #region Data Access Portal [Serializable()] private class PKCriteria { private int _DBID; public int DBID { get { return _DBID; } } public PKCriteria(int dbid) { _DBID = dbid; } } // TODO: If Create needs to access DB - It should not be marked RunLocal [RunLocal()] private new void DataPortal_Create(object criteria) { _DBID = NextDBID; // Database Defaults _ServerType = ext.DefaultServerType; _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 = "getConnection"; cm.Parameters.AddWithValue("@DBID", criteria.DBID); using (SafeDataReader dr = new SafeDataReader(cm.ExecuteReader())) { dr.Read(); _DBID = dr.GetInt32("DBID"); _Name = dr.GetString("Name"); _Title = dr.GetString("Title"); _ConnectionString = dr.GetString("ConnectionString"); _ServerType = dr.GetInt32("ServerType"); _Config = dr.GetString("Config"); _DTS = dr.GetDateTime("DTS"); _UsrID = dr.GetString("UsrID"); dr.GetBytes("LastChanged", 0, _LastChanged, 0, 8); // load child objects dr.NextResult(); _ConnectionFolders = ConnectionFolders.Get(dr); } } } } catch (Exception ex) { Database.LogException("Connection.DataPortal_Fetch", ex); _errorMessage = ex.Message; throw new DbCslaException("Connection.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 = "addConnection"; // Input All Fields - Except Calculated Columns cm.Parameters.AddWithValue("@Name", _Name); cm.Parameters.AddWithValue("@Title", _Title); cm.Parameters.AddWithValue("@ConnectionString", _ConnectionString); cm.Parameters.AddWithValue("@ServerType", _ServerType); cm.Parameters.AddWithValue("@Config", _Config); cm.Parameters.AddWithValue("@DTS", _DTS); cm.Parameters.AddWithValue("@UsrID", _UsrID); // Output Calculated Columns SqlParameter param_DBID = new SqlParameter("@newDBID", SqlDbType.Int); param_DBID.Direction = ParameterDirection.Output; cm.Parameters.Add(param_DBID); 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 _DBID = (int)cm.Parameters["@newDBID"].Value; _LastChanged = (byte[])cm.Parameters["@newLastChanged"].Value; } // update child objects _ConnectionFolders.Update(this, cn); } } catch (Exception ex) { Database.LogException("Connection.DataPortal_Insert", ex); _errorMessage = ex.Message; throw new DbCslaException("Connection.DataPortal_Fetch", ex); } } [Transactional(TransactionalTypes.TransactionScope)] public static byte[] Add(SqlConnection cn, ref int dbid, string name, string title, string connectionString, int serverType, string config, DateTime dts, string usrID) { try { using (SqlCommand cm = cn.CreateCommand()) { cm.CommandType = CommandType.StoredProcedure; cm.CommandText = "addConnection"; // Input All Fields - Except Calculated Columns cm.Parameters.AddWithValue("@Name", name); cm.Parameters.AddWithValue("@Title", title); cm.Parameters.AddWithValue("@ConnectionString", connectionString); cm.Parameters.AddWithValue("@ServerType", serverType); cm.Parameters.AddWithValue("@Config", config); cm.Parameters.AddWithValue("@DTS", dts); cm.Parameters.AddWithValue("@UsrID", usrID); // Output Calculated Columns SqlParameter param_DBID = new SqlParameter("@newDBID", SqlDbType.Int); param_DBID.Direction = ParameterDirection.Output; cm.Parameters.Add(param_DBID); 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 dbid = (int)cm.Parameters["@newDBID"].Value; return (byte[])cm.Parameters["@newLastChanged"].Value; } } catch (Exception ex) { Database.LogException("Connection.Add", ex); throw new DbCslaException("Connection.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 = "updateConnection"; // All Fields including Calculated Fields cm.Parameters.AddWithValue("@DBID", _DBID); cm.Parameters.AddWithValue("@Name", _Name); cm.Parameters.AddWithValue("@Title", _Title); cm.Parameters.AddWithValue("@ConnectionString", _ConnectionString); cm.Parameters.AddWithValue("@ServerType", _ServerType); cm.Parameters.AddWithValue("@Config", _Config); 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 _ConnectionFolders.Update(this, cn); } } catch (Exception ex) { Database.LogException("Connection.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 dbid, string name, string title, string connectionString, int serverType, string config, DateTime dts, string usrID, ref byte[] lastChanged) { try { using (SqlCommand cm = cn.CreateCommand()) { cm.CommandType = CommandType.StoredProcedure; cm.CommandText = "updateConnection"; // Input All Fields - Except Calculated Columns cm.Parameters.AddWithValue("@DBID", dbid); cm.Parameters.AddWithValue("@Name", name); cm.Parameters.AddWithValue("@Title", title); cm.Parameters.AddWithValue("@ConnectionString", connectionString); cm.Parameters.AddWithValue("@ServerType", serverType); cm.Parameters.AddWithValue("@Config", config); 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("Connection.Update", ex); throw new DbCslaException("Connection.Update", ex); } } [Transactional(TransactionalTypes.TransactionScope)] protected override void DataPortal_DeleteSelf() { DataPortal_Delete(new PKCriteria(_DBID)); } [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 = "deleteConnection"; cm.Parameters.AddWithValue("@DBID", criteria.DBID); cm.ExecuteNonQuery(); } } } catch (Exception ex) { Database.LogException("Connection.DataPortal_Delete", ex); _errorMessage = ex.Message; throw new DbCslaException("Connection.DataPortal_Delete", ex); } } [Transactional(TransactionalTypes.TransactionScope)] public static void Remove(SqlConnection cn, int dbid) { try { using (SqlCommand cm = cn.CreateCommand()) { cm.CommandType = CommandType.StoredProcedure; cm.CommandText = "deleteConnection"; // Input PK Fields cm.Parameters.AddWithValue("@DBID", dbid); // TODO: Define any additional output parameters cm.ExecuteNonQuery(); } } catch (Exception ex) { Database.LogException("Connection.Remove", ex); throw new DbCslaException("Connection.Remove", ex); } } #endregion #region Exists public static bool Exists(int dbid) { ExistsCommand result; result = DataPortal.Execute (new ExistsCommand(dbid)); return result.Exists; } [Serializable()] private class ExistsCommand : CommandBase { private int _DBID; private bool _exists; public bool Exists { get { return _exists; } } public ExistsCommand(int dbid) { _DBID = dbid; } 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 = "existsConnection"; cm.Parameters.AddWithValue("@DBID", _DBID); int count = (int)cm.ExecuteScalar(); _exists = (count > 0); } } } catch (Exception ex) { Database.LogException("Connection.DataPortal_Execute", ex); throw new DbCslaException("Connection.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 DefaultServerType { get { return 1; } } 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 ConnectionExt.cs //using System; //using System.Collections.Generic; //using System.Text; //using Csla; //namespace Volian.CSLA.Library //{ // public partial class Connection // { // partial class Extension : extensionBase // { // // TODO: Override automatic defaults // public virtual int DefaultServerType // { // get { return 1; } // } // 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)); // } // } // } //}