package enterprise.web.tookit.user;
import java.io.Serializable;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
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.xml.bind.annotation.XmlRootElement;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.validator.routines.EmailValidator;
import enterprise.web.tookit.exception.ValidationException;
/**
* @since 22/11/2012
*
* @author regis rocha
*
* @description Classe de entidade para representar o usuario.
*
*/
@Entity(name = "users")
@Table(name = "users")
@XmlRootElement
public class User implements Serializable {
/**
* serial version
*/
private static final long serialVersionUID = -4215396197265382304L;
/**
* Constant para quantidade minima de caracteres de senha para cadastro de usuario.
*/
private final Integer MIN_LENGHT_PASSWORD = 5;
// default constructor
public User() {
}
public User(Long pId, String pEmail, String pUserName, String pLastname, String pPassword, String pConfirmPassword,
List<UserRole> pListUserRoles) {
this.id = pId;
this.email = pEmail;
this.userName = pUserName;
this.userPassword = pPassword;
this.lastUserName = pLastname;
this.listUserRoles = pListUserRoles;
this.confirmUserPassword = pConfirmPassword;
}
private Long id;
private String email;
private String userName;
private String lastUserName;
private String userPassword;
private String confirmUserPassword;
private List<UserRole> listUserRoles;
/**
* @return the id
*/
@Id
@GeneratedValue
@Column(name = "id_user")
public Long getId() {
return id;
}
/**
* @param id
* the id to set
*/
public void setId(Long id) {
this.id = id;
}
/**
* @return the userName
*/
@Column(name = "name_user", length = 100, nullable = false)
public String getUserName() {
return userName;
}
/**
* @param userName
* the userName to set
*/
public void setUserName(String userName) {
this.userName = userName;
}
/**
* @return the lastUserName
*/
@Column(name = "last_name_user", length = 100, nullable = false)
public String getLastUserName() {
return lastUserName;
}
/**
* @param lastUserName
* the lastUserName to set
*/
public void setLastUserName(String lastUserName) {
this.lastUserName = lastUserName;
}
/**
* @return the userPassword
*/
@Column(name = "user_pass", length = 100, nullable = false)
public String getUserPassword() {
return userPassword;
}
/**
* @param userPassword
* the userPassword to set
*/
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
/**
* @return the email
*/
@Column(name = "email", length = 100, nullable = false)
public String getEmail() {
return email;
}
/**
* @param email the email to set
*/
public void setEmail(String email) {
this.email = email;
}
/**
* @return the confirmUserPassword
*/
@Transient
public String getConfirmUserPassword() {
return confirmUserPassword;
}
/**
* @param confirmUserPassword the confirmUserPassword to set
*/
public void setConfirmUserPassword(String confirmUserPassword) {
this.confirmUserPassword = confirmUserPassword;
}
/**
* @return the listUserRoles
*/
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "users_user_roles", joinColumns = { @JoinColumn(name = "id_user") }, inverseJoinColumns = { @JoinColumn(name = "id_user_role") })
public List<UserRole> getListUserRoles() {
return listUserRoles;
}
/**
* @param listUserRoles
* the listUserRoles to set
*/
public void setListUserRoles(List<UserRole> listUserRoles) {
this.listUserRoles = listUserRoles;
}
@Override
public boolean equals(Object obj) {
return super.equals(obj);
}
@Override
public int hashCode() {
int hash = 0;
if (this.id != null) {
hash += this.id.hashCode();
}
if (StringUtils.isNotBlank(this.email)) {
hash += this.email.hashCode();
}
if (StringUtils.isNotBlank(this.userPassword)) {
hash += this.userPassword.hashCode();
}
if (this.listUserRoles != null && !this.listUserRoles.isEmpty()) {
hash += this.listUserRoles.hashCode();
}
return hash;
}
@Override
public String toString() {
final StringBuilder toString = new StringBuilder();
toString.append(" ID: " + this.id);
toString.append(" , EMAIL: " + this.email);
toString.append(" , USER NAME: " + this.userName);
toString.append(" , LAST NAME: " + this.lastUserName);
return toString.toString();
}
/**
* @since 1.0
*
* @description Realiza as validacoes necessarias para identificar se o usuario esta preenchido
* corretamente.
*
* . Validar se os dados sao nulos
* . Validar se o email eh valido
* . Validar se o email ja esta cadastro para outro usuario
* . Validar se a senha e confirmacao de senha sao iguais
* . Validar se a quantidade de caracteres para senha eh permitido
*
* Caso alguma dessas regras nao seja atendida uma excessao sera lancada com sua descricao configurada
* em seu atributo message.
*
* @throws ValidationException
*
*/
public final void validateUserToInsert() throws ValidationException {
this.validarDadosIsNull();
this.validarEmail();
this.validarConfirmacaoSenha();
this.validarQuantidadeCaracteresSenha();
}
/**
* @author regisrocha
*
* @since 19/02/2013
*
* @description
*
* @param String
* @return
*/
public final void validarQuantidadeCaracteresSenha() throws ValidationException {
if (StringUtils.length(this.userPassword) < MIN_LENGHT_PASSWORD) {
throw new ValidationException("Sua senha deve ter no m�nimo 5 caracteres.");
}
}
/**
* @author regisrocha
*
* @since 19/01/13
*
* @description Valida se o o campo de senha e confirmacao de senha sao iguais e caso nao sejam iguais
* sera um erro sera lancado.
*
* @param String
* @param String
*
* @throws ValidationException
* Caso senha e confirmacao de senha sejam diferentes.
*
*/
public final void validarConfirmacaoSenha()
throws ValidationException {
if (!this.userPassword.equals(this.confirmUserPassword)) {
throw new ValidationException("As senhas informados n�o s�o iguais.");
}
}
/**
* @description Verifica se o email o usuario eh valido
*/
public final boolean validarEmail() throws ValidationException {
if(EmailValidator.getInstance().isValid(this.email)){
return true;
} else {
throw new ValidationException("E-mail do usuario nao eh valido");
}
}
/**
* @author regisrocha
*
* @since 19/01/13
*
* @description Valida se algum campo eh null e em caso afirmativo lanca uma excessao.
*
* @throws ValidationException
* Caso algum atributo de user esteja nulo.
*
*/
public final void validarDadosIsNull() throws ValidationException {
if (StringUtils.isBlank(this.userName)) {
throw new ValidationException("� necess�rio preencher o campo Nome de usu�rio");
} else if (StringUtils.isBlank(this.userPassword)) {
throw new ValidationException("� necess�rio preencher o campo senha.");
} else if (StringUtils.isBlank(this.confirmUserPassword)) {
throw new ValidationException("� necess�rio preencher o campo Confirme sua senha.");
} else if (StringUtils.isBlank(email)) {
throw new ValidationException("� necess�rio preencher o campo E-mail.");
} else if (StringUtils.isBlank(this.lastUserName)) {
throw new ValidationException("� necess�rio preencher o campo Sobrenome.");
}
}
}