Java tutorial
/* <<<<<<< HEAD * Copyright 2015 Garret Leotta, Jonathan Lynch, Joseph Mandarino, * Michael Yeung * * This file is part of GG Testing Center Scheduler. * GG Testing Center Scheduler is free software: you can redistribute it * and/or modify it under the terms of the GNU General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * GG Testing Center Scheduler is distributed in the hope that it will be * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General * Public License for more details. * * You should have received a copy of the GNU General Public License along * with GG Testing Center Scheduler. If not, see http://www.gnu.org/licenses/. */ package com.gigglinggnus.controllers; import com.gigglinggnus.testingcenter.Course; import java.io.File; import java.io.IOException; import java.util.List; import java.util.ArrayList; import java.util.Collections; import java.util.stream.Collectors; import java.time.Duration; import java.time.Instant; import java.time.Clock; import java.time.ZoneId; import java.time.LocalDateTime; import java.time.ZonedDateTime; import java.time.LocalDate; import java.time.LocalTime; import javax.persistence.Persistence; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.TypedQuery; import javax.persistence.NoResultException; import javax.servlet.ServletException; import javax.servlet.RequestDispatcher; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.json.JSONObject; import org.json.JSONArray; import com.gigglinggnus.testingcenter.Appointment; import com.gigglinggnus.testingcenter.Term; import com.gigglinggnus.testingcenter.User; import com.gigglinggnus.testingcenter.Exam; import com.gigglinggnus.testingcenter.Seating; import com.gigglinggnus.testingcenter.CourseExam; import com.gigglinggnus.testingcenter.ExamStatus; public class AdminMakeAppointmentController extends HttpServlet { public AdminMakeAppointmentController() { super(); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { EntityManager em = (EntityManager) request.getSession().getAttribute("em"); Clock clk = (Clock) (request.getSession().getAttribute("clock")); List<Term> terms = Term.getFutureTerms(em, Instant.now(clk)); List<Exam> exams = terms.stream().flatMap(term -> term.getExams().stream()) .filter(exam -> exam.getStatus() == ExamStatus.PENDING).collect(Collectors.toList()); request.setAttribute("exams", exams); RequestDispatcher rd = request.getRequestDispatcher("/admin/make-apmt.jsp"); rd.forward(request, response); } /** * * @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 { EntityManager em = (EntityManager) request.getSession().getAttribute("em"); String msg = request.getParameter("msg"); User user = (User) (request.getSession().getAttribute("user")); Clock clk = (Clock) (request.getSession().getAttribute("clock")); if (msg.equals("lookup_student")) { String studId = request.getParameter("studentId"); User student = em.find(User.class, studId); List<Exam> exams = student.getRegistrableExams(); JSONObject json = new JSONObject(); json.put("students_idNumber", studId); json.put("firstName", student.getFirstName()); json.put("lastName", student.getLastName()); JSONArray jExams = new JSONArray(); for (Exam exam : exams) { JSONObject elem = new JSONObject(); String examId = exam.getExamId(); String termId = exam.getTerm().getTermId(); String start = exam.getInterval().getStart().atZone(ZoneId.systemDefault()).toLocalDate() .toString(); String end = exam.getInterval().getEnd().atZone(ZoneId.systemDefault()).toLocalDate().toString(); elem.put("examId", examId); elem.put("termId", termId); if (exam instanceof CourseExam) { String courseId = ((CourseExam) exam).getCourse().getCourseId(); elem.put("courseId", courseId); elem.put("examType", "Course"); } else { elem.put("courseId", "N/A"); elem.put("examType", "AdHoc"); } elem.put("start", start); elem.put("end", end); jExams.put(elem); } json.put("exams", jExams); response.getWriter().write(json.toString()); } else if (msg.equals("exam_available_timeslots")) { String examId = request.getParameter("examId"); String dateStr = request.getParameter("date"); Exam exam = em.find(Exam.class, examId); LocalDate apptDate = LocalDate.parse(dateStr); List<LocalTime> timeslots = exam.getTimeslotsForDay(apptDate); JSONObject json = new JSONObject(); json.put("examId", examId); JSONArray jTimeSlots = new JSONArray(); for (LocalTime timeslot : timeslots) { String start = timeslot.toString(); String end = timeslot.plus(exam.getDuration()).toString(); JSONObject elem = new JSONObject(); elem.put("start", start); elem.put("end", end); jTimeSlots.put(elem); } json.put("timeSlots", jTimeSlots); response.getWriter().write(json.toString()); } else if (msg.equals("submit-appointment")) { String studId = request.getParameter("studentId"); String examId = request.getParameter("examId"); String stDate = request.getParameter("examDate"); String stTime = request.getParameter("startTime") + ":00"; String stSeat = request.getParameter("seatType"); User student = em.find(User.class, studId); Exam exam = em.find(Exam.class, examId); LocalDate lDate = LocalDate.parse(stDate); LocalTime lTime = LocalTime.parse(stTime); Instant inst = ZonedDateTime.of(lDate, lTime, ZoneId.systemDefault()).toInstant(); JSONObject json = new JSONObject(); try { em.getTransaction().begin(); if (stSeat.equals("normal")) { user.makeAppointment(student, Seating.NORMAL, exam, inst, clk); em.getTransaction().commit(); json.put("success", "Appointment Made!"); response.getWriter().write(json.toString()); } else if (stSeat.equals("setaside")) { user.makeAppointment(student, Seating.SETASIDE, exam, inst, clk); em.getTransaction().commit(); json.put("success", "Appointment Made!"); response.getWriter().write(json.toString()); } else { em.getTransaction().rollback(); json.put("error", "invalid choice of seating"); response.getWriter().write(json.toString()); } } catch (Exception e) { em.getTransaction().rollback(); json.put("error", e.toString()); response.getWriter().write(json.toString()); } } else { JSONObject json = new JSONObject(); json.put("error", msg); response.getWriter().write(json.toString()); } } }