From 1887214ed2983ca128241c8b6f7e5dc1799111f2 Mon Sep 17 00:00:00 2001 From: ricardo051199 Date: Wed, 11 Sep 2024 15:27:09 -0400 Subject: [PATCH 1/4] =?UTF-8?q?#17=20Perfil=20de=20Personal=20Administrati?= =?UTF-8?q?vo=20-=20Carga=20de=20Documentaci=C3=B3n=20Laboral=20(VISTA)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../views/WorkDocumentsView.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/main/java/com/primefactorsolutions/views/WorkDocumentsView.java b/src/main/java/com/primefactorsolutions/views/WorkDocumentsView.java index de23e08..ac29094 100644 --- a/src/main/java/com/primefactorsolutions/views/WorkDocumentsView.java +++ b/src/main/java/com/primefactorsolutions/views/WorkDocumentsView.java @@ -1,6 +1,11 @@ package com.primefactorsolutions.views; +import com.vaadin.flow.component.button.Button; +import com.vaadin.flow.component.html.Div; +import com.vaadin.flow.component.html.H2; import com.vaadin.flow.component.html.Main; +import com.vaadin.flow.component.orderedlayout.FlexComponent; +import com.vaadin.flow.component.orderedlayout.HorizontalLayout; import com.vaadin.flow.router.PageTitle; import com.vaadin.flow.router.Route; import com.vaadin.flow.spring.annotation.SpringComponent; @@ -13,4 +18,30 @@ import org.springframework.context.annotation.Scope; @PageTitle("WorkDocuments") @Route(value = "/work-documents/me", layout = MainLayout.class) public class WorkDocumentsView extends Main { + + public WorkDocumentsView() { + HorizontalLayout row1 = new HorizontalLayout(); + row1.add(createSection("Pay Slips"), createSection("Employment Contract")); + HorizontalLayout row2 = new HorizontalLayout(); + row2.add(createSection("Work Certificates"), createSection("NDA")); + HorizontalLayout row3 = new HorizontalLayout(); + row3.add(createSection("Memorandums"), createSection("Contract Approval MTEPS")); + add(row1, row2, row3); + } + + private Div createSection(String sectionTitle) { + Div section = new Div(); + section.add(new H2(sectionTitle)); + HorizontalLayout row1 = new HorizontalLayout(); + row1.setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER); + row1.add(new Button("View"), new Button("Upload"), new Button("Save")); + HorizontalLayout row2 = new HorizontalLayout(); + row2.setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER); + row2.add(new Button("Edit"), new Button("Print"), new Button("Delete")); + HorizontalLayout row3 = new HorizontalLayout(); + row3.setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER); + row3.add(new Button("Download")); + section.add(row1, row2, row3); + return section; + } } From 5bbdb5f2ee252ce342cb10e2b7677972fe607a8a Mon Sep 17 00:00:00 2001 From: ricardo051199 Date: Sat, 14 Sep 2024 15:30:51 -0400 Subject: [PATCH 2/4] =?UTF-8?q?#17=20Perfil=20de=20Personal=20Administrati?= =?UTF-8?q?vo=20-=20Carga=20de=20Documentaci=C3=B3n=20Laboral=20(Funcional?= =?UTF-8?q?idades=20save=20y=20upload)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../model/BaseEntity.java | 5 + .../primefactorsolutions/model/Document.java | 20 ++- .../model/DocumentType.java | 6 + .../repositories/DocumentRepository.java | 9 ++ .../service/DocumentService.java | 44 +++++++ .../views/GovernmentDocumentsView.java | 63 +++++++++ .../views/WorkDocumentsView.java | 123 +++++++++++++++--- 7 files changed, 239 insertions(+), 31 deletions(-) create mode 100644 src/main/java/com/primefactorsolutions/repositories/DocumentRepository.java create mode 100644 src/main/java/com/primefactorsolutions/service/DocumentService.java diff --git a/src/main/java/com/primefactorsolutions/model/BaseEntity.java b/src/main/java/com/primefactorsolutions/model/BaseEntity.java index ddc143e..98b58d2 100644 --- a/src/main/java/com/primefactorsolutions/model/BaseEntity.java +++ b/src/main/java/com/primefactorsolutions/model/BaseEntity.java @@ -14,6 +14,7 @@ public abstract class BaseEntity { @Id @GeneratedValue(strategy = GenerationType.UUID) private UUID id; + private String fileName; @Version private int version; @@ -48,4 +49,8 @@ public abstract class BaseEntity { } return super.equals(that); } + + public String getFileName() { + return fileName; + } } diff --git a/src/main/java/com/primefactorsolutions/model/Document.java b/src/main/java/com/primefactorsolutions/model/Document.java index d162230..ce944b6 100644 --- a/src/main/java/com/primefactorsolutions/model/Document.java +++ b/src/main/java/com/primefactorsolutions/model/Document.java @@ -1,12 +1,8 @@ package com.primefactorsolutions.model; -import jakarta.annotation.Nullable; -import jakarta.persistence.Entity; -import jakarta.persistence.ManyToOne; -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.EqualsAndHashCode; -import lombok.NoArgsConstructor; +import jakarta.persistence.*; + +import lombok.*; @Entity @Data @@ -14,10 +10,10 @@ import lombok.NoArgsConstructor; @NoArgsConstructor @EqualsAndHashCode(callSuper = true) public class Document extends BaseEntity { - @Nullable - private String description; - private String location; + private String fileName; + @Enumerated(EnumType.STRING) private DocumentType documentType; - @ManyToOne - private Employee employee; + @Lob + @Column(columnDefinition = "BLOB") + private byte[] fileData; } diff --git a/src/main/java/com/primefactorsolutions/model/DocumentType.java b/src/main/java/com/primefactorsolutions/model/DocumentType.java index 645beeb..007d080 100644 --- a/src/main/java/com/primefactorsolutions/model/DocumentType.java +++ b/src/main/java/com/primefactorsolutions/model/DocumentType.java @@ -3,5 +3,11 @@ package com.primefactorsolutions.model; public enum DocumentType { ID_CARD, PAY_STUB, + PAY_SLIPS, + EMPLOYMENT_CONTRACT, + WORK_CERTIFICATES, + NDA, + MEMORANDUMS, + CONTRACT_APPROVAL_MTEPS, OTHER } diff --git a/src/main/java/com/primefactorsolutions/repositories/DocumentRepository.java b/src/main/java/com/primefactorsolutions/repositories/DocumentRepository.java new file mode 100644 index 0000000..4a2bc88 --- /dev/null +++ b/src/main/java/com/primefactorsolutions/repositories/DocumentRepository.java @@ -0,0 +1,9 @@ +package com.primefactorsolutions.repositories; + +import com.primefactorsolutions.model.Document; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.UUID; + +public interface DocumentRepository extends JpaRepository { +} diff --git a/src/main/java/com/primefactorsolutions/service/DocumentService.java b/src/main/java/com/primefactorsolutions/service/DocumentService.java new file mode 100644 index 0000000..a5cda38 --- /dev/null +++ b/src/main/java/com/primefactorsolutions/service/DocumentService.java @@ -0,0 +1,44 @@ +package com.primefactorsolutions.service; + +//import com.primefactorsolutions.model.Candidate; +import com.primefactorsolutions.model.Document; +import com.primefactorsolutions.repositories.DocumentRepository; +//import lombok.AllArgsConstructor; +//import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +//import java.io.IOException; +//import java.sql.Connection; +//import java.sql.PreparedStatement; +//import java.sql.ResultSet; +//import java.sql.SQLException; +//import javax.sql.DataSource; +//import javax.sql.DataSource; +import org.springframework.transaction.annotation.Transactional; +//import org.springframework.web.multipart.MultipartFile; + +//import java.io.InputStream; +import java.util.List; +import java.util.UUID; +import java.util.Optional; + +@Service +public class DocumentService { + private final DocumentRepository documentRepository; + + public DocumentService(DocumentRepository documentRepository) { + this.documentRepository = documentRepository; + } + + public void saveDocument(Document newDocument) { + documentRepository.save(newDocument); + } + + public List getAllDocuments() { + return documentRepository.findAll(); + } + + public Optional getDocumentById(UUID id) { + return documentRepository.findById(id); + } +} diff --git a/src/main/java/com/primefactorsolutions/views/GovernmentDocumentsView.java b/src/main/java/com/primefactorsolutions/views/GovernmentDocumentsView.java index a06e4bf..da65534 100644 --- a/src/main/java/com/primefactorsolutions/views/GovernmentDocumentsView.java +++ b/src/main/java/com/primefactorsolutions/views/GovernmentDocumentsView.java @@ -1,16 +1,79 @@ package com.primefactorsolutions.views; +import com.primefactorsolutions.model.Document; +import com.primefactorsolutions.service.DocumentService; +import com.vaadin.flow.component.button.Button; +import com.vaadin.flow.component.html.Anchor; +import com.vaadin.flow.component.html.Div; +import com.vaadin.flow.component.html.H2; 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.upload.Upload; +import com.vaadin.flow.component.upload.receivers.MemoryBuffer; import com.vaadin.flow.router.PageTitle; import com.vaadin.flow.router.Route; +import com.vaadin.flow.server.StreamResource; import com.vaadin.flow.spring.annotation.SpringComponent; import jakarta.annotation.security.PermitAll; import org.springframework.context.annotation.Scope; +import java.io.ByteArrayInputStream; +import java.io.IOException; +//import java.io.InputStream; +import java.util.UUID; + @SpringComponent @PermitAll @Scope("prototype") @PageTitle("GovernmentDocumentsView") @Route(value = "/government-documents/me", layout = MainLayout.class) public class GovernmentDocumentsView extends Main { + + private final DocumentService documentService; + private final Div documentListDiv = new Div(); + + public GovernmentDocumentsView(DocumentService documentService) { + this.documentService = documentService; + initializeLayout(); + } + + private void initializeLayout() { + setSizeFull(); + add(new H2("Document Management")); + + // Upload Component + MemoryBuffer buffer = new MemoryBuffer(); + Upload upload = new Upload(buffer); + upload.addSucceededListener(event -> { + try { + String fileName = event.getFileName(); + byte[] content = buffer.getInputStream().readAllBytes(); +// documentService.saveDocument(fileName, content); + Notification.show("File uploaded successfully"); + updateDocumentList(); + } catch (IOException e) { + Notification.show("Error uploading file: " + e.getMessage()); + } + }); + + // Add Upload component + add(upload); + + // Display documents + updateDocumentList(); + } + + private void updateDocumentList() { + documentListDiv.removeAll(); + for (Document document : documentService.getAllDocuments()) { + // Create a view link for each document +// StreamResource streamResource = new StreamResource(document.getName(), () -> new ByteArrayInputStream(document.getContent())); +// Anchor viewLink = new Anchor(streamResource, "View " + document.getName()); +// viewLink.getElement().setAttribute("target", "_blank"); +// documentListDiv.add(viewLink); + } + add(documentListDiv); + } } diff --git a/src/main/java/com/primefactorsolutions/views/WorkDocumentsView.java b/src/main/java/com/primefactorsolutions/views/WorkDocumentsView.java index ac29094..dfad0df 100644 --- a/src/main/java/com/primefactorsolutions/views/WorkDocumentsView.java +++ b/src/main/java/com/primefactorsolutions/views/WorkDocumentsView.java @@ -1,47 +1,132 @@ package com.primefactorsolutions.views; +import com.primefactorsolutions.model.Document; +import com.primefactorsolutions.model.DocumentType; +import com.primefactorsolutions.service.DocumentService; import com.vaadin.flow.component.button.Button; import com.vaadin.flow.component.html.Div; import com.vaadin.flow.component.html.H2; 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.upload.Upload; +import com.vaadin.flow.component.upload.receivers.MemoryBuffer; import com.vaadin.flow.router.PageTitle; import com.vaadin.flow.router.Route; import com.vaadin.flow.spring.annotation.SpringComponent; import jakarta.annotation.security.PermitAll; import org.springframework.context.annotation.Scope; +import java.io.IOException; + + @SpringComponent @PermitAll @Scope("prototype") @PageTitle("WorkDocuments") @Route(value = "/work-documents/me", layout = MainLayout.class) public class WorkDocumentsView extends Main { - - public WorkDocumentsView() { - HorizontalLayout row1 = new HorizontalLayout(); - row1.add(createSection("Pay Slips"), createSection("Employment Contract")); - HorizontalLayout row2 = new HorizontalLayout(); - row2.add(createSection("Work Certificates"), createSection("NDA")); - HorizontalLayout row3 = new HorizontalLayout(); - row3.add(createSection("Memorandums"), createSection("Contract Approval MTEPS")); + private final DocumentService documentService; + private final MemoryBuffer buffer = new MemoryBuffer(); + private boolean fileUploaded = false; + private String lastUploadedFileName = 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); } - + private HorizontalLayout createRow(String... sectionTitles) { + HorizontalLayout row = new HorizontalLayout(); + for (String title : sectionTitles) { + row.add(createSection(title)); + } + return row; + } private Div createSection(String sectionTitle) { Div section = new Div(); section.add(new H2(sectionTitle)); - HorizontalLayout row1 = new HorizontalLayout(); - row1.setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER); - row1.add(new Button("View"), new Button("Upload"), new Button("Save")); - HorizontalLayout row2 = new HorizontalLayout(); - row2.setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER); - row2.add(new Button("Edit"), new Button("Print"), new Button("Delete")); - HorizontalLayout row3 = new HorizontalLayout(); - row3.setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER); - row3.add(new Button("Download")); - section.add(row1, row2, row3); + 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); 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"); + }); + return upload; + } + private Button createSaveButton(String sectionTitle, Button viewButton) { + Button saveButton = new Button("Save"); + saveButton.addClickListener(clickEvent -> saveFile(sectionTitle, )); + return saveButton; + } + private HorizontalLayout createLayoutWithComponent(com.vaadin.flow.component.Component component) { + HorizontalLayout layout = new HorizontalLayout(); + 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 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 { + Notification.show("Please upload a file first."); + } + } + 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; + }; + } } From 64d62a06dbc5c7952416701cfb730107f4e7e371 Mon Sep 17 00:00:00 2001 From: ricardo051199 Date: Sat, 14 Sep 2024 15:33:33 -0400 Subject: [PATCH 3/4] =?UTF-8?q?#17=20Perfil=20de=20Personal=20Administrati?= =?UTF-8?q?vo=20-=20Carga=20de=20Documentaci=C3=B3n=20Laboral=20(Funcional?= =?UTF-8?q?idades=20save=20y=20upload=20v2)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/primefactorsolutions/views/WorkDocumentsView.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/primefactorsolutions/views/WorkDocumentsView.java b/src/main/java/com/primefactorsolutions/views/WorkDocumentsView.java index dfad0df..d0ec2b0 100644 --- a/src/main/java/com/primefactorsolutions/views/WorkDocumentsView.java +++ b/src/main/java/com/primefactorsolutions/views/WorkDocumentsView.java @@ -81,7 +81,7 @@ public class WorkDocumentsView extends Main { } private Button createSaveButton(String sectionTitle, Button viewButton) { Button saveButton = new Button("Save"); - saveButton.addClickListener(clickEvent -> saveFile(sectionTitle, )); + saveButton.addClickListener(clickEvent -> saveFile(sectionTitle, viewButton)); return saveButton; } private HorizontalLayout createLayoutWithComponent(com.vaadin.flow.component.Component component) { From 97c07a45583fffc5c1c4ee26f4b5e0eb10a68031 Mon Sep 17 00:00:00 2001 From: ricardo051199 Date: Sun, 15 Sep 2024 02:25:38 -0400 Subject: [PATCH 4/4] =?UTF-8?q?#17=20Perfil=20de=20Personal=20Administrati?= =?UTF-8?q?vo=20-=20Carga=20de=20Documentaci=C3=B3n=20Laboral=20(Ver=20doc?= =?UTF-8?q?umento)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../model/BaseEntity.java | 15 +- .../views/WorkDocumentsView.java | 176 +++++++++++------- 2 files changed, 115 insertions(+), 76 deletions(-) diff --git a/src/main/java/com/primefactorsolutions/model/BaseEntity.java b/src/main/java/com/primefactorsolutions/model/BaseEntity.java index 98b58d2..89f73b4 100644 --- a/src/main/java/com/primefactorsolutions/model/BaseEntity.java +++ b/src/main/java/com/primefactorsolutions/model/BaseEntity.java @@ -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; } } diff --git a/src/main/java/com/primefactorsolutions/views/WorkDocumentsView.java b/src/main/java/com/primefactorsolutions/views/WorkDocumentsView.java index d0ec2b0..b9e5b1b 100644 --- a/src/main/java/com/primefactorsolutions/views/WorkDocumentsView.java +++ b/src/main/java/com/primefactorsolutions/views/WorkDocumentsView.java @@ -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); } }