From bc4fb6297859626ba86d25ec908fcecfa565c60f Mon Sep 17 00:00:00 2001 From: Filippo Ferrari Date: Tue, 4 Jun 2024 09:31:40 +0100 Subject: [PATCH] feat: added creditors management --- .../com/application/munera/data/Expense.java | 4 ++-- .../munera/views/expenses/ExpensesView.java | 18 +++++++++++++----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/application/munera/data/Expense.java b/src/main/java/com/application/munera/data/Expense.java index 5b844de..0b9bcfb 100644 --- a/src/main/java/com/application/munera/data/Expense.java +++ b/src/main/java/com/application/munera/data/Expense.java @@ -43,14 +43,14 @@ public class Expense { @Column(name = "PeriodInterval") private Integer periodInterval; - @ManyToMany + @ManyToMany(fetch = FetchType.EAGER) @JoinTable( name = "Creditor_expenses", joinColumns = @JoinColumn(name = "expense_id"), inverseJoinColumns = @JoinColumn(name = "people_id")) private Set creditors; - @ManyToMany + @ManyToMany(fetch = FetchType.EAGER) @JoinTable( name = "Debtors_expenses", joinColumns = @JoinColumn(name = "expense_id"), diff --git a/src/main/java/com/application/munera/views/expenses/ExpensesView.java b/src/main/java/com/application/munera/views/expenses/ExpensesView.java index 706ddab..f6adc71 100644 --- a/src/main/java/com/application/munera/views/expenses/ExpensesView.java +++ b/src/main/java/com/application/munera/views/expenses/ExpensesView.java @@ -2,14 +2,17 @@ package com.application.munera.views.expenses; import com.application.munera.data.Category; import com.application.munera.data.Expense; import com.application.munera.data.PeriodUnit; +import com.application.munera.data.Person; import com.application.munera.services.CategoryService; import com.application.munera.services.ExpenseService; +import com.application.munera.services.PersonService; import com.application.munera.views.MainLayout; import com.vaadin.flow.component.UI; import com.vaadin.flow.component.button.Button; import com.vaadin.flow.component.button.ButtonVariant; import com.vaadin.flow.component.checkbox.Checkbox; import com.vaadin.flow.component.combobox.ComboBox; +import com.vaadin.flow.component.combobox.MultiSelectComboBox; import com.vaadin.flow.component.datepicker.DatePicker; import com.vaadin.flow.component.dependency.Uses; import com.vaadin.flow.component.formlayout.FormLayout; @@ -55,6 +58,7 @@ public class ExpensesView extends Div implements BeforeEnterObserver { private final ExpenseService expenseService; private final CategoryService categoryService; + private final PersonService personService; private TextField name; private TextField cost; private ComboBox category; @@ -63,10 +67,12 @@ public class ExpensesView extends Div implements BeforeEnterObserver { private ComboBox periodUnit; private TextField periodInterval; private DatePicker date; + private MultiSelectComboBox creditors; - public ExpensesView(ExpenseService expenseService, CategoryService categoryService) { + public ExpensesView(ExpenseService expenseService, CategoryService categoryService, PersonService personService) { this.expenseService = expenseService; this.categoryService = categoryService; + this.personService = personService; addClassNames("expenses-view"); // Create UI @@ -175,9 +181,8 @@ public class ExpensesView extends Div implements BeforeEnterObserver { Optional expenseId = event.getRouteParameters().get(EXPENSE_ID).map(Long::parseLong); if (expenseId.isPresent()) { Optional expenseFromBackend = expenseService.get(expenseId.get()); - if (expenseFromBackend.isPresent()) { - populateForm(expenseFromBackend.get()); - } else { + if (expenseFromBackend.isPresent()) populateForm(expenseFromBackend.get()); + else { Notification.show( String.format("The requested expense was not found, ID = %s", expenseId.get()), 3000, Notification.Position.BOTTOM_START); @@ -208,6 +213,9 @@ public class ExpensesView extends Div implements BeforeEnterObserver { periodUnit = new ComboBox<>("Period Unit"); periodUnit.setItems(PeriodUnit.values()); periodInterval = new TextField("Period Interval"); + creditors = new MultiSelectComboBox<>("Creditors"); + creditors.setItems(personService.findAll()); + creditors.setItemLabelGenerator(Person::getFirstName); date = new DatePicker("Date"); LitRenderer isPeriodicRenderer = LitRenderer.of( "") @@ -216,7 +224,7 @@ public class ExpensesView extends Div implements BeforeEnterObserver { ? "var(--lumo-primary-text-color)" : "var(--lumo-disabled-text-color)"); - formLayout.add(name, cost, category, description, isPeriodic, periodUnit, periodInterval, date); + formLayout.add(name, cost, category, description, isPeriodic, periodUnit, periodInterval, date, creditors); grid.addColumn(isPeriodicRenderer).setHeader("Periodic").setAutoWidth(true); editorDiv.add(formLayout); createButtonLayout(editorLayoutDiv);