}
public static Boolean enrole(String groupingID, String shiftID, Integer groupNumber, List<String> studentUsernames,
String studentUsername) throws FenixServiceException {
final Bennu rootDomainObject = Bennu.getInstance();
final Grouping grouping = FenixFramework.getDomainObject(groupingID);
if (grouping == null) {
throw new NonExistingServiceException();
}
final Registration userStudent = Registration.readByUsername(studentUsername);
final IGroupEnrolmentStrategyFactory enrolmentGroupPolicyStrategyFactory = GroupEnrolmentStrategyFactory.getInstance();
final IGroupEnrolmentStrategy strategy = enrolmentGroupPolicyStrategyFactory.getGroupEnrolmentStrategyInstance(grouping);
if (grouping.getStudentAttend(studentUsername) == null) {
throw new NoChangeMadeServiceException();
}
Shift shift = null;
if (shiftID != null) {
shift = FenixFramework.getDomainObject(shiftID);
}
Set<String> allStudentsUsernames = new HashSet<String>(studentUsernames);
allStudentsUsernames.add(studentUsername);
Integer result = strategy.enrolmentPolicyNewGroup(grouping, allStudentsUsernames.size(), shift);
if (result.equals(Integer.valueOf(-1))) {
throw new InvalidArgumentsServiceException();
}
if (result.equals(Integer.valueOf(-2))) {
throw new NonValidChangeServiceException();
}
if (result.equals(Integer.valueOf(-3))) {
throw new NotAuthorizedException();
}
final Attends userAttend = grouping.getStudentAttend(userStudent);
if (userAttend == null) {
throw new InvalidStudentNumberServiceException();
}
if (strategy.checkAlreadyEnroled(grouping, studentUsername)) {
throw new InvalidSituationServiceException();
}
StudentGroup newStudentGroup = grouping.readStudentGroupBy(groupNumber);
if (newStudentGroup != null) {
throw new FenixServiceException();
}
if (!strategy.checkStudentsUserNamesInGrouping(studentUsernames, grouping)) {
throw new InvalidStudentNumberServiceException();
}
checkStudentUsernamesAlreadyEnroledInStudentGroup(strategy, studentUsernames, grouping);
newStudentGroup = new StudentGroup(groupNumber, grouping, shift);
for (final String studentUsernameIter : studentUsernames) {
Attends attend = grouping.getStudentAttend(studentUsernameIter);
attend.addStudentGroups(newStudentGroup);
}
userAttend.addStudentGroups(newStudentGroup);
grouping.addStudentGroups(newStudentGroup);
return true;
}