Vacaciones del Empleado - Refactorizar
This commit is contained in:
parent
62d678c407
commit
602ab686f5
@ -1,11 +1,15 @@
|
||||
package com.primefactorsolutions.repositories;
|
||||
|
||||
import com.primefactorsolutions.model.TimeOffRequest;
|
||||
import com.primefactorsolutions.model.TimeOffRequestType;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.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);
|
||||
}
|
||||
|
@ -3,14 +3,11 @@ package com.primefactorsolutions.service;
|
||||
import com.primefactorsolutions.model.*;
|
||||
import com.primefactorsolutions.repositories.TimeOffRequestRepository;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.apache.commons.beanutils.BeanComparator;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
@ -25,68 +22,28 @@ public class TimeOffRequestService {
|
||||
timeOffRequestRepository.deleteById(id);
|
||||
}
|
||||
|
||||
public List<TimeOffRequest> getAllTimeOffRequests() {
|
||||
public List<TimeOffRequest> findAllTimeOffRequests() {
|
||||
return timeOffRequestRepository.findAll();
|
||||
}
|
||||
|
||||
public TimeOffRequest getTimeOffRequest(final UUID id) {
|
||||
public TimeOffRequest findTimeOffRequest(final UUID id) {
|
||||
Optional<TimeOffRequest> timeOffRequest = timeOffRequestRepository.findById(id);
|
||||
return timeOffRequest.orElse(null);
|
||||
}
|
||||
|
||||
public List<TimeOffRequest> findTimeOffRequests(
|
||||
final int start, final int pageSize, final String sortProperty, final boolean asc) {
|
||||
List<TimeOffRequest> timeOffRequests = timeOffRequestRepository.findAll();
|
||||
|
||||
int end = Math.min(start + pageSize, timeOffRequests.size());
|
||||
timeOffRequests.sort(new BeanComparator<>(sortProperty));
|
||||
|
||||
if (!asc) {
|
||||
Collections.reverse(timeOffRequests);
|
||||
public List<TimeOffRequest> findRequestsByEmployeeId(final UUID idEmployee) {
|
||||
return timeOffRequestRepository.findByEmployeeId(idEmployee);
|
||||
}
|
||||
|
||||
return timeOffRequests.subList(start, end);
|
||||
public List<TimeOffRequest> findRequestsByTeamId(final UUID idTeam) {
|
||||
return timeOffRequestRepository.findByTeamId(idTeam);
|
||||
}
|
||||
|
||||
public List<TimeOffRequest> findTimeOffRequests(final int start, final int pageSize) {
|
||||
List<TimeOffRequest> timeOffRequests = timeOffRequestRepository.findAll();
|
||||
|
||||
int end = Math.min(start + pageSize, timeOffRequests.size());
|
||||
return timeOffRequests.subList(start, end);
|
||||
public List<TimeOffRequest> findRequestsByCategory(final TimeOffRequestType category) {
|
||||
return timeOffRequestRepository.findByCategory(category);
|
||||
}
|
||||
|
||||
public List<TimeOffRequest> findTimeOffRequestBy(final Employee employee,
|
||||
final Team team,
|
||||
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) {
|
||||
return timeOffRequestRepository.findByEmployee_Id(id_employee);
|
||||
public List<TimeOffRequest> findRequestsByState(final TimeOffRequest.Status state) {
|
||||
return timeOffRequestRepository.findByState(state);
|
||||
}
|
||||
}
|
@ -7,22 +7,19 @@ import com.primefactorsolutions.service.EmployeeService;
|
||||
import com.primefactorsolutions.service.TimeOffRequestService;
|
||||
import com.vaadin.flow.component.button.Button;
|
||||
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.H3;
|
||||
import com.vaadin.flow.component.html.Span;
|
||||
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
|
||||
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.HasUrlParameter;
|
||||
import com.vaadin.flow.router.PageTitle;
|
||||
import com.vaadin.flow.router.Route;
|
||||
import com.vaadin.flow.spring.annotation.SpringComponent;
|
||||
import jakarta.annotation.security.PermitAll;
|
||||
import org.apache.commons.beanutils.BeanComparator;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.vaadin.firitin.components.grid.PagingGrid;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
@ -38,21 +35,22 @@ public class RequestView extends Div implements HasUrlParameter<String> {
|
||||
|
||||
private final TimeOffRequestService requestService;
|
||||
private final EmployeeService employeeService;
|
||||
private final PagingGrid<TimeOffRequest> requestGrid = new PagingGrid<>(TimeOffRequest.class);
|
||||
private List<TimeOffRequest> requests;
|
||||
private final Grid<TimeOffRequest> requestGrid = new Grid<>(TimeOffRequest.class);
|
||||
private List<TimeOffRequest> requests = Collections.emptyList();
|
||||
private ComboBox<TimeOffRequestType> categoryFilter;
|
||||
private ComboBox<TimeOffRequest.Status> stateFilter;
|
||||
private UUID employeeId;
|
||||
|
||||
public RequestView(TimeOffRequestService requestService, EmployeeService employeeService) {
|
||||
public RequestView(final TimeOffRequestService requestService, final EmployeeService employeeService) {
|
||||
this.requestService = requestService;
|
||||
this.employeeService = employeeService;
|
||||
this.requests = Collections.emptyList();
|
||||
initializeView();
|
||||
}
|
||||
|
||||
private void initializeView() {
|
||||
setupFilters();
|
||||
setupGrid();
|
||||
add(requestGrid);
|
||||
add(createSummaryLayout());
|
||||
add(createActionButtons());
|
||||
add(requestGrid, createSummaryLayout(), createActionButtons());
|
||||
refreshRequestGrid(null, null);
|
||||
}
|
||||
|
||||
@ -89,16 +87,18 @@ public class RequestView extends Div implements HasUrlParameter<String> {
|
||||
"daysToBeTake",
|
||||
"daysBalance"
|
||||
);
|
||||
requestGrid.setAllRowsVisible(true);
|
||||
}
|
||||
|
||||
private VerticalLayout createSummaryLayout() {
|
||||
int totalVacations = 15;
|
||||
int totalTimeOff = 2;
|
||||
int totalAvailableDays = totalVacations + totalTimeOff;
|
||||
Span holidaysLabel = new Span("TOTAL HOLIDAYS: " + totalVacations);
|
||||
Span timeOffLabel = new Span("TOTAL TIME OFF: " + totalTimeOff);
|
||||
Span availableDaysLabel = new Span("TOTAL AVAILABLE DAYS: " + totalAvailableDays);
|
||||
return new VerticalLayout(holidaysLabel, timeOffLabel, availableDaysLabel);
|
||||
return new VerticalLayout(
|
||||
new Span("TOTAL HOLIDAYS: " + totalVacations),
|
||||
new Span("TOTAL TIME OFF: " + totalTimeOff),
|
||||
new Span("TOTAL AVAILABLE DAYS: " + totalAvailableDays)
|
||||
);
|
||||
}
|
||||
|
||||
private HorizontalLayout createActionButtons() {
|
||||
@ -111,47 +111,21 @@ public class RequestView extends Div implements HasUrlParameter<String> {
|
||||
|
||||
private void navigateToRequestsListView() {
|
||||
getUI().ifPresent(ui -> ui.navigate(RequestsListView.class));
|
||||
|
||||
}
|
||||
|
||||
private void refreshRequestGrid(final TimeOffRequestType category,
|
||||
final TimeOffRequest.Status state) {
|
||||
requestGrid.setPagingDataProvider((page, pageSize) -> {
|
||||
if (allFiltersAreNull(category, state)) {
|
||||
return fetchTimeOffRequests((int) page, pageSize);
|
||||
} else {
|
||||
return fetchFilteredTimeOffRequests((int) page, pageSize,category, state);
|
||||
}
|
||||
});
|
||||
requestGrid.getDataProvider().refreshAll();
|
||||
private void refreshRequestGrid(final TimeOffRequestType category, final TimeOffRequest.Status state) {
|
||||
List<TimeOffRequest> filteredRequests = allFiltersAreNull(category, state)
|
||||
? requestService.findRequestsByEmployeeId(employeeId)
|
||||
: fetchFilteredTimeOffRequests(category, state);
|
||||
requestGrid.setItems(filteredRequests);
|
||||
}
|
||||
|
||||
private boolean allFiltersAreNull(TimeOffRequestType category, TimeOffRequest.Status state) {
|
||||
private boolean allFiltersAreNull(final TimeOffRequestType category, final TimeOffRequest.Status state) {
|
||||
return category == null && state == null;
|
||||
}
|
||||
|
||||
private List<TimeOffRequest> fetchTimeOffRequests(int page, int pageSize) {
|
||||
int start = page * pageSize;
|
||||
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) {
|
||||
private List<TimeOffRequest> fetchFilteredTimeOffRequests(final TimeOffRequestType category,
|
||||
final TimeOffRequest.Status state) {
|
||||
requests = requestService.findRequestsByEmployeeId(employeeId);
|
||||
if (category != null && !"ALL".equals(category.name())) {
|
||||
requests = requests.stream()
|
||||
@ -163,31 +137,20 @@ public class RequestView extends Div implements HasUrlParameter<String> {
|
||||
.filter(req -> req.getState().equals(state))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
int end = Math.min(page + pageSize, requests.size());
|
||||
return requests.subList(page, end);
|
||||
return requests;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setParameter(BeforeEvent event, String parameter) {
|
||||
try {
|
||||
public void setParameter(final BeforeEvent event, final String parameter) {
|
||||
employeeId = UUID.fromString(parameter);
|
||||
Employee employee = employeeService.getEmployee(employeeId);
|
||||
|
||||
String employeeName = employee.getFirstName() + " " + employee.getLastName();
|
||||
String employeeTeam = employee.getTeam();
|
||||
setViewTitle(employeeName, employeeTeam);
|
||||
|
||||
requests = requestService.findRequestsByEmployeeId(employeeId);
|
||||
setViewTitle(employee.getFirstName() + " " + employee.getLastName(), employee.getTeam());
|
||||
requestGrid.setItems(requests);
|
||||
} catch (IllegalArgumentException e) {
|
||||
requestGrid.setItems();
|
||||
}
|
||||
}
|
||||
|
||||
private void setViewTitle(String employeeName, String employeeTeam) {
|
||||
H3 employeeTitle = new H3("Name: " + employeeName);
|
||||
H3 teamTitle = new H3("Team: " + employeeTeam);
|
||||
addComponentAsFirst(employeeTitle);
|
||||
addComponentAtIndex(1, teamTitle);
|
||||
private void setViewTitle(final String employeeName, final String employeeTeam) {
|
||||
addComponentAsFirst(new H3("Name: " + employeeName));
|
||||
addComponentAtIndex(1, new H3("Team: " + employeeTeam));
|
||||
}
|
||||
}
|
||||
|
@ -6,12 +6,10 @@ import com.primefactorsolutions.service.TeamService;
|
||||
import com.primefactorsolutions.service.TimeOffRequestService;
|
||||
import com.vaadin.flow.component.button.Button;
|
||||
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.Span;
|
||||
import com.vaadin.flow.component.notification.Notification;
|
||||
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.Route;
|
||||
import com.vaadin.flow.spring.annotation.SpringComponent;
|
||||
@ -19,8 +17,10 @@ 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.stream.Collectors;
|
||||
|
||||
@SpringComponent
|
||||
@Scope("prototype")
|
||||
@ -32,7 +32,8 @@ public class RequestsListView extends Main {
|
||||
private final TimeOffRequestService requestService;
|
||||
private final EmployeeService employeeService;
|
||||
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<Team> teamFilter;
|
||||
private ComboBox<TimeOffRequestType> categoryFilter;
|
||||
@ -45,7 +46,7 @@ public class RequestsListView extends Main {
|
||||
this.requestService = requestService;
|
||||
this.employeeService = employeeService;
|
||||
this.teamService = teamService;
|
||||
this.requestGrid = new PagingGrid<>(TimeOffRequest.class);
|
||||
this.requests = requestService.findAllTimeOffRequests();
|
||||
initializeView();
|
||||
refreshRequestGrid(null, null, null, null);
|
||||
}
|
||||
@ -105,11 +106,14 @@ public class RequestsListView extends Main {
|
||||
final Team team,
|
||||
final TimeOffRequestType category,
|
||||
final TimeOffRequest.Status state) {
|
||||
|
||||
requestGrid.setPagingDataProvider((page, pageSize) -> {
|
||||
requests = requestService.findAllTimeOffRequests();
|
||||
int start = (int) (page * requestGrid.getPageSize());
|
||||
if (allFiltersAreNull(employee, team, category, state)) {
|
||||
return fetchTimeOffRequests((int) page, pageSize);
|
||||
return fetchTimeOffRequests(start, pageSize);
|
||||
} else {
|
||||
return fetchFilteredTimeOffRequests((int) page, pageSize, employee, team, category, state);
|
||||
return fetchFilteredTimeOffRequests(start, pageSize, employee, team, category, state);
|
||||
}
|
||||
});
|
||||
requestGrid.getDataProvider().refreshAll();
|
||||
@ -122,33 +126,47 @@ public class RequestsListView extends Main {
|
||||
return employee == null && team == null && category == null && state == null;
|
||||
}
|
||||
|
||||
private List<TimeOffRequest> fetchTimeOffRequests(final int page, final int pageSize) {
|
||||
int start = page * pageSize;
|
||||
if (requestGrid.getSortOrder().isEmpty()) {
|
||||
return requestService.findTimeOffRequests(start, pageSize);
|
||||
} else {
|
||||
return fetchSortedTimeOffRequests(start, pageSize);
|
||||
private List<TimeOffRequest> 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<TimeOffRequest> fetchSortedTimeOffRequests(final int start, final int pageSize) {
|
||||
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,
|
||||
private List<TimeOffRequest> fetchFilteredTimeOffRequests(final int start,
|
||||
final int pageSize,
|
||||
final Employee employee,
|
||||
final Team team,
|
||||
final TimeOffRequestType category,
|
||||
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) {
|
||||
Employee employee = timeOffRequest.getEmployee();
|
||||
@ -249,7 +267,7 @@ public class RequestsListView extends Main {
|
||||
getUI().ifPresent(ui -> ui.navigate(MainView.class));
|
||||
}
|
||||
|
||||
private void navigateToTimeOffRequestView(final UUID id_employee) {
|
||||
getUI().ifPresent(ui -> ui.navigate("requests/" + id_employee.toString()));
|
||||
private void navigateToTimeOffRequestView(final UUID idEmployee) {
|
||||
getUI().ifPresent(ui -> ui.navigate("requests/" + idEmployee.toString()));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user