Compare commits

..

3 Commits

7 changed files with 284 additions and 37 deletions

View File

@ -1,10 +1,6 @@
package com.primefactorsolutions.model; package com.primefactorsolutions.model;
import jakarta.persistence.GeneratedValue; import jakarta.persistence.*;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.MappedSuperclass;
import jakarta.persistence.Version;
import java.util.UUID; import java.util.UUID;
@ -14,6 +10,12 @@ public abstract class BaseEntity {
@Id @Id
@GeneratedValue(strategy = GenerationType.UUID) @GeneratedValue(strategy = GenerationType.UUID)
private UUID id; private UUID id;
private String fileName;
@Lob
@Column(columnDefinition = "BLOB")
private byte[] fileData;
@Enumerated(EnumType.STRING)
private DocumentType documentType;
@Version @Version
private int version; private int version;
@ -48,4 +50,12 @@ public abstract class BaseEntity {
} }
return super.equals(that); return super.equals(that);
} }
public String getFileName() {
return fileName;
}
public byte[] getFileData() { return fileData; }
public DocumentType getDocumentType() { return documentType; }
} }

View File

@ -1,12 +1,8 @@
package com.primefactorsolutions.model; package com.primefactorsolutions.model;
import jakarta.annotation.Nullable; import jakarta.persistence.*;
import jakarta.persistence.Entity;
import jakarta.persistence.ManyToOne; import lombok.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
@Entity @Entity
@Data @Data
@ -14,10 +10,10 @@ import lombok.NoArgsConstructor;
@NoArgsConstructor @NoArgsConstructor
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode(callSuper = true)
public class Document extends BaseEntity { public class Document extends BaseEntity {
@Nullable private String fileName;
private String description; @Enumerated(EnumType.STRING)
private String location;
private DocumentType documentType; private DocumentType documentType;
@ManyToOne @Lob
private Employee employee; @Column(columnDefinition = "BLOB")
private byte[] fileData;
} }

View File

@ -3,5 +3,11 @@ package com.primefactorsolutions.model;
public enum DocumentType { public enum DocumentType {
ID_CARD, ID_CARD,
PAY_STUB, PAY_STUB,
PAY_SLIPS,
EMPLOYMENT_CONTRACT,
WORK_CERTIFICATES,
NDA,
MEMORANDUMS,
CONTRACT_APPROVAL_MTEPS,
OTHER OTHER
} }

View File

@ -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<Document, UUID> {
}

View File

@ -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<Document> getAllDocuments() {
return documentRepository.findAll();
}
public Optional<Document> getDocumentById(UUID id) {
return documentRepository.findById(id);
}
}

View File

@ -1,16 +1,79 @@
package com.primefactorsolutions.views; 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.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.PageTitle;
import com.vaadin.flow.router.Route; import com.vaadin.flow.router.Route;
import com.vaadin.flow.server.StreamResource;
import com.vaadin.flow.spring.annotation.SpringComponent; import com.vaadin.flow.spring.annotation.SpringComponent;
import jakarta.annotation.security.PermitAll; import jakarta.annotation.security.PermitAll;
import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.Scope;
import java.io.ByteArrayInputStream;
import java.io.IOException;
//import java.io.InputStream;
import java.util.UUID;
@SpringComponent @SpringComponent
@PermitAll @PermitAll
@Scope("prototype") @Scope("prototype")
@PageTitle("GovernmentDocumentsView") @PageTitle("GovernmentDocumentsView")
@Route(value = "/government-documents/me", layout = MainLayout.class) @Route(value = "/government-documents/me", layout = MainLayout.class)
public class GovernmentDocumentsView extends Main { 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);
}
} }

View File

