Error When Constructing Menudinamico

Asked

Viewed 7 times

-1

See that I have a table of Menus I have a Bean that reads this table and builds the menu according to two information, these being: If the user is an INTERNAL, EXTERNAL user. If the Menu is for INTERNAL, EXTERNAL OR BOTH users and beyond it also needs to check if the User is manager or not.

Well before that, I can make the menu work, exactly the way I want, however, I need to enter login twice, because the first time I login, it does not build the menu, and gives the error low:

Sep 03, 2021 7:46:43 AM com.sun.faces.lifecycle.Invokeapplicationphase execute WARNING: #{Mbusuario.validarUsuario(Mbusuario.usuario)}: java.lang.Nullpointerexception javax.faces.Facesexception: #{Mbusuario.validarUsuario(Mbusuario.usuario)}: java.lang.Nullpointerexception with.sun.faces.application.Actionlistenerimpl.getNavigationOutcome(Actionlistenerimpl.java:96) at com.sun.faces.application.Actionlistenerimpl.processAction(Actionlistenerimpl.java:71) at javax.faces.Component.UICommand.broadcast(Uicommand.java:222) at javax.faces.Component.UIViewRoot.broadcastEvents(Uiviewroot.java:847) at javax.faces.Component.UIViewRoot.processApplication(Uiviewroot.java:1396) at com.sun.faces.lifecycle.Invokeapplicationphase.execute(Invokeapplicationphase.java:58) at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:76) at com.sun.faces.lifecycle.Lifecycleimpl.execute(Lifecycleimpl.java:177) at javax.faces.webapp.FacesServlet.executeLifecyle(Facesservlet.java:707) at javax.faces.webapp.FacesServlet.service(Facesservlet.java:451) at org.apache.Catalina.core.Applicationfilterchain.internalDoFilter(Applicationfilterchain.java:231) at org.apache.Catalina.core.Applicationfilterchain.doFilter(Applicationfilterchain.java:166) at org.primefaces.Diamond.filter.Characterencodingfilter.doFilter(Characterencodingfilter.java:32) at org.apache.Catalina.core.Applicationfilterchain.internalDoFilter(Applicationfilterchain.java:193) at org.apache.Catalina.core.Applicationfilterchain.doFilter(Applicationfilterchain.java:166) at org.apache.Tomcat.websocket.server.WsFilter.doFilter(Wsfilter.java:53) at org.apache.Catalina.core.Applicationfilterchain.internalDoFilter(Applicationfilterchain.java:193) at org.apache.Catalina.core.Applicationfilterchain.doFilter(Applicationfilterchain.java:166) at org.apache.Catalina.core.Standardwrappervalve.invoke(Standardwrappervalve.java:199) at org.apache.Catalina.core.Standardcontextvalve.invoke(Standardcontextvalve.java:96) at org.apache.Catalina.authenticator.Authenticatorbase.invoke(Authenticatorbase.java:502) at org.apache.Catalina.core.Standardhostvalve.invoke(Standardhostvalve.java:140) at org.apache.Catalina.valves.Errorreportvalve.invoke(Errorreportvalve.java:81) at org.apache.Catalina.valves.Abstractaccesslogvalve.invoke(Abstractaccesslogvalve.java:651) at org.apache.Catalina.core.Standardenginevalve.invoke(Standardenginevalve.java:87) at org.apache.Catalina.connector.Coyoteadapter.service(Coyoteadapter.java:342) at org.apache.Coyote.http11.Http11processor.service(Http11processor.java:417) at org.apache.Coyote.AbstractProcessorLight.process(Abstractprocessorlight.java:66) at org.apache.Coyote.Abstractprotocol$Connectionhandler.process(Abstractprotocol.java:754) at org.apache.Tomcat.util.net.Nioendpoint$Socketprocessor.doRun(Nioendpoint.java:1376) at org.apache.Tomcat.util.net.SocketProcessorBase.run(Socketprocessorbase.java:49) at java.util.Concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.Concurrent.Threadpoolexecutor$Worker.run(Unknown Source) at org.apache.Tomcat.util.threads.Taskthread$Wrappingrunnable.run(Taskthread.java:61) at java.lang.Thread.run(Unknown Source) Caused by: javax.faces.el.Evaluationexception: java.lang.Nullpointerexception with.sun.faces.application.Methodbindingmethodexpressed dapter.invoke(Methodbindingmethodexpressed dapter.java:76) at com.sun.faces.application.Actionlistenerimpl.getNavigationOutcome(Actionlistenerimpl.java:82) ... 34 more Caused by: java.lang.Nullpointerexception at br.com.checard.bean.Menubean.constroiMenuModel(Menubean.java:234) at br.com.checard.bean.Usuariobean.validarUsuario(Usuariobean.java:148) at sun.reflect.Nativemethodaccessorimpl.invoke0(Native Method) at sun.reflect.Nativemethodaccessorimpl.invoke(Unknown Source) at sun.reflect.Delegatingmethodaccessorimpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.apache.el.parser.Astvalue.invoke(Astvalue.java:247) at org.apache.el.MethodExpressionImpl.invoke(Methodexpressionimpl.java:267) at org.apache.webbeans.el22.WrappedMethodExpression.invoke(Wrappedmethodexpression.java:50) at com.sun.faces.facelets.el.TagMethodExpression.invoke(Tagmethodexpression.java:65) at com.sun.faces.application.Methodbindingmethodexpressed dapter.invoke(Methodbindingmethodexpressed dapter.java:66) ... 35 more

