using System;
using System.Collections.Generic;
using System.Text;
using Ubik.Engine.Client;
namespace StoresAndStockPricing.Model{
/// <summary>
/// Retail store groups are tools for applying updates to multiple retail stores.
/// </summary>
public class RetailStoreGroup : Individual
{
private NonEmptyTrackedString _name;
private TrackedReference<RetailStoreGroup> _parent;
private VirtualCollection<RetailStoreGroup> _children;
private TrackedCollection<RetailStore> _directlyIncludedRetailStores;
private TrackedReference<RetailStoreGroupFunction> _function;
protected RetailStoreGroup(Session session)
: base(session)
{
_name = new NonEmptyTrackedString(this, "Name");
_parent = new TrackedReference<RetailStoreGroup>(this, "Parent");
_children = new VirtualCollection<RetailStoreGroup>(this, "Parent", VirtualCollectionMultiplicity.OneToMany);
_directlyIncludedRetailStores = new TrackedCollection<RetailStore>(this, "DirectlyIncludedRetailStores");
_directlyIncludedRetailStores.ItemAdding += DirectlyIncludedRetailStoreAdding;
_function = new TrackedReference<RetailStoreGroupFunction>(this, "Function");
}
public RetailStoreGroup(Session session, string name, RetailStoreGroupFunction function)
: this(session)
{
if (function == null)
throw new ArgumentNullException("function");
_function.Reset(function);
_name.Reset(name);
}
public RetailStoreGroup(Session session, string name, RetailStoreGroup parent)
: this(session)
{
if (parent == null)
throw new ArgumentNullException("parent");
_function.Reset(parent.Function);
_parent.Reset(parent);
_name.Reset(name);
}
public string Name
{
get
{
return _name.Value;
}
set
{
_name.Value = value;
}
}
public RetailStoreGroup Parent
{
get
{
return _parent.Value;
}
set
{
if (value != null)
{
if (value.Function != Function)
throw new ArgumentException(RetailStoreGroupResources.ParentFunctionMustMatch);
if (value == this || IsDescendant(value))
throw new ArgumentException(RetailStoreGroupResources.ParentCannotBeChild);
}
_parent.Value = value;
}
}
private bool IsDescendant(RetailStoreGroup value)
{
foreach (RetailStoreGroup child in Children)
{
if (child == value || child.IsDescendant(value))
return true;
}
return false;
}
public IEnumerable<RetailStoreGroup> Children
{
get
{
return _children;
}
}
public ICollection<RetailStore> DirectlyIncludedRetailStores
{
get
{
return _directlyIncludedRetailStores;
}
}
public IEnumerable<RetailStore> RetailStores
{
get
{
foreach (RetailStore store in DirectlyIncludedRetailStores)
yield return store;
foreach (RetailStoreGroup group in Children)
{
foreach (RetailStore store in group.RetailStores)
{
yield return store;
}
}
}
}
private void DirectlyIncludedRetailStoreAdding(object sender, TrackedCollection<RetailStore>.TrackedCollectionItemEventArgs args)
{
// Remove the store from any other groups in the same function.
foreach (RetailStoreGroup group in Session.Select<RetailStoreGroup>
("Function = {" + Function.ToString() + "} && DirectlyIncludedRetailStores[Identity = {" + ToString() + "}]"))
{
if (group != this && group.DirectlyIncludedRetailStores.Contains(args.Item))
{
group.DirectlyIncludedRetailStores.Remove(args.Item);
}
}
}
public RetailStoreGroupFunction Function
{
get
{
return _function.Value;
}
}
}
}
|