Compare commits

..

No commits in common. "aa041b33583d76c9854de16e0f451257e40699fe" and "0f63ee58a255850dce5c97b0e931954b91e4c282" have entirely different histories.

3 changed files with 93 additions and 113 deletions

View File

@ -1,12 +1,10 @@
package com.primefactorsolutions.service; package com.primefactorsolutions.service;
import com.primefactorsolutions.model.Candidate;
import com.primefactorsolutions.model.Employee; import com.primefactorsolutions.model.Employee;
import com.primefactorsolutions.repositories.EmployeeRepository; import com.primefactorsolutions.repositories.EmployeeRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import lombok.Data; import lombok.Data;
import org.springframework.data.domain.Sort; import org.apache.commons.lang3.NotImplementedException;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List; import java.util.List;
@ -33,14 +31,4 @@ public class EmployeeService {
public Employee getEmployee(final UUID id) { public Employee getEmployee(final UUID id) {
return null; return null;
} }
public Page<Employee> getEmployeesPaginated(int pageNo, int pageSize, String sortField, boolean ascending) {
Sort sort = ascending ? Sort.by(sortField).ascending() : Sort.by(sortField).descending();
Pageable pageable = PageRequest.of(pageNo - 1, pageSize, sort);
return employeeRepository.findAll(pageable);
}
public boolean hasNextPage(int currentPage, int pageSize, String sortField, boolean ascending) {
return getEmployeesPaginated(currentPage, pageSize, sortField, ascending).hasNext();
}
} }

View File

