@Override
 public boolean checkShiftType(Grouping grouping, Shift shift) {
   if (shift != null) {
     return shift.containsType(grouping.getShiftType());
   } else {
     return grouping.getShiftType() == null;
   }
 }
 @Override
 public List checkShiftsType(Grouping grouping, List shifts) {
   List result = new ArrayList();
   if (grouping.getShiftType() != null) {
     for (final Shift shift : (List<Shift>) shifts) {
       if (shift.containsType(grouping.getShiftType())) {
         result.add(shift);
       }
     }
   }
   return result;
 }
  @Override
  public boolean checkAlreadyEnroled(Grouping grouping, String studentUsername) {

    final Attends studentAttend = grouping.getStudentAttend(studentUsername);

    if (studentAttend != null) {
      Collection<StudentGroup> groupingStudentGroups = grouping.getStudentGroupsSet();
      for (final StudentGroup studentGroup : groupingStudentGroups) {
        Collection<Attends> studentGroupAttends = studentGroup.getAttendsSet();
        for (final Attends attend : studentGroupAttends) {
          if (attend == studentAttend) {
            return true;
          }
        }
      }
    }
    return false;
  }
 @Override
 public boolean checkStudentsUserNamesInGrouping(
     List<String> studentUsernames, Grouping grouping) {
   for (final String studentUsername : studentUsernames) {
     if (grouping.getStudentAttend(studentUsername) == null) {
       return false;
     }
   }
   return true;
 }
  @Override
  public boolean checkNumberOfGroups(Grouping grouping, Shift shift) {
    Integer maximumGroupCapacity = grouping.getGroupMaximumNumber();

    if (shift != null && grouping.getDifferentiatedCapacity()) {
      maximumGroupCapacity = shift.getShiftGroupingProperties().getCapacity();
    } else if (maximumGroupCapacity == null) {
      return true;
    }

    int numberOfGroups = 0;
    if (shift != null) {
      numberOfGroups = grouping.readAllStudentGroupsBy(shift).size();
    } else {
      numberOfGroups = grouping.getStudentGroupsWithoutShift().size();
    }
    if (maximumGroupCapacity == null || numberOfGroups < maximumGroupCapacity) {
      return true;
    }
    return false;
  }
  @Override
  public boolean checkPossibleToEnrolInExistingGroup(Grouping grouping, StudentGroup studentGroup) {

    final int numberOfElements = studentGroup.getAttendsSet().size();
    final Integer maximumCapacity = grouping.getMaximumCapacity();
    if (maximumCapacity == null) {
      return true;
    }
    if (numberOfElements < maximumCapacity) {
      return true;
    }

    return false;
  }
  @Override
  public boolean checkEnrolmentDate(Grouping grouping, Calendar actualDate) {
    Long actualDateInMills = new Long(actualDate.getTimeInMillis());
    Long enrolmentBeginDayInMills = null;
    Long enrolmentEndDayInMills = null;

    if (grouping.getEnrolmentBeginDay() != null) {
      enrolmentBeginDayInMills = new Long(grouping.getEnrolmentBeginDay().getTimeInMillis());
    }

    if (grouping.getEnrolmentEndDay() != null) {
      enrolmentEndDayInMills = new Long(grouping.getEnrolmentEndDay().getTimeInMillis());
    }

    if (enrolmentBeginDayInMills == null && enrolmentEndDayInMills == null) {
      return true;
    }

    if (enrolmentBeginDayInMills != null && enrolmentEndDayInMills == null) {
      if (actualDateInMills.compareTo(enrolmentBeginDayInMills) > 0) {
        return true;
      }
    }

    if (enrolmentBeginDayInMills == null && enrolmentEndDayInMills != null) {
      if (actualDateInMills.compareTo(enrolmentEndDayInMills) < 0) {
        return true;
      }
    }

    if (actualDateInMills.compareTo(enrolmentBeginDayInMills) > 0
        && actualDateInMills.compareTo(enrolmentEndDayInMills) < 0) {
      return true;
    }

    return false;
  }
 @Override
 public boolean checkHasShift(Grouping grouping) {
   return grouping.getShiftType() != null;
 }
  @Override
  public boolean checkStudentInGrouping(Grouping grouping, String username) {

    final Attends attend = grouping.getStudentAttend(username);
    return attend != null;
  }