From d1c0b8a1d3afad27195ad314355dcffd0f307669 Mon Sep 17 00:00:00 2001 From: filippo-ferrari Date: Thu, 18 Jul 2024 18:33:59 +0200 Subject: [PATCH] refactor --- .../munera/views/people/PeopleView.java | 48 ++++--------------- 1 file changed, 10 insertions(+), 38 deletions(-) diff --git a/src/main/java/com/application/munera/views/people/PeopleView.java b/src/main/java/com/application/munera/views/people/PeopleView.java index c23286e..070ad5f 100644 --- a/src/main/java/com/application/munera/views/people/PeopleView.java +++ b/src/main/java/com/application/munera/views/people/PeopleView.java @@ -77,14 +77,8 @@ public class PeopleView extends Div implements BeforeEnterObserver { grid.addHierarchyColumn(this::getNodeName).setHeader("Name"); grid.addColumn(this::getNodeCost).setHeader("Total Expenses Value").setSortable(true); grid.addColumn(new ComponentRenderer<>(persona -> { - if (persona instanceof Person) { - final var netBalance = personService.calculateNetBalance((Person) persona); - return createPersonBadge(netBalance); - } - else { - final var isResolved = ((Expense) persona).getIsResolved(); - return createExpenseBadge(isResolved); - } + if (persona instanceof Person) return createPersonBadge(personService.calculateNetBalance((Person) persona)); + else return createExpenseBadge(((Expense) persona).getIsResolved()); })).setHeader("Balance Status"); List people = (List) personService.findAll(); @@ -97,19 +91,13 @@ public class PeopleView extends Div implements BeforeEnterObserver { List expenses = expenseService.findExpenseByUser(person); // Add each expense as a child item under the person - for (Expense expense : expenses) { - grid.getTreeData().addItem(person, expense); - } + for (Expense expense : expenses) grid.getTreeData().addItem(person, expense); } - // when a row is selected or deselected, populate form grid.asSingleSelect().addValueChangeListener(event -> { Object selectedItem = event.getValue(); - if (selectedItem instanceof Person) { - Person selectedPerson = (Person) selectedItem; - UI.getCurrent().navigate(String.format("people/%d/edit", selectedPerson.getId())); - } + if (selectedItem instanceof Person selectedPerson) UI.getCurrent().navigate(String.format(PERSON_EDIT_ROUTE_TEMPLATE, selectedPerson.getId())); }); // Configure Form @@ -163,29 +151,14 @@ public class PeopleView extends Div implements BeforeEnterObserver { } private String getNodeName(Object node) { - if (node instanceof Person) { - return ((Person) node).getFirstName() + " " + ((Person) node).getLastName(); - } else if (node instanceof Expense) { - return ((Expense) node).getName(); - } + if (node instanceof Person) return ((Person) node).getFirstName() + " " + ((Person) node).getLastName(); + else if (node instanceof Expense) return ((Expense) node).getName(); return ""; } private String getNodeCost(Object node) { - if (node instanceof Person) { - return this.personService.calculateNetBalance((Person) node).toString() + " €"; - } else if (node instanceof Expense) { - return ((Expense) node).getCost().toString() + " €"; - } - return ""; - } - - private String getNodeType(Object node) { - if (node instanceof Person) { - return "Person"; - } else if (node instanceof Expense) { - return "Expense"; - } + if (node instanceof Person) return this.personService.calculateNetBalance((Person) node).toString() + " €"; + else if (node instanceof Expense) return ((Expense) node).getCost().toString() + " €"; return ""; } @@ -194,9 +167,8 @@ public class PeopleView extends Div implements BeforeEnterObserver { Optional personId = event.getRouteParameters().get(PERSON_ID).map(Long::parseLong); if (personId.isPresent()) { Optional personFromBackend = personService.get(personId.get()); - if (personFromBackend.isPresent()) { - populateForm(personFromBackend.get()); - } else { + if (personFromBackend.isPresent()) populateForm(personFromBackend.get()); + else { Notification.show( String.format("The requested person was not found, ID = %s", personId.get()), 3000, Position.BOTTOM_START);