View Javadoc

1   /*
2    *******************************************************************************
3    * Copyright (c) 2005 Chris Rose and AIMedia
4    * All rights reserved. CRONUser and the accompanying materials
5    * are made available under the terms of the Common Public License v1.0
6    * which accompanies this distribution, and is available at
7    * http://www.eclipse.org/legal/cpl-v10.html
8    * 
9    * Contributors:
10   *     Chris Rose
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        /* (non-Javadoc)
58         * @see ca.spaz.cron.config.AbstractRegexKeyValidator#doIsValid(java.lang.String, java.lang.String)
59         */
60        protected boolean doIsValid(String key, String value) {
61           return true;
62        }
63  
64        /* (non-Javadoc)
65         * @see ca.spaz.cron.config.AbstractRegexKeyValidator#getExpression()
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     /* (non-Javadoc)
94      * @see ca.spaz.cron.user.User#getFirstName()
95      */
96     public String getName() {
97        return cfg.getProperty(CU_NAME, "John Public");
98     }
99  
100 
101    /* (non-Javadoc)
102     * @see ca.spaz.cron.user.User#setName(java.lang.String)
103     */
104    public void setName(String name) {
105       cfg.setProperty(CU_NAME, name);
106       notifyListeners();
107    }
108 
109    /* (non-Javadoc)
110     * @see ca.spaz.cron.user.User#getBirthDate()
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    /* (non-Javadoc)
124     * @see ca.spaz.cron.user.User#setBirthDate(java.util.Date)
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    /* (non-Javadoc)
139     * @see ca.spaz.cron.user.User#getAge()
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    /* (non-Javadoc)
155     * @see ca.spaz.cron.user.User#getHeightInCM()
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    /* (non-Javadoc)
170     * @see ca.spaz.cron.user.User#setHeightInCM(java.lang.Double)
171     */
172    public void setHeightInCM(Double height) {
173       this.height = null;
174       cfg.setProperty(CU_HEIGHT, height.toString());
175       notifyListeners();
176    }
177 
178    /* (non-Javadoc)
179     * @see ca.spaz.cron.user.User#getUserPreference(java.lang.String)
180     */
181    public String getUserPreference(String prefName) {
182       return cfg.getProperty(CU_PREF_BASE + prefName);
183    }
184 
185    /* (non-Javadoc)
186     * @see ca.spaz.cron.user.User#setUserPreference(java.lang.String, java.lang.String)
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    /* (non-Javadoc)
201     * @see ca.spaz.cron.user.User#getUserMetrics()
202     */
203    public List getUserMetrics() {
204       logger.error("getUserMetrics() - Not implemented", null);
205       return null;
206    }
207 
208    /* (non-Javadoc)
209     * @see ca.spaz.cron.user.User#getUserMetrics(java.util.Date)
210     */
211    public List getUserMetrics(Date date) {
212       logger.error("getUserMetrics(Date) - Not implemented", null);
213       return null;
214    }
215 
216    /* (non-Javadoc)
217     * @see ca.spaz.cron.user.User#addUserMetrics(java.util.Date, ca.spaz.cron.user.UserMetrics)
218     */
219    public void addUserMetrics(Date date, UserMetrics metrics) {
220       logger.error("addUserMetrics(Date, UserMetrics) - Not implemented", null);
221    }
222 
223    /* (non-Javadoc)
224     * @see ca.spaz.cron.user.User#addUserMetrics(ca.spaz.cron.user.UserMetrics)
225     */
226    public void addUserMetrics(UserMetrics metrics) {
227       logger.error("addUserMetrics(UserMetrics) - Not implemented", null);
228    }
229 
230    /* (non-Javadoc)
231     * @see ca.spaz.cron.user.User#removeUserMetrics(ca.spaz.cron.user.UserMetrics)
232     */
233    public void removeUserMetrics(UserMetrics metrics) {
234       logger.error("removeUserMetrics(UserMetrics) - Not implemented", null);
235    }
236 
237    /* (non-Javadoc)
238     * @see ca.spaz.cron.user.User#setTarget(ca.spaz.cron.database.NutrientInfo, ca.spaz.cron.targets.Target)
239     */
240    public void setTarget(NutrientInfo nutrient, Target target) {
241       //logger.error("setTarget(NutrientInfo, Target) - Not implemented", null);
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    /* (non-Javadoc)
248     * @see ca.spaz.cron.user.User#getTarget(ca.spaz.cron.database.NutrientInfo)
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       // TODO: most likely want to cache this instead of constantly pulling it out of the properties      
255       return new Target(min,max);
256    }
257 
258    /* (non-Javadoc)
259     * @see ca.spaz.cron.user.User#addUserChangeListener(ca.spaz.cron.user.UserChangeListener)
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    /* (non-Javadoc)
284     * @see ca.spaz.cron.user.User#removeUserChangeListener(ca.spaz.cron.user.UserChangeListener)
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 }