From a5e166351a7e2f8469de68235f18bd5590510773 Mon Sep 17 00:00:00 2001 From: ricardo051199 Date: Mon, 21 Oct 2024 12:39:28 -0400 Subject: [PATCH] =?UTF-8?q?#55=20Perfil=20de=20Personal=20Administrativo?= =?UTF-8?q?=20-=20A=C3=B1adir=20Vacaciones=20del=20Empleado=20(Refactoriza?= =?UTF-8?q?cion=20configuracion=20form)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../views/RequestRegisterView.java | 119 +++++++++--------- 1 file changed, 59 insertions(+), 60 deletions(-) diff --git a/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java b/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java index 4fc4122..c4e0fd1 100644 --- a/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java +++ b/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java @@ -63,22 +63,11 @@ public class RequestRegisterView extends VerticalLayout { } private void configureFormFields() { - List employees = employeeService.findAllEmployees(); - employeeComboBox.setItems(employees); + employeeComboBox.setItems(employeeService.findAllEmployees()); employeeComboBox.setItemLabelGenerator(emp -> emp.getFirstName() + " " + emp.getLastName()); + employeeComboBox.addValueChangeListener(event -> handleEmployeeSelection(event.getValue())); categoryComboBox.setEnabled(false); - categoryComboBox.addValueChangeListener(event -> { - clearForm(); - updateAvailableDays(event.getValue()); - }); - employeeComboBox.addValueChangeListener(event -> { - clearForm(); - Employee selectedEmployee = event.getValue(); - if (event.getValue() != null) { - categoryComboBox.setEnabled(true); - filterCategories(selectedEmployee); - } - }); + categoryComboBox.addValueChangeListener(event -> handleCategorySelection(event.getValue())); startDatePicker.addValueChangeListener(event -> updateDatePickerMinValues()); endDatePicker.addValueChangeListener(event -> calculateDays()); availableDaysField.setReadOnly(true); @@ -86,7 +75,44 @@ public class RequestRegisterView extends VerticalLayout { balanceDaysField.setReadOnly(true); } - private void filterCategories(Employee employee) { + private void configureBinder() { + binder.forField(employeeComboBox) + .asRequired("Employee is required") + .bind(TimeOffRequest::getEmployee, TimeOffRequest::setEmployee); + + binder.forField(categoryComboBox) + .asRequired("Category is required") + .bind(TimeOffRequest::getCategory, TimeOffRequest::setCategory); + + binder.forField(availableDaysField) + .bind(TimeOffRequest::getAvailableDays, TimeOffRequest::setAvailableDays); + + binder.forField(startDatePicker) + .asRequired("Start date is required") + .bind(TimeOffRequest::getStartDate, TimeOffRequest::setStartDate); + + binder.forField(endDatePicker) + .asRequired("End date is required") + .bind(TimeOffRequest::getEndDate, TimeOffRequest::setEndDate); + + binder.forField(daysToBeTakenField) + .bind(TimeOffRequest::getDaysToBeTake, TimeOffRequest::setDaysToBeTake); + + binder.forField(balanceDaysField) + .bind(TimeOffRequest::getDaysBalance, TimeOffRequest::setDaysBalance); + + binder.setBean(new TimeOffRequest()); + } + + private void handleEmployeeSelection(final Employee selectedEmployee) { + clearForm(); + if (selectedEmployee != null) { + categoryComboBox.setEnabled(true); + filterCategories(selectedEmployee); + } + } + + private void filterCategories(final Employee employee) { List employeeRequests = requestService.findRequestsByEmployeeId(employee.getId()); List requestedCategories = employeeRequests.stream() .map(TimeOffRequest::getCategory) @@ -95,37 +121,39 @@ public class RequestRegisterView extends VerticalLayout { .filter(category -> !requestedCategories.contains(category)) .filter(category -> { if (employee.getGender() == Employee.Gender.MALE) { - return category != TimeOffRequestType.MATERNITY && - category != TimeOffRequestType.MOTHERS_DAY && - category != TimeOffRequestType.INTERNATIONAL_WOMENS_DAY && - category != TimeOffRequestType.NATIONAL_WOMENS_DAY; + return category != TimeOffRequestType.MATERNITY + && category != TimeOffRequestType.MOTHERS_DAY + && category != TimeOffRequestType.INTERNATIONAL_WOMENS_DAY + && category != TimeOffRequestType.NATIONAL_WOMENS_DAY; } else { - return category != TimeOffRequestType.FATHERS_DAY && - category != TimeOffRequestType.PATERNITY; + return category != TimeOffRequestType.FATHERS_DAY + && category != TimeOffRequestType.PATERNITY; } }) .toList(); categoryComboBox.setItems(availableCategories); } - private void updateAvailableDays(TimeOffRequestType selectedCategory) { + private void handleCategorySelection(final TimeOffRequestType selectedCategory) { + clearForm(); if (selectedCategory != null) { - vacation = vacationService.findVacationByCategory(selectedCategory); - if (vacation != null) { - availableDaysField.setValue(vacation.getDuration()); - setDatePickerLimits(vacation); - } + updateAvailableDays(selectedCategory); } } - private void setDatePickerLimits(Vacation vacation) { + private void updateAvailableDays(final TimeOffRequestType selectedCategory) { + vacation = vacationService.findVacationByCategory(selectedCategory); + if (vacation != null) { + availableDaysField.setValue(vacation.getDuration()); + setDatePickerLimits(vacation); + } + } + + private void setDatePickerLimits(final Vacation vacation) { LocalDate startDate; if (vacation.getVacationDate() != null) { startDate = vacation.getVacationDate(); endDate = startDate.plusDays(vacation.getExpiration().intValue() - 1); - startDatePicker.setValue(startDate); - endDatePicker.setValue(startDate.plusDays(vacation.getDuration().intValue() - 1)); - } else { startDate = LocalDate.now(); endDate = null; @@ -187,35 +215,6 @@ public class RequestRegisterView extends VerticalLayout { closeButton = new Button("Close", event -> closeForm()); } - private void configureBinder() { - binder.forField(employeeComboBox) - .asRequired("Employee is required") - .bind(TimeOffRequest::getEmployee, TimeOffRequest::setEmployee); - - binder.forField(categoryComboBox) - .asRequired("Category is required") - .bind(TimeOffRequest::getCategory, TimeOffRequest::setCategory); - - binder.forField(availableDaysField) - .bind(TimeOffRequest::getAvailableDays, TimeOffRequest::setAvailableDays); - - binder.forField(startDatePicker) - .asRequired("Start date is required") - .bind(TimeOffRequest::getStartDate, TimeOffRequest::setStartDate); - - binder.forField(endDatePicker) - .asRequired("End date is required") - .bind(TimeOffRequest::getEndDate, TimeOffRequest::setEndDate); - - binder.forField(daysToBeTakenField) - .bind(TimeOffRequest::getDaysToBeTake, TimeOffRequest::setDaysToBeTake); - - binder.forField(balanceDaysField) - .bind(TimeOffRequest::getDaysBalance, TimeOffRequest::setDaysBalance); - - binder.setBean(new TimeOffRequest()); - } - private void setupFormLayout() { add( new H3("Add Vacation Request"),