package org.appfuse.model;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import org.acegisecurity.GrantedAuthority;
import org.acegisecurity.userdetails.UserDetails;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
public class User extends BaseObject implements Serializable, UserDetails {
private static final long serialVersionUID = 3832626162173359411L;
protected Long id;
protected String username; protected String password; protected String confirmPassword;
protected String passwordHint;
protected String firstName; protected String lastName; protected String email; protected String phoneNumber;
protected String website;
protected Address address = new Address();
protected Integer version;
protected Set roles = new HashSet();
protected boolean enabled;
protected boolean accountExpired;
protected boolean accountLocked;
protected boolean credentialsExpired;
public User() {}
public User(String username) {
this.username = username;
}
public Long getId() {
return id;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public String getConfirmPassword() {
return confirmPassword;
}
public String getPasswordHint() {
return passwordHint;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getEmail() {
return email;
}
public String getPhoneNumber() {
return phoneNumber;
}
public String getWebsite() {
return website;
}
public String getFullName() {
return firstName + ' ' + lastName;
}
public Address getAddress() {
return address;
}
public Set getRoles() {
return roles;
}
public void addRole(Role role) {
getRoles().add(role);
}
public GrantedAuthority[] getAuthorities() {
return (GrantedAuthority[]) roles.toArray(new GrantedAuthority[0]);
}
public Integer getVersion() {
return version;
}
public boolean isEnabled() {
return enabled;
}
public boolean isAccountExpired() {
return accountExpired;
}
public boolean isAccountNonExpired() {
return !isAccountExpired();
}
public boolean isAccountLocked() {
return accountLocked;
}
public boolean isAccountNonLocked() {
return !isAccountLocked();
}
public boolean isCredentialsExpired() {
return credentialsExpired;
}
public boolean isCredentialsNonExpired() {
return !credentialsExpired;
}
public void setId(Long id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setConfirmPassword(String confirmPassword) {
this.confirmPassword = confirmPassword;
}
public void setPasswordHint(String passwordHint) {
this.passwordHint = passwordHint;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setEmail(String email) {
this.email = email;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public void setWebsite(String website) {
this.website = website;
}
public void setAddress(Address address) {
this.address = address;
}
public void setRoles(Set roles) {
this.roles = roles;
}
public void setVersion(Integer version) {
this.version = version;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public void setAccountExpired(boolean accountExpired) {
this.accountExpired = accountExpired;
}
public void setAccountLocked(boolean accountLocked) {
this.accountLocked = accountLocked;
}
public void setCredentialsExpired(boolean credentialsExpired) {
this.credentialsExpired = credentialsExpired;
}
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof User)) return false;
final User user = (User) o;
if (username != null ? !username.equals(user.getUsername()) : user.getUsername() != null) return false;
return true;
}
public int hashCode() {
return (username != null ? username.hashCode() : 0);
}
public String toString() {
ToStringBuilder sb = new ToStringBuilder(this,
ToStringStyle.DEFAULT_STYLE).append("username", this.username)
.append("enabled", this.enabled)
.append("accountExpired",this.accountExpired)
.append("credentialsExpired",this.credentialsExpired)
.append("accountLocked",this.accountLocked);
GrantedAuthority[] auths = this.getAuthorities();
if (auths != null) {
sb.append("Granted Authorities: ");
for (int i = 0; i < auths.length; i++) {
if (i > 0) {
sb.append(", ");
}
sb.append(auths[i].toString());
}
} else {
sb.append("No Granted Authorities");
}
return sb.toString();
}
}