#17 Perfil de Personal Administrativo - Carga de Documentación Laboral (Ver documento)
This commit is contained in:
parent
64d62a06db
commit
97c07a4558
@ -1,10 +1,6 @@
|
||||
package com.primefactorsolutions.model;
|
||||
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.MappedSuperclass;
|
||||
import jakarta.persistence.Version;
|
||||
import jakarta.persistence.*;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@ -15,6 +11,11 @@ public abstract class BaseEntity {
|
||||
@GeneratedValue(strategy = GenerationType.UUID)
|
||||
private UUID id;
|
||||
private String fileName;
|
||||
@Lob
|
||||
@Column(columnDefinition = "BLOB")
|
||||
private byte[] fileData;
|
||||
@Enumerated(EnumType.STRING)
|
||||
private DocumentType documentType;
|
||||
|
||||
@Version
|
||||
private int version;
|
||||
@ -53,4 +54,8 @@ public abstract class BaseEntity {
|
||||
public String getFileName() {
|
||||
return fileName;
|
||||
}
|
||||
|
||||
public byte[] getFileData() { return fileData; }
|
||||
|
||||
public DocumentType getDocumentType() { return documentType; }
|
||||
}
|
||||
|
@ -3,13 +3,17 @@ package com.primefactorsolutions.views;
|
||||
import com.primefactorsolutions.model.Document;
|
||||
import com.primefactorsolutions.model.DocumentType;
|
||||
import com.primefactorsolutions.service.DocumentService;
|
||||
import com.vaadin.flow.component.Component;
|
||||
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.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.upload.Upload;
|
||||
import com.vaadin.flow.component.upload.receivers.MemoryBuffer;
|
||||
import com.vaadin.flow.router.PageTitle;
|
||||
@ -19,7 +23,7 @@ import jakarta.annotation.security.PermitAll;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import java.util.Base64;
|
||||
|
||||
@SpringComponent
|
||||
@PermitAll
|
||||
@ -29,104 +33,134 @@ import java.io.IOException;
|
||||
public class WorkDocumentsView extends Main {
|
||||
private final DocumentService documentService;
|
||||
private final MemoryBuffer buffer = new MemoryBuffer();
|
||||
private boolean fileUploaded = false;
|
||||
private String lastUploadedFileName = null;
|
||||
private DocumentType currentDocumentType = null;
|
||||
|
||||
public WorkDocumentsView(DocumentService documentService) {
|
||||
this.documentService = documentService;
|
||||
initializeLayout();
|
||||
}
|
||||
|
||||
private void initializeLayout() {
|
||||
HorizontalLayout row1 = createRow("PaySlips", "Employment Contract");
|
||||
HorizontalLayout row2 = createRow("Work Certificates", "NDA");
|
||||
HorizontalLayout row3 = createRow("Memorandums", "Contract Approval MTEPS");
|
||||
add(row1, row2, row3);
|
||||
add(createRow("PaySlips", DocumentType.PAY_SLIPS,
|
||||
"Employment Contract", DocumentType.EMPLOYMENT_CONTRACT));
|
||||
add(createRow("Work Certificates", DocumentType.WORK_CERTIFICATES,
|
||||
"NDA", DocumentType.NDA));
|
||||
add(createRow("Memorandums", DocumentType.MEMORANDUMS,
|
||||
"Contract Approval MTEPS", DocumentType.CONTRACT_APPROVAL_MTEPS));
|
||||
}
|
||||
private HorizontalLayout createRow(String... sectionTitles) {
|
||||
|
||||
private HorizontalLayout createRow(String title1, DocumentType type1, String title2, DocumentType type2) {
|
||||
HorizontalLayout row = new HorizontalLayout();
|
||||
for (String title : sectionTitles) {
|
||||
row.add(createSection(title));
|
||||
}
|
||||
row.add(createDocumentSection(title1, type1), createDocumentSection(title2, type2));
|
||||
return row;
|
||||
}
|
||||
private Div createSection(String sectionTitle) {
|
||||
|
||||
private Div createDocumentSection(String title, DocumentType documentType) {
|
||||
Div section = new Div();
|
||||
section.add(new H2(sectionTitle));
|
||||
section.add(new H2(title));
|
||||
|
||||
Upload upload = createUploadComponent();
|
||||
boolean hasDocument = documentService.getAllDocuments()
|
||||
.stream()
|
||||
.anyMatch(doc -> doc.getFileName().equalsIgnoreCase(sectionTitle + ".pdf"));
|
||||
Button viewButton = new Button("View");
|
||||
viewButton.setEnabled(hasDocument);
|
||||
Button saveButton = createSaveButton(sectionTitle, viewButton);
|
||||
HorizontalLayout uploadLayout = createLayoutWithComponent(upload);
|
||||
HorizontalLayout actionButtonsLayout = createLayoutWithComponents(
|
||||
viewButton, new Button("Edit"), saveButton
|
||||
);
|
||||
HorizontalLayout additionalButtonsLayout = createLayoutWithComponents(
|
||||
new Button("Print"), new Button("Download"), new Button("Delete")
|
||||
);
|
||||
section.add(uploadLayout, actionButtonsLayout, additionalButtonsLayout);
|
||||
Button viewButton = createViewButton(documentType);
|
||||
Button saveButton = createSaveButton(viewButton, documentType);
|
||||
|
||||
section.add(createLayout(upload),
|
||||
createLayout(viewButton, new Button("Edit"), saveButton),
|
||||
createAdditionalButtons());
|
||||
return section;
|
||||
}
|
||||
|
||||
private Upload createUploadComponent() {
|
||||
Upload upload = new Upload(buffer);
|
||||
upload.setMaxFiles(1);
|
||||
upload.setAcceptedFileTypes(".pdf", ".doc", ".docx");
|
||||
upload.addSucceededListener(event -> {
|
||||
fileUploaded = true;
|
||||
lastUploadedFileName = event.getFileName();
|
||||
Notification.show("File uploaded successfully");
|
||||
});
|
||||
upload.setAcceptedFileTypes(".pdf");
|
||||
upload.addSucceededListener(event -> handleUploadSuccess(event.getFileName()));
|
||||
return upload;
|
||||
}
|
||||
private Button createSaveButton(String sectionTitle, Button viewButton) {
|
||||
|
||||
private Button createViewButton(DocumentType documentType) {
|
||||
Button viewButton = new Button("View");
|
||||
viewButton.setEnabled(documentExists(documentType));
|
||||
viewButton.addClickListener(event -> viewDocument(documentType));
|
||||
return viewButton;
|
||||
}
|
||||
|
||||
private Button createSaveButton(Button viewButton, DocumentType documentType) {
|
||||
Button saveButton = new Button("Save");
|
||||
saveButton.addClickListener(clickEvent -> saveFile(sectionTitle, viewButton));
|
||||
saveButton.addClickListener(event -> saveFile(documentType, viewButton));
|
||||
return saveButton;
|
||||
}
|
||||
private HorizontalLayout createLayoutWithComponent(com.vaadin.flow.component.Component component) {
|
||||
HorizontalLayout layout = new HorizontalLayout();
|
||||
|
||||
private void handleUploadSuccess(String fileName) {
|
||||
lastUploadedFileName = fileName;
|
||||
Notification.show("File uploaded successfully");
|
||||
}
|
||||
|
||||
private HorizontalLayout createLayout(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);
|
||||
layout.add(component);
|
||||
return layout;
|
||||
}
|
||||
private HorizontalLayout createLayoutWithComponents(com.vaadin.flow.component.Component... components) {
|
||||
HorizontalLayout layout = new HorizontalLayout();
|
||||
layout.setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER);
|
||||
layout.add(components);
|
||||
return layout;
|
||||
|
||||
private HorizontalLayout createAdditionalButtons() {
|
||||
return createLayout(new Button("Print"), new Button("Download"), new Button("Delete"));
|
||||
}
|
||||
private void saveFile(String sectionTitle, Button viewButton) {
|
||||
if (fileUploaded) {
|
||||
try {
|
||||
String fileName = lastUploadedFileName;
|
||||
byte[] content = buffer.getInputStream().readAllBytes();
|
||||
if (content != null) {
|
||||
DocumentType documentType = determineDocumentType(sectionTitle);
|
||||
Document document = new Document(fileName, documentType, content);
|
||||
documentService.saveDocument(document);
|
||||
fileUploaded = false;
|
||||
Notification.show("File saved successfully.");
|
||||
viewButton.setEnabled(true);
|
||||
} else {
|
||||
Notification.show("No file content found.");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
Notification.show("Error reading file content: " + e.getMessage());
|
||||
}
|
||||
} else {
|
||||
|
||||
private void saveFile(DocumentType documentType, 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));
|
||||
Notification.show("File saved successfully.");
|
||||
viewButton.setEnabled(true);
|
||||
} catch (IOException e) {
|
||||
Notification.show("Error saving file: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
private DocumentType determineDocumentType(String sectionTitle) {
|
||||
return switch (sectionTitle) {
|
||||
case "PaySlips" -> DocumentType.PAY_SLIPS;
|
||||
case "Employment Contract" -> DocumentType.EMPLOYMENT_CONTRACT;
|
||||
case "Work Certificates" -> DocumentType.WORK_CERTIFICATES;
|
||||
case "NDA" -> DocumentType.NDA;
|
||||
case "Memorandums" -> DocumentType.MEMORANDUMS;
|
||||
case "Contract Approval MTEPS" -> DocumentType.CONTRACT_APPROVAL_MTEPS;
|
||||
default -> DocumentType.OTHER;
|
||||
};
|
||||
|
||||
private boolean documentExists(DocumentType documentType) {
|
||||
return documentService.getAllDocuments().stream()
|
||||
.anyMatch(doc -> doc.getDocumentType() == documentType);
|
||||
}
|
||||
|
||||
private void viewDocument(DocumentType documentType) {
|
||||
documentService.getAllDocuments().stream()
|
||||
.filter(doc -> doc.getDocumentType() == documentType)
|
||||
.findFirst()
|
||||
.ifPresentOrElse(this::showPdfDialog, () -> Notification.show("Document not found."));
|
||||
}
|
||||
|
||||
private void showPdfDialog(Document document) {
|
||||
Dialog dialog = createDialog(document.getFileData());
|
||||
dialog.open();
|
||||
}
|
||||
|
||||
private Dialog createDialog(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(byte[] fileData) {
|
||||
return "data:application/pdf;base64," + Base64.getEncoder().encodeToString(fileData);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user