Sep 03, 2021 7:46:43 AM com.sun.faces.context.Ajaxexceptionhandlerimpl handlePartialResponseError GRAVE: javax.faces.el.Evaluationexception: java.lang.Nullpointerexception with.sun.faces.application.Methodbindingmethodexpressed dapter.invoke(Methodbindingmethodexpressed dapter.java:76) at com.sun.faces.application.Actionlistenerimpl.getNavigationOutcome(Actionlistenerimpl.java:82) at com.sun.faces.application.Actionlistenerimpl.processAction(Actionlistenerimpl.java:71) at javax.faces.Component.UICommand.broadcast(Uicommand.java:222) at javax.faces.Component.UIViewRoot.broadcastEvents(Uiviewroot.java:847) at javax.faces.Component.UIViewRoot.processApplication(Uiviewroot.java:1396) at com.sun.faces.lifecycle.Invokeapplicationphase.execute(Invokeapplicationphase.java:58) at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:76) at com.sun.faces.lifecycle.Lifecycleimpl.execute(Lifecycleimpl.java:177) at javax.faces.webapp.FacesServlet.executeLifecyle(Facesservlet.java:707) at javax.faces.webapp.FacesServlet.service(Facesservlet.java:451) at org.apache.Catalina.core.Applicationfilterchain.internalDoFilter(Applicationfilterchain.java:231) at org.apache.Catalina.core.Applicationfilterchain.doFilter(Applicationfilterchain.java:166) at org.primefaces.Diamond.filter.Characterencodingfilter.doFilter(Characterencodingfilter.java:32) at org.apache.Catalina.core.Applicationfilterchain.internalDoFilter(Applicationfilterchain.java:193) at org.apache.Catalina.core.Applicationfilterchain.doFilter(Applicationfilterchain.java:166) at org.apache.Tomcat.websocket.server.WsFilter.doFilter(Wsfilter.java:53) at org.apache.Catalina.core.Applicationfilterchain.internalDoFilter(Applicationfilterchain.java:193) at org.apache.Catalina.core.Applicationfilterchain.doFilter(Applicationfilterchain.java:166) at org.apache.Catalina.core.Standardwrappervalve.invoke(Standardwrappervalve.java:199) at org.apache.Catalina.core.Standardcontextvalve.invoke(Standardcontextvalve.java:96) at org.apache.Catalina.authenticator.Authenticatorbase.invoke(Authenticatorbase.java:502) at org.apache.Catalina.core.Standardhostvalve.invoke(Standardhostvalve.java:140) at org.apache.Catalina.valves.Errorreportvalve.invoke(Errorreportvalve.java:81) at org.apache.Catalina.valves.Abstractaccesslogvalve.invoke(Abstractaccesslogvalve.java:651) at org.apache.Catalina.core.Standardenginevalve.invoke(Standardenginevalve.java:87) at org.apache.Catalina.connector.Coyoteadapter.service(Coyoteadapter.java:342) at org.apache.Coyote.http11.Http11processor.service(Http11processor.java:417) at org.apache.Coyote.AbstractProcessorLight.process(Abstractprocessorlight.java:66) at org.apache.Coyote.Abstractprotocol$Connectionhandler.process(Abstractprotocol.java:754) at org.apache.Tomcat.util.net.Nioendpoint$Socketprocessor.doRun(Nioendpoint.java:1376) at org.apache.Tomcat.util.net.SocketProcessorBase.run(Socketprocessorbase.java:49) at java.util.Concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.Concurrent.Threadpoolexecutor$Worker.run(Unknown Source) at org.apache.Tomcat.util.threads.Taskthread$Wrappingrunnable.run(Taskthread.java:61) at java.lang.Thread.run(Unknown Source) Caused by: java.lang.Nullpointerexception at br.com.checard.bean.Menubean.constroiMenuModel(Menubean.java:234) at br.com.checard.bean.Usuariobean.validarUsuario(Usuariobean.java:148) at sun.reflect.Nativemethodaccessorimpl.invoke0(Native Method) at sun.reflect.Nativemethodaccessorimpl.invoke(Unknown

My Class:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int codigo;

@Column
private String nome;

@Column
private String celular;

@Column
private String email;

@Column
private String login;

@Column
private String senha;

@ManyToOne(fetch = FetchType.LAZY)
private Representante representante;

@Column
private String tipoUsuario;

@Column
private Boolean gerente;

@Column
private String urlImagem;   

@Column
private String status;

public int getCodigo() {
    return codigo;
}

public void setCodigo(int codigo) {
    this.codigo = codigo;
}

public String getNome() {
    return nome;
}

public void setNome(String nome) {
    this.nome = nome;
}

public String getCelular() {
    return celular;
}

public void setCelular(String celular) {
    this.celular = celular;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getLogin() {
    return login;
}

public void setLogin(String login) {
    this.login = login;
}

public String getSenha() {
    return senha;
}

public void setSenha(String senha) {
    this.senha = senha;
}

public Representante getRepresentante() {
    return representante;
}

public void setRepresentante(Representante representante) {
    this.representante = representante;
}

public static void main(String[] args) {
    System.out.println(GenericDAO.findAll(Usuario.class));
}

public String getUrlImagem() {
    return urlImagem;
}

public void setUrlImagem(String urlImagem) {
    this.urlImagem = urlImagem;
}


public String getTipoUsuario() {
    return tipoUsuario;
}

public void setTipoUsuario(String tipoUsuario) {
    this.tipoUsuario = tipoUsuario;
}

public Boolean getGerente() {
    return gerente;
}

public void setGerente(Boolean gerente) {
    this.gerente = gerente;
}

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

private String senhaString;

public String getSenhaString() {
    return senhaString;
}

public void setSenhaString(String senhaString) {
    this.senhaString = senhaString;
}   

}

Class Menu: public class Menu Serializable Implements {

private static final long serialVersionUID = -2858941650550980900L;

@javax.persistence.Id
private int Id;

@Column(length = 50)
private String rotulo;

@Column(length = 50)
private String caminho;

@Column
private int nivel;

@Column
private String urlImagem;

@Column
private Boolean gerente;

@OneToMany(fetch = FetchType.EAGER)
@JoinColumn(referencedColumnName = "id")
@Fetch(FetchMode.SUBSELECT)
private List<Menu> menus;

@ManyToOne
@JoinColumn(name = "Menu_pai")
public Menu getMenuPai() {
    return getMenuPai();
}

@Column
private String tipoUsuario;

public int getId() {
    return Id;
}

public void setId(int id) {
    Id = id;
}

public String getRotulo() {
    return rotulo;
}

public void setRotulo(String rotulo) {
    this.rotulo = rotulo;
}

public String getCaminho() {
    return caminho;
}

public void setCaminho(String caminho) {
    this.caminho = caminho;
}

public int getNivel() {
    return nivel;
}

public void setNivel(int nivel) {
    this.nivel = nivel;
}

public String getUrlImagem() {
    return urlImagem;
}

public void setUrlImagem(String urlImagem) {
    this.urlImagem = urlImagem;
}

public List<Menu> getMenus() {
    return menus;
}

public void setMenus(List<Menu> menus) {
    this.menus = menus;
}


public String getTipoUsuario() {
    return tipoUsuario;
}

public void setTipoUsuario(String tipoUsuario) {
    this.tipoUsuario = tipoUsuario;
}

public Boolean getGerente() {
    return gerente;
}

public void setGerente(Boolean gerente) {
    this.gerente = gerente;
}

}

Bean Menu: @Managedbean(name = "Mbmenu") @Sessionscoped public class Menubean Implements Serializable {

private static final long serialVersionUID = 8410295661058502973L;

private MenuDAO dao = new MenuDAO();
private DefaultSubMenu subMenu = new DefaultSubMenu();
private DefaultMenuItem menuItem = new DefaultMenuItem();
private ArrayList<MenuElement> menuItens = new ArrayList<MenuElement>();
private DefaultMenuModel menuModel = new DefaultMenuModel();
private List<DefaultSubMenu> subMenus = new ArrayList<DefaultSubMenu>();

private Menu menu = new Menu();

public ArrayList<MenuElement> getMenuItens() {
    return menuItens;
}

public void setMenuItens(ArrayList<MenuElement> menuItens) {
    this.menuItens = menuItens;
}

public DefaultMenuModel getMenuModel() {
    return menuModel;
}

public void setMenuModel(DefaultMenuModel menuModel) {
    this.menuModel = menuModel;
}

public DefaultSubMenu getSubMenu() {
    return subMenu;
}

public void setSubMenu(DefaultSubMenu subMenu) {
    this.subMenu = subMenu;
}

public List<DefaultSubMenu> getSubMenus() {
    return subMenus;
}

public void setSubMenus(List<DefaultSubMenu> subMenus) {
    this.subMenus = subMenus;
}

public DefaultMenuItem getMenuItem() {
    return menuItem;
}

public void setMenuItem(DefaultMenuItem menuItem) {
    this.menuItem = menuItem;
}

public void modeloDoMenu() {

}

public void salvar(Menu menu) {
    MenuDAO.save(menu);
}

public MenuDAO getDao() {
    return dao;
}

public void setDao(MenuDAO dao) {
    this.dao = dao;
}

public Menu getMenu() {
    return menu;
}

public void setMenu(Menu menu) {
    this.menu = menu;
}

private MenuModel model;

@PostConstruct
public void init(){
    this.constroiMenuModel();
    
    /*
    MenuDAO menuDAO = new MenuDAO();
    String tipo = UsuarioBean.usuarioLogado.getTipoUsuario().toString();
    
    System.out.println("Tipo: "+tipo);
    Boolean gerente = UsuarioBean.usuarioLogado.getGerente();

    System.out.println("É Gerente: "+gerente);

    List<Menu> lista = menuDAO.listar();

    // System.out.println("Tipo dentro do MenuBean: "+ tipo);

    model = new DefaultMenuModel();

    for (Menu menu : lista) {

        // System.out.println("Tipo dentro do For: " + tipo);
        if (menu.getCaminho() == null) {

            DefaultSubMenu subMenu = DefaultSubMenu.builder().label(menu.getRotulo()).build();

            model.getElements().add(subMenu);

            for (Menu item : menu.getMenus()) {

                if (item.getTipoUsuario().equals(tipo) | item.getTipoUsuario().equals("AMBOS")) {

                    if (item.getGerente() == false) {
                        DefaultMenuItem itens = DefaultMenuItem.builder().value(item.getRotulo())
                                .icon(item.getUrlImagem()).ajax(false).url(item.getCaminho()).update("messages")
                                .rendered(true).build();

                        model.getElements().add(itens);
                    } else {
                        DefaultMenuItem itens = DefaultMenuItem.builder().value(item.getRotulo())
                                .icon(item.getUrlImagem()).ajax(false).url(item.getCaminho()).update("messages")
                                .rendered(gerente).build();
                        model.getElements().add(itens);
                    }
                }
            }
        }
    }
    */
}

public MenuModel getModel() {
    return model;
}

public void redirect() throws IOException {
    ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
    ec.redirect(ec.getRequestContextPath());
}

public void save() {
    addMessage("Save", "Data saved");
}

public void update() {
    addMessage("Update", "Data updated");
}

public void delete() {
    FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_WARN, "Delete", "Data deleted");
    FacesContext.getCurrentInstance().addMessage(null, message);
}

public void addMessage(String summary, String detail) {
    FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, summary, detail);
    FacesContext.getCurrentInstance().addMessage(null, message);
}

