@SuppressWarnings("unchecked")
  @Override
  protected void handle(TitoRequest req, Session hs, TemplateRenderer tr, ViewResponse resp)
      throws ErrorResponseException {
    User user = req.getUserSession().consumeAttribute("regUser", User.class);
    if (user == null) user = new User();

    tr.put("theUser", user); // Use "theUser" instead of "user" to avoid name clash
    tr.put("availableLocales", req.getContext().getTitoTranslation().getSupportedLocales());

    final Locale locale = this.getTranslator(req).getLocale();
    List<Course> courses = hs.createQuery("FROM Course WHERE hidden = FALSE").list();
    hs.evict(courses);
    Collections.sort(
        courses,
        new Comparator<Course>() {
          @Override
          public int compare(Course o1, Course o2) {
            String s1 = ObjectUtils.toString(o1.getName(locale), "");
            String s2 = ObjectUtils.toString(o2.getName(locale), "");
            return s1.compareTo(s2);
          }
        });

    tr.put("availableCourses", courses);

    tr.put("invFields", req.getUserSession().consumeAttribute("invFields", Collection.class));
  }
  @SuppressWarnings("unchecked")
  @Override
  protected void handle(TitoRequest req, Session hs, TemplateRenderer tr, ViewResponse resp) {
    Course course = null;
    User user = req.getUserSession().getAuthenticatedUser();

    if (user.getCourseId() != null)
      course =
          (Course) hs.get(Course.class, req.getUserSession().getAuthenticatedUser().getCourseId());
    if (course == null) throw new RuntimeException("Student has no associated course.");

    List<Task> tasks =
        hs.createQuery("FROM Task WHERE hidden = FALSE AND course.id = ?")
            .setLong(0, course.getId())
            .list();

    Map<Long, Answer> taskAnswers = new HashMap<Long, Answer>();

    for (Task task : tasks) {
      // Make sure the necessary lazy data is loaded so the template can call
      // hasCompleteTranslation()
      task.hasCompleteTranslation(this.getTranslator(req).getLocale());

      // See if the task has been solved by the student.
      Answer answer =
          (Answer)
              hs.createQuery("FROM Answer WHERE task.id = ? AND user.id = ?")
                  .setLong(0, task.getId())
                  .setLong(1, user.getId())
                  .uniqueResult();
      if (answer != null) {
        answer.isSuccessful(); // Load lazy stuff needed to evaluate this
        taskAnswers.put(task.getId(), answer);
      }
    }

    tr.put("tasks", tasks);
    tr.put("taskAnswers", taskAnswers);
  }