com.mycompany.login.mb.LoginBean.java Source code

Java tutorial

Introduction

Here is the source code for com.mycompany.login.mb.LoginBean.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.login.mb;

import com.mycompany.login.bean.Bombas;
import com.mycompany.login.bean.Usuario;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.servlet.http.HttpSession;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 *
 * @author anderson.freitas
 */
@ManagedBean(name = "loginBean")
public class LoginBean {

    private EntityManager em;
    private List<Bombas> bombasusuario;

    public String logout() {
        SecurityContextHolder.clearContext();
        return "logout";
    }

    public List carregaBombas() {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
        Usuario usuario = (Usuario) session.getAttribute("usuarioLogado");
        Query query = em.createQuery("SELECT numserie FROM Bombas u WHERE u.nomecliente = :nomecliente");
        query.setParameter("nomecliente", usuario.getVinculo());
        bombasusuario = query.getResultList();
        return bombasusuario;
    }

}