| 1 | package edu.ucsb.cs156.courses.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.courses.collections.ConvertedSectionCollection; | |
| 7 | import edu.ucsb.cs156.courses.documents.ConvertedSection; | |
| 8 | import edu.ucsb.cs156.courses.models.SectionCSVLine; | |
| 9 | import edu.ucsb.cs156.courses.services.SectionCSVLineService; | |
| 10 | import io.swagger.v3.oas.annotations.Operation; | |
| 11 | import io.swagger.v3.oas.annotations.Parameter; | |
| 12 | import io.swagger.v3.oas.annotations.media.Content; | |
| 13 | import io.swagger.v3.oas.annotations.media.Schema; | |
| 14 | import io.swagger.v3.oas.annotations.responses.ApiResponse; | |
| 15 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 16 | import java.io.IOException; | |
| 17 | import java.io.OutputStreamWriter; | |
| 18 | import java.io.Writer; | |
| 19 | import java.nio.charset.StandardCharsets; | |
| 20 | import java.util.List; | |
| 21 | import java.util.stream.Collectors; | |
| 22 | import lombok.extern.slf4j.Slf4j; | |
| 23 | import org.springframework.beans.factory.annotation.Autowired; | |
| 24 | import org.springframework.data.util.Streamable; | |
| 25 | import org.springframework.http.HttpHeaders; | |
| 26 | import org.springframework.http.MediaType; | |
| 27 | import org.springframework.http.ResponseEntity; | |
| 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 | @Slf4j | |
| 35 | @Tag(name = "API for course data as CSV downloads") | |
| 36 | @RequestMapping("/api/courses/csv") | |
| 37 | @RestController | |
| 38 | public class CoursesCSVController extends ApiController { | |
| 39 | ||
| 40 |   @Autowired ConvertedSectionCollection convertedSectionCollection; | |
| 41 | ||
| 42 |   @Autowired private SectionCSVLineService sectionCsvLineService; | |
| 43 | ||
| 44 |   @Operation( | |
| 45 |       summary = "Download Course List as CSV File", | |
| 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 = "/quarter", produces = "text/csv") | |
| 58 |   public ResponseEntity<StreamingResponseBody> csvForCourses( | |
| 59 |       @Parameter(name = "yyyyq", description = "quarter in yyyyq format", example = "20252") | |
| 60 |           @RequestParam | |
| 61 |           String yyyyq) | |
| 62 |       throws Exception, IOException { | |
| 63 |     StreamingResponseBody stream = | |
| 64 |         (outputStream) -> { | |
| 65 |           Iterable<ConvertedSection> iterable = convertedSectionCollection.findByQuarter(yyyyq); | |
| 66 | ||
| 67 |           List<SectionCSVLine> list = | |
| 68 |               Streamable.of(iterable).toList().stream() | |
| 69 |                   .map( | |
| 70 |                       section -> { | |
| 71 | 
1
1. lambda$csvForCourses$0 : replaced return value with null for edu/ucsb/cs156/courses/controllers/CoursesCSVController::lambda$csvForCourses$0 → KILLED | 
                        return SectionCSVLine.toSectionCSVLine(section); | 
| 72 |                       }) | |
| 73 |                   .collect(Collectors.toList()); | |
| 74 | ||
| 75 |           try (Writer writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8)) { | |
| 76 |             try { | |
| 77 |               StatefulBeanToCsv<SectionCSVLine> beanToCsvWriter = | |
| 78 |                   sectionCsvLineService.getStatefulBeanToCSV(writer); | |
| 79 | 
1
1. lambda$csvForCourses$1 : removed call to com/opencsv/bean/StatefulBeanToCsv::write → KILLED | 
              beanToCsvWriter.write(list); | 
| 80 |             } catch (CsvDataTypeMismatchException | CsvRequiredFieldEmptyException e) { | |
| 81 |               log.error("Error writing CSV file", e); | |
| 82 |               throw new IOException("Error writing CSV file: " + e.getMessage()); | |
| 83 |             } | |
| 84 |           } | |
| 85 |         }; | |
| 86 | ||
| 87 | 
1
1. csvForCourses : replaced return value with null for edu/ucsb/cs156/courses/controllers/CoursesCSVController::csvForCourses → KILLED | 
    return ResponseEntity.ok() | 
| 88 |         .contentType(MediaType.parseMediaType("text/csv; charset=UTF-8")) | |
| 89 |         .header( | |
| 90 |             HttpHeaders.CONTENT_DISPOSITION, | |
| 91 |             String.format("attachment;filename=courses_%s.csv", yyyyq)) | |
| 92 |         .header(HttpHeaders.CONTENT_TYPE, "text/csv; charset=UTF-8") | |
| 93 |         .header(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, HttpHeaders.CONTENT_DISPOSITION) | |
| 94 |         .body(stream); | |
| 95 |   } | |
| 96 | } | |
Mutations | ||
| 71 | 
 
 1.1  | 
|
| 79 | 
 
 1.1  | 
|
| 87 | 
 
 1.1  |