Compare commits
11 Commits
6df1d45c81
...
b6a27c435e
Author | SHA1 | Date | |
---|---|---|---|
b6a27c435e | |||
26af5f63c7 | |||
6d9a2f65e5 | |||
1519d82993 | |||
4241e7b52b | |||
f0b41743c7 | |||
af3f13773b | |||
f3bda474d0 | |||
45987ccad9 | |||
514e4eaf49 | |||
2caa878c07 |
@ -13,7 +13,11 @@ public class Document extends BaseEntity {
|
|||||||
private String fileName;
|
private String fileName;
|
||||||
@Enumerated(EnumType.STRING)
|
@Enumerated(EnumType.STRING)
|
||||||
private DocumentType documentType;
|
private DocumentType documentType;
|
||||||
|
@ManyToOne
|
||||||
|
@JoinColumn(name = "employee_id", nullable = false)
|
||||||
|
private Employee employee;
|
||||||
@Lob
|
@Lob
|
||||||
@Column(columnDefinition = "BLOB")
|
@Column(columnDefinition = "BLOB")
|
||||||
private byte[] fileData;
|
private byte[] fileData;
|
||||||
|
private String creator;
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.primefactorsolutions.model;
|
package com.primefactorsolutions.model;
|
||||||
|
|
||||||
public enum DocumentType {
|
public enum DocumentType {
|
||||||
|
All,
|
||||||
ID_CARD,
|
ID_CARD,
|
||||||
PAY_STUB,
|
PAY_STUB,
|
||||||
PAY_SLIPS,
|
PAY_SLIPS,
|
||||||
|
@ -1,30 +1,78 @@
|
|||||||
package com.primefactorsolutions.service;
|
package com.primefactorsolutions.service;
|
||||||
|
|
||||||
import com.primefactorsolutions.model.Document;
|
import com.primefactorsolutions.model.Document;
|
||||||
|
import com.primefactorsolutions.model.DocumentType;
|
||||||
|
import com.primefactorsolutions.model.Employee;
|
||||||
import com.primefactorsolutions.repositories.DocumentRepository;
|
import com.primefactorsolutions.repositories.DocumentRepository;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import org.apache.commons.beanutils.BeanComparator;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
|
@AllArgsConstructor
|
||||||
public class DocumentService {
|
public class DocumentService {
|
||||||
private final DocumentRepository documentRepository;
|
private final DocumentRepository documentRepository;
|
||||||
|
|
||||||
public DocumentService(final DocumentRepository documentRepository) {
|
|
||||||
this.documentRepository = documentRepository;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void saveDocument(final Document newDocument) {
|
public void saveDocument(final Document newDocument) {
|
||||||
documentRepository.save(newDocument);
|
documentRepository.save(newDocument);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void deleteDocument(final UUID id) {
|
||||||
|
documentRepository.deleteById(id);
|
||||||
|
}
|
||||||
|
|
||||||
public List<Document> getAllDocuments() {
|
public List<Document> getAllDocuments() {
|
||||||
return documentRepository.findAll();
|
return documentRepository.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<Document> getDocumentById(final UUID id) {
|
public Document getDocument(final UUID id) {
|
||||||
return documentRepository.findById(id);
|
Optional<Document> employee = documentRepository.findById(id);
|
||||||
|
return employee.orElse(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Document> findDocuments(
|
||||||
|
final int start, final int pageSize, final String sortProperty, final boolean asc) {
|
||||||
|
List<Document> documents = documentRepository.findAll();
|
||||||
|
|
||||||
|
int end = Math.min(start + pageSize, documents.size());
|
||||||
|
documents.sort(new BeanComparator<>(sortProperty));
|
||||||
|
|
||||||
|
if (!asc) {
|
||||||
|
Collections.reverse(documents);
|
||||||
|
}
|
||||||
|
|
||||||
|
return documents.subList(start, end);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Document> findDocuments(final int start, final int pageSize) {
|
||||||
|
List<Document> employees = documentRepository.findAll();
|
||||||
|
|
||||||
|
int end = Math.min(start + pageSize, employees.size());
|
||||||
|
return employees.subList(start, end);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Document> findDocumentBy(final DocumentType documentType,
|
||||||
|
final Employee employee,
|
||||||
|
final int start,
|
||||||
|
final int pageSize) {
|
||||||
|
List<Document> documents = documentRepository.findAll();
|
||||||
|
if (documentType != null) {
|
||||||
|
documents = documents.stream()
|
||||||
|
.filter(doc -> doc.getDocumentType().equals(documentType))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
if (employee != null) {
|
||||||
|
documents = documents.stream()
|
||||||
|
.filter(doc -> doc.getEmployee().equals(employee))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
int end = Math.min(start + pageSize, documents.size());
|
||||||
|
return documents.subList(start, end);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -87,4 +87,8 @@ public class EmployeeService {
|
|||||||
|
|
||||||
ldapTemplate.modifyAttributes(buildDn(employee), new ModificationItem[] {item});
|
ldapTemplate.modifyAttributes(buildDn(employee), new ModificationItem[] {item});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<Employee> findAllEmployees() {
|
||||||
|
return employeeRepository.findAll();
|
||||||
|
}
|
||||||
}
|
}
|
252
src/main/java/com/primefactorsolutions/views/DocumentView.java
Normal file
252
src/main/java/com/primefactorsolutions/views/DocumentView.java
Normal file
@ -0,0 +1,252 @@
|
|||||||
|
package com.primefactorsolutions.views;
|
||||||
|
|
||||||
|
import com.primefactorsolutions.model.Document;
|
||||||
|
import com.primefactorsolutions.model.DocumentType;
|
||||||
|
import com.primefactorsolutions.model.Employee;
|
||||||
|
import com.primefactorsolutions.service.DocumentService;
|
||||||
|
import com.primefactorsolutions.service.EmployeeService;
|
||||||
|
import com.vaadin.flow.component.Component;
|
||||||
|
import com.vaadin.flow.component.button.Button;
|
||||||
|
import com.vaadin.flow.component.combobox.ComboBox;
|
||||||
|
import com.vaadin.flow.component.html.H2;
|
||||||
|
import com.vaadin.flow.component.notification.Notification;
|
||||||
|
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
|
||||||
|
import com.vaadin.flow.component.textfield.TextField;
|
||||||
|
import com.vaadin.flow.component.upload.Upload;
|
||||||
|
import com.vaadin.flow.component.upload.receivers.MemoryBuffer;
|
||||||
|
import com.vaadin.flow.router.*;
|
||||||
|
import com.vaadin.flow.spring.annotation.SpringComponent;
|
||||||
|
import elemental.json.Json;
|
||||||
|
import elemental.json.JsonArray;
|
||||||
|
import elemental.json.JsonObject;
|
||||||
|
import jakarta.annotation.security.PermitAll;
|
||||||
|
import org.springframework.context.annotation.Scope;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
|
import org.vaadin.firitin.form.BeanValidationForm;
|
||||||
|
import com.vaadin.flow.spring.security.AuthenticationContext;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Base64;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@SpringComponent
|
||||||
|
@PermitAll
|
||||||
|
@Scope("prototype")
|
||||||
|
@PageTitle("Document")
|
||||||
|
@Route(value = "/documents/:documentId?/:action?", layout = MainLayout.class)
|
||||||
|
public class DocumentView extends BeanValidationForm<Document> implements HasUrlParameter<String> {
|
||||||
|
|
||||||
|
private final H2 title = new H2("Edit Documents");
|
||||||
|
private final TextField fileName = new TextField("Document Name");
|
||||||
|
private final ComboBox<DocumentType> documentType = new ComboBox<>("Document Type");
|
||||||
|
private final ComboBox<Employee> employeeComboBox = new ComboBox<>("Employee");
|
||||||
|
private final MemoryBuffer buffer = new MemoryBuffer();
|
||||||
|
private final Upload uploadButton = new Upload(buffer);
|
||||||
|
private final DocumentService documentService;
|
||||||
|
private final EmployeeService employeeService;
|
||||||
|
private final AuthenticationContext authContext;
|
||||||
|
|
||||||
|
private boolean fileUploaded = false;
|
||||||
|
private Button saveButton;
|
||||||
|
private Button viewDocumentButton;
|
||||||
|
|
||||||
|
public DocumentView(final DocumentService documentService,
|
||||||
|
final EmployeeService employeeService,
|
||||||
|
final AuthenticationContext authContext) {
|
||||||
|
super(Document.class);
|
||||||
|
this.documentService = documentService;
|
||||||
|
this.employeeService = employeeService;
|
||||||
|
this.authContext = authContext;
|
||||||
|
initializeView();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initializeView() {
|
||||||
|
configureComponents();
|
||||||
|
configureUploadButton();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void configureComponents() {
|
||||||
|
setFileNameProperties();
|
||||||
|
setDocumentTypeProperties();
|
||||||
|
setEmployeeComboBoxProperties();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setFileNameProperties() {
|
||||||
|
fileName.setWidthFull();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setDocumentTypeProperties() {
|
||||||
|
documentType.setItems(DocumentType.values());
|
||||||
|
documentType.setWidthFull();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setEmployeeComboBoxProperties() {
|
||||||
|
List<Employee> employees = employeeService.findAllEmployees();
|
||||||
|
employeeComboBox.setItems(employees);
|
||||||
|
employeeComboBox.setItemLabelGenerator(employee -> employee.getFirstName() + " " + employee.getLastName());
|
||||||
|
employeeComboBox.setWidthFull();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void configureUploadButton() {
|
||||||
|
uploadButton.setMaxFiles(1);
|
||||||
|
uploadButton.setAcceptedFileTypes(".pdf");
|
||||||
|
uploadButton.addSucceededListener(event -> handleUploadSuccess());
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Button createSaveButton() {
|
||||||
|
saveButton = new Button("Save");
|
||||||
|
saveButton.addClickListener(event -> saveDocument());
|
||||||
|
return saveButton;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Button createCloseButton() {
|
||||||
|
Button closeButton = new Button("Close");
|
||||||
|
closeButton.addClickListener(event -> closeForm());
|
||||||
|
return closeButton;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Button createViewDocumentButton() {
|
||||||
|
viewDocumentButton = new Button("View Document");
|
||||||
|
viewDocumentButton.setEnabled(false);
|
||||||
|
viewDocumentButton.addClickListener(event -> viewDocument());
|
||||||
|
return viewDocumentButton;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void viewDocument() {
|
||||||
|
Document document = getEntity();
|
||||||
|
if (document.getFileData() != null && document.getFileData().length > 0) {
|
||||||
|
String base64Data = Base64.getEncoder().encodeToString(document.getFileData());
|
||||||
|
String jsCode = createJsCodeForDocument(base64Data);
|
||||||
|
getElement().executeJs(jsCode);
|
||||||
|
} else {
|
||||||
|
Notification.show("No file data available to view.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String createJsCodeForDocument(final String base64Data) {
|
||||||
|
return "var byteCharacters = atob('" + base64Data + "');"
|
||||||
|
+ "var byteNumbers = new Array(byteCharacters.length);"
|
||||||
|
+ "for (var i = 0; i < byteCharacters.length; i++) {"
|
||||||
|
+ " byteNumbers[i] = byteCharacters.charCodeAt(i);"
|
||||||
|
+ "}"
|
||||||
|
+ "var byteArray = new Uint8Array(byteNumbers);"
|
||||||
|
+ "var blob = new Blob([byteArray], { type: 'application/pdf' });"
|
||||||
|
+ "var url = URL.createObjectURL(blob);"
|
||||||
|
+ "window.open(url, '_blank');";
|
||||||
|
}
|
||||||
|
|
||||||
|
private void closeForm() {
|
||||||
|
navigateToDocumentsListView();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void navigateToDocumentsListView() {
|
||||||
|
getUI().ifPresent(ui -> ui.navigate(DocumentsListView.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleUploadSuccess() {
|
||||||
|
fileUploaded = true;
|
||||||
|
Notification.show("File uploaded successfully.");
|
||||||
|
viewDocumentButton.setEnabled(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void saveDocument() {
|
||||||
|
if (isFormValid()) {
|
||||||
|
Document document = getEntity();
|
||||||
|
document.setFileName(fileName.getValue());
|
||||||
|
document.setDocumentType(documentType.getValue());
|
||||||
|
document.setEmployee(employeeComboBox.getValue());
|
||||||
|
document.setFileData(readFileData());
|
||||||
|
setDocumentCreator(document);
|
||||||
|
|
||||||
|
documentService.saveDocument(document);
|
||||||
|
Notification.show("File saved successfully.");
|
||||||
|
clearForm();
|
||||||
|
} else {
|
||||||
|
Notification.show("Save failed: Please complete all fields and upload a file.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setDocumentCreator(final Document document) {
|
||||||
|
authContext.getAuthenticatedUser(UserDetails.class).ifPresent(user -> {
|
||||||
|
document.setCreator(user.getUsername());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isFormValid() {
|
||||||
|
return !fileName.isEmpty()
|
||||||
|
&& documentType.getValue() != null
|
||||||
|
&& employeeComboBox.getValue() != null
|
||||||
|
&& fileUploaded;
|
||||||
|
}
|
||||||
|
|
||||||
|
private byte[] readFileData() {
|
||||||
|
try {
|
||||||
|
return buffer.getInputStream().readAllBytes();
|
||||||
|
} catch (IOException e) {
|
||||||
|
Notification.show("Error reading file data.");
|
||||||
|
return new byte[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void clearForm() {
|
||||||
|
fileName.clear();
|
||||||
|
documentType.clear();
|
||||||
|
employeeComboBox.clear();
|
||||||
|
fileUploaded = false;
|
||||||
|
uploadButton.getElement().setPropertyJson("files", Json.createArray());
|
||||||
|
viewDocumentButton.setEnabled(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setFieldsReadOnly(final boolean option) {
|
||||||
|
fileName.setReadOnly(option);
|
||||||
|
documentType.setReadOnly(option);
|
||||||
|
employeeComboBox.setReadOnly(option);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void preLoadFile(final Document document) {
|
||||||
|
JsonArray jsonArray = Json.createArray();
|
||||||
|
JsonObject jsonObject = Json.createObject();
|
||||||
|
jsonObject.put("name", document.getFileName());
|
||||||
|
jsonObject.put("progress", 100);
|
||||||
|
jsonObject.put("complete", true);
|
||||||
|
jsonArray.set(0, jsonObject);
|
||||||
|
uploadButton.getElement().setPropertyJson("files", jsonArray);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setParameter(final BeforeEvent beforeEvent, final String action) {
|
||||||
|
final RouteParameters params = beforeEvent.getRouteParameters();
|
||||||
|
final String documentIdString = params.get("documentId").orElse(null);
|
||||||
|
|
||||||
|
if ("new".equals(action)) {
|
||||||
|
setEntityWithEnabledSave(new Document());
|
||||||
|
} else {
|
||||||
|
UUID documentId = UUID.fromString(documentIdString);
|
||||||
|
Document document = documentService.getDocument(documentId);
|
||||||
|
setEntity(document);
|
||||||
|
employeeComboBox.setValue(document.getEmployee());
|
||||||
|
preLoadFile(document);
|
||||||
|
configureViewOrEditAction(action, documentIdString);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void configureViewOrEditAction(final String action, final String documentIdString) {
|
||||||
|
if ("edit".equals(action) && !documentIdString.isEmpty()) {
|
||||||
|
setFieldsReadOnly(false);
|
||||||
|
preLoadFile(getEntity());
|
||||||
|
} else if ("view".equals(action) && !documentIdString.isEmpty()) {
|
||||||
|
setFieldsReadOnly(true);
|
||||||
|
saveButton.setEnabled(false);
|
||||||
|
viewDocumentButton.setEnabled(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected List<Component> getFormComponents() {
|
||||||
|
HorizontalLayout buttonLayout = new HorizontalLayout();
|
||||||
|
buttonLayout.add(uploadButton, createViewDocumentButton());
|
||||||
|
buttonLayout.setSpacing(true);
|
||||||
|
return List.of(title, fileName, documentType, employeeComboBox, buttonLayout, createCloseButton());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,205 @@
|
|||||||
|
package com.primefactorsolutions.views;
|
||||||
|
|
||||||
|
import com.primefactorsolutions.model.Document;
|
||||||
|
import com.primefactorsolutions.model.DocumentType;
|
||||||
|
import com.primefactorsolutions.model.Employee;
|
||||||
|
import com.primefactorsolutions.service.DocumentService;
|
||||||
|
import com.primefactorsolutions.service.EmployeeService;
|
||||||
|
import com.vaadin.flow.component.UI;
|
||||||
|
import com.vaadin.flow.component.button.Button;
|
||||||
|
import com.vaadin.flow.component.combobox.ComboBox;
|
||||||
|
import com.vaadin.flow.component.grid.GridSortOrder;
|
||||||
|
import com.vaadin.flow.component.html.Main;
|
||||||
|
import com.vaadin.flow.component.html.Span;
|
||||||
|
import com.vaadin.flow.data.provider.SortDirection;
|
||||||
|
import com.vaadin.flow.router.PageTitle;
|
||||||
|
import com.vaadin.flow.router.Route;
|
||||||
|
import com.vaadin.flow.server.StreamRegistration;
|
||||||
|
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 org.vaadin.firitin.components.grid.PagingGrid;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
|
@SpringComponent
|
||||||
|
@Scope("prototype")
|
||||||
|
@PageTitle("Documents")
|
||||||
|
@Route(value = "/documents", layout = MainLayout.class)
|
||||||
|
@PermitAll
|
||||||
|
public class DocumentsListView extends Main {
|
||||||
|
|
||||||
|
private final DocumentService documentService;
|
||||||
|
private final EmployeeService employeeService;
|
||||||
|
private final PagingGrid<Document> documentGrid = new PagingGrid<>(Document.class);
|
||||||
|
private ComboBox<Employee> employeeFilter;
|
||||||
|
private ComboBox<DocumentType> documentTypeFilter;
|
||||||
|
|
||||||
|
public DocumentsListView(final DocumentService documentService, final EmployeeService employeeService) {
|
||||||
|
this.documentService = documentService;
|
||||||
|
this.employeeService = employeeService;
|
||||||
|
initializeView();
|
||||||
|
updateDocumentGrid(null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initializeView() {
|
||||||
|
configureDocumentGrid();
|
||||||
|
add(createActionButton("Add Document", this::navigateToAddDocumentView));
|
||||||
|
add(createDocumentTypeFilter());
|
||||||
|
add(createEmployeeFilter());
|
||||||
|
add(documentGrid);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void configureDocumentGrid() {
|
||||||
|
documentGrid.setColumns("fileName", "documentType", "creator");
|
||||||
|
documentGrid.addComponentColumn(this::createEmployeeSpan).setHeader("Employee");
|
||||||
|
addActionColumns();
|
||||||
|
configurePagination();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Span createEmployeeSpan(final Document document) {
|
||||||
|
Employee employee = document.getEmployee();
|
||||||
|
String employeeName = employee.getFirstName() + " " + employee.getLastName();
|
||||||
|
return new Span(employeeName);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addActionColumns() {
|
||||||
|
addDocumentActionColumn("View", this::navigateToDocumentView);
|
||||||
|
addDocumentActionColumn("Edit", this::navigateToEditDocumentView);
|
||||||
|
addDocumentActionColumn("Download", this::downloadDocument);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addDocumentActionColumn(final String label, final DocumentActionHandler handler) {
|
||||||
|
documentGrid.addComponentColumn(document -> createActionButton(label, () -> handler.handle(document)));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Button createActionButton(final String label, final Runnable onClickAction) {
|
||||||
|
Button actionButton = new Button(label);
|
||||||
|
actionButton.addClickListener(event -> onClickAction.run());
|
||||||
|
return actionButton;
|
||||||
|
}
|
||||||
|
|
||||||
|
private ComboBox<DocumentType> createDocumentTypeFilter() {
|
||||||
|
documentTypeFilter = new ComboBox<>("Document Type");
|
||||||
|
documentTypeFilter.setItems(DocumentType.values());
|
||||||
|
documentTypeFilter.setValue(DocumentType.values()[0]);
|
||||||
|
documentTypeFilter.addValueChangeListener(event -> {
|
||||||
|
updateDocumentGrid(event.getValue(), employeeFilter.getValue());
|
||||||
|
});
|
||||||
|
return documentTypeFilter;
|
||||||
|
}
|
||||||
|
|
||||||
|
private ComboBox<Employee> createEmployeeFilter() {
|
||||||
|
employeeFilter = new ComboBox<>("Employee");
|
||||||
|
List<Employee> employees = employeeService.findAllEmployees();
|
||||||
|
employees.addFirst(createAllEmployeesOption());
|
||||||
|
employeeFilter.setItems(employees);
|
||||||
|
employeeFilter.setItemLabelGenerator(this::getEmployeeLabel);
|
||||||
|
employeeFilter.setValue(employees.getFirst());
|
||||||
|
employeeFilter.addValueChangeListener(event -> {
|
||||||
|
updateDocumentGrid(documentTypeFilter.getValue(), event.getValue());
|
||||||
|
});
|
||||||
|
return employeeFilter;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Employee createAllEmployeesOption() {
|
||||||
|
Employee allEmployeesOption = new Employee();
|
||||||
|
allEmployeesOption.setFirstName("All");
|
||||||
|
return allEmployeesOption;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getEmployeeLabel(final Employee employee) {
|
||||||
|
return employee.getFirstName().equals("All") ? "All" : employee.getFirstName() + " " + employee.getLastName();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void navigateToEditDocumentView(final Document document) {
|
||||||
|
navigateToDocumentView(document, "edit");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void navigateToDocumentView(final Document document) {
|
||||||
|
navigateToDocumentView(document, "view");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void navigateToDocumentView(final Document document, final String action) {
|
||||||
|
getUI().ifPresent(ui -> ui.navigate(DocumentView.class, document.getId().toString() + "/" + action));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void navigateToAddDocumentView() {
|
||||||
|
getUI().ifPresent(ui -> ui.navigate(DocumentView.class, "new"));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void configurePagination() {
|
||||||
|
documentGrid.setPaginationBarMode(PagingGrid.PaginationBarMode.BOTTOM);
|
||||||
|
documentGrid.setPageSize(5);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateDocumentGrid(final DocumentType documentType, final Employee employee) {
|
||||||
|
DocumentType finalDocumentType = isValidDocumentType(documentType) ? documentType : null;
|
||||||
|
Employee finalEmployee = isValidEmployee(employee) ? employee : null;
|
||||||
|
documentGrid.setPagingDataProvider((page, pageSize) ->
|
||||||
|
(finalDocumentType == null && finalEmployee == null)
|
||||||
|
? fetchDocuments((int) page, pageSize)
|
||||||
|
: fetchFilteredDocuments((int) page, pageSize, finalDocumentType, finalEmployee)
|
||||||
|
);
|
||||||
|
documentGrid.getDataProvider().refreshAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isValidDocumentType(final DocumentType documentType) {
|
||||||
|
return documentType != null && !"All".equals(documentType.name());
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isValidEmployee(final Employee employee) {
|
||||||
|
return employee != null && !"All".equals(employee.getFirstName());
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Document> fetchFilteredDocuments(final int page,
|
||||||
|
final int pageSize,
|
||||||
|
final DocumentType documentType,
|
||||||
|
final Employee employee) {
|
||||||
|
return documentService.findDocumentBy(documentType, employee, page, pageSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Document> fetchDocuments(final int page, final int size) {
|
||||||
|
int startIndex = page * size;
|
||||||
|
return isSortOrderPresent()
|
||||||
|
? fetchSortedDocuments(startIndex, size)
|
||||||
|
: documentService.findDocuments(startIndex, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isSortOrderPresent() {
|
||||||
|
return !documentGrid.getSortOrder().isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Document> fetchSortedDocuments(final int start, final int pageSize) {
|
||||||
|
GridSortOrder<Document> sortOrder = documentGrid.getSortOrder().getFirst();
|
||||||
|
return documentService.findDocuments(start, pageSize,
|
||||||
|
sortOrder.getSorted().getKey(),
|
||||||
|
sortOrder.getDirection() == SortDirection.ASCENDING);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void downloadDocument(final Document document) {
|
||||||
|
StreamResource resource = createDocumentStreamResource(document);
|
||||||
|
getUI().ifPresent(ui -> openDocumentStream(resource, ui));
|
||||||
|
}
|
||||||
|
|
||||||
|
private StreamResource createDocumentStreamResource(final Document document) {
|
||||||
|
StreamResource resource = new StreamResource(document.getFileName(),
|
||||||
|
() -> new ByteArrayInputStream(document.getFileData()));
|
||||||
|
resource.setContentType("application/pdf");
|
||||||
|
resource.setHeader("Content-Disposition", "attachment; filename=\"" + document.getFileName() + ".pdf\"");
|
||||||
|
return resource;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void openDocumentStream(final StreamResource resource, final UI ui) {
|
||||||
|
StreamRegistration registration = ui.getSession().getResourceRegistry().registerResource(resource);
|
||||||
|
ui.getPage().open(registration.getResourceUri().toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
private interface DocumentActionHandler {
|
||||||
|
void handle(Document document);
|
||||||
|
}
|
||||||
|
}
|
@ -1,174 +0,0 @@
|
|||||||
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("Documents")
|
|
||||||
@Route(value = "/documents/me", layout = MainLayout.class)
|
|
||||||
public abstract class DocumentsView extends Main {
|
|
||||||
private final DocumentService documentService;
|
|
||||||
private final MemoryBuffer buffer = new MemoryBuffer();
|
|
||||||
private String lastUploadedFileName = null;
|
|
||||||
|
|
||||||
public DocumentsView(final DocumentService documentService) {
|
|
||||||
this.documentService = documentService;
|
|
||||||
initializeLayout();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected abstract void initializeLayout();
|
|
||||||
|
|
||||||
protected 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)
|
|
||||||
);
|
|
||||||
return row;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected HorizontalLayout createRow(final String title1, final DocumentType type1) {
|
|
||||||
HorizontalLayout row = new HorizontalLayout();
|
|
||||||
row.add(createDocumentSection(title1, type1));
|
|
||||||
return row;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Div createDocumentSection(final String title, final 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(final DocumentType documentType) {
|
|
||||||
Button viewButton = new Button("View");
|
|
||||||
viewButton.setEnabled(documentExists(documentType));
|
|
||||||
viewButton.addClickListener(event -> viewDocument(documentType));
|
|
||||||
return viewButton;
|
|
||||||
}
|
|
||||||
|
|
||||||
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(final String fileName) {
|
|
||||||
lastUploadedFileName = fileName;
|
|
||||||
Notification.show("File uploaded successfully");
|
|
||||||
}
|
|
||||||
|
|
||||||
private HorizontalLayout createLayout(final 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(final DocumentType documentType, final 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(final DocumentType documentType) {
|
|
||||||
return documentService.getAllDocuments().stream()
|
|
||||||
.anyMatch(doc -> doc.getDocumentType() == 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(final Document document) {
|
|
||||||
Dialog dialog = createDialog(document.getFileData());
|
|
||||||
dialog.open();
|
|
||||||
}
|
|
||||||
|
|
||||||
private Dialog createDialog(final 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(final byte[] fileData) {
|
|
||||||
return "data:application/pdf;base64," + Base64.getEncoder().encodeToString(fileData);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -18,9 +18,7 @@ import com.vaadin.flow.theme.lumo.LumoUtility;
|
|||||||
import org.springframework.security.core.userdetails.UserDetails;
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
import org.vaadin.lineawesome.LineAwesomeIcon;
|
import org.vaadin.lineawesome.LineAwesomeIcon;
|
||||||
|
|
||||||
/**
|
|
||||||
* The main view is a top-level placeholder for other views.
|
|
||||||
*/
|
|
||||||
public class MainLayout extends AppLayout {
|
public class MainLayout extends AppLayout {
|
||||||
private final transient AuthenticationContext authContext;
|
private final transient AuthenticationContext authContext;
|
||||||
|
|
||||||
@ -81,9 +79,9 @@ public class MainLayout extends AppLayout {
|
|||||||
|
|
||||||
SideNavItem admin = new SideNavItem("Admin", MainView.class,
|
SideNavItem admin = new SideNavItem("Admin", MainView.class,
|
||||||
LineAwesomeIcon.SUPERSCRIPT_SOLID.create());
|
LineAwesomeIcon.SUPERSCRIPT_SOLID.create());
|
||||||
admin.addItem(new SideNavItem("Employees", EmployeeView.class,
|
// admin.addItem(new SideNavItem("Employees", EmployeeView.class,
|
||||||
LineAwesomeIcon.USER_EDIT_SOLID.create()));
|
// LineAwesomeIcon.USER_EDIT_SOLID.create()));
|
||||||
admin.addItem(new SideNavItem("Documents", DocumentsView.class,
|
admin.addItem(new SideNavItem("Documents", DocumentsListView.class,
|
||||||
LineAwesomeIcon.FILE_ALT_SOLID.create()));
|
LineAwesomeIcon.FILE_ALT_SOLID.create()));
|
||||||
|
|
||||||
SideNavItem timeOff = new SideNavItem("My Time-off", TimeoffView.class,
|
SideNavItem timeOff = new SideNavItem("My Time-off", TimeoffView.class,
|
||||||
|
Loading…
Reference in New Issue
Block a user