/////////////////////////////////////////////////////////// // UserMRUs.cs // Implementation of Class UserMRUs // CSLA type: Editable root collection // Created on: 31-Oct-2007 // Object design: John // Coded: John 31-Oct-2007 /////////////////////////////////////////////////////////// using System; using System.Data; using CSLA.Data; using GZTW.Data; using CSLA; namespace GZTW.AyaNova.BLL { /// /// Editable root collection of objects /// [Serializable] public class UserMRUs : BusinessCollectionBase { //case 768 /// /// /// public bool InPortal = false; #region Constructor //Private constructor prevents direction instantiation private UserMRUs() { InPortal = AyaBizUtils.AtDataPortalServer; }//case 768 #endregion #region Business properties and methods /// /// Locale key so that generic list editor /// UI code knows what title to give the list in a /// grid /// public string LocaleKey { get { return "UserMRU.Label.List"; } } /// /// Retrieve UserMRU by index /// /// Index public UserMRU this[int Item] { get { return (UserMRU)List[Item]; } } /// /// MRU list in a object which is displayed in AyaNova UI /// public DataView MRU { get { DataTable dt = new DataTable("MRU"); dt.Columns.Add("TID", typeof(TypeAndID)); dt.Columns.Add("Description", typeof(string)); dt.Columns.Add("Created", typeof(DateTime)); dt.DefaultView.Sort = "Created desc"; foreach (UserMRU m in List) { DataRow dr = dt.NewRow(); dr["TID"] = new TypeAndID(m.ObjectType, m.ObjectID); dr["Description"] = m.Description; dr["Created"] = m.Created; dt.Rows.Add(dr); } return dt.DefaultView; } } /// /// Add a new UserMRU to the collection /// if it isn't already in the collection /// otherwise just set the existing mru's /// created date to now and return it. /// /// (this is not intended for direct usage normally /// opening any of the main business objects that are supported /// will automatically create an MRU entry) /// public UserMRU Add(RootObjectTypes ObjectType, Guid ObjectID) { //TODO if dataportal need to call update on change here same //for remove below re case 768 foreach (UserMRU m in List) { if (m.ObjectID == ObjectID) { m.Created = DBUtil.CurrentWorkingDateTime; //case 768 if (InPortal) this.Save(); return m; } } UserMRU child = UserMRU.NewItem(ObjectType,ObjectID); List.Add(child); //case 768 if (InPortal) this.Save(); return child; } /// /// Remove UserMRU if present /// (not for direct api use) /// public void Remove(RootObjectTypes ObjectType, Guid ObjectID) { foreach (UserMRU m in List) { if (m.ObjectID == ObjectID) { List.Remove(m); //case 768 if (InPortal) this.Save(); return; } } } /// /// Remove UserMRU if present /// (not intended or required for direct api use) /// public void Remove(TypeAndID TID) { foreach (UserMRU m in List) { if (m.ObjectID == TID.ID) { List.Remove(m); //case 768 if (InPortal) this.Save(); return; } } } #endregion #region Contains /// /// Check if item in collection /// /// public bool Contains(UserMRU obj) { foreach (UserMRU child in List) { if (child.Equals(obj)) return true; } return false; } ///// ///// Check if item in collection by form name ///// // //public bool Contains(string FormName) //{ // foreach (UserMRU child in List) // { // if (child.FormName == FormName) return true; // } // return false; //} /// /// Check if item in deleted collection /// /// public bool ContainsDeleted(UserMRU obj) { foreach (UserMRU child in deletedList) { if (child.Equals(obj)) return true; } return false; } #endregion #region Static methods /// /// NewItems /// /// public static UserMRUs NewItems() { return new UserMRUs(); } /// /// Get item collection (Root collection style) /// /// /// public static UserMRUs GetItems(Guid UserID) { UserMRUs col = new UserMRUs(); return (UserMRUs)DataPortal.Fetch(new Criteria(UserID)); } #endregion #region DAL DATA ACCESS /// /// Fetch children root style /// /// protected override void DataPortal_Fetch(object Criteria) { Criteria crit = (Criteria)Criteria; SafeDataReader dr = null; try { dr = DBUtil.GetReaderFromSQLString("SELECT * FROM aUserMRU WHERE aUserID=@ID", crit.UserID); while (dr.Read()) { List.Add(UserMRU.GetItem(dr)); } } finally { if (dr != null) dr.Close(); } } /// /// Editable Root Collection Update /// protected override void DataPortal_Update() { //Remove all but the top 10 //case 768 //keep track of inportal value and temporarily set it to false //to prevent the remove statement from triggering a save bool wasinportal = InPortal; InPortal = false; DataView dv = this.MRU; int x = 0; foreach (DataRowView r in dv) { x++; if (x > 10) { Remove((TypeAndID)r["TID"]); } } //case 768 InPortal = wasinportal; using (IDbConnection connection = DBUtil.DB.GetConnection()) { connection.Open(); IDbTransaction tr = connection.BeginTransaction(); try { //update (thus deleting) any deleted child objects foreach (UserMRU child in deletedList) { child.Update(tr); } //Now that they are deleted remove them from memory deletedList.Clear(); foreach (UserMRU child in List) { child.Update(tr); } tr.Commit(); } catch { tr.Rollback(); throw;//WAS: throw(ex); } } } #endregion #region criteria /// /// Criteria for identifying existing object /// [Serializable] private class Criteria { public Guid UserID; public Criteria(Guid _UserID) { UserID = _UserID; } } #endregion }//end UserMRUs }//end namespace GZTW.AyaNova.BLL