/////////////////////////////////////////////////////////// // ClientNotes.cs // Implementation of Class ClientNotes // CSLA type: Editable root collection // Created on: 11-Nov-2004 // Object design: John // Coded: John 11-Nov-2004 /////////////////////////////////////////////////////////// using System; using System.Data; using CSLA.Data; using GZTW.Data; using CSLA; using System.Collections.Generic; namespace GZTW.AyaNova.BLL { /// /// Collection of objects attached to a /// [Serializable] public class ClientNotes : BusinessCollectionBase { #region Constructor //Private constructor prevents direction instantiation private ClientNotes() { AllowSort=false; AllowFind=true; AllowEdit=true; AllowNew=true; AllowRemove=true; } #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 "ClientNote.Label.List"; } } /// /// Reference by index /// /// Index public ClientNote this[int Item] { get { return (ClientNote) List[Item]; } } /// /// Reference from collection by ID /// /// /// public ClientNote this[Guid id] { get { foreach (ClientNote child in List) { if (child.ID == id) return child; } return null; } } /// /// Reference in collection based on a string representation of it's Guid value /// /// /// public ClientNote this[string sid] { get{ return this[new Guid(sid)]; } } /// /// Remove by passing it in /// /// public void Remove(ClientNote obj) { List.Remove(obj); } /// /// Remove from collection by ID value /// /// public void Remove(Guid ID) { ClientNote delete = null; foreach (ClientNote child in List) { if (child.ID == ID) { delete = child; break; } } if (delete != null) Remove(delete); } /// /// Remove from collection based on a string representation of it's ID /// /// public void Remove(string sID) { Remove(new Guid(sID)); } /// /// Add a new ClientNote to the collection /// public ClientNote Add() { ClientNote child=ClientNote.NewItem(); List.Add(child); return child; } /// /// Add ClientNote by passing it in /// /// public void Add(ClientNote obj) { List.Add(obj); } // protected override object OnAddNew() // { // ClientNote child=ClientNote.NewItem(); // List.Add(child); // return child; // } #endregion #region Contains /// /// Check if item in collection /// /// public bool Contains(ClientNote obj) { foreach (ClientNote child in List) { if(child.Equals(obj)) return true; } return false; } /// /// Check if item in deleted collection /// /// public bool ContainsDeleted(ClientNote obj) { foreach (ClientNote child in deletedList) { if(child.Equals(obj)) return true; } return false; } #endregion #region Reporting and shared UI editor helpers /// /// Returns the report key which is a property of /// reports used to link all reports that can be used /// with a particular data source. /// public static string ReportKey { get { return "ClientNoteList"; } } /// /// Returns the Detailed report key /// which is used to determine which reports and objects /// will be used for detailed reports /// /// If empty string then indicates there is no detailed report object or reports /// public static string DetailedReportKey { get { return ""; } } /// /// Base object that this list is reporting on /// used by shared UI editor to instantiate new objects /// when user selects new in UI elements that display this list /// /// (I.E. when user clicks on new in a read only list grid, this is the object type created) /// public static RootObjectTypes BaseObjectType { get { return RootObjectTypes.ClientNote; } } /// /// The Type of the struct used to store list records /// Used to fetch the custom display attributes of the fields /// contained within the record to modify the grid display accordingly /// /// public static Type ListRecordType { get { return typeof(ClientNote); } } /// /// Field that contains the ID of the objects /// that are the basis of this list. /// /// Used for compiling an ID list for reporting from user /// selections in a grid. /// public static string IDField { get { return "ID"; } } /// /// Same as IDField but for detailed reports /// public static string IDFieldDetailed { get { return IDField; } } #endregion #region Static methods /// /// Get item collection /// /// /// public static ClientNotes GetItems(Guid ClientID) { ClientNotes col = new ClientNotes(); return (ClientNotes)DataPortal.Fetch(new Criteria(ClientID, null)); } /// /// Get list by items indicated in IDList /// /// Generic list of Guid's /// public static ClientNotes GetListFromIDList(List IDList) { //case 274 //Handle empty list if (IDList.Count == 0) return new ClientNotes(); return (ClientNotes)DataPortal.Fetch(new Criteria(Guid.Empty, IDList)); } #endregion #region DAL DATA ACCESS #region Fetch /// /// Fetch children /// /// protected override void DataPortal_Fetch(object Criteria) { Criteria crit = (Criteria)Criteria; SafeDataReader dr = null; try { if (crit.IDList != null) { System.Text.StringBuilder sbIN = new System.Text.StringBuilder(); sbIN.Append("SELECT * FROM ACLIENTNOTE WHERE aClientNote.aID in ("); foreach (Guid gItem in crit.IDList) { sbIN.Append("'"); sbIN.Append("{"); sbIN.Append(gItem.ToString().ToUpperInvariant()); sbIN.Append("}"); sbIN.Append("',"); } sbIN.Length = sbIN.Length - 1; sbIN.Append(") order by anotedate desc"); dr = DBUtil.GetReaderFromSQLString(sbIN.ToString()); } else { dr = DBUtil.GetReaderFromSQLString("SELECT * FROM aClientNote WHERE aClientID=@ID order by anotedate desc", crit.ClientID); } while (dr.Read()) { List.Add(ClientNote.GetItem(dr)); } } finally { if(dr!=null) dr.Close(); } } #endregion fetch #region Update /// /// Editable Root Collection Update /// protected override void DataPortal_Update() { using (IDbConnection connection = DBUtil.DB.GetConnection()) { connection.Open(); IDbTransaction tr = connection.BeginTransaction(); try { //update (thus deleting) any deleted child objects foreach (ClientNote child in deletedList) { child.Update(tr); } //Now that they are deleted remove them from memory deletedList.Clear(); foreach (ClientNote child in List) { child.Update(tr); } tr.Commit(); } catch { tr.Rollback(); throw;//WAS: throw(ex); } } } #endregion update #endregion #region criteria /// /// Criteria for identifying existing object /// [Serializable] private class Criteria { public Guid ClientID; public List IDList; public Criteria(Guid _ClientID, List _IDList) { ClientID=_ClientID; IDList = _IDList; } } #endregion }//end ClientNotes }//end namespace GZTW.AyaNova.BLL