OK.
Abaixo o xhtml
<?xml version="1.0" encoding="UTF-8" ?>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<ui:composition template="/templates/interna.xhtml">
<ui:define name="titulo">Cadastro de Usuários</ui:define>
<ui:define name="corpo">
<h:form id="cadastro">
<h:messages />
<h:inputHidden value="#{usuarioBean.usuario.codigo}" />
<h:inputHidden value="#{usuarioBean.usuario.ativo}" />
<h:inputHidden value="#{usuarioBean.destinoSalvar}" />
<h:panelGrid columns="2">
<h:outputLabel value="Nome:" for="nome" />
<h:inputText id="nome" label="Nome" value="#{usuarioBean.usuario.nome}" size="50" maxlength="100"
required="true" requiredMessage="Informe seu nome">
<f:validateLength minimun="10" maximum="100" />
</h:inputText>
<h:outputLabel value="Papel:" for="papel" />
<h:inputText id="papel" label="Papel" value="#{usuarioBean.usuario.papel}" size="50" maxlength="30"
rendered="true" rerequiredMessage="Informe seu papel">
<f:validateLength maximum="30" />
</h:inputText>
<h:outputLabel value="Data Nascimento:" for="data" />
<h:inputText id="data" label="Data Nascimento" value="#{usuarioBean.usuario.nascimento}" size="8"
maxlength="10" required="false" requiredMessage="Informe sua data de nascimento">
<f:convertDateTime dateStyle="short" timeZone="America/Sao_Paulo" pattern="dd/MM/yyyy" />
</h:inputText>
<h:outputLabel value="Celular:" for="celular" />
<h:inputText id="celular" label="Celular" value="#{usuarioBean.usuario.celular}" size="11" maxlength="13"
requiredMessage="Informe seu número de telefone celular">
<f:validateLength minimun="13" maximum="13" />
</h:inputText>
<h:outputLabel value="E-mail:" for="email" />
<h:panelGroup>
<h:inputText id="email" label="E-mail" value="#{usuarioBean.usuario.email}" size="50"
maxlength="50" required="true" validatorMessage="E-mail inválido" requiredMessage="Informe seu endereço de e-mail">
<f:validateRegex pattern="[a-zA-Z0-9\-\_\.]+@[a-zA-Z0-9\-\_\.]+" />
</h:inputText>
<h:message for="email" />
</h:panelGroup>
<h:outputLabel value="Idioma:" for="idioma" />
<h:selectOneMenu id="idioma" value="#{usuarioBean.usuario.idioma}" requiredMessage="Informe seu idioma">
<f:selectItem itemValue="pt_BR" itemLabel="Português" />
<f:selectItem itemValue="en_US" itemLabel="English" />
</h:selectOneMenu>
<h:outputLabel value="Setor:" for="setor" />
<h:selectOneMenu id="setor" value="#{setorBean.setor.codigo}" valueChangeListener="#{setorBean.setNome}"
rendered="#{!empty contextoBean}" onchange="submit()" requiredMessage="Informe o seu setor de trabalho">
<f:selectItems value="#{setorBean.lista}" var="setor" itemValue="#{setor.codigo}" itemLabel="#{setor.nome}" />
</h:selectOneMenu>
<h:outputLabel value="Login:" for="login" />
<h:inputText id="login" label="Login" value="#{usuarioBean.usuario.login}" size="15" maxlength="15"
required="true" validatorMessage="Login deve ter no mínimo 5 e no máximo 15 caracteres e só pode
ter os símbolos '.' e '_'." requiredMessage="Informe o seu login">
<f:validateRegex pattern="([a-z]|[0-9]|[.][_]){5,15}" />
</h:inputText>
<h:outputLabel value="Senha:" for="senha" />
<h:inputSecret id="Senha" value="#{usuarioBean.usuario.senha}" size="10" maxlength="10" required="true"
redisplay="true" requiredMessage="informe a sua senha"/>
<h:outputLabel value="Confirmar Senha:" for="confirmarsenha" />
<h:inputSecret id="confirmarsenha" label="Confirmar Senha" value="#{usuarioBean.confirmarSenha}"
size="10" maxlength="10" required="true" redisplay="true" requiredMessage="Informe sua senha novamentes"/>
</h:panelGrid>
<h:commandButton action="#{usuarioBean.salvar}" value="Salvar" />
</h:form>
</ui:define>
</ui:composition>
</html>
Abaixo a classe Usuario
package gestao.usuario;
import gestao.setor.Setor;
import java.util.Date;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
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.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.UniqueConstraint;
import org.hibernate.annotations.ForeignKey;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
/**
* @author Cristiano
*
*/
@Entity
public class Usuario implements Serializable {
private static final long serialVersionUID = -6030139229635543710L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "codigo", nullable = false, length = 10)
private Integer codigo;
@ManyToOne(fetch = FetchType.LAZY)
@OnDelete(action = OnDeleteAction.CASCADE)
@JoinColumn(name = "setor", nullable = true)
@ForeignKey(name = "fk_usuario_setor")
private Setor setor;
@Column(name = "papel", length = 30)
private String papel;
@Column(name = "nome", length = 100)
private String nome;
@Column(name = "email", length = 50)
private String email;
@org.hibernate.annotations.NaturalId
@Column(name = "login", length = 15)
private String login;
@Column(name = "senha", length = 10)
private String senha;
@Temporal(TemporalType.DATE)
private Date nascimento;
@Column(name = "celular", length = 13)
private String celular;
@Column(name = "idioma", length = 15)
private String idioma;
@Column(name = "ativo", length = 1)
private boolean ativo;
@ElementCollection(targetClass = String.class)
@JoinTable(
name="usuario_permissao",
uniqueConstraints = {@UniqueConstraint(columnNames = {"usuario","permissao"})},
joinColumns = @JoinColumn(name = "usuario"))
@Column(name = "permissao", length=50)
private Set<String> permissao = new HashSet<String>();
/**
* @return the codigo
*/
public Integer getCodigo() {
return codigo;
}
/**
* @param codigo the codigo to set
*/
public void setCodigo(Integer codigo) {
this.codigo = codigo;
}
/**
* @return the setor
*/
public Setor getSetor() {
return setor;
}
/**
* @param setor the setor to set
*/
public void setSetor(Setor setor) {
this.setor = setor;
}
/**
* @return the papel
*/
public String getPapel() {
return papel;
}
/**
* @param papel the papel to set
*/
public void setPapel(String papel) {
this.papel = papel;
}
/**
* @return the nome
*/
public String getNome() {
return nome;
}
/**
* @param nome the nome to set
*/
public void setNome(String nome) {
this.nome = nome;
}
/**
* @return the email
*/
public String getEmail() {
return email;
}
/**
* @param email the email to set
*/
public void setEmail(String email) {
this.email = email;
}
/**
* @return the login
*/
public String getLogin() {
return login;
}
/**
* @param login the login to set
*/
public void setLogin(String login) {
this.login = login;
}
/**
* @return the senha
*/
public String getSenha() {
return senha;
}
/**
* @param senha the senha to set
*/
public void setSenha(String senha) {
this.senha = senha;
}
/**
* @return the nascimento
*/
public Date getNascimento() {
return nascimento;
}
/**
* @param nascimento the nascimento to set
*/
public void setNascimento(Date nascimento) {
this.nascimento = nascimento;
}
/**
* @return the celular
*/
public String getCelular() {
return celular;
}
/**
* @param celular the celular to set
*/
public void setCelular(String celular) {
this.celular = celular;
}
/**
* @return the idioma
*/
public String getIdioma() {
return idioma;
}
/**
* @param idioma the idioma to set
*/
public void setIdioma(String idioma) {
this.idioma = idioma;
}
/**
* @return the ativo
*/
public boolean isAtivo() {
return ativo;
}
/**
* @param ativo the ativo to set
*/
public void setAtivo(boolean ativo) {
this.ativo = ativo;
}
/**
* @return the permissao
*/
public Set<String> getPermissao() {
return permissao;
}
/**
* @param permissao the permissao to set
*/
public void setPermissao(Set<String> permissao) {
this.permissao = permissao;
}
/**
* @return the serialversionuid
*/
public static long getSerialversionuid() {
return serialVersionUID;
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (ativo ? 1231 : 1237);
result = prime * result + ((celular == null) ? 0 : celular.hashCode());
result = prime * result + ((codigo == null) ? 0 : codigo.hashCode());
result = prime * result + ((email == null) ? 0 : email.hashCode());
result = prime * result + ((idioma == null) ? 0 : idioma.hashCode());
result = prime * result + ((login == null) ? 0 : login.hashCode());
result = prime * result
+ ((nascimento == null) ? 0 : nascimento.hashCode());
result = prime * result + ((nome == null) ? 0 : nome.hashCode());
result = prime * result + ((papel == null) ? 0 : papel.hashCode());
result = prime * result
+ ((permissao == null) ? 0 : permissao.hashCode());
result = prime * result + ((senha == null) ? 0 : senha.hashCode());
result = prime * result + ((setor == null) ? 0 : setor.hashCode());
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Usuario other = (Usuario) obj;
if (ativo != other.ativo)
return false;
if (celular == null) {
if (other.celular != null)
return false;
} else if (!celular.equals(other.celular))
return false;
if (codigo == null) {
if (other.codigo != null)
return false;
} else if (!codigo.equals(other.codigo))
return false;
if (email == null) {
if (other.email != null)
return false;
} else if (!email.equals(other.email))
return false;
if (idioma == null) {
if (other.idioma != null)
return false;
} else if (!idioma.equals(other.idioma))
return false;
if (login == null) {
if (other.login != null)
return false;
} else if (!login.equals(other.login))
return false;
if (nascimento == null) {
if (other.nascimento != null)
return false;
} else if (!nascimento.equals(other.nascimento))
return false;
if (nome == null) {
if (other.nome != null)
return false;
} else if (!nome.equals(other.nome))
return false;
if (papel == null) {
if (other.papel != null)
return false;
} else if (!papel.equals(other.papel))
return false;
if (permissao == null) {
if (other.permissao != null)
return false;
} else if (!permissao.equals(other.permissao))
return false;
if (senha == null) {
if (other.senha != null)
return false;
} else if (!senha.equals(other.senha))
return false;
if (setor == null) {
if (other.setor != null)
return false;
} else if (!setor.equals(other.setor))
return false;
return true;
}
}
Abaixo a classe UsuarioBean
package gestao.web;
import java.util.List;
import java.util.Set;
import gestao.usuario.Usuario;
import gestao.usuario.UsuarioRN;
import gestao.util.DAOException;
import gestao.util.RNException;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
@ManagedBean(name="usuarioBean")
@RequestScoped
public class UsuarioBean {
private Usuario usuario = new Usuario();
private String confirmarSenha;
private List<Usuario> lista;
private String destinoSalvar;
private String permissao;
/**
* @return usuario
*/
public String novo() {
this.destinoSalvar = "usuarioSucesso";
this.usuario = new Usuario();
this.usuario.setAtivo(true);
return "usuario";
}
/**
* @return usuário a ser editado
*/
public String editar() {
this.confirmarSenha = this.usuario.getSenha();
return "/publico/usuario";
}
/**
*
* @return usuarioSucesso
*/
public String salvar() throws DAOException {
FacesContext context = FacesContext.getCurrentInstance();
String senha = this.usuario.getSenha();
if (!senha.equals(this.confirmarSenha)) {
FacesMessage facesMessage = new FacesMessage("A senha não foi confirmada corretamente");
context.addMessage(null, facesMessage);
return null;
}
UsuarioRN usuarioRN = new UsuarioRN();
usuarioRN.salvar(this.usuario);
return this.destinoSalvar;
}
/**
* @return Exclui o usuário
*/
public String excluir() throws RNException {
UsuarioRN usuarioRN = new UsuarioRN();
usuarioRN.excluir(this.usuario);
this.lista = null;
return null;
}
/**
* @return Ativa o usuário
*/
public String ativar() throws DAOException {
if (this.usuario.isAtivo()) {
this.usuario.setAtivo(false);
} else {
this.usuario.setAtivo(true);
}
UsuarioRN usuarioRN = new UsuarioRN();
usuarioRN.salvar(this.usuario);
return null;
}
public List<Usuario> getLista() {
if (this.lista == null) {
UsuarioRN usuarioRN = new UsuarioRN();
this.lista = usuarioRN.listar();
}
return this.lista;
}
public String atribuiPermissao(Usuario usuario, String permissao) {
this.usuario = usuario;
Set<String> permissoes = this.usuario.getPermissao();
if (permissoes.contains(permissao)) {
permissoes.remove(permissao);
} else {
permissoes.add(permissao);
}
return null;
}
/**
* @return the usuario
*/
public Usuario getUsuario() {
return usuario;
}
/**
* @param usuario the usuario to set
*/
public void setUsuario(Usuario usuario) {
this.usuario = usuario;
}
/**
* @return the confirmarSenha
*/
public String getConfirmarSenha() {
return confirmarSenha;
}
/**
* @param confirmarSenha the confirmarSenha to set
*/
public void setConfirmarSenha(String confirmarSenha) {
this.confirmarSenha = confirmarSenha;
}
public String getDestinoSalvar() {
return destinoSalvar;
}
public void setDestinoSalvar(String destinoSalvar) {
this.destinoSalvar = destinoSalvar;
}
public String getPermissao() {
return permissao;
}
public void setPermissao(String permissao) {
this.permissao = permissao;
}
}
Abaixo a classe Setor
package gestao.setor;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Setor implements Serializable {
private static final long serialVersionUID = -8570149793063872851L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "codigo", nullable = false, length = 10)
private Integer codigo;
@Column(name = "nome", length = 50)
private String nome;
/**
* @return the codigo
*/
public Integer getCodigo() {
return codigo;
}
/**
* @param codigo the codigo to set
*/
public void setCodigo(Integer codigo) {
this.codigo = codigo;
}
/**
* @return the nome
*/
public String getNome() {
return nome;
}
/**
* @param nome the nome to set
*/
public void setNome(String nome) {
this.nome = nome;
}
/**
* @return the serialversionuid
*/
public static long getSerialversionuid() {
return serialVersionUID;
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((codigo == null) ? 0 : codigo.hashCode());
result = prime * result + ((nome == null) ? 0 : nome.hashCode());
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Setor other = (Setor) obj;
if (codigo == null) {
if (other.codigo != null)
return false;
} else if (!codigo.equals(other.codigo))
return false;
if (nome == null) {
if (other.nome != null)
return false;
} else if (!nome.equals(other.nome))
return false;
return true;
}
}
E finalmente a classe SetorBean
package gestao.web;
import java.util.List;
import gestao.setor.Setor;
import gestao.setor.SetorRN;
import gestao.util.DAOException;
import gestao.util.RNException;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
@ManagedBean(name="setorBean")
@RequestScoped
public class SetorBean {
private Setor setor = new Setor();
private List<Setor> lista;
private String destinoSalvar;
/**
* @return setor
*/
public String novo() {
this.setor = new Setor();
return "setor";
}
/**
* @return setor a ser editado
*/
public String editar() {
return "/publico/setor";
}
/**
*
* @return
*/
public String salvar() throws DAOException {
SetorRN setorRN = new SetorRN();
setorRN.salvar(this.setor);
return this.destinoSalvar;
}
/**
* @return Exclui o usuário
*/
public String excluir() throws RNException {
SetorRN setorRN = new SetorRN();
setorRN.excluir(this.setor);
this.lista = null;
return null;
}
public List<Setor> getLista() {
if (this.lista == null) {
SetorRN setorRN = new SetorRN();
this.lista = setorRN.listar();
}
return this.lista;
}
/**
* @return the setor
*/
public Setor getSetor() {
return setor;
}
/**
* @param setor the setor to set
*/
public void setSetor(Setor setor) {
this.setor = setor;
}
/**
* @return the destinoSalvar
*/
public String getDestinoSalvar() {
return destinoSalvar;
}
/**
* @param destinoSalvar the destinoSalvar to set
*/
public void setDestinoSalvar(String destinoSalvar) {
this.destinoSalvar = destinoSalvar;
}
/**
* @param lista the lista to set
*/
public void setLista(List<Setor> lista) {
this.lista = lista;
}
}