/////////////////////////////////////////////////////////// // UnitOfMeasure.cs // Implementation of Class UnitOfMeasure // CSLA type: Editable Child // Created on: 07-Jun-2004 8:41:40 AM // Object design: Joyce // Coded: John 04-Nov-2004 /////////////////////////////////////////////////////////// using System; using System.Data; using CSLA.Data; using GZTW.Data; using CSLA; using System.Threading; using CSLA.Security; namespace GZTW.AyaNova.BLL { /// /// Displays on reports and Parts screen selection. (e.g. each, per 100, per box, /// MBF, etc) to identify units of measure for the part /// [Serializable] public class UnitOfMeasure : BusinessBase { #region Attributes private bool bReadOnly; private Guid mID; private string mName=null; private bool mActive; private SmartDate mCreated; private SmartDate mModified; private Guid mCreator; private Guid mModifier; #endregion #region Constructor /// /// Private constructor to prevent direct instantiation /// private UnitOfMeasure() { MarkAsChild(); //Set to read / write initially so that properties //can be set bReadOnly=false; //New ID mID = Guid.NewGuid(); //pre-break the rule Name=""; Active=true; //Set record history to defaults mCreated = new SmartDate(DBUtil.CurrentWorkingDateTime); mModified=new SmartDate(); mCreator=Guid.Empty; mModifier=Guid.Empty; } #endregion #region Business properties /// /// Get internal id number Read only property because it's set internally, not /// externally /// public Guid ID { get { return mID; } } /// /// 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; } } /// /// Set/get Name of item /// /// public string Name { get { return mName; } set { if(bReadOnly) ThrowSetError(); else { if(mName!=value) { mName = value; BrokenRules.Assert("NameRequired","Error.Object.RequiredFieldEmpty,UnitOfMeasure.Label.Name","Name",value.Length==0); BrokenRules.Assert("NameLength", "Error.Object.FieldLengthExceeded255,UnitOfMeasure.Label.Name","Name",value.Length>255); MarkDirty(); } } } } /// /// Get /set active status /// public bool Active { get { return mActive; } set { if(bReadOnly) ThrowSetError(); else { if(mActive!=value) { mActive = 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.UnitOfMeasure") ) ); } #endregion #region System.Object overrides /// /// /// /// public override string ToString() { return "UnitOfMeasure" + mID.ToString(); } /// /// /// /// /// public override bool Equals(Object obj) { if ( obj == null || GetType ( ) != obj.GetType ( ) ) return false; UnitOfMeasure c=(UnitOfMeasure)obj; return mID==c.mID; } /// /// /// /// public override int GetHashCode() { return ("UnitOfMeasure" + mID).GetHashCode(); } #endregion #region Static methods /// /// Get new object /// /// internal static UnitOfMeasure NewItem() { if(AyaBizUtils.Right("Object.UnitOfMeasure")>(int)SecurityLevelTypes.ReadOnly) { UnitOfMeasure c = new UnitOfMeasure(); return c; } else throw new System.Security.SecurityException( string.Format( LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToCreate"), LocalizedTextTable.GetLocalizedTextDirect("O.UnitOfMeasure"))); } /// /// /// /// /// internal static UnitOfMeasure GetItem(SafeDataReader dr) { if(AyaBizUtils.Right("Object.UnitOfMeasure")>(int)SecurityLevelTypes.NoAccess) { UnitOfMeasure child = new UnitOfMeasure(); child.Fetch(dr); return child; } else throw new System.Security.SecurityException( string.Format( LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToRetrieve"), LocalizedTextTable.GetLocalizedTextDirect("O.UnitOfMeasure"))); } /// /// Retrieve internal ID from name. /// /// /// Text value /// Guid ID value or Guid.Empty if no match public static Guid GetIDFromName(string Name) { return GuidFetcher.GetItem("AUNITOFMEASURE", "ANAME", Name); } #endregion #region DAL DATA ACCESS /// /// Populate this object from the values in the datareader passed to it /// /// private void Fetch(SafeDataReader dr) { //Standard fields mID=dr.GetGuid("aID"); mCreated=DBUtil.ToLocal(dr.GetSmartDate("aCreated")); mModified=DBUtil.ToLocal(dr.GetSmartDate("aModified")); mCreator=dr.GetGuid("aCreator"); mModifier=dr.GetGuid("aModifier"); //UnitOfMeasure fields //Important: use property not internal field //so that initial broken rule is unbroken on fetch Name=dr.GetString("aName"); Active=dr.GetBoolean("AACTIVE"); MarkOld(); //Get access rights level bReadOnly=AyaBizUtils.Right("Object.UnitOfMeasure")<(int)SecurityLevelTypes.ReadWrite; } internal void Update(IDbTransaction tr) { //No need to update if there is nothing changed if(!this.IsDirty) return; // 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,"aUnitOfMeasure"); #region Delete if(IsDeleted) { if(!IsNew) { //Delete object and child objects DBCommandWrapper cmDelete = DBUtil.GetCommandFromSQL("DELETE FROM aUnitOfMeasure WHERE aID = @ID;"); cmDelete.AddInParameter("@ID",DbType.Guid,this.mID); DBUtil.DB.ExecuteNonQuery(cmDelete, tr); //----------------------------- } 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 aUnitOfMeasure (aID, aName, AACTIVE, aCreated,aModified,aCreator,aModifier) " + "VALUES (@ID,@Name,@Active,@Created,@Modified,@CurrentUserID,@CurrentUserID)" ); else cm=DBUtil.GetCommandFromSQL( "UPDATE aUnitOfMeasure SET aID=@ID, aName=@Name, AACTIVE=@Active, aModifier=@CurrentUserID, " + "aModified=@Modified WHERE " + "aID=@ID" ); //UnitOfMeasure fields cm.AddInParameter("@Name",DbType.String,mName); cm.AddInParameter("@Active",DbType.Boolean,mActive); //Standard fields cm.AddInParameter("@ID",DbType.Guid,this.mID); 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 UnitOfMeasure }//end namespace GZTW.AyaNova.BLL