En-desarrollo #27

Merged
alex merged 39 commits from En-desarrollo into main 2024-10-07 18:23:34 +00:00
4 changed files with 31 additions and 97 deletions
Showing only changes of commit 4d12dfdd18 - Show all commits

View File

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

View File

@ -1,79 +1,16 @@
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.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.Route;
import com.vaadin.flow.server.StreamResource;
import com.vaadin.flow.spring.annotation.SpringComponent;
import jakarta.annotation.security.PermitAll;
import org.springframework.context.annotation.Scope;
import java.io.ByteArrayInputStream;
import java.io.IOException;
//import java.io.InputStream;
import java.util.UUID;
@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 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 MemoryBuffer buffer = new MemoryBuffer();
private String lastUploadedFileName = null;
private DocumentType currentDocumentType = null;
public WorkDocumentsView(DocumentService documentService) {
public WorkDocumentsView(final DocumentService documentService) {
this.documentService = documentService;
initializeLayout();
}
@ -50,13 +49,21 @@ public class WorkDocumentsView extends Main {
"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();
row.add(createDocumentSection(title1, type1), createDocumentSection(title2, type2));
row.add(
createDocumentSection(title1, type1),
createDocumentSection(title2, type2)
);
return row;
}
private Div createDocumentSection(String title, DocumentType documentType) {
private Div createDocumentSection(final String title, final DocumentType documentType) {
Div section = new Div();
section.add(new H2(title));
@ -78,25 +85,25 @@ public class WorkDocumentsView extends Main {
return upload;
}
private Button createViewButton(DocumentType documentType) {
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(Button viewButton, DocumentType documentType) {
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(String fileName) {
private void handleUploadSuccess(final String fileName) {
lastUploadedFileName = fileName;
Notification.show("File uploaded successfully");
}
private HorizontalLayout createLayout(Component... components) {
private HorizontalLayout createLayout(final Component... components) {
if (components.length != 1 && components.length != 3) {
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"));
}
private void saveFile(DocumentType documentType, Button viewButton) {
private void saveFile(final DocumentType documentType, final Button viewButton) {
if (lastUploadedFileName == null) {
Notification.show("Please upload a file first.");
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()
.anyMatch(doc -> doc.getDocumentType() == documentType);
}
private void viewDocument(DocumentType 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(Document document) {
private void showPdfDialog(final Document document) {
Dialog dialog = createDialog(document.getFileData());
dialog.open();
}
private Dialog createDialog(byte[] fileData) {
private Dialog createDialog(final byte[] fileData) {
Dialog dialog = new Dialog();
dialog.setModal(true);
dialog.setCloseOnEsc(true);
@ -160,7 +167,7 @@ public class WorkDocumentsView extends Main {
return dialog;
}
private String createPdfResource(byte[] fileData) {
private String createPdfResource(final byte[] fileData) {
return "data:application/pdf;base64," + Base64.getEncoder().encodeToString(fileData);
}
}