From 5bbdb5f2ee252ce342cb10e2b7677972fe607a8a Mon Sep 17 00:00:00 2001 From: ricardo051199 Date: Sat, 14 Sep 2024 15:30:51 -0400 Subject: [PATCH] =?UTF-8?q?#17=20Perfil=20de=20Personal=20Administrativo?= =?UTF-8?q?=20-=20Carga=20de=20Documentaci=C3=B3n=20Laboral=20(Funcionalid?= =?UTF-8?q?ades=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; + }; + } }