#17 Perfil de Personal Administrativo - Carga de Documentación Laboral (Funcionalidades save y upload)

This commit is contained in:
jesus.pelaez 2024-09-14 15:30:51 -04:00
parent 1887214ed2
commit 5bbdb5f2ee
7 changed files with 239 additions and 31 deletions

View File

@ -14,6 +14,7 @@ public abstract class BaseEntity {
@Id @Id
@GeneratedValue(strategy = GenerationType.UUID) @GeneratedValue(strategy = GenerationType.UUID)
private UUID id; private UUID id;
private String fileName;
@Version @Version
private int version; private int version;
@ -48,4 +49,8 @@ public abstract class BaseEntity {
} }
return super.equals(that); return super.equals(that);
} }
public String getFileName() {
return fileName;
}
} }

View File

@ -1,12 +1,8 @@
package com.primefactorsolutions.model; package com.primefactorsolutions.model;
import jakarta.annotation.Nullable; import jakarta.persistence.*;
import jakarta.persistence.Entity;
import jakarta.persistence.ManyToOne; import lombok.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
@Entity @Entity
@Data @Data
@ -14,10 +10,10 @@ import lombok.NoArgsConstructor;
@NoArgsConstructor @NoArgsConstructor
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode(callSuper = true)
public class Document extends BaseEntity { public class Document extends BaseEntity {
@Nullable private String fileName;
private String description; @Enumerated(EnumType.STRING)
private String location;
private DocumentType documentType; private DocumentType documentType;
@ManyToOne @Lob
private Employee employee; @Column(columnDefinition = "BLOB")
private byte[] fileData;
} }

View File

@ -3,5 +3,11 @@ 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
} }

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; 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

@ -1,47 +1,132 @@
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.button.Button; import com.vaadin.flow.component.button.Button;
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.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.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;
@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;
public WorkDocumentsView() { private final MemoryBuffer buffer = new MemoryBuffer();
HorizontalLayout row1 = new HorizontalLayout(); private boolean fileUploaded = false;
row1.add(createSection("Pay Slips"), createSection("Employment Contract")); private String lastUploadedFileName = null;
HorizontalLayout row2 = new HorizontalLayout(); public WorkDocumentsView(DocumentService documentService) {
row2.add(createSection("Work Certificates"), createSection("NDA")); this.documentService = documentService;
HorizontalLayout row3 = new HorizontalLayout(); initializeLayout();
row3.add(createSection("Memorandums"), createSection("Contract Approval MTEPS")); }
private void initializeLayout() {
HorizontalLayout row1 = createRow("PaySlips", "Employment Contract");
HorizontalLayout row2 = createRow("Work Certificates", "NDA");
HorizontalLayout row3 = createRow("Memorandums", "Contract Approval MTEPS");
add(row1, row2, row3); add(row1, row2, row3);
} }
private HorizontalLayout createRow(String... sectionTitles) {
HorizontalLayout row = new HorizontalLayout();
for (String title : sectionTitles) {
row.add(createSection(title));
}
return row;
}
private Div createSection(String sectionTitle) { private Div createSection(String sectionTitle) {
Div section = new Div(); Div section = new Div();
section.add(new H2(sectionTitle)); section.add(new H2(sectionTitle));
HorizontalLayout row1 = new HorizontalLayout(); Upload upload = createUploadComponent();
row1.setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER); boolean hasDocument = documentService.getAllDocuments()
row1.add(new Button("View"), new Button("Upload"), new Button("Save")); .stream()
HorizontalLayout row2 = new HorizontalLayout(); .anyMatch(doc -> doc.getFileName().equalsIgnoreCase(sectionTitle + ".pdf"));
row2.setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER); Button viewButton = new Button("View");
row2.add(new Button("Edit"), new Button("Print"), new Button("Delete")); viewButton.setEnabled(hasDocument);
HorizontalLayout row3 = new HorizontalLayout(); Button saveButton = createSaveButton(sectionTitle, viewButton);
row3.setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER); HorizontalLayout uploadLayout = createLayoutWithComponent(upload);
row3.add(new Button("Download")); HorizontalLayout actionButtonsLayout = createLayoutWithComponents(
section.add(row1, row2, row3); viewButton, new Button("Edit"), saveButton
);
HorizontalLayout additionalButtonsLayout = createLayoutWithComponents(
new Button("Print"), new Button("Download"), new Button("Delete")
);
section.add(uploadLayout, actionButtonsLayout, additionalButtonsLayout);
return section; return section;
} }
private Upload createUploadComponent() {
Upload upload = new Upload(buffer);
upload.setMaxFiles(1);
upload.setAcceptedFileTypes(".pdf", ".doc", ".docx");
upload.addSucceededListener(event -> {
fileUploaded = true;
lastUploadedFileName = event.getFileName();
Notification.show("File uploaded successfully");
});
return upload;
}
private Button createSaveButton(String sectionTitle, Button viewButton) {
Button saveButton = new Button("Save");
saveButton.addClickListener(clickEvent -> saveFile(sectionTitle, ));
return saveButton;
}
private HorizontalLayout createLayoutWithComponent(com.vaadin.flow.component.Component component) {
HorizontalLayout layout = new HorizontalLayout();
layout.setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER);
layout.add(component);
return layout;
}
private HorizontalLayout createLayoutWithComponents(com.vaadin.flow.component.Component... components) {
HorizontalLayout layout = new HorizontalLayout();
layout.setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER);
layout.add(components);
return layout;
}
private void saveFile(String sectionTitle, Button viewButton) {
if (fileUploaded) {
try {
String fileName = lastUploadedFileName;
byte[] content = buffer.getInputStream().readAllBytes();
if (content != null) {
DocumentType documentType = determineDocumentType(sectionTitle);
Document document = new Document(fileName, documentType, content);
documentService.saveDocument(document);
fileUploaded = false;
Notification.show("File saved successfully.");
viewButton.setEnabled(true);
} else {
Notification.show("No file content found.");
}
} catch (IOException e) {
Notification.show("Error reading file content: " + e.getMessage());
}
} else {
Notification.show("Please upload a file first.");
}
}
private DocumentType determineDocumentType(String sectionTitle) {
return switch (sectionTitle) {
case "PaySlips" -> DocumentType.PAY_SLIPS;
case "Employment Contract" -> DocumentType.EMPLOYMENT_CONTRACT;
case "Work Certificates" -> DocumentType.WORK_CERTIFICATES;
case "NDA" -> DocumentType.NDA;
case "Memorandums" -> DocumentType.MEMORANDUMS;
case "Contract Approval MTEPS" -> DocumentType.CONTRACT_APPROVAL_MTEPS;
default -> DocumentType.OTHER;
};
}
} }