From 35b98e919ca316c3d17bc281ef30085127cbab50 Mon Sep 17 00:00:00 2001 From: ricardo051199 Date: Wed, 4 Sep 2024 16:03:46 -0400 Subject: [PATCH 1/3] =?UTF-8?q?#4=20Perfil=20de=20Empleado=20-=20Registro?= =?UTF-8?q?=20de=20Informaci=C3=B3n=20Personal=20(Correccion=20guardado=20?= =?UTF-8?q?del=20campo=20estado)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../primefactorsolutions/views/EmployeeView.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/primefactorsolutions/views/EmployeeView.java b/src/main/java/com/primefactorsolutions/views/EmployeeView.java index 9e49fe4..054ae7d 100644 --- a/src/main/java/com/primefactorsolutions/views/EmployeeView.java +++ b/src/main/java/com/primefactorsolutions/views/EmployeeView.java @@ -38,10 +38,10 @@ public class EmployeeView extends BeanValidationForm implements HasUrl private final TextField firstName; private final TextField lastName; - private final ComboBox status; + private final ComboBox status; private final VDatePicker birthday; private final TextField birthCity; - private final ComboBox maritalStatus; + private final ComboBox maritalStatus; private final TextField residenceAddress; private final TextField phoneNumber; private final EmailField personalEmail; @@ -79,9 +79,9 @@ public class EmployeeView extends BeanValidationForm implements HasUrl lastName.setRequired(true); status = new ComboBox<>("Estado"); - status.setItems(List.of("ACTIVE", "INACTIVE")); - lastName.setWidthFull(); - lastName.setMaxLength(30); + status.setItems(Employee.Status.values()); + status.setItemLabelGenerator(Employee.Status::name); + status.setWidthFull(); status.setRequired(true); birthday = new VDatePicker("Fecha de Nacimiento"); @@ -92,7 +92,8 @@ public class EmployeeView extends BeanValidationForm implements HasUrl birthCity.setMaxLength(20); maritalStatus = new ComboBox<>("Estado Civil"); - maritalStatus.setItems(List.of("Soltero", "Casado", "Viudo", "Divorciado")); + maritalStatus.setItems(Employee.MaritalStatus.values()); + maritalStatus.setItemLabelGenerator(Employee.MaritalStatus::name); maritalStatus.setWidthFull(); residenceAddress = new TextField("Dirección de Residencia"); @@ -144,6 +145,7 @@ public class EmployeeView extends BeanValidationForm implements HasUrl setSavedHandler((SavedHandler) employee -> { if (validateForm()) { + employee.setStatus(status.getValue()); // Actualiza el estado del empleado antes de guardar final Employee saved = employeeService.createOrUpdate(employee); Notification.show("Employee saved successfully."); getUI().ifPresent(ui -> ui.navigate(EmployeesListView.class)); @@ -184,6 +186,7 @@ public class EmployeeView extends BeanValidationForm implements HasUrl if (StringUtils.isNotBlank(s) && !"new".equals(s)) { var employee = employeeService.getEmployee(UUID.fromString(s)); setEntityWithEnabledSave(employee); + status.setValue(employee.getStatus()); // Asegúrate de que el ComboBox esté sincronizado con el estado del empleado } else { setEntityWithEnabledSave(new Employee()); } -- 2.34.1 From 828ed4aac10191186dfd7e10d2a52ced160e4ea4 Mon Sep 17 00:00:00 2001 From: ricardo051199 Date: Thu, 5 Sep 2024 00:03:03 -0400 Subject: [PATCH 2/3] =?UTF-8?q?#5,=20#9=20Perfil=20de=20Personal=20Adminis?= =?UTF-8?q?trativo=20-=20A=C3=B1adir=20Empleado,=20Guardado=20de=20Informa?= =?UTF-8?q?ci=C3=B3n=20Personal?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../views/EmployeeView.java | 305 +++++++++--------- .../views/EmployeesListView.java | 7 +- 2 files changed, 155 insertions(+), 157 deletions(-) diff --git a/src/main/java/com/primefactorsolutions/views/EmployeeView.java b/src/main/java/com/primefactorsolutions/views/EmployeeView.java index 054ae7d..36f055e 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,170 +27,64 @@ import java.util.UUID; @PermitAll @Scope("prototype") @PageTitle("Employee") -@Route(value = "/employees", layout = MainLayout.class) +@Route(value = "/employees/:action?/:employeeId?", 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(Employee.Status.values()); - status.setItemLabelGenerator(Employee.Status::name); - status.setWidthFull(); - 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(Employee.MaritalStatus.values()); - maritalStatus.setItemLabelGenerator(Employee.MaritalStatus::name); - 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()) { - employee.setStatus(status.getValue()); // Actualiza el estado del empleado antes de guardar - 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); - status.setValue(employee.getStatus()); // Asegúrate de que el ComboBox esté sincronizado con el estado del empleado - } 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, @@ -205,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 s) { + RouteParameters params = beforeEvent.getRouteParameters(); + String action = params.get("action").orElse("new"); + + 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..f1cc6e5 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, "edit/" + employee.getId().toString())); + } + + private void navigateToEmployeeView(final Employee employee) { + getUI().ifPresent(ui -> ui.navigate(EmployeeView.class, "view/" + employee.getId().toString())); } private void navigateToAddEmployeeView() { -- 2.34.1 From b88b680e4e82cfdb4f0ac50ebf41fb690866e17f Mon Sep 17 00:00:00 2001 From: ricardo051199 Date: Thu, 5 Sep 2024 19:54:47 -0400 Subject: [PATCH 3/3] #5, #9 Perfil de Personal Administrativo - Cambiar formato de ruta --- .../java/com/primefactorsolutions/views/EmployeeView.java | 6 +++--- .../com/primefactorsolutions/views/EmployeesListView.java | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/primefactorsolutions/views/EmployeeView.java b/src/main/java/com/primefactorsolutions/views/EmployeeView.java index 36f055e..15b272d 100644 --- a/src/main/java/com/primefactorsolutions/views/EmployeeView.java +++ b/src/main/java/com/primefactorsolutions/views/EmployeeView.java @@ -27,7 +27,7 @@ import java.util.UUID; @PermitAll @Scope("prototype") @PageTitle("Employee") -@Route(value = "/employees/:action?/:employeeId?", 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"; @@ -163,9 +163,9 @@ public class EmployeeView extends BeanValidationForm implements HasUrl } @Override - public void setParameter(final BeforeEvent beforeEvent, final String s) { + public void setParameter(final BeforeEvent beforeEvent, final String action) { RouteParameters params = beforeEvent.getRouteParameters(); - String action = params.get("action").orElse("new"); + String s = params.get("employeeId").orElse(null); if ("new".equals(action)) { setEntityWithEnabledSave(new Employee()); diff --git a/src/main/java/com/primefactorsolutions/views/EmployeesListView.java b/src/main/java/com/primefactorsolutions/views/EmployeesListView.java index f1cc6e5..fed246b 100644 --- a/src/main/java/com/primefactorsolutions/views/EmployeesListView.java +++ b/src/main/java/com/primefactorsolutions/views/EmployeesListView.java @@ -68,11 +68,11 @@ public class EmployeesListView extends Main { } private void navigateToEditView(final Employee employee) { - getUI().ifPresent(ui -> ui.navigate(EmployeeView.class, "edit/" + 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, "view/" + employee.getId().toString())); + getUI().ifPresent(ui -> ui.navigate(EmployeeView.class, employee.getId().toString() + "/view")); } private void navigateToAddEmployeeView() { -- 2.34.1