#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
@Column(columnDefinition = "BLOB")
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 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<Document> 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<Document> 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();

View File

@ -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);
}