package com.vst.model;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.Version;
import org.acegisecurity.GrantedAuthority;
import org.acegisecurity.userdetails.UserDetails;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.Type;
import com.vst.model.ext.Address;
import com.vst.model.internal.LabelValue;
/**
* This class is used to generate Spring Validation rules
* as well as the Hibernate mapping file.
*
* <p><a href="User.java.html"><i>View Source</i></a>
*
* @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
* Updated by Dan Kibler (dan@getrolling.com)
* Extended to implement Acegi UserDetails interface
* by David Carter david@carter.net
*
* @hibernate.class table="app_user"
*/
@Entity
@Table(name = "app_user")
public class User extends BaseObject implements Serializable, UserDetails {
private static final long serialVersionUID = 3832626162173359411L;
protected Integer id;
protected String username; // required
protected String password; // required
protected String confirmPassword;
protected String firstName; // required
protected String lastName; // required
protected Address address = new Address();
protected String phoneNumber;
protected String email; // required; unique
protected String website;
protected String passwordHint;
protected Integer version;
protected Set roles = new HashSet();
protected boolean enabled;
protected boolean accountExpired;
protected boolean accountLocked;
protected boolean credentialsExpired;
private String roleName;
@Transient
public String getFullUserName() {
return firstName + " " + lastName;
}
@Transient
public String getRoleName() {
return roles.iterator().hasNext() ? ((Role) roles.iterator().next())
.getName() : "nothing";
}
public User() {
}
public User(String username) {
this.username = username;
}
/**
* @hibernate.id column="id" generator-class="native" unsaved-value="null"
*/
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
public Integer getId() {
return id;
}
/**
* @hibernate.property length="50" not-null="true" unique="true"
*/
@Column(nullable = false, unique = true, length = 50)
public String getUsername() {
return username;
}
/**
* @hibernate.property column="password" not-null="true"
*/
@Column(name="password", nullable = false)
public String getPassword() {
return password;
}
@Transient
public String getConfirmPassword() {
return confirmPassword;
}
/**
* @hibernate.property column="first_name" not-null="true" length="50"
*/
@Column(name = "first_name", nullable = false, length = 50)
public String getFirstName() {
return firstName;
}
/**
* @hibernate.property column="last_name" not-null="true" length="50"
*/
@Column(name = "last_name", nullable = false, length = 50)
public String getLastName() {
return lastName;
}
/**
* Returns the full name.
*/
@Transient
public String getFullName() {
return firstName + ' ' + lastName;
}
/**
* @hibernate.component
*/
@Transient
public Address getAddress() {
return address;
}
/**
* @hibernate.property name="email" not-null="true"
*/
@Column(name = "email", nullable = false)
public String getEmail() {
return email;
}
/**
* @hibernate.property column="phone_number" not-null="false"
*/
@Column(name = "phone_number", nullable = true)
public String getPhoneNumber() {
return phoneNumber;
}
/**
* @hibernate.property column="website" not-null="false"
*/
@Column(name = "website", nullable = true)
public String getWebsite() {
return website;
}
/**
* @hibernate.property column="password_hint" not-null="false"
*/
@Column(name = "password_hint", nullable = false)
public String getPasswordHint() {
return passwordHint;
}
/**
* @hibernate.set table="user_role" cascade="save-update" lazy="true" order-by="role_id"
* @hibernate.collection-key column="user_id"
* @hibernate.collection-many-to-many class="com.vst.model.Role" column="role_id"
*/
@ManyToMany(targetEntity = com.vst.model.Role.class, fetch = FetchType.EAGER)
@JoinTable(name="user_role", joinColumns=@JoinColumn(name="user_id"), inverseJoinColumns=@JoinColumn(name="role_id"))
@Cascade({ org.hibernate.annotations.CascadeType.EVICT })
public Set getRoles() {
return roles;
}
/**
* Adds a role for the user
* @param role
*/
public void addRole(Role role) {
getRoles().add(role);
}
/**
* @see org.acegisecurity.userdetails.UserDetails#getAuthorities()
*/
@Transient
public GrantedAuthority[] getAuthorities() {
return (GrantedAuthority[]) roles.toArray(new GrantedAuthority[0]);
}
/**
* @hibernate.version
*/
@Version
public Integer getVersion() {
return version;
}
/**
* @hibernate.property column="account_enabled" type="yes_no"
*/
@Column(name = "account_enabled")
@Type(type = "yes_no")
public boolean isEnabled() {
return enabled;
}
/**
* @hibernate.property column="account_expired" not-null="true" type="yes_no"
*/
@Column(name = "account_expired", nullable = false)
@Type(type = "yes_no")
public boolean isAccountExpired() {
return accountExpired;
}
/**
* @see org.acegisecurity.userdetails.UserDetails#isAccountNonExpired()
*/
@Transient
public boolean isAccountNonExpired() {
return !isAccountExpired();
}
/**
* @hibernate.property column="account_locked" not-null="true" type="yes_no"
*/
@Column(name = "account_locked", nullable = false)
@Type(type = "yes_no")
public boolean isAccountLocked() {
return accountLocked;
}
/**
* @see org.acegisecurity.userdetails.UserDetails#isAccountNonLocked()
*/
@Transient
public boolean isAccountNonLocked() {
return !isAccountLocked();
}
/**
* @hibernate.property column="credentials_expired" not-null="true" type="yes_no"
*/
@Column(name = "credentials_expired", nullable = false)
@Type(type = "yes_no")
public boolean isCredentialsExpired() {
return credentialsExpired;
}
/**
* @see org.acegisecurity.userdetails.UserDetails#isCredentialsNonExpired()
*/
@Transient
public boolean isCredentialsNonExpired() {
return !credentialsExpired;
}
public void setId(Integer id) {
this.id = id;
}
/**
* @spring.validator type="required"
*/
public void setUsername(String username) {
this.username = username;
}
/**
* @spring.validator type="required"
* @spring.validator type="twofields" msgkey="errors.twofields"
* @spring.validator-args arg1resource="user.password"
* @spring.validator-args arg1resource="user.confirmPassword"
* @spring.validator-var name="secondProperty" value="confirmPassword"
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @spring.validator type="required"
*/
public void setConfirmPassword(String confirmPassword) {
this.confirmPassword = confirmPassword;
}
/**
* @spring.validator type="required"
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @spring.validator type="required"
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setAddress(Address address) {
this.address = address;
}
/**
* @spring.validator type="email"
*/
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 setPasswordHint(String passwordHint) {
this.passwordHint = passwordHint;
}
public void setRoles(Set roles) {
this.roles = roles;
}
public void setVersion(Integer version) {
this.version = version;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
/**
* Convert user roles to LabelValue objects for convenience.
*/
@Transient
public List getRoleList() {
List userRoles = new ArrayList();
if (this.roles != null) {
for (Iterator it = roles.iterator(); it.hasNext();) {
Role role = (Role) it.next();
// convert the user's roles to LabelValue Objects
userRoles.add(new LabelValue(role.getName(), role.getName()));
}
}
return userRoles;
}
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();
}
}