00001 using System;
00002 using System.Collections.Generic;
00003 using System.Text;
00004 using Foodolini.Database;
00005
00006 namespace Foodolini.BusinessLogic
00007 {
00018 public class FoodItem
00019 {
00023 private FoodItemRow row;
00024
00028 private bool modified = true;
00029
00033 public FoodItem(double quantity, Ingredient ingredient, DateTime expirationDate, bool isOpen){
00034 this.row = new FoodItemRow();
00035 this.row.Quantity = quantity;
00036 this.ingredient = ingredient;
00037 this.row.ExpirationDate = expirationDate;
00038 this.row.IsOpen = isOpen;
00039 this.row.ConsumedDate = null;
00040 this.row.ConsumedBy = null;
00041 this.row.RegisteredDate = DateTime.Now;
00042 this.modified = true;
00043 }
00044
00045 public FoodItem(double quantity, Ingredient ingredient, DateTime expirationDate, bool isOpen, DateTime consumedDate, Person consumedBy){
00046 this.row = new FoodItemRow();
00047 this.row.Quantity = quantity;
00048 this.ingredient = ingredient;
00049 this.row.ExpirationDate = expirationDate;
00050 this.row.IsOpen = isOpen;
00051 this.row.ConsumedDate = consumedDate;
00052 this.consumedBy = consumedBy;
00053 this.row.RegisteredDate = DateTime.Now;
00054 this.modified = true;
00055 }
00056
00060 internal FoodItem(FoodItemRow row){
00061 this.row = row;
00062 this.modified = false;
00063 }
00064
00068 public long Id
00069 {
00070 get{
00071 return this.row.FoodItemRowId;
00072 }
00073 }
00074
00078 public double Quantity
00079 {
00080 get{
00081 return this.row.Quantity;
00082 }
00083 set{
00084 this.modified = true;
00085 this.row.Quantity = value;
00086 }
00087
00088 }
00089
00093 private Ingredient ingredient = null;
00094
00101 public Ingredient Ingredient
00102 {
00103 get{
00104 if(this.ingredient == null){
00105 this.ingredient = new Ingredient(this.row.FoodDescriptionId);
00106 }
00107 return this.ingredient;
00108 }
00109 set{
00110
00111 this.ingredient = value;
00112 }
00113 }
00114
00118 public DateTime ExpirationDate{
00119 get{
00120 return this.row.ExpirationDate;
00121 }
00122 set{
00123 this.modified = true;
00124 this.row.ExpirationDate = value;
00125 }
00126 }
00127
00131 public DateTime RegisteredDate
00132 {
00133 get { return this.row.RegisteredDate; }
00134 set
00135 {
00136 this.modified = true;
00137 this.row.RegisteredDate = value;
00138 }
00139 }
00140
00144 public bool IsOpen{
00145 get{
00146 return this.row.IsOpen;
00147 }
00148 }
00149
00154 public DateTime ConsumedDate{
00155 get{
00156 if(this.row.ConsumedDate == null)
00157 throw new Exception("This FoodItem is not consumed");
00158 return this.row.ConsumedDate.Value;
00159 }
00160 }
00161
00165 public Person ConsumedBy
00166 {
00167 get{
00168 if(this.consumedBy == null){
00169 if(this.row.ConsumedBy == null)
00170 return null;
00171 this.consumedBy = Person.GetById(this.row.ConsumedBy.Value);
00172 }
00173 return this.consumedBy;
00174 }
00175 }
00176
00180 public bool IsConsumed{
00181 get{
00182 return this.row.ConsumedBy != null && this.row.ConsumedDate != null;
00183 }
00184 }
00185
00189 private Person consumedBy = null;
00190
00198 public void Consume(Person person){
00199 Consume(person, this.Quantity);
00200 }
00210 public void Consume(Person person, double quantity){
00211 this.modified = true;
00212 if (quantity < this.Quantity ){
00213 FoodItem partition = this.Split(quantity);
00214 if(person != null)
00215 partition.row.ConsumedBy = person.Id;
00216 partition.row.ConsumedDate = DateTime.Now;
00217 partition.OpenFoodItem();
00218 partition.Save();
00219 }else{
00220 if(person != null)
00221 this.row.ConsumedBy = person.Id;
00222 this.row.ConsumedDate = DateTime.Now;
00223 }
00224 this.OpenFoodItem();
00225 this.Save();
00226 }
00227
00231 public void OpenFoodItem(){
00232 if (!this.IsOpen){
00233 this.modified = true;
00234 this.row.IsOpen = true;
00235
00236 if (System.DateTime.Now + this.Ingredient.ExpirationAfterOpening < this.ExpirationDate) {
00237 this.ExpirationDate = System.DateTime.Now + this.Ingredient.ExpirationAfterOpening;
00238 }
00239 }
00240 }
00241
00242 public void CloseFoodItem(){
00243 if (this.IsOpen){
00244 this.modified = true;
00245 this.row.IsOpen = false;
00246 this.ExpirationDate = this.row.RegisteredDate + this.Ingredient.ShelfLife;
00247
00248 }
00249 }
00250
00261 public FoodItem Split(double quantity){
00262 if(quantity == this.Quantity) {
00263 throw new Exception("Quantity in FoodItem equals quantity for split");
00264 } else if (quantity < this.Quantity) {
00265 this.Quantity -= quantity;
00266 this.modified = true;
00267 this.OpenFoodItem();
00268 if(this.IsConsumed)
00269 throw new Exception("Cannot split consumed FoodItem");
00270 else
00271 return new FoodItem(quantity, this.Ingredient, this.ExpirationDate, this.IsOpen);
00272 } else {
00273 throw new Exception("split more than we have");
00274 }
00275 }
00276
00277
00281 public void Save(){
00282
00283 if(this.consumedBy != null){
00284 this.consumedBy.Save();
00285
00286 if(this.row.ConsumedBy != this.consumedBy.Id){
00287 this.modified = true;
00288 this.row.ConsumedBy = this.consumedBy.Id;
00289 }
00290 }
00291
00292 if(this.ingredient != null){
00293 this.ingredient.Save();
00294
00295 if(this.row.FoodDescriptionId != this.ingredient.Id){
00296 this.modified = true;
00297 this.row.FoodDescriptionId = this.ingredient.Id;
00298 }
00299 }
00300
00301
00302 if(this.row.FoodItemRowId == 0){
00303 Settings.Repo.Add<FoodItemRow>(this.row);
00304 Settings.Repo.Commit();
00305
00306 }
00307 else if(this.modified)
00308 Settings.Repo.Update<FoodItemRow>(this.row);
00309
00310 this.modified = false;
00311
00312 }
00313
00317 public void Delete(){
00318 if(this.row.FoodItemRowId != 0)
00319 Settings.Repo.Delete<FoodItemRow>(this.row);
00320 this.modified = true;
00321 }
00322
00329 public static IEnumerable<FoodItem> ListFoodItems(){
00330 var query = Settings.Repo.Where<FoodItemRow>("ConsumedBy is NULL AND ConsumedDate is NULL");
00331 foreach(var item in query)
00332 yield return new FoodItem(item);
00333 }
00334 }
00335 }