1 22 43 package org.jboss.test.hibernate.model; 44 45 import java.io.Serializable ; 46 import java.util.ArrayList ; 47 import java.util.Calendar ; 48 import java.util.HashSet ; 49 import java.util.List ; 50 import java.util.Set ; 51 52 55 public class User implements Serializable { 56 private Long id; 57 private String handle; 58 private String password; 59 private Name name; 60 private Calendar timeOfCreation; 61 private Calendar timeOfLastUpdate; 62 private Set previousPasswords = new HashSet (); 63 private List roles = new ArrayList (); 64 private List userRoles = new ArrayList (); 65 private String email; 66 67 public String getHandle() { 68 return handle; 69 } 70 71 public void setHandle(String handle) { 72 this.handle = handle; 73 } 74 75 public Long getId() { 76 return id; 77 } 78 79 private void setId(Long 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 getPassword() { 92 return password; 93 } 94 95 public void setPassword(String password) { 96 this.password = password; 97 } 98 99 103 public boolean changePassword(String 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 122 public List getRoles() { 123 return roles; 124 } 125 126 private void setRoles(List roles) { 127 this.roles = roles; 128 } 129 130 public String getEmail() { 131 return email; 132 } 133 134 public void setEmail(String email) { 135 this.email = email; 136 } 137 138 public Calendar getTimeOfCreation() { 139 return timeOfCreation; 140 } 141 142 public void setTimeOfCreation(Calendar timeOfCreation) { 143 this.timeOfCreation = timeOfCreation; 144 } 145 146 public Calendar getTimeOfLastUpdate() { 147 return timeOfLastUpdate; 148 } 149 150 private void setTimeOfLastUpdate(Calendar timeOfLastUpdate) { 151 this.timeOfLastUpdate = timeOfLastUpdate; 152 } 153 154 public UserRole addRole(Role role) { 155 if ( getRoles().indexOf(role)>=0 ) { 156 throw new RuntimeException ("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 ("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 179 public List getUserRoles() { 180 return userRoles; 181 } 182 183 private void setUserRoles(List userRoles) { 184 this.userRoles = userRoles; 185 } 186 187 public Set getPreviousPasswords() { 188 return previousPasswords; 189 } 190 191 private void setPreviousPasswords(Set previousPasswords) { 192 this.previousPasswords = previousPasswords; 193 } 194 195 200 public boolean equals(Object 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 |