public List<Menu> listarMenu() {
    MenuDAO dao = new MenuDAO();
    return dao.listarMenus();
}

public List<Menu> listaSubMenuSQL() throws SQLException, IOException {
    MenuDAO dao = new MenuDAO();
    return dao.listaSubMenuSQL();
}

public void salvarMenu(Menu menu) {
    dao.save(menu);

}

public List<Menu> subMenus() {
    return dao.listarSubMenus();
}

// ------------------------------------------------------------------------//

public void onRowEdit(RowEditEvent event) {
    FacesMessage msg = new FacesMessage("Car Edited", ((MenuElement) event.getObject()).getId());
    FacesContext.getCurrentInstance().addMessage(null, msg);
}

public void onRowCancel(RowEditEvent event) {
    FacesMessage msg = new FacesMessage("Edit Cancelled", ((DefaultMenuItem) event.getObject()).getId());
    FacesContext.getCurrentInstance().addMessage(null, msg);
}

public void onCellEdit(CellEditEvent event) {
    Object oldValue = event.getOldValue();
    Object newValue = event.getNewValue();

    if (newValue != null && !newValue.equals(oldValue)) {
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Cell Changed",
                "Old: " + oldValue + ", New:" + newValue);
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
}

public MenuModel constroiMenuModel() {
    
    MenuDAO menuDAO = new MenuDAO();
    String tipo = UsuarioBean.usuarioLogado.getTipoUsuario();
    Boolean gerente = UsuarioBean.usuarioLogado.getGerente();

    List<Menu> lista = menuDAO.listar();

    // System.out.println("Tipo dentro do MenuBean: "+ tipo);

    model = new DefaultMenuModel();

    for (Menu menu : lista) {
        // System.out.println("Tipo dentro do For: " + tipo);
        if (menu.getCaminho() == null) {

            DefaultSubMenu subMenu = DefaultSubMenu.builder().label(menu.getRotulo()).build();

            model.getElements().add(subMenu);

            for (Menu item : menu.getMenus()) {

                if (item.getTipoUsuario().equals(tipo) | item.getTipoUsuario().equals("AMBOS")) {

                    if (item.getGerente() == false) {
                        DefaultMenuItem itens = DefaultMenuItem.builder().value(item.getRotulo())
                                .icon(item.getUrlImagem()).ajax(false).url(item.getCaminho()).update("messages")
                                .rendered(true).build();

                        model.getElements().add(itens);
                    } else {
                        DefaultMenuItem itens = DefaultMenuItem.builder().value(item.getRotulo())
                                .icon(item.getUrlImagem()).ajax(false).url(item.getCaminho()).update("messages")
                                .rendered(gerente).build();
                        model.getElements().add(itens);
                    }
                }
            }
        }
    }
    return model;

}

