Compare commits
No commits in common. "97c07a45583fffc5c1c4ee26f4b5e0eb10a68031" and "1887214ed2983ca128241c8b6f7e5dc1799111f2" have entirely different histories.
97c07a4558
...
1887214ed2
@ -1,6 +1,10 @@
|
|||||||
package com.primefactorsolutions.model;
|
package com.primefactorsolutions.model;
|
||||||
|
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.GeneratedValue;
|
||||||
|
import jakarta.persistence.GenerationType;
|
||||||
|
import jakarta.persistence.Id;
|
||||||
|
import jakarta.persistence.MappedSuperclass;
|
||||||
|
import jakarta.persistence.Version;
|
||||||
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@ -10,12 +14,6 @@ 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;
|
||||||
@ -50,12 +48,4 @@ 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; }
|
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,12 @@
|
|||||||
package com.primefactorsolutions.model;
|
package com.primefactorsolutions.model;
|
||||||
|
|
||||||
import jakarta.persistence.*;
|
import jakarta.annotation.Nullable;
|
||||||
|
import jakarta.persistence.Entity;
|
||||||
import lombok.*;
|
import jakarta.persistence.ManyToOne;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Data
|
@Data
|
||||||
@ -10,10 +14,10 @@ import lombok.*;
|
|||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@EqualsAndHashCode(callSuper = true)
|
@EqualsAndHashCode(callSuper = true)
|
||||||
public class Document extends BaseEntity {
|
public class Document extends BaseEntity {
|
||||||
private String fileName;
|
@Nullable
|
||||||
@Enumerated(EnumType.STRING)
|
private String description;
|
||||||
|
private String location;
|
||||||
private DocumentType documentType;
|
private DocumentType documentType;
|
||||||
@Lob
|
@ManyToOne
|
||||||
@Column(columnDefinition = "BLOB")
|
private Employee employee;
|
||||||
private byte[] fileData;
|
|
||||||
}
|
}
|
||||||
|
@ -3,11 +3,5 @@ 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
|
||||||
}
|
}
|
||||||
|
@ -1,9 +0,0 @@
|
|||||||
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> {
|
|
||||||
}
|
|
@ -1,44 +0,0 @@
|
|||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,79 +1,16 @@
|
|||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,166 +1,47 @@
|
|||||||
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(DocumentService documentService) {
|
public WorkDocumentsView() {
|
||||||
this.documentService = documentService;
|
HorizontalLayout row1 = new HorizontalLayout();
|
||||||
initializeLayout();
|
row1.add(createSection("Pay Slips"), createSection("Employment Contract"));
|
||||||
|
HorizontalLayout row2 = new HorizontalLayout();
|
||||||
|
row2.add(createSection("Work Certificates"), createSection("NDA"));
|
||||||
|
HorizontalLayout row3 = new HorizontalLayout();
|
||||||
|
row3.add(createSection("Memorandums"), createSection("Contract Approval MTEPS"));
|
||||||
|
add(row1, row2, row3);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initializeLayout() {
|
private Div createSection(String sectionTitle) {
|
||||||
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(title));
|
section.add(new H2(sectionTitle));
|
||||||
|
HorizontalLayout row1 = new HorizontalLayout();
|
||||||
Upload upload = createUploadComponent();
|
row1.setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER);
|
||||||
Button viewButton = createViewButton(documentType);
|
row1.add(new Button("View"), new Button("Upload"), new Button("Save"));
|
||||||
Button saveButton = createSaveButton(viewButton, documentType);
|
HorizontalLayout row2 = new HorizontalLayout();
|
||||||
|
row2.setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER);
|
||||||
section.add(createLayout(upload),
|
row2.add(new Button("Edit"), new Button("Print"), new Button("Delete"));
|
||||||
createLayout(viewButton, new Button("Edit"), saveButton),
|
HorizontalLayout row3 = new HorizontalLayout();
|
||||||
createAdditionalButtons());
|
row3.setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER);
|
||||||
|
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user