Skip to content

Commit e82460b

Browse files
Merge pull request #62 from igorcampos-dev/feat/spring-jasper-bill-example
feat(spring-jasper-example): implement bill report generation with Ja…
2 parents 42e2190 + eb6c105 commit e82460b

21 files changed

+460
-100
lines changed

spring-jasper-example/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Includes loading `.jasper` templates, populating them with mock data, and export
1919

2020
## Profiles you can activate
2121

22-
- **client-list-dev** – Used in Docker, in the future, it will use pre-compiled `.jasper` files instead of `.jrxml`. Activates the internal module that generates a simple client report.
22+
- **client-list-dev** – Used in Docker, Activates the internal module that generates a simple client report.
2323
- **client-list-local** – Used locally, activates the internal module that generates a simple client report and use `.jrxml`.
24-
24+
- **bill-dev** – Used in Docker, Activates the internal module that generates a simple bill report.
25+
- **bill-local** – Used locally, activates the internal module that generates a simple bill report and use `.jrxml`.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.io.example.base;
2+
3+
import net.sf.jasperreports.engine.*;
4+
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
5+
import net.sf.jasperreports.engine.util.JRLoader;
6+
import org.springframework.core.io.ClassPathResource;
7+
8+
import java.io.InputStream;
9+
import java.util.Collection;
10+
import java.util.HashMap;
11+
12+
public class JasperReader {
13+
14+
public static byte[] exportReportJrxmlToPdf(Collection<?> beanCollection, String path){
15+
try {
16+
InputStream stream = new ClassPathResource(path).getInputStream();
17+
JasperReport jasperReport = JasperCompileManager.compileReport(stream);
18+
JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(beanCollection);
19+
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap<>(), dataSource);
20+
return JasperExportManager.exportReportToPdf(jasperPrint);
21+
} catch (Exception e) {
22+
throw new RuntimeException(e);
23+
}
24+
}
25+
26+
public static byte[] exportReportJasperToPdf(Collection<?> beanCollection, String path){
27+
try (InputStream stream = new ClassPathResource(path).getInputStream()) {
28+
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(stream);
29+
JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(beanCollection);
30+
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap<>(), dataSource);
31+
return JasperExportManager.exportReportToPdf(jasperPrint);
32+
} catch (Exception e) {
33+
throw new RuntimeException(e);
34+
}
35+
}
36+
37+
}

spring-jasper-example/src/main/java/com/io/example/clientList/controller/ReportController.java renamed to spring-jasper-example/src/main/java/com/io/example/clientList/controller/ClientsController.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.io.example.clientList.controller;
22

3-
import com.io.example.clientList.service.ReportService;
3+
import com.io.example.clientList.service.ClientsReportService;
44
import lombok.RequiredArgsConstructor;
55
import org.springframework.context.annotation.Profile;
66
import org.springframework.http.HttpHeaders;
@@ -12,13 +12,13 @@
1212
@RestController
1313
@Profile({"client-list-dev", "client-list-local"})
1414
@RequiredArgsConstructor
15-
public class ReportController {
15+
public class ClientsController {
1616

17-
private final ReportService reportService;
17+
private final ClientsReportService clientsReportService;
1818

1919
@GetMapping("/reports/clients")
2020
public ResponseEntity<byte[]> getClientsReport() {
21-
byte[] pdf = this.reportService.generateClientReport();
21+
byte[] pdf = this.clientsReportService.generateClientsPdf();
2222
return ResponseEntity.ok()
2323
.header(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=clients.pdf")
2424
.contentType(MediaType.APPLICATION_PDF)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.io.example.clientList.service;
2+
3+
import com.io.example.clientList.dto.ClientDto;
4+
import org.springframework.context.annotation.Profile;
5+
import org.springframework.stereotype.Service;
6+
import java.util.*;
7+
8+
import static com.io.example.base.JasperReader.exportReportJasperToPdf;
9+
10+
@Service
11+
@Profile({"client-list-dev"})
12+
public class ClientsReportDevServiceImpl implements ClientsReportService {
13+
14+
private static final List<ClientDto> clients = Arrays.asList(
15+
new ClientDto(1, "Alice", "Brazil"),
16+
new ClientDto(2, "Bob", "USA"),
17+
new ClientDto(3, "Carlos", "Spain")
18+
);
19+
20+
@Override
21+
public byte[] generateClientsPdf() {
22+
return exportReportJasperToPdf(clients, "reports/client-list-template.jasper");
23+
}
24+
25+
26+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.io.example.clientList.service;
2+
3+
import com.io.example.clientList.dto.ClientDto;
4+
import org.springframework.context.annotation.Profile;
5+
import org.springframework.stereotype.Service;
6+
import java.util.*;
7+
8+
import static com.io.example.base.JasperReader.exportReportJrxmlToPdf;
9+
10+
@Service
11+
@Profile({"client-list-local"})
12+
public class ClientsReportLocalServiceImpl implements ClientsReportService {
13+
14+
private static final List<ClientDto> clients = Arrays.asList(
15+
new ClientDto(1, "Alice", "Brazil"),
16+
new ClientDto(2, "Bob", "USA"),
17+
new ClientDto(3, "Carlos", "Spain")
18+
);
19+
20+
@Override
21+
public byte[] generateClientsPdf() {
22+
return exportReportJrxmlToPdf(clients, "reports/client-list-template.jrxml");
23+
}
24+
25+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.io.example.clientList.service;
2+
3+
public interface ClientsReportService {
4+
byte[] generateClientsPdf();
5+
}

spring-jasper-example/src/main/java/com/io/example/clientList/service/ReportDevService.java

Lines changed: 0 additions & 49 deletions
This file was deleted.

spring-jasper-example/src/main/java/com/io/example/clientList/service/ReportLocalService.java

Lines changed: 0 additions & 39 deletions
This file was deleted.

spring-jasper-example/src/main/java/com/io/example/clientList/service/ReportService.java

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.io.example.dataUsageBill.controller;
2+
3+
import com.io.example.clientList.service.ClientsReportService;
4+
import com.io.example.dataUsageBill.service.BillReportService;
5+
import lombok.RequiredArgsConstructor;
6+
import org.springframework.context.annotation.Profile;
7+
import org.springframework.http.HttpHeaders;
8+
import org.springframework.http.MediaType;
9+
import org.springframework.http.ResponseEntity;
10+
import org.springframework.web.bind.annotation.GetMapping;
11+
import org.springframework.web.bind.annotation.RestController;
12+
13+
@RestController
14+
@Profile({"bill-dev", "bill-local"})
15+
@RequiredArgsConstructor
16+
public class BillController {
17+
18+
private final BillReportService billReportService;
19+
20+
@GetMapping("/reports/bill")
21+
public ResponseEntity<byte[]> getBillData() {
22+
byte[] pdf = this.billReportService.generateBillPdf();
23+
return ResponseEntity.ok()
24+
.header(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=bill.pdf")
25+
.contentType(MediaType.APPLICATION_PDF)
26+
.body(pdf);
27+
}
28+
29+
}

0 commit comments

Comments
 (0)