Files
2018-06-29 19:47:36 +00:00

363 lines
14 KiB
C#

///////////////////////////////////////////////////////////
// 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 {
/// <summary>
/// Used to quickly fetch a single name record from the db
/// </summary>
/// <example>
/// This example fetches a client name from the database
/// <code>
/// //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;
/// </code>
/// </example>
[Serializable]
public class NameFetcher : ReadOnlyBase
{
#region Attributes
private string mName;
#endregion
#region Constructor
/// <summary>
/// Private constructor to prevent direct instantiation
/// </summary>
private NameFetcher()
{
}
#endregion
#region Business properties
/// <summary>
/// The name
/// </summary>
public string RecordName
{
get
{
return mName;
}
}
#endregion
#region System.Object overrides
/// <summary>
///
/// </summary>
/// <returns></returns>
public override string ToString()
{
//Case 671 (partially)
if (string.IsNullOrEmpty(mName)) return "?";
return mName.ToString();
}
/// <summary>
///
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public override bool Equals(Object obj)
{
if ( obj == null || GetType ( ) != obj.GetType ( ) ) return false;
NameFetcher c=(NameFetcher)obj;
return mName==c.mName;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public override int GetHashCode()
{
return ("Name" + mName).GetHashCode();
}
#endregion
#region Static methods
/// <summary>
/// Retrieve name based on db schema info and record id.
/// API users: it's recommended to use the TypeAndID version override instead.
/// </summary>
/// <param name="Table">Database table name</param>
/// <param name="FieldName">Text field column name in Table</param>
/// <param name="RecordID">Guid value of "ID" field in table</param>
/// <returns>NameFetcher</returns>
public static NameFetcher GetItem(string Table, string FieldName, Guid RecordID)
{
return (NameFetcher)DataPortal.Fetch(new Criteria( Table, FieldName, RecordID, null, false));
}
/// <summary>
/// Retrieve name based on type and ID. This is a preferred method for API users as it requires no knowledge of the
/// database schema.
/// </summary>
/// <param name="TID">TypeAndID object containing the rootobjecttype and the Guid value</param>
/// <returns>NameFetcher</returns>
public static NameFetcher GetItem(TypeAndID TID)
{
return (NameFetcher)DataPortal.Fetch(new Criteria("", "", Guid.Empty, TID, false));
}
/// <summary>
/// Retrieve name based on type and ID. This is a preferred method for API users as it requires no knowledge of the
/// database schema.
/// </summary>
/// <param name="TID">TypeAndID object containing the rootobjecttype and the Guid value</param>
/// <param name="oNameOnly">If true, returns name of object only and doesn't prepend with the type</param>
/// <returns>NameFetcher</returns>
public static NameFetcher GetItem(TypeAndID TID, bool oNameOnly)
{
return (NameFetcher)DataPortal.Fetch(new Criteria("", "", Guid.Empty, TID, oNameOnly));
}
/// <summary>
/// Retrieve name based on type and ID. This is a preferred method for API users as it requires no knowledge of the
/// database schema.
/// </summary>
/// <param name="oType">RootObjectType</param>
/// <param name="oId">ID of object</param>
/// <returns>NameFetcher</returns>
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));
}
/// <summary>
/// Retrieve name based on type and ID. This is a preferred method for API users as it requires no knowledge of the
/// database schema.
/// </summary>
/// <param name="oType">RootObjectType</param>
/// <param name="oId">ID of object</param>
/// <param name="oNameOnly">If true, returns name of object only and doesn't prepend with the type</param>
/// <returns>NameFetcher</returns>
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));
}
/// <summary>
/// Simplest method to directly get only the name of the object
/// (does not add object type name) as a string and return
///
/// </summary>
/// <param name="oType">RootObjectType</param>
/// <param name="oId">ID of object</param>
/// <returns>Name directly as a string</returns>
public static string GetName(RootObjectTypes oType, Guid oId)
{
return GetItem(oType, oId, true).RecordName;
}
#endregion
#region DAL DATA ACCESS
///
/// <param name="Criteria"></param>
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
/// <summary>
/// Criteria for identifying existing object
/// </summary>
[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