00001 using System;
00002 using System.Collections.Generic;
00003 using System.Text;
00004 using HouseOver.THARM.Buildings;
00005 using System.Reflection;
00006 using HouseOver.THARM.Search;
00007 using HouseOver.THARM.Attributes;
00008
00009 namespace HouseOver.THARM
00010 {
00014 [Serializable]
00015 public class Profile
00016 {
00017 private int maxPrice;
00018 private Type item;
00019 private Customer owner;
00020 private Dictionary<string, Check> checks = new Dictionary<string, Check>();
00021
00028 public Profile(Customer owner, int maxPrice, Type item)
00029 {
00030 this.owner = owner;
00031 this.maxPrice = maxPrice;
00032 this.item = item;
00033 CreateChecks(item);
00034 }
00035
00040 private void CreateChecks(Type type)
00041 {
00042 checks.Clear();
00043 PropertyInfo[] pis = type.GetProperties();
00044
00045 foreach (PropertyInfo pi in pis)
00046
00047 {
00048 SaleCriteria sc = (SaleCriteria)Attribute.GetCustomAttribute(pi, typeof(SaleCriteria));
00049 if (sc == null)
00050 continue;
00051
00052 if (pi.PropertyType.IsEnum)
00053 {
00054 checks.Add(pi.Name, new EnumCheck(pi.Name, sc.HumanReadableName == null
00055 ? pi.Name : sc.HumanReadableName, false, pi.PropertyType));
00056 }
00057 else if (pi.PropertyType.FindInterfaces(MyInterfaceFilter, "System.IComparable").Length > 0)
00058 {
00059 checks.Add(pi.Name,new ComparableCheck(pi.Name, sc.HumanReadableName == null
00060 ? pi.Name : sc.HumanReadableName, false, pi.PropertyType, null, null));
00061 }
00062 }
00063 }
00064
00070 public bool SaleWithinParameters(Sale sale)
00071 {
00072 if (sale.Price > this.maxPrice)
00073 return false;
00074
00075 if(!item.IsAssignableFrom(sale.Item.GetType()))
00076 {
00077 return false;
00078 }
00079
00080 foreach (Check c in Checks.Values)
00081 {
00082 object retval = sale.Item.GetType().GetProperty(c.Name).GetValue(sale.Item, null);
00083 if (!c.Verify(retval))
00084 {
00085 return false;
00086 }
00087 }
00088
00089 return true;
00090 }
00091
00098 public static bool MyInterfaceFilter(Type typeObj, Object criteriaObj)
00099 {
00100 if (typeObj.ToString() == criteriaObj.ToString())
00101 return true;
00102 else
00103 return false;
00104 }
00105
00109 public Type Item { get { return item; } set { item = value; CreateChecks(item); } }
00113 public int MaxPrice { get { return maxPrice; } set { maxPrice = value; } }
00117 public Customer Owner { get { return owner; } }
00121 public Dictionary<string, Check> Checks { get { return checks; } }
00122
00123 }
00124 }