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

349 lines
9.4 KiB
C#

///////////////////////////////////////////////////////////
// UserMRUs.cs
// Implementation of Class UserMRUs
// CSLA type: Editable root collection
// Created on: 31-Oct-2007
// Object design: John
// Coded: John 31-Oct-2007
///////////////////////////////////////////////////////////
using System;
using System.Data;
using CSLA.Data;
using GZTW.Data;
using CSLA;
namespace GZTW.AyaNova.BLL
{
/// <summary>
/// Editable root collection of <see cref="UserMRU"/> objects
/// </summary>
[Serializable]
public class UserMRUs : BusinessCollectionBase
{
//case 768
/// <summary>
///
/// </summary>
public bool InPortal = false;
#region Constructor
//Private constructor prevents direction instantiation
private UserMRUs()
{ InPortal = AyaBizUtils.AtDataPortalServer; }//case 768
#endregion
#region Business properties and methods
/// <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 "UserMRU.Label.List";
}
}
/// <summary>
/// Retrieve UserMRU by index
/// </summary>
/// <param name="Item">Index</param>
public UserMRU this[int Item]
{
get
{
return (UserMRU)List[Item];
}
}
/// <summary>
/// MRU list in a <see cref="DataView"/> object which is displayed in AyaNova UI
/// </summary>
public DataView MRU
{
get
{
DataTable dt = new DataTable("MRU");
dt.Columns.Add("TID", typeof(TypeAndID));
dt.Columns.Add("Description", typeof(string));
dt.Columns.Add("Created", typeof(DateTime));
dt.DefaultView.Sort = "Created desc";
foreach (UserMRU m in List)
{
DataRow dr = dt.NewRow();
dr["TID"] = new TypeAndID(m.ObjectType, m.ObjectID);
dr["Description"] = m.Description;
dr["Created"] = m.Created;
dt.Rows.Add(dr);
}
return dt.DefaultView;
}
}
/// <summary>
/// Add a new UserMRU to the collection
/// if it isn't already in the collection
/// otherwise just set the existing mru's
/// created date to now and return it.
///
/// (this is not intended for direct usage normally
/// opening any of the main business objects that are supported
/// will automatically create an MRU entry)
/// </summary>
public UserMRU Add(RootObjectTypes ObjectType, Guid ObjectID)
{
//TODO if dataportal need to call update on change here same
//for remove below re case 768
foreach (UserMRU m in List)
{
if (m.ObjectID == ObjectID)
{
m.Created = DBUtil.CurrentWorkingDateTime;
//case 768
if (InPortal)
this.Save();
return m;
}
}
UserMRU child = UserMRU.NewItem(ObjectType,ObjectID);
List.Add(child);
//case 768
if (InPortal)
this.Save();
return child;
}
/// <summary>
/// Remove UserMRU if present
/// (not for direct api use)
/// </summary>
public void Remove(RootObjectTypes ObjectType, Guid ObjectID)
{
foreach (UserMRU m in List)
{
if (m.ObjectID == ObjectID)
{
List.Remove(m);
//case 768
if (InPortal)
this.Save();
return;
}
}
}
/// <summary>
/// Remove UserMRU if present
/// (not intended or required for direct api use)
/// </summary>
public void Remove(TypeAndID TID)
{
foreach (UserMRU m in List)
{
if (m.ObjectID == TID.ID)
{
List.Remove(m);
//case 768
if (InPortal)
this.Save();
return;
}
}
}
#endregion
#region Contains
/// <summary>
/// Check if item in collection
/// </summary>
/// <param name="obj"></param>
public bool Contains(UserMRU obj)
{
foreach (UserMRU child in List)
{
if (child.Equals(obj)) return true;
}
return false;
}
///// <summary>
///// Check if item in collection by form name
///// </summary>
//
//public bool Contains(string FormName)
//{
// foreach (UserMRU child in List)
// {
// if (child.FormName == FormName) return true;
// }
// return false;
//}
/// <summary>
/// Check if item in deleted collection
/// </summary>
/// <param name="obj"></param>
public bool ContainsDeleted(UserMRU obj)
{
foreach (UserMRU child in deletedList)
{
if (child.Equals(obj)) return true;
}
return false;
}
#endregion
#region Static methods
/// <summary>
/// NewItems
/// </summary>
/// <returns></returns>
public static UserMRUs NewItems()
{
return new UserMRUs();
}
/// <summary>
/// Get item collection (Root collection style)
/// </summary>
/// <param name="UserID"></param>
/// <returns></returns>
public static UserMRUs GetItems(Guid UserID)
{
UserMRUs col = new UserMRUs();
return (UserMRUs)DataPortal.Fetch(new Criteria(UserID));
}
#endregion
#region DAL DATA ACCESS
/// <summary>
/// Fetch children root style
/// </summary>
/// <param name="Criteria"></param>
protected override void DataPortal_Fetch(object Criteria)
{
Criteria crit = (Criteria)Criteria;
SafeDataReader dr = null;
try
{
dr = DBUtil.GetReaderFromSQLString("SELECT * FROM aUserMRU WHERE aUserID=@ID", crit.UserID);
while (dr.Read())
{
List.Add(UserMRU.GetItem(dr));
}
}
finally
{
if (dr != null) dr.Close();
}
}
/// <summary>
/// Editable Root Collection Update
/// </summary>
protected override void DataPortal_Update()
{
//Remove all but the top 10
//case 768
//keep track of inportal value and temporarily set it to false
//to prevent the remove statement from triggering a save
bool wasinportal = InPortal;
InPortal = false;
DataView dv = this.MRU;
int x = 0;
foreach (DataRowView r in dv)
{
x++;
if (x > 10)
{
Remove((TypeAndID)r["TID"]);
}
}
//case 768
InPortal = wasinportal;
using (IDbConnection connection = DBUtil.DB.GetConnection())
{
connection.Open();
IDbTransaction tr = connection.BeginTransaction();
try
{
//update (thus deleting) any deleted child objects
foreach (UserMRU child in deletedList)
{
child.Update(tr);
}
//Now that they are deleted remove them from memory
deletedList.Clear();
foreach (UserMRU child in List)
{
child.Update(tr);
}
tr.Commit();
}
catch
{
tr.Rollback();
throw;//WAS: throw(ex);
}
}
}
#endregion
#region criteria
/// <summary>
/// Criteria for identifying existing object
/// </summary>
[Serializable]
private class Criteria
{
public Guid UserID;
public Criteria(Guid _UserID)
{
UserID = _UserID;
}
}
#endregion
}//end UserMRUs
}//end namespace GZTW.AyaNova.BLL