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

368 lines
9.0 KiB
C#

///////////////////////////////////////////////////////////
// WorkorderItems.cs
// Implementation of Class WorkorderItems
// CSLA type: Editable child collection
// Created on: 07-Jun-2004 8:41:50 AM
// Object design: Joyce
// Coded: John 28-July-2004
///////////////////////////////////////////////////////////
using System;
using CSLA.Data;
using CSLA;
using System.Data;
//using GZTW.Data;
namespace GZTW.AyaNova.BLL
{
/// <summary>
/// Editable child collection of <see cref="WorkorderItem"/> objects residing in parent <see cref="Workorder"/> object
/// </summary>
[Serializable]
public class WorkorderItems : BusinessCollectionBase {
#region Constructor
//Private constructor prevents direction instantiation
private WorkorderItems()
{
//Child
MarkAsChild();
AllowSort=false;
AllowFind=true;
AllowEdit=true;
AllowNew=true;
AllowRemove=true;
}
#endregion
#region Business properties and methods
/// <summary>
/// Retrieve WorkorderItem by index
/// </summary>
/// <param name="Item">Index</param>
public WorkorderItem this[int Item]
{
get
{
return (WorkorderItem) List[Item];
}
}
/// <summary>
/// Retrieve WorkorderItem by string containing Guid value
/// </summary>
public WorkorderItem this[string WorkorderItemID]
{
get
{
if (string.IsNullOrEmpty(WorkorderItemID)) return null;
System.Guid sid = new System.Guid(WorkorderItemID);
foreach (WorkorderItem child in List)
{
if(child.ID==sid)
return child;
}
return null;
}
}
/// <summary>
/// Retrieve WorkorderItem by Guid value
/// </summary>
public WorkorderItem this[Guid WorkorderItemID]
{
get
{
foreach (WorkorderItem child in List)
{
if(child.ID==WorkorderItemID)
return child;
}
return null;
}
}
/// <summary>
/// Called by any one of the delete methods
/// if there is only one left then it throws
/// and exception (UI should ensure this never happens)
/// </summary>
public void EnsureAtLeastOneWorkorderItem()
{
if(List.Count<2)
{
throw new Exception(LocalizedTextTable.GetLocalizedTextDirect("Workorder.Label.Error.DeleteLastWorkorderItem"));
}
}
/// <summary>
/// Remove WorkorderItem by passing it in
/// Will throw an exception if attempt to delete
/// the last workorder item. A workorder must have at
/// last one workorder item and the UI should ensure the
/// user can not even attempt to delete it.
/// </summary>
/// <param name="obj"></param>
public void Remove(WorkorderItem obj)
{
EnsureAtLeastOneWorkorderItem();
List.Remove(obj);
}
/// <summary>
/// Remove WorkorderItem by string of id value
/// Will throw an exception if attempt to delete
/// the last workorder item. A workorder must have at
/// last one workorder item and the UI should ensure the
/// user can not even attempt to delete it.
/// </summary>
/// <param name="WorkorderItemID"></param>
public void Remove(string WorkorderItemID)
{
System.Guid sid = new System.Guid(WorkorderItemID);
Remove(sid);
}
/// <summary>
/// Remove by Guid value of ID
/// Will throw an exception if attempt to delete
/// the last workorder item. A workorder must have at
/// last one workorder item and the UI should ensure the
/// user can not even attempt to delete it.
/// </summary>
/// <param name="WorkorderItemID"></param>
public void Remove(Guid WorkorderItemID)
{
EnsureAtLeastOneWorkorderItem();
WorkorderItem widelete = null;
foreach (WorkorderItem child in List)
{
if (child.ID == WorkorderItemID)
{
widelete = child;
break;
}
}
if (widelete != null)
Remove(widelete);
}
/// <summary>
/// Add a new WorkorderItem to the collection
/// </summary>
/// <param name="obj"></param>
public WorkorderItem Add(Workorder obj)
{
WorkorderItem child=WorkorderItem.NewItem(obj);
List.Add(child);
return child;
}
/// <summary>
/// Sets all items to read / write status
/// used by workorder object when it's closed or service completed is true
/// </summary>
/// <param name="RO">true=Set to read only explicitly, false=set to whatever rights allowed</param>
public void SetReadOnly(bool RO)
{
foreach (WorkorderItem child in List)
{
if(RO==false)
RO=AyaBizUtils.Right("Object.WorkorderItem")<(int)SecurityLevelTypes.ReadWrite;
child.SetReadOnly(RO);
}
}
#endregion
#region Contains
/// <summary>
/// Check if item in collection
/// </summary>
/// <param name="obj"></param>
public bool Contains(WorkorderItem obj)
{
foreach (WorkorderItem child in List)
{
if(child.Equals(obj)) return true;
}
return false;
}
/// <summary>
/// Check if item in collection by string of ID value
/// </summary>
/// <param name="WorkorderItemID"></param>
public bool Contains(string WorkorderItemID)
{
System.Guid sid = new System.Guid(WorkorderItemID);
foreach (WorkorderItem child in List)
{
if(child.ID==sid) return true;
}
return false;
}
/// <summary>
/// Check if item in collection by ID value
/// </summary>
/// <param name="WorkorderItemID"></param>
public bool Contains(Guid WorkorderItemID)
{
foreach (WorkorderItem child in List)
{
if(child.ID==WorkorderItemID) return true;
}
return false;
}
/// <summary>
/// Check if item in deleted collection
/// </summary>
/// <param name="obj"></param>
public bool ContainsDeleted(WorkorderItem obj)
{
foreach (WorkorderItem child in deletedList)
{
if(child.Equals(obj)) return true;
}
return false;
}
/// <summary>
/// Check if item in deleted collection by string of ID value
/// </summary>
/// <param name="WorkorderItemID"></param>
public bool ContainsDeleted(string WorkorderItemID)
{
System.Guid sid = new System.Guid(WorkorderItemID);
foreach (WorkorderItem child in deletedList)
{
if(child.ID==sid) return true;
}
return false;
}
#endregion
#region Static methods
/// <summary>
/// NewItems
/// </summary>
/// <returns></returns>
internal static WorkorderItems NewItems()
{
return new WorkorderItems();
}
/// <summary>
/// GetItems
/// </summary>
internal static WorkorderItems GetItems(SafeDataReader dr, Workorder obj)
{
WorkorderItems col = new WorkorderItems();
col.Fetch(dr,obj.WorkorderType, obj);
return col;
}
#endregion
#region DAL DATA ACCESS
/// <summary>
/// Fetch children
/// </summary>
private void Fetch(SafeDataReader dr, WorkorderTypes type, Workorder obj)
{
while(dr.Read())
{
List.Add(WorkorderItem.GetItem(dr, type, obj));
}
}
/// <summary>
/// Update children
/// </summary>
/// <param name="obj"></param>
/// <param name="tr"></param>
internal void Update(Workorder obj,IDbTransaction tr)
{
//update (thus deleting) any deleted child objects
foreach (WorkorderItem child in deletedList)
{
if(!child.mMoved)
child.Update(obj,tr);
}
//Now that they are deleted remove them from memory
deletedList.Clear();
foreach (WorkorderItem child in List)
{
child.Update(obj,tr);
}
}
#region Shared delete method
/// <summary>
/// Given a workorder ID
///
/// retrieves all workorder item id values for that
/// workorder, then calls the child and grandchild Shared static direct
/// delete methods to clear out those collections, then finally
/// removes all workorder items of that workorder ID
///
/// Called by Workorder delete
/// </summary>
/// <param name="WorkorderID">ID of parent workorder item</param>
/// <param name="transaction">Database transaction from ascendant item</param>
internal static void DeleteItems(Guid WorkorderID,IDbTransaction transaction)
{
//This one is more tricky, here we need to iterate through all the matching workorderitem records
//and call the workorderitem static delete method
SafeDataReader dr = null;
System.Collections.ArrayList al = new System.Collections.ArrayList();
try
{
dr=DBUtil.GetReaderFromSQLString("SELECT aID " +
"FROM aWorkorderItem " +
"WHERE aWorkorderID=@ID",WorkorderID,transaction);
while(dr.Read())
{
al.Add(dr.GetGuid("aID"));
}
dr.Close();
foreach(object o in al)
WorkorderItem.DeleteItems((Guid)o,transaction);
}
finally
{
if(dr!=null) dr.Close();
}
}
#endregion
#endregion
}//end WorkorderItems
}//end namespace GZTW.AyaNova.BLL