| 1 | package edu.ucsb.cs156.frontiers.controllers; | |
| 2 | ||
| 3 | import com.opencsv.bean.StatefulBeanToCsv; | |
| 4 | import com.opencsv.exceptions.CsvDataTypeMismatchException; | |
| 5 | import com.opencsv.exceptions.CsvRequiredFieldEmptyException; | |
| 6 | import edu.ucsb.cs156.frontiers.entities.Course; | |
| 7 | import edu.ucsb.cs156.frontiers.entities.CourseStaff; | |
| 8 | import edu.ucsb.cs156.frontiers.entities.RosterStudent; | |
| 9 | import edu.ucsb.cs156.frontiers.enums.RosterStatus; | |
| 10 | import edu.ucsb.cs156.frontiers.errors.EntityNotFoundException; | |
| 11 | import edu.ucsb.cs156.frontiers.models.NameAndTeam; | |
| 12 | import edu.ucsb.cs156.frontiers.models.RosterStudentDTO; | |
| 13 | import edu.ucsb.cs156.frontiers.repositories.CourseRepository; | |
| 14 | import edu.ucsb.cs156.frontiers.repositories.CourseStaffRepository; | |
| 15 | import edu.ucsb.cs156.frontiers.repositories.RosterStudentRepository; | |
| 16 | import edu.ucsb.cs156.frontiers.services.RosterStudentDTOService; | |
| 17 | import edu.ucsb.cs156.frontiers.services.SectionTranslationService; | |
| 18 | import edu.ucsb.cs156.frontiers.services.TeamCsvService; | |
| 19 | import io.swagger.v3.oas.annotations.Operation; | |
| 20 | import io.swagger.v3.oas.annotations.Parameter; | |
| 21 | import io.swagger.v3.oas.annotations.media.Content; | |
| 22 | import io.swagger.v3.oas.annotations.media.Schema; | |
| 23 | import io.swagger.v3.oas.annotations.responses.ApiResponse; | |
| 24 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 25 | import java.io.IOException; | |
| 26 | import java.io.OutputStreamWriter; | |
| 27 | import java.io.Writer; | |
| 28 | import java.nio.charset.StandardCharsets; | |
| 29 | import java.util.List; | |
| 30 | import java.util.Map; | |
| 31 | import lombok.extern.slf4j.Slf4j; | |
| 32 | import org.apache.commons.csv.CSVFormat; | |
| 33 | import org.apache.commons.csv.CSVPrinter; | |
| 34 | import org.springframework.beans.factory.annotation.Autowired; | |
| 35 | import org.springframework.http.HttpHeaders; | |
| 36 | import org.springframework.http.HttpStatus; | |
| 37 | import org.springframework.http.MediaType; | |
| 38 | import org.springframework.http.ResponseEntity; | |
| 39 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 40 | import org.springframework.web.bind.annotation.GetMapping; | |
| 41 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 42 | import org.springframework.web.bind.annotation.RequestParam; | |
| 43 | import org.springframework.web.bind.annotation.RestController; | |
| 44 | import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody; | |
| 45 | ||
| 46 | @Tag(name = "CSV Downloads") | |
| 47 | @RequestMapping("/api/csv") | |
| 48 | @RestController | |
| 49 | @Slf4j | |
| 50 | public class CSVDownloadsController extends ApiController { | |
| 51 | ||
| 52 | @Autowired private CourseRepository courseRepository; | |
| 53 | ||
| 54 | @Autowired private CourseStaffRepository courseStaffRepository; | |
| 55 | ||
| 56 | @Autowired private RosterStudentDTOService rosterStudentDTOService; | |
| 57 | ||
| 58 | @Autowired private RosterStudentRepository rosterStudentRepository; | |
| 59 | ||
| 60 | @Autowired private SectionTranslationService sectionTranslationService; | |
| 61 | ||
| 62 | @Autowired private TeamCsvService teamCsvService; | |
| 63 | ||
| 64 | @Operation( | |
| 65 | summary = "Download CSV File of Roster Students", | |
| 66 | description = "Returns a CSV file as a response", | |
| 67 | responses = { | |
| 68 | @ApiResponse( | |
| 69 | responseCode = "200", | |
| 70 | description = "CSV file", | |
| 71 | content = | |
| 72 | @Content( | |
| 73 | mediaType = "text/csv", | |
| 74 | schema = @Schema(type = "string", format = "binary"))), | |
| 75 | @ApiResponse(responseCode = "500", description = "Internal Server Error") | |
| 76 | }) | |
| 77 | @GetMapping(value = "/rosterstudents", produces = "text/csv") | |
| 78 | @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)") | |
| 79 | public ResponseEntity<StreamingResponseBody> csvForQuarter( | |
| 80 | @Parameter(name = "courseId", description = "course id", example = "1") @RequestParam | |
| 81 | Long courseId) | |
| 82 | throws EntityNotFoundException, Exception, IOException { | |
| 83 | Course course = | |
| 84 | courseRepository | |
| 85 | .findById(courseId) | |
| 86 |
1
1. lambda$csvForQuarter$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CSVDownloadsController::lambda$csvForQuarter$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, courseId)); |
| 87 | StreamingResponseBody stream = | |
| 88 | (outputStream) -> { | |
| 89 | List<RosterStudentDTO> list = rosterStudentDTOService.getRosterStudentDTOs(courseId); | |
| 90 | try (Writer writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8)) { | |
| 91 | try { | |
| 92 | StatefulBeanToCsv<RosterStudentDTO> beanToCsvWriter = | |
| 93 | rosterStudentDTOService.getStatefulBeanToCSV(writer); | |
| 94 |
1
1. lambda$csvForQuarter$1 : removed call to com/opencsv/bean/StatefulBeanToCsv::write → KILLED |
beanToCsvWriter.write(list); |
| 95 | } catch (CsvDataTypeMismatchException | CsvRequiredFieldEmptyException e) { | |
| 96 | log.error("Error writing CSV file", e); | |
| 97 | throw new IOException("Error writing CSV file: " + e.getMessage()); | |
| 98 | } | |
| 99 | } | |
| 100 | }; | |
| 101 | ||
| 102 |
1
1. csvForQuarter : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CSVDownloadsController::csvForQuarter → KILLED |
return ResponseEntity.ok() |
| 103 | .contentType(MediaType.parseMediaType("text/csv; charset=UTF-8")) | |
| 104 | .header( | |
| 105 | HttpHeaders.CONTENT_DISPOSITION, | |
| 106 | String.format("attachment;filename=%s_roster.csv", course.getCourseName())) | |
| 107 | .header(HttpHeaders.CONTENT_TYPE, "text/csv; charset=UTF-8") | |
| 108 | .header(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, HttpHeaders.CONTENT_DISPOSITION) | |
| 109 | .body(stream); | |
| 110 | } | |
| 111 | ||
| 112 | @Operation( | |
| 113 | summary = "Download CATME CSV File of Roster Students", | |
| 114 | description = "Returns a CSV file as a response", | |
| 115 | responses = { | |
| 116 | @ApiResponse( | |
| 117 | responseCode = "200", | |
| 118 | description = "CSV file", | |
| 119 | content = | |
| 120 | @Content( | |
| 121 | mediaType = "text/csv", | |
| 122 | schema = @Schema(type = "string", format = "binary"))), | |
| 123 | @ApiResponse(responseCode = "500", description = "Internal Server Error") | |
| 124 | }) | |
| 125 | @GetMapping(value = "/catme", produces = "text/csv") | |
| 126 | @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)") | |
| 127 | public ResponseEntity<StreamingResponseBody> catmeCsv( | |
| 128 | @Parameter(name = "courseId", description = "course id", example = "1") @RequestParam | |
| 129 | Long courseId) | |
| 130 | throws EntityNotFoundException { | |
| 131 | Course course = | |
| 132 | courseRepository | |
| 133 | .findById(courseId) | |
| 134 |
1
1. lambda$catmeCsv$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CSVDownloadsController::lambda$catmeCsv$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, courseId)); |
| 135 | StreamingResponseBody stream = | |
| 136 | (outputStream) -> { | |
| 137 | List<RosterStudent> rosterStudents = | |
| 138 | rosterStudentRepository | |
| 139 | .findByCourseIdAndRosterStatusInOrderByFirstNameAscLastNameAscIgnoreCase( | |
| 140 | courseId, List.of(RosterStatus.ROSTER, RosterStatus.MANUAL)); | |
| 141 | Map<String, String> sectionTranslations = | |
| 142 | sectionTranslationService.getSectionTranslations(courseId); | |
| 143 | try (Writer writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8); | |
| 144 | CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT)) { | |
| 145 |
1
1. lambda$catmeCsv$1 : removed call to org/apache/commons/csv/CSVPrinter::printRecord → KILLED |
csvPrinter.printRecord("first", "last", "email", "id", "section", "team"); |
| 146 | for (RosterStudent student : rosterStudents) { | |
| 147 | List<String> teams = student.getTeams(); | |
| 148 |
2
1. lambda$catmeCsv$1 : negated conditional → KILLED 2. lambda$catmeCsv$1 : negated conditional → KILLED |
String team = teams != null && !teams.isEmpty() ? teams.get(0) : ""; |
| 149 |
1
1. lambda$catmeCsv$1 : removed call to org/apache/commons/csv/CSVPrinter::printRecord → KILLED |
csvPrinter.printRecord( |
| 150 | student.getFirstName(), | |
| 151 | student.getLastName(), | |
| 152 | student.getEmail(), | |
| 153 | student.getStudentId(), | |
| 154 | SectionTranslationService.translateSection( | |
| 155 | student.getSection(), sectionTranslations), | |
| 156 | team); | |
| 157 | } | |
| 158 | } | |
| 159 | }; | |
| 160 | ||
| 161 |
1
1. catmeCsv : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CSVDownloadsController::catmeCsv → KILLED |
return ResponseEntity.ok() |
| 162 | .contentType(MediaType.parseMediaType("text/csv; charset=UTF-8")) | |
| 163 | .header( | |
| 164 | HttpHeaders.CONTENT_DISPOSITION, | |
| 165 | String.format("attachment;filename=%s_catme.csv", course.getCourseName())) | |
| 166 | .header(HttpHeaders.CONTENT_TYPE, "text/csv; charset=UTF-8") | |
| 167 | .header(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, HttpHeaders.CONTENT_DISPOSITION) | |
| 168 | .body(stream); | |
| 169 | } | |
| 170 | ||
| 171 | @Operation( | |
| 172 | summary = "Download CSV File of Course Staff", | |
| 173 | description = "Returns a CSV file as a response", | |
| 174 | responses = { | |
| 175 | @ApiResponse( | |
| 176 | responseCode = "200", | |
| 177 | description = "CSV file", | |
| 178 | content = | |
| 179 | @Content( | |
| 180 | mediaType = "text/csv", | |
| 181 | schema = @Schema(type = "string", format = "binary"))), | |
| 182 | @ApiResponse(responseCode = "404", description = "Course not found"), | |
| 183 | @ApiResponse(responseCode = "500", description = "Internal Server Error") | |
| 184 | }) | |
| 185 | @GetMapping(value = "/coursestaff", produces = "text/csv") | |
| 186 | @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)") | |
| 187 | public ResponseEntity<StreamingResponseBody> courseStaffCsv( | |
| 188 | @Parameter(name = "courseId", description = "course id", example = "1") @RequestParam | |
| 189 | Long courseId) | |
| 190 | throws EntityNotFoundException { | |
| 191 | Course course = | |
| 192 | courseRepository | |
| 193 | .findById(courseId) | |
| 194 |
1
1. lambda$courseStaffCsv$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CSVDownloadsController::lambda$courseStaffCsv$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, courseId)); |
| 195 | StreamingResponseBody stream = | |
| 196 | (outputStream) -> { | |
| 197 | Iterable<CourseStaff> staffList = courseStaffRepository.findByCourseId(courseId); | |
| 198 | try (Writer writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8); | |
| 199 | CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT)) { | |
| 200 |
1
1. lambda$courseStaffCsv$1 : removed call to org/apache/commons/csv/CSVPrinter::printRecord → KILLED |
csvPrinter.printRecord("firstName", "lastName", "email"); |
| 201 | for (CourseStaff staff : staffList) { | |
| 202 |
1
1. lambda$courseStaffCsv$1 : removed call to org/apache/commons/csv/CSVPrinter::printRecord → KILLED |
csvPrinter.printRecord(staff.getFirstName(), staff.getLastName(), staff.getEmail()); |
| 203 | } | |
| 204 | } | |
| 205 | }; | |
| 206 | ||
| 207 |
1
1. courseStaffCsv : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CSVDownloadsController::courseStaffCsv → KILLED |
return ResponseEntity.ok() |
| 208 | .contentType(MediaType.parseMediaType("text/csv; charset=UTF-8")) | |
| 209 | .header( | |
| 210 | HttpHeaders.CONTENT_DISPOSITION, | |
| 211 | String.format("attachment;filename=%s_staff.csv", course.getCourseName())) | |
| 212 | .header(HttpHeaders.CONTENT_TYPE, "text/csv; charset=UTF-8") | |
| 213 | .header(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, HttpHeaders.CONTENT_DISPOSITION) | |
| 214 | .body(stream); | |
| 215 | } | |
| 216 | ||
| 217 | @Operation( | |
| 218 | summary = "Download compact Name to Team CSV File", | |
| 219 | description = | |
| 220 | "Returns a CSV file with the roster laid out in several side-by-side Name,Team column" | |
| 221 | + " pairs. Names are sorted by first name and abbreviated as far as possible while" | |
| 222 | + " remaining unique (first name; first name plus last initial; first name plus last" | |
| 223 | + " name). Exact duplicate names are marked with an asterisk.", | |
| 224 | responses = { | |
| 225 | @ApiResponse( | |
| 226 | responseCode = "200", | |
| 227 | description = "CSV file", | |
| 228 | content = | |
| 229 | @Content( | |
| 230 | mediaType = "text/csv", | |
| 231 | schema = @Schema(type = "string", format = "binary"))), | |
| 232 | @ApiResponse(responseCode = "400", description = "columns is less than 1"), | |
| 233 | @ApiResponse(responseCode = "404", description = "Course not found"), | |
| 234 | @ApiResponse(responseCode = "500", description = "Internal Server Error") | |
| 235 | }) | |
| 236 | @GetMapping(value = "/name2team", produces = "text/csv") | |
| 237 | @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)") | |
| 238 | public ResponseEntity<StreamingResponseBody> name2TeamCsv( | |
| 239 | @Parameter(name = "courseId", description = "course id", example = "1") @RequestParam | |
| 240 | Long courseId, | |
| 241 | @Parameter( | |
| 242 | name = "columns", | |
| 243 | description = "number of Name,Team column pairs (default 4)", | |
| 244 | example = "4") | |
| 245 | @RequestParam(defaultValue = "4") | |
| 246 | int columns) | |
| 247 | throws EntityNotFoundException { | |
| 248 |
1
1. name2TeamCsv : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CSVDownloadsController::name2TeamCsv → KILLED |
return teamCsvResponse(courseId, columns, "name2team", teamCsvService::writeName2TeamCsv); |
| 249 | } | |
| 250 | ||
| 251 | @Operation( | |
| 252 | summary = "Download compact Team Table CSV File", | |
| 253 | description = | |
| 254 | "Returns a CSV file with the teams laid out in several side-by-side Team,Name column" | |
| 255 | + " pairs, one row per student, teams sorted by name and balanced across the columns" | |
| 256 | + " so that no team is split. Names are sorted by first name and abbreviated as far" | |
| 257 | + " as possible while remaining unique (first name; first name plus last initial;" | |
| 258 | + " first name plus last name). Exact duplicate names are marked with an asterisk." | |
| 259 | + " Students with no team are listed last with a blank team.", | |
| 260 | responses = { | |
| 261 | @ApiResponse( | |
| 262 | responseCode = "200", | |
| 263 | description = "CSV file", | |
| 264 | content = | |
| 265 | @Content( | |
| 266 | mediaType = "text/csv", | |
| 267 | schema = @Schema(type = "string", format = "binary"))), | |
| 268 | @ApiResponse(responseCode = "400", description = "columns is less than 1"), | |
| 269 | @ApiResponse(responseCode = "404", description = "Course not found"), | |
| 270 | @ApiResponse(responseCode = "500", description = "Internal Server Error") | |
| 271 | }) | |
| 272 | @GetMapping(value = "/teamtable", produces = "text/csv") | |
| 273 | @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)") | |
| 274 | public ResponseEntity<StreamingResponseBody> teamTableCsv( | |
| 275 | @Parameter(name = "courseId", description = "course id", example = "1") @RequestParam | |
| 276 | Long courseId, | |
| 277 | @Parameter( | |
| 278 | name = "columns", | |
| 279 | description = "number of Team,Name column pairs (default 4)", | |
| 280 | example = "4") | |
| 281 | @RequestParam(defaultValue = "4") | |
| 282 | int columns) | |
| 283 | throws EntityNotFoundException { | |
| 284 |
1
1. teamTableCsv : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CSVDownloadsController::teamTableCsv → KILLED |
return teamCsvResponse(courseId, columns, "teamtable", teamCsvService::writeTeamTableCsv); |
| 285 | } | |
| 286 | ||
| 287 | /** Writes one of the compact team CSV tables from a list of abbreviated names and teams. */ | |
| 288 | @FunctionalInterface | |
| 289 | interface TeamCsvWriter { | |
| 290 | void write(Writer writer, List<NameAndTeam> entries, int columns) throws IOException; | |
| 291 | } | |
| 292 | ||
| 293 | /** | |
| 294 | * Shared response for the compact team CSV downloads: looks up the course, validates the column | |
| 295 | * count, loads the ROSTER and MANUAL students sorted by first name, abbreviates their names and | |
| 296 | * streams the table written by the given writer. | |
| 297 | * | |
| 298 | * @param courseId the course id | |
| 299 | * @param columns the number of column pairs, at least 1 | |
| 300 | * @param suffix the file name suffix, e.g. "name2team" gives "<courseName>_name2team.csv" | |
| 301 | * @param csvWriter writes the table | |
| 302 | * @return the streaming CSV response | |
| 303 | * @throws EntityNotFoundException if the course does not exist | |
| 304 | * @throws IllegalArgumentException if columns is less than 1 | |
| 305 | */ | |
| 306 | private ResponseEntity<StreamingResponseBody> teamCsvResponse( | |
| 307 | Long courseId, int columns, String suffix, TeamCsvWriter csvWriter) | |
| 308 | throws EntityNotFoundException { | |
| 309 | Course course = | |
| 310 | courseRepository | |
| 311 | .findById(courseId) | |
| 312 |
1
1. lambda$teamCsvResponse$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CSVDownloadsController::lambda$teamCsvResponse$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, courseId)); |
| 313 |
2
1. teamCsvResponse : negated conditional → KILLED 2. teamCsvResponse : changed conditional boundary → KILLED |
if (columns < 1) { |
| 314 | throw new IllegalArgumentException("columns must be at least 1"); | |
| 315 | } | |
| 316 | StreamingResponseBody stream = | |
| 317 | (outputStream) -> { | |
| 318 | List<RosterStudent> rosterStudents = | |
| 319 | rosterStudentRepository | |
| 320 | .findByCourseIdAndRosterStatusInOrderByFirstNameAscLastNameAscIgnoreCase( | |
| 321 | courseId, List.of(RosterStatus.ROSTER, RosterStatus.MANUAL)); | |
| 322 | List<NameAndTeam> entries = teamCsvService.nameAndTeams(rosterStudents); | |
| 323 | try (Writer writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8)) { | |
| 324 |
1
1. lambda$teamCsvResponse$1 : removed call to edu/ucsb/cs156/frontiers/controllers/CSVDownloadsController$TeamCsvWriter::write → KILLED |
csvWriter.write(writer, entries, columns); |
| 325 | } | |
| 326 | }; | |
| 327 | ||
| 328 |
1
1. teamCsvResponse : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CSVDownloadsController::teamCsvResponse → KILLED |
return ResponseEntity.ok() |
| 329 | .contentType(MediaType.parseMediaType("text/csv; charset=UTF-8")) | |
| 330 | .header( | |
| 331 | HttpHeaders.CONTENT_DISPOSITION, | |
| 332 | String.format("attachment;filename=%s_%s.csv", course.getCourseName(), suffix)) | |
| 333 | .header(HttpHeaders.CONTENT_TYPE, "text/csv; charset=UTF-8") | |
| 334 | .header(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, HttpHeaders.CONTENT_DISPOSITION) | |
| 335 | .body(stream); | |
| 336 | } | |
| 337 | ||
| 338 | @Override | |
| 339 | public Object handleEntityNotFoundException(Throwable e) { | |
| 340 | Map<String, String> response = | |
| 341 | Map.of( | |
| 342 | "type", e.getClass().getSimpleName(), | |
| 343 | "message", e.getMessage()); | |
| 344 |
1
1. handleEntityNotFoundException : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CSVDownloadsController::handleEntityNotFoundException → KILLED |
return ResponseEntity.status(HttpStatus.NOT_FOUND) |
| 345 | .contentType(MediaType.APPLICATION_JSON) | |
| 346 | .body(response); | |
| 347 | } | |
| 348 | } | |
Mutations | ||
| 86 |
1.1 |
|
| 94 |
1.1 |
|
| 102 |
1.1 |
|
| 134 |
1.1 |
|
| 145 |
1.1 |
|
| 148 |
1.1 2.2 |
|
| 149 |
1.1 |
|
| 161 |
1.1 |
|
| 194 |
1.1 |
|
| 200 |
1.1 |
|
| 202 |
1.1 |
|
| 207 |
1.1 |
|
| 248 |
1.1 |
|
| 284 |
1.1 |
|
| 312 |
1.1 |
|
| 313 |
1.1 2.2 |
|
| 324 |
1.1 |
|
| 328 |
1.1 |
|
| 344 |
1.1 |