// ****************************************************************//

public static void main(String[] args) throws SQLException, IOException {

    MenuBean bean = new MenuBean();
    MenuDAO dao = new MenuDAO();
    bean.dao.listar();
}

}

Bean validate User package br.com.checard.bean;

import java.io.Ioexception; import java.io.Serializable; import java.sql.Sqlexception; import java.util.Arraylist; import java.util.List; import java.util.Map;

import javax.annotation.Postconstruct; import javax.enterprise.inject.spi.Bean; import javax.faces.application.Facesmessage; import javax.faces.bean.Managedbean; import javax.faces.bean.Sessionscoped; import javax.faces.context.Externalcontext; import javax.faces.context.Facescontext; import javax.servlet.http.Httpservletrequest; import javax.servlet.http.Httpsession; import javax.swing.Imageicon;

import org.jboss.logging.Logger; import org.primefaces.Primefaces;

import br.com.checard.dao.Menudao; import br.com.checard.dao.Usuariodao; import br.com.checard.domain.Menu; import br.com.checard.domain.Representative; import br.com.checard.domain.Usuario; import br.com.checard.util.Enviar cadastrousuario;

@Managedbean(name = "Mbusuario") @Sessionscoped public class Usuariobean extends Basemb Implements Serializable {

private static final long serialVersionUID = -3187284287083174426L;
public Usuario usuario = new Usuario();
public String novaSenha = "";
public UsuarioDAO dao = new UsuarioDAO();
public static Usuario usuarioLogado = new Usuario();
public Boolean mostrarListaRepresentante = false;
public Representante representanteSelecionado = new Representante();
public ImageIcon img = new ImageIcon();
public String urlImagem = "";
public Usuario usuarioSelecionado = new Usuario();
public MenuDAO menuDao = new MenuDAO();
public List<Usuario> usuariosSelecionados = new ArrayList<Usuario>();
public List<Usuario> usuarios = new ArrayList<Usuario>();


public String getNovaSenha() {
    return novaSenha;
}

public void setNovaSenha(String novaSenha) {
    this.novaSenha = novaSenha;
}

public MenuDAO getMenuDao() {
    return menuDao;
}

public void setMenuDao(MenuDAO menuDao) {
    this.menuDao = menuDao;
}

public List<Usuario> getUsuariosSelecionados() {
    return usuariosSelecionados;
}

public void setUsuariosSelecionados(List<Usuario> usuariosSelecionados) {
    this.usuariosSelecionados = usuariosSelecionados;
}

public ImageIcon getImg() {
    return img;
}

public void setImg(ImageIcon img) {
    this.img = img;
}

public String getUrlImagem() {
    return urlImagem;
}

public void setUrlImagem(String urlImagem) {
    this.urlImagem = urlImagem;
}

public List<Representante> Representante = new ArrayList<Representante>();

public List<Representante> getRepresentante() {
    return Representante;
}

public void setRepresentante(List<Representante> representantes) {
    this.Representante = representantes;
}

public Usuario getUsuario() {
    return usuario;
}

public void setUsuario(Usuario usuario) {
    this.usuario = usuario;
}

public UsuarioDAO getDao() {
    return dao;
}

public void setDao(UsuarioDAO dao) {
    this.dao = dao;
}

public Usuario getUsuarioLogado() {
    return usuarioLogado;
}

public void setUsuarioLogado(Usuario usuarioLogado) {
    this.usuarioLogado = usuarioLogado;
}

public Boolean getMostrarListaRepresentante() {
    return mostrarListaRepresentante;
}

public void setMostrarListaRepresentante(Boolean mostrarListaRepresentante) {
    this.mostrarListaRepresentante = mostrarListaRepresentante;
}

public Representante getRepresentanteSelecionado() {
    return representanteSelecionado;
}

public void setRepresentanteSelecionado(Representante representanteSelecionado) {
    this.representanteSelecionado = representanteSelecionado;
}

public String validarUsuario(Usuario us) throws ClassNotFoundException, SQLException, IOException {
    MenuBean menu = new MenuBean();
    if (dao.validarUsuario(us) != null) {
    
        usuarioLogado = (Usuario) FacesContext.getCurrentInstance().getExternalContext().getSessionMap()
                .put("usuarioLogado", us);

        menu.constroiMenuModel();           
    
        
    return "dashboard2.xhtml";

    } else {

        return "access.xhtml";
    }
}

/*
public String logout() {
    // Redireciona o usuário para tela de login efetuando o logout.
    String loginPage = "/login.xhtml";
    ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
    HttpServletRequest request = (HttpServletRequest) context.getRequest();
    try {context.redirect(request.getContextPath() + loginPage);
    } catch (IOException e) {

        System.out.println(e);
    }
    HttpSession session = (HttpSession) context.getSession(false);
    session.invalidate();
    
    return "logout";
    
}
*/

public String logout(){
   FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
    return "login.xhtml";

}

public Usuario getUsuarioSelecionado() {
    return usuarioSelecionado;
}

public void setUsuarioSelecionado(Usuario usuarioSelecionado) {
    this.usuarioSelecionado = usuarioSelecionado;
}

public List<Usuario> listarUsuariosPorRepresentante(int rep) {
    return dao.listarUsuariosPorRepresentante(rep);
}

public void saveUsuario() throws SQLException, IOException {
    if (dao.salvarUsuario(usuario) != null) {
        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage("Usuario Adicionado: ", usuario.getNome()));

        EnviarCadastroUsuario enviar = new EnviarCadastroUsuario();
        enviar.enviar(usuario);
    } else {
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Erro ao cadastrar usuario"));
    }

