1
2
3
4 package ca.spaz.cron.datasource.sql;
5
6 import java.io.PrintStream;
7 import java.util.*;
8
9 import org.apache.log4j.Logger;
10
11 import ca.spaz.cron.database.*;
12 import ca.spaz.cron.datasource.*;
13
14 /***
15 * This class stores all there is to do with a Food item in the database.
16 *
17 * A Food has a description, some category information, a set of measures, and
18 * of course, Nutrient information.
19 *
20 * @author davidson
21 */
22 class SQLFoodImpl implements SQLFood {
23 /***
24 * Logger for this class
25 */
26 private static final Logger logger = Logger.getLogger(SQLFoodImpl.class);
27
28 private SQLDatasource dataSource;
29
30
31 protected int FID;
32
33
34 protected String ndb_id;
35
36
37 protected String description;
38
39 protected String source;
40
41 protected FoodGroup foodGroup;
42
43 protected List measures;
44
45 protected Minerals minerals;
46
47 protected Vitamins vitamins;
48
49 protected AminoAcids aminoAcids;
50
51 protected MacroNutrients macroNutrients;
52
53 protected Lipids lipids;
54
55 private String sourceUID;
56
57 SQLFoodImpl(SQLDatasource source) {
58 FID = -1;
59 this.dataSource = source;
60 this.description = "";
61 this.foodGroup = SQLFoodGroupImpl.EMPTY_GROUP;
62 this.ndb_id = "";
63 }
64
65
66
67
68 public int getID() {
69 return FID;
70 }
71
72
73
74
75 public String getNDB_ID() {
76 return ndb_id;
77 }
78
79
80
81
82 public FoodGroup getFoodGroup() {
83 return foodGroup;
84 }
85
86
87
88
89 public String getSource() {
90 return source;
91 }
92
93
94
95
96 public List getMeasures() {
97 if (measures == null) {
98 measures = dataSource.getMeasuresFor(this);
99 }
100 return measures;
101 }
102
103
104
105
106 public double getCalories() {
107 return getMacroNutrients().kcals;
108 }
109
110
111
112
113 public MacroNutrients getMacroNutrients() {
114 if (macroNutrients == null) {
115 macroNutrients = new MacroNutrients(this);
116 }
117 assert (macroNutrients != null);
118 return macroNutrients;
119 }
120
121
122
123
124 public Lipids getLipids() {
125 if (lipids == null) {
126 lipids = new Lipids(this);
127 }
128 assert (lipids != null);
129 return lipids;
130 }
131
132 public String toString() {
133 return description;
134 }
135
136
137
138
139 public String getDescription() {
140 return description;
141 }
142
143
144
145
146 public Minerals getMinerals() {
147 if (minerals == null) {
148 minerals = new Minerals(this);
149 }
150 assert (minerals != null);
151 return minerals;
152 }
153
154
155
156
157 public Vitamins getVitamins() {
158 if (vitamins == null) {
159 vitamins = new Vitamins(this);
160 }
161 assert (vitamins != null);
162 return vitamins;
163 }
164
165
166
167
168 public AminoAcids getAminoAcids() {
169 if (aminoAcids == null) {
170 aminoAcids = new AminoAcids(this);
171 }
172 assert (aminoAcids != null);
173 return aminoAcids;
174 }
175
176
177
178
179 public double getNutrientAmount(NutrientInfo ni) {
180 if (ni == null) return 0;
181 if (ni.getCategory().equals("MacroNutrients")) {
182 return getMacroNutrients().getAmountByTag(ni.getTag());
183 }
184 if (ni.getCategory().equals("Minerals")) {
185 return getMinerals().getAmountByTag(ni.getTag());
186 }
187 if (ni.getCategory().equals("AminoAcids")) {
188 return getAminoAcids().getAmountByTag(ni.getTag());
189 }
190 if (ni.getCategory().equals("Vitamins")) {
191 return getVitamins().getAmountByTag(ni.getTag());
192 }
193 if (ni.getCategory().equals("Lipids")) {
194 return getLipids().getAmountByTag(ni.getTag());
195 }
196 return 0;
197 }
198
199
200
201
202 public void setNutrientAmount(NutrientInfo ni, double val) {
203 if (ni.getCategory().equals("MacroNutrients")) {
204 getMacroNutrients().setAmountByTag(ni.getTag(), val);
205 }
206 if (ni.getCategory().equals("Minerals")) {
207 getMinerals().setAmountByTag(ni.getTag(), val);
208 }
209 if (ni.getCategory().equals("AminoAcids")) {
210 getAminoAcids().setAmountByTag(ni.getTag(), val);
211 }
212 if (ni.getCategory().equals("Vitamins")) {
213 getVitamins().setAmountByTag(ni.getTag(), val);
214 }
215 if (ni.getCategory().equals("Lipids")) {
216 getLipids().setAmountByTag(ni.getTag(), val);
217 }
218 }
219
220
221
222
223 public void writeXML(PrintStream out) {
224 out.print("<food ");
225 out.print(" name=\""+getDescription()+"\"");
226 out.print(" group=\""+getFoodGroup()+"\"");
227 out.print(" source=\""+getSource()+"\"");
228 out.println(" >");
229
230 List measures = getMeasures();
231 Iterator iter = measures.iterator();
232 while (iter.hasNext()) {
233 Measure m = (Measure)iter.next();
234 if (m != Measure.GRAM) {
235 out.print(" <measure ");
236 out.print(" name=\""+m.getDescription()+"\"");
237 out.print(" amount=\""+m.getAmount()+"\"");
238 out.print(" grams=\""+m.getGrams()+"\"");
239 out.println(" />");
240 }
241 }
242
243 List nutrients = NutrientInfo.getGlobalList();
244 iter = nutrients.iterator();
245 while (iter.hasNext()) {
246 NutrientInfo ni = (NutrientInfo)iter.next();
247 double val = getNutrientAmount(ni);
248 if (val > 0) {
249 out.print(" <nutrient ");
250 out.print(" name=\""+ni.getName()+"\"");
251 out.print(" amount=\""+val+"\"");
252 out.println(" />");
253 }
254 }
255
256 out.println("</food>");
257 }
258
259
260
261
262 public void setDescription(String text) {
263 this.description = text;
264
265 }
266
267
268
269
270 public void setFoodGroup(FoodGroup fg) {
271 if (!(fg instanceof SQLFoodGroup)) {
272 this.foodGroup = new SQLFoodGroupImpl(fg.getFoodGroupName());
273 if (!dataSource.getFoodGroups().contains(this.foodGroup)) {
274 dataSource.addFoodGroup(this.foodGroup);
275 }
276 } else {
277 this.foodGroup = fg;
278 }
279 }
280
281
282
283
284 public int getNumTimesConsumed() {
285 int num = 0;
286 if (dataSource instanceof ILocalFoodDatasource) {
287 ILocalFoodDatasource lds = (ILocalFoodDatasource) dataSource;
288 num = lds.getTimesConsumed(this);
289 }
290 return num;
291 }
292
293
294
295
296 public void setSource(String string) {
297 this.source = string;
298 }
299
300
301
302
303 public IFoodDatasource getDataSource() {
304 return dataSource;
305 }
306
307 public boolean equals(Object o) {
308 if (o instanceof Food) {
309 SQLFoodImpl f = (SQLFoodImpl) o;
310 return (f != null &&
311 dataSource == f.dataSource &&
312 description.equals(f.description) &&
313 source.equals(f.source));
314 } else {
315 return false;
316 }
317 }
318
319
320
321
322 public void setID(int ID) {
323 this.FID = ID;
324 }
325
326
327
328
329 public void setMeasures(List measures) {
330 this.measures = measures;
331 }
332
333
334
335
336 public String getSourceUID() {
337 if (sourceUID != null) {
338 setModified();
339 }
340 return sourceUID;
341 }
342
343 public void setModified() {
344 sourceUID = "sql." + dataSource.getId() + "-" + getID();
345 }
346
347 public void setSourceUID(String uid) {
348 sourceUID = uid;
349 }
350
351 }
352
353
354
355
356
357
358
359
360
361
362