Perfil-Empleado-Administrativo-Documentacion #39
@ -42,6 +42,7 @@ public class DocumentView extends BeanValidationForm<Document> implements HasUrl
|
|||||||
private final EmployeeService employeeService;
|
private final EmployeeService employeeService;
|
||||||
private final AuthenticationContext authContext;
|
private final AuthenticationContext authContext;
|
||||||
private boolean fileUploaded = false;
|
private boolean fileUploaded = false;
|
||||||
|
private Button saveButton;
|
||||||
|
|
||||||
public DocumentView(final DocumentService documentService,
|
public DocumentView(final DocumentService documentService,
|
||||||
final EmployeeService employeeService,
|
final EmployeeService employeeService,
|
||||||
@ -83,7 +84,7 @@ public class DocumentView extends BeanValidationForm<Document> implements HasUrl
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected Button createSaveButton() {
|
protected Button createSaveButton() {
|
||||||
Button saveButton = new Button("Save");
|
saveButton = new Button("Save");
|
||||||
saveButton.addClickListener(event -> saveDocument());
|
saveButton.addClickListener(event -> saveDocument());
|
||||||
return saveButton;
|
return saveButton;
|
||||||
}
|
}
|
||||||
@ -110,13 +111,18 @@ public class DocumentView extends BeanValidationForm<Document> implements HasUrl
|
|||||||
|
|
||||||
private void saveDocument() {
|
private void saveDocument() {
|
||||||
if (isFormValid()) {
|
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.setEmployee(employeeComboBox.getValue());
|
||||||
|
document.setFileData(readFileData()); // Actualizamos el archivo si es necesario
|
||||||
|
|
||||||
authContext.getAuthenticatedUser(UserDetails.class).ifPresent(user -> {
|
authContext.getAuthenticatedUser(UserDetails.class).ifPresent(user -> {
|
||||||
String creator = user.getUsername();
|
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.");
|
Notification.show("File saved successfully.");
|
||||||
clearForm();
|
clearForm();
|
||||||
} else {
|
} else {
|
||||||
@ -155,6 +161,18 @@ public class DocumentView extends BeanValidationForm<Document> implements HasUrl
|
|||||||
fileUploaded = false;
|
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
|
@Override
|
||||||
public void setParameter(final BeforeEvent beforeEvent, final String action) {
|
public void setParameter(final BeforeEvent beforeEvent, final String action) {
|
||||||
@ -166,7 +184,8 @@ public class DocumentView extends BeanValidationForm<Document> implements HasUrl
|
|||||||
} else {
|
} else {
|
||||||
UUID documentId = UUID.fromString(s);
|
UUID documentId = UUID.fromString(s);
|
||||||
var document = documentService.getDocument(documentId);
|
var document = documentService.getDocument(documentId);
|
||||||
setEntityWithEnabledSave(document);
|
setEntity(document);
|
||||||
|
employeeComboBox.setValue(document.getEmployee());
|
||||||
|
|
||||||
if ("edit".equals(action) && !s.isEmpty()) {
|
if ("edit".equals(action) && !s.isEmpty()) {
|
||||||
setFieldsReadOnly(false);
|
setFieldsReadOnly(false);
|
||||||
@ -174,6 +193,7 @@ public class DocumentView extends BeanValidationForm<Document> implements HasUrl
|
|||||||
setFieldsReadOnly(true);
|
setFieldsReadOnly(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
checkFormModifications();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setFieldsReadOnly(final boolean option) {
|
private void setFieldsReadOnly(final boolean option) {
|
||||||
|
@ -8,16 +8,20 @@ import com.primefactorsolutions.service.EmployeeService;
|
|||||||
import com.vaadin.flow.component.button.Button;
|
import com.vaadin.flow.component.button.Button;
|
||||||
import com.vaadin.flow.component.combobox.ComboBox;
|
import com.vaadin.flow.component.combobox.ComboBox;
|
||||||
import com.vaadin.flow.component.grid.GridSortOrder;
|
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.Main;
|
||||||
import com.vaadin.flow.component.html.Span;
|
import com.vaadin.flow.component.html.Span;
|
||||||
import com.vaadin.flow.data.provider.SortDirection;
|
import com.vaadin.flow.data.provider.SortDirection;
|
||||||
import com.vaadin.flow.router.PageTitle;
|
import com.vaadin.flow.router.PageTitle;
|
||||||
import com.vaadin.flow.router.Route;
|
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 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.vaadin.firitin.components.grid.PagingGrid;
|
import org.vaadin.firitin.components.grid.PagingGrid;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -138,23 +142,14 @@ public class DocumentsListView extends Main {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void downloadDocument(final Document document) {
|
private void downloadDocument(final Document document) {
|
||||||
String base64Data = Base64.getEncoder().encodeToString(document.getFileData());
|
StreamResource resource = new StreamResource(document.getFileName(),
|
||||||
String jsCode = "var byteCharacters = atob('" + base64Data + "');"
|
() -> new ByteArrayInputStream(document.getFileData()));
|
||||||
+ "var byteNumbers = new Array(byteCharacters.length);"
|
resource.setContentType("application/pdf");
|
||||||
+ "for (var i = 0; i < byteCharacters.length; i++) {"
|
resource.setHeader("Content-Disposition", "attachment; filename=\"" + document.getFileName() + ".pdf\"");
|
||||||
+ " byteNumbers[i] = byteCharacters.charCodeAt(i);"
|
getUI().ifPresent(ui -> {
|
||||||
+ "}"
|
StreamRegistration registration = ui.getSession().getResourceRegistry().registerResource(resource);
|
||||||
+ "var byteArray = new Uint8Array(byteNumbers);"
|
ui.getPage().open(registration.getResourceUri().toString());
|
||||||
+ "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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@FunctionalInterface
|
@FunctionalInterface
|
||||||
|
Loading…
Reference in New Issue
Block a user