From aa041b33583d76c9854de16e0f451257e40699fe Mon Sep 17 00:00:00 2001 From: ricardo051199 Date: Wed, 28 Aug 2024 15:06:38 -0400 Subject: [PATCH] #7 Perfil de Personal Administrativo - Listado de empleados (Ordenar ascendente y descendente) --- .../service/EmployeeService.java | 11 +- .../views/EmployeesListView.java | 156 ++++++++++-------- 2 files changed, 95 insertions(+), 72 deletions(-) diff --git a/src/main/java/com/primefactorsolutions/service/EmployeeService.java b/src/main/java/com/primefactorsolutions/service/EmployeeService.java index 97d2a07..6626fbc 100644 --- a/src/main/java/com/primefactorsolutions/service/EmployeeService.java +++ b/src/main/java/com/primefactorsolutions/service/EmployeeService.java @@ -6,6 +6,7 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import lombok.Data; +import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; import java.util.List; @@ -33,13 +34,13 @@ public class EmployeeService { return null; } - public Page getEmployeesPaginated(int pageNo, int pageSize) { - Pageable pageable = PageRequest.of(pageNo - 1, pageSize); + public Page getEmployeesPaginated(int pageNo, int pageSize, String sortField, boolean ascending) { + Sort sort = ascending ? Sort.by(sortField).ascending() : Sort.by(sortField).descending(); + Pageable pageable = PageRequest.of(pageNo - 1, pageSize, sort); return employeeRepository.findAll(pageable); } - public boolean hasNextPage(int currentPage, int pageSize) { - Page page = getEmployeesPaginated(currentPage, pageSize); - return page.hasNext(); + public boolean hasNextPage(int currentPage, int pageSize, String sortField, boolean ascending) { + return getEmployeesPaginated(currentPage, pageSize, sortField, ascending).hasNext(); } } diff --git a/src/main/java/com/primefactorsolutions/views/EmployeesListView.java b/src/main/java/com/primefactorsolutions/views/EmployeesListView.java index 9a403cb..bada406 100644 --- a/src/main/java/com/primefactorsolutions/views/EmployeesListView.java +++ b/src/main/java/com/primefactorsolutions/views/EmployeesListView.java @@ -2,15 +2,11 @@ package com.primefactorsolutions.views; import com.primefactorsolutions.model.Employee; import com.primefactorsolutions.service.EmployeeService; -import com.vaadin.flow.component.ClickEvent; -import com.vaadin.flow.component.Component; -import com.vaadin.flow.component.ComponentEventListener; import com.vaadin.flow.component.button.Button; import com.vaadin.flow.component.combobox.ComboBox; import com.vaadin.flow.component.html.H2; import com.vaadin.flow.component.html.Main; import com.vaadin.flow.component.orderedlayout.HorizontalLayout; -import com.vaadin.flow.function.ValueProvider; import com.vaadin.flow.router.PageTitle; import com.vaadin.flow.router.Route; import com.vaadin.flow.spring.annotation.SpringComponent; @@ -25,77 +21,103 @@ import org.springframework.data.domain.Page; @Route(value = "/employees", layout = MainLayout.class) @PermitAll public class EmployeesListView extends Main { - private final EmployeeService employeeService; - private int currentPage = 1; - private final int pageSize = 5; - private final Button previous; - private final Button next; - public EmployeesListView(final EmployeeService employeeService) { + private final EmployeeService employeeService; + private final int pageSize = 5; + private int currentPage = 1; + private String sortField = "firstName"; + private boolean ascending = true; + private VGrid grid; + private Button previous; + private Button next; + + public EmployeesListView(EmployeeService employeeService) { this.employeeService = employeeService; + addComponents(); + updateGrid(); + } + + private void addComponents() { + addTitle(); + addOrderButtons(); + configureGrid(); + addNavigationButtons(); + } + + private void addTitle() { final H2 title = new H2("Employees list"); + add(title); + } + + private void addOrderButtons() { final HorizontalLayout hl = new HorizontalLayout(); - final HorizontalLayout hf = new HorizontalLayout(); - final Button employeeListAscendingOrder = new Button("Employee List in Ascending Order"); - employeeListAscendingOrder.addClickListener((ComponentEventListener>) buttonClickEvent -> { - // no-op + hl.add(createOrderButton("Employee List in Ascending Order", true)); + hl.add(createOrderButton("Employee List in Descending Order", false)); + add(hl); + } + + private Button createOrderButton(String label, boolean isAscending) { + Button button = new Button(label); + button.addClickListener(event -> { + sortField = "firstName"; + ascending = isAscending; + updateGrid(); }); - hl.add(employeeListAscendingOrder); - final Button employeeListDescendingOrder = new Button("Employee List in Descending Order"); - employeeListDescendingOrder.addClickListener((ComponentEventListener>) buttonClickEvent -> { - // no-op - }); - hl.add(employeeListDescendingOrder); - final VGrid grid = new VGrid<>(Employee.class); + return button; + } + private void configureGrid() { + grid = new VGrid<>(Employee.class); grid.setColumns("firstName", "lastName", "status"); grid.setAllRowsVisible(true); - grid.addComponentColumn((ValueProvider) employee -> { - ComboBox statusComboBox = new ComboBox<>(); - statusComboBox.setItems("Active", "Inactive"); - return statusComboBox; - }).setHeader("Change Status"); - grid.addComponentColumn((ValueProvider) employee -> { - final Button edit = new Button("Edit"); - edit.addClickListener((ComponentEventListener>) buttonClickEvent -> { - // no-op - }); - return edit; - }); - grid.addComponentColumn((ValueProvider) employee -> { - final Button save = new Button("Save"); - save.addClickListener((ComponentEventListener>) buttonClickEvent -> { - // no-op - }); - return save; - }); - this.previous = new Button("Previous"); - this.previous.addClickListener((ComponentEventListener>) buttonClickEvent -> { - if (currentPage > 1) { - currentPage--; - updateGrid(grid); - } - }); - hf.add(this.previous); - this.next = new Button("Next"); - this.next.addClickListener((ComponentEventListener>) buttonClickEvent -> { - if (employeeService.hasNextPage(currentPage, pageSize)) { - currentPage++; - updateGrid(grid); - } - }); - hf.add(this.next); - final Button addEmployee = new Button("Add Employee"); - addEmployee.addClickListener((ComponentEventListener>) buttonClickEvent -> { - this.getUI().flatMap(ui -> ui.navigate(EmployeeView.class, "new")); - }); - hf.add(addEmployee); - add(title, hl, grid, hf); - updateGrid(grid); + grid.addComponentColumn(employee -> createStatusComboBox()).setHeader("Change Status"); + grid.addComponentColumn(employee -> createEditButton()); + grid.addComponentColumn(employee -> createSaveButton()); + add(grid); } - private void updateGrid(VGrid grid) { - Page page = employeeService.getEmployeesPaginated(currentPage, pageSize); + + private ComboBox createStatusComboBox() { + ComboBox statusComboBox = new ComboBox<>(); + statusComboBox.setItems("Active", "Inactive"); + return statusComboBox; + } + + private Button createEditButton() { + return new Button("Edit"); + } + private Button createSaveButton() { + return new Button("Save"); + } + + private void addNavigationButtons() { + final HorizontalLayout hf = new HorizontalLayout(); + previous = createNavigationButton("Previous", -1); + next = createNavigationButton("Next", 1); + hf.add(previous, next, createAddEmployeeButton()); + add(hf); + } + + private Button createNavigationButton(String label, int increment) { + Button button = new Button(label); + button.addClickListener(event -> changePage(increment)); + return button; + } + private void changePage(int increment) { + if (increment < 0 && currentPage > 1) { + currentPage--; + } else if (increment > 0 && employeeService.hasNextPage(currentPage, pageSize, sortField, ascending)) { + currentPage++; + } + updateGrid(); + } + private Button createAddEmployeeButton() { + return new Button("Add Employee", event -> + getUI().flatMap(ui -> ui.navigate(EmployeeView.class, "new")) + ); + } + private void updateGrid() { + Page page = employeeService.getEmployeesPaginated(currentPage, pageSize, sortField, ascending); grid.setItems(page.getContent()); - this.previous.setEnabled(currentPage > 1); - this.next.setEnabled(page.hasNext()); + previous.setEnabled(currentPage > 1); + next.setEnabled(page.hasNext()); } }