This commit is contained in:
filippo-ferrari 2024-07-18 18:33:59 +02:00
parent eab92d3640
commit d1c0b8a1d3

View file

@ -77,14 +77,8 @@ public class PeopleView extends Div implements BeforeEnterObserver {
grid.addHierarchyColumn(this::getNodeName).setHeader("Name"); grid.addHierarchyColumn(this::getNodeName).setHeader("Name");
grid.addColumn(this::getNodeCost).setHeader("Total Expenses Value").setSortable(true); grid.addColumn(this::getNodeCost).setHeader("Total Expenses Value").setSortable(true);
grid.addColumn(new ComponentRenderer<>(persona -> { grid.addColumn(new ComponentRenderer<>(persona -> {
if (persona instanceof Person) { if (persona instanceof Person) return createPersonBadge(personService.calculateNetBalance((Person) persona));
final var netBalance = personService.calculateNetBalance((Person) persona); else return createExpenseBadge(((Expense) persona).getIsResolved());
return createPersonBadge(netBalance);
}
else {
final var isResolved = ((Expense) persona).getIsResolved();
return createExpenseBadge(isResolved);
}
})).setHeader("Balance Status"); })).setHeader("Balance Status");
List<Person> people = (List<Person>) personService.findAll(); List<Person> people = (List<Person>) personService.findAll();
@ -97,19 +91,13 @@ public class PeopleView extends Div implements BeforeEnterObserver {
List<Expense> expenses = expenseService.findExpenseByUser(person); List<Expense> expenses = expenseService.findExpenseByUser(person);
// Add each expense as a child item under the person // Add each expense as a child item under the person
for (Expense expense : expenses) { for (Expense expense : expenses) grid.getTreeData().addItem(person, expense);
grid.getTreeData().addItem(person, expense);
}
} }
// when a row is selected or deselected, populate form // when a row is selected or deselected, populate form
grid.asSingleSelect().addValueChangeListener(event -> { grid.asSingleSelect().addValueChangeListener(event -> {
Object selectedItem = event.getValue(); Object selectedItem = event.getValue();
if (selectedItem instanceof Person) { if (selectedItem instanceof Person selectedPerson) UI.getCurrent().navigate(String.format(PERSON_EDIT_ROUTE_TEMPLATE, selectedPerson.getId()));
Person selectedPerson = (Person) selectedItem;
UI.getCurrent().navigate(String.format("people/%d/edit", selectedPerson.getId()));
}
}); });
// Configure Form // Configure Form
@ -163,29 +151,14 @@ public class PeopleView extends Div implements BeforeEnterObserver {
} }
private String getNodeName(Object node) { private String getNodeName(Object node) {
if (node instanceof Person) { if (node instanceof Person) return ((Person) node).getFirstName() + " " + ((Person) node).getLastName();
return ((Person) node).getFirstName() + " " + ((Person) node).getLastName(); else if (node instanceof Expense) return ((Expense) node).getName();
} else if (node instanceof Expense) {
return ((Expense) node).getName();
}
return ""; return "";
} }
private String getNodeCost(Object node) { private String getNodeCost(Object node) {
if (node instanceof Person) { if (node instanceof Person) return this.personService.calculateNetBalance((Person) node).toString() + "";
return this.personService.calculateNetBalance((Person) node).toString() + ""; else if (node instanceof Expense) return ((Expense) node).getCost().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";
}
return ""; return "";
} }
@ -194,9 +167,8 @@ public class PeopleView extends Div implements BeforeEnterObserver {
Optional<Long> personId = event.getRouteParameters().get(PERSON_ID).map(Long::parseLong); Optional<Long> personId = event.getRouteParameters().get(PERSON_ID).map(Long::parseLong);
if (personId.isPresent()) { if (personId.isPresent()) {
Optional<Person> personFromBackend = personService.get(personId.get()); Optional<Person> personFromBackend = personService.get(personId.get());
if (personFromBackend.isPresent()) { if (personFromBackend.isPresent()) populateForm(personFromBackend.get());
populateForm(personFromBackend.get()); else {
} else {
Notification.show( Notification.show(
String.format("The requested person was not found, ID = %s", personId.get()), 3000, String.format("The requested person was not found, ID = %s", personId.get()), 3000,
Position.BOTTOM_START); Position.BOTTOM_START);