KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > core > impl > user > UserImpl


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.core.impl.user;
10
11 import java.util.Date JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.HashSet JavaDoc;
14 import java.util.Map JavaDoc;
15 import java.util.Set JavaDoc;
16 import java.util.Calendar JavaDoc;
17 import java.util.Iterator JavaDoc;
18
19 import org.apache.log4j.Logger;
20 import org.jboss.portal.core.impl.preferences.MappedPreferenceSet;
21 import org.jboss.portal.core.impl.preferences.MappedPreferenceStore;
22 import org.jboss.portal.core.model.PropertyMap;
23 import org.jboss.portal.core.model.User;
24 import org.jboss.portal.core.model.Role;
25 import org.jboss.portal.core.CoreConstants;
26 import org.jboss.portal.core.security.AuthorizationRealm;
27 import org.jboss.portal.server.plugins.preferences.PreferenceStore;
28
29 /**
30  * User interface implementation.
31  *
32  * @hibernate.class table="jbp_users"
33  *
34  * @author <a HREF="mailto:julien@jboss.org">Julien Viet </a>
35  * @author <a HREF="mailto:theute@jboss.org">Thomas Heute </a>
36  * @version $Revision: 1.11 $
37  */

38 public class UserImpl
39       implements User
40 {
41
42    private static final Logger log = Logger.getLogger(UserImpl.class);
43
44    private Integer JavaDoc id;
45
46    public String JavaDoc userName;
47
48    public String JavaDoc givenName;
49
50    public String JavaDoc familyName;
51
52    public String JavaDoc realEmail;
53
54    public String JavaDoc fakeEmail;
55
56    public boolean viewRealEmail;
57
58    public String JavaDoc password;
59
60    public boolean enabled = false;
61
62    private UserPrefSet preferenceSet;
63
64    private Map JavaDoc dynamic;
65
66    private PropertyMap properties;
67
68    private Set JavaDoc roles;
69
70    private Date JavaDoc registrationDate;
71
72    private Set JavaDoc roleNames;
73
74    /**
75     * The preference set store.
76     */

77    private final MappedPreferenceStore store = new MappedPreferenceStore()
78    {
79       protected MappedPreferenceSet getRoot()
80       {
81          if (preferenceSet == null)
82          {
83             if (log.isDebugEnabled())
84             {
85                log.debug("No root - create a new one");
86             }
87             preferenceSet = new UserPrefSet("root");
88          }
89          return preferenceSet;
90       }
91    };
92
93    /**
94     * Called by hibernate.
95     */

96    public UserImpl()
97    {
98       this.id = null;
99       this.userName = null;
100       this.dynamic = new HashMap JavaDoc();
101       this.properties = new PropertyMapImpl(this);
102       this.roles = new HashSet JavaDoc();
103       this.registrationDate = new Date JavaDoc();
104    }
105
106    public UserImpl(String JavaDoc userName)
107    {
108       this.id = null;
109       this.userName = userName;
110       this.dynamic = new HashMap JavaDoc();
111       this.properties = new PropertyMapImpl(this);
112       this.roles = new HashSet JavaDoc();
113       this.registrationDate = new Date JavaDoc();
114    }
115
116    /**
117     * Called by hibernate.
118     */

119    private void setID(Integer JavaDoc id)
120    {
121       this.id = id;
122    }
123
124    /**
125     * Called by hibernate.
126     */

127    private void setUserName(String JavaDoc userName)
128    {
129       this.userName = userName;
130    }
131
132    public void setGivenName(String JavaDoc givenName)
133    {
134       this.givenName = givenName;
135    }
136
137    public void setFamilyName(String JavaDoc familyName)
138    {
139       this.familyName = familyName;
140    }
141
142    /**
143     * @hibernate.many-to-one
144     * class="org.jboss.portal.core.impl.user.UserPrefSet"
145     * column="jbp_root_pref_set_id"
146     * cascade="all"
147     */

148    private UserPrefSet getRootPreferenceSet()
149    {
150       // unique = true
151
return preferenceSet;
152    }
153
154    private void setRootPreferenceSet(UserPrefSet preferenceSet)
155    {
156       this.preferenceSet = preferenceSet;
157    }
158
159    /**
160     * @hibernate.map
161     * table="jbp_user_prop"
162     * cascade="all"
163     * @hibernate.collection-key
164     * column="jbp_uid"
165     * @hibernate.collection-index
166     * column="jbp_name"
167     * type="string"
168     * @hibernate.collection-element
169     * column="jbp_value"
170     * type="string"
171     */

172    public Map JavaDoc getDynamic()
173    {
174       return dynamic;
175    }
176
177    /**
178     * Called by Hibernate.
179     */

180    private void setDynamic(Map JavaDoc dynamic)
181    {
182       this.dynamic = dynamic;
183    }
184
185    /**
186     * Called by Hibernate.
187     */

188    public void setRoles(Set JavaDoc roles)
189    {
190       this.roles = roles;
191       this.roleNames = null;
192    }
193
194    public Set JavaDoc getRoleNames()
195    {
196       if (roleNames == null)
197       {
198          Iterator JavaDoc it = roles.iterator();
199          roleNames = new HashSet JavaDoc();
200          while (it.hasNext())
201          {
202             roleNames.add(((Role)it.next()).getName());
203          }
204       }
205       return roleNames;
206    }
207
208    // User implementation
209
// **********************************************************************************************
210

211    /**
212     * @hibernate.id
213     * column="jbp_uid"
214     * generator-class="native"
215     */

216    public Integer JavaDoc getID()
217    {
218       return id;
219    }
220
221    /**
222     * @hibernate.property
223     * column="jbp_uname"
224     * unique="true"
225     * update="false"
226     */

227    public String JavaDoc getUserName()
228    {
229       return userName;
230    }
231
232    /**
233     * @hibernate.property
234     * column="jbp_givenname"
235     * unique="false"
236     * update="true"
237     */

238    public String JavaDoc getGivenName()
239    {
240       return givenName;
241    }
242
243    /**
244     * @hibernate.property
245     * column="jbp_familyname"
246     * unique="false"
247     * update="true"
248     */

249    public String JavaDoc getFamilyName()
250    {
251       return familyName;
252    }
253
254    public void setPassword(String JavaDoc password)
255    {
256       this.password = password;
257    }
258
259    /**
260     * @hibernate.property
261     * column="jbp_password"
262     * unique="false"
263     * update="true"
264     */

265    public String JavaDoc getPassword()
266    {
267       return password;
268    }
269
270    /**
271     * @hibernate.property
272     * column="jbp_realemail"
273     * unique="false"
274     * update="true"
275     */

276    public String JavaDoc getRealEmail()
277    {
278       return realEmail;
279    }
280
281    public void setRealEmail(String JavaDoc realEmail)
282    {
283       this.realEmail = realEmail;
284    }
285
286    /**
287     * @hibernate.property
288     * column="jbp_fakeemail"
289     * unique="false"
290     * update="true"
291     */

292    public String JavaDoc getFakeEmail()
293    {
294       return fakeEmail;
295    }
296
297    public void setFakeEmail(String JavaDoc fakeEmail)
298    {
299       this.fakeEmail = fakeEmail;
300    }
301
302    /**
303     * @hibernate.property
304     * column="jbp_regdate"
305     * unique="false"
306     * update="false"
307     */

308    public Date JavaDoc getRegistrationDate()
309    {
310       return registrationDate;
311    }
312
313    public void setRegistrationDate(Date JavaDoc registrationDate)
314    {
315       this.registrationDate = registrationDate;
316    }
317
318    /**
319     * @hibernate.property
320     * column="jbp_viewrealemail"
321     * unique="false"
322     * update="true"
323     */

324    public boolean getViewRealEmail()
325    {
326       return viewRealEmail;
327    }
328
329    public void setViewRealEmail(boolean viewRealEmail)
330    {
331       this.viewRealEmail = viewRealEmail;
332    }
333
334    public void setEnabled(boolean enable)
335    {
336       this.enabled = enable;
337    }
338
339    /**
340     * @hibernate.property
341     * column="jbp_enabled"
342     * unique="false"
343     * update="true"
344     */

345    public boolean getEnabled()
346    {
347       return enabled;
348    }
349
350    public PropertyMap getProperties()
351    {
352       return properties;
353    }
354
355    public PreferenceStore getPreferenceStore()
356    {
357       return store;
358    }
359
360    /**
361     * @hibernate.set
362     * inverse="false"
363     * table="jbp_role_membership"
364     * lazy="false"
365     * cascade="none"
366     * @hibernate.collection-key
367     * column="jbp_uid"
368     * @hibernate.collection-many-to-many
369     * column="jbp_rid"
370     * class="org.jboss.portal.core.impl.role.RoleImpl"
371     * outer-join="true"
372     */

373    public Set JavaDoc getRoles()
374    {
375       return roles;
376    }
377
378    public String JavaDoc toString()
379    {
380       return "User[" + id + "," + userName + "]";
381    }
382    
383    public Date JavaDoc getLastVisitDate()
384    {
385       Date JavaDoc date = null;
386       String JavaDoc dateAsString = (String JavaDoc)getDynamic().get(CoreConstants.INFO_USER_LAST_LOGIN_DATE);
387       if (dateAsString != null)
388       {
389          try
390          {
391             long dateAsLong = Long.parseLong(dateAsString);
392             date = new Date JavaDoc(dateAsLong);
393          }
394          catch (NumberFormatException JavaDoc e)
395          {
396             log.error("Bad date format " + dateAsString + " try to remove it", e);
397             getDynamic().remove(CoreConstants.INFO_USER_LAST_LOGIN_DATE);
398          }
399       }
400       return date;
401    }
402    
403    public void setLastVisitDate(Date JavaDoc date)
404    {
405       if (date == null)
406       {
407          getDynamic().remove(CoreConstants.INFO_USER_LAST_LOGIN_DATE);
408       }
409       else
410       {
411          long dateAsLong = date.getTime();
412          String JavaDoc dateAsString = Long.toString(dateAsLong);
413          getDynamic().put(CoreConstants.INFO_USER_LAST_LOGIN_DATE, dateAsString);
414       }
415    }
416
417    public String JavaDoc getSignature()
418    {
419       return (String JavaDoc)getDynamic().get(CoreConstants.INFO_USER_SIGNATURE);
420    }
421 }
422
Popular Tags