001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.hibernate.model;
023:
024: import java.io.Serializable;
025: import java.util.ArrayList;
026: import java.util.Calendar;
027: import java.util.HashSet;
028: import java.util.List;
029: import java.util.Set;
030:
031: /**
032: * @author Gavin King
033: */
034: public class User implements Serializable {
035: static final long serialVersionUID = -1363654390077444431L;
036: private Long id;
037: private String handle;
038: private String password;
039: private Name name;
040: private Calendar timeOfCreation;
041: private Calendar timeOfLastUpdate;
042: private Set previousPasswords = new HashSet();
043: private List roles = new ArrayList();
044: private List userRoles = new ArrayList();
045: private String email;
046:
047: public String getHandle() {
048: return handle;
049: }
050:
051: public void setHandle(String handle) {
052: this .handle = handle;
053: }
054:
055: public Long getId() {
056: return id;
057: }
058:
059: private void setId(Long id) {
060: this .id = id;
061: }
062:
063: public Name getName() {
064: return name;
065: }
066:
067: public void setName(Name name) {
068: this .name = name;
069: }
070:
071: public String getPassword() {
072: return password;
073: }
074:
075: public void setPassword(String password) {
076: this .password = password;
077: }
078:
079: /**
080: * Change the password, as long as the new password has not already been
081: * used.
082: */
083: public boolean changePassword(String newPassword) {
084: if (password.equals(newPassword)
085: || previousPasswords.contains(newPassword)) {
086: return false;
087: } else {
088: previousPasswords.add(password);
089: password = newPassword;
090: return true;
091: }
092: }
093:
094: /**
095: * Many-to-many association to Role. This is a collection of Roles (if we
096: * don't want the extra information defined by UserRole).
097: */
098: public List getRoles() {
099: return roles;
100: }
101:
102: private void setRoles(List roles) {
103: this .roles = roles;
104: }
105:
106: public String getEmail() {
107: return email;
108: }
109:
110: public void setEmail(String email) {
111: this .email = email;
112: }
113:
114: public Calendar getTimeOfCreation() {
115: return timeOfCreation;
116: }
117:
118: public void setTimeOfCreation(Calendar timeOfCreation) {
119: this .timeOfCreation = timeOfCreation;
120: }
121:
122: public Calendar getTimeOfLastUpdate() {
123: return timeOfLastUpdate;
124: }
125:
126: private void setTimeOfLastUpdate(Calendar timeOfLastUpdate) {
127: this .timeOfLastUpdate = timeOfLastUpdate;
128: }
129:
130: public UserRole addRole(Role role) {
131: if (getRoles().indexOf(role) >= 0) {
132: throw new RuntimeException("role already assigned");
133: }
134: getRoles().add(role);
135: role.getUsers().add(this );
136: UserRole ur = new UserRole(this , role);
137: getUserRoles().add(ur);
138: return ur;
139: }
140:
141: public void removeRole(int selectedRole) {
142: if (selectedRole > getUserRoles().size()) {
143: throw new RuntimeException("selected role does not exist");
144: }
145: UserRole ur = (UserRole) getUserRoles().remove(selectedRole);
146: ur.getRole().getUsers().remove(this );
147: getRoles().remove(ur.getRole());
148: }
149:
150: /**
151: * Many-to-many association to Role. This is a collection of UserRoles, the
152: * association class.
153: */
154: public List getUserRoles() {
155: return userRoles;
156: }
157:
158: private void setUserRoles(List userRoles) {
159: this .userRoles = userRoles;
160: }
161:
162: public Set getPreviousPasswords() {
163: return previousPasswords;
164: }
165:
166: private void setPreviousPasswords(Set previousPasswords) {
167: this .previousPasswords = previousPasswords;
168: }
169:
170: //it is best to implement equals()/hashCode()
171: //to compare a "business key" (in this case
172: //the unique handle of the User) rather than
173: //the surrogate id
174:
175: public boolean equals(Object other) {
176: if (other == null)
177: return false;
178: if (!(other instanceof User))
179: return false;
180: return ((User) other).getHandle().equals(handle);
181: }
182:
183: public int hashCode() {
184: return handle.hashCode();
185: }
186:
187: }
|