Vacaciones del Empleado - Refactorizar

This commit is contained in:
jesus.pelaez 2024-10-11 00:53:46 -04:00
parent 62d678c407
commit 602ab686f5
4 changed files with 92 additions and 150 deletions

View File

@ -1,11 +1,15 @@
package com.primefactorsolutions.repositories; package com.primefactorsolutions.repositories;
import com.primefactorsolutions.model.TimeOffRequest; import com.primefactorsolutions.model.TimeOffRequest;
import com.primefactorsolutions.model.TimeOffRequestType;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
public interface TimeOffRequestRepository extends JpaRepository<TimeOffRequest, UUID> { public interface TimeOffRequestRepository extends JpaRepository<TimeOffRequest, UUID> {
List<TimeOffRequest> findByEmployee_Id(UUID id_employee); List<TimeOffRequest> findByEmployeeId(UUID idEmployee);
List<TimeOffRequest> findByTeamId(UUID idTeam);
List<TimeOffRequest> findByCategory(TimeOffRequestType category);
List<TimeOffRequest> findByState(TimeOffRequest.Status state);
} }

View File

@ -3,14 +3,11 @@ package com.primefactorsolutions.service;
import com.primefactorsolutions.model.*; import com.primefactorsolutions.model.*;
import com.primefactorsolutions.repositories.TimeOffRequestRepository; import com.primefactorsolutions.repositories.TimeOffRequestRepository;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import org.apache.commons.beanutils.BeanComparator;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors;
@Service @Service
@AllArgsConstructor @AllArgsConstructor
@ -25,68 +22,28 @@ public class TimeOffRequestService {
timeOffRequestRepository.deleteById(id); timeOffRequestRepository.deleteById(id);
} }
public List<TimeOffRequest> getAllTimeOffRequests() { public List<TimeOffRequest> findAllTimeOffRequests() {
return timeOffRequestRepository.findAll(); return timeOffRequestRepository.findAll();
} }
public TimeOffRequest getTimeOffRequest(final UUID id) { public TimeOffRequest findTimeOffRequest(final UUID id) {
Optional<TimeOffRequest> timeOffRequest = timeOffRequestRepository.findById(id); Optional<TimeOffRequest> timeOffRequest = timeOffRequestRepository.findById(id);
return timeOffRequest.orElse(null); return timeOffRequest.orElse(null);
} }
public List<TimeOffRequest> findTimeOffRequests( public List<TimeOffRequest> findRequestsByEmployeeId(final UUID idEmployee) {
final int start, final int pageSize, final String sortProperty, final boolean asc) { return timeOffRequestRepository.findByEmployeeId(idEmployee);
List<TimeOffRequest> timeOffRequests = timeOffRequestRepository.findAll();
int end = Math.min(start + pageSize, timeOffRequests.size());
timeOffRequests.sort(new BeanComparator<>(sortProperty));
if (!asc) {
Collections.reverse(timeOffRequests);
}
return timeOffRequests.subList(start, end);
} }
public List<TimeOffRequest> findTimeOffRequests(final int start, final int pageSize) { public List<TimeOffRequest> findRequestsByTeamId(final UUID idTeam) {
List<TimeOffRequest> timeOffRequests = timeOffRequestRepository.findAll(); return timeOffRequestRepository.findByTeamId(idTeam);
int end = Math.min(start + pageSize, timeOffRequests.size());
return timeOffRequests.subList(start, end);
} }
public List<TimeOffRequest> findTimeOffRequestBy(final Employee employee, public List<TimeOffRequest> findRequestsByCategory(final TimeOffRequestType category) {
final Team team, return timeOffRequestRepository.findByCategory(category);
final TimeOffRequestType category,
final TimeOffRequest.Status state,
final int start,
final int pageSize) {
List<TimeOffRequest> timeOffRequests = timeOffRequestRepository.findAll();
if (employee != null && !"ALL".equals(employee.getFirstName())) {
timeOffRequests = timeOffRequests.stream()
.filter(req -> req.getEmployee().equals(employee))
.collect(Collectors.toList());
}
if (team != null && !"ALL".equals(team.getName())) {
timeOffRequests = timeOffRequests.stream()
.filter(req -> req.getTeam().equals(team))
.collect(Collectors.toList());
}
if (category != null && !"ALL".equals(category.name())) {
timeOffRequests = timeOffRequests.stream()
.filter(req -> req.getCategory().equals(category))
.collect(Collectors.toList());
}
if (state != null && !"ALL".equals(state.name())) {
timeOffRequests = timeOffRequests.stream()
.filter(req -> req.getState().equals(state))
.collect(Collectors.toList());
}
int end = Math.min(start + pageSize, timeOffRequests.size());
return timeOffRequests.subList(start, end);
} }
public List<TimeOffRequest> findRequestsByEmployeeId(UUID id_employee) { public List<TimeOffRequest> findRequestsByState(final TimeOffRequest.Status state) {
return timeOffRequestRepository.findByEmployee_Id(id_employee); return timeOffRequestRepository.findByState(state);
} }
} }

