Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.82 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package action;
  7.  
  8. import dominio.Funcionario;
  9. import java.security.InvalidParameterException;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. import javax.security.auth.login.LoginException;
  13. import javax.servlet.http.HttpServletRequest;
  14. import javax.servlet.http.HttpServletResponse;
  15. import javax.servlet.http.HttpSession;
  16. import org.apache.struts.action.ActionForm;
  17. import org.apache.struts.action.ActionForward;
  18. import org.apache.struts.action.ActionMapping;
  19. import org.hibernate.criterion.Restrictions;
  20. import util.DaoFactory;
  21. import util.GenericDao;
  22.  
  23. /**
  24.  *
  25.  * @author higor
  26.  */
  27. public class LoginFuncionario extends org.apache.struts.action.Action {
  28.  
  29.     private static final String SUCCESS = "index";
  30.     private static final String FAILURE = "login";
  31.  
  32.     /**
  33.      * This is the action called from the Struts framework.
  34.      * @param mapping The ActionMapping used to select this instance.
  35.      * @param form The optional ActionForm bean for this request.
  36.      * @param request The HTTP Request we are processing.
  37.      * @param response The HTTP Response we are processing.
  38.      * @throws java.lang.Exception
  39.      * @return
  40.      */
  41.     @Override
  42.     public ActionForward execute(ActionMapping mapping, ActionForm form,
  43.                     HttpServletRequest request, HttpServletResponse response)
  44.                     throws Exception {
  45.  
  46.         HttpSession session = request.getSession();
  47.  
  48.         List<String> erros = new ArrayList<String>();
  49.  
  50.         GenericDao<Funcionario> funcionarioDao = DaoFactory.getFuncionarioDao();
  51.  
  52.         try {
  53.             if (request.getParameter("email") == null || request.getParameter("senha") == null) {
  54.                 throw new InvalidParameterException();
  55.             }
  56.  
  57.             String email = request.getParameter("email");
  58.             Integer senha = Integer.parseInt(request.getParameter("senha"));
  59.  
  60.             funcionarioDao.beginTransaction();
  61.  
  62.             Funcionario f = (Funcionario) funcionarioDao.createCriteria()
  63.                             .add(Restrictions.eq("email", email))
  64.                             .uniqueResult();
  65.  
  66.  
  67.             if (f.getSenha() != senha) {
  68.                 throw new LoginException();
  69.             }
  70.  
  71.             session.setAttribute("usuario", f);
  72.             session.setAttribute("funcionario", true);
  73.  
  74.             funcionarioDao.commit();
  75.  
  76.         } catch (NumberFormatException e) {
  77.             erros.add("A senha digitada não é um número.");
  78.         } catch (InvalidParameterException e) {
  79.             erros.add("Por favor preencha todos os campos.");
  80.         } catch (NullPointerException e) {
  81.             erros.add("Usuário desconhecido.");
  82.             System.err.println(e);
  83.         } catch (LoginException e) {
  84.             erros.add("Senha incorreta.");
  85.         }  finally {
  86.             funcionarioDao.close();
  87.         }
  88.  
  89.         if (erros.isEmpty()) {
  90.             session.setAttribute("erro_func", null);
  91.             return mapping.findForward(SUCCESS);
  92.         } else {
  93.             session.setAttribute("erro_func", erros);
  94.             return mapping.findForward(FAILURE);
  95.         }
  96.  
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement