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 Nutrient 00019 { 00020 private NutritionDefinition nutRow; 00021 00025 private Nutrient(NutritionDefinition nutRow){ 00026 this.nutRow = nutRow; 00027 } 00028 00029 internal long Id 00030 { 00031 get 00032 { 00033 return this.nutRow.NutritionDefinitionId; 00034 } 00035 } 00036 00040 public string Unit 00041 { 00042 get 00043 { 00044 return this.nutRow.Unit; 00045 } 00046 } 00047 00051 public string TagName 00052 { 00053 get 00054 { 00055 return this.nutRow.TagName; 00056 } 00057 } 00058 00062 public string Description 00063 { 00064 get 00065 { 00066 return this.nutRow.Description; 00067 } 00068 } 00069 00073 public int SortOrder { 00074 get { 00075 return this.nutRow.SortOrder; 00076 } 00077 } 00078 00085 internal static Nutrient CreateNutritionDefinition(long nutritionDefinitionRowId){ 00086 LoadDefinitions(); 00087 if(!definitions.ContainsKey(nutritionDefinitionRowId)) 00088 throw new Exception("NutritionDefinitionId " + nutritionDefinitionRowId + " unknown!"); 00089 return definitions[nutritionDefinitionRowId]; 00090 } 00091 00092 00096 private static Dictionary<long, Nutrient> definitions = null; 00097 00101 private static void LoadDefinitions(){ 00102 if(definitions == null){ 00103 definitions = new Dictionary<long, Nutrient>(); 00104 var query = Settings.Repo.Query<NutritionDefinition>("SELECT * FROM NutritionDefinitions ORDER BY SortOrder"); 00105 foreach(var nutDef in query) 00106 definitions.Add(nutDef.NutritionDefinitionId, new Nutrient(nutDef)); 00107 } 00108 } 00109 00114 public static Nutrient Calories 00115 { 00116 get { return FindNutrient("ENERC_KCAL"); } 00117 } 00118 00123 public static Nutrient Protein 00124 { 00125 get { return FindNutrient("PROCNT"); } 00126 } 00127 00132 public static Nutrient Carbohydrates 00133 { 00134 get { return FindNutrient("CHOCDF"); } 00135 } 00136 00141 public static Nutrient Fat 00142 { 00143 get { return FindNutrient("FAT"); } 00144 } 00145 00151 private static Nutrient FindNutrient(string tagName) 00152 { 00153 return new List<Nutrient>(ListNutritionDefinitions()).Find(n => string.Compare(n.TagName, tagName, true) == 0); 00154 } 00155 00162 public static ICollection<Nutrient> ListNutritionDefinitions(){ 00163 LoadDefinitions(); 00164 return definitions.Values; 00165 } 00166 } 00167 }