Compare commits
No commits in common. "4d12dfdd18a8406f6841f88e99fdac6554de4ac8" and "50a7809e6d2e61fa5492079b5b0b10df07fa70d8" have entirely different histories.
4d12dfdd18
...
50a7809e6d
@ -55,11 +55,7 @@ public abstract class BaseEntity {
|
|||||||
return fileName;
|
return fileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] getFileData() {
|
public byte[] getFileData() { return fileData; }
|
||||||
return fileData;
|
|
||||||
}
|
|
||||||
|
|
||||||
public DocumentType getDocumentType() {
|
public DocumentType getDocumentType() { return documentType; }
|
||||||
return documentType;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,23 @@
|
|||||||
package com.primefactorsolutions.service;
|
package com.primefactorsolutions.service;
|
||||||
|
|
||||||
|
//import com.primefactorsolutions.model.Candidate;
|
||||||
import com.primefactorsolutions.model.Document;
|
import com.primefactorsolutions.model.Document;
|
||||||
import com.primefactorsolutions.repositories.DocumentRepository;
|
import com.primefactorsolutions.repositories.DocumentRepository;
|
||||||
|
//import lombok.AllArgsConstructor;
|
||||||
|
//import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
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.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
@ -12,11 +26,11 @@ import java.util.Optional;
|
|||||||
public class DocumentService {
|
public class DocumentService {
|
||||||
private final DocumentRepository documentRepository;
|
private final DocumentRepository documentRepository;
|
||||||
|
|
||||||
public DocumentService(final DocumentRepository documentRepository) {
|
public DocumentService(DocumentRepository documentRepository) {
|
||||||
this.documentRepository = documentRepository;
|
this.documentRepository = documentRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void saveDocument(final Document newDocument) {
|
public void saveDocument(Document newDocument) {
|
||||||
documentRepository.save(newDocument);
|
documentRepository.save(newDocument);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -24,7 +38,7 @@ public class DocumentService {
|
|||||||
return documentRepository.findAll();
|
return documentRepository.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<Document> getDocumentById(final UUID id) {
|
public Optional<Document> getDocumentById(UUID id) {
|
||||||
return documentRepository.findById(id);
|
return documentRepository.findById(id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -34,8 +34,9 @@ public class WorkDocumentsView extends Main {
|
|||||||
private final DocumentService documentService;
|
private final DocumentService documentService;
|
||||||
private final MemoryBuffer buffer = new MemoryBuffer();
|
private final MemoryBuffer buffer = new MemoryBuffer();
|
||||||
private String lastUploadedFileName = null;
|
private String lastUploadedFileName = null;
|
||||||
|
private DocumentType currentDocumentType = null;
|
||||||
|
|
||||||
public WorkDocumentsView(final DocumentService documentService) {
|
public WorkDocumentsView(DocumentService documentService) {
|
||||||
this.documentService = documentService;
|
this.documentService = documentService;
|
||||||
initializeLayout();
|
initializeLayout();
|
||||||
}
|
}
|
||||||
@ -49,21 +50,13 @@ public class WorkDocumentsView extends Main {
|
|||||||
"Contract Approval MTEPS", DocumentType.CONTRACT_APPROVAL_MTEPS));
|
"Contract Approval MTEPS", DocumentType.CONTRACT_APPROVAL_MTEPS));
|
||||||
}
|
}
|
||||||
|
|
||||||
private HorizontalLayout createRow(
|
private HorizontalLayout createRow(String title1, DocumentType type1, String title2, DocumentType type2) {
|
||||||
final String title1,
|
|
||||||
final DocumentType type1,
|
|
||||||
final String title2,
|
|
||||||
final DocumentType type2) {
|
|
||||||
|
|
||||||
HorizontalLayout row = new HorizontalLayout();
|
HorizontalLayout row = new HorizontalLayout();
|
||||||
row.add(
|
row.add(createDocumentSection(title1, type1), createDocumentSection(title2, type2));
|
||||||
createDocumentSection(title1, type1),
|
|
||||||
createDocumentSection(title2, type2)
|
|
||||||
);
|
|
||||||
return row;
|
return row;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Div createDocumentSection(final String title, final DocumentType documentType) {
|
private Div createDocumentSection(String title, DocumentType documentType) {
|
||||||
Div section = new Div();
|
Div section = new Div();
|
||||||
section.add(new H2(title));
|
section.add(new H2(title));
|
||||||
|
|
||||||
@ -85,25 +78,25 @@ public class WorkDocumentsView extends Main {
|
|||||||
return upload;
|
return upload;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Button createViewButton(final DocumentType documentType) {
|
private Button createViewButton(DocumentType documentType) {
|
||||||
Button viewButton = new Button("View");
|
Button viewButton = new Button("View");
|
||||||
viewButton.setEnabled(documentExists(documentType));
|
viewButton.setEnabled(documentExists(documentType));
|
||||||
viewButton.addClickListener(event -> viewDocument(documentType));
|
viewButton.addClickListener(event -> viewDocument(documentType));
|
||||||
return viewButton;
|
return viewButton;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Button createSaveButton(final Button viewButton, final DocumentType documentType) {
|
private Button createSaveButton(Button viewButton, DocumentType documentType) {
|
||||||
Button saveButton = new Button("Save");
|
Button saveButton = new Button("Save");
|
||||||
saveButton.addClickListener(event -> saveFile(documentType, viewButton));
|
saveButton.addClickListener(event -> saveFile(documentType, viewButton));
|
||||||
return saveButton;
|
return saveButton;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleUploadSuccess(final String fileName) {
|
private void handleUploadSuccess(String fileName) {
|
||||||
lastUploadedFileName = fileName;
|
lastUploadedFileName = fileName;
|
||||||
Notification.show("File uploaded successfully");
|
Notification.show("File uploaded successfully");
|
||||||
}
|
}
|
||||||
|
|
||||||
private HorizontalLayout createLayout(final Component... components) {
|
private HorizontalLayout createLayout(Component... components) {
|
||||||
if (components.length != 1 && components.length != 3) {
|
if (components.length != 1 && components.length != 3) {
|
||||||
throw new IllegalArgumentException("This method only accepts 1 or 3 components.");
|
throw new IllegalArgumentException("This method only accepts 1 or 3 components.");
|
||||||
}
|
}
|
||||||
@ -116,7 +109,7 @@ public class WorkDocumentsView extends Main {
|
|||||||
return createLayout(new Button("Print"), new Button("Download"), new Button("Delete"));
|
return createLayout(new Button("Print"), new Button("Download"), new Button("Delete"));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void saveFile(final DocumentType documentType, final Button viewButton) {
|
private void saveFile(DocumentType documentType, Button viewButton) {
|
||||||
if (lastUploadedFileName == null) {
|
if (lastUploadedFileName == null) {
|
||||||
Notification.show("Please upload a file first.");
|
Notification.show("Please upload a file first.");
|
||||||
return;
|
return;
|
||||||
@ -131,24 +124,24 @@ public class WorkDocumentsView extends Main {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean documentExists(final DocumentType documentType) {
|
private boolean documentExists(DocumentType documentType) {
|
||||||
return documentService.getAllDocuments().stream()
|
return documentService.getAllDocuments().stream()
|
||||||
.anyMatch(doc -> doc.getDocumentType() == documentType);
|
.anyMatch(doc -> doc.getDocumentType() == documentType);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void viewDocument(final DocumentType documentType) {
|
private void viewDocument(DocumentType documentType) {
|
||||||
documentService.getAllDocuments().stream()
|
documentService.getAllDocuments().stream()
|
||||||
.filter(doc -> doc.getDocumentType() == documentType)
|
.filter(doc -> doc.getDocumentType() == documentType)
|
||||||
.findFirst()
|
.findFirst()
|
||||||
.ifPresentOrElse(this::showPdfDialog, () -> Notification.show("Document not found."));
|
.ifPresentOrElse(this::showPdfDialog, () -> Notification.show("Document not found."));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void showPdfDialog(final Document document) {
|
private void showPdfDialog(Document document) {
|
||||||
Dialog dialog = createDialog(document.getFileData());
|
Dialog dialog = createDialog(document.getFileData());
|
||||||
dialog.open();
|
dialog.open();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Dialog createDialog(final byte[] fileData) {
|
private Dialog createDialog(byte[] fileData) {
|
||||||
Dialog dialog = new Dialog();
|
Dialog dialog = new Dialog();
|
||||||
dialog.setModal(true);
|
dialog.setModal(true);
|
||||||
dialog.setCloseOnEsc(true);
|
dialog.setCloseOnEsc(true);
|
||||||
@ -167,7 +160,7 @@ public class WorkDocumentsView extends Main {
|
|||||||
return dialog;
|
return dialog;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String createPdfResource(final byte[] fileData) {
|
private String createPdfResource(byte[] fileData) {
|
||||||
return "data:application/pdf;base64," + Base64.getEncoder().encodeToString(fileData);
|
return "data:application/pdf;base64," + Base64.getEncoder().encodeToString(fileData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user