@Atomic
public void updateGroupingAttends(Grouping grouping, Map<String, Boolean> studentsToRemove, Map<String, Boolean> studentsToAdd) {
for (Map.Entry<String, Boolean> entry : studentsToRemove.entrySet()) {
if (entry.getValue()) {
Attends attends = (Attends) FenixFramework.getDomainObject(entry.getKey());
if (attends != null) {
grouping.getStudentGroupsSet().forEach(studentGroup -> studentGroup.removeAttends(attends));
grouping.removeAttends(attends);
}
}
}
for (Map.Entry<String, Boolean> entry : studentsToAdd.entrySet()) {
if (entry.getValue()) {
Registration registration = (Registration) FenixFramework.getDomainObject(entry.getKey());
if (grouping.getAttendsSet().stream().noneMatch(attends -> attends.getRegistration().equals(registration))) {
Optional<Attends> opt =
registration.getAssociatedAttendsSet().stream()
.filter(attends -> grouping.getExecutionCourses().stream().anyMatch(ec -> attends.isFor(ec)))
.findAny();
if (opt.isPresent()) {
grouping.addAttends(opt.get());
}
}