KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > hibernate > model > User


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 /*
23   * JBoss, Home of Professional Open Source
24   * Copyright 2005, JBoss Inc., and individual contributors as indicated
25   * by the @authors tag. See the copyright.txt in the distribution for a
26   * full listing of individual contributors.
27   *
28   * This is free software; you can redistribute it and/or modify it
29   * under the terms of the GNU Lesser General Public License as
30   * published by the Free Software Foundation; either version 2.1 of
31   * the License, or (at your option) any later version.
32   *
33   * This software is distributed in the hope that it will be useful,
34   * but WITHOUT ANY WARRANTY; without even the implied warranty of
35   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
36   * Lesser General Public License for more details.
37   *
38   * You should have received a copy of the GNU Lesser General Public
39   * License along with this software; if not, write to the Free
40   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
41   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
42   */

43 package org.jboss.test.hibernate.model;
44
45 import java.io.Serializable JavaDoc;
46 import java.util.ArrayList JavaDoc;
47 import java.util.Calendar JavaDoc;
48 import java.util.HashSet JavaDoc;
49 import java.util.List JavaDoc;
50 import java.util.Set JavaDoc;
51
52 /**
53  * @author Gavin King
54  */

55 public class User implements Serializable JavaDoc {
56     private Long JavaDoc id;
57     private String JavaDoc handle;
58     private String JavaDoc password;
59     private Name name;
60     private Calendar JavaDoc timeOfCreation;
61     private Calendar JavaDoc timeOfLastUpdate;
62     private Set JavaDoc previousPasswords = new HashSet JavaDoc();
63     private List JavaDoc roles = new ArrayList JavaDoc();
64     private List JavaDoc userRoles = new ArrayList JavaDoc();
65     private String JavaDoc email;
66     
67     public String JavaDoc getHandle() {
68         return handle;
69     }
70
71     public void setHandle(String JavaDoc handle) {
72         this.handle = handle;
73     }
74
75     public Long JavaDoc getId() {
76         return id;
77     }
78
79     private void setId(Long JavaDoc id) {
80         this.id = id;
81     }
82
83     public Name getName() {
84         return name;
85     }
86
87     public void setName(Name name) {
88         this.name = name;
89     }
90
91     public String JavaDoc getPassword() {
92         return password;
93     }
94
95     public void setPassword(String JavaDoc password) {
96         this.password = password;
97     }
98     
99     /**
100      * Change the password, as long as the new password
101      * has not already been used.
102      */

103     public boolean changePassword(String JavaDoc newPassword) {
104         if (
105             password.equals(newPassword) ||
106             previousPasswords.contains(newPassword)
107         ) {
108             return false;
109         }
110         else {
111             previousPasswords.add(password);
112             password = newPassword;
113             return true;
114         }
115     }
116     
117     /**
118      * Many-to-many association to Role. This is
119      * a collection of Roles (if we don't want the
120      * extra information defined by UserRole).
121      */

122     public List JavaDoc getRoles() {
123         return roles;
124     }
125
126     private void setRoles(List JavaDoc roles) {
127         this.roles = roles;
128     }
129
130     public String JavaDoc getEmail() {
131         return email;
132     }
133
134     public void setEmail(String JavaDoc email) {
135         this.email = email;
136     }
137
138     public Calendar JavaDoc getTimeOfCreation() {
139         return timeOfCreation;
140     }
141
142     public void setTimeOfCreation(Calendar JavaDoc timeOfCreation) {
143         this.timeOfCreation = timeOfCreation;
144     }
145
146     public Calendar JavaDoc getTimeOfLastUpdate() {
147         return timeOfLastUpdate;
148     }
149
150     private void setTimeOfLastUpdate(Calendar JavaDoc timeOfLastUpdate) {
151         this.timeOfLastUpdate = timeOfLastUpdate;
152     }
153     
154     public UserRole addRole(Role role) {
155         if ( getRoles().indexOf(role)>=0 ) {
156             throw new RuntimeException JavaDoc("role already assigned");
157         }
158         getRoles().add(role);
159         role.getUsers().add(this);
160         UserRole ur = new UserRole(this, role);
161         getUserRoles().add(ur);
162         return ur;
163     }
164     
165     public void removeRole(int selectedRole) {
166         if ( selectedRole>getUserRoles().size() ) {
167             throw new RuntimeException JavaDoc("selected role does not exist");
168         }
169         UserRole ur = (UserRole) getUserRoles().remove(selectedRole);
170         ur.getRole().getUsers().remove(this);
171         getRoles().remove( ur.getRole() );
172     }
173     
174     /**
175      * Many-to-many association to Role. This
176      * is a collection of UserRoles, the association
177      * class.
178      */

179     public List JavaDoc getUserRoles() {
180         return userRoles;
181     }
182
183     private void setUserRoles(List JavaDoc userRoles) {
184         this.userRoles = userRoles;
185     }
186
187     public Set JavaDoc getPreviousPasswords() {
188         return previousPasswords;
189     }
190
191     private void setPreviousPasswords(Set JavaDoc previousPasswords) {
192         this.previousPasswords = previousPasswords;
193     }
194
195     //it is best to implement equals()/hashCode()
196
//to compare a "business key" (in this case
197
//the unique handle of the User) rather than
198
//the surrogate id
199

200     public boolean equals(Object JavaDoc other) {
201         if (other==null) return false;
202         if ( !(other instanceof User) ) return false;
203         return ( (User) other ).getHandle().equals(handle);
204     }
205     
206     public int hashCode() {
207         return handle.hashCode();
208     }
209
210
211 }
212
213
Popular Tags