@Checked("RolePredicates.STUDENT_PREDICATE") @Service public static Boolean run(Integer studentGroupCode, String username) throws FenixServiceException { final StudentGroup studentGroup = rootDomainObject.readStudentGroupByOID(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.getAttends().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.getStudentGroups(), studentGroup, studentAttend); studentGroup.addAttends(studentAttend); informStudents(studentGroup, registration, grouping); return Boolean.TRUE; }