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