From c91e4ea1a5cd28d46c1255bedb53b885dc947ba8 Mon Sep 17 00:00:00 2001 From: ricardo051199 Date: Fri, 6 Sep 2024 00:59:20 +0000 Subject: [PATCH 1/3] #4-Registrar-empleado (#15) Reviewed-on: https://git.primefactorsolutions.com/PFS/pfs-intra/pulls/15 Reviewed-by: alex Co-authored-by: ricardo051199 Co-committed-by: ricardo051199 --- .../views/EmployeeView.java | 302 +++++++++--------- .../views/EmployeesListView.java | 7 +- 2 files changed, 155 insertions(+), 154 deletions(-) diff --git a/src/main/java/com/primefactorsolutions/views/EmployeeView.java b/src/main/java/com/primefactorsolutions/views/EmployeeView.java index 9e49fe4..15b272d 100644 --- a/src/main/java/com/primefactorsolutions/views/EmployeeView.java +++ b/src/main/java/com/primefactorsolutions/views/EmployeeView.java @@ -13,13 +13,9 @@ 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.data.value.ValueChangeMode; -import com.vaadin.flow.router.BeforeEvent; -import com.vaadin.flow.router.HasUrlParameter; -import com.vaadin.flow.router.PageTitle; -import com.vaadin.flow.router.Route; +import com.vaadin.flow.router.*; import com.vaadin.flow.spring.annotation.SpringComponent; import jakarta.annotation.security.PermitAll; -import org.apache.commons.lang3.StringUtils; import org.springframework.context.annotation.Scope; import org.vaadin.firitin.components.datepicker.VDatePicker; import org.vaadin.firitin.form.BeanValidationForm; @@ -31,167 +27,64 @@ import java.util.UUID; @PermitAll @Scope("prototype") @PageTitle("Employee") -@Route(value = "/employees", layout = MainLayout.class) +@Route(value = "/employees/:employeeId?/:action?", layout = MainLayout.class) public class EmployeeView extends BeanValidationForm implements HasUrlParameter { + private static final String SAVE_BUTTON_TEXT = "Save"; + 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."; + private final EmployeeService employeeService; - private final TextField firstName; - private final TextField lastName; - private final ComboBox status; - private final VDatePicker birthday; - private final TextField birthCity; - private final ComboBox maritalStatus; - private final TextField residenceAddress; - private final TextField phoneNumber; - private final EmailField personalEmail; + private final TextField firstName = createTextField("Nombres: ", 30, true); + private final TextField lastName = createTextField("Apellidos", 30, true); + private final ComboBox status = createStatusComboBox(); + private final VDatePicker birthday = new VDatePicker("Fecha de Nacimiento"); + private final TextField birthCity = createTextField("Ciudad y País de Nacimiento", 20, false); + private final ComboBox maritalStatus = createMaritalStatusComboBox(); + 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 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 Button saveButton = new Button(SAVE_BUTTON_TEXT, e -> saveEmployee()); - private final TextField emergencyCName; - private final TextField emergencyCAddress; - private final TextField emergencyCPhone; - private final EmailField emergencyCEmail; - - private final H2 mt; - private final H3 fs; - private final H3 ss; + 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"); public EmployeeView(final EmployeeService employeeService) { super(Employee.class); this.employeeService = employeeService; - mt = new H2("Información General del Empleado"); - fs = new H3("Información Personal"); - ss = new H3("Datos de Contacto de Emergencia"); + configureComponents(); + assembleLayout(); + } - final HorizontalLayout mainLayout = new HorizontalLayout(); - final VerticalLayout sidebar = createSidebar(); - final VerticalLayout contentLayout = createContentLayout(); - final VerticalLayout contentLayout2 = createContentLayout(); - - firstName = new TextField("Nombres: "); - firstName.setWidthFull(); - firstName.setMaxLength(30); - firstName.setRequired(true); - - lastName = new TextField("Apellidos"); - lastName.setWidthFull(); - lastName.setMaxLength(30); - lastName.setRequired(true); - - status = new ComboBox<>("Estado"); - status.setItems(List.of("ACTIVE", "INACTIVE")); - lastName.setWidthFull(); - lastName.setMaxLength(30); - status.setRequired(true); - - birthday = new VDatePicker("Fecha de Nacimiento"); - birthday.setWidthFull(); - - birthCity = new TextField("Ciudad y País de Nacimiento"); - birthCity.setWidthFull(); - birthCity.setMaxLength(20); - - maritalStatus = new ComboBox<>("Estado Civil"); - maritalStatus.setItems(List.of("Soltero", "Casado", "Viudo", "Divorciado")); - maritalStatus.setWidthFull(); - - residenceAddress = new TextField("Dirección de Residencia"); - residenceAddress.setWidthFull(); - residenceAddress.setMaxLength(50); - - phoneNumber = new TextField("Teléfono"); - phoneNumber.setWidthFull(); - phoneNumber.setMaxLength(8); + private void configureComponents() { phoneNumber.setValueChangeMode(ValueChangeMode.EAGER); - phoneNumber.addValueChangeListener(e -> { - if (!e.getValue().matches("\\d*")) { - phoneNumber.setErrorMessage("El teléfono debe contener solo números."); - } - }); - - personalEmail = new EmailField("E-mail"); - personalEmail.setWidthFull(); - personalEmail.setMaxLength(30); - - emergencyCName = new TextField("Nombres y Apellidos de Contacto"); - emergencyCName.setWidthFull(); - emergencyCName.setMaxLength(50); - - emergencyCAddress = new TextField("Dirección de Contacto"); - emergencyCAddress.setWidthFull(); - emergencyCAddress.setMaxLength(50); - - emergencyCPhone = new TextField("Teléfono de Contacto"); - emergencyCPhone.setWidthFull(); - emergencyCPhone.setMaxLength(8); + phoneNumber.addValueChangeListener(e -> validatePhoneNumber(phoneNumber, e.getValue())); emergencyCPhone.setValueChangeMode(ValueChangeMode.EAGER); - emergencyCPhone.addValueChangeListener(e -> { - if (!e.getValue().matches("\\d*")) { - emergencyCPhone.setErrorMessage("El teléfono debe contener solo números."); - } - }); + emergencyCPhone.addValueChangeListener(e -> validatePhoneNumber(emergencyCPhone, e.getValue())); - emergencyCEmail = new EmailField("Email de Contacto"); - emergencyCEmail.setWidthFull(); - emergencyCEmail.setMaxLength(30); - - contentLayout.add( - mt, fs, firstName, lastName, status, birthday, birthCity, maritalStatus, - residenceAddress, phoneNumber, personalEmail); - - contentLayout2.add( - ss, emergencyCName, emergencyCAddress, emergencyCPhone, emergencyCEmail); - - setSavedHandler((SavedHandler) employee -> { - if (validateForm()) { - final Employee saved = employeeService.createOrUpdate(employee); - Notification.show("Employee saved successfully."); - getUI().ifPresent(ui -> ui.navigate(EmployeesListView.class)); - setEntityWithEnabledSave(saved); - } else { - Notification.show("Please complete the required fields correctly.", 3000, Notification.Position.MIDDLE); - } - }); - - mainLayout.add(sidebar, contentLayout, contentLayout2); - addClassName("main-layout"); + saveButton.setVisible(true); } - private boolean validateForm() { - return !firstName.isEmpty() && !lastName.isEmpty() && status.getValue() != null; - } - - private VerticalLayout createSidebar() { - VerticalLayout sidebar = new VerticalLayout(); - sidebar.setWidth("250px"); - sidebar.add(new Button("Información General", e -> navigateToSection("Información General"))); - sidebar.add(new Button("Detalles Profesionales", e -> navigateToSection("Detalles Profesionales"))); - return sidebar; - } - - private void navigateToSection(final String section) { - Notification.show("Navigating to " + section); - } - - private VerticalLayout createContentLayout() { - VerticalLayout contentLayout = new VerticalLayout(); - contentLayout.setWidth("100%"); - return contentLayout; - } - - @Override - public void setParameter(final BeforeEvent beforeEvent, final String s) { - if (StringUtils.isNotBlank(s) && !"new".equals(s)) { - var employee = employeeService.getEmployee(UUID.fromString(s)); - setEntityWithEnabledSave(employee); - } else { - setEntityWithEnabledSave(new Employee()); + private void validatePhoneNumber(final TextField textField, final String value) { + if (!value.matches("\\d*")) { + textField.setErrorMessage(PHONE_NUMBER_ERROR_MESSAGE); } } - @Override - protected List getFormComponents() { - return List.of( + private void assembleLayout() { + HorizontalLayout mainLayout = new HorizontalLayout(); + VerticalLayout contentLayout1 = createContentLayout(); + VerticalLayout contentLayout2 = createContentLayout(); + + contentLayout1.add( mt, fs, firstName, @@ -202,12 +95,115 @@ public class EmployeeView extends BeanValidationForm implements HasUrl maritalStatus, residenceAddress, phoneNumber, - personalEmail, - ss, - emergencyCName, - emergencyCAddress, - emergencyCPhone, - emergencyCEmail + personalEmail); + contentLayout2.add(ss, emergencyCName, emergencyCAddress, emergencyCPhone, emergencyCEmail, saveButton); + + mainLayout.add(contentLayout1, contentLayout2); + addClassName("main-layout"); + } + + private ComboBox createMaritalStatusComboBox() { + ComboBox comboBox = new ComboBox<>("Estado Civil"); + comboBox.setItems(Employee.MaritalStatus.values()); + comboBox.setItemLabelGenerator(Employee.MaritalStatus::name); + return comboBox; + } + + private ComboBox createStatusComboBox() { + ComboBox comboBox = new ComboBox<>("Estado"); + comboBox.setItems(Employee.Status.values()); + comboBox.setItemLabelGenerator(Employee.Status::name); + comboBox.setRequiredIndicatorVisible(true); // Indicador de campo requerido + return comboBox; + } + + private VerticalLayout createContentLayout() { + VerticalLayout contentLayout = new VerticalLayout(); + contentLayout.setWidth("100%"); + return contentLayout; + } + + private TextField createTextField(final String label, final int maxLength, final boolean required) { + TextField textField = new TextField(label); + textField.setWidthFull(); + textField.setMaxLength(maxLength); + textField.setRequired(required); + return textField; + } + + private EmailField createEmailField(final String label) { + EmailField emailField = new EmailField(label); + emailField.setWidthFull(); + emailField.setMaxLength(30); + return emailField; + } + + private ComboBox createComboBox(final String label, final T[] items) { + ComboBox comboBox = new ComboBox<>(label); + comboBox.setItems(items); + comboBox.setItemLabelGenerator(Object::toString); + comboBox.setWidthFull(); + return comboBox; + } + + private boolean validateForm() { + return !firstName.isEmpty() && !lastName.isEmpty() && status.getValue() != null; + } + + private void saveEmployee() { + if (validateForm()) { + Employee employee = getEntity(); + employee.setStatus(status.getValue()); + employeeService.createOrUpdate(employee); + Notification.show(NOTIFICATION_SAVE_SUCCESS); + getUI().ifPresent(ui -> ui.navigate(EmployeesListView.class)); + } else { + Notification.show(NOTIFICATION_VALIDATE_ERROR, 3000, Notification.Position.MIDDLE); + } + } + + @Override + public void setParameter(final BeforeEvent beforeEvent, final String action) { + RouteParameters params = beforeEvent.getRouteParameters(); + String s = params.get("employeeId").orElse(null); + + if ("new".equals(action)) { + setEntityWithEnabledSave(new Employee()); + } else { + UUID employeeId = UUID.fromString(s); + var employee = employeeService.getEmployee(employeeId); + setEntityWithEnabledSave(employee); + if ("edit".equals(action) && !s.isEmpty()) { + status.setValue(employee.getStatus()); + } else if ("view".equals(action) && !s.isEmpty()) { + setFieldsReadOnly(); + saveButton.setVisible(false); + } + } + } + + private void setFieldsReadOnly() { + firstName.setReadOnly(true); + lastName.setReadOnly(true); + status.setReadOnly(true); + birthday.setReadOnly(true); + birthCity.setReadOnly(true); + maritalStatus.setReadOnly(true); + residenceAddress.setReadOnly(true); + phoneNumber.setReadOnly(true); + personalEmail.setReadOnly(true); + emergencyCName.setReadOnly(true); + emergencyCAddress.setReadOnly(true); + emergencyCPhone.setReadOnly(true); + emergencyCEmail.setReadOnly(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 ); } } diff --git a/src/main/java/com/primefactorsolutions/views/EmployeesListView.java b/src/main/java/com/primefactorsolutions/views/EmployeesListView.java index 489ded8..fed246b 100644 --- a/src/main/java/com/primefactorsolutions/views/EmployeesListView.java +++ b/src/main/java/com/primefactorsolutions/views/EmployeesListView.java @@ -42,6 +42,7 @@ public class EmployeesListView extends Main { private void configureTable() { table.setColumns("firstName", "lastName", "status"); + addEditButtonColumn("View", this::navigateToEmployeeView); addEditButtonColumn("Edit", this::navigateToEditView); setupPagingGrid(); } @@ -67,7 +68,11 @@ public class EmployeesListView extends Main { } private void navigateToEditView(final Employee employee) { - getUI().ifPresent(ui -> ui.navigate(EmployeeView.class, employee.getId().toString())); + getUI().ifPresent(ui -> ui.navigate(EmployeeView.class, employee.getId().toString() + "/edit")); + } + + private void navigateToEmployeeView(final Employee employee) { + getUI().ifPresent(ui -> ui.navigate(EmployeeView.class, employee.getId().toString() + "/view")); } private void navigateToAddEmployeeView() { -- 2.34.1 From ccb36294098346c4c3897033d40905b45213517d Mon Sep 17 00:00:00 2001 From: "melina.gutierrez" Date: Sun, 8 Sep 2024 13:08:41 +0000 Subject: [PATCH 2/3] =?UTF-8?q?#4-Registro-de-Informaci=C3=B3n-Personal=20?= =?UTF-8?q?(#16)?= 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)); } -- 2.34.1 From 1f429221d451d29d294ed6f677ccd0769d4d7ab0 Mon Sep 17 00:00:00 2001 From: Alex Prudencio Date: Sat, 7 Sep 2024 20:22:55 -0400 Subject: [PATCH 3/3] ldap employee updates --- pom.xml | 4 ++ .../primefactorsolutions/model/Employee.java | 1 + .../service/AssessmentService.java | 4 +- .../service/CandidateService.java | 4 +- .../service/EmployeeService.java | 48 +++++++++++++++++-- .../service/QuestionService.java | 4 +- .../views/EmployeeView.java | 13 +++-- .../views/EmployeesListView.java | 2 - .../views/MainLayout.java | 18 ++++++- src/main/resources/application.properties | 5 ++ src/main/resources/data.sql | 32 ++++++------- 11 files changed, 100 insertions(+), 35 deletions(-) diff --git a/pom.xml b/pom.xml index cdd0c6d..250c640 100644 --- a/pom.xml +++ b/pom.xml @@ -100,6 +100,10 @@ org.springframework.boot spring-boot-starter-validation + + org.springframework.boot + spring-boot-starter-data-ldap + org.springframework.boot spring-boot-devtools diff --git a/src/main/java/com/primefactorsolutions/model/Employee.java b/src/main/java/com/primefactorsolutions/model/Employee.java index 0e4d33a..4f5a49e 100644 --- a/src/main/java/com/primefactorsolutions/model/Employee.java +++ b/src/main/java/com/primefactorsolutions/model/Employee.java @@ -13,6 +13,7 @@ @NoArgsConstructor @EqualsAndHashCode(callSuper = true) public class Employee extends BaseEntity { + private String userName; private String firstName; private String lastName; private LocalDate birthday; diff --git a/src/main/java/com/primefactorsolutions/service/AssessmentService.java b/src/main/java/com/primefactorsolutions/service/AssessmentService.java index ef865ab..71dd423 100644 --- a/src/main/java/com/primefactorsolutions/service/AssessmentService.java +++ b/src/main/java/com/primefactorsolutions/service/AssessmentService.java @@ -4,7 +4,7 @@ import com.primefactorsolutions.model.*; import com.primefactorsolutions.repositories.AssessmentRepository; import jakarta.persistence.EntityManager; import jakarta.transaction.Transactional; -import lombok.Data; +import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; @@ -18,7 +18,7 @@ import java.util.UUID; import java.util.stream.Collectors; @Service -@Data +@AllArgsConstructor @Slf4j public class AssessmentService { diff --git a/src/main/java/com/primefactorsolutions/service/CandidateService.java b/src/main/java/com/primefactorsolutions/service/CandidateService.java index 4b0108d..4c3fefb 100644 --- a/src/main/java/com/primefactorsolutions/service/CandidateService.java +++ b/src/main/java/com/primefactorsolutions/service/CandidateService.java @@ -2,14 +2,14 @@ package com.primefactorsolutions.service; import com.primefactorsolutions.model.Candidate; import com.primefactorsolutions.repositories.CandidateRepository; -import lombok.Data; +import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; import java.util.List; import java.util.UUID; @Service -@Data +@AllArgsConstructor public class CandidateService { private final CandidateRepository candidateRepository; diff --git a/src/main/java/com/primefactorsolutions/service/EmployeeService.java b/src/main/java/com/primefactorsolutions/service/EmployeeService.java index 993eecd..c30f9b8 100644 --- a/src/main/java/com/primefactorsolutions/service/EmployeeService.java +++ b/src/main/java/com/primefactorsolutions/service/EmployeeService.java @@ -1,23 +1,32 @@ package com.primefactorsolutions.service; import com.primefactorsolutions.model.Employee; +import lombok.AllArgsConstructor; import org.apache.commons.beanutils.BeanComparator; import com.primefactorsolutions.repositories.EmployeeRepository; -import lombok.Data; +import org.springframework.ldap.core.LdapTemplate; +import org.springframework.ldap.support.LdapNameBuilder; import org.springframework.stereotype.Service; +import javax.naming.Name; +import javax.naming.directory.*; import java.util.List; import java.util.UUID; import java.util.Optional; import java.util.Collections; - @Service -@Data +@AllArgsConstructor public class EmployeeService { private final EmployeeRepository employeeRepository; + private final LdapTemplate ldapTemplate; - public EmployeeService(final EmployeeRepository employeeRepository) { - this.employeeRepository = employeeRepository; + public static final String BASE_DN = "dc=primefactorsolutions,dc=com"; + + protected Name buildDn(final Employee employee) { + return LdapNameBuilder.newInstance(BASE_DN) + .add("ou", "users") + .add("uid", employee.getUserName()) + .build(); } public List findEmployees( @@ -42,6 +51,12 @@ public class EmployeeService { } public Employee createOrUpdate(final Employee employee) { + if (employee.getId() == null) { + final Name dn = buildDn(employee); + // ldapClient.bind(dn).attributes(buildAttributes(employee)).execute(); + ldapTemplate.bind(dn, null, buildAttributes(employee)); + } + return employeeRepository.save(employee); } @@ -49,4 +64,27 @@ public class EmployeeService { Optional employee = employeeRepository.findById(id); return employee.orElse(null); } + + private Attributes buildAttributes(final Employee employee) { + final Attributes attrs = new BasicAttributes(); + final BasicAttribute ocattr = new BasicAttribute("objectclass"); + ocattr.add("top"); + ocattr.add("person"); + ocattr.add("organizationalPerson"); + ocattr.add("inetOrgPerson"); + attrs.put(ocattr); + attrs.put("cn", String.format("%s %s", employee.getFirstName(), employee.getLastName())); + attrs.put("sn", String.format("%s %s", employee.getFirstName(), employee.getLastName())); + attrs.put("uid", employee.getUserName()); + attrs.put("userpassword", String.format("%s%s", employee.getUserName(), 123)); + + return attrs; + } + + public void updatePassword(final Employee employee) { + final Attribute attr = new BasicAttribute("userpassword", employee.getUserName() + "123"); + final ModificationItem item = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attr); + + ldapTemplate.modifyAttributes(buildDn(employee), new ModificationItem[] {item}); + } } \ No newline at end of file diff --git a/src/main/java/com/primefactorsolutions/service/QuestionService.java b/src/main/java/com/primefactorsolutions/service/QuestionService.java index 59282b3..81eb4f3 100644 --- a/src/main/java/com/primefactorsolutions/service/QuestionService.java +++ b/src/main/java/com/primefactorsolutions/service/QuestionService.java @@ -2,14 +2,14 @@ package com.primefactorsolutions.service; import com.primefactorsolutions.model.Question; import com.primefactorsolutions.repositories.QuestionRepository; -import lombok.Data; +import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; import java.util.List; import java.util.UUID; @Service -@Data +@AllArgsConstructor public class QuestionService { private final QuestionRepository questionRepository; diff --git a/src/main/java/com/primefactorsolutions/views/EmployeeView.java b/src/main/java/com/primefactorsolutions/views/EmployeeView.java index 6709e6f..8b18f3b 100644 --- a/src/main/java/com/primefactorsolutions/views/EmployeeView.java +++ b/src/main/java/com/primefactorsolutions/views/EmployeeView.java @@ -4,6 +4,7 @@ import com.primefactorsolutions.model.Employee; import com.primefactorsolutions.service.EmployeeService; import com.vaadin.flow.component.Component; import com.vaadin.flow.component.button.Button; +import com.vaadin.flow.component.button.ButtonVariant; import com.vaadin.flow.component.combobox.ComboBox; import com.vaadin.flow.component.html.H2; import com.vaadin.flow.component.html.H3; @@ -44,6 +45,8 @@ public class EmployeeView extends BeanValidationForm implements HasUrl private final EmployeeService employeeService; + // TODO: campo usado para registrar al empleado en LDAP. Este campo podria estar en otro form eventualmente. + private final TextField userName = createTextField("Username: ", 30, true); private final TextField firstName = createTextField("Nombres: ", 30, true); private final TextField lastName = createTextField("Apellidos", 30, true); private final ComboBox status = createStatusComboBox(); @@ -72,10 +75,10 @@ public class EmployeeView extends BeanValidationForm implements HasUrl 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); this.employeeService = employeeService; + saveButton.addThemeVariants(ButtonVariant.LUMO_PRIMARY); configureComponents(); assembleLayout(); @@ -214,8 +217,8 @@ public class EmployeeView extends BeanValidationForm implements HasUrl @Override public void setParameter(final BeforeEvent beforeEvent, final String action) { - RouteParameters params = beforeEvent.getRouteParameters(); - String s = params.get("employeeId").orElse(null); + final RouteParameters params = beforeEvent.getRouteParameters(); + final String s = params.get("employeeId").orElse(null); if ("new".equals(action)) { setEntityWithEnabledSave(new Employee()); @@ -226,6 +229,7 @@ public class EmployeeView extends BeanValidationForm implements HasUrl UUID employeeId = UUID.fromString(s); var employee = employeeService.getEmployee(employeeId); setEntityWithEnabledSave(employee); + if ("edit".equals(action) && !s.isEmpty()) { saveButton.setVisible(true); editButton.setVisible(false); @@ -294,11 +298,10 @@ public class EmployeeView extends BeanValidationForm implements HasUrl upload.setVisible(true); } - @Override protected List getFormComponents() { return List.of( - mt, fs, firstName, lastName, status, birthday, birthCity, maritalStatus, + mt, fs, userName, firstName, lastName, status, birthday, birthCity, maritalStatus, 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 e9f3726..fed246b 100644 --- a/src/main/java/com/primefactorsolutions/views/EmployeesListView.java +++ b/src/main/java/com/primefactorsolutions/views/EmployeesListView.java @@ -85,8 +85,6 @@ public class EmployeesListView extends Main { } private void refreshGrid() { - List employees = employeeService.getEmployeeRepository().findAll(); - table.setItems(employees); table.setPagingDataProvider((page, pageSize) -> fetchEmployees((int) page, pageSize)); } diff --git a/src/main/java/com/primefactorsolutions/views/MainLayout.java b/src/main/java/com/primefactorsolutions/views/MainLayout.java index 212f388..d96d150 100644 --- a/src/main/java/com/primefactorsolutions/views/MainLayout.java +++ b/src/main/java/com/primefactorsolutions/views/MainLayout.java @@ -2,10 +2,13 @@ package com.primefactorsolutions.views; import com.vaadin.flow.component.applayout.AppLayout; import com.vaadin.flow.component.applayout.DrawerToggle; +import com.vaadin.flow.component.button.Button; import com.vaadin.flow.component.html.Footer; import com.vaadin.flow.component.html.H1; import com.vaadin.flow.component.html.Header; import com.vaadin.flow.component.html.Span; +import com.vaadin.flow.component.orderedlayout.FlexComponent; +import com.vaadin.flow.component.orderedlayout.HorizontalLayout; import com.vaadin.flow.component.orderedlayout.Scroller; import com.vaadin.flow.component.sidenav.SideNav; import com.vaadin.flow.component.sidenav.SideNavItem; @@ -37,7 +40,20 @@ public class MainLayout extends AppLayout { viewTitle = new H1(); viewTitle.addClassNames(LumoUtility.FontSize.LARGE, LumoUtility.Margin.NONE); - addToNavbar(true, toggle, viewTitle); + HorizontalLayout + header = + authContext.getAuthenticatedUser(UserDetails.class) + .map(user -> { + Button logout = new Button("Logout", click -> this.authContext.logout()); + Span loggedUser = new Span("Welcome " + user.getUsername()); + HorizontalLayout hl = new HorizontalLayout(loggedUser, logout); + hl.setJustifyContentMode(FlexComponent.JustifyContentMode.END); + return hl; + }).orElseGet(HorizontalLayout::new); + header.setAlignItems(FlexComponent.Alignment.STRETCH); + header.setWidthFull(); + + addToNavbar(true, toggle, viewTitle, header); } private void addDrawerContent() { diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 39a0612..f88aa2f 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -17,6 +17,11 @@ spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.starttls.required=true +spring.ldap.url=ldap://localhost:8389 +spring.ldap.base= +spring.ldap.username= +spring.ldap.password= + spring.ldap.embedded.ldif=classpath:test-server.ldif spring.ldap.embedded.base-dn=dc=primefactorsolutions,dc=com spring.ldap.embedded.port=8389 diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql index 0888a8a..ae045c3 100644 --- a/src/main/resources/data.sql +++ b/src/main/resources/data.sql @@ -9,19 +9,19 @@ insert into ASSESSMENT_QUESTIONS (assessment_id, question_id) values ('46b153f4- insert into ASSESSMENT_QUESTIONS (assessment_id, question_id) values ('46b153f4-23fd-462f-8430-fbe67b83caab', '8a4b213c-ca81-4c38-b56d-d7028c2dde88'); -insert into employee (id, version, first_name, last_name, status) values ('e99b7af5-7d3a-4c0f-b8bc-e8d0388d8fc4', 1, 'Juan', 'Perez Condori', 'INACTIVE'); -insert into employee (id, version, first_name, last_name, status) values ('f6ab3c6d-7078-45f6-9b22-4e37637bfec6', 1, 'Ana', 'Garcia Rojas', 'ACTIVE'); -insert into employee (id, version, first_name, last_name, status) values ('2e2293b1-3f9a-4f3d-abc8-32639b0a5e15', 1, 'Carlos', 'Lopez Mendoza', 'INACTIVE'); -insert into employee (id, version, first_name, last_name, status) values ('4b1c6c35-4627-4b35-b6e9-dc75c68b2c31', 1, 'Maria', 'Fernandez Villca', 'ACTIVE'); -insert into employee (id, version, first_name, last_name, status) values ('afc5c741-f70a-4394-853b-39d51b118927', 1, 'Luis', 'Gutierrez Mamani', 'ACTIVE'); -insert into employee (id, version, first_name, last_name, status) values ('b2436b82-7b9f-4f0d-9463-f2c3173a45c3', 1, 'Laura', 'Martinez Paredes', 'INACTIVE'); -insert into employee (id, version, first_name, last_name, status) values ('6e6a8a4e-9f6b-44eb-8c69-40acfdc86756', 1, 'Roberto', 'Santos Escobar', 'ACTIVE'); -insert into employee (id, version, first_name, last_name, status) values ('36b0d1c6-bdc0-4d98-94bb-08b9bce3f0d5', 1, 'Valeria', 'Morales Ochoa', 'INACTIVE'); -insert into employee (id, version, first_name, last_name, status) values ('5a1c6d80-58b3-43e3-a5a5-24b4a2d1d54a', 1, 'Jorge', 'Ramirez Tapia', 'ACTIVE'); -insert into employee (id, version, first_name, last_name, status) values ('9d6a5b2e-6d0b-4b89-8d6a-d3f3d1bfc047', 1, 'Sandra', 'Torres Huanca', 'ACTIVE'); -insert into employee (id, version, first_name, last_name, status) values ('f8b3e0c0-0d5a-4e5c-bf9d-207b9b5e8279', 1, 'Felipe', 'Quispe Huanca', 'INACTIVE'); -insert into employee (id, version, first_name, last_name, status) values ('cd80e1d0-9a08-44a6-bd63-2c63eaa003d4', 1, 'Gabriela', 'Rivas Arana', 'ACTIVE'); -insert into employee (id, version, first_name, last_name, status) values ('62d3c1b7-815e-4e96-8d7e-f8c4236bca55', 1, 'Oscar', 'Flores Quiroga', 'INACTIVE'); -insert into employee (id, version, first_name, last_name, status) values ('f20b7c5a-5a67-44f0-9ec1-4c1b8e80de05', 1, 'Marta', 'Vargas Soria', 'ACTIVE'); -insert into employee (id, version, first_name, last_name, status) values ('19b5a76e-d7b1-4b76-8b02-4d0748e85809', 1, 'Andres', 'Espinoza Chura', 'INACTIVE'); -insert into employee (id, version, first_name, last_name, status) values ('5c1a7b82-832d-4f24-8377-54b77b91b6a8', 1, 'Carla', 'Villanueva Arce', 'ACTIVE'); \ No newline at end of file +insert into employee (id, version, user_name, first_name, last_name, status) values ('e99b7af5-7d3a-4c0f-b8bc-e8d0388d8fc4', 1, 'jperez', 'Juan', 'Perez Condori', 'INACTIVE'); +insert into employee (id, version, user_name, first_name, last_name, status) values ('f6ab3c6d-7078-45f6-9b22-4e37637bfec6', 1, 'agarcia', 'Ana', 'Garcia Rojas', 'ACTIVE'); +insert into employee (id, version, user_name, first_name, last_name, status) values ('2e2293b1-3f9a-4f3d-abc8-32639b0a5e15', 1, 'clopez', 'Carlos', 'Lopez Mendoza', 'INACTIVE'); +insert into employee (id, version, user_name, first_name, last_name, status) values ('4b1c6c35-4627-4b35-b6e9-dc75c68b2c31', 1, 'mfernandez', 'Maria', 'Fernandez Villca', 'ACTIVE'); +insert into employee (id, version, user_name, first_name, last_name, status) values ('afc5c741-f70a-4394-853b-39d51b118927', 1, 'lgutierrez', 'Luis', 'Gutierrez Mamani', 'ACTIVE'); +insert into employee (id, version, user_name, first_name, last_name, status) values ('b2436b82-7b9f-4f0d-9463-f2c3173a45c3', 1, 'lmartinez', 'Laura', 'Martinez Paredes', 'INACTIVE'); +insert into employee (id, version, user_name, first_name, last_name, status) values ('6e6a8a4e-9f6b-44eb-8c69-40acfdc86756', 1, 'rsantos', 'Roberto', 'Santos Escobar', 'ACTIVE'); +insert into employee (id, version, user_name, first_name, last_name, status) values ('36b0d1c6-bdc0-4d98-94bb-08b9bce3f0d5', 1, 'vmorales', 'Valeria', 'Morales Ochoa', 'INACTIVE'); +insert into employee (id, version, user_name, first_name, last_name, status) values ('5a1c6d80-58b3-43e3-a5a5-24b4a2d1d54a', 1, 'jramirez', 'Jorge', 'Ramirez Tapia', 'ACTIVE'); +insert into employee (id, version, user_name, first_name, last_name, status) values ('9d6a5b2e-6d0b-4b89-8d6a-d3f3d1bfc047', 1, 'storres', 'Sandra', 'Torres Huanca', 'ACTIVE'); +insert into employee (id, version, user_name, first_name, last_name, status) values ('f8b3e0c0-0d5a-4e5c-bf9d-207b9b5e8279', 1, 'fquispe', 'Felipe', 'Quispe Huanca', 'INACTIVE'); +insert into employee (id, version, user_name, first_name, last_name, status) values ('cd80e1d0-9a08-44a6-bd63-2c63eaa003d4', 1, 'grivas', 'Gabriela', 'Rivas Arana', 'ACTIVE'); +insert into employee (id, version, user_name, first_name, last_name, status) values ('62d3c1b7-815e-4e96-8d7e-f8c4236bca55', 1, 'oflores', 'Oscar', 'Flores Quiroga', 'INACTIVE'); +insert into employee (id, version, user_name, first_name, last_name, status) values ('f20b7c5a-5a67-44f0-9ec1-4c1b8e80de05', 1, 'mvargas', 'Marta', 'Vargas Soria', 'ACTIVE'); +insert into employee (id, version, user_name, first_name, last_name, status) values ('19b5a76e-d7b1-4b76-8b02-4d0748e85809', 1, 'aespinoza', 'Andres', 'Espinoza Chura', 'INACTIVE'); +insert into employee (id, version, user_name, first_name, last_name, status) values ('5c1a7b82-832d-4f24-8377-54b77b91b6a8', 1, 'cvillanueva', 'Carla', 'Villanueva Arce', 'ACTIVE'); \ No newline at end of file -- 2.34.1