feat: export to CSV

This commit is contained in:
filippo-ferrari 2024-09-12 11:19:38 +02:00
parent 2e2e7c3555
commit ac67892cae
2 changed files with 10 additions and 11 deletions

View file

@ -107,11 +107,6 @@
<artifactId>vaadin-testbench-junit5</artifactId> <artifactId>vaadin-testbench-junit5</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.10.0</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

View file

@ -121,12 +121,19 @@ public class ExpensesView extends Div implements BeforeEnterObserver {
grid.addThemeVariants(GridVariant.LUMO_NO_BORDER); grid.addThemeVariants(GridVariant.LUMO_NO_BORDER);
// Export to CSV button // Export to CSV button
exportToCSVButton.addClickListener(event -> { exportToCSVButton.addClickListener(CSVClickEvent -> {
StreamResource resource = createCSVResource(expenseService.findAll()); StreamResource resource = createCSVResource(expenseService.findAll());
Anchor downloadLink = new Anchor(resource, "Download CSV"); Anchor downloadLink = new Anchor(resource, "Download CSV");
downloadLink.getElement().setAttribute("download", true); downloadLink.getElement().setAttribute("download", true);
// Remove old download links if any
this.getChildren().filter(Anchor.class::isInstance)
.forEach(child -> remove((Anchor) child));
// Add the new download link
add(downloadLink); add(downloadLink);
}); });
add(exportToCSVButton); add(exportToCSVButton);
// when a row is selected or deselected, populate form // when a row is selected or deselected, populate form
@ -354,23 +361,20 @@ public class ExpensesView extends Div implements BeforeEnterObserver {
return new StreamResource("expenses.csv", () -> { return new StreamResource("expenses.csv", () -> {
ByteArrayOutputStream stream = new ByteArrayOutputStream(); ByteArrayOutputStream stream = new ByteArrayOutputStream();
try (OutputStreamWriter writer = new OutputStreamWriter(stream, StandardCharsets.UTF_8); try (OutputStreamWriter writer = new OutputStreamWriter(stream, StandardCharsets.UTF_8);
CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT.withHeader("Name", "Cost", "Category", "Period Interval", "Period Unit", "Date", "Status"))) { CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT.withHeader("Name", "Cost", "Category", "Date", "Payment date"))) {
for (Expense expense : expenses) { for (Expense expense : expenses) {
csvPrinter.printRecord( csvPrinter.printRecord(
expense.getName(), expense.getName(),
expense.getCost(), expense.getCost(),
expense.getCategory() != null ? expense.getCategory().getName() : "", expense.getCategory() != null ? expense.getCategory().getName() : "",
expense.getPeriodInterval(),
expense.getPeriodUnit(),
expense.getDate(), expense.getDate(),
expense.getIsPaid() ? "Paid" : "Unpaid" expense.getPaymentDate() != null ? expense.getPaymentDate().toLocalDate() : "Unpaid"
); );
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return new ByteArrayInputStream(stream.toByteArray()); return new ByteArrayInputStream(stream.toByteArray());
}); });
} }