/////////////////////////////////////////////////////////// // UnitModelPickList.cs // Implementation of Class UnitModelPickList // CSLA type: Read only collection // Created on: 18-Jan-2005 // Object design: John // Coded: 18-Jan-2005 /////////////////////////////////////////////////////////// using System; using System.Data; using GZTW.Data; using CSLA.Data; using CSLA; namespace GZTW.AyaNova.BLL { /// /// Lightweight read only list of objects representing objects. /// Used for selection in UI and internally by other business objects. /// [Serializable] public class UnitModelPickList : ReadOnlyCollectionBase { #pragma warning disable 1591 #region Data structure /// /// Properties /// [Serializable] public struct UnitModelPickListInfo { internal bool mActive; internal string mName; internal Guid mID; public bool Active { get { return mActive; } } public string Name { get { return mName; } } public Guid ID { get { return mID; } } /// /// /// /// public bool Equals(UnitModelPickListInfo obj) { return this.ID.Equals(obj.ID); } }//end UnitModelPickListInfo #endregion #region Constructor protected UnitModelPickList() { // AllowSort=false; // AllowFind=true; // AllowEdit=false; // AllowNew=false; // AllowRemove=false; } #endregion #region Business properties and methods /// /// Get item by index /// /// public UnitModelPickListInfo this[int Item] { get { return (UnitModelPickListInfo) List[Item]; } } /// /// Returns display text that matches passed in itemid value /// /// public string this[Guid ItemID] { get { if(ItemID==Guid.Empty) return ""; foreach (UnitModelPickListInfo child in List) { if(child.mID==ItemID) return child.Name; } return "UnitModelPickList[ItemID], couldn't find: "+ItemID.ToString(); } } /// /// Returns display text that matches passed in itemid value /// /// Guid as string public UnitModelPickListInfo this[string strItemID] { get { if(strItemID=="") return new UnitModelPickListInfo(); Guid id=new Guid(strItemID); if(id==Guid.Empty) return new UnitModelPickListInfo(); foreach (UnitModelPickListInfo child in List) { if(child.mID==id) return child; } UnitModelPickListInfo u=new UnitModelPickListInfo(); u.mName="UnitModel NOT FOUND"; return u; } } /// /// 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 (UnitModelPickListInfo 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(UnitModelPickListInfo obj) { foreach (UnitModelPickListInfo child in List) { if(child.Equals(obj)) return true; } return false; } #endregion #region Static methods /// /// Get all UnitModels /// /// list of objects public static UnitModelPickList GetList() { return (UnitModelPickList) DataPortal.Fetch(new Criteria(Guid.Empty)); } /// /// Get one specific UnitModel only but with all the pick list info /// /// public static UnitModelPickList GetListOfOneSpecificUnitModel(Guid UnitModelID) { return (UnitModelPickList) DataPortal.Fetch(new Criteria(UnitModelID)); } /// /// Check all items for duplicate names /// /// List of duplicate names public static System.Collections.Generic.List DuplicateNameCheck() { return GetList().DuplicateNames; } #endregion #region DAL DATA ACCESS /// /// protected override void DataPortal_Fetch(object Criteria) { Criteria crit = (Criteria)Criteria; SafeDataReader dr = null; try { if(crit.UnitModelID==Guid.Empty) { dr=DBUtil.GetReaderFromSQLString( //************************************************************ "SELECT aUnitModel.aID, aUnitModel.AACTIVE, aUnitModel.aName, " + "aUnitModel.aModelNumber " + "FROM aUnitModel" //************************************************************ ); } else { dr=DBUtil.GetReaderFromSQLString( //************************************************************ "SELECT aUnitModel.aID, aUnitModel.AACTIVE, aUnitModel.aName, " + "aUnitModel.aModelNumber " + "FROM aUnitModel " + "WHERE aUnitModel.aID=@ID" ,crit.UnitModelID //************************************************************ ); } while(dr.Read()) { //******************************************* UnitModelPickListInfo info=new UnitModelPickListInfo(); info.mActive=dr.GetBoolean("AACTIVE"); info.mID=dr.GetGuid("aID"); //Model name is optional, number is required //format with optional name first and number second info.mName = (AyaBizUtils.SS("", dr.GetString("aName"), " ") + dr.GetString("aModelNumber")).Trim();//case 1934 added trim InnerList.Add(info); //******************************************* } } finally { if(dr!=null) dr.Close(); } } #endregion #region criteria /// /// Criteria for identifying existing object /// [Serializable] private class Criteria { public Guid UnitModelID; public Criteria( Guid _UnitModelID) { UnitModelID=_UnitModelID; } } #endregion #pragma warning restore 1591 }//end UnitModelPickList }//end namespace GZTW.AyaNova.BLL