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