@ -2,18 +2,27 @@ package com.primefactorsolutions.views;
import com.primefactorsolutions.model.Employee; import com.primefactorsolutions.model.Employee;
import com.primefactorsolutions.service.EmployeeService; import com.primefactorsolutions.service.EmployeeService;
import com.vaadin.flow.component.ClickEvent;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.ComponentEventListener;
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.html.H2; import com.vaadin.flow.component.html.H2;
import com.vaadin.flow.component.html.Main; import com.vaadin.flow.component.html.Main;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout; import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.data.provider.DataProvider;
import com.vaadin.flow.data.provider.DataProviderListener;
import com.vaadin.flow.data.provider.Query;
import com.vaadin.flow.function.ValueProvider;
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.shared.Registration;
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.springframework.context.annotation.Scope; import org.springframework.context.annotation.Scope;
import org.vaadin.firitin.components.grid.VGrid; import org.vaadin.firitin.components.grid.VGrid;
import org.springframework.data.domain.Page;
import java.util.stream.Stream;
@SpringComponent @SpringComponent
@Scope("prototype") @Scope("prototype")
@ -21,103 +30,97 @@ import org.springframework.data.domain.Page;
@Route(value = "/employees", layout = MainLayout.class) @Route(value = "/employees", layout = MainLayout.class)
@PermitAll @PermitAll
public class EmployeesListView extends Main { public class EmployeesListView extends Main {
private final EmployeeService employeeService; private final EmployeeService employeeService;
private final int pageSize = 5;
private int currentPage = 1;
private String sortField = "firstName";
private boolean ascending = true;
private VGrid<Employee> grid;
private Button previous;
private Button next;
public EmployeesListView(EmployeeService employeeService) { public EmployeesListView(final EmployeeService employeeService) {
this.employeeService = employeeService; this.employeeService = employeeService;
addComponents();
updateGrid();
}
private void addComponents() {
addTitle();
addOrderButtons();
configureGrid();
addNavigationButtons();
}
private void addTitle() {
final H2 title = new H2("Employees list"); final H2 title = new H2("Employees list");
add(title);
}
private void addOrderButtons() {
final HorizontalLayout hl = new HorizontalLayout(); final HorizontalLayout hl = new HorizontalLayout();
hl.add(createOrderButton("Employee List in Ascending Order", true)); final HorizontalLayout hf = new HorizontalLayout();
hl.add(createOrderButton("Employee List in Descending Order", false)); final Button employeeListAscendingOrder = new Button("Employee List in Ascending Order");
add(hl); employeeListAscendingOrder.addClickListener((ComponentEventListener<ClickEvent<Button>>) buttonClickEvent -> {
} // no-op
private Button createOrderButton(String label, boolean isAscending) {
Button button = new Button(label);
button.addClickListener(event -> {
sortField = "firstName";
ascending = isAscending;
updateGrid();
}); });
return button; hl.add(employeeListAscendingOrder);
} final Button employeeListDescendingOrder = new Button("Employee List in Descending Order");
private void configureGrid() { employeeListDescendingOrder.addClickListener((ComponentEventListener<ClickEvent<Button>>) buttonClickEvent -> {
grid = new VGrid<>(Employee.class); // no-op
});
hl.add(employeeListDescendingOrder);
final VGrid<Employee> grid = new VGrid<>(Employee.class);
grid.setColumns("firstName", "lastName", "status"); grid.setColumns("firstName", "lastName", "status");
grid.setAllRowsVisible(true); grid.setAllRowsVisible(true);
grid.addComponentColumn(employee -> createStatusComboBox()).setHeader("Change Status"); grid.addComponentColumn((ValueProvider<Employee, Component>) employee -> {
grid.addComponentColumn(employee -> createEditButton()); ComboBox<String> statusComboBox = new ComboBox<>();
grid.addComponentColumn(employee -> createSaveButton()); statusComboBox.setItems("Active", "Inactive");
add(grid); return statusComboBox;
} }).setHeader("Change Status");
grid.addComponentColumn((ValueProvider<Employee, Component>) employee -> {
final Button edit = new Button("Edit");
edit.addClickListener((ComponentEventListener<ClickEvent<Button>>) buttonClickEvent -> {
// no-op
});
return edit;
});
grid.addComponentColumn((ValueProvider<Employee, Component>) employee -> {
final Button save = new Button("Save");
save.addClickListener((ComponentEventListener<ClickEvent<Button>>) buttonClickEvent -> {
// no-op
});
return save;
});
private ComboBox<String> createStatusComboBox() { grid.setDataProvider(new DataProvider<>() {
ComboBox<String> statusComboBox = new ComboBox<>(); @Override
statusComboBox.setItems("Active", "Inactive"); public boolean isInMemory() {
return statusComboBox; return false;
} }
private Button createEditButton() { @Override
return new Button("Edit"); public int size(final Query<Employee, Object> query) {
} return employeeService.getEmployees().size();
private Button createSaveButton() { }
return new Button("Save");
}
private void addNavigationButtons() { @Override
final HorizontalLayout hf = new HorizontalLayout(); public Stream<Employee> fetch(final Query<Employee, Object> query) {
previous = createNavigationButton("Previous", -1); int limit = query.getLimit();
next = createNavigationButton("Next", 1); int pagerSize = query.getPageSize();
hf.add(previous, next, createAddEmployeeButton()); int page = query.getPage();
add(hf); return employeeService.getEmployees().stream();
} }
private Button createNavigationButton(String label, int increment) { @Override
Button button = new Button(label); public void refreshItem(final Employee employee) {
button.addClickListener(event -> changePage(increment)); // no-op
return button; }
}
private void changePage(int increment) { @Override
if (increment < 0 && currentPage > 1) { public void refreshAll() {
currentPage--; // no-op
} else if (increment > 0 && employeeService.hasNextPage(currentPage, pageSize, sortField, ascending)) { }
currentPage++;
} @Override
updateGrid(); public Registration addDataProviderListener(final DataProviderListener<Employee> dataProviderListener) {
} return null;
private Button createAddEmployeeButton() { }
return new Button("Add Employee", event -> });
getUI().flatMap(ui -> ui.navigate(EmployeeView.class, "new"))
); final Button previous = new Button("Previous");
} previous.addClickListener((ComponentEventListener<ClickEvent<Button>>) buttonClickEvent -> {
private void updateGrid() { // no-op
Page<Employee> page = employeeService.getEmployeesPaginated(currentPage, pageSize, sortField, ascending); });
grid.setItems(page.getContent()); hf.add(previous);
previous.setEnabled(currentPage > 1); final Button next = new Button("Next");
next.setEnabled(page.hasNext()); next.addClickListener((ComponentEventListener<ClickEvent<Button>>) buttonClickEvent -> {
// no-op
});
hf.add(next);
final Button addEmployee = new Button("Add Employee");
addEmployee.addClickListener((ComponentEventListener<ClickEvent<Button>>) buttonClickEvent -> {
this.getUI().get().navigate(EmployeeView.class, "new");
});
hf.add(addEmployee);
add(title, hl, grid, hf);
} }
} }

