From ccb36294098346c4c3897033d40905b45213517d Mon Sep 17 00:00:00 2001 From: "melina.gutierrez" Date: Sun, 8 Sep 2024 13:08:41 +0000 Subject: [PATCH] =?UTF-8?q?#4-Registro-de-Informaci=C3=B3n-Personal=20(#16?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: ricardo051199 Co-authored-by: Melina Gutierrez Reviewed-on: https://git.primefactorsolutions.com/PFS/pfs-intra/pulls/16 Co-authored-by: melina.gutierrez Co-committed-by: melina.gutierrez --- .../primefactorsolutions/model/Employee.java | 4 + .../views/EmployeeView.java | 108 +++++++++++++++++- .../views/EmployeesListView.java | 2 + 3 files changed, 109 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/primefactorsolutions/model/Employee.java b/src/main/java/com/primefactorsolutions/model/Employee.java index c6d918b..0e4d33a 100644 --- a/src/main/java/com/primefactorsolutions/model/Employee.java +++ b/src/main/java/com/primefactorsolutions/model/Employee.java @@ -22,10 +22,14 @@ 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; + @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..6709e6f 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,15 +53,25 @@ 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"); private final H3 ss = new H3("Datos de Contacto de Emergencia"); + private final H3 si = new H3("Foto del Empleado"); + public EmployeeView(final EmployeeService employeeService) { super(Employee.class); @@ -69,8 +86,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 +133,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, si, upload, + profileImagePreview, saveButton, editButton); mainLayout.add(contentLayout1, contentLayout2); addClassName("main-layout"); @@ -162,6 +205,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 +219,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 +265,43 @@ 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, si, upload, profileImagePreview, + saveButton, editButton ); } } diff --git a/src/main/java/com/primefactorsolutions/views/EmployeesListView.java b/src/main/java/com/primefactorsolutions/views/EmployeesListView.java index fed246b..e9f3726 100644 --- a/src/main/java/com/primefactorsolutions/views/EmployeesListView.java +++ b/src/main/java/com/primefactorsolutions/views/EmployeesListView.java @@ -85,6 +85,8 @@ public class EmployeesListView extends Main { } private void refreshGrid() { + List employees = employeeService.getEmployeeRepository().findAll(); + table.setItems(employees); table.setPagingDataProvider((page, pageSize) -> fetchEmployees((int) page, pageSize)); }