diff --git a/src/main/java/com/primefactorsolutions/repositories/TimeOffRequestRepository.java b/src/main/java/com/primefactorsolutions/repositories/TimeOffRequestRepository.java index f9b426b..1ea24a6 100644 --- a/src/main/java/com/primefactorsolutions/repositories/TimeOffRequestRepository.java +++ b/src/main/java/com/primefactorsolutions/repositories/TimeOffRequestRepository.java @@ -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 { - List findByEmployee_Id(UUID id_employee); + List findByEmployeeId(UUID idEmployee); + List findByTeamId(UUID idTeam); + List findByCategory(TimeOffRequestType category); + List findByState(TimeOffRequest.Status state); } diff --git a/src/main/java/com/primefactorsolutions/service/TimeOffRequestService.java b/src/main/java/com/primefactorsolutions/service/TimeOffRequestService.java index b447657..6e19ec1 100644 --- a/src/main/java/com/primefactorsolutions/service/TimeOffRequestService.java +++ b/src/main/java/com/primefactorsolutions/service/TimeOffRequestService.java @@ -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 getAllTimeOffRequests() { + public List findAllTimeOffRequests() { return timeOffRequestRepository.findAll(); } - public TimeOffRequest getTimeOffRequest(final UUID id) { + public TimeOffRequest findTimeOffRequest(final UUID id) { Optional timeOffRequest = timeOffRequestRepository.findById(id); return timeOffRequest.orElse(null); } - public List findTimeOffRequests( - final int start, final int pageSize, final String sortProperty, final boolean asc) { - List 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 findRequestsByEmployeeId(final UUID idEmployee) { + return timeOffRequestRepository.findByEmployeeId(idEmployee); } - public List findTimeOffRequests(final int start, final int pageSize) { - List timeOffRequests = timeOffRequestRepository.findAll(); - - int end = Math.min(start + pageSize, timeOffRequests.size()); - return timeOffRequests.subList(start, end); + public List findRequestsByTeamId(final UUID idTeam) { + return timeOffRequestRepository.findByTeamId(idTeam); } - public List findTimeOffRequestBy(final Employee employee, - final Team team, - final TimeOffRequestType category, - final TimeOffRequest.Status state, - final int start, - final int pageSize) { - List 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 findRequestsByCategory(final TimeOffRequestType category) { + return timeOffRequestRepository.findByCategory(category); } - public List findRequestsByEmployeeId(UUID id_employee) { - return timeOffRequestRepository.findByEmployee_Id(id_employee); + public List findRequestsByState(final TimeOffRequest.Status state) { + return timeOffRequestRepository.findByState(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 98adf45..25c3c0b 100644 --- a/src/main/java/com/primefactorsolutions/views/RequestView.java +++ b/src/main/java/com/primefactorsolutions/views/RequestView.java @@ -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 { private final TimeOffRequestService requestService; private final EmployeeService employeeService; - private final PagingGrid requestGrid = new PagingGrid<>(TimeOffRequest.class); - private List requests; + private final Grid requestGrid = new Grid<>(TimeOffRequest.class); + private List requests = Collections.emptyList(); private ComboBox categoryFilter; private ComboBox 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 { "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 { 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 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 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 fetchSortedTimeOffRequests(int start, int pageSize) { - GridSortOrder 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 fetchFilteredTimeOffRequests(int page, int pageSize, TimeOffRequestType category, TimeOffRequest.Status state) { + private List 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 { .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 { - 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); - requestGrid.setItems(requests); - } catch (IllegalArgumentException e) { - requestGrid.setItems(); - } + public void setParameter(final BeforeEvent event, final String parameter) { + employeeId = UUID.fromString(parameter); + Employee employee = employeeService.getEmployee(employeeId); + requests = requestService.findRequestsByEmployeeId(employeeId); + setViewTitle(employee.getFirstName() + " " + employee.getLastName(), employee.getTeam()); + requestGrid.setItems(requests); } - 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)); } } diff --git a/src/main/java/com/primefactorsolutions/views/RequestsListView.java b/src/main/java/com/primefactorsolutions/views/RequestsListView.java index 8243d72..b79b0a4 100644 --- a/src/main/java/com/primefactorsolutions/views/RequestsListView.java +++ b/src/main/java/com/primefactorsolutions/views/RequestsListView.java @@ -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 requestGrid; + private final PagingGrid requestGrid = new PagingGrid<>(TimeOffRequest.class); + private List requests = Collections.emptyList(); private ComboBox employeeFilter; private ComboBox teamFilter; private ComboBox 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,34 +126,48 @@ public class RequestsListView extends Main { return employee == null && team == null && category == null && state == null; } - private List 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 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 fetchSortedTimeOffRequests(final int start, final int pageSize) { - GridSortOrder sortOrder = requestGrid.getSortOrder().getFirst(); - return requestService.findTimeOffRequests( - start, - pageSize, - sortOrder.getSorted().getKey(), - sortOrder.getDirection() == SortDirection.ASCENDING - ); - } - - private List fetchFilteredTimeOffRequests(final int page, + private List 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(); return new Span(employee.getFirstName() + " " + employee.getLastName()); @@ -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())); } } \ No newline at end of file