/////////////////////////////////////////////////////////// // AssignedDoc.cs // Implementation of Class AssignedDoc // CSLA type: Editable Child // Created on: 07-Jun-2004 8:41:13 AM // Object design: Joyce // Coded: John July 9 2004 // Re-coded: as editable child by John Nov 11 2004 /////////////////////////////////////////////////////////// using System; using System.Data; using CSLA.Data; using GZTW.Data; using CSLA; using System.Threading; using CSLA.Security; namespace GZTW.AyaNova.BLL { /// /// AssignedDoc class /// This is the original method of attaching documents to various objects in AyaNova and is used to link a file location on disk to a business object. /// If you want to actually store the document itself in the AyaNova database (making it accessible from WBI and location independant) /// you should use the object instead to store documents in the database. /// /// [Serializable] public class AssignedDoc : BusinessBase { #region Attributes private bool bReadOnly; private Guid mID; private Guid mRootObjectID; private RootObjectTypes mRootObjectType; private SmartDate mCreated; private SmartDate mModified; private Guid mCreator; private Guid mModifier; private string mDescription=""; private string mURL=null; //case 1584 private RootObjectTypes mExactObjectType; #endregion #region Constructor /// /// Private constructor to prevent direct instantiation /// private AssignedDoc() { MarkAsChild(); //Set to read / write initially so that properties //can be set bReadOnly=false; mID=Guid.NewGuid(); //pre-break the rule this.URL=""; //Set record history to defaults mCreated = new SmartDate(DBUtil.CurrentWorkingDateTime); mModified=new SmartDate(); mCreator=User.CurrentThreadUserID; mModifier=User.CurrentThreadUserID; //case 1584 mExactObjectType = RootObjectTypes.Nothing; } #endregion #region Business properties /// /// ID of assigneddoc entry /// public Guid ID { get { return mID; } } /// /// RootObject Type /// public RootObjectTypes RootObjectType { get { return mRootObjectType; } set { if(bReadOnly) ThrowSetError(); else { if(mRootObjectType!=value) { mRootObjectType = value; MarkDirty(); } } } } /// /// RootObjectID /// public Guid RootObjectID { get { return mRootObjectID; } set { if(bReadOnly) ThrowSetError(); else { if(mRootObjectID!=value) { mRootObjectID = value; MarkDirty(); } } } } /// /// AssignedDocs associated with any workorder type are saved attributed to rootobject type Workorder. /// Since version 7 that has been deprecated but to maintain backward compatibility wikipages are still /// associated with Workorder. /// /// This properly shows the actual exact workorder type that retrieved this assigneddoc object. /// /// It is not stored in the database but used on the fly internally by AyaNova to check exact rights. /// public RootObjectTypes ExactRootObjectType //case 1584 { get { return mExactObjectType; } set { mExactObjectType = value; } } /// /// 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 Description of item /// /// public string Description { get { return mDescription; } set { if(bReadOnly) ThrowSetError(); else { if(mDescription!=value) { mDescription = value; BrokenRules.Assert("DescriptionLength", "Error.Object.FieldLengthExceeded255,AssignedDoc.Label.Description","Description",value.Length>255); MarkDirty(); } } } } /// ///URL for document ///This could be an internet URL, a UNC path and file name or a ///drive letter path filename to the file in question /// ///Basically this is anything that is understood by the Windows Shell ///Maximum of 4000 characters /// public string URL { get { return mURL; } set { if(bReadOnly) ThrowSetError(); else { if(mURL!=value) { mURL = value; BrokenRules.Assert("URLRequired", "Error.Object.RequiredFieldEmpty,AssignedDoc.Label.URL","URL",value.Length==0); BrokenRules.Assert("URLLength", "Error.Object.FieldLengthExceeded,AssignedDoc.Label.URL,3500","URL",value.Length>3500); 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.AssignedDoc") ) ); } #endregion #region System.Object overrides /// /// /// /// public override string ToString() { return "AssignedDoc"+ mID.ToString(); } /// /// /// /// /// public override bool Equals(Object obj) { if ( obj == null || GetType ( ) != obj.GetType ( ) ) return false; AssignedDoc c=(AssignedDoc)obj; return (mID==c.mID); } /// /// /// /// public override int GetHashCode() { return ("AssignedDoc"+ mID.ToString()).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) { SearchResult sr=new SearchResult(); System.Text.StringBuilder sb = new System.Text.StringBuilder(); SafeDataReader dr = null; try { dr=DBUtil.GetReaderFromSQLString( "SELECT aCreated, aModified, aCreator, aModifier, aRootObjectID, " + "aRootObjectType, aURL, aDescription FROM AASSIGNEDDOC " + "WHERE (aID = @ID)",ID); if(!dr.Read()) return new SearchResult();//DBUtil.ThrowFetchError("SearchResult for AssignedDoc: " + ID.ToString()); //Get description for later adding to description string sr.Description=dr.GetString("aDescription"); sb.Append(dr.GetString("aURL")); sb.Append(" "); sb.Append(sr.Description); sr.AncestorRootObjectID=dr.GetGuid("aRootObjectID"); sr.AncestorRootObjectType=(RootObjectTypes)dr.GetInt16("aRootObjectType"); 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(); } //Case 58 //Is the document's ancestor within their region? if ( !AyaBizUtils.InYourRegion( ObjectRegionIDFetcher.ObjectRegion( new TypeAndID( sr.AncestorRootObjectType,sr.AncestorRootObjectID ) ) ) ) return new SearchResult();//case 58 //Security check..do they have rights to the ancestor object? if(AyaBizUtils.Right("Object." + sr.AncestorRootObjectType.ToString())<(int)SecurityLevelTypes.ReadOnly) return new SearchResult(); //Formulate results ExtractAndRank er = new ExtractAndRank(); er.Process(sb.ToString().Trim(),searchTerms); sr.Extract=er.Extract; sr.Rank=er.Ranking; return sr; } #endregion #region Static methods /// /// Get new object /// Parent is responsible for RootObjectID, RootObjectType and DocumentID /// /// internal static AssignedDoc NewItem(RootObjectTypes ObjectType, RootObjectTypes ExactObjectType) //case 1584 { if(AyaBizUtils.Right(ExactObjectType)>(int)SecurityLevelTypes.ReadOnly) return new AssignedDoc(); else throw new System.Security.SecurityException( string.Format( LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToCreate"), LocalizedTextTable.GetLocalizedTextDirect("O.AssignedDoc"))); } internal static AssignedDoc GetItem(SafeDataReader dr, RootObjectTypes ObjectType, RootObjectTypes ExactObjectType) //case 1584 { if (AyaBizUtils.Right(ExactObjectType) > (int)SecurityLevelTypes.NoAccess) { AssignedDoc child = new AssignedDoc(); child.Fetch(dr,ExactObjectType); return child; } else throw new System.Security.SecurityException( string.Format( LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToRetrieve"), LocalizedTextTable.GetLocalizedTextDirect("O.AssignedDoc"))); } #endregion #region DAL Data access /// /// Populate this object from the values in the datareader passed to it /// /// /// private void Fetch(SafeDataReader dr, RootObjectTypes ExactObjectType) { //Standard fields mCreated=DBUtil.ToLocal(dr.GetSmartDate("aCreated")); mModified=DBUtil.ToLocal(dr.GetSmartDate("aModified")); mCreator=dr.GetGuid("aCreator"); mModifier=dr.GetGuid("aModifier"); //AssignedDoc fields mID=dr.GetGuid("aID"); mRootObjectID=dr.GetGuid("aRootObjectID"); mRootObjectType=(RootObjectTypes)dr.GetInt16("aRootObjectType"); mExactObjectType = ExactObjectType;//case 1584 mDescription=dr.GetString("aDescription"); URL=dr.GetString("aURL"); //Get access rights level bReadOnly=AyaBizUtils.Right(ExactObjectType)<(int)SecurityLevelTypes.ReadWrite;//case 1584 MarkOld(); } /// /// /// /// internal void Update(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.CheckSafeToUpdateInsideTransaction(this.mModified.Date, this.mID, "AASSIGNEDDOC", tr);//case 1960 #region Delete if(IsDeleted) { if(!IsNew) { //Delete user and child objects DBCommandWrapper cmDelete = DBUtil.GetCommandFromSQL("DELETE FROM AASSIGNEDDOC WHERE aID = @ID;"); cmDelete.AddInParameter("@ID",DbType.Guid,this.mID); DBUtil.DB.ExecuteNonQuery(cmDelete, tr); DBUtil.RemoveKeywords(tr,RootObjectTypes.AssignedDocument,this.ID); } MarkNew(); return; } #endregion #region Add / Update DBCommandWrapper cm = null; if(IsNew)//Add or update? cm=DBUtil.GetCommandFromSQL( "INSERT INTO AASSIGNEDDOC (aRootObjectID,aRootObjectType, aID, aURL, aDescription, aCreated,aModified,aCreator,aModifier) " + "VALUES (@RootObjectID,@RootObjectType, @ID, @URL,@Description, @Created, @Modified, @CurrentUserID,@CurrentUserID)" ); else cm=DBUtil.GetCommandFromSQL( "UPDATE AASSIGNEDDOC SET aRootObjectID=@RootObjectID, " + "aRootObjectType=@RootObjectType, aID=@ID, aURL=@URL, " + "aDescription=@Description,aModified=@Modified, aModifier=@CurrentUserID " + "WHERE aID=@ID" ); cm.AddInParameter("@ID",DbType.Guid,mID); cm.AddInParameter("@RootObjectID",DbType.Guid, mRootObjectID); cm.AddInParameter("@RootObjectType",DbType.Int16, mRootObjectType); cm.AddInParameter("@Description",DbType.String, mDescription); cm.AddInParameter("@URL",DbType.String, mURL); 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); //Process keywords DBUtil.ProcessKeywords(tr,this.mID,RootObjectTypes.AssignedDocument,IsNew,AyaBizUtils.Break(false,mDescription,mURL)); MarkOld();//db is now synched with object //Successful update so //change modification time to match this.mModified.Date=dtModified; #endregion } #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 RootObjectID; // public Guid DocumentID; // public RootObjectTypes RootObjectType; // public Criteria(Guid _RootObjectID, Guid _DocumentID, RootObjectTypes _RootObjectType ) // { // RootObjectID=_RootObjectID; // DocumentID=_DocumentID; // RootObjectType=_RootObjectType; // // } // // } #endregion }//end AssignedDoc }//end namespace GZTW.AyaNova.BLL