diff --git a/src/main/java/com/primefactorsolutions/model/Employee.java b/src/main/java/com/primefactorsolutions/model/Employee.java index c6d918b..c130d86 100644 --- a/src/main/java/com/primefactorsolutions/model/Employee.java +++ b/src/main/java/com/primefactorsolutions/model/Employee.java @@ -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 { diff --git a/src/main/java/com/primefactorsolutions/views/EmployeeView.java b/src/main/java/com/primefactorsolutions/views/EmployeeView.java index 9e49fe4..e367113 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.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 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 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 formFields = new ArrayList<>(); + public EmployeeView(final EmployeeService employeeService) { super(Employee.class); this.employeeService = employeeService; @@ -113,6 +128,14 @@ public class EmployeeView extends BeanValidationForm 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 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 -> { 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 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 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 implements HasUrl residenceAddress, phoneNumber, personalEmail, + position, + team, + imageUpload, + employeeImage, ss, emergencyCName, emergencyCAddress, emergencyCPhone, - emergencyCEmail + emergencyCEmail, + editButton ); } } diff --git a/src/main/java/com/primefactorsolutions/views/EmployeesListView.java b/src/main/java/com/primefactorsolutions/views/EmployeesListView.java index 489ded8..3227a3c 100644 --- a/src/main/java/com/primefactorsolutions/views/EmployeesListView.java +++ b/src/main/java/com/primefactorsolutions/views/EmployeesListView.java @@ -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 employees = employeeService.getEmployeeRepository().findAll(); + table.setItems(employees); table.setPagingDataProvider((page, pageSize) -> fetchEmployees((int) page, pageSize)); }