/////////////////////////////////////////////////////////// // PartSerial.cs // Implementation of Class PartSerial // CSLA type: Editable Child // Created on: 15-Nov-2004 // Object design: Joyce // Coded: John 15-Nov-20044 /////////////////////////////////////////////////////////// using System; using System.Data; using CSLA.Data; using GZTW.Data; using CSLA; using System.Threading; using CSLA.Security; namespace GZTW.AyaNova.BLL { /// /// Stores a serial number record, not accessed directly but rather from it's parent collection /// [Serializable] public class PartSerial : BusinessBase { #region Attributes private bool bReadOnly; private Guid mID; private SmartDate mCreated; private SmartDate mModified; private Guid mCreator; private Guid mModifier; private Guid mPartID; private Guid mPartWarehouseID; private string mSerialNumber=null; private SmartDate mDateReceived; private SmartDate mDateConsumed; private Guid mWorkorderItemPartID=Guid.Empty; private Guid mAdjustmentID=Guid.Empty; private Guid mPurchaseOrderReceiptItemID=Guid.Empty; private bool mAvailable=true; #endregion #region Constructor /// /// Private constructor to prevent direct instantiation /// private PartSerial() { //Set as child object MarkAsChild(); //Set to read / write initially so that properties //can be set bReadOnly=false; //New ID mID = Guid.NewGuid(); mDateReceived = new SmartDate(DBUtil.CurrentWorkingDateTime); mDateConsumed=new SmartDate(); //Pre break serial number required rule //to save work in UI SerialNumber=""; //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; } } /// /// Part /// public Guid PartID { get { return mPartID; } set { if(bReadOnly) ThrowSetError(); else { if(mPartID!=value) { mPartID = value; MarkDirty(); } } } } /// /// Warehouse /// public Guid PartWarehouseID { get { return mPartWarehouseID; } set { if(bReadOnly) ThrowSetError(); else { if(mPartWarehouseID!=value) { mPartWarehouseID = value; MarkDirty(); } } } } /// /// SerialNumber /// public string SerialNumber { get { return mSerialNumber; } set { if(bReadOnly) ThrowSetError(); else { if(mSerialNumber!=value) { mSerialNumber = value; BrokenRules.Assert("SerialNumberRequired","Error.Object.RequiredFieldEmpty,Common.Label.SerialNumber","SerialNumber",value.Length==0); BrokenRules.Assert("SerialNumberLength", "Error.Object.FieldLengthExceeded255,Common.Label.SerialNumber","SerialNumber",value.Length>255); MarkDirty(); } } } } /// /// set by collection if this serial number /// is a duplicate of another in the collection /// so that broken rule will be set /// /// (assumes no two partID's that are the same will have the same /// serial number if serialized part) /// internal bool DuplicateSerialNumber { set { BrokenRules.Assert("SerialNumberDuplicate","PartSerial.Label.Error.SerialNumberNotUnique", "SerialNumber",value); } } /// /// DateReceived /// public object DateReceived { get { return mDateReceived.DBValue; } set { if(bReadOnly) ThrowSetError(); else { if (!AyaBizUtils.SmartDateEquals(mDateReceived, value)) //Case 298 { mDateReceived.DBValue = value; MarkDirty(); } } } } /// /// DateReceived internal version used /// by inventory affecting objects /// internal SmartDate sdDateReceived { get { return mDateReceived; } set { if(bReadOnly) ThrowSetError(); else { if(mDateReceived != value) { mDateReceived = value; MarkDirty(); } } } } /// /// Date consumed /// public object DateConsumed { get { return mDateConsumed.DBValue; } set { if(bReadOnly) ThrowSetError(); else { if (!AyaBizUtils.SmartDateEquals(mDateConsumed, value)) //Case 298 { mDateConsumed.DBValue = value; MarkDirty(); } } } } /// /// WorkorderItemPartID record /// indicates which workorder item part record this part /// was used on /// public Guid WorkorderItemPartID { get { return mWorkorderItemPartID; } set { if(bReadOnly) ThrowSetError(); else { if(mWorkorderItemPartID!=value) { mWorkorderItemPartID = value; MarkDirty(); } } } } /// /// ID of PartInventoryAdjustmentItem object /// that caused the adjustment to happen /// public Guid AdjustmentID { get { return mAdjustmentID; } set { if(bReadOnly) ThrowSetError(); else { if(mAdjustmentID!=value) { mAdjustmentID = value; MarkDirty(); } } } } /// /// Purchase order receipt record ID /// Indicates which receipt this was entered from /// public Guid PurchaseOrderReceiptItemID { get { return mPurchaseOrderReceiptItemID; } set { if(bReadOnly) ThrowSetError(); else { if(mPurchaseOrderReceiptItemID!=value) { mPurchaseOrderReceiptItemID = value; MarkDirty(); } } } } /// ///Available for consumption /// public bool Available { get { return mAvailable; } set { if(bReadOnly) ThrowSetError(); else { if(mAvailable!=value) { mAvailable = 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.PartSerial") ) ); } #endregion #region System.object overrides /// /// /// /// public override string ToString() { return "PartSerial" + mID.ToString(); } /// /// /// /// /// public override bool Equals(Object obj) { if ( obj == null || GetType ( ) != obj.GetType ( ) ) return false; PartSerial c=(PartSerial)obj; return mID==c.mID; } /// /// /// /// public override int GetHashCode() { return ("PartSerial"+mID).GetHashCode(); } #endregion #region Static methods /// /// Create new PartSerial /// /// PartSerial internal static PartSerial NewItem(RootObjectTypes ObjectType) { if(AyaBizUtils.Right(ObjectType)>(int)SecurityLevelTypes.ReadOnly) { PartSerial c = new PartSerial(); return c; } else throw new System.Security.SecurityException( string.Format( LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToCreate"), LocalizedTextTable.GetLocalizedTextDirect("O.PartSerial"))); } /// /// Fetch existing PartSerial /// internal static PartSerial GetItem(SafeDataReader dr,RootObjectTypes ObjectType) { if(AyaBizUtils.Right(ObjectType)>(int)SecurityLevelTypes.NoAccess) { PartSerial child = new PartSerial(); child.Fetch(dr,ObjectType); return child; } else throw new System.Security.SecurityException( string.Format( LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToRetrieve"), LocalizedTextTable.GetLocalizedTextDirect("O.PartSerial"))); } /// /// Given a part serial record ID returns the serial number /// that is stored in that part serial record or an empty /// string if not found /// /// Guid value of partserial record /// String serial number or empty string if not found public static string GetSerialNumberFromPartSerialID(Guid PartSerialID) { //Case 499 //return SNHelper.GetSN(PartSerialID); return PartSerialFetcher.GetSN(PartSerialID); } #endregion #region DAL DATA ACCESS #region Fetch /// /// Populate this object from the values in the datareader passed to it /// private void Fetch(SafeDataReader dr,RootObjectTypes ObjectType) { //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"); //PartSerial fields mPartID=dr.GetGuid("aPartID"); mPartWarehouseID=dr.GetGuid("aPartWarehouseID"); mWorkorderItemPartID=dr.GetGuid("aWorkorderItemPartID"); mAdjustmentID=dr.GetGuid("AADJUSTMENTID"); mPurchaseOrderReceiptItemID=dr.GetGuid("aPurchaseOrderReceiptItemID"); mDateReceived=DBUtil.ToLocal(dr.GetSmartDate("aDateReceived")); mDateConsumed=DBUtil.ToLocal(dr.GetSmartDate("aDateConsumed")); //Important: use property not internal field //so that initial broken rule is unbroken on fetch SerialNumber=dr.GetString("aSerialNumber"); mAvailable=dr.GetBoolean("AAVAILABLE"); //Get access rights level bReadOnly=AyaBizUtils.Right(ObjectType)<(int)SecurityLevelTypes.ReadWrite; MarkOld(); } #endregion fetch #region Update /// /// /// /// 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,"aPartSerial"); #region Delete if(IsDeleted) { if(!IsNew) { //Delete object and child objects DBCommandWrapper cmDelete = DBUtil.GetCommandFromSQL("DELETE FROM aPartSerial 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 aPartSerial (aID, aPartID, aPartWarehouseID, aSerialNumber, aDateReceived, " + "aDateConsumed, aWorkorderItemPartID, AADJUSTMENTID, AAVAILABLE, " + "aPurchaseOrderReceiptItemID, aCreated, aModified, aCreator,aModifier) " + "VALUES (@ID, @PartID,@PartWarehouseID,@SerialNumber,@DateReceived, " + "@DateConsumed,@WorkorderItemPartID,@AdjustmentID,@Available, " + "@PurchaseOrderReceiptItemID,@Created,@Modified,@CurrentUserID,@CurrentUserID)" ); else cm=DBUtil.GetCommandFromSQL( "UPDATE aPartSerial SET aID=@ID, aPartID=@PartID, aPartWarehouseID=@PartWarehouseID, " + "aSerialNumber=@SerialNumber, " + "aDateReceived=@DateReceived, aDateConsumed=@DateConsumed, " + "aWorkorderItemPartID=@WorkorderItemPartID, " + "AADJUSTMENTID=@AdjustmentID, aPurchaseOrderReceiptItemID=@PurchaseOrderReceiptItemID, " + "AAVAILABLE=@Available, aModifier=@CurrentUserID, " + "aModified=@Modified WHERE aID=@ID" ); //PartSerial fields cm.AddInParameter("@PartID",DbType.Guid,mPartID); cm.AddInParameter("@PartWarehouseID",DbType.Guid,mPartWarehouseID); cm.AddInParameter("@WorkorderItemPartID",DbType.Guid,mWorkorderItemPartID); cm.AddInParameter("@AdjustmentID",DbType.Guid,mAdjustmentID); cm.AddInParameter("@PurchaseOrderReceiptItemID",DbType.Guid,mPurchaseOrderReceiptItemID); cm.AddInParameter("@DateReceived",DbType.DateTime,DBUtil.ToUTC(mDateReceived).DBValue); cm.AddInParameter("@DateConsumed",DbType.DateTime,DBUtil.ToUTC(mDateConsumed).DBValue); cm.AddInParameter("@SerialNumber",DbType.String,mSerialNumber); cm.AddInParameter("@Available",DbType.Boolean, mAvailable); //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 update #endregion #region Get serial number from id //Case 499 //[Serializable(), System.ComponentModel.Browsable(false)] // public class SNHelper //{ // string _SN = ""; // Guid _ID; // public SNHelper(Guid ID) // { // _ID=ID; // } // public static string GetSN(Guid ID) // { //todo fix for 425 // SNHelper e=new SNHelper(ID); // DataPortal.Update(e); // return e._SN; // } // public void DataPortal_Update() // { // this._SN=DBUtil.ScalarToString( // DBUtil.GetScalarFromSQLString( // "SELECT aSerialNumber FROM aPartSerial WHERE " + // "(aID = @ID)",_ID) // ); // } //} #endregion }//end PartSerial }//end namespace GZTW.AyaNova.BLL