Implementar funcionalidad para descargar reporte de las solicitudes de vacaciones rechazadas
All checks were successful
PR Builder / Build-PR (pull_request) Successful in 0s

This commit is contained in:
jesus.pelaez 2024-11-28 01:58:06 -04:00
parent e8152308fe
commit 6ac9d4c305

View File

@ -8,6 +8,8 @@ import com.primefactorsolutions.views.util.MenuBarUtils;
import com.vaadin.flow.component.ClickEvent; import com.vaadin.flow.component.ClickEvent;
import com.vaadin.flow.component.Component; import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.ComponentEventListener; import com.vaadin.flow.component.ComponentEventListener;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.combobox.ComboBox; import com.vaadin.flow.component.combobox.ComboBox;
import com.vaadin.flow.component.contextmenu.MenuItem; import com.vaadin.flow.component.contextmenu.MenuItem;
import com.vaadin.flow.component.icon.VaadinIcon; import com.vaadin.flow.component.icon.VaadinIcon;
@ -17,11 +19,23 @@ import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.function.ValueProvider; import com.vaadin.flow.function.ValueProvider;
import com.vaadin.flow.router.PageTitle; import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route; import com.vaadin.flow.router.Route;
import com.vaadin.flow.server.StreamRegistration;
import com.vaadin.flow.server.StreamResource;
import com.vaadin.flow.spring.annotation.SpringComponent; import com.vaadin.flow.spring.annotation.SpringComponent;
import jakarta.annotation.security.PermitAll; import jakarta.annotation.security.PermitAll;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.Scope;
import org.vaadin.firitin.components.grid.PagingGrid; import org.vaadin.firitin.components.grid.PagingGrid;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.time.LocalDate;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -54,6 +68,8 @@ public class PendingRequestsListView extends BaseView {
} }
private void initializeView() { private void initializeView() {
Button downloadButton = new Button("Descargar reporte de rechazos", event -> downloadReport());
getCurrentPageLayout().add(downloadButton);
setupFilters(); setupFilters();
setupPendingRequestsGrid(); setupPendingRequestsGrid();
} }
@ -221,4 +237,50 @@ public class PendingRequestsListView extends BaseView {
allTeamsOption.setName("TODOS"); allTeamsOption.setName("TODOS");
return allTeamsOption; return allTeamsOption;
} }
private void downloadReport() {
StreamResource resource = generateGeneralVacationReport();
getUI().ifPresent(ui -> openDocumentStream(resource, ui));
}
private StreamResource generateGeneralVacationReport() {
List<TimeOffRequest> requests = requestService.findAllTimeOffRequests().stream()
.filter(request -> request.getState() == TimeOffRequestStatus.RECHAZADO)
.collect(Collectors.toList());
ByteArrayInputStream excelStream = generateExcelReport(requests);
return new StreamResource("reporte_de_solicitudes_rechazadas_" + LocalDate.now() + ".xlsx",
() -> excelStream);
}
private void openDocumentStream(final StreamResource resource, final UI ui) {
StreamRegistration registration = ui.getSession().getResourceRegistry().registerResource(resource);
ui.getPage().open(registration.getResourceUri().toString());
}
private ByteArrayInputStream generateExcelReport(final List<TimeOffRequest> requests) {
try (Workbook workbook = new XSSFWorkbook()) {
Sheet sheet = workbook.createSheet("REPORTE_DE_SOLICITUDES_DE_VACACIONES_RECHAZADAS");
Row headerRow = sheet.createRow(0);
String[] headers = {"Empleado", "Categoria", "Observaciones"};
for (int i = 0; i < headers.length; i++) {
Cell cell = headerRow.createCell(i);
cell.setCellValue(headers[i]);
}
int rowIndex = 1;
for (TimeOffRequest request : requests) {
Row row = sheet.createRow(rowIndex++);
row.createCell(0).setCellValue(getEmployeeFullName(request));
row.createCell(1).setCellValue(getCategory(request));
}
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
workbook.write(out);
return new ByteArrayInputStream(out.toByteArray());
}
} catch (IOException e) {
throw new UncheckedIOException("Error al generar el archivo Excel", e);
}
}
} }