1
2
3
4
5
6
7
8
9
10
11
12 package ca.spaz.cron.user.impl;
13
14 import org.apache.log4j.Logger;
15
16 import java.util.*;
17
18 import ca.spaz.cron.CRONConfiguration;
19 import ca.spaz.cron.config.*;
20 import ca.spaz.cron.database.NutrientInfo;
21 import ca.spaz.cron.targets.Target;
22 import ca.spaz.cron.user.*;
23
24 /***
25 * A CRONOMETER-specific, property-based <code>User</code> implementation.
26 * @author Chris Rose
27 */
28 public class CRONUser implements User {
29
30 private static final String CU_BASE = "cron.user.";
31 private static final String CU_FIRST_RUN = CU_BASE + "firstrun";
32 private static final String CU_PREF_BASE = CU_BASE + "pref.";
33 private static final String CU_HEIGHT = CU_BASE + "height";
34 private static final String CU_NAME = CU_BASE + "name";
35 private static final String CU_MALE = CU_BASE + "male";
36 private static final String CU_BD_DAY = CU_BASE + "birthdate.day";
37 private static final String CU_BD_MONTH = CU_BASE + "birthdate.month";
38 private static final String CU_BD_YEAR = CU_BASE + "birthdate.year";
39 private static final String CU_TARGET = CU_BASE + "target.";
40
41 /***
42 * Logger for this class
43 */
44 private static final Logger logger = Logger.getLogger(CRONUser.class);
45
46 /***
47 * A simple validator for user properties.
48 * @author Chris Rose
49 */
50 public static class UserPropertyValidator extends AbstractRegexKeyValidator {
51 /***
52 * Logger for this class
53 */
54 private static final Logger logger = Logger
55 .getLogger(UserPropertyValidator.class);
56
57
58
59
60 protected boolean doIsValid(String key, String value) {
61 return true;
62 }
63
64
65
66
67 protected String getExpression() {
68 return "^cron.user.pref";
69 }
70
71 }
72
73 private static CRONUser instance = null;
74 private CRONConfiguration cfg;
75 private List listeners;
76 private static final PropertyValidator USER_PROPERTY_VALIDATOR = new UserPropertyValidator();
77 private Double height;
78 private Date birthDate;
79
80 public static final User getUser() {
81 if (null == instance) {
82 instance = new CRONUser();
83 }
84 return instance;
85 }
86
87 private CRONUser() {
88 this.cfg = CRONConfiguration.getInstance();
89 height = new Double(Double.NaN);
90 birthDate = new Date(0);
91 }
92
93
94
95
96 public String getName() {
97 return cfg.getProperty(CU_NAME, "John Public");
98 }
99
100
101
102
103
104 public void setName(String name) {
105 cfg.setProperty(CU_NAME, name);
106 notifyListeners();
107 }
108
109
110
111
112 public Date getBirthDate() {
113 if (null == birthDate) {
114 Calendar cal = Calendar.getInstance();
115 cal.set(Calendar.YEAR, Integer.valueOf(cfg.getProperty(CU_BD_YEAR)).intValue());
116 cal.set(Calendar.MONTH, Integer.valueOf(cfg.getProperty(CU_BD_MONTH)).intValue());
117 cal.set(Calendar.DAY_OF_MONTH, Integer.valueOf(cfg.getProperty(CU_BD_DAY)).intValue());
118 birthDate = new Date(cal.getTimeInMillis());
119 }
120 return birthDate;
121 }
122
123
124
125
126 public void setBirthDate(Date date) {
127 if (null != date) {
128 Calendar cal = Calendar.getInstance();
129 cal.setTime(date);
130 cfg.setProperty(CU_BD_YEAR, "" + cal.get(Calendar.YEAR));
131 cfg.setProperty(CU_BD_MONTH, "" + cal.get(Calendar.MONTH));
132 cfg.setProperty(CU_BD_DAY, "" + cal.get(Calendar.DAY_OF_MONTH));
133 }
134 birthDate = date;
135 notifyListeners();
136 }
137
138
139
140
141 public Integer getAge() {
142 Integer age;
143 if (null != getBirthDate()) {
144 Calendar bdcal = Calendar.getInstance();
145 bdcal.setTime(getBirthDate());
146 Calendar cal = Calendar.getInstance();
147 age = new Integer(cal.get(Calendar.YEAR) - bdcal.get(Calendar.YEAR));
148 } else {
149 age = new Integer(0);
150 }
151 return age;
152 }
153
154
155
156
157 public Double getHeightInCM() {
158 if (null == height) {
159 String h = cfg.getProperty(CU_HEIGHT);
160 try {
161 height = new Double(h);
162 } catch (NumberFormatException e) {
163 height = null;
164 }
165 }
166 return height;
167 }
168
169
170
171
172 public void setHeightInCM(Double height) {
173 this.height = null;
174 cfg.setProperty(CU_HEIGHT, height.toString());
175 notifyListeners();
176 }
177
178
179
180
181 public String getUserPreference(String prefName) {
182 return cfg.getProperty(CU_PREF_BASE + prefName);
183 }
184
185
186
187
188 public void setUserPreference(String prefName, String value) {
189 String old = null;
190 synchronized(cfg) {
191 cfg.addPropertyValidator(USER_PROPERTY_VALIDATOR);
192 old = cfg.setProperty(CU_PREF_BASE + prefName, value);
193 cfg.removePropertyValidator(USER_PROPERTY_VALIDATOR);
194 }
195 if (!old.equals(value)) {
196 notifyListeners();
197 }
198 }
199
200
201
202
203 public List getUserMetrics() {
204 logger.error("getUserMetrics() - Not implemented", null);
205 return null;
206 }
207
208
209
210
211 public List getUserMetrics(Date date) {
212 logger.error("getUserMetrics(Date) - Not implemented", null);
213 return null;
214 }
215
216
217
218
219 public void addUserMetrics(Date date, UserMetrics metrics) {
220 logger.error("addUserMetrics(Date, UserMetrics) - Not implemented", null);
221 }
222
223
224
225
226 public void addUserMetrics(UserMetrics metrics) {
227 logger.error("addUserMetrics(UserMetrics) - Not implemented", null);
228 }
229
230
231
232
233 public void removeUserMetrics(UserMetrics metrics) {
234 logger.error("removeUserMetrics(UserMetrics) - Not implemented", null);
235 }
236
237
238
239
240 public void setTarget(NutrientInfo nutrient, Target target) {
241
242 cfg.setProperty(CU_TARGET+nutrient.getTag()+".min", Double.toString(target.getMin()));
243 cfg.setProperty(CU_TARGET+nutrient.getTag()+".max", Double.toString(target.getMax()));
244 notifyListeners();
245 }
246
247
248
249
250 public Target getTarget(NutrientInfo nutrient) {
251 if (nutrient == null) return null;
252 double min = Double.parseDouble(cfg.getProperty(CU_TARGET+nutrient.getTag()+".min", "0"));
253 double max = Double.parseDouble(cfg.getProperty(CU_TARGET+nutrient.getTag()+".max", "0"));
254
255 return new Target(min,max);
256 }
257
258
259
260
261 public final void addUserChangeListener(UserChangeListener l) {
262 getListeners().add(l);
263 }
264
265 /***
266 * @return
267 */
268 private List getListeners() {
269 if (null == listeners) {
270 listeners = new ArrayList();
271 }
272 return listeners;
273 }
274
275 protected final void notifyListeners() {
276 List l = getListeners();
277 for (Iterator iter = l.iterator(); iter.hasNext();) {
278 UserChangeListener listener = (UserChangeListener) iter.next();
279 listener.userChanged(this);
280 }
281 }
282
283
284
285
286 public final void removeUserChangeListener(UserChangeListener l) {
287 getListeners().remove(l);
288 }
289
290 public boolean isMale() {
291 return cfg.getProperty(CU_MALE, "true").equals("true");
292 }
293
294 public boolean isFemale() {
295 return !isMale();
296 }
297
298 public void setGender(boolean male) {
299 cfg.setProperty(CU_MALE, Boolean.toString(male));
300 notifyListeners();
301 }
302
303 public boolean firstRun() {
304 return cfg.getProperty(CU_FIRST_RUN, "true").equals("true");
305 }
306
307 public void setFirstRun(boolean val) {
308 cfg.setProperty(CU_FIRST_RUN, Boolean.toString(val));
309 notifyListeners();
310 }
311
312
313 }