From 93e42baa52b630dceaa3a0ab5952aebe6a04725c Mon Sep 17 00:00:00 2001 From: ricardo051199 Date: Wed, 28 Aug 2024 13:29:11 -0400 Subject: [PATCH 1/8] #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 -- 2.34.1 From aa041b33583d76c9854de16e0f451257e40699fe Mon Sep 17 00:00:00 2001 From: ricardo051199 Date: Wed, 28 Aug 2024 15:06:38 -0400 Subject: [PATCH 2/8] #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()); } } -- 2.34.1 From a393c82649423e1f0b2a60c3cf8d2160e81cc233 Mon Sep 17 00:00:00 2001 From: ricardo051199 Date: Wed, 28 Aug 2024 15:37:09 -0400 Subject: [PATCH 3/8] =?UTF-8?q?#7=20Perfil=20de=20Personal=20Administrativ?= =?UTF-8?q?o=20-=20Listado=20de=20empleados=20(Mostrar=20mensaje:=20No=20e?= =?UTF-8?q?xisten=20m=C3=A1s=20datos=20en=20la=20lista)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../views/EmployeesListView.java | 29 +++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/primefactorsolutions/views/EmployeesListView.java b/src/main/java/com/primefactorsolutions/views/EmployeesListView.java index bada406..4dc9706 100644 --- a/src/main/java/com/primefactorsolutions/views/EmployeesListView.java +++ b/src/main/java/com/primefactorsolutions/views/EmployeesListView.java @@ -7,6 +7,7 @@ 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.component.notification.Notification; import com.vaadin.flow.router.PageTitle; import com.vaadin.flow.router.Route; import com.vaadin.flow.spring.annotation.SpringComponent; @@ -98,16 +99,28 @@ public class EmployeesListView extends Main { private Button createNavigationButton(String label, int increment) { Button button = new Button(label); - button.addClickListener(event -> changePage(increment)); + button.addClickListener(event -> handlePageChange(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++; + private void handlePageChange(int increment) { + if (isPageChangeValid(increment)) { + currentPage += increment; + updateGrid(); + } else { + showNotification("No existen más datos en lista."); } - updateGrid(); + } + private boolean isPageChangeValid(int increment) { + if (increment < 0) { + return currentPage > 1; + } else if (increment > 0) { + return employeeService.hasNextPage(currentPage, pageSize, sortField, ascending); + } + return false; + } + private void showNotification(String message) { + Notification notification = new Notification(message, 3000, Notification.Position.BOTTOM_END); + notification.open(); } private Button createAddEmployeeButton() { return new Button("Add Employee", event -> @@ -117,7 +130,5 @@ public class EmployeesListView extends Main { private void updateGrid() { Page page = employeeService.getEmployeesPaginated(currentPage, pageSize, sortField, ascending); grid.setItems(page.getContent()); - previous.setEnabled(currentPage > 1); - next.setEnabled(page.hasNext()); } } -- 2.34.1 From 59c7b2af859e16de7e84eef7cb8325d7b0d5704d Mon Sep 17 00:00:00 2001 From: ricardo051199 Date: Fri, 30 Aug 2024 14:54:36 -0400 Subject: [PATCH 4/8] Integracioin modulos employeeView y employeeListView --- .../primefactorsolutions/model/Employee.java | 22 +++ .../service/EmployeeService.java | 5 +- .../views/EmployeeView.java | 169 +++++++++++++++++- 3 files changed, 188 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/primefactorsolutions/model/Employee.java b/src/main/java/com/primefactorsolutions/model/Employee.java index 6e883f0..3d0d863 100644 --- a/src/main/java/com/primefactorsolutions/model/Employee.java +++ b/src/main/java/com/primefactorsolutions/model/Employee.java @@ -19,6 +19,21 @@ import java.util.List; public class Employee extends BaseEntity { private String firstName; private String lastName; + private LocalDate birthday; + private String birthCity; + + @Enumerated(EnumType.STRING) + private MaritalStatus maritalStatus; + + private String residenceAddress; + private String phoneNumber; + private String personalEmail; + + private String emergencyCName; + private String emergencyCAddress; + private String emergencyCPhone; + private String emergencyCEmail; + @Enumerated(EnumType.STRING) private Status status; @@ -26,4 +41,11 @@ public class Employee extends BaseEntity { ACTIVE, INACTIVE } + + public enum MaritalStatus { + SINGLE, + MARRIED, + WIDOWED, + DIVORCED + } } diff --git a/src/main/java/com/primefactorsolutions/service/EmployeeService.java b/src/main/java/com/primefactorsolutions/service/EmployeeService.java index 6626fbc..5e5a94f 100644 --- a/src/main/java/com/primefactorsolutions/service/EmployeeService.java +++ b/src/main/java/com/primefactorsolutions/service/EmployeeService.java @@ -11,6 +11,8 @@ import org.springframework.stereotype.Service; import java.util.List; import java.util.UUID; +import java.util.Optional; + @Service @Data @@ -31,7 +33,8 @@ public class EmployeeService { } public Employee getEmployee(final UUID id) { - return null; + Optional employee = employeeRepository.findById(id); + return employee.orElse(null); } public Page getEmployeesPaginated(int pageNo, int pageSize, String sortField, boolean ascending) { diff --git a/src/main/java/com/primefactorsolutions/views/EmployeeView.java b/src/main/java/com/primefactorsolutions/views/EmployeeView.java index 6003b76..d2a570e 100644 --- a/src/main/java/com/primefactorsolutions/views/EmployeeView.java +++ b/src/main/java/com/primefactorsolutions/views/EmployeeView.java @@ -3,8 +3,16 @@ package com.primefactorsolutions.views; import com.primefactorsolutions.model.Employee; import com.primefactorsolutions.service.EmployeeService; import com.vaadin.flow.component.Component; +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.H3; +import com.vaadin.flow.component.notification.Notification; +import com.vaadin.flow.component.orderedlayout.HorizontalLayout; +import com.vaadin.flow.component.orderedlayout.VerticalLayout; import com.vaadin.flow.component.textfield.EmailField; import com.vaadin.flow.component.textfield.TextField; +import com.vaadin.flow.data.value.ValueChangeMode; import com.vaadin.flow.router.BeforeEvent; import com.vaadin.flow.router.HasUrlParameter; import com.vaadin.flow.router.PageTitle; @@ -13,6 +21,7 @@ import com.vaadin.flow.spring.annotation.SpringComponent; import jakarta.annotation.security.PermitAll; import org.apache.commons.lang3.StringUtils; import org.springframework.context.annotation.Scope; +import org.vaadin.firitin.components.datepicker.VDatePicker; import org.vaadin.firitin.form.BeanValidationForm; import java.util.List; @@ -24,21 +33,150 @@ import java.util.UUID; @PageTitle("Employee") @Route(value = "/employees", layout = MainLayout.class) public class EmployeeView extends BeanValidationForm implements HasUrlParameter { + private final EmployeeService employeeService; - private TextField name = null; + private final TextField firstName; + private final TextField lastName; + private final ComboBox status; + private final VDatePicker birthday; + private final TextField birthCity; + private final ComboBox maritalStatus; + private final TextField residenceAddress; + private final TextField phoneNumber; + private final EmailField personalEmail; + + private final TextField emergencyCName; + private final TextField emergencyCAddress; + private final TextField emergencyCPhone; + private final EmailField emergencyCEmail; + + private final H2 mt; + private final H3 fs; + private final H3 ss; public EmployeeView(final EmployeeService employeeService) { super(Employee.class); this.employeeService = employeeService; - name = new TextField(); - name.setWidthFull(); - name.setLabel("Name"); + + mt = new H2("Información General del Empleado"); + fs = new H3("Información Personal"); + ss = new H3("Datos de Contacto de Emergencia"); + + final HorizontalLayout mainLayout = new HorizontalLayout(); + final VerticalLayout sidebar = createSidebar(); + final VerticalLayout contentLayout = createContentLayout(); + final VerticalLayout contentLayout2 = createContentLayout(); + + firstName = new TextField("Nombres: "); + firstName.setWidthFull(); + firstName.setMaxLength(30); + firstName.setRequired(true); + + lastName = new TextField("Apellidos"); + lastName.setWidthFull(); + lastName.setMaxLength(30); + lastName.setRequired(true); + + status = new ComboBox<>("Estado"); + status.setItems(List.of("ACTIVE", "INACTIVE")); + lastName.setWidthFull(); + lastName.setMaxLength(30); + status.setRequired(true); + + birthday = new VDatePicker("Fecha de Nacimiento"); + birthday.setWidthFull(); + + birthCity = new TextField("Ciudad y País de Nacimiento"); + birthCity.setWidthFull(); + birthCity.setMaxLength(20); + + maritalStatus = new ComboBox<>("Estado Civil"); + maritalStatus.setItems(List.of("Soltero", "Casado", "Viudo", "Divorciado")); + maritalStatus.setWidthFull(); + + residenceAddress = new TextField("Dirección de Residencia"); + residenceAddress.setWidthFull(); + residenceAddress.setMaxLength(50); + + phoneNumber = new TextField("Teléfono"); + phoneNumber.setWidthFull(); + phoneNumber.setMaxLength(8); + phoneNumber.setValueChangeMode(ValueChangeMode.EAGER); + phoneNumber.addValueChangeListener(e -> { + if (!e.getValue().matches("\\d*")) { + phoneNumber.setErrorMessage("El teléfono debe contener solo números."); + } + }); + + personalEmail = new EmailField("E-mail"); + personalEmail.setWidthFull(); + personalEmail.setMaxLength(30); + + emergencyCName = new TextField("Nombres y Apellidos de Contacto"); + emergencyCName.setWidthFull(); + emergencyCName.setMaxLength(50); + + emergencyCAddress = new TextField("Dirección de Contacto"); + emergencyCAddress.setWidthFull(); + emergencyCAddress.setMaxLength(50); + + emergencyCPhone = new TextField("Teléfono de Contacto"); + emergencyCPhone.setWidthFull(); + emergencyCPhone.setMaxLength(8); + emergencyCPhone.setValueChangeMode(ValueChangeMode.EAGER); + emergencyCPhone.addValueChangeListener(e -> { + if (!e.getValue().matches("\\d*")) { + emergencyCPhone.setErrorMessage("El teléfono debe contener solo números."); + } + }); + + emergencyCEmail = new EmailField("Email de Contacto"); + emergencyCEmail.setWidthFull(); + emergencyCEmail.setMaxLength(30); + + contentLayout.add( + mt, fs, firstName, lastName, status, birthday, birthCity, maritalStatus, + residenceAddress, phoneNumber, personalEmail); + + contentLayout2.add( + ss, emergencyCName, emergencyCAddress, emergencyCPhone, emergencyCEmail); setSavedHandler((SavedHandler) employee -> { - final Employee saved = employeeService.createOrUpdate(employee); - setEntityWithEnabledSave(saved); + if (validateForm()) { + final Employee saved = employeeService.createOrUpdate(employee); + Notification.show("Empleado guardado exitosamente."); + getUI().ifPresent(ui -> ui.navigate(EmployeesListView.class)); + setEntityWithEnabledSave(saved); + } else { + Notification.show("Acción inválida. Por favor, complete todos los campos requeridos correctamente.", 3000, Notification.Position.MIDDLE); + } }); + + mainLayout.add(sidebar, contentLayout, contentLayout2); + addClassName("main-layout"); + } + + private boolean validateForm() { + return !firstName.isEmpty() && !lastName.isEmpty() && status.getValue() != null; + } + + private VerticalLayout createSidebar() { + VerticalLayout sidebar = new VerticalLayout(); + sidebar.setWidth("250px"); + sidebar.add(new Button("Información General", e -> navigateToSection("Información General"))); + sidebar.add(new Button("Detalles Profesionales", e -> navigateToSection("Detalles Profesionales"))); + return sidebar; + } + + private void navigateToSection(final String section) { + Notification.show("Navigating to " + section); + } + + private VerticalLayout createContentLayout() { + VerticalLayout contentLayout = new VerticalLayout(); + contentLayout.setWidth("100%"); + return contentLayout; } @Override @@ -53,7 +191,24 @@ public class EmployeeView extends BeanValidationForm implements HasUrl @Override protected List getFormComponents() { - return List.of(name); + return List.of( + mt, + fs, + firstName, + lastName, + status, + birthday, + birthCity, + maritalStatus, + residenceAddress, + phoneNumber, + personalEmail, + ss, + emergencyCName, + emergencyCAddress, + emergencyCPhone, + emergencyCEmail + ); } } -- 2.34.1 From 4eebdf6f0113f5242d1c1457e809d24778c52a3a Mon Sep 17 00:00:00 2001 From: ricardo051199 Date: Tue, 3 Sep 2024 11:42:20 -0400 Subject: [PATCH 5/8] #7 Perfil de Personal Administrativo - Listado de empleados (Implementar PagingGrid) --- pom.xml | 5 + .../primefactorsolutions/model/Employee.java | 108 ++++++----- .../service/EmployeeService.java | 52 +++-- .../views/EmployeesListView.java | 180 +++++++++--------- 4 files changed, 182 insertions(+), 163 deletions(-) diff --git a/pom.xml b/pom.xml index e192968..cdd0c6d 100644 --- a/pom.xml +++ b/pom.xml @@ -110,6 +110,11 @@ spring-boot-starter-test test + + commons-beanutils + commons-beanutils + 1.9.4 + org.mockito mockito-core diff --git a/src/main/java/com/primefactorsolutions/model/Employee.java b/src/main/java/com/primefactorsolutions/model/Employee.java index 3d0d863..0a8e09d 100644 --- a/src/main/java/com/primefactorsolutions/model/Employee.java +++ b/src/main/java/com/primefactorsolutions/model/Employee.java @@ -1,51 +1,59 @@ -package com.primefactorsolutions.model; - -import io.hypersistence.utils.hibernate.type.json.JsonType; -import jakarta.persistence.*; -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.EqualsAndHashCode; -import lombok.NoArgsConstructor; -import org.hibernate.annotations.Type; - -import java.time.LocalDate; -import java.util.List; - -@Data -@Entity -@AllArgsConstructor -@NoArgsConstructor -@EqualsAndHashCode(callSuper = true) -public class Employee extends BaseEntity { - private String firstName; - private String lastName; - private LocalDate birthday; - private String birthCity; - - @Enumerated(EnumType.STRING) - private MaritalStatus maritalStatus; - - private String residenceAddress; - private String phoneNumber; - private String personalEmail; - - private String emergencyCName; - private String emergencyCAddress; - private String emergencyCPhone; - private String emergencyCEmail; - - @Enumerated(EnumType.STRING) - private Status status; - - public enum Status { - ACTIVE, - INACTIVE + package com.primefactorsolutions.model; + + import io.hypersistence.utils.hibernate.type.json.JsonType; + import jakarta.persistence.*; + import lombok.AllArgsConstructor; + import lombok.Data; + import lombok.EqualsAndHashCode; + import lombok.NoArgsConstructor; + import org.hibernate.annotations.Type; + + import java.time.LocalDate; + import java.util.List; + + @Data + @Entity + @AllArgsConstructor + @NoArgsConstructor + @EqualsAndHashCode(callSuper = true) + public class Employee extends BaseEntity { + private String firstName; + private String lastName; + private LocalDate birthday; + private String birthCity; + + @Enumerated(EnumType.STRING) + private MaritalStatus maritalStatus; + + private String residenceAddress; + private String phoneNumber; + private String personalEmail; + + private String emergencyCName; + private String emergencyCAddress; + private String emergencyCPhone; + private String emergencyCEmail; + + @Enumerated(EnumType.STRING) + private Status status; + + public enum Status { + ACTIVE, + INACTIVE + } + + public enum MaritalStatus { + SINGLE, + MARRIED, + WIDOWED, + DIVORCED + } + + public Status getStatus() { + return status; + } + + public void setStatus(Status status) { + this.status = status; + } } - - public enum MaritalStatus { - SINGLE, - MARRIED, - WIDOWED, - DIVORCED - } -} diff --git a/src/main/java/com/primefactorsolutions/service/EmployeeService.java b/src/main/java/com/primefactorsolutions/service/EmployeeService.java index 5e5a94f..55c2218 100644 --- a/src/main/java/com/primefactorsolutions/service/EmployeeService.java +++ b/src/main/java/com/primefactorsolutions/service/EmployeeService.java @@ -1,49 +1,65 @@ package com.primefactorsolutions.service; import com.primefactorsolutions.model.Employee; +import org.apache.commons.beanutils.BeanComparator; 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.springframework.data.domain.Sort; import org.springframework.stereotype.Service; import java.util.List; import java.util.UUID; import java.util.Optional; +import java.util.Collections; @Service @Data public class EmployeeService { private final EmployeeRepository employeeRepository; + private List pagedBase; public EmployeeService(EmployeeRepository employeeRepository) { this.employeeRepository = employeeRepository; } + private void initializePagedBase() { + if (pagedBase == null) { + this.pagedBase = employeeRepository.findAll(); + } + } + + public List findEmployees(int start, int pageSize, String sortProperty, boolean asc) { + initializePagedBase(); // Asegurarse de que pagedBase esté inicializada + System.err.println("findAll " + start + " " + pageSize + " sort " + sortProperty + " asc:" + asc ); + int end = (int) (start + pageSize); + if (end > pagedBase.size()) { + end = pagedBase.size(); + } + Collections.sort(pagedBase, new BeanComparator(sortProperty)); + + if(!asc) { + Collections.reverse(pagedBase); + } + return pagedBase.subList((int) start, end); + } + + public List findEmployees(int start, int pageSize) { + initializePagedBase(); // Asegurarse de que pagedBase esté inicializada + System.err.println("findAll " + start + " " + pageSize); + int end = (int) (start + pageSize); + if (end > pagedBase.size()) { + end = pagedBase.size(); + } + return pagedBase.subList((int) start, end); + } + public Employee createOrUpdate(final Employee employee) { final Employee saved = employeeRepository.save(employee); return saved; } - public List getEmployees() { - return employeeRepository.findAll(); - } - public Employee getEmployee(final UUID id) { Optional employee = employeeRepository.findById(id); return employee.orElse(null); } - - 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, 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 4dc9706..3b0e664 100644 --- a/src/main/java/com/primefactorsolutions/views/EmployeesListView.java +++ b/src/main/java/com/primefactorsolutions/views/EmployeesListView.java @@ -3,18 +3,20 @@ package com.primefactorsolutions.views; import com.primefactorsolutions.model.Employee; import com.primefactorsolutions.service.EmployeeService; import com.vaadin.flow.component.button.Button; -import com.vaadin.flow.component.combobox.ComboBox; +import com.vaadin.flow.component.checkbox.Checkbox; 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.component.notification.Notification; import com.vaadin.flow.router.PageTitle; import com.vaadin.flow.router.Route; import com.vaadin.flow.spring.annotation.SpringComponent; +import org.vaadin.firitin.components.grid.PagingGrid; +import com.vaadin.flow.component.grid.GridSortOrder; +import com.vaadin.flow.data.provider.SortDirection; import jakarta.annotation.security.PermitAll; import org.springframework.context.annotation.Scope; -import org.vaadin.firitin.components.grid.VGrid; -import org.springframework.data.domain.Page; + +import java.util.List; + @SpringComponent @Scope("prototype") @@ -24,111 +26,99 @@ import org.springframework.data.domain.Page; public class EmployeesListView extends Main { 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; + private final PagingGrid table = new PagingGrid<>(Employee.class); public EmployeesListView(EmployeeService employeeService) { this.employeeService = employeeService; - addComponents(); - updateGrid(); + setupView(); + refreshGrid(); } - private void addComponents() { - addTitle(); - addOrderButtons(); - configureGrid(); - addNavigationButtons(); + private void setupView() { + add(new H2("Employee List")); + configureTable(); + add(createAddEmployeeButton()); + add(table); } - private void addTitle() { - final H2 title = new H2("Employees list"); - add(title); + private void configureTable() { + table.setColumns("firstName", "lastName", "status"); + addStatusColumn(); + addEditButtonColumn("Edit", this::navigateToEditView); + addEditButtonColumn("Save", this::navigateToSaveChangeStatus); + setupPagingGrid(); } - private void addOrderButtons() { - final HorizontalLayout hl = new HorizontalLayout(); - hl.add(createOrderButton("Employee List in Ascending Order", true)); - hl.add(createOrderButton("Employee List in Descending Order", false)); - add(hl); + private void addStatusColumn() { + table.addComponentColumn(this::createStatusCheckbox) + .setHeader("Change Status"); } - private Button createOrderButton(String label, boolean isAscending) { + private Checkbox createStatusCheckbox(Employee employee) { + Checkbox statusCheckbox = new Checkbox( employee.getStatus() == Employee.Status.ACTIVE); + return statusCheckbox; + } + + private void updateEmployeeStatus(Employee employee, boolean isActive) { + employee.setStatus(isActive ? Employee.Status.ACTIVE : Employee.Status.INACTIVE); + employeeService.createOrUpdate(employee); + refreshGrid(); + } + + private void addEditButtonColumn(String label, ButtonClickHandler handler) { + table.addComponentColumn(employee -> createButton(label, () -> handler.handle(employee))); + } + + private Button createButton(String label, Runnable onClickAction) { Button button = new Button(label); - button.addClickListener(event -> { - sortField = "firstName"; - ascending = isAscending; - updateGrid(); - }); + button.addClickListener(event -> onClickAction.run()); return button; } - private void configureGrid() { - grid = new VGrid<>(Employee.class); - grid.setColumns("firstName", "lastName", "status"); - grid.setAllRowsVisible(true); - grid.addComponentColumn(employee -> createStatusComboBox()).setHeader("Change Status"); - grid.addComponentColumn(employee -> createEditButton()); - grid.addComponentColumn(employee -> createSaveButton()); - add(grid); - } - 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 -> handlePageChange(increment)); - return button; - } - private void handlePageChange(int increment) { - if (isPageChangeValid(increment)) { - currentPage += increment; - updateGrid(); - } else { - showNotification("No existen más datos en lista."); - } - } - private boolean isPageChangeValid(int increment) { - if (increment < 0) { - return currentPage > 1; - } else if (increment > 0) { - return employeeService.hasNextPage(currentPage, pageSize, sortField, ascending); - } - return false; - } - private void showNotification(String message) { - Notification notification = new Notification(message, 3000, Notification.Position.BOTTOM_END); - notification.open(); - } private Button createAddEmployeeButton() { - return new Button("Add Employee", event -> - getUI().flatMap(ui -> ui.navigate(EmployeeView.class, "new")) - ); + return createButton("Add Employee", this::navigateToAddEmployeeView); } - private void updateGrid() { - Page page = employeeService.getEmployeesPaginated(currentPage, pageSize, sortField, ascending); - grid.setItems(page.getContent()); + + private void navigateToEditView(Employee employee) { + getUI().ifPresent(ui -> ui.navigate(EmployeeView.class, employee.getId().toString())); } -} + + private void navigateToAddEmployeeView() { + getUI().ifPresent(ui -> ui.navigate(EmployeeView.class, "new")); + } + + private void navigateToSaveChangeStatus(Employee employee) { + // no-op + } + + private void setupPagingGrid() { + table.setPaginationBarMode(PagingGrid.PaginationBarMode.BOTTOM); + table.setPageSize(5); + } + + private void refreshGrid() { + table.setPagingDataProvider((page, pageSize) -> fetchEmployees((int) page, pageSize)); + } + + private List fetchEmployees(int page, int pageSize) { + int start = page * pageSize; + if (hasSortOrder()) { + return fetchSortedEmployees(start, pageSize); + } + return employeeService.findEmployees(start, pageSize); + } + + private boolean hasSortOrder() { + return !table.getSortOrder().isEmpty(); + } + + private List fetchSortedEmployees(int start, int pageSize) { + GridSortOrder sortOrder = table.getSortOrder().getFirst(); + return employeeService.findEmployees(start, pageSize, sortOrder.getSorted().getKey(), sortOrder.getDirection() == SortDirection.ASCENDING); + } + + @FunctionalInterface + private interface ButtonClickHandler { + void handle(Employee employee); + } +} \ No newline at end of file -- 2.34.1 From 7d6955a5c355b532ae787b6721662a22a9ef049f Mon Sep 17 00:00:00 2001 From: ricardo051199 Date: Tue, 3 Sep 2024 20:00:09 -0400 Subject: [PATCH 6/8] #7 Perfil de Personal Administrativo - Listado de empleados (correcciones) --- .../primefactorsolutions/model/Employee.java | 8 ---- .../service/EmployeeService.java | 42 +++++++------------ .../views/EmployeesListView.java | 11 ----- 3 files changed, 14 insertions(+), 47 deletions(-) diff --git a/src/main/java/com/primefactorsolutions/model/Employee.java b/src/main/java/com/primefactorsolutions/model/Employee.java index 0a8e09d..94fce40 100644 --- a/src/main/java/com/primefactorsolutions/model/Employee.java +++ b/src/main/java/com/primefactorsolutions/model/Employee.java @@ -21,38 +21,30 @@ private String lastName; private LocalDate birthday; private String birthCity; - @Enumerated(EnumType.STRING) private MaritalStatus maritalStatus; - private String residenceAddress; private String phoneNumber; private String personalEmail; - private String emergencyCName; private String emergencyCAddress; private String emergencyCPhone; private String emergencyCEmail; - @Enumerated(EnumType.STRING) private Status status; - public enum Status { ACTIVE, INACTIVE } - public enum MaritalStatus { SINGLE, MARRIED, WIDOWED, DIVORCED } - public Status getStatus() { return status; } - public void setStatus(Status status) { this.status = status; } diff --git a/src/main/java/com/primefactorsolutions/service/EmployeeService.java b/src/main/java/com/primefactorsolutions/service/EmployeeService.java index 55c2218..da815d9 100644 --- a/src/main/java/com/primefactorsolutions/service/EmployeeService.java +++ b/src/main/java/com/primefactorsolutions/service/EmployeeService.java @@ -1,5 +1,4 @@ package com.primefactorsolutions.service; - import com.primefactorsolutions.model.Employee; import org.apache.commons.beanutils.BeanComparator; import com.primefactorsolutions.repositories.EmployeeRepository; @@ -16,50 +15,37 @@ import java.util.Collections; @Data public class EmployeeService { private final EmployeeRepository employeeRepository; - private List pagedBase; public EmployeeService(EmployeeRepository employeeRepository) { this.employeeRepository = employeeRepository; } - private void initializePagedBase() { - if (pagedBase == null) { - this.pagedBase = employeeRepository.findAll(); - } - } - public List findEmployees(int start, int pageSize, String sortProperty, boolean asc) { - initializePagedBase(); // Asegurarse de que pagedBase esté inicializada - System.err.println("findAll " + start + " " + pageSize + " sort " + sortProperty + " asc:" + asc ); - int end = (int) (start + pageSize); - if (end > pagedBase.size()) { - end = pagedBase.size(); - } - Collections.sort(pagedBase, new BeanComparator(sortProperty)); + List employees = employeeRepository.findAll(); - if(!asc) { - Collections.reverse(pagedBase); + int end = Math.min(start + pageSize, employees.size()); + employees.sort(new BeanComparator<>(sortProperty)); + + if (!asc) { + Collections.reverse(employees); } - return pagedBase.subList((int) start, end); + + return employees.subList(start, end); } public List findEmployees(int start, int pageSize) { - initializePagedBase(); // Asegurarse de que pagedBase esté inicializada - System.err.println("findAll " + start + " " + pageSize); - int end = (int) (start + pageSize); - if (end > pagedBase.size()) { - end = pagedBase.size(); - } - return pagedBase.subList((int) start, end); + List employees = employeeRepository.findAll(); + + int end = Math.min(start + pageSize, employees.size()); + return employees.subList(start, end); } public Employee createOrUpdate(final Employee employee) { - final Employee saved = employeeRepository.save(employee); - return saved; + return employeeRepository.save(employee); } public Employee getEmployee(final UUID id) { Optional employee = employeeRepository.findById(id); return employee.orElse(null); } -} +} \ No newline at end of file diff --git a/src/main/java/com/primefactorsolutions/views/EmployeesListView.java b/src/main/java/com/primefactorsolutions/views/EmployeesListView.java index 3b0e664..e8adeb4 100644 --- a/src/main/java/com/primefactorsolutions/views/EmployeesListView.java +++ b/src/main/java/com/primefactorsolutions/views/EmployeesListView.java @@ -43,22 +43,11 @@ public class EmployeesListView extends Main { private void configureTable() { table.setColumns("firstName", "lastName", "status"); - addStatusColumn(); addEditButtonColumn("Edit", this::navigateToEditView); addEditButtonColumn("Save", this::navigateToSaveChangeStatus); setupPagingGrid(); } - private void addStatusColumn() { - table.addComponentColumn(this::createStatusCheckbox) - .setHeader("Change Status"); - } - - private Checkbox createStatusCheckbox(Employee employee) { - Checkbox statusCheckbox = new Checkbox( employee.getStatus() == Employee.Status.ACTIVE); - return statusCheckbox; - } - private void updateEmployeeStatus(Employee employee, boolean isActive) { employee.setStatus(isActive ? Employee.Status.ACTIVE : Employee.Status.INACTIVE); employeeService.createOrUpdate(employee); -- 2.34.1 From dd95b70c66d042a79b17b79b8433b9da85e8fddc Mon Sep 17 00:00:00 2001 From: ricardo051199 Date: Tue, 3 Sep 2024 20:11:44 -0400 Subject: [PATCH 7/8] #7 Perfil de Personal Administrativo - Listado de empleados (correcciones v2) --- .../com/primefactorsolutions/views/EmployeesListView.java | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/main/java/com/primefactorsolutions/views/EmployeesListView.java b/src/main/java/com/primefactorsolutions/views/EmployeesListView.java index e8adeb4..b91c03c 100644 --- a/src/main/java/com/primefactorsolutions/views/EmployeesListView.java +++ b/src/main/java/com/primefactorsolutions/views/EmployeesListView.java @@ -3,7 +3,6 @@ package com.primefactorsolutions.views; import com.primefactorsolutions.model.Employee; import com.primefactorsolutions.service.EmployeeService; import com.vaadin.flow.component.button.Button; -import com.vaadin.flow.component.checkbox.Checkbox; import com.vaadin.flow.component.html.H2; import com.vaadin.flow.component.html.Main; import com.vaadin.flow.router.PageTitle; @@ -44,7 +43,6 @@ public class EmployeesListView extends Main { private void configureTable() { table.setColumns("firstName", "lastName", "status"); addEditButtonColumn("Edit", this::navigateToEditView); - addEditButtonColumn("Save", this::navigateToSaveChangeStatus); setupPagingGrid(); } @@ -76,10 +74,6 @@ public class EmployeesListView extends Main { getUI().ifPresent(ui -> ui.navigate(EmployeeView.class, "new")); } - private void navigateToSaveChangeStatus(Employee employee) { - // no-op - } - private void setupPagingGrid() { table.setPaginationBarMode(PagingGrid.PaginationBarMode.BOTTOM); table.setPageSize(5); -- 2.34.1 From 49dc6421f52a43c451f1977577fc79673e201b99 Mon Sep 17 00:00:00 2001 From: ricardo051199 Date: Wed, 4 Sep 2024 12:44:47 -0400 Subject: [PATCH 8/8] Correccion de estilo de codigo --- .../primefactorsolutions/model/Employee.java | 10 +++------- .../service/EmployeeService.java | 7 ++++--- .../views/EmployeeView.java | 4 ++-- .../views/EmployeesListView.java | 18 ++++++++++-------- 4 files changed, 19 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/primefactorsolutions/model/Employee.java b/src/main/java/com/primefactorsolutions/model/Employee.java index 94fce40..c6d918b 100644 --- a/src/main/java/com/primefactorsolutions/model/Employee.java +++ b/src/main/java/com/primefactorsolutions/model/Employee.java @@ -1,16 +1,12 @@ package com.primefactorsolutions.model; - - import io.hypersistence.utils.hibernate.type.json.JsonType; import jakarta.persistence.*; import lombok.AllArgsConstructor; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; - import org.hibernate.annotations.Type; - + import java.time.LocalDate; - import java.util.List; - + @Data @Entity @AllArgsConstructor @@ -45,7 +41,7 @@ public Status getStatus() { return status; } - public void setStatus(Status status) { + public void setStatus(final Status status) { this.status = status; } } diff --git a/src/main/java/com/primefactorsolutions/service/EmployeeService.java b/src/main/java/com/primefactorsolutions/service/EmployeeService.java index da815d9..993eecd 100644 --- a/src/main/java/com/primefactorsolutions/service/EmployeeService.java +++ b/src/main/java/com/primefactorsolutions/service/EmployeeService.java @@ -16,11 +16,12 @@ import java.util.Collections; public class EmployeeService { private final EmployeeRepository employeeRepository; - public EmployeeService(EmployeeRepository employeeRepository) { + public EmployeeService(final EmployeeRepository employeeRepository) { this.employeeRepository = employeeRepository; } - public List findEmployees(int start, int pageSize, String sortProperty, boolean asc) { + public List findEmployees( + final int start, final int pageSize, final String sortProperty, final boolean asc) { List employees = employeeRepository.findAll(); int end = Math.min(start + pageSize, employees.size()); @@ -33,7 +34,7 @@ public class EmployeeService { return employees.subList(start, end); } - public List findEmployees(int start, int pageSize) { + public List findEmployees(final int start, final int pageSize) { List employees = employeeRepository.findAll(); int end = Math.min(start + pageSize, employees.size()); diff --git a/src/main/java/com/primefactorsolutions/views/EmployeeView.java b/src/main/java/com/primefactorsolutions/views/EmployeeView.java index d2a570e..9e49fe4 100644 --- a/src/main/java/com/primefactorsolutions/views/EmployeeView.java +++ b/src/main/java/com/primefactorsolutions/views/EmployeeView.java @@ -145,11 +145,11 @@ public class EmployeeView extends BeanValidationForm implements HasUrl setSavedHandler((SavedHandler) employee -> { if (validateForm()) { final Employee saved = employeeService.createOrUpdate(employee); - Notification.show("Empleado guardado exitosamente."); + Notification.show("Employee saved successfully."); getUI().ifPresent(ui -> ui.navigate(EmployeesListView.class)); setEntityWithEnabledSave(saved); } else { - Notification.show("Acción inválida. Por favor, complete todos los campos requeridos correctamente.", 3000, Notification.Position.MIDDLE); + Notification.show("Please complete the required fields correctly.", 3000, Notification.Position.MIDDLE); } }); diff --git a/src/main/java/com/primefactorsolutions/views/EmployeesListView.java b/src/main/java/com/primefactorsolutions/views/EmployeesListView.java index b91c03c..489ded8 100644 --- a/src/main/java/com/primefactorsolutions/views/EmployeesListView.java +++ b/src/main/java/com/primefactorsolutions/views/EmployeesListView.java @@ -27,7 +27,7 @@ public class EmployeesListView extends Main { private final EmployeeService employeeService; private final PagingGrid table = new PagingGrid<>(Employee.class); - public EmployeesListView(EmployeeService employeeService) { + public EmployeesListView(final EmployeeService employeeService) { this.employeeService = employeeService; setupView(); refreshGrid(); @@ -46,17 +46,17 @@ public class EmployeesListView extends Main { setupPagingGrid(); } - private void updateEmployeeStatus(Employee employee, boolean isActive) { + private void updateEmployeeStatus(final Employee employee, final boolean isActive) { employee.setStatus(isActive ? Employee.Status.ACTIVE : Employee.Status.INACTIVE); employeeService.createOrUpdate(employee); refreshGrid(); } - private void addEditButtonColumn(String label, ButtonClickHandler handler) { + private void addEditButtonColumn(final String label, final ButtonClickHandler handler) { table.addComponentColumn(employee -> createButton(label, () -> handler.handle(employee))); } - private Button createButton(String label, Runnable onClickAction) { + private Button createButton(final String label, final Runnable onClickAction) { Button button = new Button(label); button.addClickListener(event -> onClickAction.run()); return button; @@ -66,7 +66,7 @@ public class EmployeesListView extends Main { return createButton("Add Employee", this::navigateToAddEmployeeView); } - private void navigateToEditView(Employee employee) { + private void navigateToEditView(final Employee employee) { getUI().ifPresent(ui -> ui.navigate(EmployeeView.class, employee.getId().toString())); } @@ -83,7 +83,7 @@ public class EmployeesListView extends Main { table.setPagingDataProvider((page, pageSize) -> fetchEmployees((int) page, pageSize)); } - private List fetchEmployees(int page, int pageSize) { + private List fetchEmployees(final int page, final int pageSize) { int start = page * pageSize; if (hasSortOrder()) { return fetchSortedEmployees(start, pageSize); @@ -95,9 +95,11 @@ public class EmployeesListView extends Main { return !table.getSortOrder().isEmpty(); } - private List fetchSortedEmployees(int start, int pageSize) { + private List fetchSortedEmployees(final int start, final int pageSize) { GridSortOrder sortOrder = table.getSortOrder().getFirst(); - return employeeService.findEmployees(start, pageSize, sortOrder.getSorted().getKey(), sortOrder.getDirection() == SortDirection.ASCENDING); + return employeeService.findEmployees(start, pageSize, + sortOrder.getSorted().getKey(), + sortOrder.getDirection() == SortDirection.ASCENDING); } @FunctionalInterface -- 2.34.1