com.gigglinggnus.controllers.ManageExamsController.java Source code

Java tutorial

Introduction

Here is the source code for com.gigglinggnus.controllers.ManageExamsController.java

Source

/*
 <<<<<<< 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 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.ExamStatus;
import com.gigglinggnus.util.Interval;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;

public class ManageExamsController extends HttpServlet {

    public ManageExamsController() {
        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());

        Map<Exam, ArrayList<String>> utilMap = new HashMap();
        for (Exam e : exams) {
            Interval<Instant> examInt = e.getInterval();

            LocalDate testDate = examInt.getStart().atZone(ZoneId.systemDefault()).toLocalDate();
            LocalDate endDate = examInt.getEnd().atZone(ZoneId.systemDefault()).toLocalDate();

            Term t = e.getTerm();
            Map<LocalDate, Long> examUtilMap = new HashMap();
            ArrayList<String> examList = new ArrayList();
            while (testDate.isBefore(endDate.plusDays(1))) {

                examList.add(testDate.toString() + "=" + t.utilizationForDay(testDate, clk) + "   ");

                testDate = testDate.plusDays(1);
            }
            utilMap.put(e, examList);

        }

        request.setAttribute("exams", exams);
        request.setAttribute("utilList", utilMap);
        RequestDispatcher rd = request.getRequestDispatcher("/admin/manage-exams.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 examId = request.getParameter("examid");
        String status = request.getParameter("status");
        Clock clk = (Clock) (request.getSession().getAttribute("clock"));
        User user = (User) (request.getSession().getAttribute("user"));

        Exam exam = em.find(Exam.class, examId);

        try {
            em.getTransaction().begin();
        } catch (Exception e) {
        }

        try {
            if (status.equals("approved")) {
                user.changeExamStatus(exam, ExamStatus.APPROVED);
                em.persist(exam);
                em.getTransaction().commit();
                request.setAttribute("msg", "Exam APPROVED");
            } else if (status.equals("denied")) {
                user.changeExamStatus(exam, ExamStatus.DENIED);
                em.persist(exam);
                em.getTransaction().commit();
                request.setAttribute("msg", "Exam Denied");
            } else {
                request.setAttribute("msg", "Invalid status");
            }
        } catch (Exception e) {
            em.getTransaction().rollback();
            request.setAttribute("msg", e.toString());
            throw e;
        }

        RequestDispatcher rd = request.getRequestDispatcher("/home.jsp");
        rd.forward(request, response);
    }
}