Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package action;
- import dominio.Funcionario;
- import java.security.InvalidParameterException;
- import java.util.ArrayList;
- import java.util.List;
- import javax.security.auth.login.LoginException;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
- import org.apache.struts.action.ActionForm;
- import org.apache.struts.action.ActionForward;
- import org.apache.struts.action.ActionMapping;
- import org.hibernate.criterion.Restrictions;
- import util.DaoFactory;
- import util.GenericDao;
- /**
- *
- * @author higor
- */
- public class LoginFuncionario extends org.apache.struts.action.Action {
- private static final String SUCCESS = "index";
- private static final String FAILURE = "login";
- /**
- * This is the action called from the Struts framework.
- * @param mapping The ActionMapping used to select this instance.
- * @param form The optional ActionForm bean for this request.
- * @param request The HTTP Request we are processing.
- * @param response The HTTP Response we are processing.
- * @throws java.lang.Exception
- * @return
- */
- @Override
- public ActionForward execute(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response)
- throws Exception {
- HttpSession session = request.getSession();
- List<String> erros = new ArrayList<String>();
- GenericDao<Funcionario> funcionarioDao = DaoFactory.getFuncionarioDao();
- try {
- if (request.getParameter("email") == null || request.getParameter("senha") == null) {
- throw new InvalidParameterException();
- }
- String email = request.getParameter("email");
- Integer senha = Integer.parseInt(request.getParameter("senha"));
- funcionarioDao.beginTransaction();
- Funcionario f = (Funcionario) funcionarioDao.createCriteria()
- .add(Restrictions.eq("email", email))
- .uniqueResult();
- if (f.getSenha() != senha) {
- throw new LoginException();
- }
- session.setAttribute("usuario", f);
- session.setAttribute("funcionario", true);
- funcionarioDao.commit();
- } catch (NumberFormatException e) {
- erros.add("A senha digitada não é um número.");
- } catch (InvalidParameterException e) {
- erros.add("Por favor preencha todos os campos.");
- } catch (NullPointerException e) {
- erros.add("Usuário desconhecido.");
- System.err.println(e);
- } catch (LoginException e) {
- erros.add("Senha incorreta.");
- } finally {
- funcionarioDao.close();
- }
- if (erros.isEmpty()) {
- session.setAttribute("erro_func", null);
- return mapping.findForward(SUCCESS);
- } else {
- session.setAttribute("erro_func", erros);
- return mapping.findForward(FAILURE);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement