KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */

18
19 package org.apache.roller.pojos;
20
21 import java.io.Serializable JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Date JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Set JavaDoc;
28
29 import org.apache.roller.RollerException;
30 import org.apache.roller.config.RollerConfig;
31 import org.apache.roller.model.Roller;
32 import org.apache.roller.util.PojoUtil;
33 import org.apache.roller.util.Utilities;
34
35
36 /**
37  * User bean.
38  * @author David M Johnson
39  *
40  * @ejb:bean name="UserData"
41  * @struts.form include-all="true"
42  * @hibernate.class lazy="false" table="rolleruser"
43  * @hibernate.cache usage="read-write"
44  */

45 public class UserData
46         extends org.apache.roller.pojos.PersistentObject
47         implements Serializable JavaDoc {
48     public static final UserData SYSTEM_USER = new UserData(
49             "n/a","systemuser","n/a","systemuser","n/a",
50             "en_US_WIN", "America/Los_Angeles", new Date JavaDoc(), Boolean.TRUE);
51     
52     public static final UserData ANONYMOUS_USER = new UserData(
53             "n/a","anonymoususer","n/a","anonymoususer","n/a",
54             "en_US_WIN", "America/Los_Angeles", new Date JavaDoc(), Boolean.TRUE);
55     
56     static final long serialVersionUID = -6354583200913127874L;
57     
58     private String JavaDoc id;
59     private String JavaDoc userName;
60     private String JavaDoc password;
61     private String JavaDoc fullName;
62     private String JavaDoc emailAddress;
63     private Date JavaDoc dateCreated;
64     private String JavaDoc locale;
65     private String JavaDoc timeZone;
66     private Boolean JavaDoc enabled = Boolean.TRUE;
67     
68     private Set JavaDoc roles = new HashSet JavaDoc();
69     private List JavaDoc permissions = new ArrayList JavaDoc();
70     
71     public UserData() {
72     }
73     
74     public UserData( String JavaDoc id, String JavaDoc userName,
75             String JavaDoc password, String JavaDoc fullName,
76             String JavaDoc emailAddress,
77             String JavaDoc locale, String JavaDoc timeZone,
78             Date JavaDoc dateCreated,
79             Boolean JavaDoc isEnabled) {
80         this.id = id;
81         this.userName = userName;
82         this.password = password;
83         this.fullName = fullName;
84         this.emailAddress = emailAddress;
85         this.dateCreated = (Date JavaDoc)dateCreated.clone();
86         this.locale = locale;
87         this.timeZone = timeZone;
88         this.enabled = enabled;
89     }
90     
91     public UserData( UserData otherData ) {
92         setData(otherData);
93     }
94     
95     /**
96      * @hibernate.bag lazy="true" inverse="true" cascade="delete"
97      * @hibernate.collection-key column="user_id"
98      * @hibernate.collection-one-to-many
99      * class="org.apache.roller.pojos.PermissionsData"
100      */

101     public List JavaDoc getPermissions() {
102         return permissions;
103     }
104     public void setPermissions(List JavaDoc perms) {
105         permissions = perms;
106     }
107     
108     /**
109      * @ejb:persistent-field
110      * @hibernate.property column="isenabled" non-null="true" unique="false"
111      */

112     public Boolean JavaDoc getEnabled() {
113         return this.enabled;
114     }
115     
116     /** @ejb:persistent-field */
117     public void setEnabled(Boolean JavaDoc enabled) {
118         this.enabled = enabled;
119     }
120     
121     /** Id of the User.
122      * Not remote since primary key may be extracted by other means.
123      *
124      * @struts.validator type="required" msgkey="errors.required"
125      * @ejb:persistent-field
126      * @hibernate.id column="id"
127      * generator-class="uuid.hex" unsaved-value="null"
128      */

129     public String JavaDoc getId() {
130         return this.id;
131     }
132     
133     /** @ejb:persistent-field */
134     public void setId( String JavaDoc id ) {
135         this.id = id;
136     }
137     
138     /** User name of the user.
139      * @ejb:persistent-field
140      * @hibernate.property column="username" non-null="true" unique="true"
141      * @roller.wrapPojoMethod type="simple"
142      */

143     public String JavaDoc getUserName() {
144         return this.userName;
145     }
146     /** @ejb:persistent-field */
147     public void setUserName( String JavaDoc userName ) {
148         this.userName = userName;
149     }
150     
151     /**
152      * Get password.
153      * If password encryption is enabled, will return encrypted password.
154      *
155      * @ejb:persistent-field
156      * @hibernate.property column="passphrase" non-null="true"
157      */

158     public String JavaDoc getPassword() {
159         return this.password;
160     }
161     /**
162      * Set password.
163      * If password encryption is turned on, then pass in an encrypted password.
164      * @ejb:persistent-field
165      */

166     public void setPassword( String JavaDoc password ) {
167         this.password = password;
168     }
169     
170     /**
171      * Full name of the user.
172      *
173      * @roller.wrapPojoMethod type="simple"
174      * @ejb:persistent-field
175      * @hibernate.property column="fullname" non-null="true" unique="true"
176      */

177     public String JavaDoc getFullName() {
178         return this.fullName;
179     }
180     /** @ejb:persistent-field */
181     public void setFullName( String JavaDoc fullName ) {
182         this.fullName = fullName;
183     }
184     
185     /**
186      * E-mail address of the user.
187      *
188      * @roller.wrapPojoMethod type="simple"
189      * @ejb:persistent-field
190      * @hibernate.property column="emailaddress" non-null="true" unique="true"
191      */

192     public String JavaDoc getEmailAddress() {
193         return this.emailAddress;
194     }
195     /** @ejb:persistent-field */
196     public void setEmailAddress( String JavaDoc emailAddress ) {
197         this.emailAddress = emailAddress;
198     }
199     
200     /**
201      * @roller.wrapPojoMethod type="simple"
202      * @ejb:persistent-field
203      * @hibernate.property column="datecreated" non-null="true" unique="false"
204      */

205     public Date JavaDoc getDateCreated() {
206         if (dateCreated == null) {
207             return null;
208         } else {
209             return (Date JavaDoc)dateCreated.clone();
210         }
211     }
212     /** @ejb:persistent-field */
213     public void setDateCreated(final Date JavaDoc date) {
214         if (date != null) {
215             dateCreated = (Date JavaDoc)date.clone();
216         } else {
217             dateCreated = null;
218         }
219     }
220     
221     /**
222      * Locale of the user.
223      * @ejb:persistent-field
224      * @hibernate.property column="locale" non-null="true" unique="false"
225      * @roller.wrapPojoMethod type="simple"
226      */

227     public String JavaDoc getLocale() {
228         return this.locale;
229     }
230     
231     /** @ejb:persistent-field */
232     public void setLocale(String JavaDoc locale) {
233         this.locale = locale;
234     }
235     
236     /**
237      * Timezone of the user.
238      * @ejb:persistent-field
239      * @hibernate.property column="timeZone" non-null="true" unique="false"
240      * @roller.wrapPojoMethod type="simple"
241      */

242     public String JavaDoc getTimeZone() {
243         return this.timeZone;
244     }
245     
246     /** @ejb:persistent-field */
247     public void setTimeZone(String JavaDoc timeZone) {
248         this.timeZone = timeZone;
249     }
250     
251     //------------------------------------------------------------------- citizenship
252
public String JavaDoc toString() {
253         StringBuffer JavaDoc str = new StringBuffer JavaDoc("{");
254         
255         str.append("id=" + id + " ");
256         str.append("userName=" + userName + " ");
257         str.append("password=" + password + " ");
258         str.append("fullName=" + fullName + " ");
259         str.append("emailAddress=" + emailAddress + " ");
260         str.append("dateCreated=" + dateCreated + " ");
261         str.append('}');
262         
263         return(str.toString());
264     }
265     
266     public boolean equals( Object JavaDoc pOther ) {
267         if (pOther instanceof UserData) {
268             UserData lTest = (UserData) pOther;
269             boolean lEquals = true;
270             lEquals = PojoUtil.equals(lEquals, this.getId(), lTest.getId());
271             lEquals = PojoUtil.equals(lEquals, this.getUserName(), lTest.getUserName());
272             lEquals = PojoUtil.equals(lEquals, this.getPassword(), lTest.getPassword());
273             lEquals = PojoUtil.equals(lEquals, this.getFullName(), lTest.getFullName());
274             lEquals = PojoUtil.equals(lEquals, this.getEmailAddress(), lTest.getEmailAddress());
275             return lEquals;
276         } else {
277             return false;
278         }
279     }
280     
281    /*public boolean equals( Object pOther )
282    {
283       if( pOther instanceof UserData )
284       {
285          UserData lTest = (UserData) pOther;
286          boolean lEquals = true;
287     
288          if( this.id == null )
289          {
290             lEquals = lEquals && ( lTest.id == null );
291          }
292          else
293          {
294             lEquals = lEquals && this.id.equals( lTest.id );
295          }
296          if( this.userName == null )
297          {
298             lEquals = lEquals && ( lTest.userName == null );
299          }
300          else
301          {
302             lEquals = lEquals && this.userName.equals( lTest.userName );
303          }
304          if( this.password == null )
305          {
306             lEquals = lEquals && ( lTest.password == null );
307          }
308          else
309          {
310             lEquals = lEquals && this.password.equals( lTest.password );
311          }
312          if( this.fullName == null )
313          {
314             lEquals = lEquals && ( lTest.fullName == null );
315          }
316          else
317          {
318             lEquals = lEquals && this.fullName.equals( lTest.fullName );
319          }
320          if( this.emailAddress == null )
321          {
322             lEquals = lEquals && ( lTest.emailAddress == null );
323          }
324          else
325          {
326             lEquals = lEquals && this.emailAddress.equals( lTest.emailAddress );
327          }
328     
329                 if( this.dateCreated == null )
330                 {
331                    lEquals = lEquals && ( lTest.dateCreated == null );
332                 }
333                 else
334                 {
335                    lEquals = lEquals && datesEquivalent(this.dateCreated, lTest.dateCreated);
336                 }
337     
338         return lEquals;
339       }
340       else
341       {
342          return false;
343       }
344    }*/

345     
346     private boolean datesEquivalent(Date JavaDoc d1, Date JavaDoc d2) {
347         boolean equiv = true;
348         equiv = equiv && d1.getHours() == d1.getHours();
349         equiv = equiv && d1.getMinutes() == d1.getMinutes();
350         equiv = equiv && d1.getSeconds() == d1.getSeconds();
351         equiv = equiv && d1.getMonth() == d1.getMonth();
352         equiv = equiv && d1.getDay() == d1.getDay();
353         equiv = equiv && d1.getYear() == d1.getYear();
354         return equiv;
355     }
356     
357     public int hashCode() {
358         int result = 17;
359         result = 37*result + ((this.id != null) ? this.id.hashCode() : 0);
360         result = 37*result + ((this.userName != null) ? this.userName.hashCode() : 0);
361         result = 37*result + ((this.password != null) ? this.password.hashCode() : 0);
362         result = 37*result + ((this.fullName != null) ? this.fullName.hashCode() : 0);
363         result = 37*result + ((this.emailAddress != null) ? this.emailAddress.hashCode() : 0);
364         result = 37*result + ((this.dateCreated != null) ? this.dateCreated.hashCode() : 0);
365         return result;
366     }
367     
368     /**
369      * Setter is needed in RollerImpl.storePersistentObject()
370      */

371     public void setData( org.apache.roller.pojos.PersistentObject otherData ) {
372         UserData other = (UserData)otherData;
373         this.id = other.getId();
374         this.userName = other.getUserName();
375         this.password = other.getPassword();
376         this.fullName = other.getFullName();
377         this.emailAddress = other.getEmailAddress();
378         this.locale = other.getLocale();
379         this.timeZone = other.getTimeZone();
380         this.dateCreated = other.getDateCreated()!=null ? (Date JavaDoc)other.getDateCreated().clone() : null;
381     }
382     
383     
384     /**
385      * Reset this user's password.
386      * @param roller Roller instance to use for configuration information
387      * @param new1 New password
388      * @param new2 Confirm this matches new password
389      * @author Dave Johnson
390      */

391     public void resetPassword(Roller roller, String JavaDoc new1, String JavaDoc new2) throws RollerException {
392         if (!new1.equals(new2)) {
393             throw new RollerException("newUser.error.mismatchedPasswords");
394         }
395         
396         String JavaDoc encrypt = RollerConfig.getProperty("passwds.encryption.enabled");
397         String JavaDoc algorithm = RollerConfig.getProperty("passwds.encryption.algorithm");
398         if (new Boolean JavaDoc(encrypt).booleanValue()) {
399             password = Utilities.encodePassword(new1, algorithm);
400         } else {
401             password = new1;
402         }
403     }
404     
405     
406     /**
407      * @hibernate.set lazy="false" inverse="true" cascade="all-delete-orphan"
408      * @hibernate.collection-key column="userid"
409      * @hibernate.collection-one-to-many class="org.apache.roller.pojos.RoleData"
410      */

411     public Set JavaDoc getRoles() {
412         return roles;
413     }
414     
415     /**
416      * this is private to force the use of grant/revokeRole() methods.
417      */

418     private void setRoles(Set JavaDoc roles) {
419         this.roles = roles;
420     }
421     
422     
423     /**
424      * Returns true if user has role specified.
425      */

426     public boolean hasRole(String JavaDoc roleName) {
427         Iterator JavaDoc iter = roles.iterator();
428         while (iter.hasNext()) {
429             RoleData role = (RoleData) iter.next();
430             if (role.getRole().equals(roleName)) {
431                 return true;
432             }
433         }
434         return false;
435     }
436     
437     
438     /**
439      * Revokes specified role from user.
440      */

441     public void revokeRole(String JavaDoc roleName) throws RollerException {
442         RoleData removeme = null;
443         Iterator JavaDoc iter = roles.iterator();
444         while (iter.hasNext()) {
445             RoleData role = (RoleData) iter.next();
446             if (role.getRole().equals(roleName)) {
447                 removeme = role;
448             }
449         }
450         
451         /*
452          * NOTE: we do this outside the loop above because we are not allowed
453          * to modify the contents of the Set while we are iterating over it.
454          * doing so causes a ConcurrentModificationException
455          */

456         if(removeme != null) {
457             roles.remove(removeme);
458         }
459     }
460     
461     
462     /**
463      * Grant to user role specified by role name.
464      */

465     public void grantRole(String JavaDoc roleName) throws RollerException {
466         if (!hasRole(roleName)) {
467             RoleData role = new RoleData(null, this, roleName);
468             roles.add(role);
469         }
470     }
471     
472 }
473
Popular Tags