#4 , 5 , 6 Perfil de Empleado - Registro, Guardado y Modificacion de Informacion Personal

This commit is contained in:
Melina Gutierrez 2024-09-06 10:31:35 -04:00
parent fce9f6ef0e
commit 5c4d2cdc74
3 changed files with 96 additions and 2 deletions

View File

@ -22,10 +22,15 @@
private String residenceAddress;
private String phoneNumber;
private String personalEmail;
private String position;
private String team;
private String emergencyCName;
private String emergencyCAddress;
private String emergencyCPhone;
private String emergencyCEmail;
@Lob
private byte[] profilePicture;
@Enumerated(EnumType.STRING)
private Status status;
public enum Status {

View File

@ -7,11 +7,14 @@ 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.H3;
import com.vaadin.flow.component.html.Image;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.textfield.EmailField;
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.router.BeforeEvent;
import com.vaadin.flow.router.HasUrlParameter;
@ -24,6 +27,8 @@ import org.springframework.context.annotation.Scope;
import org.vaadin.firitin.components.datepicker.VDatePicker;
import org.vaadin.firitin.form.BeanValidationForm;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@ -45,6 +50,8 @@ public class EmployeeView extends BeanValidationForm<Employee> implements HasUrl
private final TextField residenceAddress;
private final TextField phoneNumber;
private final EmailField personalEmail;
private final TextField position;
private final TextField team;
private final TextField emergencyCName;
private final TextField emergencyCAddress;
@ -55,6 +62,14 @@ public class EmployeeView extends BeanValidationForm<Employee> implements HasUrl
private final H3 fs;
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) {
super(Employee.class);
this.employeeService = employeeService;
@ -113,6 +128,14 @@ public class EmployeeView extends BeanValidationForm<Employee> implements HasUrl
personalEmail.setWidthFull();
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.setWidthFull();
emergencyCName.setMaxLength(50);
@ -135,19 +158,53 @@ public class EmployeeView extends BeanValidationForm<Employee> implements HasUrl
emergencyCEmail.setWidthFull();
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(
mt, fs, firstName, lastName, status, birthday, birthCity, maritalStatus,
residenceAddress, phoneNumber, personalEmail);
residenceAddress, phoneNumber, personalEmail, position, team, imageUpload, employeeImage);
contentLayout2.add(
ss, emergencyCName, emergencyCAddress, emergencyCPhone, emergencyCEmail);
setSavedHandler((SavedHandler<Employee>) employee -> {
if (validateForm()) {
employee.setProfilePicture(imageBytes);
employee.setStatus(Employee.Status.valueOf(status.getValue()));
final Employee saved = employeeService.createOrUpdate(employee);
Notification.show("Employee saved successfully.");
getUI().ifPresent(ui -> ui.navigate(EmployeesListView.class));
setEntityWithEnabledSave(saved);
setFormFieldsEnabled(false);
} else {
Notification.show("Please complete the required fields correctly.", 3000, Notification.Position.MIDDLE);
}
@ -157,6 +214,24 @@ public class EmployeeView extends BeanValidationForm<Employee> implements HasUrl
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() {
return !firstName.isEmpty() && !lastName.isEmpty() && status.getValue() != null;
}
@ -184,8 +259,10 @@ public class EmployeeView extends BeanValidationForm<Employee> implements HasUrl
if (StringUtils.isNotBlank(s) && !"new".equals(s)) {
var employee = employeeService.getEmployee(UUID.fromString(s));
setEntityWithEnabledSave(employee);
setFormFieldsEnabled(true);
} else {
setEntityWithEnabledSave(new Employee());
setFormFieldsEnabled(true);
}
}
@ -203,11 +280,16 @@ public class EmployeeView extends BeanValidationForm<Employee> implements HasUrl
residenceAddress,
phoneNumber,
personalEmail,
position,
team,
imageUpload,
employeeImage,
ss,
emergencyCName,
emergencyCAddress,
emergencyCPhone,
emergencyCEmail
emergencyCEmail,
editButton
);
}
}

View File

@ -44,6 +44,11 @@ public class EmployeesListView extends Main {
table.setColumns("firstName", "lastName", "status");
addEditButtonColumn("Edit", this::navigateToEditView);
setupPagingGrid();
table.addItemDoubleClickListener(event -> {
Employee employee = event.getItem();
navigateToEditView(employee);
});
}
private void updateEmployeeStatus(final Employee employee, final boolean isActive) {
@ -80,6 +85,8 @@ public class EmployeesListView extends Main {
}
private void refreshGrid() {
List<Employee> employees = employeeService.getEmployeeRepository().findAll();
table.setItems(employees);
table.setPagingDataProvider((page, pageSize) -> fetchEmployees((int) page, pageSize));
}