Compare commits
No commits in common. "5c4d2cdc74926d2c661fb7806175cc0d3cacf296" and "59c7b2af859e16de7e84eef7cb8325d7b0d5704d" have entirely different histories.
5c4d2cdc74
...
59c7b2af85
5
pom.xml
5
pom.xml
@ -110,11 +110,6 @@
|
|||||||
<artifactId>spring-boot-starter-test</artifactId>
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>commons-beanutils</groupId>
|
|
||||||
<artifactId>commons-beanutils</artifactId>
|
|
||||||
<version>1.9.4</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.mockito</groupId>
|
<groupId>org.mockito</groupId>
|
||||||
<artifactId>mockito-core</artifactId>
|
<artifactId>mockito-core</artifactId>
|
||||||
|
@ -1,52 +1,51 @@
|
|||||||
package com.primefactorsolutions.model;
|
package com.primefactorsolutions.model;
|
||||||
import jakarta.persistence.*;
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import lombok.Data;
|
|
||||||
import lombok.EqualsAndHashCode;
|
|
||||||
import lombok.NoArgsConstructor;
|
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import io.hypersistence.utils.hibernate.type.json.JsonType;
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import org.hibernate.annotations.Type;
|
||||||
|
|
||||||
@Data
|
import java.time.LocalDate;
|
||||||
@Entity
|
import java.util.List;
|
||||||
@AllArgsConstructor
|
|
||||||
@NoArgsConstructor
|
@Data
|
||||||
@EqualsAndHashCode(callSuper = true)
|
@Entity
|
||||||
public class Employee extends BaseEntity {
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
public class Employee extends BaseEntity {
|
||||||
private String firstName;
|
private String firstName;
|
||||||
private String lastName;
|
private String lastName;
|
||||||
private LocalDate birthday;
|
private LocalDate birthday;
|
||||||
private String birthCity;
|
private String birthCity;
|
||||||
|
|
||||||
@Enumerated(EnumType.STRING)
|
@Enumerated(EnumType.STRING)
|
||||||
private MaritalStatus maritalStatus;
|
private MaritalStatus maritalStatus;
|
||||||
|
|
||||||
private String residenceAddress;
|
private String residenceAddress;
|
||||||
private String phoneNumber;
|
private String phoneNumber;
|
||||||
private String personalEmail;
|
private String personalEmail;
|
||||||
private String position;
|
|
||||||
private String team;
|
|
||||||
private String emergencyCName;
|
private String emergencyCName;
|
||||||
private String emergencyCAddress;
|
private String emergencyCAddress;
|
||||||
private String emergencyCPhone;
|
private String emergencyCPhone;
|
||||||
private String emergencyCEmail;
|
private String emergencyCEmail;
|
||||||
@Lob
|
|
||||||
private byte[] profilePicture;
|
|
||||||
|
|
||||||
@Enumerated(EnumType.STRING)
|
@Enumerated(EnumType.STRING)
|
||||||
private Status status;
|
private Status status;
|
||||||
|
|
||||||
public enum Status {
|
public enum Status {
|
||||||
ACTIVE,
|
ACTIVE,
|
||||||
INACTIVE
|
INACTIVE
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum MaritalStatus {
|
public enum MaritalStatus {
|
||||||
SINGLE,
|
SINGLE,
|
||||||
MARRIED,
|
MARRIED,
|
||||||
WIDOWED,
|
WIDOWED,
|
||||||
DIVORCED
|
DIVORCED
|
||||||
}
|
}
|
||||||
public Status getStatus() {
|
}
|
||||||
return status;
|
|
||||||
}
|
|
||||||
public void setStatus(final Status status) {
|
|
||||||
this.status = status;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -1,14 +1,17 @@
|
|||||||
package com.primefactorsolutions.service;
|
package com.primefactorsolutions.service;
|
||||||
|
|
||||||
import com.primefactorsolutions.model.Employee;
|
import com.primefactorsolutions.model.Employee;
|
||||||
import org.apache.commons.beanutils.BeanComparator;
|
|
||||||
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.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
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.Collections;
|
|
||||||
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@ -16,37 +19,31 @@ import java.util.Collections;
|
|||||||
public class EmployeeService {
|
public class EmployeeService {
|
||||||
private final EmployeeRepository employeeRepository;
|
private final EmployeeRepository employeeRepository;
|
||||||
|
|
||||||
public EmployeeService(final EmployeeRepository employeeRepository) {
|
public EmployeeService(EmployeeRepository employeeRepository) {
|
||||||
this.employeeRepository = employeeRepository;
|
this.employeeRepository = employeeRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Employee> findEmployees(
|
|
||||||
final int start, final int pageSize, final String sortProperty, final boolean asc) {
|
|
||||||
List<Employee> employees = employeeRepository.findAll();
|
|
||||||
|
|
||||||
int end = Math.min(start + pageSize, employees.size());
|
|
||||||
employees.sort(new BeanComparator<>(sortProperty));
|
|
||||||
|
|
||||||
if (!asc) {
|
|
||||||
Collections.reverse(employees);
|
|
||||||
}
|
|
||||||
|
|
||||||
return employees.subList(start, end);
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Employee> findEmployees(final int start, final int pageSize) {
|
|
||||||
List<Employee> employees = employeeRepository.findAll();
|
|
||||||
|
|
||||||
int end = Math.min(start + pageSize, employees.size());
|
|
||||||
return employees.subList(start, end);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Employee createOrUpdate(final Employee employee) {
|
public Employee createOrUpdate(final Employee employee) {
|
||||||
return employeeRepository.save(employee);
|
final Employee saved = employeeRepository.save(employee);
|
||||||
|
return saved;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Employee> getEmployees() {
|
||||||
|
return employeeRepository.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Employee getEmployee(final UUID id) {
|
public Employee getEmployee(final UUID id) {
|
||||||
Optional<Employee> employee = employeeRepository.findById(id);
|
Optional<Employee> employee = employeeRepository.findById(id);
|
||||||
return employee.orElse(null);
|
return employee.orElse(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();
|
||||||
|
}
|
||||||
}
|
}
|
@ -7,14 +7,11 @@ 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.H3;
|
import com.vaadin.flow.component.html.H3;
|
||||||
import com.vaadin.flow.component.html.Image;
|
|
||||||
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.component.orderedlayout.VerticalLayout;
|
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
|
||||||
import com.vaadin.flow.component.textfield.EmailField;
|
import com.vaadin.flow.component.textfield.EmailField;
|
||||||
import com.vaadin.flow.component.textfield.TextField;
|
import com.vaadin.flow.component.textfield.TextField;
|
||||||
import com.vaadin.flow.component.upload.Upload;
|
|
||||||
import com.vaadin.flow.component.upload.receivers.MemoryBuffer;
|
|
||||||
import com.vaadin.flow.data.value.ValueChangeMode;
|
import com.vaadin.flow.data.value.ValueChangeMode;
|
||||||
import com.vaadin.flow.router.BeforeEvent;
|
import com.vaadin.flow.router.BeforeEvent;
|
||||||
import com.vaadin.flow.router.HasUrlParameter;
|
import com.vaadin.flow.router.HasUrlParameter;
|
||||||
@ -27,8 +24,6 @@ import org.springframework.context.annotation.Scope;
|
|||||||
import org.vaadin.firitin.components.datepicker.VDatePicker;
|
import org.vaadin.firitin.components.datepicker.VDatePicker;
|
||||||
import org.vaadin.firitin.form.BeanValidationForm;
|
import org.vaadin.firitin.form.BeanValidationForm;
|
||||||
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@ -50,8 +45,6 @@ public class EmployeeView extends BeanValidationForm<Employee> implements HasUrl
|
|||||||
private final TextField residenceAddress;
|
private final TextField residenceAddress;
|
||||||
private final TextField phoneNumber;
|
private final TextField phoneNumber;
|
||||||
private final EmailField personalEmail;
|
private final EmailField personalEmail;
|
||||||
private final TextField position;
|
|
||||||
private final TextField team;
|
|
||||||
|
|
||||||
private final TextField emergencyCName;
|
private final TextField emergencyCName;
|
||||||
private final TextField emergencyCAddress;
|
private final TextField emergencyCAddress;
|
||||||
@ -62,14 +55,6 @@ public class EmployeeView extends BeanValidationForm<Employee> implements HasUrl
|
|||||||
private final H3 fs;
|
private final H3 fs;
|
||||||
private final H3 ss;
|
private final H3 ss;
|
||||||
|
|
||||||
private final Upload imageUpload;
|
|
||||||
private final Image employeeImage;
|
|
||||||
private byte[] imageBytes;
|
|
||||||
|
|
||||||
private final Button saveButton;
|
|
||||||
private final Button editButton;
|
|
||||||
private final List<TextField> formFields = new ArrayList<>();
|
|
||||||
|
|
||||||
public EmployeeView(final EmployeeService employeeService) {
|
public EmployeeView(final EmployeeService employeeService) {
|
||||||
super(Employee.class);
|
super(Employee.class);
|
||||||
this.employeeService = employeeService;
|
this.employeeService = employeeService;
|
||||||
@ -128,14 +113,6 @@ public class EmployeeView extends BeanValidationForm<Employee> implements HasUrl
|
|||||||
personalEmail.setWidthFull();
|
personalEmail.setWidthFull();
|
||||||
personalEmail.setMaxLength(30);
|
personalEmail.setMaxLength(30);
|
||||||
|
|
||||||
position = new TextField("Cargo");
|
|
||||||
position.setWidthFull();
|
|
||||||
position.setMaxLength(30);
|
|
||||||
|
|
||||||
team = new TextField("Equipo");
|
|
||||||
team.setWidthFull();
|
|
||||||
team.setMaxLength(30);
|
|
||||||
|
|
||||||
emergencyCName = new TextField("Nombres y Apellidos de Contacto");
|
emergencyCName = new TextField("Nombres y Apellidos de Contacto");
|
||||||
emergencyCName.setWidthFull();
|
emergencyCName.setWidthFull();
|
||||||
emergencyCName.setMaxLength(50);
|
emergencyCName.setMaxLength(50);
|
||||||
@ -158,55 +135,21 @@ public class EmployeeView extends BeanValidationForm<Employee> implements HasUrl
|
|||||||
emergencyCEmail.setWidthFull();
|
emergencyCEmail.setWidthFull();
|
||||||
emergencyCEmail.setMaxLength(30);
|
emergencyCEmail.setMaxLength(30);
|
||||||
|
|
||||||
MemoryBuffer buffer = new MemoryBuffer();
|
|
||||||
imageUpload = new Upload(buffer);
|
|
||||||
imageUpload.setAcceptedFileTypes("image/jpeg", "image/png", "image/gif");
|
|
||||||
imageUpload.setMaxFileSize(1024 * 1024);
|
|
||||||
|
|
||||||
employeeImage = new Image();
|
|
||||||
employeeImage.setMaxHeight("150px");
|
|
||||||
employeeImage.setMaxWidth("150px");
|
|
||||||
|
|
||||||
|
|
||||||
imageUpload.addSucceededListener(event -> {
|
|
||||||
try {
|
|
||||||
InputStream inputStream = buffer.getInputStream();
|
|
||||||
imageBytes = inputStream.readAllBytes();
|
|
||||||
employeeImage.setSrc(
|
|
||||||
"data:image/png;base64," + java.util.Base64.getEncoder().encodeToString(imageBytes));
|
|
||||||
Notification.show("Imagen subida correctamente.");
|
|
||||||
} catch (Exception ex) {
|
|
||||||
Notification.show(
|
|
||||||
"Error al subir la imagen: " + ex.getMessage(), 3000, Notification.Position.MIDDLE);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
editButton = new Button("Editar");
|
|
||||||
saveButton = new Button("Guardar");
|
|
||||||
|
|
||||||
setFormFieldsEnabled(false);
|
|
||||||
|
|
||||||
editButton.addClickListener(event -> setFormFieldsEnabled(true));
|
|
||||||
|
|
||||||
|
|
||||||
contentLayout.add(
|
contentLayout.add(
|
||||||
mt, fs, firstName, lastName, status, birthday, birthCity, maritalStatus,
|
mt, fs, firstName, lastName, status, birthday, birthCity, maritalStatus,
|
||||||
residenceAddress, phoneNumber, personalEmail, position, team, imageUpload, employeeImage);
|
residenceAddress, phoneNumber, personalEmail);
|
||||||
|
|
||||||
contentLayout2.add(
|
contentLayout2.add(
|
||||||
ss, emergencyCName, emergencyCAddress, emergencyCPhone, emergencyCEmail);
|
ss, emergencyCName, emergencyCAddress, emergencyCPhone, emergencyCEmail);
|
||||||
|
|
||||||
setSavedHandler((SavedHandler<Employee>) employee -> {
|
setSavedHandler((SavedHandler<Employee>) employee -> {
|
||||||
if (validateForm()) {
|
if (validateForm()) {
|
||||||
employee.setProfilePicture(imageBytes);
|
|
||||||
employee.setStatus(Employee.Status.valueOf(status.getValue()));
|
|
||||||
final Employee saved = employeeService.createOrUpdate(employee);
|
final Employee saved = employeeService.createOrUpdate(employee);
|
||||||
Notification.show("Employee saved successfully.");
|
Notification.show("Empleado guardado exitosamente.");
|
||||||
getUI().ifPresent(ui -> ui.navigate(EmployeesListView.class));
|
getUI().ifPresent(ui -> ui.navigate(EmployeesListView.class));
|
||||||
setEntityWithEnabledSave(saved);
|
setEntityWithEnabledSave(saved);
|
||||||
setFormFieldsEnabled(false);
|
|
||||||
} else {
|
} else {
|
||||||
Notification.show("Please complete the required fields correctly.", 3000, Notification.Position.MIDDLE);
|
Notification.show("Acción inválida. Por favor, complete todos los campos requeridos correctamente.", 3000, Notification.Position.MIDDLE);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -214,24 +157,6 @@ public class EmployeeView extends BeanValidationForm<Employee> implements HasUrl
|
|||||||
addClassName("main-layout");
|
addClassName("main-layout");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setFormFieldsEnabled(final boolean enabled) {
|
|
||||||
firstName.setEnabled(enabled);
|
|
||||||
lastName.setEnabled(enabled);
|
|
||||||
status.setEnabled(enabled);
|
|
||||||
birthday.setEnabled(enabled);
|
|
||||||
birthCity.setEnabled(enabled);
|
|
||||||
maritalStatus.setEnabled(enabled);
|
|
||||||
residenceAddress.setEnabled(enabled);
|
|
||||||
phoneNumber.setEnabled(enabled);
|
|
||||||
personalEmail.setEnabled(enabled);
|
|
||||||
position.setEnabled(enabled);
|
|
||||||
team.setEnabled(enabled);
|
|
||||||
emergencyCName.setEnabled(enabled);
|
|
||||||
emergencyCAddress.setEnabled(enabled);
|
|
||||||
emergencyCPhone.setEnabled(enabled);
|
|
||||||
emergencyCEmail.setEnabled(enabled);
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean validateForm() {
|
private boolean validateForm() {
|
||||||
return !firstName.isEmpty() && !lastName.isEmpty() && status.getValue() != null;
|
return !firstName.isEmpty() && !lastName.isEmpty() && status.getValue() != null;
|
||||||
}
|
}
|
||||||
@ -259,10 +184,8 @@ public class EmployeeView extends BeanValidationForm<Employee> implements HasUrl
|
|||||||
if (StringUtils.isNotBlank(s) && !"new".equals(s)) {
|
if (StringUtils.isNotBlank(s) && !"new".equals(s)) {
|
||||||
var employee = employeeService.getEmployee(UUID.fromString(s));
|
var employee = employeeService.getEmployee(UUID.fromString(s));
|
||||||
setEntityWithEnabledSave(employee);
|
setEntityWithEnabledSave(employee);
|
||||||
setFormFieldsEnabled(true);
|
|
||||||
} else {
|
} else {
|
||||||
setEntityWithEnabledSave(new Employee());
|
setEntityWithEnabledSave(new Employee());
|
||||||
setFormFieldsEnabled(true);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -280,16 +203,11 @@ public class EmployeeView extends BeanValidationForm<Employee> implements HasUrl
|
|||||||
residenceAddress,
|
residenceAddress,
|
||||||
phoneNumber,
|
phoneNumber,
|
||||||
personalEmail,
|
personalEmail,
|
||||||
position,
|
|
||||||
team,
|
|
||||||
imageUpload,
|
|
||||||
employeeImage,
|
|
||||||
ss,
|
ss,
|
||||||
emergencyCName,
|
emergencyCName,
|
||||||
emergencyCAddress,
|
emergencyCAddress,
|
||||||
emergencyCPhone,
|
emergencyCPhone,
|
||||||
emergencyCEmail,
|
emergencyCEmail
|
||||||
editButton
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,19 +3,18 @@ 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.button.Button;
|
import com.vaadin.flow.component.button.Button;
|
||||||
|
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.notification.Notification;
|
||||||
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 org.vaadin.firitin.components.grid.PagingGrid;
|
|
||||||
import com.vaadin.flow.component.grid.GridSortOrder;
|
|
||||||
import com.vaadin.flow.data.provider.SortDirection;
|
|
||||||
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 java.util.List;
|
import org.springframework.data.domain.Page;
|
||||||
|
|
||||||
|
|
||||||
@SpringComponent
|
@SpringComponent
|
||||||
@Scope("prototype")
|
@Scope("prototype")
|
||||||
@ -25,92 +24,111 @@ import java.util.List;
|
|||||||
public class EmployeesListView extends Main {
|
public class EmployeesListView extends Main {
|
||||||
|
|
||||||
private final EmployeeService employeeService;
|
private final EmployeeService employeeService;
|
||||||
private final PagingGrid<Employee> table = new PagingGrid<>(Employee.class);
|
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(final EmployeeService employeeService) {
|
public EmployeesListView(EmployeeService employeeService) {
|
||||||
this.employeeService = employeeService;
|
this.employeeService = employeeService;
|
||||||
setupView();
|
addComponents();
|
||||||
refreshGrid();
|
updateGrid();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setupView() {
|
private void addComponents() {
|
||||||
add(new H2("Employee List"));
|
addTitle();
|
||||||
configureTable();
|
addOrderButtons();
|
||||||
add(createAddEmployeeButton());
|
configureGrid();
|
||||||
add(table);
|
addNavigationButtons();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void configureTable() {
|
private void addTitle() {
|
||||||
table.setColumns("firstName", "lastName", "status");
|
final H2 title = new H2("Employees list");
|
||||||
addEditButtonColumn("Edit", this::navigateToEditView);
|
add(title);
|
||||||
setupPagingGrid();
|
|
||||||
|
|
||||||
table.addItemDoubleClickListener(event -> {
|
|
||||||
Employee employee = event.getItem();
|
|
||||||
navigateToEditView(employee);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateEmployeeStatus(final Employee employee, final boolean isActive) {
|
private void addOrderButtons() {
|
||||||
employee.setStatus(isActive ? Employee.Status.ACTIVE : Employee.Status.INACTIVE);
|
final HorizontalLayout hl = new HorizontalLayout();
|
||||||
employeeService.createOrUpdate(employee);
|
hl.add(createOrderButton("Employee List in Ascending Order", true));
|
||||||
refreshGrid();
|
hl.add(createOrderButton("Employee List in Descending Order", false));
|
||||||
|
add(hl);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addEditButtonColumn(final String label, final ButtonClickHandler handler) {
|
private Button createOrderButton(String label, boolean isAscending) {
|
||||||
table.addComponentColumn(employee -> createButton(label, () -> handler.handle(employee)));
|
|
||||||
}
|
|
||||||
|
|
||||||
private Button createButton(final String label, final Runnable onClickAction) {
|
|
||||||
Button button = new Button(label);
|
Button button = new Button(label);
|
||||||
button.addClickListener(event -> onClickAction.run());
|
button.addClickListener(event -> {
|
||||||
|
sortField = "firstName";
|
||||||
|
ascending = isAscending;
|
||||||
|
updateGrid();
|
||||||
|
});
|
||||||
return button;
|
return button;
|
||||||
}
|
}
|
||||||
|
private void configureGrid() {
|
||||||
|
grid = new VGrid<>(Employee.class);
|
||||||
|
grid.setColumns("firstName", "lastName", "status");
|
||||||
|
grid.setAllRowsVisible(true);
|
||||||
|
grid.addComponentColumn(employee -> createStatusComboBox()).setHeader("Change Status");
|
||||||
|
grid.addComponentColumn(employee -> createEditButton());
|
||||||
|
grid.addComponentColumn(employee -> createSaveButton());
|
||||||
|
add(grid);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ComboBox<String> createStatusComboBox() {
|
||||||
|
ComboBox<String> statusComboBox = new ComboBox<>();
|
||||||
|
statusComboBox.setItems("Active", "Inactive");
|
||||||
|
return statusComboBox;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Button createEditButton() {
|
||||||
|
return new Button("Edit");
|
||||||
|
}
|
||||||
|
private Button createSaveButton() {
|
||||||
|
return new Button("Save");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addNavigationButtons() {
|
||||||
|
final HorizontalLayout hf = new HorizontalLayout();
|
||||||
|
previous = createNavigationButton("Previous", -1);
|
||||||
|
next = createNavigationButton("Next", 1);
|
||||||
|
hf.add(previous, next, createAddEmployeeButton());
|
||||||
|
add(hf);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Button createNavigationButton(String label, int increment) {
|
||||||
|
Button button = new Button(label);
|
||||||
|
button.addClickListener(event -> handlePageChange(increment));
|
||||||
|
return button;
|
||||||
|
}
|
||||||
|
private void handlePageChange(int increment) {
|
||||||
|
if (isPageChangeValid(increment)) {
|
||||||
|
currentPage += increment;
|
||||||
|
updateGrid();
|
||||||
|
} else {
|
||||||
|
showNotification("No existen más datos en lista.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private boolean isPageChangeValid(int increment) {
|
||||||
|
if (increment < 0) {
|
||||||
|
return currentPage > 1;
|
||||||
|
} else if (increment > 0) {
|
||||||
|
return employeeService.hasNextPage(currentPage, pageSize, sortField, ascending);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
private void showNotification(String message) {
|
||||||
|
Notification notification = new Notification(message, 3000, Notification.Position.BOTTOM_END);
|
||||||
|
notification.open();
|
||||||
|
}
|
||||||
private Button createAddEmployeeButton() {
|
private Button createAddEmployeeButton() {
|
||||||
return createButton("Add Employee", this::navigateToAddEmployeeView);
|
return new Button("Add Employee", event ->
|
||||||
|
getUI().flatMap(ui -> ui.navigate(EmployeeView.class, "new"))
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
private void updateGrid() {
|
||||||
private void navigateToEditView(final Employee employee) {
|
Page<Employee> page = employeeService.getEmployeesPaginated(currentPage, pageSize, sortField, ascending);
|
||||||
getUI().ifPresent(ui -> ui.navigate(EmployeeView.class, employee.getId().toString()));
|
grid.setItems(page.getContent());
|
||||||
}
|
|
||||||
|
|
||||||
private void navigateToAddEmployeeView() {
|
|
||||||
getUI().ifPresent(ui -> ui.navigate(EmployeeView.class, "new"));
|
|
||||||
}
|
|
||||||
|
|
||||||
private void setupPagingGrid() {
|
|
||||||
table.setPaginationBarMode(PagingGrid.PaginationBarMode.BOTTOM);
|
|
||||||
table.setPageSize(5);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void refreshGrid() {
|
|
||||||
List<Employee> employees = employeeService.getEmployeeRepository().findAll();
|
|
||||||
table.setItems(employees);
|
|
||||||
table.setPagingDataProvider((page, pageSize) -> fetchEmployees((int) page, pageSize));
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<Employee> fetchEmployees(final int page, final int pageSize) {
|
|
||||||
int start = page * pageSize;
|
|
||||||
if (hasSortOrder()) {
|
|
||||||
return fetchSortedEmployees(start, pageSize);
|
|
||||||
}
|
|
||||||
return employeeService.findEmployees(start, pageSize);
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean hasSortOrder() {
|
|
||||||
return !table.getSortOrder().isEmpty();
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<Employee> fetchSortedEmployees(final int start, final int pageSize) {
|
|
||||||
GridSortOrder<Employee> sortOrder = table.getSortOrder().getFirst();
|
|
||||||
return employeeService.findEmployees(start, pageSize,
|
|
||||||
sortOrder.getSorted().getKey(),
|
|
||||||
sortOrder.getDirection() == SortDirection.ASCENDING);
|
|
||||||
}
|
|
||||||
|
|
||||||
@FunctionalInterface
|
|
||||||
private interface ButtonClickHandler {
|
|
||||||
void handle(Employee employee);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user