diff --git a/src/main/java/com/primefactorsolutions/model/Document.java b/src/main/java/com/primefactorsolutions/model/Document.java index 2e3b222..61620fa 100644 --- a/src/main/java/com/primefactorsolutions/model/Document.java +++ b/src/main/java/com/primefactorsolutions/model/Document.java @@ -19,4 +19,5 @@ public class Document extends BaseEntity { @Lob @Column(columnDefinition = "BLOB") private byte[] fileData; + private String creator; } diff --git a/src/main/java/com/primefactorsolutions/views/DocumentView.java b/src/main/java/com/primefactorsolutions/views/DocumentView.java index b2b84ca..fdf29d5 100644 --- a/src/main/java/com/primefactorsolutions/views/DocumentView.java +++ b/src/main/java/com/primefactorsolutions/views/DocumentView.java @@ -17,7 +17,9 @@ import com.vaadin.flow.router.*; import com.vaadin.flow.spring.annotation.SpringComponent; import jakarta.annotation.security.PermitAll; import org.springframework.context.annotation.Scope; +import org.springframework.security.core.userdetails.UserDetails; import org.vaadin.firitin.form.BeanValidationForm; +import com.vaadin.flow.spring.security.AuthenticationContext; import java.io.IOException; import java.util.List; @@ -38,12 +40,16 @@ public class DocumentView extends BeanValidationForm implements HasUrl private final Upload uploadButton = new Upload(buffer); private final DocumentService documentService; private final EmployeeService employeeService; + private final AuthenticationContext authContext; private boolean fileUploaded = false; - public DocumentView(final DocumentService documentService, final EmployeeService employeeService) { + public DocumentView(final DocumentService documentService, + final EmployeeService employeeService, + final AuthenticationContext authContext) { super(Document.class); this.documentService = documentService; this.employeeService = employeeService; + this.authContext = authContext; initializeView(); } @@ -106,6 +112,10 @@ public class DocumentView extends BeanValidationForm implements HasUrl if (isFormValid()) { Document document = createDocument(); document.setEmployee(employeeComboBox.getValue()); + authContext.getAuthenticatedUser(UserDetails.class).ifPresent(user -> { + String creator = user.getUsername(); + document.setCreator(creator); // Asignar el nombre del creador al documento + }); documentService.saveDocument(document); Notification.show("File saved successfully."); clearForm(); diff --git a/src/main/java/com/primefactorsolutions/views/DocumentsListView.java b/src/main/java/com/primefactorsolutions/views/DocumentsListView.java index 229d333..64c5f9f 100644 --- a/src/main/java/com/primefactorsolutions/views/DocumentsListView.java +++ b/src/main/java/com/primefactorsolutions/views/DocumentsListView.java @@ -49,7 +49,7 @@ public class DocumentsListView extends Main { } private void configureDocumentGrid() { - documentGrid.setColumns("fileName", "documentType"); + documentGrid.setColumns("fileName", "documentType", "creator"); documentGrid.addComponentColumn(this::createEmployeeSpan).setHeader("Employee"); addDocumentActionColumn("View", this::navigateToDocumentView); addDocumentActionColumn("Edit", this::navigateToEditDocumentView); @@ -139,21 +139,21 @@ 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);"; + 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); }