public static Boolean run(String userName, String studentGroupCode) throws FenixServiceException {
check(RolePredicates.STUDENT_PREDICATE);
ServiceMonitoring.logService(UnEnrollStudentInGroup.class, userName, studentGroupCode);
StudentGroup studentGroup = FenixFramework.getDomainObject(studentGroupCode);
if (studentGroup == null) {
throw new InvalidSituationServiceException();
}
final List<String> emails = new ArrayList<String>();
final Collection<Person> people = new ArrayList<Person>();
for (final Attends attends : studentGroup.getAttendsSet()) {
final Person person = attends.getRegistration().getPerson();
people.add(person);
}
final Group fixedSetGroup = UserGroup.of(Person.convertToUsers(people));
final Recipient recipient = new Recipient("", fixedSetGroup);
final Collection<Recipient> recipients = new ArrayList<Recipient>();
recipients.add(recipient);
Registration registration = Registration.readByUsername(userName);
Grouping groupProperties = studentGroup.getGrouping();
Attends attend = groupProperties.getStudentAttend(registration);
if (attend == null) {
throw new NotAuthorizedException();
}
IGroupEnrolmentStrategyFactory enrolmentGroupPolicyStrategyFactory = GroupEnrolmentStrategyFactory.getInstance();
IGroupEnrolmentStrategy strategy = enrolmentGroupPolicyStrategyFactory.getGroupEnrolmentStrategyInstance(groupProperties);
boolean resultEmpty = strategy.checkIfStudentGroupIsEmpty(attend, studentGroup);
studentGroup.removeAttends(attend);
if (resultEmpty) {
studentGroup.delete();
return Boolean.FALSE;
}
final StringBuilder executionCourseNames = new StringBuilder();
for (final ExecutionCourse executionCourse : groupProperties.getExecutionCourses()) {
if (executionCourseNames.length() > 0) {
executionCourseNames.append(", ");
}
executionCourseNames.append(executionCourse.getNome());
}
final String message =
messages.getMessage("message.body.grouping.change.unenrolment", registration.getNumber().toString(), studentGroup
.getGroupNumber().toString(), attend.getExecutionCourse().getNome());
SystemSender systemSender = Bennu.getInstance().getSystemSender();
new Message(systemSender, systemSender.getConcreteReplyTos(), recipients,
messages.getMessage("message.subject.grouping.change"), message, "");