Perfil-Empleado-Administrativo-Documentacion #39

Merged
jesus.pelaez merged 10 commits from Perfil-Empleado-Administrativo-Documentacion into En-desarrollo 2024-09-27 07:31:15 +00:00
Showing only changes of commit af3f13773b - Show all commits

View File

@ -18,6 +18,7 @@ import jakarta.annotation.security.PermitAll;
import org.springframework.context.annotation.Scope;
import org.vaadin.firitin.components.grid.PagingGrid;
import java.util.Base64;
import java.util.List;
@ -52,7 +53,7 @@ public class DocumentsListView extends Main {
documentGrid.addComponentColumn(this::createEmployeeSpan).setHeader("Employee");
addDocumentActionColumn("View", this::navigateToDocumentView);
addDocumentActionColumn("Edit", this::navigateToEditDocumentView);
addDocumentActionColumn("Download", this::navigateToDownloadDocumentView);
addDocumentActionColumn("Download", this::downloadDocument);
configurePagination();
}
@ -136,6 +137,26 @@ public class DocumentsListView extends Main {
sortOrder.getDirection() == SortDirection.ASCENDING);
}
private void downloadDocument(final Document document) {
String base64Data = Base64.getEncoder().encodeToString(document.getFileData());
String jsCode = "var byteCharacters = atob('" + base64Data + "');" +
"var byteNumbers = new Array(byteCharacters.length);" +
"for (var i = 0; i < byteCharacters.length; i++) {" +
" byteNumbers[i] = byteCharacters.charCodeAt(i);" +
"}" +
"var byteArray = new Uint8Array(byteNumbers);" +
"var blob = new Blob([byteArray], { type: 'application/pdf' });" +
"var url = URL.createObjectURL(blob);" +
"var a = document.createElement('a');" +
"a.href = url;" +
"a.download = '" + document.getFileName() + "';" +
"document.body.appendChild(a);" +
"a.click();" +
"document.body.removeChild(a);" +
"URL.revokeObjectURL(url);";
getElement().executeJs(jsCode);
}
@FunctionalInterface
private interface DocumentActionHandler {
void handle(Document document);