/////////////////////////////////////////////////////////// // ScheduleableUserGroup.cs // Implementation of Class ScheduleableUserGroup // CSLA type: Editable Root // Created on: 19-Aug-2004 8:40:00 AM // Object design: John // Coded: John 19-Aug-2004 8:40:00 AM /////////////////////////////////////////////////////////// using System; using System.Data; using CSLA.Data; using GZTW.Data; using CSLA; using System.Threading; using CSLA.Security; namespace GZTW.AyaNova.BLL { /// /// ScheduleableUserGroup - a preselected group of individual scheduleable /// users. Used for dispatching and display purposes /// [Serializable] public class ScheduleableUserGroup : BusinessBase { #region Attributes private bool bReadOnly; private Guid mID; private string mName=null; private SmartDate mCreated; private SmartDate mModified; private bool mActive; private Guid mCreator; private Guid mModifier; private string mDescription=""; private ScheduleableUserGroupUsers mScheduleableUserGroupUsers; #endregion #region Constructor /// /// Private constructor to prevent direct instantiation /// private ScheduleableUserGroup() { //Set to read / write initially so that properties //can be set bReadOnly=false; //New ID mID = Guid.NewGuid(); Name = DBUtil.CurrentWorkingDateTime.ToString(); Active=true; mScheduleableUserGroupUsers=ScheduleableUserGroupUsers.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 ID of person who modified this record /// /// /// public Guid Modifier { get { return mModifier; } } /// /// Set/get client group name /// public string Name { get { return mName; } set { if(bReadOnly) ThrowSetError(); else { if(mName!=value) { mName = value; BrokenRules.Assert("NameRequired","Error.Object.RequiredFieldEmpty,ScheduleableUserGroup.Label.Name","Name",value.Length==0); BrokenRules.Assert("NameLength", "Error.Object.FieldLengthExceeded255,ScheduleableUserGroup.Label.Name","Name",value.Length>255); MarkDirty(); } } } } /// /// Get /set active status of client group /// If active = true then ScheduleableUserGroup is selectable for workorders etc /// If active = false then ScheduleableUserGroup in not selectable, but history can be /// viewed /// public bool Active { get { return mActive; } set { if(bReadOnly) ThrowSetError(); else { if(mActive!=value) { mActive = value; MarkDirty(); } } } } /// /// /// public string Description { get { return mDescription; } set { if(bReadOnly) ThrowSetError(); else { if(mDescription!=value) { mDescription = value; MarkDirty(); } } } } /// /// ScheduleableUsers collection /// public ScheduleableUserGroupUsers ScheduleableUsers { get { return mScheduleableUserGroupUsers; } } /// /// 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.ScheduleableUserGroup") ) ); } #endregion #region System.Object overrides /// /// /// /// public override string ToString() { return "ScheduleableUserGroup" + mID.ToString(); } /// /// /// /// /// public override bool Equals(Object obj) { if ( obj == null || GetType ( ) != obj.GetType ( ) ) return false; ScheduleableUserGroup c=(ScheduleableUserGroup)obj; return mID==c.mID; } /// /// /// /// public override int GetHashCode() { return ("ScheduleableUserGroup"+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.ScheduleableUserGroup")<(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, aName, " + " aDescription FROM aScheduleableUserGroup WHERE " + "(aID = @ID)" ,ID); if(!dr.Read()) return new SearchResult();//DBUtil.ThrowFetchError("SearchResult for ScheduleableUserGroupID: " + ID.ToString()); sr.Description=dr.GetString("aName"); sb.Append(sr.Description); sb.Append(" "); sb.Append(dr.GetString("aDescription")); 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.ScheduleableUserGroup; return sr; } #endregion #region Static methods /// /// Default guid to indicate All Users pseudo group /// This is not an actual group stored in the database /// /// Used in lists with ScheduleableUserGroups as /// the item that will return all users regardless of group membership /// public static Guid AllUsersGroupID { get { return new Guid("{A51ECBE7-792F-45bc-A88D-B16E7E6B1CDD}"); } } /// /// Create new ScheduleableUserGroup /// public static ScheduleableUserGroup NewItem() { if(AyaBizUtils.Right("Object.ScheduleableUserGroup")>(int)SecurityLevelTypes.ReadOnly) return new ScheduleableUserGroup(); else throw new System.Security.SecurityException( string.Format( LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToCreate"), LocalizedTextTable.GetLocalizedTextDirect("O.ScheduleableUserGroup"))); } /// /// Fetch existing ScheduleableUserGroup /// /// Client Guid public static ScheduleableUserGroup GetItem(Guid _ID) { if(AyaBizUtils.Right("Object.ScheduleableUserGroup")>(int)SecurityLevelTypes.NoAccess) return (ScheduleableUserGroup)DataPortal.Fetch(new Criteria(_ID)); else throw new System.Security.SecurityException( string.Format( LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToRetrieve"), LocalizedTextTable.GetLocalizedTextDirect("O.ScheduleableUserGroup"))); } /// /// Delete ScheduleableUserGroup (if clients assigned to this group, should be notified) /// /// Client GUID public static void DeleteItem(Guid _ID) { if(AyaBizUtils.Right("Object.ScheduleableUserGroup")>(int)SecurityLevelTypes.ReadWrite) DataPortal.Delete(new Criteria(_ID)); else throw new System.Security.SecurityException( string.Format( LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToDelete"), LocalizedTextTable.GetLocalizedTextDirect("O.ScheduleableUserGroup"))); } /// /// 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("ASCHEDULEABLEUSERGROUP", "ANAME", Name); } #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 aScheduleableUserGroup WHERE aID=@ID;",crit.ID); if(!dr.Read()) DBUtil.ThrowFetchError("ScheduleableUserGroup 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"); //ScheduleableUserGroup fields Active=dr.GetBoolean("AACTIVE"); //Important: use property not internal field //so that initial broken rule is unbroken on fetch Name=dr.GetString("aName"); Description=dr.GetString("aDescription"); if(dr!=null) dr.Close(); //Children dr=DBUtil.GetReaderFromSQLString( "SELECT * FROM aScheduleableUserGroupUser " + "WHERE aScheduleableUserGroupID=@ID" ,crit.ID); mScheduleableUserGroupUsers = ScheduleableUserGroupUsers.GetItems(dr); if(dr!=null) dr.Close(); } finally { if(dr!=null) dr.Close(); } MarkOld(); //Get access rights level bReadOnly=AyaBizUtils.Right("Object.ScheduleableUserGroup")<(int)SecurityLevelTypes.ReadWrite; } #endregion fetch #region Update /// /// Called by DataPortal to delete/add/update data into the database /// protected override void DataPortal_Update() { // 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,"aScheduleableUserGroup"); #region Delete if(IsDeleted) { if(!IsNew) { //Delete object and child objects DBCommandWrapper cmDelete = DBUtil.GetCommandFromSQL("DELETE FROM aScheduleableUserGroup WHERE aID = @ID;"); cmDelete.AddInParameter("@ID",DbType.Guid,this.mID); //Delete all child records DBCommandWrapper cmDeleteChildren = DBUtil.GetCommandFromSQL("DELETE FROM aScheduleableUserGroupUser WHERE aScheduleableUserGroupID=@ID;"); cmDeleteChildren.AddInParameter("@ID",DbType.Guid,this.mID); using (IDbConnection connection = DBUtil.DB.GetConnection()) { connection.Open(); IDbTransaction transaction = connection.BeginTransaction(); try { DBUtil.DB.ExecuteNonQuery(cmDelete, transaction); DBUtil.DB.ExecuteNonQuery(cmDeleteChildren, transaction); DBUtil.RemoveKeywords(transaction,RootObjectTypes.ScheduleableUserGroup,this.mID); // Commit the transaction transaction.Commit(); } catch { // Rollback transaction transaction.Rollback(); throw; } finally { connection.Close(); } } //----------------------------- } 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 aScheduleableUserGroup (aID, AACTIVE, aName, " + "aDescription, aCreated,aModified,aCreator,aModifier) VALUES (@ID, " + "@Active,@Name,@Description,@Created,@Modified,@CurrentUserID,@CurrentUserID)" ); else cm=DBUtil.GetCommandFromSQL( "UPDATE aScheduleableUserGroup SET aID=@ID, AACTIVE=@Active, " + "aName=@Name, aDescription=@Description, " + "aModifier=@CurrentUserID, aModified=@Modified " + "WHERE aID=@ID" ); cm.AddInParameter("@ID",DbType.Guid,mID); cm.AddInParameter("@Active",DbType.Boolean, mActive); cm.AddLargeStringInParameter("@Description",mDescription); cm.AddInParameter("@Name",DbType.String, mName); //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(); IDbTransaction transaction = connection.BeginTransaction(); try { DBUtil.DB.ExecuteNonQuery(cm, transaction); //Update child objects mScheduleableUserGroupUsers.Update(this,transaction); //Process keywords DBUtil.ProcessKeywords(transaction,this.mID,RootObjectTypes.ScheduleableUserGroup,IsNew,AyaBizUtils.Break(false, mName,mDescription)); MarkOld();//db is now synched with object // Commit the transaction transaction.Commit(); } catch { // Rollback transaction transaction.Rollback(); throw; } finally { connection.Close(); } //Successful update so //change modification time to match this.mModified.Date=dtModified; } #endregion } #endregion update #region Delete /// /// Remove a ScheduleableUserGroup record from the database /// /// protected override void DataPortal_Delete(object Criteria) { Criteria crit = (Criteria)Criteria; //Delete object and child objects DBCommandWrapper cmDelete = DBUtil.GetCommandFromSQL("DELETE FROM aScheduleableUserGroup WHERE aID = @ID;"); cmDelete.AddInParameter("@ID",DbType.Guid,crit.ID); //Delete all child records DBCommandWrapper cmDeleteChildren = DBUtil.GetCommandFromSQL("DELETE FROM aScheduleableUserGroupUser WHERE aScheduleableUserGroupID=@ID;"); cmDeleteChildren.AddInParameter("@ID",DbType.Guid,crit.ID); using (IDbConnection connection = DBUtil.DB.GetConnection()) { connection.Open(); IDbTransaction transaction = connection.BeginTransaction(); try { DBUtil.DB.ExecuteNonQuery(cmDelete, transaction); DBUtil.DB.ExecuteNonQuery(cmDeleteChildren, transaction); DBUtil.RemoveKeywords(transaction,RootObjectTypes.ScheduleableUserGroup,crit.ID); // Commit the transaction transaction.Commit(); } catch { // Rollback transaction transaction.Rollback(); throw; } finally { connection.Close(); } } } #endregion delete #endregion #region Override IsValid / IsDirty //Override base class version if there are child objects /// /// /// public override bool IsValid { get { return base.IsValid && mScheduleableUserGroupUsers.IsValid; } } /// /// /// public override bool IsDirty { get { return base.IsDirty || mScheduleableUserGroupUsers.IsDirty; } } #endregion #region criteria /// /// Criteria for identifying existing object /// [Serializable] private class Criteria { public Guid ID; public Criteria(Guid _ID) { ID=_ID; } } #endregion }//end Task }//end namespace GZTW.AyaNova.BLL