| 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.errors.EntityNotFoundException; | |
| 8 | import edu.ucsb.cs156.frontiers.models.RosterStudentDTO; | |
| 9 | import edu.ucsb.cs156.frontiers.repositories.CourseRepository; | |
| 10 | import edu.ucsb.cs156.frontiers.services.RosterStudentDTOService; | |
| 11 | import io.swagger.v3.oas.annotations.Operation; | |
| 12 | import io.swagger.v3.oas.annotations.Parameter; | |
| 13 | import io.swagger.v3.oas.annotations.media.Content; | |
| 14 | import io.swagger.v3.oas.annotations.media.Schema; | |
| 15 | import io.swagger.v3.oas.annotations.responses.ApiResponse; | |
| 16 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 17 | import java.io.IOException; | |
| 18 | import java.io.OutputStreamWriter; | |
| 19 | import java.io.Writer; | |
| 20 | import java.nio.charset.StandardCharsets; | |
| 21 | import java.util.List; | |
| 22 | import lombok.extern.slf4j.Slf4j; | |
| 23 | import org.springframework.beans.factory.annotation.Autowired; | |
| 24 | import org.springframework.http.HttpHeaders; | |
| 25 | import org.springframework.http.MediaType; | |
| 26 | import org.springframework.http.ResponseEntity; | |
| 27 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 28 | import org.springframework.web.bind.annotation.GetMapping; | |
| 29 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 30 | import org.springframework.web.bind.annotation.RequestParam; | |
| 31 | import org.springframework.web.bind.annotation.RestController; | |
| 32 | import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody; | |
| 33 | ||
| 34 | @Tag(name = "CSV Downloads") | |
| 35 | @RequestMapping("/api/csv") | |
| 36 | @RestController | |
| 37 | @Slf4j | |
| 38 | public class CSVDownloadsController extends ApiController { | |
| 39 | ||
| 40 | @Autowired private CourseRepository courseRepository; | |
| 41 | ||
| 42 | @Autowired private RosterStudentDTOService rosterStudentDTOService; | |
| 43 | ||
| 44 | @Operation( | |
| 45 | summary = "Download CSV File of Roster Students", | |
| 46 | description = "Returns a CSV file as a response", | |
| 47 | responses = { | |
| 48 | @ApiResponse( | |
| 49 | responseCode = "200", | |
| 50 | description = "CSV file", | |
| 51 | content = | |
| 52 | @Content( | |
| 53 | mediaType = "text/csv", | |
| 54 | schema = @Schema(type = "string", format = "binary"))), | |
| 55 | @ApiResponse(responseCode = "500", description = "Internal Server Error") | |
| 56 | }) | |
| 57 | @GetMapping(value = "/rosterstudents", produces = "text/csv") | |
| 58 | @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)") | |
| 59 | public ResponseEntity<StreamingResponseBody> csvForQuarter( | |
| 60 | @Parameter(name = "courseId", description = "course id", example = "1") @RequestParam | |
| 61 | Long courseId) | |
| 62 | throws EntityNotFoundException, Exception, IOException { | |
| 63 | Course course = | |
| 64 | courseRepository | |
| 65 | .findById(courseId) | |
| 66 |
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)); |
| 67 | StreamingResponseBody stream = | |
| 68 | (outputStream) -> { | |
| 69 | List<RosterStudentDTO> list = rosterStudentDTOService.getRosterStudentDTOs(courseId); | |
| 70 | try (Writer writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8)) { | |
| 71 | try { | |
| 72 | StatefulBeanToCsv<RosterStudentDTO> beanToCsvWriter = | |
| 73 | rosterStudentDTOService.getStatefulBeanToCSV(writer); | |
| 74 |
1
1. lambda$csvForQuarter$1 : removed call to com/opencsv/bean/StatefulBeanToCsv::write → KILLED |
beanToCsvWriter.write(list); |
| 75 | } catch (CsvDataTypeMismatchException | CsvRequiredFieldEmptyException e) { | |
| 76 | log.error("Error writing CSV file", e); | |
| 77 | throw new IOException("Error writing CSV file: " + e.getMessage()); | |
| 78 | } | |
| 79 | } | |
| 80 | }; | |
| 81 | ||
| 82 |
1
1. csvForQuarter : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CSVDownloadsController::csvForQuarter → KILLED |
return ResponseEntity.ok() |
| 83 | .contentType(MediaType.parseMediaType("text/csv; charset=UTF-8")) | |
| 84 | .header( | |
| 85 | HttpHeaders.CONTENT_DISPOSITION, | |
| 86 | String.format("attachment;filename=%s_roster.csv", course.getCourseName())) | |
| 87 | .header(HttpHeaders.CONTENT_TYPE, "text/csv; charset=UTF-8") | |
| 88 | .header(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, HttpHeaders.CONTENT_DISPOSITION) | |
| 89 | .body(stream); | |
| 90 | } | |
| 91 | } | |
Mutations | ||
| 66 |
1.1 |
|
| 74 |
1.1 |
|
| 82 |
1.1 |