/*
* User Servlet
*
*/
package servlets;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import models.User;
/**
*
* @author buzz
*/
public class AdminUserController extends HttpServlet {
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
String page = "user";
response.setContentType("text/html;charset=UTF-8");
String servletPath = request.getServletPath();
if(servletPath.equals("/admin/users")) {
if(session.getAttribute("admin") != null && session.getAttribute("admin").equals("true")) {
List<User> users = User.getAll();
request.setAttribute("users", users);
}
}
else {
if(servletPath.equals("/admin/users/search")) {
if(session.getAttribute("admin") != null && session.getAttribute("admin").equals("true")) {
List<User> users = User.getByName(request.getParameter("searchName"));
request.setAttribute("users", users);
}
}
else if(servletPath.equals("/admin/users/new")) {
if(session.getAttribute("admin") != null && session.getAttribute("admin").equals("true")) {
page += "_new";
}
}
else if(servletPath.equals("/admin/users/edit")) {
if(session.getAttribute("admin") != null && session.getAttribute("admin").equals("true")) {
page += "_edit";
String[] tokens = request.getRequestURI().split("/");
User user = User.getById(new Integer(tokens[tokens.length - 1]));
request.setAttribute("user", user);
}
}
else if(servletPath.equals("/admin/users/delete")) {
if(session.getAttribute("admin") != null && session.getAttribute("admin").equals("true")) {
String[] tokens = request.getRequestURI().split("/");
User.delete(new Integer(tokens[tokens.length - 1]));
List<User> users = User.getAll();
request.setAttribute("users", users);
}
}
}
getServletContext().getRequestDispatcher("/templates/admin/header.jsp").include(request, response);
getServletContext().getRequestDispatcher("/templates/admin/" + page + ".jsp").include(request, response);
getServletContext().getRequestDispatcher("/templates/admin/right.jsp").include(request, response);
getServletContext().getRequestDispatcher("/templates/admin/footer.jsp").include(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
String servletPath = request.getServletPath();
if(servletPath.equals("/admin/users/save")) {
if(session.getAttribute("admin") != null && session.getAttribute("admin").equals("true")) {
User user = new User();
user.setUsername(request.getParameter("newUsername"));
user.setPassword(request.getParameter("newPassword"));
user.setMail(request.getParameter("newMail"));
user.save();
}
}
else if(servletPath.equals("/admin/users/update")) {
if(session.getAttribute("admin") != null && session.getAttribute("admin").equals("true")) {
User user = new User();
user.setId(new Integer(request.getParameter("editId")));
user.setUsername(request.getParameter("editUsername"));
user.setPassword(request.getParameter("editPassword"));
user.setMail(request.getParameter("editMail"));
user.update();
}
}
response.sendRedirect(request.getContextPath() + "/admin/users");
}
}