From 8f4a571b0f646c59e472085159b5b3a9530e4d61 Mon Sep 17 00:00:00 2001 From: effe Date: Sat, 14 Sep 2024 13:52:37 -0400 Subject: [PATCH] refactor: chart generation --- .../munera/views/dashboard/DashboardView.java | 39 ++++++++++--------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/application/munera/views/dashboard/DashboardView.java b/src/main/java/com/application/munera/views/dashboard/DashboardView.java index a20074a..b25b404 100644 --- a/src/main/java/com/application/munera/views/dashboard/DashboardView.java +++ b/src/main/java/com/application/munera/views/dashboard/DashboardView.java @@ -3,6 +3,7 @@ package com.application.munera.views.dashboard; import com.application.munera.data.Expense; import com.application.munera.data.Person; import com.application.munera.data.User; +import com.application.munera.facades.PersonFacade; import com.application.munera.services.ExpenseService; import com.application.munera.services.PersonService; import com.application.munera.services.UserService; @@ -29,17 +30,15 @@ import java.util.stream.Collectors; public class DashboardView extends Div { private final ExpenseService expenseService; - private final PersonService personService; - private final UserService userService; + private final PersonFacade personFacade; private final User loggedUser; private final Person loggedPerson; - public DashboardView(final ExpenseService expenseService, final PersonService personService, UserService userService) { + public DashboardView(ExpenseService expenseService, UserService userService, PersonFacade personFacade) { this.expenseService = expenseService; - this.personService = personService; - this.userService = userService; + this.personFacade = personFacade; loggedUser = userService.getLoggedInUser(); - loggedPerson = personService.getLoggedInPerson(); + loggedPerson = personFacade.getLoggedInPerson(); addClassName("highcharts-view"); // Optional CSS class for styling VerticalLayout mainLayout = new VerticalLayout(); @@ -197,18 +196,22 @@ public class DashboardView extends Div { } private String generateNegativeColumnChartScript() { - final var people = personService.findAllExcludeLoggedUser(loggedUser).stream() - .filter(person -> personService.calculateNetBalance(person).compareTo(BigDecimal.ZERO) != 0) - .toList(); - if (people.isEmpty()) return generatePlaceholderChartScript("bottomLeftChart", "All Payments Settled"); - - Map personData = new LinkedHashMap<>(); - - for (Person person : people) { - BigDecimal balance = personService.calculateNetBalance(person); - // Invert the balance value - personData.put(person.getFirstName(), balance.negate().doubleValue()); - } + final var people = personFacade.findAllExcludeLoggedUser(loggedUser); + // Create a map to store person names and their balances + Map personData = people.stream() + .map(person -> { + BigDecimal balance = personFacade.calculateNetBalance(person); + // Return an array with the person’s first name and the balance + return new Object[]{person.getFirstName(), balance}; + }) + .filter(entry -> ((BigDecimal) entry[1]).compareTo(BigDecimal.ZERO) != 0) + .collect(Collectors.toMap( + entry -> (String) entry[0], + entry -> ((BigDecimal) entry[1]).negate().doubleValue(), + (existing, replacement) -> existing, + LinkedHashMap::new // Preserves insertion order + )); + if (personData.isEmpty()) return generatePlaceholderChartScript("bottomLeftChart", "All Payments Settled"); // Prepare series data for Highcharts with conditional coloring StringBuilder data = new StringBuilder("[");