@ -1,47 +1,166 @@
package com.primefactorsolutions.views; package com.primefactorsolutions.views;
import com.primefactorsolutions.model.Document;
import com.primefactorsolutions.model.DocumentType;
import com.primefactorsolutions.service.DocumentService;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.button.Button; import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.dialog.Dialog;
import com.vaadin.flow.component.html.Div; import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.H2; import com.vaadin.flow.component.html.H2;
import com.vaadin.flow.component.html.IFrame;
import com.vaadin.flow.component.html.Main; import com.vaadin.flow.component.html.Main;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.orderedlayout.FlexComponent; import com.vaadin.flow.component.orderedlayout.FlexComponent;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout; import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.upload.Upload;
import com.vaadin.flow.component.upload.receivers.MemoryBuffer;
import com.vaadin.flow.router.PageTitle; import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route; import com.vaadin.flow.router.Route;
import com.vaadin.flow.spring.annotation.SpringComponent; import com.vaadin.flow.spring.annotation.SpringComponent;
import jakarta.annotation.security.PermitAll; import jakarta.annotation.security.PermitAll;
import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.Scope;
import java.io.IOException;
import java.util.Base64;
@SpringComponent @SpringComponent
@PermitAll @PermitAll
@Scope("prototype") @Scope("prototype")
@PageTitle("WorkDocuments") @PageTitle("WorkDocuments")
@Route(value = "/work-documents/me", layout = MainLayout.class) @Route(value = "/work-documents/me", layout = MainLayout.class)
public class WorkDocumentsView extends Main { public class WorkDocumentsView extends Main {
private final DocumentService documentService;
private final MemoryBuffer buffer = new MemoryBuffer();
private String lastUploadedFileName = null;
private DocumentType currentDocumentType = null;
public WorkDocumentsView() { public WorkDocumentsView(DocumentService documentService) {
HorizontalLayout row1 = new HorizontalLayout(); this.documentService = documentService;
row1.add(createSection("Pay Slips"), createSection("Employment Contract")); initializeLayout();
HorizontalLayout row2 = new HorizontalLayout();
row2.add(createSection("Work Certificates"), createSection("NDA"));
HorizontalLayout row3 = new HorizontalLayout();
row3.add(createSection("Memorandums"), createSection("Contract Approval MTEPS"));
add(row1, row2, row3);
} }
private Div createSection(String sectionTitle) { private void initializeLayout() {
add(createRow("PaySlips", DocumentType.PAY_SLIPS,
"Employment Contract", DocumentType.EMPLOYMENT_CONTRACT));
add(createRow("Work Certificates", DocumentType.WORK_CERTIFICATES,
"NDA", DocumentType.NDA));
add(createRow("Memorandums", DocumentType.MEMORANDUMS,
"Contract Approval MTEPS", DocumentType.CONTRACT_APPROVAL_MTEPS));
}
private HorizontalLayout createRow(String title1, DocumentType type1, String title2, DocumentType type2) {
HorizontalLayout row = new HorizontalLayout();
row.add(createDocumentSection(title1, type1), createDocumentSection(title2, type2));
return row;
}
private Div createDocumentSection(String title, DocumentType documentType) {
Div section = new Div(); Div section = new Div();
section.add(new H2(sectionTitle)); section.add(new H2(title));
HorizontalLayout row1 = new HorizontalLayout();
row1.setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER); Upload upload = createUploadComponent();
row1.add(new Button("View"), new Button("Upload"), new Button("Save")); Button viewButton = createViewButton(documentType);
HorizontalLayout row2 = new HorizontalLayout(); Button saveButton = createSaveButton(viewButton, documentType);
row2.setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER);
row2.add(new Button("Edit"), new Button("Print"), new Button("Delete")); section.add(createLayout(upload),
HorizontalLayout row3 = new HorizontalLayout(); createLayout(viewButton, new Button("Edit"), saveButton),
row3.setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER); createAdditionalButtons());
row3.add(new Button("Download"));
section.add(row1, row2, row3);
return section; 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(DocumentType documentType) {
Button viewButton = new Button("View");
viewButton.setEnabled(documentExists(documentType));
viewButton.addClickListener(event -> viewDocument(documentType));
return viewButton;
}
private Button createSaveButton(Button viewButton, DocumentType documentType) {
Button saveButton = new Button("Save");
saveButton.addClickListener(event -> saveFile(documentType, viewButton));
return saveButton;
}
private void handleUploadSuccess(String fileName) {
lastUploadedFileName = fileName;
Notification.show("File uploaded successfully");
}
private HorizontalLayout createLayout(Component... components) {
if (components.length != 1 && components.length != 3) {
throw new IllegalArgumentException("This method only accepts 1 or 3 components.");
}
HorizontalLayout layout = new HorizontalLayout(components);
layout.setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER);
return layout;
}
private HorizontalLayout createAdditionalButtons() {
return createLayout(new Button("Print"), new Button("Download"), new Button("Delete"));
}
private void saveFile(DocumentType documentType, Button viewButton) {
if (lastUploadedFileName == null) {
Notification.show("Please upload a file first.");
return;
}
try {
byte[] content = buffer.getInputStream().readAllBytes();
documentService.saveDocument(new Document(lastUploadedFileName, documentType, content));
Notification.show("File saved successfully.");
viewButton.setEnabled(true);
} catch (IOException e) {
Notification.show("Error saving file: " + e.getMessage());
}
}
private boolean documentExists(DocumentType documentType) {
return documentService.getAllDocuments().stream()
.anyMatch(doc -> doc.getDocumentType() == documentType);
}
private void viewDocument(DocumentType documentType) {
documentService.getAllDocuments().stream()
.filter(doc -> doc.getDocumentType() == documentType)
.findFirst()
.ifPresentOrElse(this::showPdfDialog, () -> Notification.show("Document not found."));
}
private void showPdfDialog(Document document) {
Dialog dialog = createDialog(document.getFileData());
dialog.open();
}
private Dialog createDialog(byte[] fileData) {
Dialog dialog = new Dialog();
dialog.setModal(true);
dialog.setCloseOnEsc(true);
dialog.setCloseOnOutsideClick(true);
IFrame pdfFrame = new IFrame();
pdfFrame.setSrc(createPdfResource(fileData));
pdfFrame.setWidth("800px");
pdfFrame.setHeight("600px");
Button closeButton = new Button("Close", event -> dialog.close());
VerticalLayout layout = new VerticalLayout(pdfFrame, closeButton);
layout.setAlignItems(FlexComponent.Alignment.CENTER);
dialog.add(layout);
return dialog;
}
private String createPdfResource(byte[] fileData) {
return "data:application/pdf;base64," + Base64.getEncoder().encodeToString(fileData);
}
} }