Files
ayanova7/source/bizobjects/AyaLib/GZTW.AyaNova.BLL/ScheduleMarkerList.cs
2018-06-29 19:47:36 +00:00

537 lines
18 KiB
C#

///////////////////////////////////////////////////////////
// ScheduleMarkerList.cs
// Implementation of Class ScheduleMarkerList for case 1967
// CSLA type: Read only collection
// Created on: 13-Feb-2017
// Object design: John
// Coded: 13-Feb-2017
///////////////////////////////////////////////////////////
using System;
using System.Data;
using GZTW.Data;
using CSLA.Data;
using CSLA;
using System.Collections.Generic;
namespace GZTW.AyaNova.BLL
{
#pragma warning disable 1591
/// <summary>
/// Lightweight list of <see cref="ScheduleMarkerList.ScheduleMarkerListInfo"/> objects representing <see cref="ScheduleMarker"/> objects
///
/// </summary>
[Serializable]
public class ScheduleMarkerList : ReadOnlyCollectionBase
{
#region Data structure
/// <summary>
/// Properties
/// </summary>
[Serializable]
public struct ScheduleMarkerListInfo
{
internal GridNameValueCellItem mScheduleMarker;
internal string mMarkerFor;
internal string mFollowObject;
internal int mARGB;
internal SmartDate mStartDateTime;
internal SmartDate mEndDateTime;
internal bool mCompleted;
[SqlColumnNameAttribute("ASCHEDULEMARKER.ANAME", "ASCHEDULEMARKER.AID"),
Display(DisplayType.Button, RootObjectType = RootObjectTypes.ScheduleMarker)]
public GridNameValueCellItem LT_O_ScheduleMarker
{
get
{
return mScheduleMarker;
}
}
[SqlColumnNameAttribute("grid"), Display(DisplayType.Text)]
public string LT_ScheduleMarker_Label_ScheduleMarkerSourceType
{
get
{
return mMarkerFor;
}
}
[SqlColumnNameAttribute("grid"), Display(DisplayType.Text)]
public string LT_ScheduleMarker_Label_FollowUp
{
get
{
return mFollowObject;
}
}
//see workorder pm custom list case 40 for how to deal with this
//on a coloured button might be useful for this list
[Display(DisplayType.Hidden)]
public int ARGB { get { return mARGB; } }
[SqlColumnNameAttribute("ASCHEDULEMARKER.ASTARTDATE"), Display(DisplayType.DateTime)]
public object LT_ScheduleMarker_Label_StartDate
{
get
{
return mStartDateTime.DBValue;
}
}
[SqlColumnNameAttribute("ASCHEDULEMARKER.ASTOPDATE"), Display(DisplayType.DateTime)]
public object LT_ScheduleMarker_Label_StopDate
{
get
{
return mEndDateTime.DBValue;
}
}
[SqlColumnNameAttribute("ASCHEDULEMARKER.ACOMPLETED"),Display(DisplayType.TrueFalse)]
public bool LT_ScheduleMarker_Label_Completed
{
get
{
return mCompleted;
}
}
/// <summary>
///
/// </summary>
/// <param name="obj"></param>
public bool Equals(ScheduleMarkerListInfo obj)
{
return this.mScheduleMarker.Value.Equals(obj.mScheduleMarker.Value);
}
}//end ScheduleMarkerListInfo
#endregion
#region Constructor
protected ScheduleMarkerList()
{
// AllowSort=false;
// AllowFind=true;
// AllowEdit=false;
// AllowNew=false;
// AllowRemove=false;
}
#endregion
#region Business properties and methods
/// <summary>
/// Get item by index
/// </summary>
/// <param name="Item"></param>
public ScheduleMarkerListInfo this[int Item]
{
get
{
return (ScheduleMarkerListInfo) List[Item];
}
}
/// <summary>
/// Returns display text that matches passed in itemid value
/// </summary>
/// <param name="ItemID"></param>
public string this[Guid ItemID]
{
get
{
foreach (ScheduleMarkerListInfo child in List)
{
if (child.mScheduleMarker.Value == ItemID) return child.ToString();
}
return "Missing: "+ItemID.ToString();
}
}
#endregion
#region contains
/// <summary>
/// Check if item in collection
/// </summary>
/// <param name="obj"></param>
public bool Contains(ScheduleMarkerListInfo obj)
{
foreach (ScheduleMarkerListInfo child in List)
{
if(child.Equals(obj)) return true;
}
return false;
}
#endregion
#region Reporting and shared UI editor helpers
/// <summary>
/// Returns the report key which is a property of
/// reports used to link all reports that can be used
/// with a particular data source.
/// </summary>
public static string ReportKey
{
get
{
return "ScheduleMarkerList";
}
}
/// <summary>
/// Returns the Detailed report key
/// which is used to determine which reports and objects
/// will be used for detailed reports
///
/// If empty string then indicates there is no detailed report object or reports
/// </summary>
public static string DetailedReportKey
{
get
{
return "";
}
}
/// <summary>
/// Base object that this list is reporting on
/// used by shared UI editor to instantiate new objects
/// when user selects new in UI elements that display this list
///
/// (I.E. when user clicks on new in a read only list grid, this is the object type created)
/// </summary>
public static RootObjectTypes BaseObjectType
{
get
{
return RootObjectTypes.ScheduleMarker;
}
}
/// <summary>
/// Locale key so that generic list editor
/// UI code knows what title to give the list in a
/// grid
/// </summary>
public string LocaleKey
{
get
{
return "ScheduleMarker.Label.List";
}
}
/// <summary>
/// The Type of the struct used to store list records
/// Used to fetch the custom display attributes of the fields
/// contained within the record to modify the grid display accordingly
/// <see cref="DisplayAttribute"/>
/// </summary>
public static Type ListRecordType
{
get
{
return typeof(ScheduleMarkerListInfo);
}
}
/// <summary>
/// Field that contains the ID of the objects
/// that are the basis of this list.
///
/// Used for compiling an ID list for reporting from user
/// selections in a grid.
/// </summary>
public static string IDField
{
get
{
return "LT_O_ScheduleMarker";
}
}
/// <summary>
/// Same as IDField but for detailed reports
/// </summary>
public static string IDFieldDetailed
{
get
{
return IDField;
}
}
#endregion
#region Static methods
/// <summary>
/// Internal method used by list factory
/// Fetches all items for current user if they are scheduleable
/// </summary>
internal static ScheduleMarkerList Get(string Filter, int MaxRecords, List<Guid> IDList)
{
//instead of throwing exception, return an empty list if they are not a scheduleable user
if (User.CurrentUserType != UserTypes.Schedulable)
return new ScheduleMarkerList();
else
return (ScheduleMarkerList)DataPortal.Fetch(new Criteria(Filter, IDList, MaxRecords));
}
/// <summary>
/// Get all ScheduleMarker (filtered by crit)
/// gets all items for current user if they are scheduleable
/// </summary>
/// <param name="xmlCriteria">Use AyaNova UI to easily build xmlCriteria and Ctrl-Alt-g keyboard command to display it for use in your code</param>
/// <returns>list of <see cref="ScheduleMarkerList.ScheduleMarkerListInfo"/> objects</returns>
public static ScheduleMarkerList GetList(string xmlCriteria)
{
//instead of throwing exception, return an empty list if they are not a scheduleable user
if (User.CurrentUserType != UserTypes.Schedulable)
return new ScheduleMarkerList();
else
return (ScheduleMarkerList)DataPortal.Fetch(new Criteria(xmlCriteria, null, -1));
}
/// <summary>
/// Get list by items indicated in IDList
/// </summary>
/// <param name="IDList">Generic list of Guid's</param>
/// <returns>list of <see cref="ScheduleMarkerList.ScheduleMarkerListInfo"/> objects</returns>
public static ScheduleMarkerList GetListFromIDList(List<Guid> IDList)
{
//case 556
//Handle empty list
if (IDList.Count == 0)
return new ScheduleMarkerList();
//instead of throwing exception, return an empty list if they are not a scheduleable user
if (User.CurrentUserType != UserTypes.Schedulable)
return new ScheduleMarkerList();
else
return (ScheduleMarkerList)DataPortal.Fetch(new Criteria("", IDList, -1));
}
/// <summary>
/// Return an empty list
/// used for initializing grid
/// </summary>
/// <returns></returns>
public static ScheduleMarkerList GetEmptyList()
{
return new ScheduleMarkerList();
}
#endregion
#region DAL DATA ACCESS
///
/// <param name="Criteria"></param>
protected override void DataPortal_Fetch(object Criteria)
{
Criteria crit = (Criteria)Criteria;
SafeDataReader dr = null;
Guid activeUserId = User.CurrentThreadUserID;
//cached data
UserListScheduleable uls = UserListScheduleable.GetList();
try
{
string q = "";
if (crit.IDList != null)
{
//Case 556
System.Text.StringBuilder sbIN = new System.Text.StringBuilder();
sbIN.Append(" WHERE aScheduleMarker.aID in (");
foreach (Guid gItem in crit.IDList)
{
sbIN.Append("'");
sbIN.Append("{");
sbIN.Append(gItem.ToString().ToUpperInvariant());
sbIN.Append("}");
sbIN.Append("',");
}
sbIN.Length = sbIN.Length - 1;
sbIN.Append(") ");
q = "SELECT * FROM aScheduleMarker " +
sbIN.ToString() +
" " +
AyaBizUtils.GetGridSortOrderColumns(crit.CriteriaXML);
}
else
{
//************************************************************
q = "SELECT ~MAXRECS~ ASCHEDULEMARKER.* FROM ASCHEDULEMARKER "+
AyaBizUtils.GetGridColumnCriteria(crit.CriteriaXML, true) + " " +
AyaBizUtils.GetGridSortOrderColumns(crit.CriteriaXML);
if (crit.MaxRecords > 0)
q = q.Replace("~MAXRECS~", "TOP " + crit.MaxRecords.ToString());
else
q = q.Replace("~MAXRECS~", "");
//************************************************************
}
//-----------------------------------------------------------
dr=DBUtil.GetReaderFromSQLString(q);
while(dr.Read())
{
ScheduleMarkerSourceTypes smt = (ScheduleMarkerSourceTypes)dr.GetInt16("ASCHEDULEMARKERSOURCETYPE");
Guid AppliesToObjectID = dr.GetGuid("ASOURCEID");
if (relevantScheduleMarker(activeUserId, smt, AppliesToObjectID, uls))
{
//*******************************************
ScheduleMarkerListInfo info = new ScheduleMarkerListInfo();
info.mScheduleMarker = new GridNameValueCellItem(
dr.GetGuid("AID"),
dr.GetString("ANAME"),
RootObjectTypes.ScheduleMarker);
//marker for
switch (smt)
{
case ScheduleMarkerSourceTypes.User:
info.mMarkerFor = uls[AppliesToObjectID].FullName;
break;
case ScheduleMarkerSourceTypes.Regional:
info.mMarkerFor = NameFetcher.GetItem(RootObjectTypes.Region, AppliesToObjectID).RecordName;
break;
case ScheduleMarkerSourceTypes.Global:
info.mMarkerFor = NameFetcher.GetItem(RootObjectTypes.Global, Guid.Empty).RecordName;
break;
case ScheduleMarkerSourceTypes.DispatchZone:
info.mMarkerFor = NameFetcher.GetItem(RootObjectTypes.DispatchZone, AppliesToObjectID).RecordName;
break;
case ScheduleMarkerSourceTypes.ScheduleableUserGroup:
info.mMarkerFor = NameFetcher.GetItem(RootObjectTypes.ScheduleableUserGroup, AppliesToObjectID).RecordName;
break;
}
RootObjectTypes followType = (RootObjectTypes)dr.GetInt16("AFOLLOWTYPE");
Guid followId = dr.GetGuid("AFOLLOWID");
info.mFollowObject = "";
if (followId != Guid.Empty && followType != RootObjectTypes.Nothing)
{
info.mFollowObject = NameFetcher.GetItem(followType, followId).RecordName;
}
info.mARGB = dr.GetInt32("AARGB");
info.mStartDateTime = DBUtil.ToLocal(dr.GetSmartDate("ASTARTDATE"));
info.mEndDateTime = DBUtil.ToLocal(dr.GetSmartDate("ASTOPDATE"));
info.mCompleted = dr.GetBoolean("ACOMPLETED");
InnerList.Add(info);
}
//*******************************************
}
}
finally
{
if(dr!=null) dr.Close();
}
}
#endregion
private static bool relevantScheduleMarker(Guid activeUserId, ScheduleMarkerSourceTypes appliesToObjectType, Guid appliesToObjectId, UserListScheduleable uls)
{
//Don't process for unassigned user
if (activeUserId == Guid.Empty) return false;
//Could be a bunch by region , global , dispatchzone, schedusergroup
//or could be a single by one user ID
switch (appliesToObjectType)
{
case ScheduleMarkerSourceTypes.User:
{
if (appliesToObjectId == activeUserId)
return true;
}
break;
case ScheduleMarkerSourceTypes.Regional:
{
if (uls[activeUserId].RegionID == appliesToObjectId)
return true;
}
break;
case ScheduleMarkerSourceTypes.DispatchZone:
{
if (uls[activeUserId].DispatchZoneID == appliesToObjectId)
return true;
}
break;
case ScheduleMarkerSourceTypes.ScheduleableUserGroup:
{
ScheduleableUserGroupUsersList ScheduleMarkerGroup = ScheduleableUserGroupUsersList.GetList(appliesToObjectId);
if (ScheduleMarkerGroup.Contains(activeUserId))//Case 835
return true;
}
break;
case ScheduleMarkerSourceTypes.Global:
{
return true;
}
}
return false;
}
#region criteria
/// <summary>
/// Criteria for identifying existing object
/// </summary>
[Serializable]
private class Criteria
{
public List<Guid> IDList;
public string CriteriaXML;
public int MaxRecords;
public Criteria(string _CriteriaXML, List<Guid> _IDList, int _MaxRecords)
{
CriteriaXML = _CriteriaXML;
IDList = _IDList;
MaxRecords = _MaxRecords;
}
}
#endregion
}//end ScheduleMarkerList
#pragma warning restore 1591
}//end namespace GZTW.AyaNova.BLL