From 22963adad9d7cc35610d99650664a82d7fcfc1e4 Mon Sep 17 00:00:00 2001 From: ricardo051199 Date: Tue, 15 Oct 2024 15:04:26 -0400 Subject: [PATCH] #37, #41#, 42, #43, #44 Listado General Vacaciones - Vista y Filtros --- .../TimeOffRequestRepository.java | 3 + .../service/TimeOffRequestService.java | 4 + .../views/RequestView.java | 13 +- .../views/RequestsListView.java | 201 ++++++++++-------- src/main/resources/data.sql | 10 +- 5 files changed, 123 insertions(+), 108 deletions(-) diff --git a/src/main/java/com/primefactorsolutions/repositories/TimeOffRequestRepository.java b/src/main/java/com/primefactorsolutions/repositories/TimeOffRequestRepository.java index 76bdcbb..7225b28 100644 --- a/src/main/java/com/primefactorsolutions/repositories/TimeOffRequestRepository.java +++ b/src/main/java/com/primefactorsolutions/repositories/TimeOffRequestRepository.java @@ -1,11 +1,14 @@ package com.primefactorsolutions.repositories; import com.primefactorsolutions.model.TimeOffRequest; +import com.primefactorsolutions.model.TimeOffRequestStatus; import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; +import java.util.Optional; import java.util.UUID; public interface TimeOffRequestRepository extends JpaRepository { List findByEmployeeId(UUID idEmployee); + Optional findByEmployeeIdAndState(UUID employeeId, TimeOffRequestStatus state); } diff --git a/src/main/java/com/primefactorsolutions/service/TimeOffRequestService.java b/src/main/java/com/primefactorsolutions/service/TimeOffRequestService.java index a1f673f..2f7f661 100644 --- a/src/main/java/com/primefactorsolutions/service/TimeOffRequestService.java +++ b/src/main/java/com/primefactorsolutions/service/TimeOffRequestService.java @@ -34,4 +34,8 @@ public class TimeOffRequestService { public List findRequestsByEmployeeId(final UUID idEmployee) { return timeOffRequestRepository.findByEmployeeId(idEmployee); } + + public Optional findByEmployeeAndState(final UUID employeeId, final TimeOffRequestStatus state) { + return timeOffRequestRepository.findByEmployeeIdAndState(employeeId, state); + } } \ No newline at end of file diff --git a/src/main/java/com/primefactorsolutions/views/RequestView.java b/src/main/java/com/primefactorsolutions/views/RequestView.java index 45b1ea4..1fae708 100644 --- a/src/main/java/com/primefactorsolutions/views/RequestView.java +++ b/src/main/java/com/primefactorsolutions/views/RequestView.java @@ -111,11 +111,11 @@ public class RequestView extends BeanValidationForm implements H setRequestFieldValues(request); requestService.saveTimeOffRequest(request); Notification.show("Request saved successfully."); - clearForm(); + closeForm(); } } - private void setRequestFieldValues(TimeOffRequest request) { + private void setRequestFieldValues(final TimeOffRequest request) { request.setState(state.getValue()); request.setExpiration(expiration.getValue()); request.setStartDate(startDate.getValue()); @@ -137,15 +137,6 @@ public class RequestView extends BeanValidationForm implements H && daysToBeTake.getValue() != null; } - private void clearForm() { - state.clear(); - expiration.clear(); - startDate.clear(); - endDate.clear(); - availableDays.clear(); - daysToBeTake.clear(); - } - private void configureViewOrEditAction(final String action) { if ("edit".equals(action) && !request.getId().toString().isEmpty()) { setFieldsReadOnly(false); diff --git a/src/main/java/com/primefactorsolutions/views/RequestsListView.java b/src/main/java/com/primefactorsolutions/views/RequestsListView.java index 71216d2..24207cc 100644 --- a/src/main/java/com/primefactorsolutions/views/RequestsListView.java +++ b/src/main/java/com/primefactorsolutions/views/RequestsListView.java @@ -7,7 +7,6 @@ import com.primefactorsolutions.service.TimeOffRequestService; import com.vaadin.flow.component.button.Button; import com.vaadin.flow.component.combobox.ComboBox; import com.vaadin.flow.component.html.Main; -import com.vaadin.flow.component.html.Span; import com.vaadin.flow.component.notification.Notification; import com.vaadin.flow.component.orderedlayout.HorizontalLayout; import com.vaadin.flow.router.PageTitle; @@ -17,9 +16,7 @@ import jakarta.annotation.security.PermitAll; import org.springframework.context.annotation.Scope; import org.vaadin.firitin.components.grid.PagingGrid; -import java.util.Collections; -import java.util.List; -import java.util.UUID; +import java.util.*; import java.util.stream.Collectors; @SpringComponent @@ -32,23 +29,25 @@ public class RequestsListView extends Main { private final TimeOffRequestService requestService; private final EmployeeService employeeService; private final TeamService teamService; - private final PagingGrid requestGrid = new PagingGrid<>(TimeOffRequest.class); - private List requests = Collections.emptyList(); + private final PagingGrid requestGrid = new PagingGrid<>(); + + private List employees = Collections.emptyList(); private ComboBox employeeFilter; private ComboBox teamFilter; private ComboBox categoryFilter; - private ComboBox stateFilter; + private ComboBox stateFilter; private UUID selectedEmployeeId; + public RequestsListView(final TimeOffRequestService requestService, final EmployeeService employeeService, final TeamService teamService) { this.requestService = requestService; this.employeeService = employeeService; this.teamService = teamService; - this.requests = requestService.findAllTimeOffRequests(); + this.employees = employeeService.findAllEmployees(); initializeView(); - refreshRequestGrid(null, null, null, null); + refreshGeneralRequestGrid(null, null, null, null); } private void initializeView() { @@ -66,24 +65,20 @@ public class RequestsListView extends Main { } private void setupRequestGrid() { - requestGrid.setColumns( - "category", - "state", - "availableDays", - "expiration", - "startDate", - "endDate", - "daysToBeTake", - "daysBalance" - ); - requestGrid.addComponentColumn(this::createEmployeeSpan).setHeader("Employee"); - requestGrid.addComponentColumn(this::createTeamSpan).setHeader("Team"); + requestGrid.addColumn(this::getEmployeeFullName).setHeader("Employee"); + requestGrid.addColumn(this::getTeamName).setHeader("Team"); + requestGrid.addColumn(this::getEmployeeStatus).setHeader("State"); + requestGrid.addColumn(this::getCategory).setHeader("Category"); + requestGrid.addColumn(this::getStartDate).setHeader("Start Date"); + requestGrid.addColumn(this::getEndDate).setHeader("End Date"); + requestGrid.addColumn(this::getDaysBalance).setHeader("Days Balance"); + requestGrid.setPaginationBarMode(PagingGrid.PaginationBarMode.BOTTOM); requestGrid.setPageSize(5); requestGrid.asSingleSelect().addValueChangeListener(event -> { - TimeOffRequest selectedRequest = event.getValue(); + Employee selectedRequest = event.getValue(); if (selectedRequest != null) { - selectedEmployeeId = selectedRequest.getEmployee().getId(); + selectedEmployeeId = selectedRequest.getId(); } }); } @@ -100,90 +95,114 @@ public class RequestsListView extends Main { return new HorizontalLayout(viewButton, closeButton); } - private void refreshRequestGrid(final Employee employee, - final Team team, - final TimeOffRequestType category, - final TimeOffRequestStatus state) { - + private void refreshGeneralRequestGrid(final Employee employee, + final Team team, + final TimeOffRequestType category, + final Status state) { requestGrid.setPagingDataProvider((page, pageSize) -> { - requests = requestService.findAllTimeOffRequests(); int start = (int) (page * requestGrid.getPageSize()); - if (allFiltersAreNull(employee, team, category, state)) { - return fetchTimeOffRequests(start, pageSize); - } else { - return fetchFilteredTimeOffRequests(start, pageSize, employee, team, category, state); - } + return fetchFilteredEmployees(start, pageSize, employee, team, category, state); }); requestGrid.getDataProvider().refreshAll(); } - private boolean allFiltersAreNull(final Employee employee, - final Team team, - final TimeOffRequestType category, - final TimeOffRequestStatus state) { - return employee == null && team == null && category == null && state == null; - } + private List fetchFilteredEmployees(final int start, + final int pageSize, + final Employee employee, + final Team team, + final TimeOffRequestType category, + final Status state) { + List filteredEmployees = employeeService.findAllEmployees(); - private List fetchTimeOffRequests(final int start, final int pageSize) { - int end = start + pageSize; - if (end > requests.size()) { - end = requests.size(); - } - return requests.subList(start, end); - } - - private List fetchFilteredTimeOffRequests(final int start, - final int pageSize, - final Employee employee, - final Team team, - final TimeOffRequestType category, - final TimeOffRequestStatus state) { if (employee != null && !"ALL".equals(employee.getFirstName())) { - requests = requests.stream() - .filter(request -> request.getEmployee().getId().equals(employee.getId())) + filteredEmployees = filteredEmployees.stream() + .filter(emp -> emp.getId().equals(employee.getId())) .collect(Collectors.toList()); } + if (team != null && !"ALL".equals(team.getName())) { - requests = requests.stream() - .filter(request -> request.getEmployee().getTeam().getId().equals(team.getId())) + filteredEmployees = filteredEmployees.stream() + .filter(emp -> emp.getTeam() != null && emp.getTeam().getId().equals(team.getId())) .collect(Collectors.toList()); } - if (category != null && !"ALL".equals(category.name())) { - requests = requests.stream() - .filter(request -> request.getCategory().equals(category)) + + if (category != null && category != TimeOffRequestType.values()[0]) { + filteredEmployees = filteredEmployees.stream() + .filter(emp -> { + Optional request = requestService + .findByEmployeeAndState(emp.getId(), TimeOffRequestStatus.TAKEN); + return request.isPresent() && request.get().getCategory() == category; + }) .collect(Collectors.toList()); } - if (state != null && !"ALL".equals(state.name())) { - requests = requests.stream() - .filter(request -> request.getState().equals(state)) + + if (state != null && state != Status.ALL) { + filteredEmployees = filteredEmployees.stream() + .filter(emp -> { + Optional request = requestService + .findByEmployeeAndState(emp.getId(), TimeOffRequestStatus.TAKEN); + return state == Status.IDLE ? request.isPresent() : request.isEmpty(); + }) .collect(Collectors.toList()); } - int end = start + pageSize; - if (end > requests.size()) { - end = requests.size(); - } - return requests.subList(start, end); + + int end = Math.min(start + pageSize, filteredEmployees.size()); + return filteredEmployees.subList(start, end); + } + + private String getEmployeeFullName(final Employee employee) { + return "ALL".equals(employee.getFirstName()) ? "ALL" : employee.getFirstName() + " " + employee.getLastName(); + } + + private String getTeamName(final Employee employee) { + Team team = employee.getTeam(); + return team != null ? team.getName() : "Unassigned"; + } + + private String getTeamLabel(final Team team) { + return "ALL".equals(team.getName()) ? "ALL" : team.getName(); } - private Span createEmployeeSpan(final TimeOffRequest timeOffRequest) { - Employee employee = timeOffRequest.getEmployee(); - return new Span(employee.getFirstName() + " " + employee.getLastName()); + private String getEmployeeStatus(final Employee employee) { + Optional activeRequest = requestService + .findByEmployeeAndState(employee.getId(), TimeOffRequestStatus.TAKEN); + return activeRequest.isPresent() ? "IDLE" : "ACTIVE"; } - private Span createTeamSpan(final TimeOffRequest timeOffRequest) { - return new Span(timeOffRequest.getEmployee().getTeam().getName()); + private String getCategory(final Employee employee) { + Optional activeRequest = requestService + .findByEmployeeAndState(employee.getId(), TimeOffRequestStatus.TAKEN); + return activeRequest.map(request -> request.getCategory().toString()).orElse(""); + } + + private String getStartDate(final Employee employee) { + Optional activeRequest = requestService + .findByEmployeeAndState(employee.getId(), TimeOffRequestStatus.TAKEN); + return activeRequest.map(request -> request.getStartDate().toString()).orElse(""); + } + + private String getEndDate(final Employee employee) { + Optional activeRequest = requestService + .findByEmployeeAndState(employee.getId(), TimeOffRequestStatus.TAKEN); + return activeRequest.map(request -> request.getEndDate().toString()).orElse(""); + } + + private String getDaysBalance(final Employee employee) { + Optional activeRequest = requestService + .findByEmployeeAndState(employee.getId(), TimeOffRequestStatus.TAKEN); + return activeRequest.map(request -> request.getDaysBalance().toString()).orElse(""); } private ComboBox createEmployeeFilter() { employeeFilter = new ComboBox<>("Employee"); - List employees = employeeService.findAllEmployees(); + List employees = new ArrayList<>(employeeService.findAllEmployees()); employees.addFirst(createAllEmployeesOption()); employeeFilter.setItems(employees); - employeeFilter.setItemLabelGenerator(this::getEmployeeLabel); + employeeFilter.setItemLabelGenerator(this::getEmployeeFullName); employeeFilter.setValue(employees.getFirst()); employeeFilter.addValueChangeListener(event -> - refreshRequestGrid( + refreshGeneralRequestGrid( event.getValue(), teamFilter.getValue(), categoryFilter.getValue(), @@ -195,13 +214,13 @@ public class RequestsListView extends Main { private ComboBox createTeamFilter() { teamFilter = new ComboBox<>("Team"); - List teams = teamService.findAllTeams(); + List teams = new ArrayList<>(teamService.findAllTeams()); teams.addFirst(createAllTeamsOption()); teamFilter.setItems(teams); teamFilter.setItemLabelGenerator(this::getTeamLabel); teamFilter.setValue(teams.getFirst()); teamFilter.addValueChangeListener(event -> - refreshRequestGrid( + refreshGeneralRequestGrid( employeeFilter.getValue(), event.getValue(), categoryFilter.getValue(), @@ -216,7 +235,7 @@ public class RequestsListView extends Main { categoryFilter.setItems(TimeOffRequestType.values()); categoryFilter.setValue(TimeOffRequestType.values()[0]); categoryFilter.addValueChangeListener(event -> - refreshRequestGrid( + refreshGeneralRequestGrid( employeeFilter.getValue(), teamFilter.getValue(), event.getValue(), @@ -226,12 +245,12 @@ public class RequestsListView extends Main { return categoryFilter; } - private ComboBox createStateFilter() { + private ComboBox createStateFilter() { stateFilter = new ComboBox<>("State"); - stateFilter.setItems(TimeOffRequestStatus.values()); - stateFilter.setValue(TimeOffRequestStatus.values()[0]); + stateFilter.setItems(Status.values()); + stateFilter.setValue(Status.values()[0]); stateFilter.addValueChangeListener(event -> - refreshRequestGrid( + refreshGeneralRequestGrid( employeeFilter.getValue(), teamFilter.getValue(), categoryFilter.getValue(), @@ -241,6 +260,12 @@ public class RequestsListView extends Main { return stateFilter; } + private enum Status { + ALL, + IDLE, + ACTIVE + } + private Employee createAllEmployeesOption() { Employee allEmployeesOption = new Employee(); allEmployeesOption.setFirstName("ALL"); @@ -253,14 +278,6 @@ public class RequestsListView extends Main { return allTeamsOption; } - private String getEmployeeLabel(final Employee employee) { - return "ALL".equals(employee.getFirstName()) ? "ALL" : employee.getFirstName() + " " + employee.getLastName(); - } - - private String getTeamLabel(final Team team) { - return "ALL".equals(team.getName()) ? "ALL" : team.getName(); - } - private void navigateToMainView() { getUI().ifPresent(ui -> ui.navigate(MainView.class)); } diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql index 93f2ca9..5aca318 100644 --- a/src/main/resources/data.sql +++ b/src/main/resources/data.sql @@ -43,26 +43,26 @@ values ('2fa314bc-f547-4b12-a8b6-bb789feabc12', 1, '19b5a76e-d7b1-4b76-8b02-4d07 insert into time_off_request (id, version, employee_id, category, state, available_days, expiration, start_date, end_date, days_to_be_take, days_balance) values ('d5f6341a-913d-4e7f-a0b2-cfe0786acd34', 1, 'cba3efb7-32bc-44be-9fdc-fc5e4f211254', 'FIXED_HOLIDAY', 'IN_USE', 20, '2025-11-30', '2024-11-10', '2024-11-20', 10, 10); insert into time_off_request (id, version, employee_id, category, state, available_days, expiration, start_date, end_date, days_to_be_take, days_balance) -values ('4f913b23-ff23-4527-bcd6-adfe01234567', 1, 'e99b7af5-7d3a-4c0f-b8bc-e8d0388d8fc4', 'MATERNITY', 'TAKEN', 18, '2025-06-30', '2024-07-01', '2024-07-15', 10, 8); +values ('4f913b23-ff23-4527-bcd6-adfe01234567', 1, 'e99b7af5-7d3a-4c0f-b8bc-e8d0388d8fc4', 'MATERNITY', 'IN_USE', 18, '2025-06-30', '2024-07-01', '2024-07-15', 10, 8); insert into time_off_request (id, version, employee_id, category, state, available_days, expiration, start_date, end_date, days_to_be_take, days_balance) values ('8c653f2a-f9a3-4d67-b3b6-12ad98fe0983', 1, 'f6ab3c6d-7078-45f6-9b22-4e37637bfec6', 'VACATION', 'REQUESTED', 10, '2025-10-31', '2024-09-15', '2024-09-20', 5, 5); insert into time_off_request (id, version, employee_id, category, state, available_days, expiration, start_date, end_date, days_to_be_take, days_balance) values ('fb9d9d75-b2ab-4ea4-b8b3-0a8f89e5c123', 1, '2e2293b1-3f9a-4f3d-abc8-32639b0a5e15', 'FIXED_HOLIDAY', 'IN_USE', 12, '2025-08-31', '2024-08-05', '2024-08-15', 6, 6); insert into time_off_request (id, version, employee_id, category, state, available_days, expiration, start_date, end_date, days_to_be_take, days_balance) -values ('1c913a12-46e9-47b7-9e31-ab903fedc789', 1, '4b1c6c35-4627-4b35-b6e9-dc75c68b2c31', 'BIRTHDAY', 'TAKEN', 14, '2025-12-31', '2024-10-20', '2024-10-25', 5, 9); +values ('1c913a12-46e9-47b7-9e31-ab903fedc789', 1, '4b1c6c35-4627-4b35-b6e9-dc75c68b2c31', 'BIRTHDAY', 'APPROVED', 14, '2025-12-31', '2024-10-20', '2024-10-25', 5, 9); insert into time_off_request (id, version, employee_id, category, state, available_days, expiration, start_date, end_date, days_to_be_take, days_balance) values ('b1249d3a-cc34-4954-88d9-1e4f67fe2436', 1, 'afc5c741-f70a-4394-853b-39d51b118927', 'MATERNITY', 'APPROVED', 20, '2025-11-30', '2024-11-05', '2024-11-12', 7, 13); insert into time_off_request (id, version, employee_id, category, state, available_days, expiration, start_date, end_date, days_to_be_take, days_balance) -values ('6fdc47a8-127b-41c4-8d12-7fc12098ab12', 1, 'b2436b82-7b9f-4f0d-9463-f2c3173a45c3', 'VACATION', 'TAKEN', 18, '2025-06-30', '2024-07-10', '2024-07-20', 8, 10); +values ('6fdc47a8-127b-41c4-8d12-7fc12098ab12', 1, 'b2436b82-7b9f-4f0d-9463-f2c3173a45c3', 'VACATION', 'PENDING', 18, '2025-06-30', '2024-07-10', '2024-07-20', 8, 10); insert into time_off_request (id, version, employee_id, category, state, available_days, expiration, start_date, end_date, days_to_be_take, days_balance) values ('12ec8b74-983d-4a17-b67e-134f45ae904c', 1, '5c1a7b82-832d-4f24-8377-54b77b91b6a8', 'FIXED_HOLIDAY', 'PENDING', 15, '2025-12-31', '2024-09-01', '2024-09-05', 4, 11); insert into time_off_request (id, version, employee_id, category, state, available_days, expiration, start_date, end_date, days_to_be_take, days_balance) values ('89bc4b2a-943f-487c-a9f3-bacf78145e67', 1, 'cba3efb7-32bc-44be-9fdc-fc5e4f211254', 'VACATION', 'APPROVED', 20, '2025-11-30', '2024-10-25', '2024-11-05', 9, 11); insert into time_off_request (id, version, employee_id, category, state, available_days, expiration, start_date, end_date, days_to_be_take, days_balance) -values ('37adfc2a-7463-4b2d-a7c1-fae04567cdef', 1, 'e99b7af5-7d3a-4c0f-b8bc-e8d0388d8fc4', 'BIRTHDAY', 'TAKEN', 18, '2025-06-30', '2024-06-01', '2024-06-10', 6, 12); +values ('37adfc2a-7463-4b2d-a7c1-fae04567cdef', 1, 'e99b7af5-7d3a-4c0f-b8bc-e8d0388d8fc4', 'BIRTHDAY', 'REQUESTED', 18, '2025-06-30', '2024-06-01', '2024-06-10', 6, 12); insert into time_off_request (id, version, employee_id, category, state, available_days, expiration, start_date, end_date, days_to_be_take, days_balance) values ('2bc138ea-12db-4b89-a0b4-78e045e34b4e', 1, 'f6ab3c6d-7078-45f6-9b22-4e37637bfec6', 'MATERNITY', 'REQUESTED', 10, '2025-10-31', '2024-10-01', '2024-10-10', 3, 7); insert into time_off_request (id, version, employee_id, category, state, available_days, expiration, start_date, end_date, days_to_be_take, days_balance) values ('14de1a56-6893-4e12-90f3-4faec457f002', 1, 'cd80e1d0-9a08-44a6-bd63-2c63eaa003d4', 'FIXED_HOLIDAY', 'PENDING', 22, '2025-08-31', '2024-07-15', '2024-07-25', 8, 14); insert into time_off_request (id, version, employee_id, category, state, available_days, expiration, start_date, end_date, days_to_be_take, days_balance) -values ('fb08a6c9-cd17-42e8-b9e2-734ec834cae2', 1, '4b1c6c35-4627-4b35-b6e9-dc75c68b2c31', 'VACATION', 'TAKEN', 16, '2025-12-31', '2024-09-30', '2024-10-05', 4, 12); +values ('fb08a6c9-cd17-42e8-b9e2-734ec834cae2', 1, '4b1c6c35-4627-4b35-b6e9-dc75c68b2c31', 'VACATION', 'PENDING', 16, '2025-12-31', '2024-09-30', '2024-10-05', 4, 12); -- 2.34.1