00001 using System;
00002 using System.Collections.Generic;
00003 using System.Text;
00004 using Foodolini.Database;
00005
00006 namespace Foodolini.BusinessLogic
00007 {
00008 public partial class Ingredient
00009 {
00010 private FoodDescription row;
00011 private NutritionDictionary nutrients;
00012 private bool modified = true;
00013
00014 internal Ingredient(long foodDescriptionId)
00015 {
00016 this.row = Settings.Repo.SingleWhere<FoodDescription>("FoodDescriptionId = " + foodDescriptionId);
00017 if (this.row == null)
00018 throw new Exception("Ingredient not found in the database!");
00019 this.modified = false;
00020 }
00021
00022 public Ingredient() { }
00023
00024 internal Ingredient(FoodDescription row)
00025 {
00026 this.row = row;
00027 this.modified = row.FoodDescriptionId == 0;
00028 }
00029
00030 public Ingredient(String name)
00031 {
00032 this.row = new FoodDescription();
00033 this.row.LongDescription = name;
00034 this.modified = true;
00035 }
00036
00040 public IDictionary<Nutrient, double> Nutrients
00041 {
00042 get
00043 {
00044 if (this.nutrients == null)
00045 this.nutrients = new NutritionDictionary(this.Id);
00046 return this.nutrients;
00047 }
00048 }
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00067 internal long Id
00068 {
00069 get { return this.row.FoodDescriptionId; }
00070 }
00071
00072
00073
00074
00078 public string CommercialName
00079 {
00080 get { return this.row.CommercialName; }
00081 set
00082 {
00083 this.modified = true;
00084 this.row.CommercialName = value;
00085 }
00086 }
00087
00091 public string ShortDescription
00092 {
00093 get { return this.row.ShortDescription; }
00094 set
00095 {
00096 this.modified = true;
00097 this.row.ShortDescription = value;
00098 }
00099 }
00100
00104 public string LongDescription
00105 {
00106 get { return this.row.LongDescription; }
00107 set
00108 {
00109 this.modified = true;
00110 this.row.LongDescription = value;
00111 }
00112 }
00113
00117 public string Manufacturer
00118 {
00119 get { return this.row.Manufacturer; }
00120 set
00121 {
00122 this.modified = true;
00123 this.row.Manufacturer = value;
00124 }
00125 }
00126
00130 public TimeSpan ShelfLife
00131 {
00132 get { return new TimeSpan(this.row.ShelfLife.GetValueOrDefault(TimeSpan.Zero.Ticks)); }
00133 set
00134 {
00135 this.modified = true;
00136 this.row.ShelfLife = value.Ticks;
00137 }
00138 }
00139
00143 public TimeSpan ExpirationAfterOpening
00144 {
00145 get { return new TimeSpan(this.row.ExpirationAfterOpening.GetValueOrDefault(new TimeSpan(1,0,0,0).Ticks)); }
00146 set
00147 {
00148 this.modified = true;
00149 if (value == TimeSpan.MaxValue)
00150 this.row.ExpirationAfterOpening = null;
00151 else
00152 this.row.ExpirationAfterOpening = value.Ticks;
00153 }
00154 }
00155
00160 public string Category
00161 {
00162 get
00163 {
00164 LoadFoodGroups();
00165 string val;
00166 if (this.row.FoodGroupId.HasValue && foodGroups.TryGetValue(this.row.FoodGroupId.Value, out val))
00167 return val;
00168 return string.Empty;
00169 }
00170 set
00171 {
00172 this.modified = true;
00173 this.row.FoodGroupId = GetFoodGroupId(value);
00174 }
00175 }
00176
00180 public double Protein
00181 {
00182 get { return this.row.Protein.GetValueOrDefault(); }
00183 }
00184
00188 public double Carbohydrates
00189 {
00190 get { return this.row.Carbohydrates.GetValueOrDefault(); }
00191 }
00192
00196 public double Fat
00197 {
00198 get { return this.row.Fat.GetValueOrDefault(); }
00199 }
00200
00205 private static long? GetFoodGroupId(string category)
00206 {
00207 long id = 0;
00208 LoadFoodGroups();
00209 foreach (var pair in foodGroups) {
00210 if (pair.Value == category)
00211 id = pair.Key;
00212 }
00213 if (id == 0 && category == string.Empty)
00214 return null;
00215 else if (id == 0)
00216 throw new Exception("The FoodGroup '" + category + "' doesn't exist!");
00217 else
00218 return id;
00219 }
00220
00227 public static IEnumerable<Ingredient> ListByCategory(string category)
00228 {
00229 foreach (var fd in Settings.Repo.Where<FoodDescription>("FoodGroupId = @0", GetFoodGroupId(category)))
00230 yield return new Ingredient(fd);
00231 }
00232
00245 public static IEnumerable<Ingredient> ListByCategory(string category, string searchString)
00246 {
00247 foreach (var fd in Settings.Repo.Where<FoodDescription>("FoodGroupId = @0 AND LongDescription LIKE @1", GetFoodGroupId(category), "%" + searchString + "%"))
00248 yield return new Ingredient(fd);
00249 }
00250
00260 public static IEnumerable<Ingredient> ListByLongDescription(string searchString)
00261 {
00262 foreach (var fd in Settings.Repo.Where<FoodDescription>("LongDescription LIKE @0 limit 500", "%" + searchString + "%"))
00263 yield return new Ingredient(fd);
00264 }
00265
00271 public static Ingredient GetByLongDescription(string description)
00272 {
00273 FoodDescription row = Settings.Repo.SingleWhere<FoodDescription>("longdescription = @0", description);
00274 if (row != null)
00275 return new Ingredient(row);
00276 return null;
00277 }
00278
00284 internal static Ingredient GetById(long ingredientId)
00285 {
00286 return new Ingredient(Settings.Repo.SingleWhere<FoodDescription>(" FoodDescriptionId = @0 ", ingredientId));
00287 }
00288
00292 public void Save()
00293 {
00294
00295
00296 if (this.nutrients != null) {
00297
00298 List<Nutrient> definitions = new List<Nutrient>(Nutrient.ListNutritionDefinitions());
00299
00300 if (this.nutrients.ContainsKey(Nutrient.Protein))
00301 this.row.Protein = this.nutrients[Nutrient.Protein];
00302
00303 if (this.nutrients.ContainsKey(Nutrient.Carbohydrates))
00304 this.row.Carbohydrates = this.nutrients[Nutrient.Carbohydrates];
00305
00306 if (this.nutrients.ContainsKey(Nutrient.Fat))
00307 this.row.Fat = this.nutrients[Nutrient.Fat];
00308 }
00309
00310 if (this.row.FoodDescriptionId == 0)
00311 Settings.Repo.Add<FoodDescription>(this.row);
00312 else {
00313 if (this.modified)
00314 Settings.Repo.Update<FoodDescription>(this.row);
00315 }
00316
00317
00318 if (this.nutrients != null)
00319 this.nutrients.Save(this.row.FoodDescriptionId);
00320
00321 this.modified = false;
00322 }
00323
00327 public void Delete()
00328 {
00329 long rowid = this.row.FoodDescriptionId;
00330 if (this.row.FoodDescriptionId != 0) {
00331 if (this.nutrients != null)
00332 this.nutrients.Delete(rowid);
00333 Settings.Repo.Delete<FoodDescription>(this.row);
00334 }
00335 this.row.FoodDescriptionId = 0;
00336 }
00337
00341 private static Dictionary<long, string> foodGroups = null;
00342
00346 public static ICollection<string> Categories
00347 {
00348 get
00349 {
00350 LoadFoodGroups();
00351 return foodGroups.Values;
00352 }
00353 }
00354
00358 public FoodItem InStorage()
00359 {
00360 FoodItem foodItem = null;
00361 foreach(FoodItem item in FoodItem.ListFoodItems())
00362 {
00363 if(Ingredient.Compare(this, item.Ingredient))
00364 {
00365 if(foodItem == null)
00366 foodItem = item;
00367 else if(DateTime.Compare(foodItem.ExpirationDate, item.ExpirationDate) > 0)
00368 foodItem = item;
00369 }
00370 }
00371
00372 return foodItem;
00373 }
00374
00381 public static bool Compare(Ingredient ingredient1, Ingredient ingredient2)
00382 {
00383 return ingredient1.Id.Equals(ingredient2.Id);
00384 }
00385
00386 public override string ToString()
00387 {
00388 return this.Id.ToString();
00389 }
00390
00394 private static void LoadFoodGroups()
00395 {
00396 if (foodGroups == null) {
00397 foodGroups = new Dictionary<long, string>();
00398 foreach (var fg in Settings.Repo.All<FoodGroup>())
00399 foodGroups.Add(fg.FoodGroupId, fg.Description);
00400 }
00401 }
00402 }
00403 }