En-desarrollo #27
@ -9,5 +9,9 @@ public enum DocumentType {
|
|||||||
NDA,
|
NDA,
|
||||||
MEMORANDUMS,
|
MEMORANDUMS,
|
||||||
CONTRACT_APPROVAL_MTEPS,
|
CONTRACT_APPROVAL_MTEPS,
|
||||||
|
BACKGROUND_CHECK_CERTIFICATE,
|
||||||
|
PRE_EMPLOYMENT_EVALUATION,
|
||||||
|
INSURANCE_REGISTRATION_FORM,
|
||||||
|
INSURANCE_CANCELLATION_FORM,
|
||||||
OTHER
|
OTHER
|
||||||
}
|
}
|
||||||
|
@ -1,16 +1,178 @@
|
|||||||
package com.primefactorsolutions.views;
|
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.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;
|
import com.vaadin.flow.router.PageTitle;
|
||||||
import com.vaadin.flow.router.Route;
|
import com.vaadin.flow.router.Route;
|
||||||
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 java.io.IOException;
|
||||||
|
import java.util.Base64;
|
||||||
|
|
||||||
@SpringComponent
|
@SpringComponent
|
||||||
@PermitAll
|
@PermitAll
|
||||||
@Scope("prototype")
|
@Scope("prototype")
|
||||||
@PageTitle("PersonalDocuments")
|
@PageTitle("PersonalDocuments")
|
||||||
@Route(value = "/personal-documents/me", layout = MainLayout.class)
|
@Route(value = "/personal-documents/me", layout = MainLayout.class)
|
||||||
public class PersonalDocumentsView extends Main {
|
public class PersonalDocumentsView extends Main {
|
||||||
|
|||||||
|
private final DocumentService documentService;
|
||||||
|
private final MemoryBuffer buffer = new MemoryBuffer();
|
||||||
|
private String lastUploadedFileName = null;
|
||||||
|
|
||||||
|
public PersonalDocumentsView(final DocumentService documentService) {
|
||||||
|
this.documentService = documentService;
|
||||||
|
initializeLayout();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initializeLayout() {
|
||||||
|
add(createRow("Identity Card", DocumentType.ID_CARD,
|
||||||
|
"Background check certificate", DocumentType.BACKGROUND_CHECK_CERTIFICATE));
|
||||||
|
add(createRow("Pre-employment evaluation", DocumentType.PRE_EMPLOYMENT_EVALUATION,
|
||||||
|
"Insurance registration form", DocumentType.INSURANCE_REGISTRATION_FORM));
|
||||||
|
add(createRow());
|
||||||
|
}
|
||||||
|
|
||||||
|
private 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 HorizontalLayout createRow() {
|
||||||
|
HorizontalLayout row = new HorizontalLayout();
|
||||||
|
row.add(createDocumentSection("Insurance cancellation form", DocumentType.INSURANCE_CANCELLATION_FORM));
|
||||||
|
return row;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 Upload createUploadComponent() {
|
||||||
|
Upload upload = new Upload(buffer);
|
||||||
|
upload.setMaxFiles(1);
|
||||||
|
upload.setAcceptedFileTypes(".pdf");
|
||||||
|
upload.addSucceededListener(event -> handleUploadSuccess(event.getFileName()));
|
||||||
|
return upload;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Button createViewButton(final DocumentType documentType) {
|
||||||
|
Button viewButton = new Button("View");
|
||||||
|
viewButton.setEnabled(documentExists(documentType));
|
||||||
|
viewButton.addClickListener(event -> viewDocument(documentType));
|
||||||
|
return viewButton;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Button createSaveButton(final Button viewButton, final DocumentType documentType) {
|
||||||
|
Button saveButton = new Button("Save");
|
||||||
|
saveButton.addClickListener(event -> saveFile(documentType, viewButton));
|
||||||
|
return saveButton;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleUploadSuccess(final String fileName) {
|
||||||
|
lastUploadedFileName = fileName;
|
||||||
|
Notification.show("File uploaded successfully");
|
||||||
|
}
|
||||||
|
|
||||||
|
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 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));
|
||||||
|
Notification.show("File saved successfully.");
|
||||||
|
viewButton.setEnabled(true);
|
||||||
|
} catch (IOException e) {
|
||||||
|
Notification.show("Error saving file: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean documentExists(final DocumentType documentType) {
|
||||||
|
return documentService.getAllDocuments().stream()
|
||||||
|
.anyMatch(doc -> doc.getDocumentType() == documentType);
|
||||||
|
}
|
||||||
|
|
||||||
|
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 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user
todas estas paginas son similares. podemos crear una clase madre con todo el codigo similar para evitar duplicacion de codigo.