KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > pojos > UserData


1
2 package org.roller.pojos;
3
4 import java.util.Date JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.Set JavaDoc;
7 import java.util.TreeSet JavaDoc;
8
9 import org.roller.RollerException;
10 import org.roller.config.RollerConfig;
11 import org.roller.model.Roller;
12 import org.roller.model.RollerFactory;
13 import org.roller.model.UserManager;
14 import org.roller.util.Utilities;
15
16
17 /**
18  * User bean.
19  * @author David M Johnson
20  *
21  * @ejb:bean name="UserData"
22  * @struts.form include-all="true"
23  * @hibernate.class table="rolleruser"
24  * hibernate.jcs-cache usage="read-write"
25  */

26 public class UserData
27    extends org.roller.pojos.PersistentObject
28    implements java.io.Serializable JavaDoc
29 {
30     public static final UserData SYSTEM_USER =
31         new UserData("n/a","systemuser","n/a","systemuser","n/a",new Date JavaDoc());
32     
33     public static final UserData ANONYMOUS_USER =
34         new UserData("n/a","anonymoususer","n/a","anonymoususer","n/a",new Date JavaDoc());
35    
36    static final long serialVersionUID = -6354583200913127874L;
37
38    protected java.lang.String JavaDoc id;
39    protected java.lang.String JavaDoc userName;
40    protected java.lang.String JavaDoc password;
41    protected java.lang.String JavaDoc fullName;
42    protected java.lang.String JavaDoc emailAddress;
43    protected java.util.Date JavaDoc dateCreated;
44    private Set JavaDoc roles = new TreeSet JavaDoc();
45
46    public UserData()
47    {
48    }
49
50     public UserData( java.lang.String JavaDoc id, java.lang.String JavaDoc userName,
51                      java.lang.String JavaDoc password, java.lang.String JavaDoc fullName,
52                      java.lang.String JavaDoc emailAddress, java.util.Date JavaDoc dateCreated)
53     {
54         this.id = id;
55         this.userName = userName;
56         this.password = password;
57         this.fullName = fullName;
58         this.emailAddress = emailAddress;
59         this.dateCreated = (Date JavaDoc)dateCreated.clone();
60     }
61
62     public UserData( UserData otherData )
63     {
64         this.id = otherData.id;
65         this.userName = otherData.userName;
66         this.password = otherData.password;
67         this.fullName = otherData.fullName;
68         this.emailAddress = otherData.emailAddress;
69         this.dateCreated = (Date JavaDoc)otherData.dateCreated.clone();
70
71     }
72
73    /** Id of the User.
74      * Not remote since primary key may be extracted by other means.
75      *
76      * @struts.validator type="required" msgkey="errors.required"
77      * @ejb:persistent-field
78      * @hibernate.id column="id" type="string"
79      * generator-class="uuid.hex" unsaved-value="null"
80      */

81    public java.lang.String JavaDoc getId()
82    {
83       return this.id;
84    }
85    /** @ejb:persistent-field */
86    public void setId( java.lang.String JavaDoc id )
87    {
88       this.id = id;
89    }
90
91    /** User name of the user.
92      * @ejb:persistent-field
93      * @hibernate.property column="username" non-null="true" unique="true"
94      */

95    public java.lang.String JavaDoc getUserName()
96    {
97       return this.userName;
98    }
99    /** @ejb:persistent-field */
100    public void setUserName( java.lang.String JavaDoc userName )
101    {
102       this.userName = userName;
103    }
104
105    /**
106     * Get password.
107     * If password encryption is enabled, will return encrypted password.
108     * @ejb:persistent-field
109     * @hibernate.property column="passphrase" non-null="true"
110     */

111    public java.lang.String JavaDoc getPassword()
112    {
113       return this.password;
114    }
115    /**
116     * Set password.
117     * If password encryption is turned on, then pass in an encrypted password.
118     * @ejb:persistent-field
119     */

120    public void setPassword( java.lang.String JavaDoc password )
121    {
122       this.password = password;
123    }
124
125    /** Full name of the user.
126      * @ejb:persistent-field
127      * @hibernate.property column="fullname" non-null="true" unique="true"
128      */

129    public java.lang.String JavaDoc getFullName()
130    {
131       return this.fullName;
132    }
133    /** @ejb:persistent-field */
134    public void setFullName( java.lang.String JavaDoc fullName )
135    {
136       this.fullName = fullName;
137    }
138
139    /** E-mail address of the user.
140      * @ejb:persistent-field
141      * @hibernate.property column="emailaddress" non-null="true" unique="true"
142      */

143    public java.lang.String JavaDoc getEmailAddress()
144    {
145       return this.emailAddress;
146    }
147    /** @ejb:persistent-field */
148    public void setEmailAddress( java.lang.String JavaDoc emailAddress )
149    {
150       this.emailAddress = emailAddress;
151    }
152
153    /**
154     * @ejb:persistent-field
155     * @hibernate.property column="datecreated" non-null="true" unique="false"
156     */

157    public java.util.Date JavaDoc getDateCreated()
158    {
159        if (dateCreated == null)
160        {
161            return null;
162        }
163        else
164        {
165            return (Date JavaDoc)dateCreated.clone();
166        }
167    }
168    /** @ejb:persistent-field */
169    public void setDateCreated(final java.util.Date JavaDoc date)
170    {
171        if (date != null)
172        {
173            dateCreated = (Date JavaDoc)date.clone();
174        }
175        else
176        {
177            dateCreated = null;
178        }
179    }
180
181    public String JavaDoc toString()
182    {
183         StringBuffer JavaDoc str = new StringBuffer JavaDoc("{");
184
185         str.append("id=" + id + " ");
186         str.append("userName=" + userName + " ");
187         str.append("password=" + password + " ");
188         str.append("fullName=" + fullName + " ");
189         str.append("emailAddress=" + emailAddress + " ");
190         str.append("dateCreated=" + dateCreated + " ");
191         str.append('}');
192
193         return(str.toString());
194    }
195
196    public boolean equals( Object JavaDoc pOther )
197    {
198       if( pOther instanceof UserData )
199       {
200          UserData lTest = (UserData) pOther;
201          boolean lEquals = true;
202
203          if( this.id == null )
204          {
205             lEquals = lEquals && ( lTest.id == null );
206          }
207          else
208          {
209             lEquals = lEquals && this.id.equals( lTest.id );
210          }
211          if( this.userName == null )
212          {
213             lEquals = lEquals && ( lTest.userName == null );
214          }
215          else
216          {
217             lEquals = lEquals && this.userName.equals( lTest.userName );
218          }
219          if( this.password == null )
220          {
221             lEquals = lEquals && ( lTest.password == null );
222          }
223          else
224          {
225             lEquals = lEquals && this.password.equals( lTest.password );
226          }
227          if( this.fullName == null )
228          {
229             lEquals = lEquals && ( lTest.fullName == null );
230          }
231          else
232          {
233             lEquals = lEquals && this.fullName.equals( lTest.fullName );
234          }
235          if( this.emailAddress == null )
236          {
237             lEquals = lEquals && ( lTest.emailAddress == null );
238          }
239          else
240          {
241             lEquals = lEquals && this.emailAddress.equals( lTest.emailAddress );
242          }
243          
244         if( this.dateCreated == null )
245         {
246            lEquals = lEquals && ( lTest.dateCreated == null );
247         }
248         else
249         {
250            lEquals = lEquals && datesEquivalent(this.dateCreated, lTest.dateCreated);
251         }
252
253         return lEquals;
254       }
255       else
256       {
257          return false;
258       }
259    }
260    
261     private boolean datesEquivalent(Date JavaDoc d1, Date JavaDoc d2)
262     {
263         boolean equiv = true;
264         equiv = equiv && d1.getHours() == d1.getHours();
265         equiv = equiv && d1.getMinutes() == d1.getMinutes();
266         equiv = equiv && d1.getSeconds() == d1.getSeconds();
267         equiv = equiv && d1.getMonth() == d1.getMonth();
268         equiv = equiv && d1.getDay() == d1.getDay();
269         equiv = equiv && d1.getYear() == d1.getYear();
270         return equiv;
271     }
272
273    public int hashCode()
274    {
275       int result = 17;
276       result = 37*result + ((this.id != null) ? this.id.hashCode() : 0);
277       result = 37*result + ((this.userName != null) ? this.userName.hashCode() : 0);
278       result = 37*result + ((this.password != null) ? this.password.hashCode() : 0);
279       result = 37*result + ((this.fullName != null) ? this.fullName.hashCode() : 0);
280       result = 37*result + ((this.emailAddress != null) ? this.emailAddress.hashCode() : 0);
281       result = 37*result + ((this.dateCreated != null) ? this.dateCreated.hashCode() : 0);
282       return result;
283       }
284
285    /**
286     * Setter is needed in RollerImpl.storePersistentObject()
287     */

288    public void setData( org.roller.pojos.PersistentObject otherData )
289    {
290       this.id = ((UserData)otherData).id;
291       this.userName = ((UserData)otherData).userName;
292       this.password = ((UserData)otherData).password;
293       this.fullName = ((UserData)otherData).fullName;
294       this.emailAddress = ((UserData)otherData).emailAddress;
295       this.dateCreated = ((UserData)otherData).dateCreated;
296    }
297
298     /**
299      * Removing a user also removes his/her website.
300      * @see org.roller.pojos.PersistentObject#remove()
301      */

302     public void remove() throws RollerException
303     {
304         UserManager uMgr = RollerFactory.getRoller().getUserManager();
305         uMgr.removeUserWebsites(this);
306         
307         // remove user roles
308
Iterator JavaDoc roles = uMgr.getUserRoles(this).iterator();
309         while (roles.hasNext())
310         {
311             ((RoleData)roles.next()).remove();
312         }
313         
314         super.remove();
315     }
316     
317     /**
318      * Reset this user's password.
319      * @param roller Roller instance to use for configuration information
320      * @param new1 New password
321      * @param new2 Confirm this matches new password
322      * @author Dave Johnson
323      */

324     public void resetPassword(Roller roller, String JavaDoc new1, String JavaDoc new2) throws RollerException
325     {
326         if (!new1.equals(new2))
327         {
328             throw new RollerException("newUser.error.mismatchedPasswords");
329         }
330
331         String JavaDoc encrypt = RollerConfig.getProperty("passwds.encryption.enabled");
332         String JavaDoc algorithm = RollerConfig.getProperty("passwds.encryption.algorithm");
333         if (new Boolean JavaDoc(encrypt).booleanValue())
334         {
335             password = Utilities.encodePassword(new1, algorithm);
336         }
337         else
338         {
339             password = new1;
340         }
341     }
342     
343     /**
344      * @hibernate.set lazy="false" inverse="true" cascade="delete"
345      * @hibernate.collection-key column="userid"
346      * @hibernate.collection-one-to-many class="org.roller.pojos.RoleData"
347      */

348     public Set JavaDoc getRoles()
349     {
350         return roles;
351     }
352     
353     public void setRoles(Set JavaDoc roles)
354     {
355         this.roles = roles;
356     }
357     
358
359     /**
360      * Returns true if user has role specified.
361      * @param roleName Name of role
362      * @return True if user has specified role.
363      */

364     public boolean hasRole(String JavaDoc roleName)
365     {
366         Iterator JavaDoc iter = roles.iterator();
367         while (iter.hasNext())
368         {
369             RoleData role = (RoleData) iter.next();
370             if (role.getRole().equals(roleName))
371             {
372                 return true;
373             }
374         }
375         return false;
376     }
377
378     /**
379      * Revokes specified role from user.
380      * @param roleName Name of role to be revoked.
381      */

382     public void revokeRole(String JavaDoc roleName) throws RollerException
383     {
384         RoleData removeme = null;
385         Iterator JavaDoc iter = roles.iterator();
386         while (iter.hasNext())
387         {
388             RoleData role = (RoleData) iter.next();
389             if (role.getRole().equals(roleName))
390             {
391                 removeme = role;
392             }
393         }
394         if (removeme != null)
395         {
396             roles.remove(removeme);
397             RollerFactory.getRoller().getUserManager().removeRole(removeme.getId());
398         }
399     }
400
401     /**
402      * Grant to user role specified by role name.
403      * @param roleName Name of role to be granted.
404      */

405     public void grantRole(String JavaDoc roleName) throws RollerException
406     {
407         if (!hasRole(roleName))
408         {
409             RoleData role = new RoleData(null, this, roleName);
410             RollerFactory.getRoller().getUserManager().storeRole(role);
411             roles.add(role);
412         }
413     }
414 }
415
Popular Tags