    PrimeFaces.current().executeScript("PF('usuarioDialog').hide()");
    PrimeFaces.current().ajax().update("form:messages", "form:dt-usuarios");
}

public String alterarSenhaUsuario(Usuario us, String senha) {

    System.out.println(us.getSenha() + " Nova Senha: " + senha);
    dao.atualizarSenha(us, senha);

    return "login.xhtml";
}



/*
 public void onRowToggle(ToggleEvent event) {
        if (event.getVisibility() == Visibility.VISIBLE) {
            Usuario usuario = (Usuario) event.getData();
            if (product.getOrders() == null) {
                product.setOrders(orderService.getOrders((int) (Math.random() * 10)));
            }
        }
    }

*/

public String getDeleteButtonMessage() {
    if (hasUsuariosSelecionados()) {
        int size = this.usuariosSelecionados.size();
        return size > 1 ? size + " Usuarios selecionados" : "1 Usuario selecionado";
    }

    return "Delete";
}

 public void deleteUsuariosSelecionados() {
        this.usuarios.removeAll(this.usuariosSelecionados);
        this.usuariosSelecionados = null;
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Usuários Excluídos"));
        PrimeFaces.current().ajax().update("form:messages", "form:dt-usuarios");
    }


public void deleteUsuario() {
    this.usuariosSelecionados.remove(this.usuarioSelecionado);
    this.usuarioSelecionado = null;
    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Usuario Removido"));
    PrimeFaces.current().ajax().update("form:messages", "form:dt-usuarios");
}

public boolean hasUsuariosSelecionados() {
    return this.usuariosSelecionados != null && !this.usuariosSelecionados.isEmpty();
}

public List<Usuario> getUsuarios() {
    return usuarios;
}

public void setUsuarios(List<Usuario> usuarios) {
    this.usuarios = usuarios;
}   

}

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.