| 1 | package controller;
|
|---|
| 2 |
|
|---|
| 3 | import DAO.AdviceDAO;
|
|---|
| 4 | import DAO.impl.AdviceDAOImpl;
|
|---|
| 5 | import DAO.impl.StudentDAOImpl;
|
|---|
| 6 | import DAO.impl.ProfessorDAOImpl;
|
|---|
| 7 | import model.Advice;
|
|---|
| 8 | import model.Student;
|
|---|
| 9 | import model.Professor;
|
|---|
| 10 |
|
|---|
| 11 | import javax.servlet.ServletException;
|
|---|
| 12 | import javax.servlet.annotation.WebServlet;
|
|---|
| 13 | import javax.servlet.http.HttpServlet;
|
|---|
| 14 | import javax.servlet.http.HttpServletRequest;
|
|---|
| 15 | import javax.servlet.http.HttpServletResponse;
|
|---|
| 16 | import java.io.IOException;
|
|---|
| 17 | import java.sql.Date;
|
|---|
| 18 | import java.sql.SQLException;
|
|---|
| 19 | import java.util.List;
|
|---|
| 20 |
|
|---|
| 21 | @WebServlet("/advice/*")
|
|---|
| 22 | public class AdviceServlet extends HttpServlet {
|
|---|
| 23 | private static final long serialVersionUID = 1L;
|
|---|
| 24 | private AdviceDAO adviceDAO;
|
|---|
| 25 | private StudentDAOImpl studentDAO;
|
|---|
| 26 | private ProfessorDAOImpl professorDAO;
|
|---|
| 27 |
|
|---|
| 28 | public void init() {
|
|---|
| 29 | adviceDAO = new AdviceDAOImpl();
|
|---|
| 30 | studentDAO = new StudentDAOImpl();
|
|---|
| 31 | professorDAO = new ProfessorDAOImpl();
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
|---|
| 35 | throws ServletException, IOException {
|
|---|
| 36 | doGet(request, response);
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | protected void doGet(HttpServletRequest request, HttpServletResponse response)
|
|---|
| 40 | throws ServletException, IOException {
|
|---|
| 41 | String action = request.getPathInfo();
|
|---|
| 42 | if (action == null) action = "/list";
|
|---|
| 43 |
|
|---|
| 44 | try {
|
|---|
| 45 | switch (action) {
|
|---|
| 46 | case "/new":
|
|---|
| 47 | showNewForm(request, response);
|
|---|
| 48 | break;
|
|---|
| 49 | case "/insert":
|
|---|
| 50 | insertAdvice(request, response);
|
|---|
| 51 | break;
|
|---|
| 52 | case "/delete":
|
|---|
| 53 | deleteAdvice(request, response);
|
|---|
| 54 | break;
|
|---|
| 55 | case "/edit":
|
|---|
| 56 | showEditForm(request, response);
|
|---|
| 57 | break;
|
|---|
| 58 | case "/update":
|
|---|
| 59 | updateAdvice(request, response);
|
|---|
| 60 | break;
|
|---|
| 61 | default:
|
|---|
| 62 | listAdvice(request, response);
|
|---|
| 63 | break;
|
|---|
| 64 | }
|
|---|
| 65 | } catch (SQLException ex) {
|
|---|
| 66 | throw new ServletException(ex);
|
|---|
| 67 | }
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | private void listAdvice(HttpServletRequest request, HttpServletResponse response)
|
|---|
| 71 | throws SQLException, IOException, ServletException {
|
|---|
| 72 | List<Advice> listAdvice = adviceDAO.getAll();
|
|---|
| 73 | request.setAttribute("listAdvice", listAdvice);
|
|---|
| 74 | request.getRequestDispatcher("/pages/advice-list.jsp").forward(request, response);
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | private void showNewForm(HttpServletRequest request, HttpServletResponse response)
|
|---|
| 78 | throws ServletException, IOException, SQLException {
|
|---|
| 79 | // We need these lists to populate the dropdowns in the form
|
|---|
| 80 | List<Student> listStudents = studentDAO.getAll();
|
|---|
| 81 | List<Professor> listProfessors = professorDAO.getall();
|
|---|
| 82 | request.setAttribute("listStudents", listStudents);
|
|---|
| 83 | request.setAttribute("listProfessors", listProfessors);
|
|---|
| 84 | request.getRequestDispatcher("/pages/advice-form.jsp").forward(request, response);
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | private void showEditForm(HttpServletRequest request, HttpServletResponse response)
|
|---|
| 88 | throws SQLException, ServletException, IOException {
|
|---|
| 89 | int studentId = Integer.parseInt(request.getParameter("studentId"));
|
|---|
| 90 | int professorId = Integer.parseInt(request.getParameter("professorId"));
|
|---|
| 91 |
|
|---|
| 92 | Advice existingAdvice = adviceDAO.getById(studentId, professorId);
|
|---|
| 93 | request.setAttribute("advice", existingAdvice);
|
|---|
| 94 |
|
|---|
| 95 | // Still need lists if we want to show who is being edited
|
|---|
| 96 | request.setAttribute("listStudents", studentDAO.getAll());
|
|---|
| 97 | request.setAttribute("listProfessors", professorDAO.getall());
|
|---|
| 98 |
|
|---|
| 99 | request.getRequestDispatcher("/pages/advice-form.jsp").forward(request, response);
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | private void insertAdvice(HttpServletRequest request, HttpServletResponse response)
|
|---|
| 103 | throws SQLException, IOException {
|
|---|
| 104 | int studentId = Integer.parseInt(request.getParameter("studentId"));
|
|---|
| 105 | int professorId = Integer.parseInt(request.getParameter("professorId"));
|
|---|
| 106 | String startStr = request.getParameter("startDate");
|
|---|
| 107 | Date startDate;
|
|---|
| 108 | if (startStr == null || startStr.isEmpty()) {
|
|---|
| 109 | startDate = new Date(System.currentTimeMillis());
|
|---|
| 110 | } else {
|
|---|
| 111 | startDate = Date.valueOf(startStr);
|
|---|
| 112 | }
|
|---|
| 113 | String endStr = request.getParameter("endDate");
|
|---|
| 114 | Date endDate = (endStr != null && !endStr.isEmpty()) ? Date.valueOf(endStr) : null;
|
|---|
| 115 |
|
|---|
| 116 | Advice newAdvice = new Advice(studentId, professorId, startDate, endDate);
|
|---|
| 117 | adviceDAO.save(newAdvice);
|
|---|
| 118 | response.sendRedirect(request.getContextPath() + "/advice/list");
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | private void updateAdvice(HttpServletRequest request, HttpServletResponse response)
|
|---|
| 122 | throws SQLException, IOException {
|
|---|
| 123 | int studentId = Integer.parseInt(request.getParameter("studentId"));
|
|---|
| 124 | int professorId = Integer.parseInt(request.getParameter("professorId"));
|
|---|
| 125 | Date startDate = Date.valueOf(request.getParameter("startDate"));
|
|---|
| 126 |
|
|---|
| 127 | String endStr = request.getParameter("endDate");
|
|---|
| 128 | Date endDate = (endStr != null && !endStr.isEmpty()) ? Date.valueOf(endStr) : null;
|
|---|
| 129 |
|
|---|
| 130 | Advice advice = new Advice(studentId, professorId, startDate, endDate);
|
|---|
| 131 | adviceDAO.update(advice);
|
|---|
| 132 | response.sendRedirect(request.getContextPath() + "/advice/list");
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | private void deleteAdvice(HttpServletRequest request, HttpServletResponse response)
|
|---|
| 136 | throws SQLException, IOException {
|
|---|
| 137 | int studentId = Integer.parseInt(request.getParameter("studentId"));
|
|---|
| 138 | int professorId = Integer.parseInt(request.getParameter("professorId"));
|
|---|
| 139 | adviceDAO.delete(studentId, professorId);
|
|---|
| 140 | response.sendRedirect(request.getContextPath() + "/advice/list");
|
|---|
| 141 | }
|
|---|
| 142 | } |
|---|