ReportsController.java

  1. package edu.ucsb.cs156.happiercows.controllers;

  2. import edu.ucsb.cs156.happiercows.entities.Report;
  3. import edu.ucsb.cs156.happiercows.entities.ReportLine;
  4. import edu.ucsb.cs156.happiercows.helpers.ReportCSVHelper;
  5. import edu.ucsb.cs156.happiercows.repositories.CommonsRepository;
  6. import edu.ucsb.cs156.happiercows.repositories.ReportLineRepository;
  7. import edu.ucsb.cs156.happiercows.repositories.ReportRepository;
  8. import edu.ucsb.cs156.happiercows.repositories.UserCommonsRepository;
  9. import io.swagger.v3.oas.annotations.tags.Tag;
  10. import io.swagger.v3.oas.annotations.Operation;
  11. import io.swagger.v3.oas.annotations.Parameter;

  12. import org.springframework.data.domain.Sort.Order;
  13. import org.springframework.http.HttpHeaders;
  14. import org.springframework.http.MediaType;
  15. import org.springframework.http.ResponseEntity;

  16. import java.io.ByteArrayInputStream;
  17. import java.io.IOException;
  18. import java.util.List;
  19. import java.util.Optional;

  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.core.io.InputStreamResource;
  22. import org.springframework.core.io.Resource;
  23. import org.springframework.data.domain.Sort;
  24. import org.springframework.security.access.prepost.PreAuthorize;
  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. @Tag(name = "Reports")
  30. @RequestMapping("/api/reports")
  31. @RestController
  32. public class ReportsController extends ApiController {

  33.     @Autowired
  34.     CommonsRepository commonsRepository;

  35.     @Autowired
  36.     UserCommonsRepository userCommonsRepository;

  37.     @Autowired
  38.     ReportRepository reportRepository;

  39.     @Autowired
  40.     ReportLineRepository reportLineRepository;

  41.     @Operation(summary = "Get all report headers")
  42.     @PreAuthorize("hasRole('ROLE_ADMIN')")
  43.     @GetMapping("")
  44.     public Iterable<Report> allReports() {
  45.         Iterable<Report> reports = reportRepository.findAll(
  46.                 Sort.by(List.of(
  47.                         new Order(Sort.Direction.ASC, "commonsId"),
  48.                         new Order(Sort.Direction.DESC, "id"))));
  49.         return reports;
  50.     }

  51.     @Operation(summary = "Get report headers for a given report")
  52.     @PreAuthorize("hasRole('ROLE_ADMIN')")
  53.     @GetMapping("/byReportId")
  54.     public Optional<Report> findByReportId(
  55.             @Parameter(name = "reportId") @RequestParam Long reportId) {
  56.         Optional<Report> reports = reportRepository.findById(reportId);
  57.         return reports;
  58.     }

  59.     @Operation(summary = "Get report headers for a given user commons")
  60.     @PreAuthorize("hasRole('ROLE_ADMIN')")
  61.     @GetMapping("/headers")
  62.     public Iterable<Report> allReportsByCommonsId(
  63.             @Parameter(name = "commonsId") @RequestParam Long commonsId) {
  64.         Iterable<Report> reports = reportRepository.findAllByCommonsId(commonsId);
  65.         return reports;
  66.     }

  67.     @Operation(summary = "Get report lines for a report id")
  68.     @PreAuthorize("hasRole('ROLE_ADMIN')")
  69.     @GetMapping("/lines")
  70.     public Iterable<ReportLine> allLinesByReportId(
  71.             @Parameter(name = "reportId") @RequestParam Long reportId) {
  72.         Iterable<ReportLine> reportLines = reportLineRepository.findAllByReportId(reportId);
  73.         return reportLines;
  74.     }

  75.     @Operation(summary = "Get report lines for a report id and user commons id")
  76.     @PreAuthorize("hasRole('ROLE_ADMIN')")
  77.     @GetMapping("/download")
  78.     public ResponseEntity<Resource> getLinesCSV(
  79.             @Parameter(name = "reportId") @RequestParam Long reportId) throws IOException {

  80.         Iterable<ReportLine> reportLines = reportLineRepository.findAllByReportId(reportId);

  81.         String filename = String.format("report%05d.csv",reportId);

  82.         ByteArrayInputStream bais = ReportCSVHelper.toCSV(reportLines);
  83.         InputStreamResource isr = new InputStreamResource(bais);

  84.         return ResponseEntity.ok()
  85.                 .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + filename)
  86.                 .contentType(MediaType.parseMediaType("application/csv"))
  87.                 .body(isr);
  88.     }

  89. }