#17-Perfil-Personal-Administrativo #24
@ -1,10 +1,6 @@
|
|||||||
package com.primefactorsolutions.model;
|
package com.primefactorsolutions.model;
|
||||||
|
|
||||||
import jakarta.persistence.GeneratedValue;
|
import jakarta.persistence.*;
|
||||||
import jakarta.persistence.GenerationType;
|
|
||||||
import jakarta.persistence.Id;
|
|
||||||
import jakarta.persistence.MappedSuperclass;
|
|
||||||
import jakarta.persistence.Version;
|
|
||||||
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@ -15,6 +11,11 @@ public abstract class BaseEntity {
|
|||||||
@GeneratedValue(strategy = GenerationType.UUID)
|
@GeneratedValue(strategy = GenerationType.UUID)
|
||||||
private UUID id;
|
private UUID id;
|
||||||
private String fileName;
|
private String fileName;
|
||||||
|
@Lob
|
||||||
|
@Column(columnDefinition = "BLOB")
|
||||||
|
private byte[] fileData;
|
||||||
|
@Enumerated(EnumType.STRING)
|
||||||
|
private DocumentType documentType;
|
||||||
|
|
||||||
@Version
|
@Version
|
||||||
private int version;
|
private int version;
|
||||||
@ -53,4 +54,8 @@ public abstract class BaseEntity {
|
|||||||
public String getFileName() {
|
public String getFileName() {
|
||||||
return fileName;
|
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.Document;
|
||||||
import com.primefactorsolutions.model.DocumentType;
|
import com.primefactorsolutions.model.DocumentType;
|
||||||
import com.primefactorsolutions.service.DocumentService;
|
import com.primefactorsolutions.service.DocumentService;
|
||||||
|
import com.vaadin.flow.component.Component;
|
||||||
import com.vaadin.flow.component.button.Button;
|
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.Div;
|
||||||
import com.vaadin.flow.component.html.H2;
|
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.html.Main;
|
||||||
import com.vaadin.flow.component.notification.Notification;
|
import com.vaadin.flow.component.notification.Notification;
|
||||||
import com.vaadin.flow.component.orderedlayout.FlexComponent;
|
import com.vaadin.flow.component.orderedlayout.FlexComponent;
|
||||||
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
|
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.Upload;
|
||||||
import com.vaadin.flow.component.upload.receivers.MemoryBuffer;
|
import com.vaadin.flow.component.upload.receivers.MemoryBuffer;
|
||||||
import com.vaadin.flow.router.PageTitle;
|
import com.vaadin.flow.router.PageTitle;
|
||||||
@ -19,7 +23,7 @@ import jakarta.annotation.security.PermitAll;
|
|||||||
import org.springframework.context.annotation.Scope;
|
import org.springframework.context.annotation.Scope;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.Base64;
|
||||||
|
|
||||||
@SpringComponent
|
@SpringComponent
|
||||||
@PermitAll
|
@PermitAll
|
||||||
@ -29,104 +33,134 @@ import java.io.IOException;
|
|||||||
public class WorkDocumentsView extends Main {
|
public class WorkDocumentsView extends Main {
|
||||||
private final DocumentService documentService;
|
private final DocumentService documentService;
|
||||||
private final MemoryBuffer buffer = new MemoryBuffer();
|
private final MemoryBuffer buffer = new MemoryBuffer();
|
||||||
private boolean fileUploaded = false;
|
|
||||||
private String lastUploadedFileName = null;
|
private String lastUploadedFileName = null;
|
||||||
|
private DocumentType currentDocumentType = null;
|
||||||
|
|
||||||
public WorkDocumentsView(DocumentService documentService) {
|
public WorkDocumentsView(DocumentService documentService) {
|
||||||
this.documentService = documentService;
|
this.documentService = documentService;
|
||||||
initializeLayout();
|
initializeLayout();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initializeLayout() {
|
private void initializeLayout() {
|
||||||
HorizontalLayout row1 = createRow("PaySlips", "Employment Contract");
|
add(createRow("PaySlips", DocumentType.PAY_SLIPS,
|
||||||
HorizontalLayout row2 = createRow("Work Certificates", "NDA");
|
"Employment Contract", DocumentType.EMPLOYMENT_CONTRACT));
|
||||||
HorizontalLayout row3 = createRow("Memorandums", "Contract Approval MTEPS");
|
add(createRow("Work Certificates", DocumentType.WORK_CERTIFICATES,
|
||||||
add(row1, row2, row3);
|
"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();
|
HorizontalLayout row = new HorizontalLayout();
|
||||||
for (String title : sectionTitles) {
|
row.add(createDocumentSection(title1, type1), createDocumentSection(title2, type2));
|
||||||
row.add(createSection(title));
|
|
||||||
}
|
|
||||||
return row;
|
return row;
|
||||||
}
|
}
|
||||||
private Div createSection(String sectionTitle) {
|
|
||||||
|
private Div createDocumentSection(String title, DocumentType documentType) {
|
||||||
Div section = new Div();
|
Div section = new Div();
|
||||||
section.add(new H2(sectionTitle));
|
section.add(new H2(title));
|
||||||
|
|
||||||
Upload upload = createUploadComponent();
|
Upload upload = createUploadComponent();
|
||||||
boolean hasDocument = documentService.getAllDocuments()
|
Button viewButton = createViewButton(documentType);
|
||||||
.stream()
|
Button saveButton = createSaveButton(viewButton, documentType);
|
||||||
.anyMatch(doc -> doc.getFileName().equalsIgnoreCase(sectionTitle + ".pdf"));
|
|
||||||
Button viewButton = new Button("View");
|
section.add(createLayout(upload),
|
||||||
viewButton.setEnabled(hasDocument);
|
createLayout(viewButton, new Button("Edit"), saveButton),
|
||||||
Button saveButton = createSaveButton(sectionTitle, viewButton);
|
createAdditionalButtons());
|
||||||
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);
|
|
||||||
return section;
|
return section;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Upload createUploadComponent() {
|
private Upload createUploadComponent() {
|
||||||
Upload upload = new Upload(buffer);
|
Upload upload = new Upload(buffer);
|
||||||
upload.setMaxFiles(1);
|
upload.setMaxFiles(1);
|
||||||
upload.setAcceptedFileTypes(".pdf", ".doc", ".docx");
|
upload.setAcceptedFileTypes(".pdf");
|
||||||
upload.addSucceededListener(event -> {
|
upload.addSucceededListener(event -> handleUploadSuccess(event.getFileName()));
|
||||||
fileUploaded = true;
|
|
||||||
lastUploadedFileName = event.getFileName();
|
|
||||||
Notification.show("File uploaded successfully");
|
|
||||||
});
|
|
||||||
return upload;
|
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");
|
Button saveButton = new Button("Save");
|
||||||
saveButton.addClickListener(clickEvent -> saveFile(sectionTitle, viewButton));
|
saveButton.addClickListener(event -> saveFile(documentType, viewButton));
|
||||||
return saveButton;
|
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.setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER);
|
||||||
layout.add(component);
|
|
||||||
return layout;
|
return layout;
|
||||||
}
|
}
|
||||||
private HorizontalLayout createLayoutWithComponents(com.vaadin.flow.component.Component... components) {
|
|
||||||
HorizontalLayout layout = new HorizontalLayout();
|
private HorizontalLayout createAdditionalButtons() {
|
||||||
layout.setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER);
|
return createLayout(new Button("Print"), new Button("Download"), new Button("Delete"));
|
||||||
layout.add(components);
|
|
||||||
return layout;
|
|
||||||
}
|
}
|
||||||
private void saveFile(String sectionTitle, Button viewButton) {
|
|
||||||
if (fileUploaded) {
|
private void saveFile(DocumentType documentType, Button viewButton) {
|
||||||
try {
|
if (lastUploadedFileName == null) {
|
||||||
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 {
|
|
||||||
Notification.show("Please upload a file first.");
|
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) {
|
private boolean documentExists(DocumentType documentType) {
|
||||||
case "PaySlips" -> DocumentType.PAY_SLIPS;
|
return documentService.getAllDocuments().stream()
|
||||||
case "Employment Contract" -> DocumentType.EMPLOYMENT_CONTRACT;
|
.anyMatch(doc -> doc.getDocumentType() == documentType);
|
||||||
case "Work Certificates" -> DocumentType.WORK_CERTIFICATES;
|
}
|
||||||
case "NDA" -> DocumentType.NDA;
|
|
||||||
case "Memorandums" -> DocumentType.MEMORANDUMS;
|
private void viewDocument(DocumentType documentType) {
|
||||||
case "Contract Approval MTEPS" -> DocumentType.CONTRACT_APPROVAL_MTEPS;
|
documentService.getAllDocuments().stream()
|
||||||
default -> DocumentType.OTHER;
|
.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