/////////////////////////////////////////////////////////// // PartAssemblies.cs // Implementation of Class PartAssemblies // 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 PartAssemblies : BusinessCollectionBase { #region Constructor //Private constructor prevents direction instantiation private PartAssemblies() { 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 "PartAssembly.Label.List"; } } /// /// Retrieve PartAssembly by index /// /// Index public PartAssembly this[int Item] { get { return (PartAssembly) List[Item]; } } /// /// Remove PartAssembly by passing it in /// /// public void Remove(PartAssembly obj) { List.Remove(obj); } /// /// Remove by Guid value of ID /// /// public void Remove(Guid ID) { PartAssembly delete = null; foreach (PartAssembly child in List) { if (child.ID == ID) { delete = child; break; } } if (delete != null) Remove(delete); } /// /// Add a new PartAssembly to the collection /// public PartAssembly Add() { PartAssembly child=PartAssembly.NewItem(); List.Add(child); return child; } /// /// Add PartAssembly by passing it in /// /// public void Add(PartAssembly obj) { List.Add(obj); } /// /// /// /// protected override object OnAddNew() { PartAssembly child=PartAssembly.NewItem(); List.Add(child); return child; } /// /// Get a list of all duplicate names in this list /// public System.Collections.Generic.List DuplicateNames { get { System.Collections.Generic.List dupes = new System.Collections.Generic.List(); System.Collections.Generic.List all = new System.Collections.Generic.List(); foreach (PartAssembly i in List) { if (all.Contains(i.Name)) { dupes.Add(i.Name); } else { all.Add(i.Name); } } return dupes; } } #endregion #region Contains /// /// Check if item in collection /// /// public bool Contains(PartAssembly obj) { foreach (PartAssembly child in List) { if(child.Equals(obj)) return true; } return false; } /// /// Check if item in deleted collection /// /// public bool ContainsDeleted(PartAssembly obj) { foreach (PartAssembly child in deletedList) { if(child.Equals(obj)) return true; } return false; } #endregion #region Static methods /// /// Get item collection /// /// /// public static PartAssemblies GetItems() { //in future specify criteria if filtering (e.g. filter by region) PartAssemblies col = new PartAssemblies(); return (PartAssemblies)DataPortal.Fetch(new Criteria()); } /// /// Check all items for duplicate names /// /// List of duplicate names public static System.Collections.Generic.List DuplicateNameCheck() { return GetItems().DuplicateNames; } #endregion #region DAL DATA ACCESS /// /// Fetch children /// /// protected override void DataPortal_Fetch(object Criteria) { Criteria crit = (Criteria)Criteria; SafeDataReader dr = null; try { dr=DBUtil.GetReaderFromSQLString("SELECT * FROM aPartAssembly ORDER BY ANAME;"); while(dr.Read()) { List.Add(PartAssembly.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 (PartAssembly child in deletedList) { child.Update(tr); } //Now that they are deleted remove them from memory deletedList.Clear(); foreach (PartAssembly 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 Criteria(/*string _ObjectName*/) { //ObjectName=_ObjectName; } } #endregion }//end PartAssemblies }//end namespace GZTW.AyaNova.BLL