1
2
3
4 package ca.spaz.cron.database;
5
6 import java.lang.reflect.Field;
7
8 import org.apache.log4j.Logger;
9
10 import ca.spaz.cron.datasource.ILocalFoodDatasource;
11
12 /***
13 * A base class for a group of related nutrient values Provides common support
14 * for loading and updating the database.
15 *
16 * The nutrient table is expected to have field names that match the names and
17 * types of the corresponding database table. This greatly simplifies
18 * maintaining the database and generating UI forms through reflection.
19 *
20 * @author davidson
21 */
22 public abstract class NutrientTable {
23 /***
24 * Logger for this class
25 */
26 private static final Logger logger = Logger.getLogger(NutrientTable.class);
27 private Food food;
28
29 public NutrientTable() {
30 }
31
32 public NutrientTable(Food f) {
33 this.food = f;
34 f.getDataSource().getNutrientsFor(f, this);
35 }
36
37 /***
38 * Get the name of the table corresponding to this class. By default it uses
39 * the class name. Override this method if it is different.
40 *
41 * @return the name of the database table for this object
42 */
43 public String getTableName() {
44 return doGetTableName();
45 }
46
47 /***
48 * Subclasses must implement this in order to supply their table name.
49 *
50 * @return the table name to perform lookups on.
51 */
52 protected abstract String doGetTableName();
53
54 /***
55 * Add the nutrients in the given table to our total
56 *
57 * @param toAdd
58 * the nutrients ratios to add
59 * @param weight
60 * multiplier for the amount in the added nutrients
61 */
62 public void addFood(NutrientTable toAdd, double weight) {
63 assert (toAdd.getClass() == this.getClass());
64 Field[] fields = this.getClass().getFields();
65 for (int i = 0; i < fields.length; i++) {
66 try {
67 double d1 = fields[i].getDouble(this);
68 double d2 = fields[i].getDouble(toAdd);
69 fields[i].setDouble(this, d1 + d2 * weight);
70 } catch (Exception e) {
71 logger.error("addFood(NutrientTable, double)", e);
72 }
73 }
74 }
75
76 /***
77 * Get the amount of a nutrient by the tag name
78 *
79 * @param tag
80 * the tag name of the nutrient
81 * @return the amount of the nutrient, or 0 if none found
82 */
83 public double getAmountByTag(String tag) {
84 double val = 0;
85 try {
86 Field f = this.getClass().getField(tag);
87 val = f.getDouble(this);
88 } catch (Exception e) {
89 logger.error("getAmountByTag(String)", e);
90 }
91 return val;
92 }
93
94 /***
95 * Set the amount of a nutrient by the tag name
96 *
97 * @param tag the tag name of the nutrient
98 * @param val the amount of the nutrient, or 0 if none found
99 */
100 public void setAmountByTag(String tag, double val) {
101 try {
102 Field f = this.getClass().getField(tag);
103 f.setDouble(this, val);
104 } catch (Exception e) {
105 logger.error("setAmountByTag(String, double)", e);
106 }
107 }
108
109 /***
110 * Update the existing food information
111 */
112 public void updateDatabase() {
113 ILocalFoodDatasource lds = (ILocalFoodDatasource) food.getDataSource();
114 lds.saveFood(food);
115 }
116
117 }