Compare commits

...

2 Commits

4 changed files with 31 additions and 97 deletions

View File

@ -55,7 +55,11 @@ public abstract class BaseEntity {
return fileName; return fileName;
} }
public byte[] getFileData() { return fileData; } public byte[] getFileData() {
return fileData;
}
public DocumentType getDocumentType() { return documentType; } public DocumentType getDocumentType() {
return documentType;
}
} }

View File

@ -1,23 +1,9 @@
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;
@ -26,11 +12,11 @@ import java.util.Optional;
public class DocumentService { public class DocumentService {
private final DocumentRepository documentRepository; private final DocumentRepository documentRepository;
public DocumentService(DocumentRepository documentRepository) { public DocumentService(final DocumentRepository documentRepository) {
this.documentRepository = documentRepository; this.documentRepository = documentRepository;
} }
public void saveDocument(Document newDocument) { public void saveDocument(final Document newDocument) {
documentRepository.save(newDocument); documentRepository.save(newDocument);
} }
@ -38,7 +24,7 @@ public class DocumentService {
return documentRepository.findAll(); return documentRepository.findAll();
} }
public Optional<Document> getDocumentById(UUID id) { public Optional<Document> getDocumentById(final UUID id) {
return documentRepository.findById(id); return documentRepository.findById(id);
} }
} }

View File

@ -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);
}
} }

View File

@ -34,9 +34,8 @@ 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(DocumentService documentService) { public WorkDocumentsView(final DocumentService documentService) {
this.documentService = documentService; this.documentService = documentService;
initializeLayout(); initializeLayout();
} }
@ -50,13 +49,21 @@ public class WorkDocumentsView extends Main {
"Contract Approval MTEPS", DocumentType.CONTRACT_APPROVAL_MTEPS)); "Contract Approval MTEPS", DocumentType.CONTRACT_APPROVAL_MTEPS));
} }
private HorizontalLayout createRow(String title1, DocumentType type1, String title2, DocumentType type2) { private HorizontalLayout createRow(
final String title1,
final DocumentType type1,
final String title2,
final DocumentType type2) {
HorizontalLayout row = new HorizontalLayout(); HorizontalLayout row = new HorizontalLayout();
row.add(createDocumentSection(title1, type1), createDocumentSection(title2, type2)); row.add(
createDocumentSection(title1, type1),
createDocumentSection(title2, type2)
);
return row; return row;
} }
private Div createDocumentSection(String title, DocumentType documentType) { private Div createDocumentSection(final String title, final DocumentType documentType) {
Div section = new Div(); Div section = new Div();
section.add(new H2(title)); section.add(new H2(title));
@ -78,25 +85,25 @@ public class WorkDocumentsView extends Main {
return upload; return upload;
} }
private Button createViewButton(DocumentType documentType) { private Button createViewButton(final 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(Button viewButton, DocumentType documentType) { private Button createSaveButton(final Button viewButton, final 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(String fileName) { private void handleUploadSuccess(final String fileName) {
lastUploadedFileName = fileName; lastUploadedFileName = fileName;
Notification.show("File uploaded successfully"); Notification.show("File uploaded successfully");
} }
private HorizontalLayout createLayout(Component... components) { private HorizontalLayout createLayout(final 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.");
} }
@ -109,7 +116,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(DocumentType documentType, Button viewButton) { private void saveFile(final DocumentType documentType, final Button viewButton) {
if (lastUploadedFileName == null) { if (lastUploadedFileName == null) {
Notification.show("Please upload a file first."); Notification.show("Please upload a file first.");
return; return;
@ -124,24 +131,24 @@ public class WorkDocumentsView extends Main {
} }
} }
private boolean documentExists(DocumentType documentType) { private boolean documentExists(final DocumentType documentType) {
return documentService.getAllDocuments().stream() return documentService.getAllDocuments().stream()
.anyMatch(doc -> doc.getDocumentType() == documentType); .anyMatch(doc -> doc.getDocumentType() == documentType);
} }
private void viewDocument(DocumentType documentType) { private void viewDocument(final 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(Document document) { private void showPdfDialog(final Document document) {
Dialog dialog = createDialog(document.getFileData()); Dialog dialog = createDialog(document.getFileData());
dialog.open(); dialog.open();
} }
private Dialog createDialog(byte[] fileData) { private Dialog createDialog(final byte[] fileData) {
Dialog dialog = new Dialog(); Dialog dialog = new Dialog();
dialog.setModal(true); dialog.setModal(true);
dialog.setCloseOnEsc(true); dialog.setCloseOnEsc(true);
@ -160,7 +167,7 @@ public class WorkDocumentsView extends Main {
return dialog; return dialog;
} }
private String createPdfResource(byte[] fileData) { private String createPdfResource(final byte[] fileData) {
return "data:application/pdf;base64," + Base64.getEncoder().encodeToString(fileData); return "data:application/pdf;base64," + Base64.getEncoder().encodeToString(fileData);
} }
} }