#17-Perfil-Personal-Administrativo #24

Merged
jesus.pelaez merged 4 commits from #17-Perfil-Personal-Administrativo into En-desarrollo 2024-09-15 06:27:57 +00:00
7 changed files with 295 additions and 17 deletions

View File

@ -1,10 +1,6 @@
package com.primefactorsolutions.model;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.MappedSuperclass;
import jakarta.persistence.Version;
import jakarta.persistence.*;
import java.util.UUID;
@ -14,6 +10,12 @@ public abstract class BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.UUID)
private UUID id;
private String fileName;
@Lob
@Column(columnDefinition = "BLOB")
private byte[] fileData;
@Enumerated(EnumType.STRING)
private DocumentType documentType;
@Version
private int version;
@ -48,4 +50,12 @@ public abstract class BaseEntity {
}
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;
import jakarta.annotation.Nullable;
import jakarta.persistence.Entity;
import jakarta.persistence.ManyToOne;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import jakarta.persistence.*;
import lombok.*;
@Entity
@Data
@ -14,10 +10,10 @@ import lombok.NoArgsConstructor;
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class Document extends BaseEntity {
@Nullable
private String description;
private String location;
private String fileName;
@Enumerated(EnumType.STRING)
private DocumentType documentType;
@ManyToOne
private Employee employee;
@Lob
@Column(columnDefinition = "BLOB")
private byte[] fileData;
}

View File

@ -3,5 +3,11 @@ package com.primefactorsolutions.model;
public enum DocumentType {
ID_CARD,
PAY_STUB,
PAY_SLIPS,
EMPLOYMENT_CONTRACT,
WORK_CERTIFICATES,
NDA,
MEMORANDUMS,
CONTRACT_APPROVAL_MTEPS,
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;
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

@ -1,16 +1,166 @@
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.dialog.Dialog;
import com.vaadin.flow.component.html.Div;
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.notification.Notification;
import com.vaadin.flow.component.orderedlayout.FlexComponent;
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.Route;
import com.vaadin.flow.spring.annotation.SpringComponent;
import jakarta.annotation.security.PermitAll;
import org.springframework.context.annotation.Scope;
import java.io.IOException;
import java.util.Base64;
@SpringComponent
@PermitAll
@Scope("prototype")
@PageTitle("WorkDocuments")
@Route(value = "/work-documents/me", layout = MainLayout.class)
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) {
this.documentService = documentService;
initializeLayout();
}
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();
section.add(new H2(title));
Upload upload = createUploadComponent();
Button viewButton = createViewButton(documentType);
Button saveButton = createSaveButton(viewButton, documentType);
section.add(createLayout(upload),
createLayout(viewButton, new Button("Edit"), saveButton),
createAdditionalButtons());
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);
}
}