| 1 | package edu.ucsb.cs156.frontiers.services; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.frontiers.entities.RosterStudent; | |
| 4 | import edu.ucsb.cs156.frontiers.entities.Team; | |
| 5 | import edu.ucsb.cs156.frontiers.entities.TeamMember; | |
| 6 | import edu.ucsb.cs156.frontiers.repositories.TeamMemberRepository; | |
| 7 | import java.util.ArrayList; | |
| 8 | import java.util.List; | |
| 9 | import org.springframework.stereotype.Service; | |
| 10 | ||
| 11 | /** | |
| 12 | * Carries out everything that needs to happen when roster students are dropped from a course (that | |
| 13 | * is, when their roster status becomes DROPPED, e.g. because they are missing from a newly uploaded | |
| 14 | * or synced roster). Code that drops students should call {@link #handleDroppedStudents(List)}, so | |
| 15 | * that this class is the single place to add any further work that a drop requires. | |
| 16 | */ | |
| 17 | @Service | |
| 18 | public class StudentDropService { | |
| 19 | ||
| 20 | private final TeamMemberRepository teamMemberRepository; | |
| 21 | ||
| 22 | public StudentDropService(TeamMemberRepository teamMemberRepository) { | |
| 23 | this.teamMemberRepository = teamMemberRepository; | |
| 24 | } | |
| 25 | ||
| 26 | /** | |
| 27 | * Handles the consequences of dropping the given students. Currently, each student is removed | |
| 28 | * from every team they belong to. | |
| 29 | * | |
| 30 | * <p>The team memberships are removed from the given student and team objects as well as from the | |
| 31 | * database. Callers typically hold these objects outside of a transaction and save the students | |
| 32 | * again afterwards (e.g. {@code RemoveStudentsJob} does), so the objects must not keep referring | |
| 33 | * to the deleted memberships. | |
| 34 | * | |
| 35 | * @param droppedStudents the students who have been dropped from their course | |
| 36 | */ | |
| 37 | public void handleDroppedStudents(List<RosterStudent> droppedStudents) { | |
| 38 | List<Long> teamMemberIds = new ArrayList<>(); | |
| 39 | for (RosterStudent student : droppedStudents) { | |
| 40 | teamMemberIds.addAll(detachFromAllTeams(student)); | |
| 41 | } | |
| 42 |
1
1. handleDroppedStudents : negated conditional → KILLED |
if (!teamMemberIds.isEmpty()) { |
| 43 |
1
1. handleDroppedStudents : removed call to edu/ucsb/cs156/frontiers/repositories/TeamMemberRepository::deleteAllByIdInBatch → KILLED |
teamMemberRepository.deleteAllByIdInBatch(teamMemberIds); |
| 44 | } | |
| 45 | } | |
| 46 | ||
| 47 | /** | |
| 48 | * Removes all of a student's team memberships from the student and from the teams. | |
| 49 | * | |
| 50 | * <p>The membership lists are replaced with new lists rather than modified in place. A list | |
| 51 | * loaded by Hibernate remembers the entities it was loaded with, and saving a detached student | |
| 52 | * whose list still remembers deleted memberships fails because Hibernate looks them up again. | |
| 53 | * | |
| 54 | * @param student the student to detach | |
| 55 | * @return the ids of the removed team memberships, which still need to be deleted | |
| 56 | */ | |
| 57 | private List<Long> detachFromAllTeams(RosterStudent student) { | |
| 58 | List<Long> teamMemberIds = new ArrayList<>(); | |
| 59 | List<TeamMember> memberships = student.getTeamMembers(); | |
| 60 |
1
1. detachFromAllTeams : negated conditional → KILLED |
if (memberships != null) { |
| 61 | for (TeamMember membership : memberships) { | |
| 62 | Team team = membership.getTeam(); | |
| 63 | List<TeamMember> remainingMembers = new ArrayList<>(team.getTeamMembers()); | |
| 64 | remainingMembers.remove(membership); | |
| 65 |
1
1. detachFromAllTeams : removed call to edu/ucsb/cs156/frontiers/entities/Team::setTeamMembers → KILLED |
team.setTeamMembers(remainingMembers); |
| 66 | teamMemberIds.add(membership.getId()); | |
| 67 | } | |
| 68 |
1
1. detachFromAllTeams : removed call to edu/ucsb/cs156/frontiers/entities/RosterStudent::setTeamMembers → KILLED |
student.setTeamMembers(new ArrayList<>()); |
| 69 | } | |
| 70 |
1
1. detachFromAllTeams : replaced return value with Collections.emptyList for edu/ucsb/cs156/frontiers/services/StudentDropService::detachFromAllTeams → KILLED |
return teamMemberIds; |
| 71 | } | |
| 72 | } | |
Mutations | ||
| 42 |
1.1 |
|
| 43 |
1.1 |
|
| 60 |
1.1 |
|
| 65 |
1.1 |
|
| 68 |
1.1 |
|
| 70 |
1.1 |