View Javadoc

1   /*
2    * Created on Apr 2, 2005 by davidson
3    */
4   package ca.spaz.cron.database;
5   
6   import java.util.*;
7   
8   import org.apache.log4j.Logger;
9   
10  import ca.spaz.cron.datasource.ILocalFoodDatasource;
11  
12  /***
13   * Stores an amount and time of a food serving
14   * 
15   * @author davidson
16   */
17  public class Serving {
18      /***
19       * Logger for this class
20       */
21      private static final Logger logger = Logger.getLogger(Serving.class);
22  
23      private int ID = -1;
24      
25      private Food food;
26  
27      private double grams;
28      private Measure measure = Measure.GRAM;
29      
30      private Date date;
31  
32      private int meal;
33  
34      public Serving() {}
35      
36      public Serving(Food f) {
37          this(f, 1.0);
38      }
39  
40      public Serving(Food f, double grams) {
41          this.food = f;
42          this.grams = grams;
43          this.date = new Date(System.currentTimeMillis());
44          this.meal = -1;
45      }
46  
47      
48      public double getGrams() {
49          return grams;
50      }
51      
52  
53      public double getAmount() {
54         assert(getMeasure() != null);
55         return getGrams()/getMeasure().getGrams();
56      }
57  
58  
59      public void setAmount(double val) {
60         assert(getMeasure() != null);
61         // TODO: verify this is correct with measures that have Amount != 1
62         setGrams(getMeasure().getGrams() * val);
63      }
64  
65      public Food getFood() {
66          return food;
67      }
68  
69      public Date getDate() {
70          return date;
71      }
72  
73      public void setDate(Date d) {
74          this.date = d;
75      }
76  
77      /***
78       * @return true if the object is already in the database
79       */
80      public boolean exists() {
81          return ID != -1;
82      }
83  
84      /***
85       * Add the current food to the current day
86       */
87  /*    public void addToDatabase() {
88          ILocalFoodDatasource lds = (ILocalFoodDatasource) getFood().getDataSource();
89          lds.consumeFood(this);
90      }
91  */
92      /***
93       * Update the existing food information
94       */
95      public void updateDatabase() {
96          ILocalFoodDatasource lds = (ILocalFoodDatasource) getFood().getDataSource();
97          lds.changeServingAmount(this);
98      }
99  
100     public void deleteFromDatabase() {
101         ILocalFoodDatasource lds = (ILocalFoodDatasource) getFood().getDataSource();
102         lds.removeServing(this);
103     }
104 
105     public void setGrams(double amount) {
106         this.grams = amount;
107     }
108 
109     /***
110      * @return the ID of the meal this serving was eaten at.
111      */
112     public int getMeal() {
113         return meal;
114     }
115 
116     /***
117      * @return Returns the iD.
118      */
119     public int getID() {
120         return ID;
121     }
122     /***
123      * @param id The ID to set.
124      */
125     public void setID(int id) {
126         ID = id;
127     }
128     /***
129      * @return Returns the measure.
130      */
131     public Measure getMeasure() {
132         return measure;
133     }
134     /***
135      * @param measure The measure to set.
136      */
137     public void setMeasure(Measure measure) {
138         this.measure = measure;
139     }
140     /***
141      * @param meal The meal to set.
142      */
143     public void setMeal(int meal) {
144         this.meal = meal;
145     }
146 
147     /***
148      * @param food
149      */
150     public void setFood(Food food) {
151         this.food = food;
152         // map existing measure to a matching measure in this food
153         // this is a hack to work around converting from a food
154         // in one datasource to another...
155         if (food == null) return;
156         List measures = getFood().getMeasures();
157         if (measures == null) return;
158         for (int i=0; i<measures.size(); i++) {
159            Measure m = (Measure)measures.get(i);
160            if (m == measure) return;
161            if (m.getGrams() == measure.getGrams()) {
162               if (m.getDescription().equals(measure.getDescription())) {
163                  setMeasure(m);
164               }
165            }
166         } 
167     }
168 
169    public void setMeasure(int MID) {
170       List measures = getFood().getMeasures();
171       for (int i=0; i<measures.size(); i++) {
172          Measure m = (Measure)measures.get(i);
173          if (m.getID() == MID) setMeasure(m);
174       }      
175    }
176 }