/////////////////////////////////////////////////////////// // PartInventoryAdjustment.cs // Implementation of Class PartInventoryAdjustment // CSLA type: Editable Root // Created on: 15-Nov-2004 8:41:45 AM // Object design: Joyce // Coded: John 15-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 { /// /// Manual inventory adjustment /// [Serializable] public class PartInventoryAdjustment : BusinessBase { #region Attributes private bool bReadOnly; private Guid mID; private SmartDate mCreated; private SmartDate mModified; private Guid mCreator; private Guid mModifier; private SmartDate mDateAdjusted; private int mAdjustmentNumber; private string mReasonForAdjustment=""; private PartInventoryAdjustmentItems mItems; #endregion #region Constructor /// /// Private constructor to prevent direct instantiation /// private PartInventoryAdjustment() { //Set to read / write initially so that properties //can be set bReadOnly=false; //New ID mID = Guid.NewGuid(); mDateAdjusted = new SmartDate(DBUtil.CurrentWorkingDateTime); mItems=PartInventoryAdjustmentItems.NewItems(); //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 record ID of person who modified this record /// /// /// public Guid Modifier { get { return mModifier; } } /// /// collection of containing objects /// public PartInventoryAdjustmentItems Items { get { return mItems; } } /// /// Effective date of adjustment (may not be entry date) /// public object DateAdjusted { get { return mDateAdjusted.DBValue; } set { if(bReadOnly) ThrowSetError(); else { if (!AyaBizUtils.SmartDateEquals(mDateAdjusted, value)) //Case 298 { mDateAdjusted.DBValue = value; BrokenRules.Assert("DateAdjustedRequired", "Error.Object.RequiredFieldEmpty,PartInventoryAdjustment.Label.DateAdjusted", "DateAdjusted",mDateAdjusted.IsEmpty); MarkDirty(); foreach(PartInventoryAdjustmentItem i in mItems) { i.mDateAdjusted=mDateAdjusted; } } } } } /// /// /// public SmartDate sdDateAdjusted { get{return mDateAdjusted;} } /// /// Read only DB set unique identity number /// public int AdjustmentNumber { get { return mAdjustmentNumber; } } /// /// Reason for adjustment /// public string ReasonForAdjustment { get { return mReasonForAdjustment; } set { if(bReadOnly) ThrowSetError(); else { if(mReasonForAdjustment!=value) { mReasonForAdjustment = 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.PartInventoryAdjustment") ) ); } /// /// called by UI on change of collection of items /// public void CheckForDuplicateSerialNumbers() { foreach(PartInventoryAdjustmentItem p in mItems) { p.SerialNumbers.CheckForDuplicateSerialNumbers(); } } #endregion #region System.object overrides /// /// /// /// public override string ToString() { return "PartInventoryAdjustment" + mID.ToString(); } /// /// /// /// /// public override bool Equals(Object obj) { if ( obj == null || GetType ( ) != obj.GetType ( ) ) return false; PartInventoryAdjustment c=(PartInventoryAdjustment)obj; return mID==c.mID; } /// /// /// /// public override int GetHashCode() { return ("PartInventoryAdjustment"+mID).GetHashCode(); } #endregion #region Searching /// /// Returns a search result object based on search terms /// for the ID specified /// /// /// /// public static SearchResult GetSearchResult(Guid ID, string[]searchTerms) { if (AyaBizUtils.Right("Object.PartInventoryAdjustment") < (int)SecurityLevelTypes.ReadOnly) return new SearchResult(); SearchResult sr = new SearchResult(); System.Text.StringBuilder sb = new System.Text.StringBuilder(); SafeDataReader dr = null; try {// dr = DBUtil.GetReaderFromSQLString( "SELECT aCreated, aModified, aCreator, aModifier, aReasonForAdjustment FROM aPartInventoryAdjustment " + "WHERE (aID = @ID)" , ID); if (!dr.Read()) return new SearchResult();//DBUtil.ThrowFetchError("SearchResult for PartID: " + ID.ToString()); sr.Description = LocalizedTextTable.GetLocalizedTextDirect("O.PartInventoryAdjustment"); sb.Append(sr.Description); sb.Append(" "); sb.Append(dr.GetString("aReasonForAdjustment")); sb.Append(" "); sr.Created = DBUtil.ToLocal(dr.GetSmartDate("aCreated")); sr.Modified = DBUtil.ToLocal(dr.GetSmartDate("aModified")); sr.Creator = dr.GetGuid("aCreator"); sr.Modifier = dr.GetGuid("aModifier"); } finally { if (dr != null) dr.Close(); } //Formulate results ExtractAndRank er = new ExtractAndRank(); er.Process(sb.ToString().Trim(), searchTerms); sr.Extract = er.Extract; sr.Rank = er.Ranking; sr.AncestorRootObjectID = ID; sr.AncestorRootObjectType = RootObjectTypes.PartInventoryAdjustment; return sr; } #endregion #region Static methods /// /// Create new PartInventoryAdjustment /// /// PartInventoryAdjustment public static PartInventoryAdjustment NewItem() { PartInventoryAdjustment c; if(AyaBizUtils.Right("Object.PartInventoryAdjustment")>(int)SecurityLevelTypes.ReadOnly) { c = new PartInventoryAdjustment(); return c; } else throw new System.Security.SecurityException( string.Format( LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToCreate"), LocalizedTextTable.GetLocalizedTextDirect("O.PartInventoryAdjustment"))); } /// /// Fetch existing PartInventoryAdjustment /// /// PartInventoryAdjustment /// PartInventoryAdjustment Guid public static PartInventoryAdjustment GetItem(Guid _ID) { if (_ID == AyaBizUtils.NewObjectGuid) return NewItem(); if(AyaBizUtils.Right("Object.PartInventoryAdjustment")>(int)SecurityLevelTypes.NoAccess) return (PartInventoryAdjustment)DataPortal.Fetch(new Criteria(_ID)); else throw new System.Security.SecurityException( string.Format( LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToRetrieve"), LocalizedTextTable.GetLocalizedTextDirect("O.PartInventoryAdjustment"))); } /// /// /// /// public static void SetVisibleIDNumber(int newID) { VisibleIDNumber.SetVisibleIDNumber(newID); } #endregion #region DAL DATA ACCESS #region Fetch /// /// protected override void DataPortal_Fetch(object Criteria) { //set to false to load items initially bReadOnly=false; Criteria crit = (Criteria)Criteria;SafeDataReader dr = null; try { dr=DBUtil.GetReaderFromSQLString("SELECT * FROM aPartInventoryAdjustment WHERE aID=@ID;",crit.ID); if(!dr.Read()) DBUtil.ThrowFetchError("PartInventoryAdjustment ID: " + crit.ID.ToString()); //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"); //PartInventoryAdjustment fields mDateAdjusted=DBUtil.ToLocal(dr.GetSmartDate("aDateAdjusted")); mAdjustmentNumber=dr.GetInt32("AADJUSTMENTNUMBER"); mReasonForAdjustment=dr.GetString("aReasonForAdjustment"); if(dr!=null) dr.Close(); /* * Load child collection objects */ //Items dr=DBUtil.GetReaderFromSQLString( "SELECT * FROM aPartInventoryAdjustmentItem " + "WHERE aPartInventoryAdjustmentItem.aPartInventoryAdjustmentID=@ID " ,crit.ID); mItems=PartInventoryAdjustmentItems.GetItems(dr); if(dr!=null) dr.Close(); } finally { if(dr!=null) dr.Close(); } MarkOld(); //Get access rights level bReadOnly=AyaBizUtils.Right("Object.PartInventoryAdjustment")<(int)SecurityLevelTypes.ReadWrite; } #endregion fetch #region Update /// /// Called by DataPortal to delete/add/update data into the database /// protected override void DataPortal_Update() { //PartInventoryAdjustment adjustment is unchangeable add only: if(!IsNew) throw new System.ApplicationException ( string.Format ( LocalizedTextTable.GetLocalizedTextDirect("Error.Object.NotChangeable"), LocalizedTextTable.GetLocalizedTextDirect("O.PartInventoryAdjustment") ) ); if(IsDeleted) throw new System.ApplicationException ( string.Format ( LocalizedTextTable.GetLocalizedTextDirect("Error.Object.NotDeleteable"), LocalizedTextTable.GetLocalizedTextDirect("O.PartInventoryAdjustment") ) ); #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 aPartInventoryAdjustment (aID, aDateAdjusted, aReasonForAdjustment, aCreated,aModified,aCreator,aModifier) " + "VALUES (@ID,@DateAdjusted,@ReasonForAdjustment,@Created,@Modified,@CurrentUserID,@CurrentUserID)" ); else cm=DBUtil.GetCommandFromSQL( "UPDATE aPartInventoryAdjustment SET aID=@ID, aDateAdjusted=@DateAdjusted, " + "aReasonForAdjustment=@ReasonForAdjustment, " + "aModifier=@CurrentUserID, aModified=@Modified " + "WHERE aID=@ID" ); cm.AddInParameter("@ID",DbType.Guid, this.mID); cm.AddInParameter("@DateAdjusted",DbType.DateTime,DBUtil.ToUTC(mDateAdjusted).DBValue); cm.AddInParameter("@ReasonForAdjustment",DbType.String,mReasonForAdjustment); //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(); //All inventory affecting methods use a high isolation level transaction: IDbTransaction transaction = connection.BeginTransaction(System.Data.IsolationLevel.Serializable); bool bGetIdentity=IsNew; try { DBUtil.DB.ExecuteNonQuery(cm, transaction); //Update child objects //Items mItems.Update(mID,transaction); //Process keywords DBUtil.ProcessKeywords(transaction,this.mID,RootObjectTypes.PartInventoryAdjustment,IsNew, AyaBizUtils.Break(false, this.mReasonForAdjustment)); MarkOld();//db is now synched with object // Commit the transaction transaction.Commit(); } catch { // Rollback transaction transaction.Rollback(); throw; } finally { connection.Close(); } //Get new DB generated identity value if(bGetIdentity) this.mAdjustmentNumber=DBUtil.GetIdentity("AADJUSTMENTNUMBER","aPartInventoryAdjustment",this.mID,null); //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 && mItems.IsValid && mItems.GrandChildIsValid(); } } /// /// /// public override bool IsDirty { get { return base.IsDirty || mItems.IsDirty || mItems.GrandChildIsDirty() ; } } #endregion #region criteria /// /// Criteria for identifying existing object /// [Serializable] private class Criteria { public Guid ID; public Criteria(Guid _ID) { ID=_ID; } } #endregion #region Set VisibleIDNumber #pragma warning disable 1591 /// /// Set the DB generated visible ID number /// to a new user chosen starting point /// [Serializable(), System.ComponentModel.Browsable(false)] public class VisibleIDNumber//DO_NOT_OBFUSCATE { int _newStartID = -1; public VisibleIDNumber(int newStartID) { _newStartID = newStartID; } public static void SetVisibleIDNumber(int newStartID) { DataPortal.Update(new VisibleIDNumber(newStartID)); } public void DataPortal_Update() { //Find the highest existing number object o=DBUtil.GetScalarFromSQLString( "SELECT MAX(AADJUSTMENTNUMBER) FROM aPartInventoryAdjustment" ); if(o==null || o==System.DBNull.Value) o=0; int nHighestExisting=(int)o; //ensure new number is larger than highest existing one if(_newStartID