View File

@ -7,22 +7,19 @@ import com.primefactorsolutions.service.EmployeeService;
import com.primefactorsolutions.service.TimeOffRequestService; import com.primefactorsolutions.service.TimeOffRequestService;
import com.vaadin.flow.component.button.Button; import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.combobox.ComboBox; import com.vaadin.flow.component.combobox.ComboBox;
import com.vaadin.flow.component.grid.GridSortOrder; import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.html.Div; import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.H3; import com.vaadin.flow.component.html.H3;
import com.vaadin.flow.component.html.Span; import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout; import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout; import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.data.provider.SortDirection;
import com.vaadin.flow.router.BeforeEvent; import com.vaadin.flow.router.BeforeEvent;
import com.vaadin.flow.router.HasUrlParameter; import com.vaadin.flow.router.HasUrlParameter;
import com.vaadin.flow.router.PageTitle; import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route; import com.vaadin.flow.router.Route;
import com.vaadin.flow.spring.annotation.SpringComponent; import com.vaadin.flow.spring.annotation.SpringComponent;
import jakarta.annotation.security.PermitAll; import jakarta.annotation.security.PermitAll;
import org.apache.commons.beanutils.BeanComparator;
import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.Scope;
import org.vaadin.firitin.components.grid.PagingGrid;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -38,21 +35,22 @@ public class RequestView extends Div implements HasUrlParameter<String> {
private final TimeOffRequestService requestService; private final TimeOffRequestService requestService;
private final EmployeeService employeeService; private final EmployeeService employeeService;
private final PagingGrid<TimeOffRequest> requestGrid = new PagingGrid<>(TimeOffRequest.class); private final Grid<TimeOffRequest> requestGrid = new Grid<>(TimeOffRequest.class);
private List<TimeOffRequest> requests; private List<TimeOffRequest> requests = Collections.emptyList();
private ComboBox<TimeOffRequestType> categoryFilter; private ComboBox<TimeOffRequestType> categoryFilter;
private ComboBox<TimeOffRequest.Status> stateFilter; private ComboBox<TimeOffRequest.Status> stateFilter;
private UUID employeeId; private UUID employeeId;
public RequestView(TimeOffRequestService requestService, EmployeeService employeeService) { public RequestView(final TimeOffRequestService requestService, final EmployeeService employeeService) {
this.requestService = requestService; this.requestService = requestService;
this.employeeService = employeeService; this.employeeService = employeeService;
this.requests = Collections.emptyList(); initializeView();
}
private void initializeView() {
setupFilters(); setupFilters();
setupGrid(); setupGrid();
add(requestGrid); add(requestGrid, createSummaryLayout(), createActionButtons());
add(createSummaryLayout());
add(createActionButtons());
refreshRequestGrid(null, null); refreshRequestGrid(null, null);
} }
@ -89,16 +87,18 @@ public class RequestView extends Div implements HasUrlParameter<String> {
"daysToBeTake", "daysToBeTake",
"daysBalance" "daysBalance"
); );
requestGrid.setAllRowsVisible(true);
} }
private VerticalLayout createSummaryLayout() { private VerticalLayout createSummaryLayout() {
int totalVacations = 15; int totalVacations = 15;
int totalTimeOff = 2; int totalTimeOff = 2;
int totalAvailableDays = totalVacations + totalTimeOff; int totalAvailableDays = totalVacations + totalTimeOff;
Span holidaysLabel = new Span("TOTAL HOLIDAYS: " + totalVacations); return new VerticalLayout(
Span timeOffLabel = new Span("TOTAL TIME OFF: " + totalTimeOff); new Span("TOTAL HOLIDAYS: " + totalVacations),
Span availableDaysLabel = new Span("TOTAL AVAILABLE DAYS: " + totalAvailableDays); new Span("TOTAL TIME OFF: " + totalTimeOff),
return new VerticalLayout(holidaysLabel, timeOffLabel, availableDaysLabel); new Span("TOTAL AVAILABLE DAYS: " + totalAvailableDays)
);
} }
private HorizontalLayout createActionButtons() { private HorizontalLayout createActionButtons() {
@ -111,47 +111,21 @@ public class RequestView extends Div implements HasUrlParameter<String> {
private void navigateToRequestsListView() { private void navigateToRequestsListView() {
getUI().ifPresent(ui -> ui.navigate(RequestsListView.class)); getUI().ifPresent(ui -> ui.navigate(RequestsListView.class));
} }
private void refreshRequestGrid(final TimeOffRequestType category, private void refreshRequestGrid(final TimeOffRequestType category, final TimeOffRequest.Status state) {
final TimeOffRequest.Status state) { List<TimeOffRequest> filteredRequests = allFiltersAreNull(category, state)
requestGrid.setPagingDataProvider((page, pageSize) -> { ? requestService.findRequestsByEmployeeId(employeeId)
if (allFiltersAreNull(category, state)) { : fetchFilteredTimeOffRequests(category, state);
return fetchTimeOffRequests((int) page, pageSize); requestGrid.setItems(filteredRequests);
} else {
return fetchFilteredTimeOffRequests((int) page, pageSize,category, state);
}
});
requestGrid.getDataProvider().refreshAll();
} }
private boolean allFiltersAreNull(TimeOffRequestType category, TimeOffRequest.Status state) { private boolean allFiltersAreNull(final TimeOffRequestType category, final TimeOffRequest.Status state) {
return category == null && state == null; return category == null && state == null;
} }
private List<TimeOffRequest> fetchTimeOffRequests(int page, int pageSize) { private List<TimeOffRequest> fetchFilteredTimeOffRequests(final TimeOffRequestType category,
int start = page * pageSize; final TimeOffRequest.Status state) {
if (requestGrid.getSortOrder().isEmpty()) {
int end = Math.min(start + pageSize, requests.size());
return requests.subList(start, end);
} else {
return fetchSortedTimeOffRequests(start, pageSize);
}
}
private List<TimeOffRequest> fetchSortedTimeOffRequests(int start, int pageSize) {
GridSortOrder<TimeOffRequest> sortOrder = requestGrid.getSortOrder().getFirst();
int end = Math.min(start + pageSize, requests.size());
requests.sort(new BeanComparator<>(sortOrder.getSorted().getKey()));
if (sortOrder.getDirection() != SortDirection.ASCENDING) {
Collections.reverse(requests);
}
return requests.subList(start, end);
}
private List<TimeOffRequest> fetchFilteredTimeOffRequests(int page, int pageSize, TimeOffRequestType category, TimeOffRequest.Status state) {
requests = requestService.findRequestsByEmployeeId(employeeId); requests = requestService.findRequestsByEmployeeId(employeeId);
if (category != null && !"ALL".equals(category.name())) { if (category != null && !"ALL".equals(category.name())) {
requests = requests.stream() requests = requests.stream()
@ -163,31 +137,20 @@ public class RequestView extends Div implements HasUrlParameter<String> {
.filter(req -> req.getState().equals(state)) .filter(req -> req.getState().equals(state))
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
int end = Math.min(page + pageSize, requests.size()); return requests;
return requests.subList(page, end);
} }
@Override @Override
public void setParameter(BeforeEvent event, String parameter) { public void setParameter(final BeforeEvent event, final String parameter) {
try { employeeId = UUID.fromString(parameter);
employeeId = UUID.fromString(parameter); Employee employee = employeeService.getEmployee(employeeId);
Employee employee = employeeService.getEmployee(employeeId); requests = requestService.findRequestsByEmployeeId(employeeId);
setViewTitle(employee.getFirstName() + " " + employee.getLastName(), employee.getTeam());
String employeeName = employee.getFirstName() + " " + employee.getLastName(); requestGrid.setItems(requests);
String employeeTeam = employee.getTeam();
setViewTitle(employeeName, employeeTeam);
requests = requestService.findRequestsByEmployeeId(employeeId);
requestGrid.setItems(requests);
} catch (IllegalArgumentException e) {
requestGrid.setItems();
}
} }
private void setViewTitle(String employeeName, String employeeTeam) { private void setViewTitle(final String employeeName, final String employeeTeam) {
H3 employeeTitle = new H3("Name: " + employeeName); addComponentAsFirst(new H3("Name: " + employeeName));
H3 teamTitle = new H3("Team: " + employeeTeam); addComponentAtIndex(1, new H3("Team: " + employeeTeam));
addComponentAsFirst(employeeTitle);
addComponentAtIndex(1, teamTitle);
} }
} }

View File

@ -6,12 +6,10 @@ import com.primefactorsolutions.service.TeamService;
import com.primefactorsolutions.service.TimeOffRequestService; import com.primefactorsolutions.service.TimeOffRequestService;
import com.vaadin.flow.component.button.Button; import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.combobox.ComboBox; import com.vaadin.flow.component.combobox.ComboBox;
import com.vaadin.flow.component.grid.GridSortOrder;
import com.vaadin.flow.component.html.Main; import com.vaadin.flow.component.html.Main;
import com.vaadin.flow.component.html.Span; import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.component.notification.Notification; import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout; import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.data.provider.SortDirection;
import com.vaadin.flow.router.PageTitle; import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route; import com.vaadin.flow.router.Route;
import com.vaadin.flow.spring.annotation.SpringComponent; import com.vaadin.flow.spring.annotation.SpringComponent;
@ -19,8 +17,10 @@ import jakarta.annotation.security.PermitAll;
import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.Scope;
import org.vaadin.firitin.components.grid.PagingGrid; import org.vaadin.firitin.components.grid.PagingGrid;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors;
@SpringComponent @SpringComponent
@Scope("prototype") @Scope("prototype")
@ -32,7 +32,8 @@ public class RequestsListView extends Main {
private final TimeOffRequestService requestService; private final TimeOffRequestService requestService;
private final EmployeeService employeeService; private final EmployeeService employeeService;
private final TeamService teamService; private final TeamService teamService;
private final PagingGrid<TimeOffRequest> requestGrid; private final PagingGrid<TimeOffRequest> requestGrid = new PagingGrid<>(TimeOffRequest.class);
private List<TimeOffRequest> requests = Collections.emptyList();
private ComboBox<Employee> employeeFilter; private ComboBox<Employee> employeeFilter;
private ComboBox<Team> teamFilter; private ComboBox<Team> teamFilter;
private ComboBox<TimeOffRequestType> categoryFilter; private ComboBox<TimeOffRequestType> categoryFilter;
@ -45,7 +46,7 @@ public class RequestsListView extends Main {
this.requestService = requestService; this.requestService = requestService;
this.employeeService = employeeService; this.employeeService = employeeService;
this.teamService = teamService; this.teamService = teamService;
this.requestGrid = new PagingGrid<>(TimeOffRequest.class); this.requests = requestService.findAllTimeOffRequests();
initializeView(); initializeView();
refreshRequestGrid(null, null, null, null); refreshRequestGrid(null, null, null, null);
} }
@ -105,11 +106,14 @@ public class RequestsListView extends Main {
final Team team, final Team team,
final TimeOffRequestType category, final TimeOffRequestType category,
final TimeOffRequest.Status state) { final TimeOffRequest.Status state) {
requestGrid.setPagingDataProvider((page, pageSize) -> { requestGrid.setPagingDataProvider((page, pageSize) -> {
requests = requestService.findAllTimeOffRequests();
int start = (int) (page * requestGrid.getPageSize());
if (allFiltersAreNull(employee, team, category, state)) { if (allFiltersAreNull(employee, team, category, state)) {
return fetchTimeOffRequests((int) page, pageSize); return fetchTimeOffRequests(start, pageSize);
} else { } else {
return fetchFilteredTimeOffRequests((int) page, pageSize, employee, team, category, state); return fetchFilteredTimeOffRequests(start, pageSize, employee, team, category, state);
} }
}); });
requestGrid.getDataProvider().refreshAll(); requestGrid.getDataProvider().refreshAll();
@ -122,34 +126,48 @@ public class RequestsListView extends Main {
return employee == null && team == null && category == null && state == null; return employee == null && team == null && category == null && state == null;
} }
private List<TimeOffRequest> fetchTimeOffRequests(final int page, final int pageSize) { private List<TimeOffRequest> fetchTimeOffRequests(final int start, final int pageSize) {
int start = page * pageSize; int end = start + pageSize;
if (requestGrid.getSortOrder().isEmpty()) { if (end > requests.size()) {
return requestService.findTimeOffRequests(start, pageSize); end = requests.size();
} else {
return fetchSortedTimeOffRequests(start, pageSize);
} }
return requests.subList(start, end);
} }
private List<TimeOffRequest> fetchSortedTimeOffRequests(final int start, final int pageSize) { private List<TimeOffRequest> fetchFilteredTimeOffRequests(final int start,
GridSortOrder<TimeOffRequest> sortOrder = requestGrid.getSortOrder().getFirst();
return requestService.findTimeOffRequests(
start,
pageSize,
sortOrder.getSorted().getKey(),
sortOrder.getDirection() == SortDirection.ASCENDING
);
}
private List<TimeOffRequest> fetchFilteredTimeOffRequests(final int page,
final int pageSize, final int pageSize,
final Employee employee, final Employee employee,
final Team team, final Team team,
final TimeOffRequestType category, final TimeOffRequestType category,
final TimeOffRequest.Status state) { final TimeOffRequest.Status state) {
return requestService.findTimeOffRequestBy(employee, team, category, state, page, pageSize); if (employee != null && !"ALL".equals(employee.getFirstName())) {
requests = requests.stream()
.filter(request -> request.getEmployee().getId().equals(employee.getId()))
.collect(Collectors.toList());
}
if (team != null && !"ALL".equals(team.getName())) {
requests = requests.stream()
.filter(request -> request.getTeam().getId().equals(team.getId()))
.collect(Collectors.toList());
}
if (category != null && !"ALL".equals(category.name())) {
requests = requests.stream()
.filter(request -> request.getCategory().equals(category))
.collect(Collectors.toList());
}
if (state != null && !"ALL".equals(state.name())) {
requests = requests.stream()
.filter(request -> request.getState().equals(state))
.collect(Collectors.toList());
}
int end = start + pageSize;
if (end > requests.size()) {
end = requests.size();
}
return requests.subList(start, end);
} }
private Span createEmployeeSpan(final TimeOffRequest timeOffRequest) { private Span createEmployeeSpan(final TimeOffRequest timeOffRequest) {
Employee employee = timeOffRequest.getEmployee(); Employee employee = timeOffRequest.getEmployee();
return new Span(employee.getFirstName() + " " + employee.getLastName()); return new Span(employee.getFirstName() + " " + employee.getLastName());
@ -249,7 +267,7 @@ public class RequestsListView extends Main {
getUI().ifPresent(ui -> ui.navigate(MainView.class)); getUI().ifPresent(ui -> ui.navigate(MainView.class));
} }
private void navigateToTimeOffRequestView(final UUID id_employee) { private void navigateToTimeOffRequestView(final UUID idEmployee) {
getUI().ifPresent(ui -> ui.navigate("requests/" + id_employee.toString())); getUI().ifPresent(ui -> ui.navigate("requests/" + idEmployee.toString()));
} }
} }