#4 Perfil de Empleado-Registro, Guardado y Modificacion de Informacion Personal con las mejoras implementadas
This commit is contained in:
parent
94ea2393c4
commit
9b3fed87a9
@ -28,9 +28,8 @@
|
|||||||
private String emergencyCAddress;
|
private String emergencyCAddress;
|
||||||
private String emergencyCPhone;
|
private String emergencyCPhone;
|
||||||
private String emergencyCEmail;
|
private String emergencyCEmail;
|
||||||
@Lob
|
@Column(columnDefinition = "TEXT")
|
||||||
private byte[] profilePicture;
|
private String profileImage;
|
||||||
|
|
||||||
@Enumerated(EnumType.STRING)
|
@Enumerated(EnumType.STRING)
|
||||||
private Status status;
|
private Status status;
|
||||||
public enum Status {
|
public enum Status {
|
||||||
|
@ -7,11 +7,14 @@ 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.*;
|
import com.vaadin.flow.router.*;
|
||||||
import com.vaadin.flow.spring.annotation.SpringComponent;
|
import com.vaadin.flow.spring.annotation.SpringComponent;
|
||||||
@ -20,6 +23,9 @@ 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.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Base64;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@ -31,6 +37,7 @@ import java.util.UUID;
|
|||||||
public class EmployeeView extends BeanValidationForm<Employee> implements HasUrlParameter<String> {
|
public class EmployeeView extends BeanValidationForm<Employee> implements HasUrlParameter<String> {
|
||||||
|
|
||||||
private static final String SAVE_BUTTON_TEXT = "Save";
|
private static final String SAVE_BUTTON_TEXT = "Save";
|
||||||
|
private static final String EDIT_BUTTON_TEXT = "Edit";
|
||||||
private static final String NOTIFICATION_SAVE_SUCCESS = "Employee saved successfully.";
|
private static final String NOTIFICATION_SAVE_SUCCESS = "Employee saved successfully.";
|
||||||
private static final String NOTIFICATION_VALIDATE_ERROR = "Please complete the required fields correctly.";
|
private static final String NOTIFICATION_VALIDATE_ERROR = "Please complete the required fields correctly.";
|
||||||
private static final String PHONE_NUMBER_ERROR_MESSAGE = "El teléfono debe contener solo números.";
|
private static final String PHONE_NUMBER_ERROR_MESSAGE = "El teléfono debe contener solo números.";
|
||||||
@ -46,11 +53,19 @@ public class EmployeeView extends BeanValidationForm<Employee> implements HasUrl
|
|||||||
private final TextField residenceAddress = createTextField("Dirección de Residencia", 50, false);
|
private final TextField residenceAddress = createTextField("Dirección de Residencia", 50, false);
|
||||||
private final TextField phoneNumber = createTextField("Teléfono", 8, false);
|
private final TextField phoneNumber = createTextField("Teléfono", 8, false);
|
||||||
private final EmailField personalEmail = createEmailField("E-mail");
|
private final EmailField personalEmail = createEmailField("E-mail");
|
||||||
|
private final TextField position = createTextField("Cargo", 30, false);
|
||||||
|
private final TextField team = createTextField("Equipo", 30, false);
|
||||||
private final TextField emergencyCName = createTextField("Nombres y Apellidos de Contacto", 50, false);
|
private final TextField emergencyCName = createTextField("Nombres y Apellidos de Contacto", 50, false);
|
||||||
private final TextField emergencyCAddress = createTextField("Dirección de Contacto", 50, false);
|
private final TextField emergencyCAddress = createTextField("Dirección de Contacto", 50, false);
|
||||||
private final TextField emergencyCPhone = createTextField("Teléfono de Contacto", 8, false);
|
private final TextField emergencyCPhone = createTextField("Teléfono de Contacto", 8, false);
|
||||||
private final EmailField emergencyCEmail = createEmailField("Email de Contacto");
|
private final EmailField emergencyCEmail = createEmailField("Email de Contacto");
|
||||||
|
|
||||||
|
private final MemoryBuffer buffer = new MemoryBuffer();
|
||||||
|
private final Upload upload = new Upload(buffer);
|
||||||
|
private final Image profileImagePreview = new Image();
|
||||||
|
|
||||||
private final Button saveButton = new Button(SAVE_BUTTON_TEXT, e -> saveEmployee());
|
private final Button saveButton = new Button(SAVE_BUTTON_TEXT, e -> saveEmployee());
|
||||||
|
private final Button editButton = new Button(EDIT_BUTTON_TEXT, e -> enableEditMode());
|
||||||
|
|
||||||
private final H2 mt = new H2("Información General del Empleado");
|
private final H2 mt = new H2("Información General del Empleado");
|
||||||
private final H3 fs = new H3("Información Personal");
|
private final H3 fs = new H3("Información Personal");
|
||||||
@ -69,8 +84,29 @@ public class EmployeeView extends BeanValidationForm<Employee> implements HasUrl
|
|||||||
phoneNumber.addValueChangeListener(e -> validatePhoneNumber(phoneNumber, e.getValue()));
|
phoneNumber.addValueChangeListener(e -> validatePhoneNumber(phoneNumber, e.getValue()));
|
||||||
emergencyCPhone.setValueChangeMode(ValueChangeMode.EAGER);
|
emergencyCPhone.setValueChangeMode(ValueChangeMode.EAGER);
|
||||||
emergencyCPhone.addValueChangeListener(e -> validatePhoneNumber(emergencyCPhone, e.getValue()));
|
emergencyCPhone.addValueChangeListener(e -> validatePhoneNumber(emergencyCPhone, e.getValue()));
|
||||||
|
configureUpload();
|
||||||
saveButton.setVisible(true);
|
saveButton.setVisible(true);
|
||||||
|
editButton.setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void configureUpload() {
|
||||||
|
upload.setAcceptedFileTypes("image/jpeg", "image/png");
|
||||||
|
upload.setMaxFileSize(1024 * 1024);
|
||||||
|
upload.addSucceededListener(event -> {
|
||||||
|
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
|
||||||
|
buffer.getInputStream().transferTo(outputStream);
|
||||||
|
byte[] imageBytes = outputStream.toByteArray();
|
||||||
|
String base64Image = Base64.getEncoder().encodeToString(imageBytes);
|
||||||
|
|
||||||
|
getEntity().setProfileImage(base64Image);
|
||||||
|
|
||||||
|
profileImagePreview.setSrc("data:image/jpeg;base64," + base64Image);
|
||||||
|
profileImagePreview.setMaxWidth("150px");
|
||||||
|
profileImagePreview.setMaxHeight("150px");
|
||||||
|
} catch (IOException e) {
|
||||||
|
Notification.show("Error al subir la imagen.");
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void validatePhoneNumber(final TextField textField, final String value) {
|
private void validatePhoneNumber(final TextField textField, final String value) {
|
||||||
@ -95,8 +131,13 @@ public class EmployeeView extends BeanValidationForm<Employee> implements HasUrl
|
|||||||
maritalStatus,
|
maritalStatus,
|
||||||
residenceAddress,
|
residenceAddress,
|
||||||
phoneNumber,
|
phoneNumber,
|
||||||
personalEmail);
|
personalEmail,
|
||||||
contentLayout2.add(ss, emergencyCName, emergencyCAddress, emergencyCPhone, emergencyCEmail, saveButton);
|
position,
|
||||||
|
team);
|
||||||
|
contentLayout2.add(
|
||||||
|
ss, emergencyCName, emergencyCAddress,
|
||||||
|
emergencyCPhone, emergencyCEmail, upload,
|
||||||
|
profileImagePreview, saveButton, editButton);
|
||||||
|
|
||||||
mainLayout.add(contentLayout1, contentLayout2);
|
mainLayout.add(contentLayout1, contentLayout2);
|
||||||
addClassName("main-layout");
|
addClassName("main-layout");
|
||||||
@ -162,6 +203,13 @@ public class EmployeeView extends BeanValidationForm<Employee> implements HasUrl
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void enableEditMode() {
|
||||||
|
setFieldsEditable();
|
||||||
|
saveButton.setVisible(true);
|
||||||
|
editButton.setVisible(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setParameter(final BeforeEvent beforeEvent, final String action) {
|
public void setParameter(final BeforeEvent beforeEvent, final String action) {
|
||||||
RouteParameters params = beforeEvent.getRouteParameters();
|
RouteParameters params = beforeEvent.getRouteParameters();
|
||||||
@ -169,19 +217,42 @@ public class EmployeeView extends BeanValidationForm<Employee> implements HasUrl
|
|||||||
|
|
||||||
if ("new".equals(action)) {
|
if ("new".equals(action)) {
|
||||||
setEntityWithEnabledSave(new Employee());
|
setEntityWithEnabledSave(new Employee());
|
||||||
|
saveButton.setVisible(true);
|
||||||
|
editButton.setVisible(false);
|
||||||
|
setFieldsEditable();
|
||||||
} else {
|
} else {
|
||||||
UUID employeeId = UUID.fromString(s);
|
UUID employeeId = UUID.fromString(s);
|
||||||
var employee = employeeService.getEmployee(employeeId);
|
var employee = employeeService.getEmployee(employeeId);
|
||||||
setEntityWithEnabledSave(employee);
|
setEntityWithEnabledSave(employee);
|
||||||
if ("edit".equals(action) && !s.isEmpty()) {
|
if ("edit".equals(action) && !s.isEmpty()) {
|
||||||
|
saveButton.setVisible(true);
|
||||||
|
editButton.setVisible(false);
|
||||||
status.setValue(employee.getStatus());
|
status.setValue(employee.getStatus());
|
||||||
|
setFieldsEditable();
|
||||||
} else if ("view".equals(action) && !s.isEmpty()) {
|
} else if ("view".equals(action) && !s.isEmpty()) {
|
||||||
setFieldsReadOnly();
|
setFieldsReadOnly();
|
||||||
saveButton.setVisible(false);
|
saveButton.setVisible(false);
|
||||||
|
editButton.setVisible(true);
|
||||||
|
setFieldsReadOnly();
|
||||||
|
displayProfileImage(employee);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void displayProfileImage(final Employee employee) {
|
||||||
|
if (employee.getProfileImage() != null && !employee.getProfileImage().isEmpty()) {
|
||||||
|
profileImagePreview.setSrc("data:image/jpeg;base64," + employee.getProfileImage());
|
||||||
|
profileImagePreview.setVisible(true);
|
||||||
|
profileImagePreview.setMaxWidth("150px");
|
||||||
|
profileImagePreview.setMaxHeight("150px");
|
||||||
|
|
||||||
|
upload.setVisible(false);
|
||||||
|
} else {
|
||||||
|
profileImagePreview.setVisible(false);
|
||||||
|
upload.setVisible(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void setFieldsReadOnly() {
|
private void setFieldsReadOnly() {
|
||||||
firstName.setReadOnly(true);
|
firstName.setReadOnly(true);
|
||||||
lastName.setReadOnly(true);
|
lastName.setReadOnly(true);
|
||||||
@ -192,18 +263,42 @@ public class EmployeeView extends BeanValidationForm<Employee> implements HasUrl
|
|||||||
residenceAddress.setReadOnly(true);
|
residenceAddress.setReadOnly(true);
|
||||||
phoneNumber.setReadOnly(true);
|
phoneNumber.setReadOnly(true);
|
||||||
personalEmail.setReadOnly(true);
|
personalEmail.setReadOnly(true);
|
||||||
|
position.setReadOnly(true);
|
||||||
|
team.setReadOnly(true);
|
||||||
emergencyCName.setReadOnly(true);
|
emergencyCName.setReadOnly(true);
|
||||||
emergencyCAddress.setReadOnly(true);
|
emergencyCAddress.setReadOnly(true);
|
||||||
emergencyCPhone.setReadOnly(true);
|
emergencyCPhone.setReadOnly(true);
|
||||||
emergencyCEmail.setReadOnly(true);
|
emergencyCEmail.setReadOnly(true);
|
||||||
|
upload.setVisible(true);
|
||||||
|
profileImagePreview.setVisible(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void setFieldsEditable() {
|
||||||
|
firstName.setReadOnly(false);
|
||||||
|
lastName.setReadOnly(false);
|
||||||
|
status.setReadOnly(false);
|
||||||
|
birthday.setReadOnly(false);
|
||||||
|
birthCity.setReadOnly(false);
|
||||||
|
maritalStatus.setReadOnly(false);
|
||||||
|
residenceAddress.setReadOnly(false);
|
||||||
|
phoneNumber.setReadOnly(false);
|
||||||
|
personalEmail.setReadOnly(false);
|
||||||
|
position.setReadOnly(false);
|
||||||
|
team.setReadOnly(false);
|
||||||
|
emergencyCName.setReadOnly(false);
|
||||||
|
emergencyCAddress.setReadOnly(false);
|
||||||
|
emergencyCPhone.setReadOnly(false);
|
||||||
|
emergencyCEmail.setReadOnly(false);
|
||||||
|
upload.setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected List<Component> getFormComponents() {
|
protected List<Component> getFormComponents() {
|
||||||
return List.of(
|
return List.of(
|
||||||
mt, fs, firstName, lastName, status, birthday, birthCity, maritalStatus,
|
mt, fs, firstName, lastName, status, birthday, birthCity, maritalStatus,
|
||||||
residenceAddress, phoneNumber, personalEmail, ss, emergencyCName,
|
residenceAddress, phoneNumber, personalEmail, position, team, ss, emergencyCName,
|
||||||
emergencyCAddress, emergencyCPhone, emergencyCEmail, saveButton
|
emergencyCAddress, emergencyCPhone, emergencyCEmail, upload, profileImagePreview, saveButton, editButton
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user