/**
   * Checks if person belongs to deparment on a specified execution year. The unit path format
   * should be: IST.<DepartmentAcronym>
   *
   * <p>Accepted roles: STUDENT, TEACHER and EMPLOYEE
   *
   * @param person
   * @param groupCheckQuery
   * @return
   * @throws ExcepcaoPersistencia
   * @throws FenixServiceException
   */
  private static Boolean checkDepartmentGroup(Person person, GroupCheckQuery groupCheckQuery)
      throws NonExistingServiceException {

    final String[] unitAcronyms = groupCheckQuery.unitFullPath.split("\\.");
    if (!groupCheckQuery.roleType.equals("TEACHER")
        && !groupCheckQuery.roleType.equals("STUDENT")
        && !groupCheckQuery.roleType.equals("EMPLOYEE")) {
      throw new NonExistingServiceException();
    }

    if (groupCheckQuery.roleType.equals("TEACHER")) {
      return TeacherGroup.get(getDepartment(unitAcronyms), getExecutionYear(groupCheckQuery.year))
          .isMember(person.getUser());
    } else if (groupCheckQuery.roleType.equals("EMPLOYEE")) {
      if (person != null && person.getEmployee() != null) {
        final Department lastDepartmentWorkingPlace =
            person
                .getEmployee()
                .getLastDepartmentWorkingPlace(
                    getExecutionYear(groupCheckQuery.year).getBeginDateYearMonthDay(),
                    getExecutionYear(groupCheckQuery.year).getEndDateYearMonthDay());
        return (lastDepartmentWorkingPlace != null
            && lastDepartmentWorkingPlace.equals(getDepartment(unitAcronyms)));
      }
      return false;
    } else {
      if (person != null && person.getStudent() != null) {
        for (final Registration registration : person.getStudent().getRegistrationsSet()) {
          for (final Enrolment enrolment :
              registration
                  .getLastStudentCurricularPlan()
                  .getEnrolmentsByExecutionYear(getExecutionYear(groupCheckQuery.year))) {
            if (enrolment.getCurricularCourse().getCompetenceCourse() != null) {
              final CompetenceCourse competenceCourse =
                  enrolment.getCurricularCourse().getCompetenceCourse();
              if (competenceCourse.getDepartmentsSet().contains(getDepartment(unitAcronyms))) {
                return true;
              }

              if (competenceCourse.hasDepartmentUnit()
                  && competenceCourse.getDepartmentUnit().getDepartment()
                      == getDepartment(unitAcronyms)) {
                return true;
              }
            }
          }
        }
      }
      return false;
    }
  }
 @Override
 public boolean isMember(User user) {
   if (user == null) {
     return false;
   }
   if (user.getPerson().getStudent() != null) {
     final Set<CompetenceCourse> competenceCourses = executionCourse.getCompetenceCourses();
     for (Registration registration : user.getPerson().getStudent().getRegistrationsSet()) {
       // students of any degree sharing the same competence of the given execution course
       for (StudentCurricularPlan studentCurricularPlan :
           registration.getStudentCurricularPlansSet()) {
         for (Enrolment enrolment : studentCurricularPlan.getEnrolmentsSet()) {
           CompetenceCourse competenceCourse =
               enrolment.getCurricularCourse().getCompetenceCourse();
           if (competenceCourses.contains(competenceCourse)) {
             return true;
           }
         }
       }
       // students attending the given execution course (most will be in the previous case but some
       // may not)
       if (registration.getAttendingExecutionCoursesFor().contains(executionCourse)) {
         return true;
       }
     }
   }
   return false;
 }
  protected void putAllStudentsStatisticsInTheRequest(
      HttpServletRequest request,
      List<StudentCurricularPlan> students,
      ExecutionYear currentMonitoringYear) {
    Map<Integer, TutorStatisticsBean> statisticsByApprovedEnrolmentsNumber =
        new HashMap<Integer, TutorStatisticsBean>();

    int maxApprovedEnrolments = 0;

    for (StudentCurricularPlan scp : students) {
      List<Enrolment> enrolments = scp.getEnrolmentsByExecutionYear(currentMonitoringYear);
      int approvedEnrolments = 0;

      for (Enrolment enrolment : enrolments) {
        if (!enrolment.getCurricularCourse().isAnual()
            && enrolment.getEnrollmentState().equals(EnrollmentState.APROVED)) {
          approvedEnrolments++;
        }
      }

      maxApprovedEnrolments = Math.max(maxApprovedEnrolments, approvedEnrolments);

      if (statisticsByApprovedEnrolmentsNumber.containsKey(approvedEnrolments)) {
        TutorStatisticsBean tutorStatisticsbean =
            statisticsByApprovedEnrolmentsNumber.get(approvedEnrolments);
        tutorStatisticsbean.setStudentsNumber(tutorStatisticsbean.getStudentsNumber() + 1);
      } else {
        TutorStatisticsBean tutorStatisticsBean =
            new TutorStatisticsBean(1, approvedEnrolments, students.size());
        tutorStatisticsBean.setApprovedEnrolmentsNumber(approvedEnrolments);
        statisticsByApprovedEnrolmentsNumber.put(approvedEnrolments, tutorStatisticsBean);
      }
    }

    putStatisticsInTheRequest(
        request,
        maxApprovedEnrolments,
        students.size(),
        statisticsByApprovedEnrolmentsNumber,
        "allStudentsStatistics");
  }