En-desarrollo #27

Merged
alex merged 39 commits from En-desarrollo into main 2024-10-07 18:23:34 +00:00
3 changed files with 28 additions and 17 deletions
Showing only changes of commit f0b41743c7 - Show all commits

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