/////////////////////////////////////////////////////////// // ContractRate.cs // Implementation of Class ContractRate // CSLA type: Editable Child // Created on: 07-Jun-2004 8:41:22 AM // Object design: Joyce // Coded: John 14-July-2004 /////////////////////////////////////////////////////////// using System; using System.Data; using CSLA.Data; using GZTW.Data; using CSLA; using System.Threading; using CSLA.Security; namespace GZTW.AyaNova.BLL { /// /// Rate applicable to only /// [Serializable] public class ContractRate : BusinessBase { #region Attributes private bool bReadOnly; private Guid mID; private Guid mCreator; private Guid mModifier; private SmartDate mCreated; private SmartDate mModified; /// /// Guid ID of root object (contract) /// private Guid mContractID; /// /// ID of rate (this brings over the name, account number, etc check with john if /// need more /// private Guid mRateID; #endregion #region Constructor private ContractRate() { //Set to read / write initially so that properties //can be set bReadOnly=false; //Child object MarkAsChild(); //New ID mID = Guid.NewGuid(); //Set record history to defaults mCreated = new SmartDate(DBUtil.CurrentWorkingDateTime); mModified=new SmartDate(); mCreator=Guid.Empty; mModifier=Guid.Empty; } #endregion #region Business Properties //---Common properties /// /// Initial created date of this object /// public string Created { get { return mCreated.ToString(); } } /// /// User ID of who initially created this object /// public Guid Creator { get { return mCreator; } } /// /// Last modified date of this object /// public string Modified { get { return mModified.ToString(); } } /// /// User ID of who last modified this object /// public Guid Modifier { get { return mModifier; } } /// /// Unique ID of this object /// public Guid ID { get { return mID; } } //---ContractRate specific properties /// /// Guid ID of root object (contract) /// public Guid ContractID { get { return mContractID; } } /// /// ID of rate (this brings over the name, account number, etc check with john if /// need more /// public Guid RateID { get { return mRateID; } set { if(bReadOnly) ThrowSetError(); else { if(mRateID!=value) { mRateID = value; MarkDirty(); } } } } /// /// Throw an error when a read only user /// tries to set a property /// (this should normally never be called unless someone is using the developer api since the UI /// should prevent it from happening initially) /// private void ThrowSetError() { throw new System.Security.SecurityException ( string.Format ( LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToChange"), LocalizedTextTable.GetLocalizedTextDirect("O.ContractRate") ) ); } #endregion #region System.object overrides /// /// /// /// public override string ToString() { return "ContractRate" + mID.ToString(); } /// /// /// /// /// public override bool Equals(Object obj) { if ( obj == null || GetType ( ) != obj.GetType ( ) ) return false; ContractRate c=(ContractRate)obj; return mID==c.mID; } /// /// /// /// public override int GetHashCode() { return ("ContractRate" + mID).GetHashCode(); } #endregion #region Static methods /// /// Create item /// /// Parent ID /// New Item internal static ContractRate NewItem(Contract obj) { if(AyaBizUtils.Right("Object.ContractRate")>(int)SecurityLevelTypes.ReadOnly) { ContractRate child=new ContractRate(); child.mContractID=obj.ID; return child; } else throw new System.Security.SecurityException( string.Format( LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToCreate"), LocalizedTextTable.GetLocalizedTextDirect("O.ContractRate"))); } /// /// Retrieve item /// /// Data reader /// item from database internal static ContractRate GetItem(SafeDataReader dr) { if(AyaBizUtils.Right("Object.ContractRate")>(int)SecurityLevelTypes.NoAccess) { ContractRate child = new ContractRate(); child.Fetch(dr); return child; } else throw new System.Security.SecurityException( string.Format( LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToRetrieve"), LocalizedTextTable.GetLocalizedTextDirect("O.ContractRate"))); } #endregion #region DAL DATA ACCESS /// /// Fetch from db /// /// private void Fetch(SafeDataReader dr) { //Standard items mCreated=DBUtil.ToLocal(dr.GetSmartDate("aCreated")); mCreator=dr.GetGuid("aCreator"); mModified=DBUtil.ToLocal(dr.GetSmartDate("aModified")); mModifier=dr.GetGuid("aModifier"); //ContractRate specific parameters mID=dr.GetGuid("aID"); mContractID=dr.GetGuid("aContractID"); mRateID=dr.GetGuid("aRateID"); //Get access rights level bReadOnly=AyaBizUtils.Right("Object.ContractRate")<(int)SecurityLevelTypes.ReadWrite; MarkOld(); } /// /// Persist object to database /// /// Parent object /// Parents transaction object internal void Update(Contract obj,IDbTransaction tr) { //No need to update if there is nothing changed if(!this.IsDirty) return; //get modification time temporarily, if update succeeds then //set to this time System.DateTime dtModified = DBUtil.CurrentWorkingDateTime; // If not a new record, check if record was modified //by another user since original retrieval: if(!IsNew) DBUtil.CheckSafeToUpdate(this.mModified.Date,this.mID,"aContractRate"); #region Delete if(IsDeleted) { if(!IsNew) { //Delete DBCommandWrapper cmDelete = DBUtil.GetCommandFromSQL("DELETE FROM aContractRate WHERE aID = @ID;"); cmDelete.AddInParameter("@ID",DbType.Guid,this.mID); DBUtil.DB.ExecuteNonQuery(cmDelete, tr); } MarkNew(); return; } #endregion #region Add / Update DBCommandWrapper cm = null; if(IsNew)//Add or update? cm=DBUtil.GetCommandFromSQL( "INSERT INTO aContractRate (aRateID, aContractID, aID, aCreated, aModified, aCreator,aModifier) " + "VALUES (@RateID,@ContractID,@ID, @Created, @Modified, @CurrentUserID,@CurrentUserID)" ); else cm=DBUtil.GetCommandFromSQL( "UPDATE aContractRate SET aRateID=@RateID, aContractID=@ContractID, " + "aID=@ID, aModifier=@CurrentUserID, " + "aModified=@Modified WHERE aID=@ID" ); //ContractRate fields cm.AddInParameter("@ID",DbType.Guid,mID); cm.AddInParameter("@ContractID",DbType.Guid,mContractID); cm.AddInParameter("@RateID",DbType.Guid,mRateID); //Standard fields cm.AddInParameter("@CurrentUserID",DbType.Guid, CurrentUserID); cm.AddInParameter("@Created",DbType.DateTime, DBUtil.ToUTC(mCreated.Date)); cm.AddInParameter("@Modified",DbType.DateTime, DBUtil.ToUTC(dtModified)); DBUtil.DB.ExecuteNonQuery(cm, tr); MarkOld();//db is now synched with object //Successful update so //change modification time to match this.mModified.Date=dtModified; #endregion } #endregion }//end ContractRate }//end namespace GZTW.AyaNova.BLL