From 613221dfb6631979496774f48444db58558fb038 Mon Sep 17 00:00:00 2001 From: ricardo051199 Date: Thu, 19 Sep 2024 00:56:44 -0400 Subject: [PATCH] Refactorizacion del modulo de documentos --- .../model/BaseEntity.java | 28 --- .../primefactorsolutions/model/Document.java | 2 +- .../views/CorporateDocumentsView.java | 150 +---------------- .../views/DocumentsView.java | 159 +++++++++++++++++- .../views/EmployeeView.java | 4 +- .../views/GovernmentDocumentsView.java | 150 +---------------- .../views/PersonalDocumentsView.java | 158 +---------------- .../views/ProfessionalDocumentsView.java | 150 +---------------- .../views/WorkDocumentsView.java | 150 +---------------- 9 files changed, 182 insertions(+), 769 deletions(-) diff --git a/src/main/java/com/primefactorsolutions/model/BaseEntity.java b/src/main/java/com/primefactorsolutions/model/BaseEntity.java index 065f3fe..381a9c6 100644 --- a/src/main/java/com/primefactorsolutions/model/BaseEntity.java +++ b/src/main/java/com/primefactorsolutions/model/BaseEntity.java @@ -10,14 +10,6 @@ public abstract class BaseEntity { @Id @GeneratedValue(strategy = GenerationType.UUID) private UUID id; - @Enumerated(EnumType.STRING) - private Employee.Status status; - private String fileName; - @Lob - @Column(columnDefinition = "BLOB") - private byte[] fileData; - @Enumerated(EnumType.STRING) - private DocumentType documentType; @Version private int version; @@ -52,24 +44,4 @@ public abstract class BaseEntity { } return super.equals(that); } - - public String getFileName() { - return fileName; - } - - public byte[] getFileData() { - return fileData; - } - - public DocumentType getDocumentType() { - return documentType; - } - - public void setStatusEmployee(Employee.Status status) { - this.status = status; - } - - public Employee.Status getStatusEmployee() { - return status; - } } diff --git a/src/main/java/com/primefactorsolutions/model/Document.java b/src/main/java/com/primefactorsolutions/model/Document.java index ce944b6..3b8b200 100644 --- a/src/main/java/com/primefactorsolutions/model/Document.java +++ b/src/main/java/com/primefactorsolutions/model/Document.java @@ -4,8 +4,8 @@ import jakarta.persistence.*; import lombok.*; -@Entity @Data +@Entity @AllArgsConstructor @NoArgsConstructor @EqualsAndHashCode(callSuper = true) diff --git a/src/main/java/com/primefactorsolutions/views/CorporateDocumentsView.java b/src/main/java/com/primefactorsolutions/views/CorporateDocumentsView.java index c1e5c56..9810e85 100644 --- a/src/main/java/com/primefactorsolutions/views/CorporateDocumentsView.java +++ b/src/main/java/com/primefactorsolutions/views/CorporateDocumentsView.java @@ -1,46 +1,26 @@ 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 class CorporateDocumentsView extends DocumentsView { public CorporateDocumentsView(final DocumentService documentService) { - this.documentService = documentService; - initializeLayout(); + super(documentService); } - private void initializeLayout() { + @Override + protected 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, @@ -48,126 +28,4 @@ public class CorporateDocumentsView extends Main { 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); - } } diff --git a/src/main/java/com/primefactorsolutions/views/DocumentsView.java b/src/main/java/com/primefactorsolutions/views/DocumentsView.java index 6a41a66..545472c 100644 --- a/src/main/java/com/primefactorsolutions/views/DocumentsView.java +++ b/src/main/java/com/primefactorsolutions/views/DocumentsView.java @@ -1,17 +1,174 @@ 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("Documents") @Route(value = "/documents/me", layout = MainLayout.class) -public class DocumentsView extends Main { +public abstract class DocumentsView extends Main { + private final DocumentService documentService; + private final MemoryBuffer buffer = new MemoryBuffer(); + private String lastUploadedFileName = null; + + public DocumentsView(final DocumentService documentService) { + this.documentService = documentService; + initializeLayout(); + } + + protected abstract void initializeLayout(); + + protected 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; + } + + protected HorizontalLayout createRow(final String title1, final DocumentType type1) { + HorizontalLayout row = new HorizontalLayout(); + row.add(createDocumentSection(title1, type1)); + 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); + } + } diff --git a/src/main/java/com/primefactorsolutions/views/EmployeeView.java b/src/main/java/com/primefactorsolutions/views/EmployeeView.java index 42e95e2..8b18f3b 100644 --- a/src/main/java/com/primefactorsolutions/views/EmployeeView.java +++ b/src/main/java/com/primefactorsolutions/views/EmployeeView.java @@ -199,7 +199,7 @@ public class EmployeeView extends BeanValidationForm implements HasUrl private void saveEmployee() { if (validateForm()) { Employee employee = getEntity(); - employee.setStatusEmployee(status.getValue()); + employee.setStatus(status.getValue()); employeeService.createOrUpdate(employee); Notification.show(NOTIFICATION_SAVE_SUCCESS); getUI().ifPresent(ui -> ui.navigate(EmployeesListView.class)); @@ -233,7 +233,7 @@ public class EmployeeView extends BeanValidationForm implements HasUrl if ("edit".equals(action) && !s.isEmpty()) { saveButton.setVisible(true); editButton.setVisible(false); - status.setValue(employee.getStatusEmployee()); + status.setValue(employee.getStatus()); setFieldsEditable(); } else if ("view".equals(action) && !s.isEmpty()) { setFieldsReadOnly(); diff --git a/src/main/java/com/primefactorsolutions/views/GovernmentDocumentsView.java b/src/main/java/com/primefactorsolutions/views/GovernmentDocumentsView.java index e3ad798..826be3d 100644 --- a/src/main/java/com/primefactorsolutions/views/GovernmentDocumentsView.java +++ b/src/main/java/com/primefactorsolutions/views/GovernmentDocumentsView.java @@ -1,46 +1,26 @@ 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 class GovernmentDocumentsView extends DocumentsView { public GovernmentDocumentsView(final DocumentService documentService) { - this.documentService = documentService; - initializeLayout(); + super(documentService); } - private void initializeLayout() { + @Override + protected void initializeLayout() { add(createRow( "General Labor Law", DocumentType.GENERAL_LABOR_LAW, "Supreme Decrees", DocumentType.SUPREME_DECREE @@ -56,126 +36,4 @@ public class GovernmentDocumentsView extends Main { "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); - } } diff --git a/src/main/java/com/primefactorsolutions/views/PersonalDocumentsView.java b/src/main/java/com/primefactorsolutions/views/PersonalDocumentsView.java index ca31c9e..b56535e 100644 --- a/src/main/java/com/primefactorsolutions/views/PersonalDocumentsView.java +++ b/src/main/java/com/primefactorsolutions/views/PersonalDocumentsView.java @@ -1,178 +1,30 @@ 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 class PersonalDocumentsView extends DocumentsView { public PersonalDocumentsView(final DocumentService documentService) { - this.documentService = documentService; - initializeLayout(); + super(documentService); } - private void initializeLayout() { + @Override + protected 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); + add(createRow("Insurance cancellation form", DocumentType.INSURANCE_CANCELLATION_FORM)); } } diff --git a/src/main/java/com/primefactorsolutions/views/ProfessionalDocumentsView.java b/src/main/java/com/primefactorsolutions/views/ProfessionalDocumentsView.java index 3148b50..9f8ec85 100644 --- a/src/main/java/com/primefactorsolutions/views/ProfessionalDocumentsView.java +++ b/src/main/java/com/primefactorsolutions/views/ProfessionalDocumentsView.java @@ -1,46 +1,26 @@ 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 class ProfessionalDocumentsView extends DocumentsView { public ProfessionalDocumentsView(final DocumentService documentService) { - this.documentService = documentService; - initializeLayout(); + super(documentService); } - private void initializeLayout() { + @Override + protected 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, @@ -48,126 +28,4 @@ public class ProfessionalDocumentsView extends Main { 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); - } } diff --git a/src/main/java/com/primefactorsolutions/views/WorkDocumentsView.java b/src/main/java/com/primefactorsolutions/views/WorkDocumentsView.java index 29421d8..c3b7892 100644 --- a/src/main/java/com/primefactorsolutions/views/WorkDocumentsView.java +++ b/src/main/java/com/primefactorsolutions/views/WorkDocumentsView.java @@ -1,46 +1,26 @@ 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("WorkDocuments") @Route(value = "/work-documents/me", layout = MainLayout.class) -public class WorkDocumentsView extends Main { - private final DocumentService documentService; - private final MemoryBuffer buffer = new MemoryBuffer(); - private String lastUploadedFileName = null; +public class WorkDocumentsView extends DocumentsView { public WorkDocumentsView(final DocumentService documentService) { - this.documentService = documentService; - initializeLayout(); + super(documentService); } - private void initializeLayout() { + @Override + protected void initializeLayout() { add(createRow("PaySlips", DocumentType.PAY_SLIPS, "Employment Contract", DocumentType.EMPLOYMENT_CONTRACT)); add(createRow("Work Certificates", DocumentType.WORK_CERTIFICATES, @@ -48,126 +28,4 @@ public class WorkDocumentsView extends Main { add(createRow("Memorandums", DocumentType.MEMORANDUMS, "Contract Approval MTEPS", DocumentType.CONTRACT_APPROVAL_MTEPS)); } - - 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); - } }