KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > appfuse > model > User


1 package org.appfuse.model;
2
3 import java.io.Serializable JavaDoc;
4 import java.util.HashSet JavaDoc;
5 import java.util.Set JavaDoc;
6
7 import org.acegisecurity.GrantedAuthority;
8 import org.acegisecurity.userdetails.UserDetails;
9 import org.apache.commons.lang.builder.ToStringBuilder;
10 import org.apache.commons.lang.builder.ToStringStyle;
11
12 /**
13  * This class is used to generate the Struts Validator Form as well as the
14  * This class is used to generate Spring Validation rules
15  * as well as the Hibernate mapping file.
16  *
17  * <p><a HREF="User.java.htm"><i>View Source</i></a>
18  *
19  * @author <a HREF="mailto:matt@raibledesigns.com">Matt Raible</a>
20  * Updated by Dan Kibler (dan@getrolling.com)
21  * Extended to implement Acegi UserDetails interface
22  * by David Carter david@carter.net
23  *
24  * @struts.form include-all="true" extends="BaseForm"
25  * @hibernate.class table="app_user"
26  */

27 public class User extends BaseObject implements Serializable JavaDoc, UserDetails {
28     private static final long serialVersionUID = 3832626162173359411L;
29
30     protected Long JavaDoc id;
31     protected String JavaDoc username; // required
32
protected String JavaDoc password; // required
33
protected String JavaDoc confirmPassword;
34     protected String JavaDoc passwordHint;
35     protected String JavaDoc firstName; // required
36
protected String JavaDoc lastName; // required
37
protected String JavaDoc email; // required; unique
38
protected String JavaDoc phoneNumber;
39     protected String JavaDoc website;
40     protected Address address = new Address();
41     protected Integer JavaDoc version;
42     protected Set JavaDoc roles = new HashSet JavaDoc();
43     protected boolean enabled;
44     protected boolean accountExpired;
45     protected boolean accountLocked;
46     protected boolean credentialsExpired;
47
48     public User() {}
49
50     public User(String JavaDoc username) {
51         this.username = username;
52     }
53
54     /**
55      * @hibernate.id column="id" generator-class="native" unsaved-value="null"
56      */

57     public Long JavaDoc getId() {
58         return id;
59     }
60
61     /**
62      * @struts.validator type="required"
63      * @hibernate.property length="50" not-null="true" unique="true"
64      */

65     public String JavaDoc getUsername() {
66         return username;
67     }
68
69     /**
70      * @struts.validator type="required"
71      * @struts.validator type="twofields" msgkey="errors.twofields"
72      * @struts.validator-args arg1resource="userForm.password"
73      * @struts.validator-args arg1resource="userForm.confirmPassword"
74      * @struts.validator-var name="secondProperty" value="confirmPassword"
75      * @hibernate.property column="password" not-null="true"
76      */

77     public String JavaDoc getPassword() {
78         return password;
79     }
80
81     /**
82      * @struts.validator type="required"
83      */

84     public String JavaDoc getConfirmPassword() {
85         return confirmPassword;
86     }
87
88     /**
89      * @struts.validator type="required"
90      * @hibernate.property column="password_hint" not-null="false"
91      */

92     public String JavaDoc getPasswordHint() {
93         return passwordHint;
94     }
95
96     /**
97      * @struts.validator type="required"
98      * @hibernate.property column="first_name" not-null="true" length="50"
99      */

100     public String JavaDoc getFirstName() {
101         return firstName;
102     }
103
104     /**
105      * @struts.validator type="required"
106      * @hibernate.property column="last_name" not-null="true" length="50"
107      */

108     public String JavaDoc getLastName() {
109         return lastName;
110     }
111
112     /**
113      * @struts.validator type="required"
114      * @struts.validator type="email"
115      * @hibernate.property name="email" not-null="true" unique="true"
116      */

117     public String JavaDoc getEmail() {
118         return email;
119     }
120
121     /**
122      * @struts.validator type="mask" msgkey="errors.phone"
123      * @struts.validator-var name="mask" value="${phone}"
124      * @hibernate.property column="phone_number" not-null="false"
125      */

126     public String JavaDoc getPhoneNumber() {
127         return phoneNumber;
128     }
129
130     /**
131      * @struts.validator type="required"
132      * @hibernate.property column="website" not-null="false"
133      */

134     public String JavaDoc getWebsite() {
135         return website;
136     }
137
138     /**
139      * Returns the full name.
140      */

141     public String JavaDoc getFullName() {
142         return firstName + ' ' + lastName;
143     }
144
145     /**
146      * @hibernate.component
147      */

148     public Address getAddress() {
149         return address;
150     }
151
152     /**
153      * @hibernate.set table="user_role" cascade="save-update" lazy="false"
154      * @hibernate.collection-key column="user_id"
155      * @hibernate.collection-many-to-many class="org.appfuse.model.Role" column="role_id"
156      */

157     public Set JavaDoc getRoles() {
158         return roles;
159     }
160
161     /**
162      * Adds a role for the user
163      * @param role
164      */

165     public void addRole(Role role) {
166         getRoles().add(role);
167     }
168     
169     /**
170      * @see org.acegisecurity.userdetails.UserDetails#getAuthorities()
171      */

172     public GrantedAuthority[] getAuthorities() {
173         return (GrantedAuthority[]) roles.toArray(new GrantedAuthority[0]);
174     }
175
176     /**
177      * @hibernate.version
178      */

179     public Integer JavaDoc getVersion() {
180         return version;
181     }
182     
183     /**
184      * @hibernate.property column="account_enabled" type="yes_no"
185      */

186     public boolean isEnabled() {
187         return enabled;
188     }
189     
190     /**
191      * @hibernate.property column="account_expired" not-null="true" type="yes_no"
192      */

193     public boolean isAccountExpired() {
194         return accountExpired;
195     }
196     
197     /**
198      * @see org.acegisecurity.userdetails.UserDetails#isAccountNonExpired()
199      */

200     public boolean isAccountNonExpired() {
201         return !isAccountExpired();
202     }
203
204     /**
205      * @hibernate.property column="account_locked" not-null="true" type="yes_no"
206      */

207     public boolean isAccountLocked() {
208         return accountLocked;
209     }
210     
211     /**
212      * @see org.acegisecurity.userdetails.UserDetails#isAccountNonLocked()
213      */

214     public boolean isAccountNonLocked() {
215         return !isAccountLocked();
216     }
217
218     /**
219      * @hibernate.property column="credentials_expired" not-null="true" type="yes_no"
220      */

221     public boolean isCredentialsExpired() {
222         return credentialsExpired;
223     }
224     
225     /**
226      * @see org.acegisecurity.userdetails.UserDetails#isCredentialsNonExpired()
227      */

228     public boolean isCredentialsNonExpired() {
229         return !credentialsExpired;
230     }
231     
232     public void setId(Long JavaDoc id) {
233         this.id = id;
234     }
235     
236     public void setUsername(String JavaDoc username) {
237         this.username = username;
238     }
239
240     public void setPassword(String JavaDoc password) {
241         this.password = password;
242     }
243
244     public void setConfirmPassword(String JavaDoc confirmPassword) {
245         this.confirmPassword = confirmPassword;
246     }
247
248     public void setPasswordHint(String JavaDoc passwordHint) {
249         this.passwordHint = passwordHint;
250     }
251
252     public void setFirstName(String JavaDoc firstName) {
253         this.firstName = firstName;
254     }
255
256     public void setLastName(String JavaDoc lastName) {
257         this.lastName = lastName;
258     }
259
260     public void setEmail(String JavaDoc email) {
261         this.email = email;
262     }
263
264     public void setPhoneNumber(String JavaDoc phoneNumber) {
265         this.phoneNumber = phoneNumber;
266     }
267
268     public void setWebsite(String JavaDoc website) {
269         this.website = website;
270     }
271
272     public void setAddress(Address address) {
273         this.address = address;
274     }
275
276     public void setRoles(Set JavaDoc roles) {
277         this.roles = roles;
278     }
279
280     public void setVersion(Integer JavaDoc version) {
281         this.version = version;
282     }
283     
284     public void setEnabled(boolean enabled) {
285         this.enabled = enabled;
286     }
287
288     public void setAccountExpired(boolean accountExpired) {
289         this.accountExpired = accountExpired;
290     }
291     
292     public void setAccountLocked(boolean accountLocked) {
293         this.accountLocked = accountLocked;
294     }
295
296     public void setCredentialsExpired(boolean credentialsExpired) {
297         this.credentialsExpired = credentialsExpired;
298     }
299     
300     public boolean equals(Object JavaDoc o) {
301         if (this == o) return true;
302         if (!(o instanceof User)) return false;
303
304         final User user = (User) o;
305
306         if (username != null ? !username.equals(user.getUsername()) : user.getUsername() != null) return false;
307
308         return true;
309     }
310
311     public int hashCode() {
312         return (username != null ? username.hashCode() : 0);
313     }
314
315     public String JavaDoc toString() {
316         ToStringBuilder sb = new ToStringBuilder(this,
317                 ToStringStyle.DEFAULT_STYLE).append("username", this.username)
318                 .append("enabled", this.enabled)
319                 .append("accountExpired",this.accountExpired)
320                 .append("credentialsExpired",this.credentialsExpired)
321                 .append("accountLocked",this.accountLocked);
322
323         GrantedAuthority[] auths = this.getAuthorities();
324         if (auths != null) {
325             sb.append("Granted Authorities: ");
326
327             for (int i = 0; i < auths.length; i++) {
328                 if (i > 0) {
329                     sb.append(", ");
330                 }
331                 sb.append(auths[i].toString());
332             }
333         } else {
334             sb.append("No Granted Authorities");
335         }
336         return sb.toString();
337     }
338 }
339
Popular Tags