/*TODO Move this displaying logic to separate jstl? Or just move it to jsp*/ private TreeMap<String, Collection<Exam>> groupBySubjects(Collection<Exam> examHeaders) { TreeMap<String, Collection<Exam>> subjectsNameExamMap = new TreeMap<>(); for (Exam exam : examHeaders) { if (subjectsNameExamMap.containsKey(exam.getSubject().getName())) { subjectsNameExamMap.get(exam.getSubject().getName()).add(exam); } else { Set<Exam> subjEx = new HashSet<>(); subjEx.add(exam); subjectsNameExamMap.put(exam.getSubject().getName(), subjEx); } } return subjectsNameExamMap; }
@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); } }