From ca307e2c0bf9379cd9baebe742bbcc700fb65101 Mon Sep 17 00:00:00 2001 From: ricardo051199 Date: Sun, 15 Sep 2024 03:09:38 -0400 Subject: [PATCH 1/4] =?UTF-8?q?#20=20Perfil=20de=20Personal=20Administrati?= =?UTF-8?q?vo=20-=20Carga=20de=20Documentaci=C3=B3n=20Personal=20(Ver,=20c?= =?UTF-8?q?argar=20y=20guardar)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../model/DocumentType.java | 4 + .../views/PersonalDocumentsView.java | 162 ++++++++++++++++++ 2 files changed, 166 insertions(+) diff --git a/src/main/java/com/primefactorsolutions/model/DocumentType.java b/src/main/java/com/primefactorsolutions/model/DocumentType.java index 007d080..f6e0bc8 100644 --- a/src/main/java/com/primefactorsolutions/model/DocumentType.java +++ b/src/main/java/com/primefactorsolutions/model/DocumentType.java @@ -9,5 +9,9 @@ public enum DocumentType { NDA, MEMORANDUMS, CONTRACT_APPROVAL_MTEPS, + BACKGROUND_CHECK_CERTIFICATE, + PRE_EMPLOYMENT_EVALUATION, + INSURANCE_REGISTRATION_FORM, + INSURANCE_CANCELLATION_FORM, OTHER } diff --git a/src/main/java/com/primefactorsolutions/views/PersonalDocumentsView.java b/src/main/java/com/primefactorsolutions/views/PersonalDocumentsView.java index 433b5c5..ca31c9e 100644 --- a/src/main/java/com/primefactorsolutions/views/PersonalDocumentsView.java +++ b/src/main/java/com/primefactorsolutions/views/PersonalDocumentsView.java @@ -1,16 +1,178 @@ 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; 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; +import java.util.Base64; + @SpringComponent @PermitAll @Scope("prototype") @PageTitle("PersonalDocuments") @Route(value = "/personal-documents/me", layout = MainLayout.class) 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); + } } -- 2.34.1 From 0744e16dea0fcd15975f807568ebb6d5b08fee2b Mon Sep 17 00:00:00 2001 From: ricardo051199 Date: Sun, 15 Sep 2024 03:22:18 -0400 Subject: [PATCH 2/4] =?UTF-8?q?#21=20Perfil=20de=20Personal=20Administrati?= =?UTF-8?q?vo=20-=20Carga=20de=20Documentaci=C3=B3n=20Profesional=20(Ver,?= =?UTF-8?q?=20cargar=20y=20guardar)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../model/DocumentType.java | 6 + .../views/ProfessionalDocumentsView.java | 157 ++++++++++++++++++ 2 files changed, 163 insertions(+) diff --git a/src/main/java/com/primefactorsolutions/model/DocumentType.java b/src/main/java/com/primefactorsolutions/model/DocumentType.java index f6e0bc8..e8f64d9 100644 --- a/src/main/java/com/primefactorsolutions/model/DocumentType.java +++ b/src/main/java/com/primefactorsolutions/model/DocumentType.java @@ -13,5 +13,11 @@ public enum DocumentType { PRE_EMPLOYMENT_EVALUATION, INSURANCE_REGISTRATION_FORM, INSURANCE_CANCELLATION_FORM, + PROFESSIONAL_DEGREE_1, + PROFESSIONAL_CERTIFICATE_1, + PROFESSIONAL_DEGREE_2, + PROFESSIONAL_CERTIFICATE_2, + PROFESSIONAL_DEGREE_3, + PROFESSIONAL_CERTIFICATE_3, OTHER } diff --git a/src/main/java/com/primefactorsolutions/views/ProfessionalDocumentsView.java b/src/main/java/com/primefactorsolutions/views/ProfessionalDocumentsView.java index 2b2badd..3148b50 100644 --- a/src/main/java/com/primefactorsolutions/views/ProfessionalDocumentsView.java +++ b/src/main/java/com/primefactorsolutions/views/ProfessionalDocumentsView.java @@ -1,16 +1,173 @@ 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; 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; +import java.util.Base64; + @SpringComponent @PermitAll @Scope("prototype") @PageTitle("ProfessionalDocuments") @Route(value = "/professional-documents/me", layout = MainLayout.class) public class ProfessionalDocumentsView extends Main { + private final DocumentService documentService; + private final MemoryBuffer buffer = new MemoryBuffer(); + private String lastUploadedFileName = null; + + public ProfessionalDocumentsView(final DocumentService documentService) { + this.documentService = documentService; + initializeLayout(); + } + + private void initializeLayout() { + add(createRow("Professional Degree 1", DocumentType.PROFESSIONAL_DEGREE_1, + "Professional Certificate 1", DocumentType.PROFESSIONAL_CERTIFICATE_1)); + add(createRow("Professional Degree 2", DocumentType.PROFESSIONAL_DEGREE_2, + "Professional Certificate 2", DocumentType.PROFESSIONAL_CERTIFICATE_2)); + add(createRow("Professional Degree 3", DocumentType.PROFESSIONAL_DEGREE_3, + "Professional Certificate 3", DocumentType.PROFESSIONAL_CERTIFICATE_3)); + } + + 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 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); + } } -- 2.34.1 From 109ddde243d5ebbead20dec482fd148e525fc410 Mon Sep 17 00:00:00 2001 From: ricardo051199 Date: Sun, 15 Sep 2024 17:52:43 -0400 Subject: [PATCH 3/4] =?UTF-8?q?#18=20Perfil=20de=20Personal=20Administrati?= =?UTF-8?q?vo=20-=20Carga=20de=20Documentaci=C3=B3n=20Corporativa=20(Ver,?= =?UTF-8?q?=20cargar=20y=20guardar)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../model/DocumentType.java | 6 + .../views/CorporateDocumentsView.java | 157 ++++++++++++++++++ 2 files changed, 163 insertions(+) diff --git a/src/main/java/com/primefactorsolutions/model/DocumentType.java b/src/main/java/com/primefactorsolutions/model/DocumentType.java index e8f64d9..ac1a994 100644 --- a/src/main/java/com/primefactorsolutions/model/DocumentType.java +++ b/src/main/java/com/primefactorsolutions/model/DocumentType.java @@ -19,5 +19,11 @@ public enum DocumentType { PROFESSIONAL_CERTIFICATE_2, PROFESSIONAL_DEGREE_3, PROFESSIONAL_CERTIFICATE_3, + GENERAL_LABOR_REGULATIONS, + REMOTE_WORK_GUIDELINES, + SAFETY_REGULATIONS, + HUMAN_RESOURCES_GUIDELINES, + ADMINISTRATION_FUNCTIONS_MANUAL, + ENGINEERING_FUNCTIONS_MANUAL, OTHER } diff --git a/src/main/java/com/primefactorsolutions/views/CorporateDocumentsView.java b/src/main/java/com/primefactorsolutions/views/CorporateDocumentsView.java index 7806438..c1e5c56 100644 --- a/src/main/java/com/primefactorsolutions/views/CorporateDocumentsView.java +++ b/src/main/java/com/primefactorsolutions/views/CorporateDocumentsView.java @@ -1,16 +1,173 @@ 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; 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; +import java.util.Base64; + @SpringComponent @PermitAll @Scope("prototype") @PageTitle("CorporateDocuments") @Route(value = "/corporate-documents/me", layout = MainLayout.class) public class CorporateDocumentsView extends Main { + private final DocumentService documentService; + private final MemoryBuffer buffer = new MemoryBuffer(); + private String lastUploadedFileName = null; + + public CorporateDocumentsView(final DocumentService documentService) { + this.documentService = documentService; + initializeLayout(); + } + + private void initializeLayout() { + add(createRow("General Labor Regulations", DocumentType.GENERAL_LABOR_REGULATIONS, + "Remote Work Guidelines", DocumentType.REMOTE_WORK_GUIDELINES)); + add(createRow("Safety Regulations", DocumentType.SAFETY_REGULATIONS, + "Human Resources Guidelines", DocumentType.HUMAN_RESOURCES_GUIDELINES)); + add(createRow("Administration Functions Manual", DocumentType.ADMINISTRATION_FUNCTIONS_MANUAL, + "Engineering Functions Manual", DocumentType.ENGINEERING_FUNCTIONS_MANUAL)); + } + + 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 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); + } } -- 2.34.1 From f2a1d53e4a71aa2a8a4abf0b65b042a345cc8b1d Mon Sep 17 00:00:00 2001 From: ricardo051199 Date: Sun, 15 Sep 2024 18:13:11 -0400 Subject: [PATCH 4/4] =?UTF-8?q?#19=20Perfil=20de=20Personal=20Administrati?= =?UTF-8?q?vo=20-=20Cargar=20Documentaci=C3=B3n=20de=20Gobierno=20(Ver,=20?= =?UTF-8?q?cargar=20y=20guardar)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../model/BaseEntity.java | 9 + .../model/DocumentType.java | 6 + .../primefactorsolutions/model/Employee.java | 93 +++++----- .../views/EmployeesListView.java | 6 - .../views/GovernmentDocumentsView.java | 165 ++++++++++++++++++ 5 files changed, 223 insertions(+), 56 deletions(-) diff --git a/src/main/java/com/primefactorsolutions/model/BaseEntity.java b/src/main/java/com/primefactorsolutions/model/BaseEntity.java index 38f3d3f..09b1382 100644 --- a/src/main/java/com/primefactorsolutions/model/BaseEntity.java +++ b/src/main/java/com/primefactorsolutions/model/BaseEntity.java @@ -10,6 +10,7 @@ public abstract class BaseEntity { @Id @GeneratedValue(strategy = GenerationType.UUID) private UUID id; + private Employee.Status status; private String fileName; @Lob @Column(columnDefinition = "BLOB") @@ -62,4 +63,12 @@ public abstract class BaseEntity { public DocumentType getDocumentType() { return documentType; } + + public void setStatus(Employee.Status status) { + this.status = status; + } + + public Employee.Status getStatus() { + return status; + } } diff --git a/src/main/java/com/primefactorsolutions/model/DocumentType.java b/src/main/java/com/primefactorsolutions/model/DocumentType.java index ac1a994..138439e 100644 --- a/src/main/java/com/primefactorsolutions/model/DocumentType.java +++ b/src/main/java/com/primefactorsolutions/model/DocumentType.java @@ -25,5 +25,11 @@ public enum DocumentType { HUMAN_RESOURCES_GUIDELINES, ADMINISTRATION_FUNCTIONS_MANUAL, ENGINEERING_FUNCTIONS_MANUAL, + GENERAL_LABOR_LAW, + SUPREME_DECREE, + REGULATORY_RESOLUTION, + COMPLEMENTARY_REGULATION, + HEALTH_SAFETY_LAW, + INTERNSHIP_RULES, OTHER } diff --git a/src/main/java/com/primefactorsolutions/model/Employee.java b/src/main/java/com/primefactorsolutions/model/Employee.java index 4f5a49e..be9d163 100644 --- a/src/main/java/com/primefactorsolutions/model/Employee.java +++ b/src/main/java/com/primefactorsolutions/model/Employee.java @@ -1,52 +1,45 @@ - package com.primefactorsolutions.model; - import jakarta.persistence.*; - import lombok.AllArgsConstructor; - import lombok.Data; - import lombok.EqualsAndHashCode; - import lombok.NoArgsConstructor; +package com.primefactorsolutions.model; +import jakarta.persistence.*; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; +import java.time.LocalDate; - import java.time.LocalDate; - - @Data - @Entity - @AllArgsConstructor - @NoArgsConstructor - @EqualsAndHashCode(callSuper = true) - public class Employee extends BaseEntity { - private String userName; - private String firstName; - private String lastName; - private LocalDate birthday; - private String birthCity; - @Enumerated(EnumType.STRING) - private MaritalStatus maritalStatus; - private String residenceAddress; - private String phoneNumber; - private String personalEmail; - private String position; - private String team; - private String emergencyCName; - private String emergencyCAddress; - private String emergencyCPhone; - private String emergencyCEmail; - @Column(columnDefinition = "TEXT") - private String profileImage; - @Enumerated(EnumType.STRING) - private Status status; - public enum Status { - ACTIVE, - INACTIVE - } - public enum MaritalStatus { - SINGLE, - MARRIED, - WIDOWED, - DIVORCED - } - public Status getStatus() { - return status; - } - public void setStatus(final Status status) { - this.status = status; - } +@Data +@Entity +@AllArgsConstructor +@NoArgsConstructor +@EqualsAndHashCode(callSuper = true) +public class Employee extends BaseEntity { + private String userName; + private String firstName; + private String lastName; + private LocalDate birthday; + private String birthCity; + @Enumerated(EnumType.STRING) + private MaritalStatus maritalStatus; + private String residenceAddress; + private String phoneNumber; + private String personalEmail; + private String position; + private String team; + private String emergencyCName; + private String emergencyCAddress; + private String emergencyCPhone; + private String emergencyCEmail; + @Column(columnDefinition = "TEXT") + private String profileImage; + @Enumerated(EnumType.STRING) + private Status status; + public enum Status { + ACTIVE, + INACTIVE } + public enum MaritalStatus { + SINGLE, + MARRIED, + WIDOWED, + DIVORCED + } +} \ No newline at end of file diff --git a/src/main/java/com/primefactorsolutions/views/EmployeesListView.java b/src/main/java/com/primefactorsolutions/views/EmployeesListView.java index fed246b..15fc02a 100644 --- a/src/main/java/com/primefactorsolutions/views/EmployeesListView.java +++ b/src/main/java/com/primefactorsolutions/views/EmployeesListView.java @@ -47,12 +47,6 @@ public class EmployeesListView extends Main { setupPagingGrid(); } - private void updateEmployeeStatus(final Employee employee, final boolean isActive) { - employee.setStatus(isActive ? Employee.Status.ACTIVE : Employee.Status.INACTIVE); - employeeService.createOrUpdate(employee); - refreshGrid(); - } - private void addEditButtonColumn(final String label, final ButtonClickHandler handler) { table.addComponentColumn(employee -> createButton(label, () -> handler.handle(employee))); } diff --git a/src/main/java/com/primefactorsolutions/views/GovernmentDocumentsView.java b/src/main/java/com/primefactorsolutions/views/GovernmentDocumentsView.java index a06e4bf..e3ad798 100644 --- a/src/main/java/com/primefactorsolutions/views/GovernmentDocumentsView.java +++ b/src/main/java/com/primefactorsolutions/views/GovernmentDocumentsView.java @@ -1,16 +1,181 @@ 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; 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; +import java.util.Base64; + @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 MemoryBuffer buffer = new MemoryBuffer(); + private String lastUploadedFileName = null; + + public GovernmentDocumentsView(final DocumentService documentService) { + this.documentService = documentService; + initializeLayout(); + } + + private void initializeLayout() { + add(createRow( + "General Labor Law", DocumentType.GENERAL_LABOR_LAW, + "Supreme Decrees", DocumentType.SUPREME_DECREE + )); + + add(createRow( + "Regulatory Resolutions/Provisions", DocumentType.REGULATORY_RESOLUTION, + "Complementary Regulations", DocumentType.COMPLEMENTARY_REGULATION + )); + + add(createRow( + "General Law on Hygiene, Health, Occupational Safety, and Well-being", DocumentType.HEALTH_SAFETY_LAW, + "Regulatory Framework for Internship Development", DocumentType.INTERNSHIP_RULES + )); + } + + 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 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); + } } -- 2.34.1