From 4241e7b52b1d7f9958aa6b70d5d8c4f733dca0f5 Mon Sep 17 00:00:00 2001 From: ricardo051199 Date: Wed, 25 Sep 2024 18:50:59 -0400 Subject: [PATCH] #29 Perfil de Empleado Administrativo - LISTAR Documentos (correccion download, view, edit) --- .../views/DocumentView.java | 30 +++++++++++++++---- .../views/DocumentsListView.java | 29 ++++++++---------- 2 files changed, 37 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/primefactorsolutions/views/DocumentView.java b/src/main/java/com/primefactorsolutions/views/DocumentView.java index fdf29d5..fe8d8a5 100644 --- a/src/main/java/com/primefactorsolutions/views/DocumentView.java +++ b/src/main/java/com/primefactorsolutions/views/DocumentView.java @@ -42,6 +42,7 @@ public class DocumentView extends BeanValidationForm implements HasUrl private final EmployeeService employeeService; private final AuthenticationContext authContext; private boolean fileUploaded = false; + private Button saveButton; public DocumentView(final DocumentService documentService, final EmployeeService employeeService, @@ -83,7 +84,7 @@ public class DocumentView extends BeanValidationForm implements HasUrl } protected Button createSaveButton() { - Button saveButton = new Button("Save"); + saveButton = new Button("Save"); saveButton.addClickListener(event -> saveDocument()); return saveButton; } @@ -110,13 +111,18 @@ public class DocumentView extends BeanValidationForm implements HasUrl private void saveDocument() { if (isFormValid()) { - Document document = createDocument(); + Document document = getEntity(); // Obtenemos el documento actual del formulario + document.setFileName(fileName.getValue()); + document.setDocumentType(documentType.getValue()); document.setEmployee(employeeComboBox.getValue()); + document.setFileData(readFileData()); // Actualizamos el archivo si es necesario + authContext.getAuthenticatedUser(UserDetails.class).ifPresent(user -> { String creator = user.getUsername(); - document.setCreator(creator); // Asignar el nombre del creador al documento + document.setCreator(creator); // Asignar el nombre del creador al documento si corresponde }); - documentService.saveDocument(document); + + documentService.saveDocument(document); // Guardamos el documento actualizado Notification.show("File saved successfully."); clearForm(); } else { @@ -155,6 +161,18 @@ public class DocumentView extends BeanValidationForm implements HasUrl fileUploaded = false; } + private void checkFormModifications() { + boolean hasModifications = !fileName.isEmpty() + && documentType.getValue() != null + && employeeComboBox.getValue() != null; + + if (hasModifications || fileUploaded) { + saveButton.setEnabled(true); + } else { + saveButton.setEnabled(false); + } + } + @Override public void setParameter(final BeforeEvent beforeEvent, final String action) { @@ -166,7 +184,8 @@ public class DocumentView extends BeanValidationForm implements HasUrl } else { UUID documentId = UUID.fromString(s); var document = documentService.getDocument(documentId); - setEntityWithEnabledSave(document); + setEntity(document); + employeeComboBox.setValue(document.getEmployee()); if ("edit".equals(action) && !s.isEmpty()) { setFieldsReadOnly(false); @@ -174,6 +193,7 @@ public class DocumentView extends BeanValidationForm implements HasUrl setFieldsReadOnly(true); } } + checkFormModifications(); } private void setFieldsReadOnly(final boolean option) { diff --git a/src/main/java/com/primefactorsolutions/views/DocumentsListView.java b/src/main/java/com/primefactorsolutions/views/DocumentsListView.java index 64c5f9f..7159229 100644 --- a/src/main/java/com/primefactorsolutions/views/DocumentsListView.java +++ b/src/main/java/com/primefactorsolutions/views/DocumentsListView.java @@ -8,16 +8,20 @@ import com.primefactorsolutions.service.EmployeeService; import com.vaadin.flow.component.button.Button; import com.vaadin.flow.component.combobox.ComboBox; import com.vaadin.flow.component.grid.GridSortOrder; +import com.vaadin.flow.component.html.Anchor; import com.vaadin.flow.component.html.Main; import com.vaadin.flow.component.html.Span; import com.vaadin.flow.data.provider.SortDirection; import com.vaadin.flow.router.PageTitle; 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 jakarta.annotation.security.PermitAll; import org.springframework.context.annotation.Scope; import org.vaadin.firitin.components.grid.PagingGrid; +import java.io.ByteArrayInputStream; import java.util.Base64; import java.util.List; @@ -138,23 +142,14 @@ public class DocumentsListView extends Main { } 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); + StreamResource resource = new StreamResource(document.getFileName(), + () -> new ByteArrayInputStream(document.getFileData())); + resource.setContentType("application/pdf"); + resource.setHeader("Content-Disposition", "attachment; filename=\"" + document.getFileName() + ".pdf\""); + getUI().ifPresent(ui -> { + StreamRegistration registration = ui.getSession().getResourceRegistry().registerResource(resource); + ui.getPage().open(registration.getResourceUri().toString()); + }); } @FunctionalInterface