KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > model > core > User


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not 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.
15  */

16 package com.blandware.atleap.model.core;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21
22 /**
23  * <p>Represents User. User is entity used to perform authentication. User
24  * may have some roles, they define what pages resources and so on he can assess.
25  * </p>
26  * <p>
27  * Also, user may be placed to groups to ease assigning roles.
28  * </p>
29  * <p><a HREF="User.java.htm"><i>View Source</i></a></p>
30  *
31  * @author Matt Raible <a HREF="mailto:matt@raibledesigns.com">&lt;matt@raibledesigns.com&gt;</a>
32  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
33  * @author Roman Puchkovskiy <a HREF="mailto:roman.puchkovskiy@blandware.com">
34  * &lt;roman.puchkovskiy@blandware.com&gt;</a>
35  * @version $Revision: 1.31 $ $Date: 2006/03/06 18:12:27 $
36  * @see Role
37  * @see Group
38  * @struts.form include-all="false" extends="BaseForm"
39  * @hibernate.class table="`al_core_user`" lazy="false" dynamic-update="true" dynamic-insert="true"
40  * @hibernate.cache usage="read-write"
41  */

42 public class User extends BaseObject {
43     //~ Instance fields
44
// ========================================================
45

46     protected String JavaDoc name;
47     protected String JavaDoc password;
48     protected String JavaDoc updatePassword;
49     protected String JavaDoc confirmPassword;
50     protected String JavaDoc updateConfirmPassword;
51     protected String JavaDoc firstName;
52     protected String JavaDoc lastName;
53     protected String JavaDoc address;
54     protected String JavaDoc city;
55     protected String JavaDoc province;
56     protected String JavaDoc country;
57     protected String JavaDoc postalCode;
58     protected String JavaDoc phoneNumber;
59     protected String JavaDoc email;
60     protected String JavaDoc website;
61
62     /**
63      * Roles that were assigned to user
64      */

65     protected List JavaDoc roles = new ArrayList JavaDoc();
66     /**
67      * Associations with roles through groups (ternary association between
68      * users, roles and groups): each role is assigned to user through some
69      * group. Exceptions are 'free' roles which are assigned to user directly,
70      * so association <code>group</code> component is <code>null</code>.
71      */

72     protected List JavaDoc rolesAssociations = new ArrayList JavaDoc();
73     /**
74      * Free roles: roles that were assigned to user directly (not through groups)
75      */

76     protected List JavaDoc freeRoles = new ArrayList JavaDoc();
77     /**
78      * Groups that contain this user
79      */

80     protected List JavaDoc groups = new ArrayList JavaDoc();
81     /**
82      * Version (used for optimistic-locking)
83      */

84     protected Long JavaDoc version;
85     /**
86      * Whether user may log in and operate
87      */

88     protected Boolean JavaDoc enabled;
89
90     /**
91      * Returns name of this user (it's actually a system identifier that is
92      * unique among users)
93      *
94      * @return user name
95      * @struts.form-field
96      * @struts.validator type="required"
97      * @struts.validator type="mask" msgkey="core.commons.errors.identifier"
98      * @struts.validator-args arg0resource="core.user.form.name"
99      * @struts.validator-var name="mask" value="${identifier}"
100      * @hibernate.id column="`username`" length="20" generator-class="assigned"
101      * unsaved-value="null"
102      */

103     public String JavaDoc getName() {
104         return name;
105     }
106
107     /**
108      * Returns the password.
109      *
110      * @return password
111      * @struts.form-field
112      * @struts.validator type="required"
113      * @struts.validator type="twofields" msgkey="core.commons.errors.twofields"
114      * @struts.validator type="minlength" msgkey="core.commons.errors.minlength" arg0resource="core.user.form.password" arg1value="${var:minlength}"
115      * @struts.validator type="maxlength" msgkey="core.commons.errors.maxlength" arg0resource="core.user.form.password" arg1value="${var:maxlength}"
116      * @struts.validator-args arg0resource="core.user.form.password"
117      * @struts.validator-args arg1resource="core.user.form.confirmPassword"
118      * @struts.validator-var name="secondProperty" value="confirmPassword"
119      * @struts.validator-var name="minlength" value="${passwordMinLength}"
120      * @struts.validator-var name="maxlength" value="${passwordMaxLength}"
121      * @hibernate.property column="`password`" not-null="true" length="252"
122      */

123     public String JavaDoc getPassword() {
124         return password;
125     }
126
127     /**
128      * <p>
129      * Returns the 'update password'.
130      * </p>
131      * <p>
132      * Password may be omitted while updating user
133      * It means that we don't want to change old password, so these two fields
134      * (<code>updatePassword</code> and <code>updateConfirmPassword</code>) were
135      * included to correctly handle this situation
136      * </p>
137      *
138      * @return 'update password'
139      * @struts.form-field
140      * @struts.validator type="twofields" msgkey="core.commons.errors.twofields"
141      * @struts.validator type="minlength" msgkey="core.commons.errors.minlength" arg0resource="core.user.form.password" arg1value="${var:minlength}"
142      * @struts.validator type="maxlength" msgkey="core.commons.errors.maxlength" arg0resource="core.user.form.password" arg1value="${var:maxlength}"
143      * @struts.validator-args arg0resource="core.user.form.updatePassword"
144      * @struts.validator-args arg1resource="core.user.form.confirmPassword"
145      * @struts.validator-var name="secondProperty" value="updateConfirmPassword"
146      * @struts.validator-var name="minlength" value="${passwordMinLength}"
147      * @struts.validator-var name="maxlength" value="${passwordMaxLength}"
148      */

149     public String JavaDoc getUpdatePassword() {
150         return updatePassword;
151     }
152
153     /**
154      * Returns the confirmed password
155      *
156      * @return confirmed password
157      * @struts.form-field
158      * @struts.validator type="required"
159      * @struts.validator-args arg0resource="core.user.form.confirmPassword"
160      */

161     public String JavaDoc getConfirmPassword() {
162         return confirmPassword;
163     }
164
165     /**
166      * <p>
167      * Returns the 'update confirmed password'
168      * </p>
169      * <p>
170      * Password may be omitted while updating user
171      * It means that we don't want to change old password, so these two fields
172      * (<code>updatePassword</code> and <code>updateConfirmPassword</code>) were
173      * included to correctly handle this situation
174      * </p>
175      *
176      * @return 'update confirmed password'
177      * @struts.form-field
178      */

179     public String JavaDoc getUpdateConfirmPassword() {
180         return confirmPassword;
181     }
182
183     /**
184      * Returns the first name of this user
185      *
186      * @return first name
187      * @struts.form-field
188      * @struts.validator type="required"
189      * @struts.validator-args arg0resource="core.user.form.firstName"
190      * @hibernate.property column="`first_name`" not-null="true" length="50"
191      */

192     public String JavaDoc getFirstName() {
193         return firstName;
194     }
195
196     /**
197      * Returns the last name of this user
198      *
199      * @return last name
200      * @struts.form-field
201      * @hibernate.property column="`last_name`" not-null="false" length="50"
202      */

203     public String JavaDoc getLastName() {
204         return lastName;
205     }
206
207     /**
208      * Returns full user name (constructed from first name and last name)
209      *
210      * @return full name
211      */

212     public String JavaDoc getFullName() {
213         if ( lastName == null || lastName.length() == 0 ) {
214             return firstName;
215         }
216         return firstName + ' ' + lastName;
217     }
218
219     /**
220      * Returns the address of this user
221      *
222      * @return address
223      * @struts.form-field
224      * @hibernate.property column="`address`" not-null="false" length="150"
225      */

226     public String JavaDoc getAddress() {
227         return address;
228     }
229
230     /**
231      * Returns the city of this user
232      *
233      * @return city
234      * @struts.form-field
235      * @hibernate.property column="`city`" not-null="false" length="50"
236      */

237     public String JavaDoc getCity() {
238         return city;
239     }
240
241     /**
242      * Returns the province
243      *
244      * @return province
245      * @struts.form-field
246      * @hibernate.property column="`province`" not-null="false" length="100"
247      */

248     public String JavaDoc getProvince() {
249         return province;
250     }
251
252     /**
253      * Returns the country
254      *
255      * @return country
256      * @struts.form-field
257      * @hibernate.property column="`country`" not-null="false" length="100"
258      */

259     public String JavaDoc getCountry() {
260         return country;
261     }
262
263     /**
264      * Returns the postal code
265      *
266      * @return postal code
267      * @struts.form-field
268      * @struts.validator type="mask" msgkey="core.commons.errors.zip"
269      * @struts.validator-args arg0resource="core.user.form.postalCode"
270      * @struts.validator-var name="mask" value="${zip}"
271      * @hibernate.property column="`postal_code`" not-null="false" length="15"
272      */

273     public String JavaDoc getPostalCode() {
274         return postalCode;
275     }
276
277     /**
278      * Returns the email. This is an optional field for specifying a
279      * different e-mail than the username.
280      *
281      * @return e-mail address
282      * @struts.form-field
283      * @struts.validator type="required"
284      * @struts.validator type="email"
285      * @struts.validator-args arg0resource="core.user.form.email"
286      * @hibernate.property
287      * @hibernate.column name="`email`" not-null="true" unique="false" length="252"
288      */

289     public String JavaDoc getEmail() {
290         return email;
291     }
292
293     /**
294      * Returns the phone number
295      *
296      * @return phone number
297      * @struts.form-field
298      * @struts.validator type="mask" msgkey="core.commons.errors.phone"
299      * @struts.validator-args arg0resource="core.user.form.phoneNumber"
300      * @struts.validator-var name="mask" value="${phone}"
301      * @hibernate.property column="`phone_number`" not-null="false" length="252"
302      */

303     public String JavaDoc getPhoneNumber() {
304         return phoneNumber;
305     }
306
307     /**
308      * Returns the website
309      *
310      * @return website
311      * @struts.form-field
312      * @hibernate.property column="`website`" not-null="false" length="252"
313      */

314     public String JavaDoc getWebsite() {
315         return website;
316     }
317
318     /**
319      * Returns the user's roles associations
320      *
321      * @return list of roles associations
322      * @see #rolesAssociations
323      * @see UserRoleAssociation
324      * @hibernate.bag table="`al_core_user_role`" lazy="true"
325      * @hibernate.collection-key
326      * @hibernate.collection-key-column name="`username`" length="20"
327      * @hibernate.collection-composite-element class="com.blandware.atleap.model.core.UserRoleAssociation"
328      */

329     public List JavaDoc getRolesAssociations() {
330         return rolesAssociations;
331     }
332
333     /**
334      * Returns user's roles
335      *
336      * @return list of roles
337      */

338     public List JavaDoc getRoles() {
339         return roles;
340     }
341
342     /**
343      * Returns user's free roles (these are roles that were assigned to user
344      * directly, not through some group)
345      *
346      * @return list of free roles
347      */

348     public List JavaDoc getFreeRoles() {
349         return freeRoles;
350     }
351
352     /**
353      * Returns list of groups to which this user belongs
354      *
355      * @return list of groups
356      * @hibernate.bag table="`al_core_group_user`" cascade="none" lazy="true" inverse="true" outer-join="false"
357      * @hibernate.collection-key
358      * @hibernate.collection-key-column name="`username`" length="20"
359      * @hibernate.collection-many-to-many class="com.blandware.atleap.model.core.Group"
360      * column="`groupname`" outer-join="false"
361      */

362     public List JavaDoc getGroups() {
363         return groups;
364     }
365
366     /**
367      * Sets list of groups to which this user belongs
368      *
369      * @param groups list of groups
370      */

371     public void setGroups(List JavaDoc groups) {
372         this.groups = groups;
373     }
374
375     /**
376      * Returns <code>Boolean.TRUE</code> if user is enabled (can login and operate) and
377      * <code>Boolean.FALSE</code> otherwise
378      *
379      * @return whether this user is active
380      * @hibernate.property column="`enabled`" not-null="true" type="true_false"
381      */

382     public Boolean JavaDoc getEnabled() {
383         return enabled;
384     }
385
386     /**
387      * Gets all user roles, represented as string (roles are separated with commas)
388      *
389      * @return roles separated with commas
390      */

391     public String JavaDoc getRolesAsString() {
392         StringBuffer JavaDoc roles = new StringBuffer JavaDoc();
393         if ( this.roles != null && this.roles.size() > 0 ) {
394             for ( Iterator JavaDoc i = this.roles.iterator(); i.hasNext(); ) {
395                 Role role = (Role) i.next();
396                 roles.append(role.getTitle());
397                 if ( i.hasNext() ) {
398                     roles.append(", ");
399                 }
400             }
401         }
402         return roles.toString();
403     }
404
405     /**
406      * Gets free user roles, represented as string (roles are separated with commas)
407      *
408      * @return roles separated with commas
409      */

410     public String JavaDoc getFreeRolesAsString() {
411         StringBuffer JavaDoc roles = new StringBuffer JavaDoc();
412         if ( this.freeRoles != null && this.freeRoles.size() > 0 ) {
413             for ( Iterator JavaDoc i = this.freeRoles.iterator(); i.hasNext(); ) {
414                 Role role = (Role) i.next();
415                 roles.append(role.getTitle());
416                 if ( i.hasNext() ) {
417                     roles.append(", ");
418                 }
419             }
420         }
421         return roles.toString();
422     }
423
424     /**
425      * Gets all user groups, represented as string (groups are separated with commas)
426      *
427      * @return roles separated with commas
428      */

429     public String JavaDoc getGroupsAsString() {
430         StringBuffer JavaDoc groups = new StringBuffer JavaDoc();
431         if ( this.groups != null && this.groups.size() > 0 ) {
432             for ( Iterator JavaDoc i = this.groups.iterator(); i.hasNext(); ) {
433                 Group group = (Group) i.next();
434                 groups.append(group.getTitle());
435                 if ( i.hasNext() ) {
436                     groups.append(", ");
437                 }
438             }
439         }
440         return groups.toString();
441     }
442
443     /**
444      * Returns version
445      *
446      * @return version
447      * @struts.form-field
448      * @hibernate.version column="`version`" type="long" unsaved-value="null"
449      */

450     public Long JavaDoc getVersion() {
451         return version;
452     }
453
454     /**
455      * Sets the user name.
456      *
457      * @param name the user name to set
458      */

459     public void setName(String JavaDoc name) {
460         this.name = name;
461     }
462
463     /**
464      * Sets the password.
465      *
466      * @param password the password to set
467      */

468     public void setPassword(String JavaDoc password) {
469         this.password = password;
470     }
471
472     /**
473      * Sets the 'update password'
474      *
475      * @param updatePassword the 'update password to set'
476      * @see User#getUpdatePassword()
477      */

478     public void setUpdatePassword(String JavaDoc updatePassword) {
479         this.updatePassword = updatePassword;
480     }
481
482     /**
483      * Sets the confirmed password.
484      *
485      * @param confirmPassword the confirmed password to set
486      */

487     public void setConfirmPassword(String JavaDoc confirmPassword) {
488         this.confirmPassword = confirmPassword;
489     }
490
491     /**
492      * Sets the 'update confirm password'
493      *
494      * @param updateConfirmPassword the 'update password' to set
495      * @see User#getUpdateConfirmPassword()
496      */

497     public void setUpdateConfirmPassword(String JavaDoc updateConfirmPassword) {
498         this.updateConfirmPassword = updateConfirmPassword;
499     }
500
501     /**
502      * Sets the first name of the user
503      *
504      * @param firstName the first name to set
505      */

506     public void setFirstName(String JavaDoc firstName) {
507         this.firstName = firstName;
508     }
509
510     /**
511      * Sets the last name of the user
512      *
513      * @param lastName the last name to set
514      */

515     public void setLastName(String JavaDoc lastName) {
516         this.lastName = lastName;
517     }
518
519     /**
520      * Sets the address
521      *
522      * @param address the address to set
523      */

524     public void setAddress(String JavaDoc address) {
525         this.address = address;
526     }
527
528     /**
529      * Sets the city
530      *
531      * @param city the city to set
532      */

533     public void setCity(String JavaDoc city) {
534         this.city = city;
535     }
536
537     /**
538      * Sets the country
539      *
540      * @param country the country to set
541      */

542     public void setCountry(String JavaDoc country) {
543         this.country = country;
544     }
545
546     /**
547      * Sets the postal code
548      *
549      * @param postalCode the postal code to set
550      */

551     public void setPostalCode(String JavaDoc postalCode) {
552         this.postalCode = postalCode;
553     }
554
555     /**
556      * Sets the province
557      *
558      * @param province the province to set
559      */

560     public void setProvince(String JavaDoc province) {
561         this.province = province;
562     }
563
564     /**
565      * Sets the email
566      *
567      * @param email the e-mail address to set
568      */

569     public void setEmail(String JavaDoc email) {
570         this.email = email;
571     }
572
573     /**
574      * Sets the phone number
575      *
576      * @param phoneNumber the phone number to set
577      */

578     public void setPhoneNumber(String JavaDoc phoneNumber) {
579         this.phoneNumber = phoneNumber;
580     }
581
582     /**
583      * Sets the website
584      *
585      * @param website the website to set
586      */

587     public void setWebsite(String JavaDoc website) {
588         this.website = website;
589     }
590
591     /**
592      * Sets the roles associations
593      *
594      * @param rolesAssociations list of roles associations
595      * @see #rolesAssociations
596      */

597     public void setRolesAssociations(List JavaDoc rolesAssociations) {
598         this.rolesAssociations = rolesAssociations;
599
600         roles = new ArrayList JavaDoc();
601         freeRoles = new ArrayList JavaDoc();
602         for (Iterator JavaDoc i = rolesAssociations.iterator(); i.hasNext();) {
603             UserRoleAssociation association = (UserRoleAssociation) i.next();
604             Role role = association.getRole();
605 /* User user = association.getUser();
606             role.getUsers().add(user);*/

607             if (!roles.contains(role)) {
608                 roles.add(role);
609             }
610             if (association.getGroup() == null) {
611                 if (!freeRoles.contains(role)) {
612                     freeRoles.add(role);
613                 }
614             }
615         }
616     }
617
618 /* public void setRoles(List roles) {
619         this.roles = roles;
620     }*/

621
622     // Methods for operating with object model representing "user - roles" relationship
623

624     /**
625      * Adds a connection between this user and a given role (as free role)
626      *
627      * @param role role to connect with
628      */

629     public void addFreeRole(Role role) {
630          addRole(role, null);
631     }
632
633     /**
634      * Adds a connection between this user and a given role through a given
635      * group (like a user is placed into <em>group</em> which has <em>role</em>
636      * assigned to it)
637      *
638      * @param role role to connect with
639      * @param group group through which to connect. If this is <code>null</code>,
640      * added role will be considered as free.
641      */

642     public void addRole(Role role, Group group) {
643         UserRoleAssociation association = new UserRoleAssociation(this, role, group);
644         if ( !getRolesAssociations().contains(association) ) {
645             getRolesAssociations().add(association);
646         }
647         // This is done to update roles and freeRoles lists too
648
setRolesAssociations(getRolesAssociations());
649     }
650
651     /**
652      * Removes a connection between this user and a given free role
653      *
654      * @param role the role
655      */

656     public void removeFreeRole(Role role) {
657         removeRole(role, null);
658     }
659
660     /**
661      * Removes a connection between this user and a given role through a given
662      * group (like a user is removed from <em>group</em> which has <em>role</em>
663      * assigned to it)
664      *
665      * @param role the role
666      * @param group the group. If this is <code>null</code>, free role will
667      * be removed.
668      */

669     public void removeRole(Role role, Group group) {
670         UserRoleAssociation association = new UserRoleAssociation(this, role, group);
671         getRolesAssociations().remove(association);
672         // This is done to update roles and freeRoles lists too
673
setRolesAssociations(getRolesAssociations());
674     }
675
676     /**
677      * Removes all connections of this user to given role (free and assigned
678      * through groups)
679      *
680      * @param role the role
681      */

682     public void removeRole(Role role) {
683         List JavaDoc newAssociations = new ArrayList JavaDoc();
684         List JavaDoc oldAssociations = getRolesAssociations();
685         for (int i = 0; i < oldAssociations.size(); i++) {
686             UserRoleAssociation association = (UserRoleAssociation) oldAssociations.get(i);
687             if (!association.getRole().equals(role)) {
688                 newAssociations.add(association);
689             }
690         }
691         setRolesAssociations(newAssociations);
692     }
693
694     /**
695      * Sets version
696      *
697      * @param version the version to set
698      */

699     public void setVersion(Long JavaDoc version) {
700         this.version = version;
701     }
702
703     /**
704      * Sets user to be enabled or disabled
705      *
706      * @param enabled Boolean value to set user enabled or disabled
707      */

708     public void setEnabled(Boolean JavaDoc enabled) {
709         this.enabled = enabled;
710     }
711
712     public boolean equals(Object JavaDoc o) {
713         if ( this == o ) {
714             return true;
715         }
716         if ( !(o instanceof User) ) {
717             return false;
718         }
719
720         final User user = (User) o;
721
722         if ( address != null ? !address.equals(user.address) : user.address != null ) {
723             return false;
724         }
725         if ( city != null ? !city.equals(user.city) : user.city != null ) {
726             return false;
727         }
728         if ( country != null ? !country.equals(user.country) : user.country != null ) {
729             return false;
730         }
731         if ( email != null ? !email.equals(user.email) : user.email != null ) {
732             return false;
733         }
734         if ( firstName != null ? !firstName.equals(user.firstName) : user.firstName != null ) {
735             return false;
736         }
737         if ( lastName != null ? !lastName.equals(user.lastName) : user.lastName != null ) {
738             return false;
739         }
740         if ( password != null ? !password.equals(user.password) : user.password != null ) {
741             return false;
742         }
743         if ( phoneNumber != null ? !phoneNumber.equals(user.phoneNumber) : user.phoneNumber != null ) {
744             return false;
745         }
746         if ( postalCode != null ? !postalCode.equals(user.postalCode) : user.postalCode != null ) {
747             return false;
748         }
749         if ( province != null ? !province.equals(user.province) : user.province != null ) {
750             return false;
751         }
752         if ( website != null ? !website.equals(user.website) : user.website != null ) {
753             return false;
754         }
755
756         return true;
757     }
758
759     public int hashCode() {
760         int result;
761         result = (password != null ? password.hashCode() : 0);
762         result = 29 * result + (firstName != null ? firstName.hashCode() : 0);
763         result = 29 * result + (lastName != null ? lastName.hashCode() : 0);
764         result = 29 * result + (address != null ? address.hashCode() : 0);
765         result = 29 * result + (city != null ? city.hashCode() : 0);
766         result = 29 * result + (province != null ? province.hashCode() : 0);
767         result = 29 * result + (country != null ? country.hashCode() : 0);
768         result = 29 * result + (postalCode != null ? postalCode.hashCode() : 0);
769         result = 29 * result + (phoneNumber != null ? phoneNumber.hashCode() : 0);
770         result = 29 * result + (email != null ? email.hashCode() : 0);
771         result = 29 * result + (website != null ? website.hashCode() : 0);
772         return result;
773     }
774
775 }
Popular Tags