00001 using System;
00002 using System.Collections.Generic;
00003 using System.Text;
00004 using Foodolini.Database;
00005
00006 namespace Foodolini.BusinessLogic
00007 {
00008 public class Person
00009 {
00010
00014 private User row;
00015
00019 private bool modified = true;
00020
00027 private Person(User user){
00028 this.row = user;
00029 this.modified = user.UserId == 0;
00030 }
00031
00035 public Person(string userName,
00036 string fullName,
00037 DateTime birthDay,
00038 Gender gender,
00039 double height,
00040 double weight,
00041 double activityFactor){
00042 this.row = new User();
00043 this.row.UserName = userName;
00044 this.row.FullName = fullName;
00045 this.row.BirthDate = birthDay;
00046 this.row.DietId = null;
00047 this.row.Gender = (int)gender;
00048 this.row.Height = height;
00049 this.row.Weight = weight;
00050 this.row.ActivityFactor = activityFactor;
00051 this.modified = true;
00052
00053 }
00054
00058 internal long Id{
00059 get{
00060 return this.row.UserId;
00061 }
00062 }
00063
00067 public string UserName {
00068 get { return this.row.UserName; }
00069 set {
00070 this.modified = true;
00071 this.row.UserName = value;
00072 }
00073 }
00074
00078 public string FullName {
00079 get { return this.row.FullName; }
00080 set {
00081 this.modified = true;
00082 this.row.FullName = value;
00083 }
00084 }
00085
00089 public DateTime BirthDate {
00090 get {
00091 return this.row.BirthDate;
00092 }
00093 set {
00094 this.modified = true;
00095 this.row.BirthDate = value;
00096 }
00097 }
00098
00102 public Gender Gender {
00103 get {
00104 return (Gender)this.row.Gender;
00105 }
00106 set {
00107 this.modified = true;
00108 this.row.Gender = (int)value;
00109 }
00110 }
00111
00115 public double Weight {
00116 get {
00117 return this.row.Weight;
00118 }
00119 set {
00120 this.modified = true;
00121 this.row.Weight = value;
00122 }
00123 }
00124
00128 public double Height {
00129 get {
00130 return this.row.Height;
00131 }
00132 set {
00133 this.modified = true;
00134 this.row.Height = value;
00135 }
00136 }
00137
00141 private Diet diet = null;
00142
00146 public Diet Diet {
00147 get {
00148 if(this.diet == null){
00149 this.diet = this.row.DietId != null ? new Diet(this.row.DietId.Value) : Diet.DefaultDiet();
00150 }
00151 return this.diet;
00152 }
00153 set {
00154 this.diet = value;
00155 }
00156 }
00157
00174 public double ActivityFactor
00175 {
00176 get
00177 {
00178 return this.row.ActivityFactor;
00179 }
00180 set
00181 {
00182 this.row.ActivityFactor = value;
00183 modified = true;
00184 }
00185 }
00186
00190 public int Age {
00191 get { return (DateTime.Now - BirthDate).Days / 365; }
00192 }
00193
00197 public int BMR {
00198 get {
00199
00200 if (Gender == Gender.Male)
00201 return (int)(10 * Weight + 6.25 * Height - 5 * Age + 5);
00202 else
00203 return (int)(10 * Weight + 6.25 * Height - 5 * Age - 161);
00204 }
00205 }
00206
00210 public int METS {
00211 get { return (int)((this.BMR * 1.2) / 24); }
00212 }
00213
00220 public IEnumerable<FoodItem> GetConsumedFoodItems(){
00221 var query = Settings.Repo.Where<FoodItemRow>("ConsumedBy = @0", this.Id);
00222 foreach(var item in query)
00223 yield return new FoodItem(item);
00224 }
00225
00238 public IEnumerable<FoodItem> GetConsumedFoodItems(DateTime start, DateTime end){
00239 var query = Settings.Repo.Where<FoodItemRow>("ConsumedBy = @0 AND ConsumedDate > @1 AND ConsumedDate < @2", this.Id, start, end);
00240 foreach(var row in query)
00241 yield return new FoodItem(row);
00242 }
00243
00244 public IEnumerable<Exercise> GetPerformedExercises (){
00245 var query = Settings.Repo.Where<ExerciseRow>("userid = @0", this.Id);
00246 foreach(var row in query)
00247 yield return new Exercise(row);
00248
00249 }
00250
00254 public void Save(){
00255
00256 if(this.diet != null){
00257 this.diet.Save();
00258
00259 if(this.row.DietId != this.diet.DietId){
00260 this.row.DietId = this.diet.DietId;
00261 this.modified = true;
00262 }
00263 }
00264 if(this.row.UserId == 0){
00265 LoadCache();
00266 Settings.Repo.Add<User>(this.row);
00267 cachedPersons.Add(this.row.UserId, this);
00268 }else if(this.modified)
00269 Settings.Repo.Update<User>(this.row);
00270 this.modified = false;
00271 }
00272
00276 public void Delete(){
00277 if(this.row.UserId != 0){
00278 LoadCache();
00279 cachedPersons.Remove(this.row.UserId);
00280 Settings.Repo.Delete<User>(this.row);
00281 }
00282 this.modified = true;
00283 }
00284
00288 private static Dictionary<long, Person> cachedPersons = null;
00289
00293 private static void LoadCache(){
00294 if(cachedPersons == null){
00295 cachedPersons = new Dictionary<long, Person>();
00296 foreach(var user in Settings.Repo.All<User>())
00297 cachedPersons.Add(user.UserId, new Person(user));
00298 }
00299 }
00300
00304 internal static Person GetById(long userId){
00305 LoadCache();
00306
00307 if(!cachedPersons.ContainsKey(userId))
00308 throw new Exception("Person does not exists!!");
00309 return cachedPersons[userId];
00310 }
00311
00315 public static IEnumerable<Person> ListUsers (){
00316
00317
00318 LoadCache();
00319 foreach(var p in cachedPersons.Values)
00320 yield return p;
00321 }
00322 }
00323 }