Advertisement
n0tch

UsuarioBean.java

May 30th, 2015
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.10 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package com.ifb.bean;
  7.  
  8. import java.io.IOException;
  9. import java.io.UnsupportedEncodingException;
  10. import java.security.MessageDigest;
  11. import java.security.NoSuchAlgorithmException;
  12. import java.util.List;
  13. import javax.faces.bean.ManagedBean;
  14. import javax.faces.bean.RequestScoped;
  15. import javax.faces.context.FacesContext;
  16. import javax.persistence.EntityManager;
  17. import javax.persistence.EntityManagerFactory;
  18. import javax.persistence.EntityTransaction;
  19. import javax.persistence.Persistence;
  20. import javax.persistence.Query;
  21. import javax.servlet.http.HttpSession;
  22.  
  23. /**
  24.  *
  25.  * @author gustavo
  26.  */
  27. @ManagedBean
  28. @RequestScoped
  29. public class UsuarioBean {
  30.  
  31.     public UsuarioBean() {
  32.     }
  33.  
  34.     private Usuario usuario = new Usuario();
  35.     private List<Usuario> listaUsuario;
  36.  
  37.     public Usuario getUsuario() {
  38.         return usuario;
  39.     }
  40.  
  41.     public void setUsuario(Usuario usuario) {
  42.         this.usuario = usuario;
  43.     }
  44.  
  45.     public void setListaUsuario(List<Usuario> listaUsuario) {
  46.         this.listaUsuario = listaUsuario;
  47.     }
  48.  
  49.     public List<Usuario> getListaUsuario() {
  50.         EntityManagerFactory emf = Persistence.createEntityManagerFactory("default");
  51.         EntityManager em = emf.createEntityManager();
  52.  
  53.         Query q = em.createNativeQuery("SELECT * FROM tblusuario;", Usuario.class);
  54.         this.listaUsuario = q.getResultList();
  55.         em.close();
  56.         return listaUsuario;
  57.     }
  58.  
  59.     public void salvar(int id_login) throws IOException, IndexOutOfBoundsException, NoSuchAlgorithmException {
  60.         EntityManagerFactory emf = Persistence.createEntityManagerFactory("default");
  61.         EntityManager em = emf.createEntityManager();
  62.  
  63.         usuario.setFk_login(id_login);
  64.         em.getTransaction().begin();
  65.         em.persist(usuario);
  66.         em.getTransaction().commit();
  67.         em.close();
  68.  
  69.         FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml");
  70.     }
  71.  
  72.     public void excluir(Usuario usuario) {
  73.         EntityManagerFactory emf = Persistence.createEntityManagerFactory("default");
  74.         EntityManager em = emf.createEntityManager();
  75.  
  76.         EntityTransaction tx = em.getTransaction();
  77.         tx.begin();
  78.         usuario = em.merge(usuario);
  79.         em.remove(usuario);
  80.         tx.commit();
  81.         em.close();
  82.     }
  83.  
  84.     public void atualizar() throws IOException {
  85.         EntityManagerFactory emf = Persistence.createEntityManagerFactory("default");
  86.         EntityManager em = emf.createEntityManager();
  87.  
  88.         em.getTransaction().begin();
  89.         Usuario usuarioDet = em.find(Usuario.class, usuario.getId_usuario());
  90.  
  91.         usuarioDet.setApelido(usuario.getApelido());
  92.         usuarioDet.setFk_estado(usuario.getFk_estado());
  93.  
  94.         em.merge(usuarioDet);
  95.         em.getTransaction().commit();
  96.         em.close();
  97.         FacesContext.getCurrentInstance().getExternalContext().redirect("lista_usuario.xhtml");
  98.     }
  99.  
  100.     public String criptografar() throws NoSuchAlgorithmException, UnsupportedEncodingException {
  101.         MessageDigest algoritmo = MessageDigest.getInstance("SHA-256");
  102.  
  103.         byte arraySenha[] = algoritmo.digest(usuario.getApelido().getBytes("UTF-8"));
  104.  
  105.         StringBuilder senhaCripto = new StringBuilder();
  106.         for (byte b : arraySenha) {
  107.             senhaCripto.append(String.format("%02X", b));
  108.         }
  109.         return senhaCripto.toString();
  110.     }
  111.  
  112.     public void validaUsuario() throws IOException, NoSuchAlgorithmException {
  113.  
  114.         EntityManagerFactory emf = Persistence.createEntityManagerFactory("default");
  115.         EntityManager em = emf.createEntityManager();
  116.  
  117.         Query q = em.createNativeQuery("SELECT COUNT(id_usuario) FROM tblusuario AS usuario "
  118.                 + " INNER JOIN tbllogin AS login on usuario.fk_login = login.id_login "
  119.                 + " WHERE usuario.login = ? AND login.senha = ? ");
  120.        
  121.        
  122.         q.setParameter(1, usuario.getApelido());
  123.     q.setParameter(2, this.criptografar());
  124.  
  125.         int total = Integer.parseInt(q.getSingleResult().toString());
  126.  
  127.         em.close();
  128.  
  129.         if (total == 1) {
  130.             HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
  131.             session.setAttribute("usuarioLogado", usuario.getApelido());
  132.  
  133.             FacesContext.getCurrentInstance().getExternalContext().redirect("admin/index.xhtml");
  134.         } else if (total == 0) {
  135.             FacesContext.getCurrentInstance().getExternalContext().redirect("negado.xhtml");
  136.         } else {
  137.             FacesContext.getCurrentInstance().getExternalContext().redirect("erro.xhtml");
  138.         }
  139.     }
  140.  
  141.     public void logout() throws IOException {
  142.         HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
  143.         session.invalidate();
  144.         FacesContext.getCurrentInstance().getExternalContext().redirect("../index.xhtml");
  145.     }
  146.    
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement