00001 using System; 00002 using System.Collections.Generic; 00003 using System.Text; 00004 using Foodolini.Database; 00005 00006 using ZBar; 00007 00008 namespace Foodolini.BusinessLogic 00009 { 00015 public class BarCode 00016 { 00017 private BarCodeRow row; 00018 private bool modified = true; 00019 00020 private BarCode(BarCodeRow row){ 00021 this.row = row; 00022 if(this.row.BarCodeRowId != 0) 00023 this.modified = false; 00024 } 00025 00044 public BarCode(string identifier, string productName, float quantity, Ingredient ingredient, BarCodeType type){ 00045 this.row = new BarCodeRow(); 00046 this.row.Identifier = identifier; 00047 this.row.FoodDescriptionId = ingredient.Id; 00048 this.row.ProductName = productName; 00049 this.row.Quantity = quantity; 00050 this.ingredient = ingredient; 00051 this.row.Type = (int)type; 00052 this.modified = true; 00053 } 00054 00064 public BarCode(string identifier, BarCodeType type){ 00065 this.row = new BarCodeRow(); 00066 this.row.Identifier = identifier; 00067 this.row.Type = (int)type; 00068 this.modified = true; 00069 } 00070 00074 public string Identifier{ 00075 get{ 00076 return this.row.Identifier; 00077 } 00078 set{ 00079 this.modified = true; 00080 this.row.Identifier = value; 00081 } 00082 } 00083 00087 public string ProductName{ 00088 get{ 00089 return this.row.ProductName; 00090 } 00091 set{ 00092 this.modified = true; 00093 this.row.ProductName = value; 00094 } 00095 } 00096 00101 public double Quantity{ 00102 get{ 00103 return this.row.Quantity.GetValueOrDefault(1000); 00104 } 00105 set{ 00106 this.modified = true; 00107 this.row.Quantity = value; 00108 } 00109 } 00110 00111 private Ingredient ingredient = null; 00112 00116 public Ingredient Ingredient{ 00117 get{ 00118 if(this.ingredient == null) 00119 this.ingredient = new Ingredient(this.row.FoodDescriptionId); 00120 return this.ingredient; 00121 } 00122 set{ 00123 this.modified = true; 00124 this.ingredient = value; 00125 } 00126 } 00127 00131 public BarCodeType Type{ 00132 get{ 00133 return (BarCodeType)this.row.Type; 00134 } 00135 set{ 00136 this.modified = true; 00137 this.row.Type = (int)value; 00138 } 00139 } 00140 00147 public static BarCode Find(string identifier, BarCodeType barCodeType){ 00148 string condition = "Identifier = @0 AND Type = @1"; 00149 BarCodeRow barcoderow = Settings.Repo.SingleWhere<BarCodeRow>(condition, identifier, (int)barCodeType); 00150 if (barcoderow != null) 00151 return new BarCode(barcoderow); 00152 else 00153 return null; 00154 00155 } 00156 00157 public void Save(){ 00158 if(this.ingredient != null){ 00159 this.ingredient.Save(); 00160 if(this.row.FoodDescriptionId != this.ingredient.Id){ 00161 this.row.FoodDescriptionId = this.ingredient.Id; 00162 Console.WriteLine(this.row.FoodDescriptionId + " in save barcode"); 00163 this.modified = true; 00164 } 00165 } 00166 if(this.row.BarCodeRowId == 0){ 00167 Settings.Repo.Add<BarCodeRow>(this.row); 00168 Console.WriteLine(this.row.FoodDescriptionId + " 2in save barcode"); 00169 } 00170 else{ 00171 if(this.modified) 00172 Settings.Repo.Update<BarCodeRow>(this.row); 00173 } 00174 this.modified = false; 00175 } 00183 public override string ToString () 00184 { 00185 Console.WriteLine(this.Type.ToString()); 00186 00187 return Identifier + " (" + this.Type.ToString() + ")"; 00188 00189 } 00190 00191 00192 } 00193 }