private void goToExamsPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String profileLanguage = ((Profile) request.getSession().getAttribute("profile")).getLanguageCode(); request.setAttribute( "examsBySubject", groupBySubjects(examService.getExamHeaders(profileLanguage))); goToPage(request, response, "WEB-INF/view/exams.jsp"); }
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String creationForm = "creationForm"; String create = "create"; String take = "take"; String submit = "submit"; String action = request.getParameter("action"); String profileLanguage = ((Profile) request.getSession().getAttribute("profile")).getLanguageCode(); if (creationForm.equals(action)) { request.setAttribute("subjects", subjectService.getAll(profileLanguage)); goToPage(request, response, "WEB-INF/view/createExam.jsp"); } else if (take.equals(action)) { request.setAttribute("exam", examService.getExam(request.getParameter("id"))); goToPage(request, response, "WEB-INF/view/takeExam.jsp"); } else if (create.equals(action)) { Exam createdExam = examCreationMapper.mapExam(request.getParameterMap()); // TODO move logic from controller String subjectName = createdExam.getSubject().getName(); Subject subject = subjectService.getSubject(subjectName); if (subject.getId().isEmpty()) { subject = subjectService.createSubject(subjectName, profileLanguage); } createdExam = new Exam.Builder(createdExam).subject(subject).build(); examService.createExam(createdExam); request.setAttribute("createdExamName", createdExam.getName()); goToExamsPage(request, response); } else if (submit.equals(action)) { Exam submittedExam = examSubmissionMapper.mapExam(request.getParameterMap()); Exam originalExam = examService.getExam(submittedExam.getId()); request.setAttribute("takenExamName", originalExam.getName()); request.setAttribute("questionsInExam", originalExam.getQuestions().size()); request.setAttribute( "correctlyAnsweredQuestions", examChecker.checkExam(originalExam, submittedExam)); goToExamsPage(request, response); } else { // TODO: Implement wrong action handling. Going to exams view page goToExamsPage(request, response); } }