| 1 | package edu.ucsb.cs156.frontiers.jobs; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.frontiers.entities.Course; | |
| 4 | import edu.ucsb.cs156.frontiers.entities.RosterStudent; | |
| 5 | import edu.ucsb.cs156.frontiers.entities.Team; | |
| 6 | import edu.ucsb.cs156.frontiers.entities.TeamMember; | |
| 7 | import edu.ucsb.cs156.frontiers.models.CanvasGroupDetail; | |
| 8 | import edu.ucsb.cs156.frontiers.models.CanvasGroupSetDetail; | |
| 9 | import edu.ucsb.cs156.frontiers.repositories.CourseRepository; | |
| 10 | import edu.ucsb.cs156.frontiers.repositories.TeamRepository; | |
| 11 | import edu.ucsb.cs156.frontiers.services.CanvasService; | |
| 12 | import edu.ucsb.cs156.frontiers.utilities.CanonicalFormConverter; | |
| 13 | import edu.ucsb.cs156.jobs.services.JobContext; | |
| 14 | import edu.ucsb.cs156.jobs.services.JobContextConsumer; | |
| 15 | import java.util.ArrayList; | |
| 16 | import java.util.Comparator; | |
| 17 | import java.util.HashMap; | |
| 18 | import java.util.HashSet; | |
| 19 | import java.util.LinkedHashMap; | |
| 20 | import java.util.List; | |
| 21 | import java.util.Map; | |
| 22 | import java.util.Objects; | |
| 23 | import java.util.Set; | |
| 24 | import lombok.Builder; | |
| 25 | import lombok.Getter; | |
| 26 | import lombok.extern.slf4j.Slf4j; | |
| 27 | ||
| 28 | /** | |
| 29 | * Updates the groups in a Canvas group set to match the teams in Frontiers: creates a Canvas group | |
| 30 | * for each team that has none, adds and removes group members so that each group's membership | |
| 31 | * matches the team's, and deletes Canvas groups that do not correspond to any team. A team is | |
| 32 | * matched to a group by the team's stored Canvas id first, then by name. Staff teams are skipped: | |
| 33 | * they have no students, so there is nothing to put in a Canvas group. | |
| 34 | * | |
| 35 | * <p>Problems with a single team, group or student (a student who is not enrolled in the Canvas | |
| 36 | * course, a Canvas API call that fails) are logged as warnings and the job proceeds; only a failure | |
| 37 | * to read the group set or the Canvas roster fails the job. | |
| 38 | */ | |
| 39 | @Builder | |
| 40 | @Getter | |
| 41 | @Slf4j | |
| 42 | public class PushTeamsToCanvasJob implements JobContextConsumer { | |
| 43 | ||
| 44 | Course course; | |
| 45 | String groupSetId; | |
| 46 | CanvasService canvasService; | |
| 47 | CourseRepository courseRepository; | |
| 48 | TeamRepository teamRepository; | |
| 49 | ||
| 50 | @Override | |
| 51 | public String getScopeType() { | |
| 52 |
1
1. getScopeType : replaced return value with "" for edu/ucsb/cs156/frontiers/jobs/PushTeamsToCanvasJob::getScopeType → KILLED |
return "course"; |
| 53 | } | |
| 54 | ||
| 55 | @Override | |
| 56 | public Long getScopeId() { | |
| 57 |
1
1. getScopeId : replaced Long return value with 0L for edu/ucsb/cs156/frontiers/jobs/PushTeamsToCanvasJob::getScopeId → KILLED |
return course.getId(); |
| 58 | } | |
| 59 | ||
| 60 | @Override | |
| 61 | public void accept(JobContext ctx) throws Exception { | |
| 62 | course = courseRepository.findById(course.getId()).get(); | |
| 63 | CanvasGroupSetDetail groupSet = canvasService.getCanvasGroupSetDetail(course, groupSetId); | |
| 64 | Map<String, Integer> canvasUserIds = canvasService.getCanvasUserIdsByEmail(course); | |
| 65 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log( |
| 66 | String.format( | |
| 67 | "Pushing %d teams to Canvas group set %s (id %d), which has %d groups", | |
| 68 | course.getTeams().size(), | |
| 69 | groupSet.getName(), | |
| 70 | groupSet.getId(), | |
| 71 | groupSet.getGroups().size())); | |
| 72 | ||
| 73 | Map<Integer, CanvasGroupDetail> groupsByCanvasId = new HashMap<>(); | |
| 74 | Map<String, CanvasGroupDetail> groupsByName = new HashMap<>(); | |
| 75 | for (CanvasGroupDetail group : groupSet.getGroups()) { | |
| 76 | groupsByCanvasId.put(group.getId(), group); | |
| 77 | groupsByName.put(group.getName().trim(), group); | |
| 78 | } | |
| 79 | ||
| 80 | List<Team> teams = new ArrayList<>(course.getTeams()); | |
| 81 |
1
1. accept : removed call to java/util/List::sort → KILLED |
teams.sort(Comparator.comparing(Team::getName)); |
| 82 | Set<Integer> matchedGroupIds = new HashSet<>(); | |
| 83 | ||
| 84 | for (Team team : teams) { | |
| 85 |
1
1. accept : negated conditional → KILLED |
if (Boolean.TRUE.equals(team.getIsStaffTeam())) { |
| 86 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log( |
| 87 | String.format( | |
| 88 | "Skipping staff team %s; staff teams are not pushed to Canvas", team.getName())); | |
| 89 | continue; | |
| 90 | } | |
| 91 | CanvasGroupDetail group = groupsByCanvasId.get(team.getCanvasId()); | |
| 92 |
1
1. accept : negated conditional → KILLED |
if (group == null) { |
| 93 | group = groupsByName.get(team.getName().trim()); | |
| 94 | } | |
| 95 |
1
1. accept : negated conditional → KILLED |
if (group != null) { |
| 96 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log( |
| 97 | String.format( | |
| 98 | "Team %s matches Canvas group %s (id %d)", | |
| 99 | team.getName(), group.getName(), group.getId())); | |
| 100 | } else { | |
| 101 | try { | |
| 102 | Integer newId = canvasService.createCanvasGroup(course, groupSet.getId(), team.getName()); | |
| 103 | group = | |
| 104 | CanvasGroupDetail.builder() | |
| 105 | .id(newId) | |
| 106 | .name(team.getName()) | |
| 107 | .memberUserIdsByEmail(new LinkedHashMap<>()) | |
| 108 | .build(); | |
| 109 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log(String.format("Created Canvas group %s (id %d)", team.getName(), newId)); |
| 110 | } catch (Exception e) { | |
| 111 | warn(ctx, "Could not create Canvas group " + team.getName() + ": " + e.getMessage()); | |
| 112 | continue; | |
| 113 | } | |
| 114 | } | |
| 115 | matchedGroupIds.add(group.getId()); | |
| 116 |
1
1. accept : negated conditional → KILLED |
if (!Objects.equals(team.getCanvasId(), group.getId())) { |
| 117 |
1
1. accept : removed call to edu/ucsb/cs156/frontiers/entities/Team::setCanvasId → KILLED |
team.setCanvasId(group.getId()); |
| 118 | teamRepository.save(team); | |
| 119 | } | |
| 120 |
1
1. accept : removed call to edu/ucsb/cs156/frontiers/jobs/PushTeamsToCanvasJob::syncMembers → KILLED |
syncMembers(ctx, team, group, canvasUserIds); |
| 121 | } | |
| 122 | ||
| 123 | for (CanvasGroupDetail group : groupSet.getGroups()) { | |
| 124 |
1
1. accept : negated conditional → KILLED |
if (!matchedGroupIds.contains(group.getId())) { |
| 125 | try { | |
| 126 |
1
1. accept : removed call to edu/ucsb/cs156/frontiers/services/CanvasService::deleteCanvasGroup → KILLED |
canvasService.deleteCanvasGroup(course, group.getId()); |
| 127 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log( |
| 128 | String.format( | |
| 129 | "Deleted Canvas group %s (id %d), which has no team in Frontiers", | |
| 130 | group.getName(), group.getId())); | |
| 131 | } catch (Exception e) { | |
| 132 |
1
1. accept : removed call to edu/ucsb/cs156/frontiers/jobs/PushTeamsToCanvasJob::warn → KILLED |
warn( |
| 133 | ctx, | |
| 134 | String.format( | |
| 135 | "Could not delete Canvas group %s (id %d): %s", | |
| 136 | group.getName(), group.getId(), e.getMessage())); | |
| 137 | } | |
| 138 | } | |
| 139 | } | |
| 140 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log("Done pushing teams to Canvas"); |
| 141 | } | |
| 142 | ||
| 143 | private void syncMembers( | |
| 144 | JobContext ctx, Team team, CanvasGroupDetail group, Map<String, Integer> canvasUserIds) { | |
| 145 | Map<String, Integer> wanted = new LinkedHashMap<>(); | |
| 146 | for (TeamMember member : team.getTeamMembers()) { | |
| 147 | RosterStudent student = member.getRosterStudent(); | |
| 148 | String email = CanonicalFormConverter.convertToValidEmail(student.getEmail()); | |
| 149 | Integer userId = canvasUserIds.get(email); | |
| 150 |
1
1. syncMembers : negated conditional → KILLED |
if (userId == null) { |
| 151 |
1
1. syncMembers : removed call to edu/ucsb/cs156/frontiers/jobs/PushTeamsToCanvasJob::warn → KILLED |
warn( |
| 152 | ctx, | |
| 153 | String.format( | |
| 154 | "Student %s on team %s is not enrolled in the Canvas course; skipping", | |
| 155 | email, team.getName())); | |
| 156 | } else { | |
| 157 | wanted.put(email, userId); | |
| 158 | } | |
| 159 | } | |
| 160 | ||
| 161 | Map<String, Integer> current = group.getMemberUserIdsByEmail(); | |
| 162 | for (Map.Entry<String, Integer> entry : wanted.entrySet()) { | |
| 163 |
1
1. syncMembers : negated conditional → KILLED |
if (!current.containsKey(entry.getKey())) { |
| 164 | try { | |
| 165 |
1
1. syncMembers : removed call to edu/ucsb/cs156/frontiers/services/CanvasService::addCanvasGroupMember → KILLED |
canvasService.addCanvasGroupMember(course, group.getId(), entry.getValue()); |
| 166 |
1
1. syncMembers : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log(String.format("Added %s to Canvas group %s", entry.getKey(), group.getName())); |
| 167 | } catch (Exception e) { | |
| 168 |
1
1. syncMembers : removed call to edu/ucsb/cs156/frontiers/jobs/PushTeamsToCanvasJob::warn → KILLED |
warn( |
| 169 | ctx, | |
| 170 | String.format( | |
| 171 | "Could not add %s to Canvas group %s: %s", | |
| 172 | entry.getKey(), group.getName(), e.getMessage())); | |
| 173 | } | |
| 174 | } | |
| 175 | } | |
| 176 | for (Map.Entry<String, Integer> entry : current.entrySet()) { | |
| 177 |
1
1. syncMembers : negated conditional → KILLED |
if (!wanted.containsKey(entry.getKey())) { |
| 178 | try { | |
| 179 |
1
1. syncMembers : removed call to edu/ucsb/cs156/frontiers/services/CanvasService::removeCanvasGroupMember → KILLED |
canvasService.removeCanvasGroupMember(course, group.getId(), entry.getValue()); |
| 180 |
1
1. syncMembers : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log( |
| 181 | String.format("Removed %s from Canvas group %s", entry.getKey(), group.getName())); | |
| 182 | } catch (Exception e) { | |
| 183 |
1
1. syncMembers : removed call to edu/ucsb/cs156/frontiers/jobs/PushTeamsToCanvasJob::warn → KILLED |
warn( |
| 184 | ctx, | |
| 185 | String.format( | |
| 186 | "Could not remove %s from Canvas group %s: %s", | |
| 187 | entry.getKey(), group.getName(), e.getMessage())); | |
| 188 | } | |
| 189 | } | |
| 190 | } | |
| 191 | } | |
| 192 | ||
| 193 | private static void warn(JobContext ctx, String message) { | |
| 194 | log.warn(message); | |
| 195 |
1
1. warn : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log("WARNING: " + message); |
| 196 | } | |
| 197 | } | |
Mutations | ||
| 52 |
1.1 |
|
| 57 |
1.1 |
|
| 65 |
1.1 |
|
| 81 |
1.1 |
|
| 85 |
1.1 |
|
| 86 |
1.1 |
|
| 92 |
1.1 |
|
| 95 |
1.1 |
|
| 96 |
1.1 |
|
| 109 |
1.1 |
|
| 116 |
1.1 |
|
| 117 |
1.1 |
|
| 120 |
1.1 |
|
| 124 |
1.1 |
|
| 126 |
1.1 |
|
| 127 |
1.1 |
|
| 132 |
1.1 |
|
| 140 |
1.1 |
|
| 150 |
1.1 |
|
| 151 |
1.1 |
|
| 163 |
1.1 |
|
| 165 |
1.1 |
|
| 166 |
1.1 |
|
| 168 |
1.1 |
|
| 177 |
1.1 |
|
| 179 |
1.1 |
|
| 180 |
1.1 |
|
| 183 |
1.1 |
|
| 195 |
1.1 |