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);