#30 Perfil de Usuario Administrativo - Añadir NUEVO Documento
This commit is contained in:
parent
e349503fb4
commit
2caa878c07
@ -87,4 +87,8 @@ public class EmployeeService {
|
||||
|
||||
ldapTemplate.modifyAttributes(buildDn(employee), new ModificationItem[] {item});
|
||||
}
|
||||
|
||||
public List<Employee> findAllEmployees() {
|
||||
return employeeRepository.findAll();
|
||||
}
|
||||
}
|
@ -2,18 +2,16 @@ package com.primefactorsolutions.views;
|
||||
|
||||
import com.primefactorsolutions.model.Document;
|
||||
import com.primefactorsolutions.model.DocumentType;
|
||||
import com.primefactorsolutions.model.Employee;
|
||||
import com.primefactorsolutions.service.DocumentService;
|
||||
import com.vaadin.flow.component.Component;
|
||||
import com.primefactorsolutions.service.EmployeeService;
|
||||
import com.vaadin.flow.component.button.Button;
|
||||
import com.vaadin.flow.component.dialog.Dialog;
|
||||
import com.vaadin.flow.component.html.Div;
|
||||
import com.vaadin.flow.component.combobox.ComboBox;
|
||||
import com.vaadin.flow.component.html.H2;
|
||||
import com.vaadin.flow.component.html.IFrame;
|
||||
import com.vaadin.flow.component.html.Main;
|
||||
import com.vaadin.flow.component.notification.Notification;
|
||||
import com.vaadin.flow.component.orderedlayout.FlexComponent;
|
||||
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
|
||||
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
|
||||
import com.vaadin.flow.component.textfield.TextField;
|
||||
import com.vaadin.flow.component.upload.Upload;
|
||||
import com.vaadin.flow.component.upload.receivers.MemoryBuffer;
|
||||
import com.vaadin.flow.router.PageTitle;
|
||||
@ -23,152 +21,129 @@ import jakarta.annotation.security.PermitAll;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
|
||||
@SpringComponent
|
||||
@PermitAll
|
||||
@Scope("prototype")
|
||||
@PageTitle("Documents")
|
||||
@Route(value = "/documents/me", layout = MainLayout.class)
|
||||
public abstract class DocumentsView extends Main {
|
||||
private final DocumentService documentService;
|
||||
@Route(value = "/documents", layout = MainLayout.class)
|
||||
public class DocumentsView extends Main {
|
||||
|
||||
private final TextField fileName = new TextField("Document Name");
|
||||
private final ComboBox<DocumentType> documentType = new ComboBox<>("Document Type");
|
||||
private final ComboBox<Employee> employeeComboBox = new ComboBox<>("Employee");
|
||||
private final MemoryBuffer buffer = new MemoryBuffer();
|
||||
private String lastUploadedFileName = null;
|
||||
private final Upload uploadButton = new Upload(buffer);
|
||||
private final DocumentService documentService;
|
||||
private final EmployeeService employeeService;
|
||||
private boolean fileUploaded = false;
|
||||
|
||||
public DocumentsView(final DocumentService documentService) {
|
||||
public DocumentsView(final DocumentService documentService, final EmployeeService employeeService) {
|
||||
this.documentService = documentService;
|
||||
initializeLayout();
|
||||
this.employeeService = employeeService;
|
||||
initializeView();
|
||||
addComponents();
|
||||
}
|
||||
|
||||
protected abstract void initializeLayout();
|
||||
|
||||
protected HorizontalLayout createRow(
|
||||
final String title1,
|
||||
final DocumentType type1,
|
||||
final String title2,
|
||||
final DocumentType type2) {
|
||||
|
||||
HorizontalLayout row = new HorizontalLayout();
|
||||
row.add(
|
||||
createDocumentSection(title1, type1),
|
||||
createDocumentSection(title2, type2)
|
||||
);
|
||||
return row;
|
||||
private void initializeView() {
|
||||
setFileNameProperties();
|
||||
setDocumentTypeProperties();
|
||||
setEmployeeComboBoxProperties();
|
||||
setUploadButtonProperties();
|
||||
}
|
||||
|
||||
protected HorizontalLayout createRow(final String title1, final DocumentType type1) {
|
||||
HorizontalLayout row = new HorizontalLayout();
|
||||
row.add(createDocumentSection(title1, type1));
|
||||
return row;
|
||||
private void setFileNameProperties() {
|
||||
fileName.setWidthFull();
|
||||
}
|
||||
|
||||
private Div createDocumentSection(final String title, final DocumentType documentType) {
|
||||
Div section = new Div();
|
||||
section.add(new H2(title));
|
||||
|
||||
Upload upload = createUploadComponent();
|
||||
Button viewButton = createViewButton(documentType);
|
||||
Button saveButton = createSaveButton(viewButton, documentType);
|
||||
|
||||
section.add(createLayout(upload),
|
||||
createLayout(viewButton, new Button("Edit"), saveButton),
|
||||
createAdditionalButtons());
|
||||
return section;
|
||||
private void setDocumentTypeProperties() {
|
||||
documentType.setItems(DocumentType.values());
|
||||
documentType.setWidthFull();
|
||||
}
|
||||
|
||||
private Upload createUploadComponent() {
|
||||
Upload upload = new Upload(buffer);
|
||||
upload.setMaxFiles(1);
|
||||
upload.setAcceptedFileTypes(".pdf");
|
||||
upload.addSucceededListener(event -> handleUploadSuccess(event.getFileName()));
|
||||
return upload;
|
||||
private void setEmployeeComboBoxProperties() {
|
||||
List<Employee> employees = employeeService.findAllEmployees();
|
||||
employeeComboBox.setItems(employees);
|
||||
employeeComboBox.setItemLabelGenerator(employee -> employee.getFirstName() + " " + employee.getLastName());
|
||||
employeeComboBox.setWidthFull();
|
||||
}
|
||||
|
||||
private Button createViewButton(final DocumentType documentType) {
|
||||
Button viewButton = new Button("View");
|
||||
viewButton.setEnabled(documentExists(documentType));
|
||||
viewButton.addClickListener(event -> viewDocument(documentType));
|
||||
return viewButton;
|
||||
private void setUploadButtonProperties() {
|
||||
uploadButton.setMaxFiles(1);
|
||||
uploadButton.setAcceptedFileTypes(".pdf");
|
||||
uploadButton.addSucceededListener(event -> handleUploadSuccess());
|
||||
}
|
||||
|
||||
private Button createSaveButton(final Button viewButton, final DocumentType documentType) {
|
||||
private void addComponents() {
|
||||
H2 title = new H2("Edit Documents");
|
||||
Button saveButton = createSaveButton();
|
||||
Button closeButton = createCloseButton();
|
||||
HorizontalLayout buttonLayout = new HorizontalLayout(saveButton, closeButton);
|
||||
|
||||
add(title, fileName, documentType, employeeComboBox, uploadButton, buttonLayout);
|
||||
}
|
||||
|
||||
private Button createSaveButton() {
|
||||
Button saveButton = new Button("Save");
|
||||
saveButton.addClickListener(event -> saveFile(documentType, viewButton));
|
||||
saveButton.addClickListener(event -> saveDocument());
|
||||
return saveButton;
|
||||
}
|
||||
|
||||
private void handleUploadSuccess(final String fileName) {
|
||||
lastUploadedFileName = fileName;
|
||||
Notification.show("File uploaded successfully");
|
||||
private Button createCloseButton() {
|
||||
Button closeButton = new Button("Close");
|
||||
closeButton.addClickListener(event -> closeForm());
|
||||
return closeButton;
|
||||
}
|
||||
|
||||
private HorizontalLayout createLayout(final Component... components) {
|
||||
if (components.length != 1 && components.length != 3) {
|
||||
throw new IllegalArgumentException("This method only accepts 1 or 3 components.");
|
||||
}
|
||||
HorizontalLayout layout = new HorizontalLayout(components);
|
||||
layout.setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER);
|
||||
return layout;
|
||||
private void handleUploadSuccess() {
|
||||
fileUploaded = true;
|
||||
Notification.show("File uploaded successfully.");
|
||||
}
|
||||
|
||||
private HorizontalLayout createAdditionalButtons() {
|
||||
return createLayout(new Button("Print"), new Button("Download"), new Button("Delete"));
|
||||
}
|
||||
|
||||
private void saveFile(final DocumentType documentType, final Button viewButton) {
|
||||
if (lastUploadedFileName == null) {
|
||||
Notification.show("Please upload a file first.");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
byte[] content = buffer.getInputStream().readAllBytes();
|
||||
documentService.saveDocument(new Document(lastUploadedFileName, documentType, content));
|
||||
private void saveDocument() {
|
||||
if (isFormValid()) {
|
||||
Document document = createDocument();
|
||||
documentService.saveDocument(document);
|
||||
Notification.show("File saved successfully.");
|
||||
viewButton.setEnabled(true);
|
||||
clearForm();
|
||||
} else {
|
||||
Notification.show("Save failed: Please complete all fields and upload a file.");
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isFormValid() {
|
||||
return !fileName.isEmpty()
|
||||
&& documentType.getValue() != null
|
||||
&& employeeComboBox.getValue() != null
|
||||
&& fileUploaded;
|
||||
}
|
||||
|
||||
private Document createDocument() {
|
||||
Document document = new Document();
|
||||
document.setFileName(fileName.getValue());
|
||||
document.setDocumentType(documentType.getValue());
|
||||
document.setFileData(readFileData());
|
||||
return document;
|
||||
}
|
||||
|
||||
private byte[] readFileData() {
|
||||
try {
|
||||
return buffer.getInputStream().readAllBytes();
|
||||
} catch (IOException e) {
|
||||
Notification.show("Error saving file: " + e.getMessage());
|
||||
Notification.show("Error reading file data.");
|
||||
return new byte[0];
|
||||
}
|
||||
}
|
||||
|
||||
private boolean documentExists(final DocumentType documentType) {
|
||||
return documentService.getAllDocuments().stream()
|
||||
.anyMatch(doc -> doc.getDocumentType() == documentType);
|
||||
private void clearForm() {
|
||||
fileName.clear();
|
||||
documentType.clear();
|
||||
employeeComboBox.clear();
|
||||
fileUploaded = false;
|
||||
}
|
||||
|
||||
private void viewDocument(final DocumentType documentType) {
|
||||
documentService.getAllDocuments().stream()
|
||||
.filter(doc -> doc.getDocumentType() == documentType)
|
||||
.findFirst()
|
||||
.ifPresentOrElse(this::showPdfDialog, () -> Notification.show("Document not found."));
|
||||
private void closeForm() {
|
||||
clearForm();
|
||||
}
|
||||
|
||||
private void showPdfDialog(final Document document) {
|
||||
Dialog dialog = createDialog(document.getFileData());
|
||||
dialog.open();
|
||||
}
|
||||
|
||||
private Dialog createDialog(final byte[] fileData) {
|
||||
Dialog dialog = new Dialog();
|
||||
dialog.setModal(true);
|
||||
dialog.setCloseOnEsc(true);
|
||||
dialog.setCloseOnOutsideClick(true);
|
||||
|
||||
IFrame pdfFrame = new IFrame();
|
||||
pdfFrame.setSrc(createPdfResource(fileData));
|
||||
pdfFrame.setWidth("800px");
|
||||
pdfFrame.setHeight("600px");
|
||||
|
||||
Button closeButton = new Button("Close", event -> dialog.close());
|
||||
VerticalLayout layout = new VerticalLayout(pdfFrame, closeButton);
|
||||
layout.setAlignItems(FlexComponent.Alignment.CENTER);
|
||||
|
||||
dialog.add(layout);
|
||||
return dialog;
|
||||
}
|
||||
|
||||
private String createPdfResource(final byte[] fileData) {
|
||||
return "data:application/pdf;base64," + Base64.getEncoder().encodeToString(fileData);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -18,9 +18,7 @@ import com.vaadin.flow.theme.lumo.LumoUtility;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.vaadin.lineawesome.LineAwesomeIcon;
|
||||
|
||||
/**
|
||||
* The main view is a top-level placeholder for other views.
|
||||
*/
|
||||
|
||||
public class MainLayout extends AppLayout {
|
||||
private final transient AuthenticationContext authContext;
|
||||
|
||||
@ -81,8 +79,8 @@ public class MainLayout extends AppLayout {
|
||||
|
||||
SideNavItem admin = new SideNavItem("Admin", MainView.class,
|
||||
LineAwesomeIcon.SUPERSCRIPT_SOLID.create());
|
||||
admin.addItem(new SideNavItem("Employees", EmployeeView.class,
|
||||
LineAwesomeIcon.USER_EDIT_SOLID.create()));
|
||||
// admin.addItem(new SideNavItem("Employees", EmployeeView.class,
|
||||
// LineAwesomeIcon.USER_EDIT_SOLID.create()));
|
||||
admin.addItem(new SideNavItem("Documents", DocumentsView.class,
|
||||
LineAwesomeIcon.FILE_ALT_SOLID.create()));
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user