/**
   * Method is to return GradeClass list.
   *
   * @param request - HttpServletRequest
   * @param modelMap - ModelMap attribute.
   * @return list of classGrade
   * @throws AkuraAppException - Detailed exception
   */
  @RequestMapping(value = ATTENDANCE_GRADE_CLASS_LIST_HTM)
  @ResponseBody
  public String populateGradeClass(ModelMap modelMap, HttpServletRequest request)
      throws AkuraAppException {

    StringBuilder allGradeClass = new StringBuilder();
    int gradeId = Integer.parseInt(request.getParameter(SELECTED_VALUE));
    Grade grade = commonService.findGradeById(gradeId);
    List<ClassGrade> classGrades =
        SortUtil.sortClassGradeList(commonService.getClassGradeListByGrade(grade));
    boolean isFirst = true;
    StringBuilder classes = new StringBuilder();

    for (ClassGrade classGrade : classGrades) {
      classes.append(classGrade.getDescription());
      classes.append(AkuraWebConstant.UNDERSCORE_STRING);
      classes.append(classGrade.getClassGradeId());

      if (isFirst) {
        allGradeClass.append(classes.toString()); // no comma
        isFirst = false;
      } else {
        allGradeClass.append(AkuraWebConstant.STRING_COMMA); // comma
        allGradeClass.append(classes.toString());
      }
      classes.delete(0, classes.length());
    }
    return allGradeClass.toString();
  }
  /**
   * Returns the attendance dash board information with pagination.
   *
   * @param modelMap - a model map contains the data.
   * @param request - request scope data.
   * @param response - response scope data.
   * @param attendanceDashboardDto - AttendanceDashboardDto
   * @return - the name of the view to be redirected.
   * @throws AkuraAppException - The exception details that occurred when processing.
   */
  @RequestMapping(value = ATTENDANCE_PAGINATION, method = RequestMethod.POST)
  public ModelAndView getPagination(
      HttpServletRequest request,
      HttpServletResponse response,
      @ModelAttribute(MODEL_ATT_TEMPLATE) AttendanceDashboardDto attendanceDashboardDto,
      ModelMap modelMap)
      throws AkuraAppException {

    boolean flag = false;
    modelMap.addAttribute(STATUS, Boolean.FALSE);

    String selectedClass = request.getParameter(SELECT_CLASSES);

    if (selectedClass != null) {
      ClassGrade classGradeObj = commonService.findClassGrade(Integer.parseInt(selectedClass));

      if (classGradeObj != null) {
        attendanceDashboardDto.setClassDescription(classGradeObj.getDescription());
      }
    }

    setMonthAndClass(request, attendanceDashboardDto);
    attendanceDashboardDto.setAcademicDays(getAcademicDays(attendanceDashboardDto));
    setSelectedValues(attendanceDashboardDto, modelMap);
    List<BestStudentAttendanceTemplate> bestStudentAttendanceList =
        getBestAttendanceInfoWithPagination(modelMap, request, attendanceDashboardDto, flag);

    if (bestStudentAttendanceList == null) {

      int minNo = attendanceDashboardDto.getMinNo() - PAGINATION_NO;
      attendanceDashboardDto.setMinNo(minNo);
      bestStudentAttendanceList =
          dailyAttendanceService.getTopAttendanceList(attendanceDashboardDto);
      modelMap.addAttribute(STATUS, Boolean.TRUE);
      modelMap.addAttribute(PAGE_MIN_NO, minNo);
    }

    if (bestStudentAttendanceList != null) {
      if (attendanceDashboardDto.isFlag()) {

        modelMap.addAttribute(PAGE_MAX_NO, bestStudentAttendanceList.size() + PAGINATION_NO);
      } else {
        modelMap.addAttribute(PAGE_MAX_NO, bestStudentAttendanceList.size());
      }
    }

    modelMap.addAttribute(BEST_STUDENT_ATTENDANCE_LIST, bestStudentAttendanceList);

    return new ModelAndView(VIEW_ATTENDANCE_DASHBOARD, modelMap);
  }
  /**
   * Handle POST requests for attendance dash board view.
   *
   * @param request - HttpServletRequest
   * @param response - HttpServletResponse
   * @param attendanceDashboardDto - AttendanceDashboardDto
   * @param result - BindingResult
   * @param modelView - ModelAndView
   * @param modelMap - ModelMap
   * @return the name of the view.
   * @throws AkuraAppException - The exception details that occurred when processing.
   */
  @RequestMapping(value = ATTENDANCE_DASHBOARD_HTM, method = RequestMethod.POST)
  public ModelAndView searchAttendanceDashboard(
      HttpServletRequest request,
      HttpServletResponse response,
      @ModelAttribute() AttendanceDashboardDto attendanceDashboardDto,
      BindingResult result,
      ModelAndView modelView,
      ModelMap modelMap)
      throws AkuraAppException {

    // String for error messages.
    String message = null;

    // Set the selected month and class.
    setMonthAndClass(request, attendanceDashboardDto);

    attendanceDashboardValidator.validate(attendanceDashboardDto, result);

    // Check the validations.
    if (result.hasErrors()) {
      return new ModelAndView(VIEW_ATTENDANCE_DASHBOARD, modelMap);

    } else {

      // Get the academic days.
      int academicDays = getAcademicDays(attendanceDashboardDto);
      if (academicDays >= AkuraConstant.TEN) {

        String selectedClass = request.getParameter(SELECT_CLASSES);

        if (selectedClass != null) {
          ClassGrade classGradeObj = commonService.findClassGrade(Integer.parseInt(selectedClass));

          if (classGradeObj != null) {
            attendanceDashboardDto.setClassDescription(classGradeObj.getDescription());
          }
        }

        boolean flag = false;

        attendanceDashboardDto.setAcademicDays(academicDays);
        // Get student attendance list
        List<BestStudentAttendanceTemplate> bestStudentAttendanceList =
            getBestAttendanceInfoWithPagination(modelMap, request, attendanceDashboardDto, flag);

        if (bestStudentAttendanceList != null) {
          if (!bestStudentAttendanceList.isEmpty()) {
            modelMap.addAttribute(BEST_STUDENT_ATTENDANCE_LIST, bestStudentAttendanceList);
          } else {
            message = new ErrorMsgLoader().getErrorMessage(NOT_MINIMUM_ATTENDANCE_ERROR);
          }
        } else {
          message = new ErrorMsgLoader().getErrorMessage(NO_DATA_ERROR_MESSAGE);
        }
      } else {
        message = new ErrorMsgLoader().getErrorMessage(WORKING_DAYS_ERROR);
      }

      setSelectedValues(attendanceDashboardDto, modelMap);
      modelMap.addAttribute(MODEL_ATT_MESSAGE, message);
      modelMap.addAttribute(MODEL_ATT_ATTENDANCE_DASHBOARD, attendanceDashboardDto);
    }

    return new ModelAndView(VIEW_ATTENDANCE_DASHBOARD, modelMap);
  }