ServiceMonitoring.logService(GroupStudentEnrolment.class, studentGroupCode, username);
final StudentGroup studentGroup = FenixFramework.getDomainObject(studentGroupCode);
if (studentGroup == null) {
throw new InvalidArgumentsServiceException();
}
final Registration registration = Registration.readByUsername(username);
if (registration == null) {
throw new InvalidArgumentsServiceException();
}
final Grouping grouping = studentGroup.getGrouping();
final Attends studentAttend = grouping.getStudentAttend(registration);
if (studentAttend == null) {
throw new NotAuthorizedException();
}
if (studentGroup.getAttendsSet().contains(studentAttend)) {
throw new InvalidSituationServiceException();
}
final IGroupEnrolmentStrategyFactory enrolmentGroupPolicyStrategyFactory = GroupEnrolmentStrategyFactory.getInstance();
final IGroupEnrolmentStrategy strategy = enrolmentGroupPolicyStrategyFactory.getGroupEnrolmentStrategyInstance(grouping);
boolean result = strategy.checkPossibleToEnrolInExistingGroup(grouping, studentGroup);
if (!result) {
throw new InvalidArgumentsServiceException();
}
checkIfStudentIsNotEnrolledInOtherGroups(grouping.getStudentGroupsSet(), studentGroup, studentAttend);
studentGroup.addAttends(studentAttend);