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

434 lines
14 KiB
C#

///////////////////////////////////////////////////////////
// UserPickList.cs
// Implementation of Class UserPickList
// CSLA type: Read only collection
// Created on: 16-April-2005
// Object design: John
// Coded: 16-April-2005
///////////////////////////////////////////////////////////
using System;
using System.Data;
using GZTW.Data;
using CSLA.Data;
using CSLA;
namespace GZTW.AyaNova.BLL
{
/// <summary>
/// Lightweight read only list of <see cref="UserPickList.UserPickListInfo"/> objects representing *all* <see cref="User"/> objects.
/// Used in UI for picking users where not limited to a particular type and extensively by business objects internally.
/// </summary>
[Serializable]
public class UserPickList : ReadOnlyCollectionBase
{
#region Data structure
/// <summary>
/// Properties
/// </summary>
[Serializable]
public struct UserPickListInfo
{
internal bool mActive;
internal string mName;
internal UserTypes mType;
internal Guid mID;
internal bool mSubContractor;
internal Guid mRegionID;//case 58
/// <summary>
/// <see cref="User"/> active status
/// </summary>
public bool Active
{
get
{
return mActive;
}
}
/// <summary>
/// <see cref="User"/> Name
/// </summary>
public string Name
{
get
{
return mName;
}
}
/// <summary>
/// <see cref="User"/> <see cref="UserTypes"/>
/// </summary>
public UserTypes Type
{
get
{
return mType;
}
}
/// <summary>
/// <see cref="User"/> ID
/// </summary>
public Guid ID
{
get
{
return mID;
}
}
/// <summary>
/// True if <see cref="User"/> is a subcontractor
/// </summary>
public bool SubContractor
{
get
{
return mSubContractor;
}
}
/// <summary>
/// <see cref="User"/> <see cref="Region"/> ID
/// </summary>
public Guid RegionID { get { return mRegionID; } }//case 58
/// <summary>
///
/// </summary>
/// <param name="obj"></param>
public bool Equals(UserPickListInfo obj)
{
return this.ID.Equals(obj.ID);
}
}//end UserPickListInfo
#endregion
#region Constructor
/// <summary>
///
/// </summary>
protected UserPickList()
{
// 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 UserPickListInfo this[int Item]
{
get
{
return (UserPickListInfo) List[Item];
}
}
/// <summary>
/// Returns display text that matches passed in itemid value
/// </summary>
/// <param name="ItemID"></param>
public string this[Guid ItemID]
{
get
{
if(ItemID==Guid.Empty) return "";
foreach (UserPickListInfo child in List)
{
if(child.mID==ItemID) return child.Name;
}
return "UserPickList[ItemID], couldn't find: "+ItemID.ToString();
}
}
/// <summary>
/// Indexer to retrieve by string format of UserID
/// </summary>
/// <param name="strItemID">Guid of user ID as a string</param>
/// <returns>UserPickListInfo, if id is invalid returns emtpy userpicklistinfo with name set to "USER NOT FOUND"</returns>
public UserPickListInfo this[string strItemID]
{
get
{
if(strItemID=="") return new UserPickListInfo();
Guid id=new Guid(strItemID);
if(id==Guid.Empty) return new UserPickListInfo();
foreach (UserPickListInfo child in List)
{
if(child.mID==id) return child;
}
UserPickListInfo u=new UserPickListInfo();
u.mName="USER NOT FOUND";
return u;
}
}
#endregion
#region contains
/// <summary>
/// Check if item in collection
/// </summary>
/// <param name="obj"></param>
public bool Contains(UserPickListInfo obj)
{
foreach (UserPickListInfo child in List)
{
if(child.Equals(obj)) return true;
}
return false;
}
#endregion
#region Static methods
/// <summary>
/// Get all Users
/// </summary>
/// <returns>list of <see cref="UserPickList.UserPickListInfo"/> objects</returns>
public static UserPickList GetList(bool Regional)//case 58
{
return (UserPickList)DataPortal.Fetch(new Criteria(string.Empty, Guid.Empty, Regional, false));
}
//case 1975
/// <summary>
/// Get all Users who are internal staff
/// (excludes customer accounts, head office accounts etc)
/// </summary>
/// <returns>list of <see cref="UserPickList.UserPickListInfo"/> objects</returns>
public static UserPickList GetInternalStaffOnlyList(bool Regional)//case 58
{
return (UserPickList)DataPortal.Fetch(new Criteria(string.Empty, Guid.Empty, Regional, true));
}
/// <summary>
/// Get one specific user only but with all the pick list info
/// </summary>
/// <returns>list of <see cref="UserPickList.UserPickListInfo"/> objects</returns>
public static UserPickList GetListOfOneSpecificUser(Guid UserID)
{
return (UserPickList)DataPortal.Fetch(new Criteria(string.Empty, UserID, false, false));
}
/// <summary>
/// Get all Users filtered by search term
/// </summary>
/// <param name="searchTerm">string to match</param>
/// <param name="Regional">restrict to current users region</param>
/// <returns>list of <see cref="UserPickList.UserPickListInfo"/> objects</returns>
public static UserPickList GetList(string searchTerm, bool Regional)//case 1975
{
return (UserPickList)DataPortal.Fetch(new Criteria(searchTerm, Guid.Empty, Regional, false));
}
#endregion
#region DAL DATA ACCESS
///
/// <param name="Criteria"></param>
protected override void DataPortal_Fetch(object Criteria)
{
Criteria crit = (Criteria)Criteria;
SafeDataReader dr = null;
try
{
if(crit.UserID==Guid.Empty)
{
if (!string.IsNullOrEmpty(crit.SearchTerm))//case 1975
{
string likeTerm = "WHERE (LOWER(aUser.aFirstName) LIKE '%" + crit.SearchTerm + "%')"+
" OR (LOWER(aUser.aLastName) LIKE '%" + crit.SearchTerm + "%')" +
" OR (LOWER(aUser.aEmployeeNumber) LIKE '%" + crit.SearchTerm + "%')"
;
dr = DBUtil.GetReaderFromSQLString(
//************************************************************
"SELECT aUser.aID AS AUSERID, aUser.AACTIVE AS AUSERACTIVE, aUser.aUserType, " +
"aUser.aFirstName, aUser.aLastName, aUser.aInitials, " +
"aUser.aEmployeeNumber, aUser.aRegionID AS AUSERREGION, AREGION.ANAME AS AREGIONNAME, aClient.aName " +
"AS aCLIENTNAME, aHeadOffice.aName " +
"AS aHEADOFFICENAME, aUser.aSubContractor FROM " +
"aUser LEFT OUTER JOIN aHeadOffice ON aUser.aHeadOfficeID " +
"= aHeadOffice.aID LEFT OUTER " +
"JOIN aClient ON aUser.aClientID = aClient.aID " +
" LEFT OUTER JOIN AREGION ON (AUSER.AREGIONID=AREGION.AID) " +
likeTerm +
"ORDER BY aUser.aLastName"
//************************************************************
);
}
else
{
dr = DBUtil.GetReaderFromSQLString(
//************************************************************
"SELECT aUser.aID AS AUSERID, aUser.AACTIVE AS AUSERACTIVE, aUser.aUserType, " +
"aUser.aFirstName, aUser.aLastName, aUser.aInitials, " +
"aUser.aEmployeeNumber, aUser.aRegionID AS AUSERREGION, AREGION.ANAME AS AREGIONNAME, aClient.aName " +
"AS aCLIENTNAME, aHeadOffice.aName " +
"AS aHEADOFFICENAME, aUser.aSubContractor FROM " +
"aUser LEFT OUTER JOIN aHeadOffice ON aUser.aHeadOfficeID " +
"= aHeadOffice.aID LEFT OUTER " +
"JOIN aClient ON aUser.aClientID = aClient.aID " +
" LEFT OUTER JOIN AREGION ON (AUSER.AREGIONID=AREGION.AID) " +
"ORDER BY aUser.aLastName"
//************************************************************
);
}
}
else
{
dr=DBUtil.GetReaderFromSQLString(
//************************************************************
"SELECT aUser.aID AS AUSERID, aUser.AACTIVE AS AUSERACTIVE, aUser.aUserType, " +
"aUser.aFirstName, aUser.aLastName, aUser.aInitials, " +
"aUser.aEmployeeNumber, aUser.aRegionID AS AUSERREGION, AREGION.ANAME AS AREGIONNAME, aClient.aName " +
"AS aCLIENTNAME, aHeadOffice.aName " +
"AS aHEADOFFICENAME, aUser.aSubContractor FROM " +
"aUser LEFT OUTER JOIN aHeadOffice ON aUser.aHeadOfficeID " +
"= aHeadOffice.aID LEFT OUTER " +
"JOIN aClient ON aUser.aClientID = aClient.aID " +
" LEFT OUTER JOIN AREGION ON (AUSER.AREGIONID=AREGION.AID) " +
"WHERE aUser.aID=@ID"
,crit.UserID
//************************************************************
);
}
long lLicensedUserCount=0;
//case 1300
bool bCurrentUserIsInDefaultRegion = User.CurrentUserIsInDefaultRegion;
Guid myRegion = User.CurrentUserRegionID;
while(dr.Read())
{
//*******************************************
if((UserTypes)dr.GetInt16("aUserType")==UserTypes.Utility) continue;
UserPickListInfo info=new UserPickListInfo();
info.mActive = dr.GetBoolean("AUSERACTIVE");
info.mID = dr.GetGuid("AUSERID");
info.mName=User.NameFormatter(dr.GetString("aFirstName"),dr.GetString("aLastName"),dr.GetString("aInitials"),
dr.GetString("aEmployeeNumber"), dr.GetString("AREGIONNAME"), AyaBizUtils.GlobalSettings.DefaultScheduleableUserNameDisplayFormat);
info.mSubContractor=dr.GetBoolean("aSubContractor");
info.mType=(UserTypes)dr.GetInt16("aUserType");
info.mRegionID = dr.GetGuid("AUSERREGION");//case 58
switch(info.mType)
{
case UserTypes.Client:
info.mName=info.mName+" - " + dr.GetString("aCLIENTNAME");
break;
case UserTypes.HeadOffice:
info.mName=info.mName+" - " + dr.GetString("aHEADOFFICENAME");
break;
}
if(info.mActive && info.mType==UserTypes.Schedulable)
lLicensedUserCount++;
////case 58
//if (crit.Regional)
//{
// //theory is that users outside current region should just be inactive so they don't show in
// //workorders unless preselected but will then be grayed out.
// if (!AyaBizUtils.InYourRegion(info.mRegionID))
// info.mActive = false;
//}
//case 1300 same effect as above but without triggering db calls to get user region
if (crit.Regional)
if (!bCurrentUserIsInDefaultRegion)
if (info.mRegionID != Region.DefaultRegionID && info.mRegionID != myRegion)
info.mActive = false;
if (crit.InternalOnly)
{
if(info.Type!= UserTypes.Client && info.Type!= UserTypes.HeadOffice && info.Type!= UserTypes.Utility)
InnerList.Add(info);
}
else
{
InnerList.Add(info);
}
//*******************************************
}
if(!AyaBizUtils.Lite && !User.IsAdmin && lLicensedUserCount > AyaBizUtils.GlobalX.ScheduleableUsers )//case 1172
{
throw new System.Security.SecurityException(
"\r\nLICENSE VIOLATION\r\n\r\n" +
"AyaNova has detected an attempt to circumvent AyaNova licensing.\r\n\r\n" +
"This database is licensed for " + AyaBizUtils.GlobalX.ScheduleableUsers.ToString() + " scheduleable users\r\n" +
"Currently there are " + lLicensedUserCount.ToString() + " scheduleable users active.\r\n\r\n" +
"All AyaNova accounts other than Manager have been suspended until\r\n" +
"the extra scheduleable users that are not licensed are\r\n" +
"set inactive, removed or changed to non-scheduleable users\r\n"+
"to bring this database back into license compliance.\r\n\r\n"+
"Directly editing the AyaNova database can lead to lost or damaged data\r\n"+
"Please ensure no one tampers with the AyaNova database in future.\r\n\r\n" +
"** License violations are logged for the protection of the licensee and licensor **");
}
}
finally
{
if(dr!=null) dr.Close();
}
}
#endregion
#region criteria
/// <summary>
/// Criteria for identifying existing object
/// </summary>
[Serializable]
private class Criteria
{
public string SearchTerm;//case 1975
public Guid UserID;
public bool Regional;//case 58
public bool InternalOnly;//case 1975
public Criteria(string _searchTerm, Guid _UserID, bool _Regional, bool _InternalOnly)
{
UserID=_UserID;
Regional = _Regional;
SearchTerm = _searchTerm;
InternalOnly = _InternalOnly;
}
}
#endregion
}//end UserPickList
}//end namespace GZTW.AyaNova.BLL