Package labsis.usuario.servlets

Source Code of labsis.usuario.servlets.UsuarioServlet

package labsis.usuario.servlets;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import labsis.usuario.managers.Manager;
import labsis.usuario.model.LogUsuario;
import labsis.usuario.model.TipoUsuario;
import labsis.usuario.model.Usuario;

/**
* Servlet implementation class Controler
*/
public class UsuarioServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;

  /**
   * @see HttpServlet#HttpServlet()
   */
  public UsuarioServlet() {
    super();
    // TODO Auto-generated constructor stub
  }

  /**
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
   *      response)
   */
  protected void doGet(HttpServletRequest request,
      HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doPost(request, response);
  }

  /**
   * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
   *      response)
   */
  protected void doPost(HttpServletRequest request,
      HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    String comm = "";
    String msg = "";
    String url = "/usuarios/jsp/login.jsp";
    comm = request.getParameter("comm");
    Usuario user = (Usuario) request.getSession().getAttribute("usuario");

    if (comm == null || comm.isEmpty()) {
      request.setAttribute("msg", msg);
      RequestDispatcher rd = request
          .getRequestDispatcher("/usuarios/jsp/login.jsp");
      rd.forward(request, response);
    }
    // efetua login
    else if (comm.equals("login")) {
      String username = request.getParameter("username");
      String password = request.getParameter("password");
      url = "/usuarios/jsp/login.jsp";
      if (username != null && password != null) {
        Manager m = Manager.getInstace();
        Usuario u = null;
        u = m.login(username, password);
        if (u == null) {
          msg = "Usuario e/ou senha invalidos! Tente novamente.";
        } else {
          request.getSession().setAttribute("usuario", u);
          url = "/usuarios/jsp/welcome.jsp";
        }
      }
      request.setAttribute("msg", msg);
      RequestDispatcher rd = request.getRequestDispatcher(url);
      rd.forward(request, response);
    }
    // navega pelo sistema apenas com usuario logado
    else if (user != null) {
      // efetua logoff
      if (comm.equals("logoff")) {
        Manager m = Manager.getInstace();
        url = "/usuarios/jsp/login.jsp";
        if (m.logoff(user.getUsername())) {
          request.getSession().removeAttribute("usuario");
          msg = "Logged off";
        } else {
          url = "/usuarios/jsp/welcome.jsp";
          msg = "Erro ao efetuar log off!";
        }
        request.setAttribute("msg", msg);
        RequestDispatcher rd = request.getRequestDispatcher(url);
        rd.forward(request, response);
      }
      // consulta usuarios
      else if (comm.equals("consultaUsuario")
          || comm.equals("atualizaUsuario")
          || comm.equals("deletaUsuario")) {
        url = "/usuarios/jsp/consultaUsuario.jsp";
        Manager m = Manager.getInstace();

        int pageNum = 1;
        Integer totalPages = 1;
        String page = request.getParameter("pageNum");
        if (page != null && !page.equals("")) {
          pageNum = Integer.parseInt(page);
        }
        List<Usuario> users = m.retrieveUsers(user.getUsername(),
            pageNum);
        totalPages = Integer.parseInt(users.remove(users.size() - 1)
            .getUsername());
        request.setAttribute("totalPages", totalPages);
        request.setAttribute("pageNum", pageNum);
        request.setAttribute("users", users);

        request.setAttribute("msg", msg);
        RequestDispatcher rd = request.getRequestDispatcher(url);
        rd.forward(request, response);
      }
      // cadastro de usuarios
      else if (comm.equals("cadastraUsuario")) {
        url = "/usuarios/jsp/cadastroUsuario.jsp";
        Manager m = Manager.getInstace();

        List<TipoUsuario> tipousers = m.getTipoUsuarios();
        request.setAttribute("tipousers", tipousers);

        if (tipousers.size() == 0) {
          url = "/usuarios/jsp/welcome.jsp";
          msg = "Erro ao carregar tipos de usuarios!";
        }

        request.setAttribute("msg", msg);
        RequestDispatcher rd = request.getRequestDispatcher(url);
        rd.forward(request, response);
      }
      // cria usuario
      else if (comm.equals("create")) {
        url = "/usuarios/jsp/welcome.jsp";
        Manager m = Manager.getInstace();
        String nome = request.getParameter("nome");
        String username = request.getParameter("username");
        Integer tipousuarioid = Integer.parseInt(request
            .getParameter("tipousuario"));
        String senha = request.getParameter("senha");
        msg = m.createUser(username, senha, nome, tipousuarioid, user
            .getUsername());
        request.setAttribute("msg", msg);
        RequestDispatcher rd = request.getRequestDispatcher(url);
        rd.forward(request, response);
      }
      // deleta usuarios
      else if (comm.equals("delete")) {
        url = "/usuarios/jsp/welcome.jsp";
        Manager m = Manager.getInstace();
        String username = request.getParameter("username");

        msg = m.deleteUser(username, user.getUsername());
        if (username.equals(user.getUsername())) {
          url = "/usuarios/jsp/login.jsp";
          request.getSession().removeAttribute("usuario");
        }
        request.setAttribute("msg", msg);
        RequestDispatcher rd = request.getRequestDispatcher(url);
        rd.forward(request, response);
      }
      // alteracao de usuario
      else if (comm.equals("update")) {
        url = "/usuarios/jsp/alteraUsuario.jsp";
        Manager m = Manager.getInstace();
        String username = request.getParameter("username");

        Usuario u = m.getUser(username);
        request.getSession().setAttribute("alterUser", u);

        List<TipoUsuario> tipousers = m.getTipoUsuarios();
        request.setAttribute("tipousers", tipousers);

        request.setAttribute("msg", msg);
        RequestDispatcher rd = request.getRequestDispatcher(url);
        rd.forward(request, response);
      }
      // altera usuar
      else if (comm.equals("commitUpdate")) {
        url = "/usuarios/jsp/welcome.jsp";
        Manager m = Manager.getInstace();
        Usuario u = (Usuario) request.getSession().getAttribute(
            "alterUser");
        String nome = request.getParameter("nome");
        String username = u.getUsername();
        System.out.println(request.getParameter("tipousuario"));
        Integer tipousuarioid = Integer.parseInt(request
            .getParameter("tipousuario"));
        String senha = request.getParameter("novasenha");
        if (senha.equals("")) {
          senha = (u).getSenha();
          request.getSession().removeAttribute("alterUser");
        }
        msg = m.updateUser(username, nome, senha, tipousuarioid, user
            .getUsername());

        request.setAttribute("msg", msg);
        RequestDispatcher rd = request.getRequestDispatcher(url);
        rd.forward(request, response);
      }
      // consulta log de usuarios
      else if (comm.equals("consultaLog")) {
        url = "/usuarios/jsp/consultaLog.jsp";
        Manager m = Manager.getInstace();
        int pageNum = 1;
        int totalPages = 1;
        String page = request.getParameter("pageNum");
        if (page != null && !page.equals("")) {
          pageNum = Integer.parseInt(page);
        }
        List<LogUsuario> l = m.retrieveLogs(pageNum);
        totalPages = ((LogUsuario) l.remove(l.size() - 1)).getId();
        request.setAttribute("logs", l);
        request.setAttribute("totalPages", totalPages);
        request.setAttribute("pageNum", pageNum);

        request.setAttribute("msg", msg);
        RequestDispatcher rd = request.getRequestDispatcher(url);
        rd.forward(request, response);
        // comandos de vendas
      } else if (comm.equals("novoPedido")
          || comm.equals("consultaCliente")
          || comm.equals("cadastrarCliente")
          || comm.equals("consultaPedido")) {
        RequestDispatcher rd = getServletContext()
            .getRequestDispatcher("/vendas");
        rd.forward(request, response);
      }// fim dos comandos de vendas

      // comandos de log�stica
      else if (comm.equals("avaliarPedidoCompra")
          || comm.equals("avaliarPedidoVenda")
          || comm.equals("cadastroProduto")
          || comm.equals("confirmarRecebimentoProduto")
          || comm.equals("cotacaoProduto")
            || comm.equals("pedidoCompra")
            || comm.equals("produtoFornecedor")
            || comm.equals("reavaliarRecebimentoProduto")){
        RequestDispatcher rd = getServletContext()
            .getRequestDispatcher("/logistica");
        rd.forward(request, response);
      }// fim dos comandos de log�stica
     
      // comandos do m�dulo financeiro
      else if (comm.equals("pagcadastrarfornecedor")
          || comm.equals("paglistarfornecedor")
          || comm.equals("pagexcluirfornecedor")
          || comm.equals("paglistarfluxo")
          || comm.equals("paglistarContas")
          || comm.equals("paglistarChequeCliente")
          || comm.equals("pagalterarCredito")
          || comm.equals("pagcadastrarCheque")
          || comm.equals("pagexcluirCheque"))
        {
        RequestDispatcher rd = getServletContext().getRequestDispatcher("/financeiro");
        rd.forward(request, response);
      }
      //fim dos comandos do m�dulo financeiro
    } else {
      request.setAttribute("msg", msg);
      RequestDispatcher rd = request
          .getRequestDispatcher("/usuarios/jsp/login.jsp");
      rd.forward(request, response);
    }
  }
}
TOP

Related Classes of labsis.usuario.servlets.UsuarioServlet

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.