| 1 | package edu.ucsb.cs156.courses.controllers; | |
| 2 | ||
| 3 | import com.opencsv.bean.StatefulBeanToCsvBuilder; | |
| 4 | import com.opencsv.exceptions.CsvDataTypeMismatchException; | |
| 5 | import com.opencsv.exceptions.CsvRequiredFieldEmptyException; | |
| 6 | import edu.ucsb.cs156.courses.entities.EnrollmentDataPoint; | |
| 7 | import edu.ucsb.cs156.courses.repositories.EnrollmentDataPointRepository; | |
| 8 | import io.swagger.v3.oas.annotations.Operation; | |
| 9 | import io.swagger.v3.oas.annotations.Parameter; | |
| 10 | import io.swagger.v3.oas.annotations.media.Content; | |
| 11 | import io.swagger.v3.oas.annotations.media.Schema; | |
| 12 | import io.swagger.v3.oas.annotations.responses.ApiResponse; | |
| 13 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 14 | import java.io.IOException; | |
| 15 | import java.io.OutputStreamWriter; | |
| 16 | import java.io.Writer; | |
| 17 | import java.nio.charset.StandardCharsets; | |
| 18 | import java.util.List; | |
| 19 | import lombok.extern.slf4j.Slf4j; | |
| 20 | import org.springframework.beans.factory.annotation.Autowired; | |
| 21 | import org.springframework.data.util.Streamable; | |
| 22 | import org.springframework.http.HttpHeaders; | |
| 23 | import org.springframework.http.MediaType; | |
| 24 | import org.springframework.http.ResponseEntity; | |
| 25 | import org.springframework.web.bind.annotation.GetMapping; | |
| 26 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 27 | import org.springframework.web.bind.annotation.RequestParam; | |
| 28 | import org.springframework.web.bind.annotation.RestController; | |
| 29 | import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody; | |
| 30 | ||
| 31 | @Slf4j | |
| 32 | @Tag(name = "API for enrollment data") | |
| 33 | @RequestMapping("/api/enrollment") | |
| 34 | @RestController | |
| 35 | public class EnrollmentController extends ApiController { | |
| 36 | ||
| 37 |   @Autowired EnrollmentDataPointRepository enrollmentDataPointRepository; | |
| 38 | ||
| 39 |   @Operation( | |
| 40 |       summary = "Download Enrollment Data as CSV File", | |
| 41 |       description = "Returns a CSV file as a response", | |
| 42 |       responses = { | |
| 43 |         @ApiResponse( | |
| 44 |             responseCode = "200", | |
| 45 |             description = "CSV file", | |
| 46 |             content = | |
| 47 |                 @Content( | |
| 48 |                     mediaType = "text/csv", | |
| 49 |                     schema = @Schema(type = "string", format = "binary"))), | |
| 50 |         @ApiResponse(responseCode = "500", description = "Internal Server Error") | |
| 51 |       }) | |
| 52 |   @GetMapping(value = "/csv/quarter", produces = "text/csv") | |
| 53 |   public ResponseEntity<StreamingResponseBody> csvForQuarter( | |
| 54 |       @Parameter(name = "yyyyq", description = "quarter in yyyyq format", example = "20252") | |
| 55 |           @RequestParam | |
| 56 |           String yyyyq, | |
| 57 |       @Parameter( | |
| 58 |               name = "testException", | |
| 59 |               description = "test exception (e.g. CsvDataTypeMismatchException)", | |
| 60 |               example = "") | |
| 61 |           @RequestParam(required = false, defaultValue = "") | |
| 62 |           String testException) | |
| 63 |       throws Exception, IOException { | |
| 64 |     StreamingResponseBody stream = | |
| 65 |         (outputStream) -> { | |
| 66 |           Iterable<EnrollmentDataPoint> iterable = enrollmentDataPointRepository.findByYyyyq(yyyyq); | |
| 67 |           List<EnrollmentDataPoint> list = Streamable.of(iterable).toList(); | |
| 68 | ||
| 69 |           try (Writer writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8)) { | |
| 70 |             try { | |
| 71 | 
1
1. lambda$csvForQuarter$0 : negated conditional → KILLED | 
              if (testException.equals("CsvDataTypeMismatchException")) { | 
| 72 |                 throw new CsvDataTypeMismatchException("test exception"); | |
| 73 |               } | |
| 74 | 
1
1. lambda$csvForQuarter$0 : removed call to com/opencsv/bean/StatefulBeanToCsv::write → KILLED | 
              new StatefulBeanToCsvBuilder<EnrollmentDataPoint>(writer).build().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/courses/controllers/EnrollmentController::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=enrollment_%s.csv", yyyyq)) | |
| 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 | ||
| 71 | 
 
 1.1  | 
|
| 74 | 
 
 1.1  | 
|
| 82 | 
 
 1.1  |