/////////////////////////////////////////////////////////// // UIGridLayoutShared.cs // Implementation of Class UIGridLayoutShared // CSLA type: Editable Root // Created on: 20-March-2006 // Object design: John // Coded: John 20-March-2006 /////////////////////////////////////////////////////////// using System; using System.Data; using CSLA.Data; using GZTW.Data; using CSLA; using System.Threading; using CSLA.Security; namespace GZTW.AyaNova.BLL { /// /// User interface grid layout preferences /// Used to store and retrieve user and default /// layouts for all grid objects in user interface /// [Serializable] public class UIGridLayoutShared : BusinessBase { #region Attributes private Guid mID; private SmartDate mCreated; private SmartDate mModified; private Guid mCreator; private Guid mModifier; private string mGridKey=null; private string mName=""; private int mLayoutSize; private byte[] mLayoutContent; #endregion #region Constructor /// /// Private constructor to prevent direct instantiation /// private UIGridLayoutShared() { //New ID mID = Guid.NewGuid(); //prebreak the rule Name=""; mCreated = new SmartDate(DBUtil.CurrentWorkingDateTime); mModified=new SmartDate(); mCreator=Guid.Empty; mModifier=Guid.Empty; //pre-break the rules this.GridKey=""; } #endregion #region Business properties /// /// Get created date /// /// /// public string Created { get { return mCreated.ToString(); } } /// /// Get modified date /// /// /// public string Modified { get { return mModified.ToString(); } } /// /// Get user record ID of person who created this record /// /// /// public Guid Creator { get { return mCreator; } } /// /// Get user ID of person who modified this record /// /// /// public Guid Modifier { get { return mModifier; } } /// /// Size of binary layout data content /// public int LayoutSize { get { return mLayoutSize; } set { if(mLayoutSize!=value) { mLayoutSize=value; MarkDirty(); } } } //These are done as methods rather //than property accessors because //of an efficiency problem with binary data //and properties /// /// Get Document binary data /// /// public byte[] GetLayoutContent() { return mLayoutContent; } /// /// Set document binary data /// /// public void SetLayoutContent(byte[] bData) { mLayoutContent=bData; MarkDirty(); } /// /// Set/get GridKey of item /// /// public string GridKey { get { return mGridKey; } set { if(mGridKey!=value) { mGridKey = value; BrokenRules.Assert("GridKeyRequired","Error.Object.RequiredFieldEmpty,UIGridLayoutShared.Label.GridKey","GridKey",value.Length==0); BrokenRules.Assert("GridKeyLength", "Error.Object.FieldLengthExceeded255,UIGridLayoutShared.Label.GridKey","GridKey",value.Length>255); MarkDirty(); } } } /// /// Set/get Name of item /// /// /// public string Name { get { return mName; } set { if(mName!=value) { mName = value; BrokenRules.Assert("NameRequired", "Error.Object.RequiredFieldEmpty,UIGridLayoutShared.Label.Name", "Name",value.Length==0); BrokenRules.Assert("NameLength", "Error.Object.FieldLengthExceeded255,UIGridLayoutShared.Label.Name","Name",value.Length>255); MarkDirty(); } } } #endregion #region System.Object overrides /// /// /// /// public override string ToString() { return "UIGridLayoutShared" + mGridKey+" - " +mName; } /// /// /// /// /// public override bool Equals(Object obj) { if ( obj == null || GetType ( ) != obj.GetType ( ) ) return false; UIGridLayoutShared c=(UIGridLayoutShared)obj; return ((mID==c.mID)); } /// /// /// /// public override int GetHashCode() { return ("UIGridLayoutShared" + mID).GetHashCode(); } #endregion #region Static methods /// /// Get new object /// /// public static UIGridLayoutShared NewItem() { return new UIGridLayoutShared(); } /// /// Fetch shared Grid layout requested /// /// /// /// public static UIGridLayoutShared GetItem(Guid _ID) { return (UIGridLayoutShared)DataPortal.Fetch(new Criteria(_ID)); } #endregion #region DAL DATA ACCESS #region Fetch /// /// protected override void DataPortal_Fetch(object Criteria) { Criteria crit = (Criteria)Criteria; SafeDataReader dr = null; try { DBCommandWrapper dbCommandWrapper = DBUtil.DB.GetSqlStringCommandWrapper( "SELECT * from aUIGridLayoutShared WHERE " + "aUIGridLayoutShared.aID=@ID" ); dbCommandWrapper.AddInParameter("@ID",DbType.Guid,crit.ID); dr= new SafeDataReader(DBUtil.DB.ExecuteReader(dbCommandWrapper)); if(dr.Read()) { //Standard fields mCreated=DBUtil.ToLocal(dr.GetSmartDate("aCreated")); mModified=DBUtil.ToLocal(dr.GetSmartDate("aModified")); mCreator=dr.GetGuid("aCreator"); mModifier=dr.GetGuid("aModifier"); //UIGridLayoutShared fields //Unbreaking property style Name=dr.GetString("aName"); mLayoutSize=dr.GetInt32("aLayoutSize"); mID=crit.ID; //Important: use property not internal field //so that initial broken rule is unbroken on fetch GridKey=dr.GetString("aGridKey"); //Get the number of bytes mLayoutContent=new Byte[mLayoutSize]; //retrieve the bytes dr.GetBytes("aLayoutContent",0,mLayoutContent,0,mLayoutContent.Length); } } finally { if(dr!=null) dr.Close(); } MarkOld(); } #endregion fetch #region Update /// /// Called by DataPortal to delete/add/update data into the database /// protected override void DataPortal_Update() { //Apparently no concurrency check for this as it wasn't in the old data layer code when ported //makes sense as long as users aren't logging in with same account at more than one station #region Delete if(IsDeleted) { if(!IsNew) { //Delete object and child objects DBCommandWrapper cmDelete = DBUtil.GetCommandFromSQL( "DELETE FROM aUIGridLayoutShared WHERE aID=@ID"); cmDelete.AddInParameter("@ID",DbType.Guid,this.mID); using (IDbConnection connection = DBUtil.DB.GetConnection()) { connection.Open(); IDbTransaction transaction = connection.BeginTransaction(); try { DBUtil.DB.ExecuteNonQuery(cmDelete, transaction); // Commit the transaction transaction.Commit(); } catch { // Rollback transaction transaction.Rollback(); throw; } finally { connection.Close(); } } //----------------------------- } MarkNew(); return; } #endregion #region Add / Update //get modification time temporarily, if update succeeds then //set to this time System.DateTime dtModified = DBUtil.CurrentWorkingDateTime; DBCommandWrapper cm = null; if(IsNew)//Add or update? cm=DBUtil.GetCommandFromSQL( "INSERT INTO aUIGridLayoutShared (aGridKey, " + "aName, aLayoutSize, aLayoutContent, " + "aCreated,aModified,aCreator,aModifier) VALUES (@GridKey, " + "@Name,@LayoutSize,@LayoutContent,@Created,@Modified,@CurrentUserID, " + "@CurrentUserID)" ); else cm=DBUtil.GetCommandFromSQL( "UPDATE aUIGridLayoutShared SET aGridKey=@GridKey, " + "aName=@Name, " + "aLayoutSize=@LayoutSize, aLayoutContent=@LayoutContent, " + "aModifier=@CurrentUserID, " + "aModified=@Modified WHERE aUIGridLayoutShared.aID=@ID " ); cm.AddInParameter("@ID",DbType.Guid, mID); cm.AddInParameter("@GridKey",DbType.String,this.mGridKey); cm.AddInParameter("@Name",DbType.String,mName); cm.AddInParameter("@LayoutSize",DbType.Int32,mLayoutSize); cm.AddInParameter("@LayoutContent",DbType.Object,mLayoutContent); //Standard fields cm.AddInParameter("@CurrentUserID",DbType.Guid, CurrentUserID); cm.AddInParameter("@Created",DbType.DateTime, DBUtil.ToUTC(mCreated).DBValue); cm.AddInParameter("@Modified",DbType.DateTime, DBUtil.ToUTC(dtModified)); using (IDbConnection connection = DBUtil.DB.GetConnection()) { connection.Open(); IDbTransaction transaction = connection.BeginTransaction(); try { DBUtil.DB.ExecuteNonQuery(cm, transaction); MarkOld();//db is now synched with object // Commit the transaction transaction.Commit(); } catch { // Rollback transaction transaction.Rollback(); throw; } finally { connection.Close(); } //Successful update so //change modification time to match this.mModified.Date=dtModified; } #endregion } #endregion update #endregion #region Override IsValid / IsDirty //Override base class version if there are child objects /* public override bool IsValid { get { return base.IsValid && ChildItem.IsValid; } } public override bool IsDirty { get { return base.IsDirty || ChildItem.IsDirty; } } */ #endregion #region criteria /// /// Criteria for identifying existing object /// [Serializable] private class Criteria { public Guid ID; public Criteria(Guid _ID) { ID=_ID; } } #endregion }//end UIGridLayoutShared }//end namespace GZTW.AyaNova.BLL