/////////////////////////////////////////////////////////// // Name.cs // Implementation of Class NameFetcher // CSLA type: Read-only object // Created on: 07-Jun-2004 8:41:37 AM // Object design: John // Coded: John Aug 4 2004 /////////////////////////////////////////////////////////// using System; using System.Data; using CSLA.Data; using GZTW.Data; using CSLA; using System.Threading; using CSLA.Security; namespace GZTW.AyaNova.BLL { /// /// Used to quickly fetch a single name record from the db /// /// /// This example fetches a client name from the database /// /// //Note the table and column names automatically get the letter A prepended to them if not specified /// string sClientName = NameFetcher.GetItem("CLIENT", "NAME", MyClientID).RecordName; /// /// [Serializable] public class NameFetcher : ReadOnlyBase { #region Attributes private string mName; #endregion #region Constructor /// /// Private constructor to prevent direct instantiation /// private NameFetcher() { } #endregion #region Business properties /// /// The name /// public string RecordName { get { return mName; } } #endregion #region System.Object overrides /// /// /// /// public override string ToString() { //Case 671 (partially) if (string.IsNullOrEmpty(mName)) return "?"; return mName.ToString(); } /// /// /// /// /// public override bool Equals(Object obj) { if ( obj == null || GetType ( ) != obj.GetType ( ) ) return false; NameFetcher c=(NameFetcher)obj; return mName==c.mName; } /// /// /// /// public override int GetHashCode() { return ("Name" + mName).GetHashCode(); } #endregion #region Static methods /// /// Retrieve name based on db schema info and record id. /// API users: it's recommended to use the TypeAndID version override instead. /// /// Database table name /// Text field column name in Table /// Guid value of "ID" field in table /// NameFetcher public static NameFetcher GetItem(string Table, string FieldName, Guid RecordID) { return (NameFetcher)DataPortal.Fetch(new Criteria( Table, FieldName, RecordID, null, false)); } /// /// Retrieve name based on type and ID. This is a preferred method for API users as it requires no knowledge of the /// database schema. /// /// TypeAndID object containing the rootobjecttype and the Guid value /// NameFetcher public static NameFetcher GetItem(TypeAndID TID) { return (NameFetcher)DataPortal.Fetch(new Criteria("", "", Guid.Empty, TID, false)); } /// /// Retrieve name based on type and ID. This is a preferred method for API users as it requires no knowledge of the /// database schema. /// /// TypeAndID object containing the rootobjecttype and the Guid value /// If true, returns name of object only and doesn't prepend with the type /// NameFetcher public static NameFetcher GetItem(TypeAndID TID, bool oNameOnly) { return (NameFetcher)DataPortal.Fetch(new Criteria("", "", Guid.Empty, TID, oNameOnly)); } /// /// Retrieve name based on type and ID. This is a preferred method for API users as it requires no knowledge of the /// database schema. /// /// RootObjectType /// ID of object /// NameFetcher public static NameFetcher GetItem(RootObjectTypes oType, Guid oId) {//added for case 1975 TypeAndID tid = new TypeAndID(oType, oId); return (NameFetcher)DataPortal.Fetch(new Criteria("", "", Guid.Empty, tid, false)); } /// /// Retrieve name based on type and ID. This is a preferred method for API users as it requires no knowledge of the /// database schema. /// /// RootObjectType /// ID of object /// If true, returns name of object only and doesn't prepend with the type /// NameFetcher public static NameFetcher GetItem(RootObjectTypes oType, Guid oId, bool oNameOnly) {//added for case 1975 TypeAndID tid = new TypeAndID(oType, oId); return (NameFetcher)DataPortal.Fetch(new Criteria("", "", Guid.Empty, tid, oNameOnly)); } /// /// Simplest method to directly get only the name of the object /// (does not add object type name) as a string and return /// /// /// RootObjectType /// ID of object /// Name directly as a string public static string GetName(RootObjectTypes oType, Guid oId) { return GetItem(oType, oId, true).RecordName; } #endregion #region DAL DATA ACCESS /// /// protected override void DataPortal_Fetch(object Criteria) { Criteria crit = (Criteria)Criteria; if (crit.TID == null) { try { mName = (string)DBUtil.GetScalarFromSQLString("SELECT " + AyaBizUtils.ToDBName(crit.FieldName) + " FROM " + AyaBizUtils.ToDBName(crit.Table) + " WHERE aID=@ID", crit.RecordID); } catch (Exception ex) { throw new System.ArgumentException( string.Format(LocalizedTextTable.GetLocalizedTextDirect("Error.Object.NameFetcherNotFound")/*Name/bool Fetcher: Field {0} in table {1} with record ID {2} not found!*/, crit.FieldName, crit.Table, crit.RecordID.ToString()) + "\r\n(" + ex.Message + ")"); } } else {//modified this block for case 1975 if (!crit.NameOnly || crit.TID.RootObjectType == RootObjectTypes.Nothing || crit.TID.RootObjectType == RootObjectTypes.Global) { mName = EnumDescConverter.GetEnumDescription(crit.TID.RootObjectType); if (crit.TID.RootObjectType != RootObjectTypes.Nothing && crit.TID.RootObjectType != RootObjectTypes.Global) { mName = mName + ": "; } } switch (crit.TID.RootObjectType) {//modified this block for case 1975 case RootObjectTypes.WorkorderQuote: { WorkorderQuoteDescriptionFetcher wdf = WorkorderQuoteDescriptionFetcher.GetItem(crit.TID.ID); mName = mName + wdf.WorkorderNumber + " - " + wdf.ClientName; } break; case RootObjectTypes.Workorder: { WorkorderDescriptionFetcher wdf = WorkorderDescriptionFetcher.GetItem(crit.TID.ID); mName = mName + wdf.WorkorderNumber + " - " + wdf.ClientName; } break; //case 1379 case RootObjectTypes.WorkorderService: { WorkorderDescriptionFetcher wdf = WorkorderDescriptionFetcher.GetItem(crit.TID.ID); mName = mName + wdf.WorkorderNumber + " - " + wdf.ClientName; } break; case RootObjectTypes.WorkorderPreventiveMaintenance: { WorkorderPMDescriptionFetcher wdf = WorkorderPMDescriptionFetcher.GetItem(crit.TID.ID); mName = mName + wdf.WorkorderNumber + " - " + wdf.ClientName; } break; case RootObjectTypes.WorkorderQuoteTemplate: { WorkorderQuoteDescriptionFetcher wdf = WorkorderQuoteDescriptionFetcher.GetItem(crit.TID.ID); mName = mName + wdf.WorkorderNumber + " - " + wdf.TemplateDescription; } break; case RootObjectTypes.WorkorderServiceTemplate: { WorkorderDescriptionFetcher wdf = WorkorderDescriptionFetcher.GetItem(crit.TID.ID); mName = mName + wdf.WorkorderNumber + " - " + wdf.TemplateDescription; } break; case RootObjectTypes.WorkorderPreventiveMaintenanceTemplate: { WorkorderPMDescriptionFetcher wdf = WorkorderPMDescriptionFetcher.GetItem(crit.TID.ID); mName = mName + wdf.WorkorderNumber + " - " + wdf.TemplateDescription; } break; case RootObjectTypes.Unit: { mName = mName + UnitNameFetcher.GetUnitNameFromUnitID(crit.TID.ID); } break; case RootObjectTypes.ClientServiceRequest: { mName = mName + ClientServiceRequestNameFetcher.Description(crit.TID.ID); } break; case RootObjectTypes.Global://case 73 //Don't fetch, nothing to fetch, just return with name; break; case RootObjectTypes.Nothing://case 73 (actually just for testing) //Don't fetch, nothing to fetch, just return with name; break; case RootObjectTypes.WikiPage://case 73 { mName = mName + NameFetcher.GetItem("a" + crit.TID.RootObjectType.ToString(), "aTitle", crit.TID.ID); } break; case RootObjectTypes.PurchaseOrder://case 480 { PurchaseOrderList po=PurchaseOrderList.GetListForSingleItem(crit.TID.ID); mName = mName + po[0].LT_PurchaseOrder_Label_PONumber.Display; } break; case RootObjectTypes.User://case 73 { mName = mName + UserPickList.GetListOfOneSpecificUser(crit.TID.ID)[0].Name; } break; case RootObjectTypes.AyaFile://case 1453 { mName = mName + NameFetcher.GetItem("FILE", "NAME", crit.TID.ID); } break; case RootObjectTypes.PartSerial://case 1975 { mName = mName + NameFetcher.GetItem("PARTSERIAL", "SERIALNUMBER", crit.TID.ID); } break; default: { //Use regular name fetcher if(crit.TID.ID==Guid.Empty)//case 1452 mName = mName + Guid.Empty.ToString(); else mName = mName + NameFetcher.GetItem("a"+crit.TID.RootObjectType.ToString(), "aName", crit.TID.ID); } break; } } } #endregion #region criteria /// /// Criteria for identifying existing object /// [Serializable] private class Criteria { public string Table; public string FieldName; public Guid RecordID; public TypeAndID TID; public bool NameOnly; public Criteria(string _Table, string _FieldName, Guid _RecordID, TypeAndID _TID, bool _NameOnly) { if (_Table == "User") _Table = "aUser"; Table = _Table; FieldName = _FieldName; RecordID = _RecordID; TID = _TID; NameOnly = _NameOnly; } } #endregion }//end Name }//end namespace GZTW.AyaNova.BLL