1 package fr.tiogars.inception.domains.domaintemplate.controllers;
2
3 import java.io.IOException;
4
5 import org.springframework.http.ContentDisposition;
6 import org.springframework.http.HttpHeaders;
7 import org.springframework.http.MediaType;
8 import org.springframework.http.ResponseEntity;
9 import org.springframework.web.bind.annotation.GetMapping;
10 import org.springframework.web.bind.annotation.PathVariable;
11 import org.springframework.web.bind.annotation.PostMapping;
12 import org.springframework.web.bind.annotation.RequestBody;
13 import org.springframework.web.bind.annotation.RequestMapping;
14 import org.springframework.web.bind.annotation.RestController;
15
16 import fr.tiogars.inception.domains.domaintemplate.models.PrintSelectedDomainTemplatesRequest;
17 import fr.tiogars.inception.domains.domaintemplate.services.DomainTemplatePrintService;
18 import fr.tiogars.inception.domains.domaintemplate.services.DomainTemplatesPrintAllService;
19 import fr.tiogars.inception.domains.domaintemplate.services.DomainTemplatesPrintSelectedService;
20 import io.swagger.v3.oas.annotations.Operation;
21 import io.swagger.v3.oas.annotations.responses.ApiResponse;
22 import io.swagger.v3.oas.annotations.responses.ApiResponses;
23 import io.swagger.v3.oas.annotations.tags.Tag;
24 import jakarta.validation.Valid;
25
26
27 @Tag(name = "DomainTemplates", description = "CRUD operations for the DomainTemplate business object.")
28 @RestController
29 @RequestMapping("/api/v1/domaintemplate")
30 public class DomainTemplatePrintController {
31
32 private final DomainTemplatePrintService printService;
33 private final DomainTemplatesPrintAllService printAllService;
34 private final DomainTemplatesPrintSelectedService printSelectedService;
35
36 public DomainTemplatePrintController(DomainTemplatePrintService printService,
37 DomainTemplatesPrintAllService printAllService, DomainTemplatesPrintSelectedService printSelectedService) {
38 this.printService = printService;
39 this.printAllService = printAllService;
40 this.printSelectedService = printSelectedService;
41 }
42
43 @Operation(summary = "Print one DomainTemplate as PDF")
44 @ApiResponses({
45 @ApiResponse(responseCode = "200", description = "Printable PDF."),
46 @ApiResponse(responseCode = "404", description = "The template was not found."),
47 @ApiResponse(responseCode = "422", description = "The PDF font does not support some record characters.")
48 })
49 @GetMapping(value = "/{id}/print.pdf", produces = MediaType.APPLICATION_PDF_VALUE)
50 public ResponseEntity<byte[]> printDomainTemplate(@PathVariable Long id) throws IOException {
51 return printService.print(id)
52 .map(bytes -> pdf(bytes, "domaintemplate-" + id + ".pdf"))
53 .orElseGet(() -> ResponseEntity.notFound().build());
54 }
55
56 @Operation(summary = "Print all DomainTemplates as PDF")
57 @ApiResponses({
58 @ApiResponse(responseCode = "200", description = "Printable PDF."),
59 @ApiResponse(responseCode = "422", description = "The PDF font does not support some record characters.")
60 })
61 @GetMapping(value = "/print.pdf", produces = MediaType.APPLICATION_PDF_VALUE)
62 public ResponseEntity<byte[]> printAllDomainTemplates() throws IOException {
63 return pdf(printAllService.printAll(), "domaintemplates.pdf");
64 }
65
66 @Operation(summary = "Print selected DomainTemplates as PDF")
67 @ApiResponses({
68 @ApiResponse(responseCode = "200", description = "Printable PDF."),
69 @ApiResponse(responseCode = "400", description = "Select between 1 and 1000 positive identifiers."),
70 @ApiResponse(responseCode = "404", description = "One or more selected templates were not found."),
71 @ApiResponse(responseCode = "422", description = "The PDF font does not support some record characters.")
72 })
73 @PostMapping(value = "/print-selected.pdf", consumes = MediaType.APPLICATION_JSON_VALUE,
74 produces = MediaType.APPLICATION_PDF_VALUE)
75 public ResponseEntity<byte[]> printSelectedDomainTemplates(
76 @Valid @RequestBody PrintSelectedDomainTemplatesRequest request) throws IOException {
77 return printSelectedService.printSelected(request.ids())
78 .map(bytes -> pdf(bytes, "domaintemplates-selected.pdf"))
79 .orElseGet(() -> ResponseEntity.notFound().build());
80 }
81
82 private static ResponseEntity<byte[]> pdf(byte[] bytes, String filename) {
83 return ResponseEntity.ok()
84 .contentType(MediaType.APPLICATION_PDF)
85 .header(HttpHeaders.CONTENT_DISPOSITION, ContentDisposition.inline().filename(filename).build().toString())
86 .header(HttpHeaders.CACHE_CONTROL, "no-store")
87 .body(bytes);
88 }
89 }