#30 Perfil de Usuario Administrativo - Añadir NUEVO Documento (agregar creador del registro del doc)

This commit is contained in:
jesus.pelaez 2024-09-24 20:44:16 -04:00
parent af3f13773b
commit f0b41743c7
3 changed files with 28 additions and 17 deletions

View File

@ -19,4 +19,5 @@ public class Document extends BaseEntity {
@Lob @Lob
@Column(columnDefinition = "BLOB") @Column(columnDefinition = "BLOB")
private byte[] fileData; private byte[] fileData;
private String creator;
} }

View File

@ -17,7 +17,9 @@ import com.vaadin.flow.router.*;
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.springframework.context.annotation.Scope; import org.springframework.context.annotation.Scope;
import org.springframework.security.core.userdetails.UserDetails;
import org.vaadin.firitin.form.BeanValidationForm; import org.vaadin.firitin.form.BeanValidationForm;
import com.vaadin.flow.spring.security.AuthenticationContext;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
@ -38,12 +40,16 @@ public class DocumentView extends BeanValidationForm<Document> implements HasUrl
private final Upload uploadButton = new Upload(buffer); private final Upload uploadButton = new Upload(buffer);
private final DocumentService documentService; private final DocumentService documentService;
private final EmployeeService employeeService; private final EmployeeService employeeService;
private final AuthenticationContext authContext;
private boolean fileUploaded = false; 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); super(Document.class);
this.documentService = documentService; this.documentService = documentService;
this.employeeService = employeeService; this.employeeService = employeeService;
this.authContext = authContext;
initializeView(); initializeView();
} }
@ -106,6 +112,10 @@ public class DocumentView extends BeanValidationForm<Document> implements HasUrl
if (isFormValid()) { if (isFormValid()) {
Document document = createDocument(); Document document = createDocument();
document.setEmployee(employeeComboBox.getValue()); 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); documentService.saveDocument(document);
Notification.show("File saved successfully."); Notification.show("File saved successfully.");
clearForm(); clearForm();

View File

@ -49,7 +49,7 @@ public class DocumentsListView extends Main {
} }
private void configureDocumentGrid() { private void configureDocumentGrid() {
documentGrid.setColumns("fileName", "documentType"); documentGrid.setColumns("fileName", "documentType", "creator");
documentGrid.addComponentColumn(this::createEmployeeSpan).setHeader("Employee"); documentGrid.addComponentColumn(this::createEmployeeSpan).setHeader("Employee");
addDocumentActionColumn("View", this::navigateToDocumentView); addDocumentActionColumn("View", this::navigateToDocumentView);
addDocumentActionColumn("Edit", this::navigateToEditDocumentView); addDocumentActionColumn("Edit", this::navigateToEditDocumentView);
@ -139,21 +139,21 @@ public class DocumentsListView extends Main {
private void downloadDocument(final Document document) { private void downloadDocument(final Document document) {
String base64Data = Base64.getEncoder().encodeToString(document.getFileData()); String base64Data = Base64.getEncoder().encodeToString(document.getFileData());
String jsCode = "var byteCharacters = atob('" + base64Data + "');" + String jsCode = "var byteCharacters = atob('" + base64Data + "');"
"var byteNumbers = new Array(byteCharacters.length);" + + "var byteNumbers = new Array(byteCharacters.length);"
"for (var i = 0; i < byteCharacters.length; i++) {" + + "for (var i = 0; i < byteCharacters.length; i++) {"
" byteNumbers[i] = byteCharacters.charCodeAt(i);" + + " byteNumbers[i] = byteCharacters.charCodeAt(i);"
"}" + + "}"
"var byteArray = new Uint8Array(byteNumbers);" + + "var byteArray = new Uint8Array(byteNumbers);"
"var blob = new Blob([byteArray], { type: 'application/pdf' });" + + "var blob = new Blob([byteArray], { type: 'application/pdf' });"
"var url = URL.createObjectURL(blob);" + + "var url = URL.createObjectURL(blob);"
"var a = document.createElement('a');" + + "var a = document.createElement('a');"
"a.href = url;" + + "a.href = url;"
"a.download = '" + document.getFileName() + "';" + + "a.download = '" + document.getFileName() + "';"
"document.body.appendChild(a);" + + "document.body.appendChild(a);"
"a.click();" + + "a.click();"
"document.body.removeChild(a);" + + "document.body.removeChild(a);"
"URL.revokeObjectURL(url);"; + "URL.revokeObjectURL(url);";
getElement().executeJs(jsCode); getElement().executeJs(jsCode);
} }