From 7d6955a5c355b532ae787b6721662a22a9ef049f Mon Sep 17 00:00:00 2001 From: ricardo051199 Date: Tue, 3 Sep 2024 20:00:09 -0400 Subject: [PATCH] #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);