View File

@ -13,15 +13,4 @@ insert into employee (id, version, first_name, last_name, status) values ('e99b7
insert into employee (id, version, first_name, last_name, status) values ('f6ab3c6d-7078-45f6-9b22-4e37637bfec6', 1, 'Ana', 'Garcia Rojas', 'ACTIVE'); insert into employee (id, version, first_name, last_name, status) values ('f6ab3c6d-7078-45f6-9b22-4e37637bfec6', 1, 'Ana', 'Garcia Rojas', 'ACTIVE');
insert into employee (id, version, first_name, last_name, status) values ('2e2293b1-3f9a-4f3d-abc8-32639b0a5e15', 1, 'Carlos', 'Lopez Mendoza', 'INACTIVE'); insert into employee (id, version, first_name, last_name, status) values ('2e2293b1-3f9a-4f3d-abc8-32639b0a5e15', 1, 'Carlos', 'Lopez Mendoza', 'INACTIVE');
insert into employee (id, version, first_name, last_name, status) values ('4b1c6c35-4627-4b35-b6e9-dc75c68b2c31', 1, 'Maria', 'Fernandez Villca', 'ACTIVE'); insert into employee (id, version, first_name, last_name, status) values ('4b1c6c35-4627-4b35-b6e9-dc75c68b2c31', 1, 'Maria', 'Fernandez Villca', 'ACTIVE');
insert into employee (id, version, first_name, last_name, status) values ('afc5c741-f70a-4394-853b-39d51b118927', 1, 'Luis', 'Gutierrez Mamani', 'ACTIVE'); insert into employee (id, version, first_name, last_name, status) values ('afc5c741-f70a-4394-853b-39d51b118927', 1, 'Luis', 'Gutierrez Mamani', 'ACTIVE');
insert into employee (id, version, first_name, last_name, status) values ('b2436b82-7b9f-4f0d-9463-f2c3173a45c3', 1, 'Laura', 'Martinez Paredes', 'INACTIVE');
insert into employee (id, version, first_name, last_name, status) values ('6e6a8a4e-9f6b-44eb-8c69-40acfdc86756', 1, 'Roberto', 'Santos Escobar', 'ACTIVE');
insert into employee (id, version, first_name, last_name, status) values ('36b0d1c6-bdc0-4d98-94bb-08b9bce3f0d5', 1, 'Valeria', 'Morales Ochoa', 'INACTIVE');
insert into employee (id, version, first_name, last_name, status) values ('5a1c6d80-58b3-43e3-a5a5-24b4a2d1d54a', 1, 'Jorge', 'Ramirez Tapia', 'ACTIVE');
insert into employee (id, version, first_name, last_name, status) values ('9d6a5b2e-6d0b-4b89-8d6a-d3f3d1bfc047', 1, 'Sandra', 'Torres Huanca', 'ACTIVE');
insert into employee (id, version, first_name, last_name, status) values ('f8b3e0c0-0d5a-4e5c-bf9d-207b9b5e8279', 1, 'Felipe', 'Quispe Huanca', 'INACTIVE');
insert into employee (id, version, first_name, last_name, status) values ('cd80e1d0-9a08-44a6-bd63-2c63eaa003d4', 1, 'Gabriela', 'Rivas Arana', 'ACTIVE');
insert into employee (id, version, first_name, last_name, status) values ('62d3c1b7-815e-4e96-8d7e-f8c4236bca55', 1, 'Oscar', 'Flores Quiroga', 'INACTIVE');
insert into employee (id, version, first_name, last_name, status) values ('f20b7c5a-5a67-44f0-9ec1-4c1b8e80de05', 1, 'Marta', 'Vargas Soria', 'ACTIVE');
insert into employee (id, version, first_name, last_name, status) values ('19b5a76e-d7b1-4b76-8b02-4d0748e85809', 1, 'Andres', 'Espinoza Chura', 'INACTIVE');
insert into employee (id, version, first_name, last_name, status) values ('5c1a7b82-832d-4f24-8377-54b77b91b6a8', 1, 'Carla', 'Villanueva Arce', 'ACTIVE');