From 93e42baa52b630dceaa3a0ab5952aebe6a04725c Mon Sep 17 00:00:00 2001 From: ricardo051199 Date: Wed, 28 Aug 2024 13:29:11 -0400 Subject: [PATCH] #7 Perfil de Personal Administrativo - Listado de empleados (Navegacion de paginas) --- .../service/EmployeeService.java | 15 +++- .../views/EmployeesListView.java | 77 +++++++------------ src/main/resources/data.sql | 13 +++- 3 files changed, 51 insertions(+), 54 deletions(-) diff --git a/src/main/java/com/primefactorsolutions/service/EmployeeService.java b/src/main/java/com/primefactorsolutions/service/EmployeeService.java index 716827d..97d2a07 100644 --- a/src/main/java/com/primefactorsolutions/service/EmployeeService.java +++ b/src/main/java/com/primefactorsolutions/service/EmployeeService.java @@ -1,10 +1,11 @@ package com.primefactorsolutions.service; -import com.primefactorsolutions.model.Candidate; import com.primefactorsolutions.model.Employee; import com.primefactorsolutions.repositories.EmployeeRepository; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; import lombok.Data; -import org.apache.commons.lang3.NotImplementedException; import org.springframework.stereotype.Service; import java.util.List; @@ -31,4 +32,14 @@ public class EmployeeService { public Employee getEmployee(final UUID id) { return null; } + + public Page getEmployeesPaginated(int pageNo, int pageSize) { + Pageable pageable = PageRequest.of(pageNo - 1, pageSize); + return employeeRepository.findAll(pageable); + } + + public boolean hasNextPage(int currentPage, int pageSize) { + Page page = getEmployeesPaginated(currentPage, pageSize); + return page.hasNext(); + } } diff --git a/src/main/java/com/primefactorsolutions/views/EmployeesListView.java b/src/main/java/com/primefactorsolutions/views/EmployeesListView.java index 59fbb3f..9a403cb 100644 --- a/src/main/java/com/primefactorsolutions/views/EmployeesListView.java +++ b/src/main/java/com/primefactorsolutions/views/EmployeesListView.java @@ -10,19 +10,14 @@ 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.data.provider.DataProvider; -import com.vaadin.flow.data.provider.DataProviderListener; -import com.vaadin.flow.data.provider.Query; import com.vaadin.flow.function.ValueProvider; import com.vaadin.flow.router.PageTitle; import com.vaadin.flow.router.Route; -import com.vaadin.flow.shared.Registration; import com.vaadin.flow.spring.annotation.SpringComponent; import jakarta.annotation.security.PermitAll; import org.springframework.context.annotation.Scope; import org.vaadin.firitin.components.grid.VGrid; - -import java.util.stream.Stream; +import org.springframework.data.domain.Page; @SpringComponent @Scope("prototype") @@ -31,6 +26,10 @@ import java.util.stream.Stream; @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) { this.employeeService = employeeService; @@ -69,58 +68,34 @@ public class EmployeesListView extends Main { }); return save; }); - - grid.setDataProvider(new DataProvider<>() { - @Override - public boolean isInMemory() { - return false; - } - - @Override - public int size(final Query query) { - return employeeService.getEmployees().size(); - } - - @Override - public Stream fetch(final Query query) { - int limit = query.getLimit(); - int pagerSize = query.getPageSize(); - int page = query.getPage(); - return employeeService.getEmployees().stream(); - } - - @Override - public void refreshItem(final Employee employee) { - // no-op - } - - @Override - public void refreshAll() { - // no-op - } - - @Override - public Registration addDataProviderListener(final DataProviderListener dataProviderListener) { - return null; + this.previous = new Button("Previous"); + this.previous.addClickListener((ComponentEventListener>) buttonClickEvent -> { + if (currentPage > 1) { + currentPage--; + updateGrid(grid); } }); - - final Button previous = new Button("Previous"); - previous.addClickListener((ComponentEventListener>) buttonClickEvent -> { - // no-op + hf.add(this.previous); + this.next = new Button("Next"); + this.next.addClickListener((ComponentEventListener>) buttonClickEvent -> { + if (employeeService.hasNextPage(currentPage, pageSize)) { + currentPage++; + updateGrid(grid); + } }); - hf.add(previous); - final Button next = new Button("Next"); - next.addClickListener((ComponentEventListener>) buttonClickEvent -> { - // no-op - }); - hf.add(next); + hf.add(this.next); final Button addEmployee = new Button("Add Employee"); addEmployee.addClickListener((ComponentEventListener>) buttonClickEvent -> { - this.getUI().get().navigate(EmployeeView.class, "new"); + this.getUI().flatMap(ui -> ui.navigate(EmployeeView.class, "new")); }); hf.add(addEmployee); - add(title, hl, grid, hf); + updateGrid(grid); + } + private void updateGrid(VGrid grid) { + Page page = employeeService.getEmployeesPaginated(currentPage, pageSize); + grid.setItems(page.getContent()); + this.previous.setEnabled(currentPage > 1); + this.next.setEnabled(page.hasNext()); } } diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql index f797344..0888a8a 100644 --- a/src/main/resources/data.sql +++ b/src/main/resources/data.sql @@ -13,4 +13,15 @@ insert into employee (id, version, first_name, last_name, status) values ('e99b7 insert into employee (id, version, first_name, last_name, status) values ('f6ab3c6d-7078-45f6-9b22-4e37637bfec6', 1, 'Ana', 'Garcia Rojas', 'ACTIVE'); insert into employee (id, version, first_name, last_name, status) values ('2e2293b1-3f9a-4f3d-abc8-32639b0a5e15', 1, 'Carlos', 'Lopez Mendoza', 'INACTIVE'); insert into employee (id, version, first_name, last_name, status) values ('4b1c6c35-4627-4b35-b6e9-dc75c68b2c31', 1, 'Maria', 'Fernandez Villca', 'ACTIVE'); -insert into employee (id, version, first_name, last_name, status) values ('afc5c741-f70a-4394-853b-39d51b118927', 1, 'Luis', 'Gutierrez Mamani', 'ACTIVE'); \ No newline at end of file +insert into employee (id, version, first_name, last_name, status) values ('afc5c741-f70a-4394-853b-39d51b118927', 1, 'Luis', 'Gutierrez Mamani', 'ACTIVE'); +insert into employee (id, version, first_name, last_name, status) values ('b2436b82-7b9f-4f0d-9463-f2c3173a45c3', 1, 'Laura', 'Martinez Paredes', 'INACTIVE'); +insert into employee (id, version, first_name, last_name, status) values ('6e6a8a4e-9f6b-44eb-8c69-40acfdc86756', 1, 'Roberto', 'Santos Escobar', 'ACTIVE'); +insert into employee (id, version, first_name, last_name, status) values ('36b0d1c6-bdc0-4d98-94bb-08b9bce3f0d5', 1, 'Valeria', 'Morales Ochoa', 'INACTIVE'); +insert into employee (id, version, first_name, last_name, status) values ('5a1c6d80-58b3-43e3-a5a5-24b4a2d1d54a', 1, 'Jorge', 'Ramirez Tapia', 'ACTIVE'); +insert into employee (id, version, first_name, last_name, status) values ('9d6a5b2e-6d0b-4b89-8d6a-d3f3d1bfc047', 1, 'Sandra', 'Torres Huanca', 'ACTIVE'); +insert into employee (id, version, first_name, last_name, status) values ('f8b3e0c0-0d5a-4e5c-bf9d-207b9b5e8279', 1, 'Felipe', 'Quispe Huanca', 'INACTIVE'); +insert into employee (id, version, first_name, last_name, status) values ('cd80e1d0-9a08-44a6-bd63-2c63eaa003d4', 1, 'Gabriela', 'Rivas Arana', 'ACTIVE'); +insert into employee (id, version, first_name, last_name, status) values ('62d3c1b7-815e-4e96-8d7e-f8c4236bca55', 1, 'Oscar', 'Flores Quiroga', 'INACTIVE'); +insert into employee (id, version, first_name, last_name, status) values ('f20b7c5a-5a67-44f0-9ec1-4c1b8e80de05', 1, 'Marta', 'Vargas Soria', 'ACTIVE'); +insert into employee (id, version, first_name, last_name, status) values ('19b5a76e-d7b1-4b76-8b02-4d0748e85809', 1, 'Andres', 'Espinoza Chura', 'INACTIVE'); +insert into employee (id, version, first_name, last_name, status) values ('5c1a7b82-832d-4f24-8377-54b77b91b6a8', 1, 'Carla', 'Villanueva Arce', 'ACTIVE'); \ No newline at end of file