From 9b3fed87a9c3ec26c23056c96eccf3a5ca1f1dcd Mon Sep 17 00:00:00 2001 From: Melina Gutierrez Date: Sat, 7 Sep 2024 17:33:54 -0400 Subject: [PATCH] #4 Perfil de Empleado-Registro, Guardado y Modificacion de Informacion Personal con las mejoras implementadas --- .../primefactorsolutions/model/Employee.java | 5 +- .../views/EmployeeView.java | 105 +++++++++++++++++- 2 files changed, 102 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/primefactorsolutions/model/Employee.java b/src/main/java/com/primefactorsolutions/model/Employee.java index c130d86..0e4d33a 100644 --- a/src/main/java/com/primefactorsolutions/model/Employee.java +++ b/src/main/java/com/primefactorsolutions/model/Employee.java @@ -28,9 +28,8 @@ private String emergencyCAddress; private String emergencyCPhone; private String emergencyCEmail; - @Lob - private byte[] profilePicture; - + @Column(columnDefinition = "TEXT") + private String profileImage; @Enumerated(EnumType.STRING) private Status status; public enum Status { diff --git a/src/main/java/com/primefactorsolutions/views/EmployeeView.java b/src/main/java/com/primefactorsolutions/views/EmployeeView.java index 15b272d..0011715 100644 --- a/src/main/java/com/primefactorsolutions/views/EmployeeView.java +++ b/src/main/java/com/primefactorsolutions/views/EmployeeView.java @@ -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.*; 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.form.BeanValidationForm; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.Base64; import java.util.List; import java.util.UUID; @@ -31,6 +37,7 @@ import java.util.UUID; public class EmployeeView extends BeanValidationForm implements HasUrlParameter { 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_VALIDATE_ERROR = "Please complete the required fields correctly."; 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 implements HasUrl private final TextField residenceAddress = createTextField("Dirección de Residencia", 50, false); private final TextField phoneNumber = createTextField("Teléfono", 8, false); 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 emergencyCAddress = createTextField("Dirección de Contacto", 50, false); private final TextField emergencyCPhone = createTextField("Teléfono de Contacto", 8, false); 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 editButton = new Button(EDIT_BUTTON_TEXT, e -> enableEditMode()); private final H2 mt = new H2("Información General del Empleado"); private final H3 fs = new H3("Información Personal"); @@ -69,8 +84,29 @@ public class EmployeeView extends BeanValidationForm implements HasUrl phoneNumber.addValueChangeListener(e -> validatePhoneNumber(phoneNumber, e.getValue())); emergencyCPhone.setValueChangeMode(ValueChangeMode.EAGER); emergencyCPhone.addValueChangeListener(e -> validatePhoneNumber(emergencyCPhone, e.getValue())); - + configureUpload(); 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) { @@ -95,8 +131,13 @@ public class EmployeeView extends BeanValidationForm implements HasUrl maritalStatus, residenceAddress, phoneNumber, - personalEmail); - contentLayout2.add(ss, emergencyCName, emergencyCAddress, emergencyCPhone, emergencyCEmail, saveButton); + personalEmail, + position, + team); + contentLayout2.add( + ss, emergencyCName, emergencyCAddress, + emergencyCPhone, emergencyCEmail, upload, + profileImagePreview, saveButton, editButton); mainLayout.add(contentLayout1, contentLayout2); addClassName("main-layout"); @@ -162,6 +203,13 @@ public class EmployeeView extends BeanValidationForm implements HasUrl } } + private void enableEditMode() { + setFieldsEditable(); + saveButton.setVisible(true); + editButton.setVisible(false); + } + + @Override public void setParameter(final BeforeEvent beforeEvent, final String action) { RouteParameters params = beforeEvent.getRouteParameters(); @@ -169,19 +217,42 @@ public class EmployeeView extends BeanValidationForm implements HasUrl if ("new".equals(action)) { setEntityWithEnabledSave(new Employee()); + saveButton.setVisible(true); + editButton.setVisible(false); + setFieldsEditable(); } else { UUID employeeId = UUID.fromString(s); var employee = employeeService.getEmployee(employeeId); setEntityWithEnabledSave(employee); if ("edit".equals(action) && !s.isEmpty()) { + saveButton.setVisible(true); + editButton.setVisible(false); status.setValue(employee.getStatus()); + setFieldsEditable(); } else if ("view".equals(action) && !s.isEmpty()) { setFieldsReadOnly(); 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() { firstName.setReadOnly(true); lastName.setReadOnly(true); @@ -192,18 +263,42 @@ public class EmployeeView extends BeanValidationForm implements HasUrl residenceAddress.setReadOnly(true); phoneNumber.setReadOnly(true); personalEmail.setReadOnly(true); + position.setReadOnly(true); + team.setReadOnly(true); emergencyCName.setReadOnly(true); emergencyCAddress.setReadOnly(true); emergencyCPhone.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 protected List getFormComponents() { return List.of( mt, fs, firstName, lastName, status, birthday, birthCity, maritalStatus, - residenceAddress, phoneNumber, personalEmail, ss, emergencyCName, - emergencyCAddress, emergencyCPhone, emergencyCEmail, saveButton + residenceAddress, phoneNumber, personalEmail, position, team, ss, emergencyCName, + emergencyCAddress, emergencyCPhone, emergencyCEmail, upload, profileImagePreview, saveButton, editButton ); } }