Java tutorial
/* * 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 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; public class CheckinController extends HttpServlet { public CheckinController() { super(); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { EntityManager em = (EntityManager) request.getSession().getAttribute("em"); RequestDispatcher rd = request.getRequestDispatcher("/admin/check-in-student.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 { String msg = request.getParameter("msg"); EntityManager em = (EntityManager) request.getSession().getAttribute("em"); Clock clk = (Clock) (request.getSession().getAttribute("clock")); Term term = Term.getTermforInstant(em, Instant.now(clk)); if (msg.equals("get_appointments")) { String userId = request.getParameter("username"); User user = em.find(User.class, userId); JSONArray json = getAppointments(user, term, clk); response.getWriter().write(json.toString()); } else if (msg.equals("do_checkin")) { String userId = request.getParameter("username"); String examId = request.getParameter("examid"); User admin = (User) (request.getSession().getAttribute("user")); User student = em.find(User.class, userId); Exam exam = em.find(Exam.class, examId); Appointment appt = student.getAppointmentByExam(exam); em.getTransaction().begin(); admin.checkInStudent(appt); em.persist(appt); em.persist(student); em.getTransaction().commit(); JSONArray json = getAppointments(student, term, clk); response.getWriter().write(json.toString()); } else { JSONObject json = new JSONObject(); json.put("error", msg); response.getWriter().write(json.toString()); } } private JSONArray getAppointments(User user, Term term, Clock clk) { Instant now = Instant.now(clk); //return a list of appointments in this term List<Appointment> appts = user.getAppointments().stream().filter(appt -> appt.getTerm().equals(term)) .filter(appt -> !appt.isStudentCheckedIn()).collect(Collectors.toList()); //sorted by distance to current time Collections.sort(appts, (Appointment a1, Appointment a2) -> Duration.between(now, a1.getInterval().getStart()).abs() .compareTo(Duration.between(now, a1.getInterval().getStart()).abs())); JSONArray json = new JSONArray(); for (Appointment appt : appts) { String examId = appt.getExam().getExamId(); String startTime = LocalDateTime.from(appt.getInterval().getStart().atZone(ZoneId.systemDefault())) .toString(); String seatNum = appt.prettySeat(); //write JSON JSONObject elem = new JSONObject(); elem.put("exam", examId); elem.put("start", startTime); elem.put("seat", seatNum); json.put(elem); } return json; } }