View Javadoc

1   /*
2    * Created on 19-Mar-2005
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      // primary key
31      protected int FID;
32  
33      // USDA ndb_id (deprecated)
34      protected String ndb_id;
35  
36      // names:
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      /* (non-Javadoc)
66      * @see ca.spaz.cron.database.Food#getID()
67      */
68     public int getID() {
69          return FID;
70      }
71  
72      /* (non-Javadoc)
73      * @see ca.spaz.cron.database.Food#getNDB_ID()
74      */
75     public String getNDB_ID() {
76          return ndb_id;
77      }
78  
79      /* (non-Javadoc)
80      * @see ca.spaz.cron.database.Food#getFoodGroup()
81      */
82     public FoodGroup getFoodGroup() {
83          return foodGroup;
84      }
85  
86      /* (non-Javadoc)
87      * @see ca.spaz.cron.database.Food#getSource()
88      */
89     public String getSource() {
90          return source;
91      }
92  
93      /* (non-Javadoc)
94      * @see ca.spaz.cron.database.Food#getMeasures()
95      */
96     public List getMeasures() {
97          if (measures == null) {
98              measures = dataSource.getMeasuresFor(this);
99          }
100         return measures;
101     }
102 
103     /* (non-Javadoc)
104     * @see ca.spaz.cron.database.Food#getCalories()
105     */
106    public double getCalories() {
107         return getMacroNutrients().kcals;
108     }
109 
110     /* (non-Javadoc)
111     * @see ca.spaz.cron.database.Food#getMacroNutrients()
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     /* (non-Javadoc)
122     * @see ca.spaz.cron.database.Food#getLipids()
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     /* (non-Javadoc)
137     * @see ca.spaz.cron.database.Food#getDescription()
138     */
139    public String getDescription() {
140         return description;
141     }
142 
143     /* (non-Javadoc)
144     * @see ca.spaz.cron.database.Food#getMinerals()
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     /* (non-Javadoc)
155     * @see ca.spaz.cron.database.Food#getVitamins()
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     /* (non-Javadoc)
166     * @see ca.spaz.cron.database.Food#getAminoAcids()
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     /* (non-Javadoc)
177     * @see ca.spaz.cron.database.Food#getNutrientAmount(ca.spaz.cron.database.NutrientInfo)
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     /* (non-Javadoc)
200     * @see ca.spaz.cron.database.Food#setNutrientAmount(ca.spaz.cron.database.NutrientInfo, double)
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    /* (non-Javadoc)
221     * @see ca.spaz.cron.datasource.sql.SQLFood#writeXML(java.io.PrintStream)
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     /* (non-Javadoc)
260     * @see ca.spaz.cron.database.Food#setDescription(java.lang.String)
261     */
262    public void setDescription(String text) {
263         this.description = text;
264         
265     }
266 
267     /* (non-Javadoc)
268     * @see ca.spaz.cron.database.Food#setFoodGroup(ca.spaz.cron.database.FoodGroup)
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     /* (non-Javadoc)
282     * @see ca.spaz.cron.database.Food#getNumTimesConsumed()
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     /* (non-Javadoc)
294     * @see ca.spaz.cron.database.Food#setSource(java.lang.String)
295     */
296    public void setSource(String string) {
297         this.source = string;
298     }
299 
300     /* (non-Javadoc)
301     * @see ca.spaz.cron.database.Food#getDataSource()
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     /* (non-Javadoc)
320     * @see ca.spaz.cron.database.Food#setID(int)
321     */
322     public void setID(int ID) {
323         this.FID = ID;
324     }
325 
326    /* (non-Javadoc)
327     * @see ca.spaz.cron.database.Food#setMeasures(java.util.List)
328     */
329    public void setMeasures(List measures) {
330       this.measures = measures; 
331    }
332 
333    /* (non-Javadoc)
334     * @see ca.spaz.cron.database.Food#getSourceUID()
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  * Example XML:
355  * 
356  * <food name="apple, raw" category="Fruits and Vegetables" source="USDA sr17">
357  * <weights> <weight name="cup" grams="50" multiplier="1.0"> </weights>
358  * <nutrient name="magnesium" amount="20"/> </food>
359  * 
360  * <nutrientinfo name="magnesium" category="Minerals" units="mg" rda_per_pound_male="1000"/>
361  *  
362  */