/////////////////////////////////////////////////////////// // DispatchZones.cs // Implementation of Class DispatchZones // CSLA type: Editable root collection // Created on: 04-Nov-2004 // Object design: John // Coded: John 04-Nov-2004 /////////////////////////////////////////////////////////// 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 DispatchZones : BusinessCollectionBase { #region Constructor //Private constructor prevents direction instantiation private DispatchZones() { 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 "DispatchZone.Label.List"; } } /// /// Retrieve DispatchZone by index /// /// Index public DispatchZone this[int Item] { get { return (DispatchZone) List[Item]; } } /// /// Remove DispatchZone by passing it in /// /// public void Remove(DispatchZone obj) { List.Remove(obj); } /// /// Remove by Guid value of ID /// /// public void Remove(Guid ID) { DispatchZone delete = null; foreach (DispatchZone child in List) { if (child.ID == ID) { delete = child; break; } } if (delete != null) Remove(delete); } /// /// Add a new DispatchZone to the collection /// public DispatchZone Add() { DispatchZone child=DispatchZone.NewItem(); List.Add(child); return child; } /// /// Add DispatchZone by passing it in /// /// public void Add(DispatchZone obj) { List.Add(obj); } /// /// /// /// protected override object OnAddNew() { DispatchZone child=DispatchZone.NewItem(); List.Add(child); return child; } #endregion #region Contains /// /// Check if item in collection /// /// public bool Contains(DispatchZone obj) { foreach (DispatchZone child in List) { if(child.Equals(obj)) return true; } return false; } /// /// Check if item in deleted collection /// /// public bool ContainsDeleted(DispatchZone obj) { foreach (DispatchZone child in deletedList) { if(child.Equals(obj)) return true; } return false; } #endregion #region Static methods /// /// Get item collection /// /// /// public static DispatchZones GetItems(bool Regional) { //in future specify criteria if filtering (e.g. filter by region) DispatchZones col = new DispatchZones(); return (DispatchZones)DataPortal.Fetch(new Criteria(Regional)); } #endregion #region DAL DATA ACCESS /// /// Fetch children /// /// protected override void DataPortal_Fetch(object Criteria) { Criteria crit = (Criteria)Criteria; SafeDataReader dr = null; try { //case 58 dr=DBUtil.GetReaderFromSQLString(DBUtil.AddRegionFilter(//case 58 "SELECT * FROM aDispatchZone ORDER BY ANAME ", "aDispatchZone", "", crit.Regional ) ); while(dr.Read()) { List.Add(DispatchZone.GetItem(dr)); } } finally { if(dr!=null) dr.Close(); } } /// /// 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 (DispatchZone child in deletedList) { child.Update(tr); } //Now that they are deleted remove them from memory deletedList.Clear(); foreach (DispatchZone 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 string ObjectName; public bool Regional; public Criteria(bool _Regional) { Regional = _Regional; } } #endregion }//end DispatchZones }//end namespace GZTW.AyaNova.BLL