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