Compare commits
6 Commits
45987ccad9
...
6d9a2f65e5
Author | SHA1 | Date | |
---|---|---|---|
6d9a2f65e5 | |||
1519d82993 | |||
4241e7b52b | |||
f0b41743c7 | |||
af3f13773b | |||
f3bda474d0 |
@ -19,4 +19,5 @@ public class Document extends BaseEntity {
|
|||||||
@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,6 +1,8 @@
|
|||||||
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 lombok.AllArgsConstructor;
|
||||||
import org.apache.commons.beanutils.BeanComparator;
|
import org.apache.commons.beanutils.BeanComparator;
|
||||||
@ -10,6 +12,7 @@ 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
|
@AllArgsConstructor
|
||||||
@ -20,6 +23,10 @@ public class DocumentService {
|
|||||||
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();
|
||||||
}
|
}
|
||||||
@ -49,4 +56,23 @@ public class DocumentService {
|
|||||||
int end = Math.min(start + pageSize, employees.size());
|
int end = Math.min(start + pageSize, employees.size());
|
||||||
return employees.subList(start, end);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,16 +10,23 @@ import com.vaadin.flow.component.button.Button;
|
|||||||
import com.vaadin.flow.component.combobox.ComboBox;
|
import com.vaadin.flow.component.combobox.ComboBox;
|
||||||
import com.vaadin.flow.component.html.H2;
|
import com.vaadin.flow.component.html.H2;
|
||||||
import com.vaadin.flow.component.notification.Notification;
|
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.textfield.TextField;
|
||||||
import com.vaadin.flow.component.upload.Upload;
|
import com.vaadin.flow.component.upload.Upload;
|
||||||
import com.vaadin.flow.component.upload.receivers.MemoryBuffer;
|
import com.vaadin.flow.component.upload.receivers.MemoryBuffer;
|
||||||
import com.vaadin.flow.router.*;
|
import com.vaadin.flow.router.*;
|
||||||
import com.vaadin.flow.spring.annotation.SpringComponent;
|
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 jakarta.annotation.security.PermitAll;
|
||||||
import org.springframework.context.annotation.Scope;
|
import org.springframework.context.annotation.Scope;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
import org.vaadin.firitin.form.BeanValidationForm;
|
import org.vaadin.firitin.form.BeanValidationForm;
|
||||||
|
import com.vaadin.flow.spring.security.AuthenticationContext;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.Base64;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@ -38,20 +45,31 @@ public class DocumentView extends BeanValidationForm<Document> implements HasUrl
|
|||||||
private final Upload uploadButton = new Upload(buffer);
|
private final Upload uploadButton = new Upload(buffer);
|
||||||
private final DocumentService documentService;
|
private final DocumentService documentService;
|
||||||
private final EmployeeService employeeService;
|
private final EmployeeService employeeService;
|
||||||
private boolean fileUploaded = false;
|
private final AuthenticationContext authContext;
|
||||||
|
|
||||||
public DocumentView(final DocumentService documentService, final EmployeeService employeeService) {
|
private boolean fileUploaded = false;
|
||||||
|
private Button saveButton;
|
||||||
|
private Button viewDocumentButton;
|
||||||
|
|
||||||
|
public DocumentView(final DocumentService documentService,
|
||||||
|
final EmployeeService employeeService,
|
||||||
|
final AuthenticationContext authContext) {
|
||||||
super(Document.class);
|
super(Document.class);
|
||||||
this.documentService = documentService;
|
this.documentService = documentService;
|
||||||
this.employeeService = employeeService;
|
this.employeeService = employeeService;
|
||||||
|
this.authContext = authContext;
|
||||||
initializeView();
|
initializeView();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initializeView() {
|
private void initializeView() {
|
||||||
|
configureComponents();
|
||||||
|
configureUploadButton();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void configureComponents() {
|
||||||
setFileNameProperties();
|
setFileNameProperties();
|
||||||
setDocumentTypeProperties();
|
setDocumentTypeProperties();
|
||||||
setEmployeeComboBoxProperties();
|
setEmployeeComboBoxProperties();
|
||||||
setUploadButtonProperties();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setFileNameProperties() {
|
private void setFileNameProperties() {
|
||||||
@ -70,14 +88,14 @@ public class DocumentView extends BeanValidationForm<Document> implements HasUrl
|
|||||||
employeeComboBox.setWidthFull();
|
employeeComboBox.setWidthFull();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setUploadButtonProperties() {
|
private void configureUploadButton() {
|
||||||
uploadButton.setMaxFiles(1);
|
uploadButton.setMaxFiles(1);
|
||||||
uploadButton.setAcceptedFileTypes(".pdf");
|
uploadButton.setAcceptedFileTypes(".pdf");
|
||||||
uploadButton.addSucceededListener(event -> handleUploadSuccess());
|
uploadButton.addSucceededListener(event -> handleUploadSuccess());
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Button createSaveButton() {
|
protected Button createSaveButton() {
|
||||||
Button saveButton = new Button("Save");
|
saveButton = new Button("Save");
|
||||||
saveButton.addClickListener(event -> saveDocument());
|
saveButton.addClickListener(event -> saveDocument());
|
||||||
return saveButton;
|
return saveButton;
|
||||||
}
|
}
|
||||||
@ -88,6 +106,35 @@ public class DocumentView extends BeanValidationForm<Document> implements HasUrl
|
|||||||
return closeButton;
|
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() {
|
private void closeForm() {
|
||||||
navigateToDocumentsListView();
|
navigateToDocumentsListView();
|
||||||
@ -100,12 +147,18 @@ public class DocumentView extends BeanValidationForm<Document> implements HasUrl
|
|||||||
private void handleUploadSuccess() {
|
private void handleUploadSuccess() {
|
||||||
fileUploaded = true;
|
fileUploaded = true;
|
||||||
Notification.show("File uploaded successfully.");
|
Notification.show("File uploaded successfully.");
|
||||||
|
viewDocumentButton.setEnabled(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void saveDocument() {
|
private void saveDocument() {
|
||||||
if (isFormValid()) {
|
if (isFormValid()) {
|
||||||
Document document = createDocument();
|
Document document = getEntity();
|
||||||
|
document.setFileName(fileName.getValue());
|
||||||
|
document.setDocumentType(documentType.getValue());
|
||||||
document.setEmployee(employeeComboBox.getValue());
|
document.setEmployee(employeeComboBox.getValue());
|
||||||
|
document.setFileData(readFileData());
|
||||||
|
setDocumentCreator(document);
|
||||||
|
|
||||||
documentService.saveDocument(document);
|
documentService.saveDocument(document);
|
||||||
Notification.show("File saved successfully.");
|
Notification.show("File saved successfully.");
|
||||||
clearForm();
|
clearForm();
|
||||||
@ -114,6 +167,12 @@ public class DocumentView extends BeanValidationForm<Document> implements HasUrl
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void setDocumentCreator(final Document document) {
|
||||||
|
authContext.getAuthenticatedUser(UserDetails.class).ifPresent(user -> {
|
||||||
|
document.setCreator(user.getUsername());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private boolean isFormValid() {
|
private boolean isFormValid() {
|
||||||
return !fileName.isEmpty()
|
return !fileName.isEmpty()
|
||||||
&& documentType.getValue() != null
|
&& documentType.getValue() != null
|
||||||
@ -121,14 +180,6 @@ public class DocumentView extends BeanValidationForm<Document> implements HasUrl
|
|||||||
&& fileUploaded;
|
&& fileUploaded;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Document createDocument() {
|
|
||||||
Document document = new Document();
|
|
||||||
document.setFileName(fileName.getValue());
|
|
||||||
document.setDocumentType(documentType.getValue());
|
|
||||||
document.setFileData(readFileData());
|
|
||||||
return document;
|
|
||||||
}
|
|
||||||
|
|
||||||
private byte[] readFileData() {
|
private byte[] readFileData() {
|
||||||
try {
|
try {
|
||||||
return buffer.getInputStream().readAllBytes();
|
return buffer.getInputStream().readAllBytes();
|
||||||
@ -143,25 +194,59 @@ public class DocumentView extends BeanValidationForm<Document> implements HasUrl
|
|||||||
documentType.clear();
|
documentType.clear();
|
||||||
employeeComboBox.clear();
|
employeeComboBox.clear();
|
||||||
fileUploaded = false;
|
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
|
@Override
|
||||||
public void setParameter(final BeforeEvent beforeEvent, final String action) {
|
public void setParameter(final BeforeEvent beforeEvent, final String action) {
|
||||||
final RouteParameters params = beforeEvent.getRouteParameters();
|
final RouteParameters params = beforeEvent.getRouteParameters();
|
||||||
final String s = params.get("documentId").orElse(null);
|
final String documentIdString = params.get("documentId").orElse(null);
|
||||||
|
|
||||||
if ("new".equals(action)) {
|
if ("new".equals(action)) {
|
||||||
setEntityWithEnabledSave(new Document());
|
setEntityWithEnabledSave(new Document());
|
||||||
} else {
|
} else {
|
||||||
UUID documentId = UUID.fromString(s);
|
UUID documentId = UUID.fromString(documentIdString);
|
||||||
var document = documentService.getDocument(documentId);
|
Document document = documentService.getDocument(documentId);
|
||||||
setEntityWithEnabledSave(document);
|
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
|
@Override
|
||||||
protected List<Component> getFormComponents() {
|
protected List<Component> getFormComponents() {
|
||||||
return List.of(title, fileName, documentType, employeeComboBox, uploadButton, createCloseButton());
|
HorizontalLayout buttonLayout = new HorizontalLayout();
|
||||||
|
buttonLayout.add(uploadButton, createViewDocumentButton());
|
||||||
|
buttonLayout.setSpacing(true);
|
||||||
|
return List.of(title, fileName, documentType, employeeComboBox, buttonLayout, createCloseButton());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import com.primefactorsolutions.model.DocumentType;
|
|||||||
import com.primefactorsolutions.model.Employee;
|
import com.primefactorsolutions.model.Employee;
|
||||||
import com.primefactorsolutions.service.DocumentService;
|
import com.primefactorsolutions.service.DocumentService;
|
||||||
import com.primefactorsolutions.service.EmployeeService;
|
import com.primefactorsolutions.service.EmployeeService;
|
||||||
|
import com.vaadin.flow.component.UI;
|
||||||
import com.vaadin.flow.component.button.Button;
|
import com.vaadin.flow.component.button.Button;
|
||||||
import com.vaadin.flow.component.combobox.ComboBox;
|
import com.vaadin.flow.component.combobox.ComboBox;
|
||||||
import com.vaadin.flow.component.grid.GridSortOrder;
|
import com.vaadin.flow.component.grid.GridSortOrder;
|
||||||
@ -13,11 +14,14 @@ import com.vaadin.flow.component.html.Span;
|
|||||||
import com.vaadin.flow.data.provider.SortDirection;
|
import com.vaadin.flow.data.provider.SortDirection;
|
||||||
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.StreamRegistration;
|
||||||
|
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 org.vaadin.firitin.components.grid.PagingGrid;
|
import org.vaadin.firitin.components.grid.PagingGrid;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
@ -31,12 +35,14 @@ public class DocumentsListView extends Main {
|
|||||||
private final DocumentService documentService;
|
private final DocumentService documentService;
|
||||||
private final EmployeeService employeeService;
|
private final EmployeeService employeeService;
|
||||||
private final PagingGrid<Document> documentGrid = new PagingGrid<>(Document.class);
|
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) {
|
public DocumentsListView(final DocumentService documentService, final EmployeeService employeeService) {
|
||||||
this.documentService = documentService;
|
this.documentService = documentService;
|
||||||
this.employeeService = employeeService;
|
this.employeeService = employeeService;
|
||||||
initializeView();
|
initializeView();
|
||||||
updateDocumentGrid();
|
updateDocumentGrid(null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initializeView() {
|
private void initializeView() {
|
||||||
@ -48,11 +54,9 @@ public class DocumentsListView extends Main {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void configureDocumentGrid() {
|
private void configureDocumentGrid() {
|
||||||
documentGrid.setColumns("fileName", "documentType");
|
documentGrid.setColumns("fileName", "documentType", "creator");
|
||||||
documentGrid.addComponentColumn(this::createEmployeeSpan).setHeader("Employee");
|
documentGrid.addComponentColumn(this::createEmployeeSpan).setHeader("Employee");
|
||||||
addDocumentActionColumn("View", this::navigateToDocumentView);
|
addActionColumns();
|
||||||
addDocumentActionColumn("Edit", this::navigateToEditDocumentView);
|
|
||||||
addDocumentActionColumn("Download", this::navigateToDownloadDocumentView);
|
|
||||||
configurePagination();
|
configurePagination();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,6 +66,11 @@ public class DocumentsListView extends Main {
|
|||||||
return new Span(employeeName);
|
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) {
|
private void addDocumentActionColumn(final String label, final DocumentActionHandler handler) {
|
||||||
documentGrid.addComponentColumn(document -> createActionButton(label, () -> handler.handle(document)));
|
documentGrid.addComponentColumn(document -> createActionButton(label, () -> handler.handle(document)));
|
||||||
@ -74,21 +83,38 @@ public class DocumentsListView extends Main {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private ComboBox<DocumentType> createDocumentTypeFilter() {
|
private ComboBox<DocumentType> createDocumentTypeFilter() {
|
||||||
ComboBox<DocumentType> documentTypeFilter = new ComboBox<>("Document Type");
|
documentTypeFilter = new ComboBox<>("Document Type");
|
||||||
documentTypeFilter.setItems(DocumentType.values());
|
documentTypeFilter.setItems(DocumentType.values());
|
||||||
documentTypeFilter.addValueChangeListener(event -> updateDocumentGrid());
|
documentTypeFilter.setValue(DocumentType.values()[0]);
|
||||||
|
documentTypeFilter.addValueChangeListener(event -> {
|
||||||
|
updateDocumentGrid(event.getValue(), employeeFilter.getValue());
|
||||||
|
});
|
||||||
return documentTypeFilter;
|
return documentTypeFilter;
|
||||||
}
|
}
|
||||||
|
|
||||||
private ComboBox<Employee> createEmployeeFilter() {
|
private ComboBox<Employee> createEmployeeFilter() {
|
||||||
ComboBox<Employee> employeeFilter = new ComboBox<>("Employee");
|
employeeFilter = new ComboBox<>("Employee");
|
||||||
List<Employee> employees = employeeService.findAllEmployees();
|
List<Employee> employees = employeeService.findAllEmployees();
|
||||||
|
employees.addFirst(createAllEmployeesOption());
|
||||||
employeeFilter.setItems(employees);
|
employeeFilter.setItems(employees);
|
||||||
employeeFilter.setItemLabelGenerator(employee -> employee.getFirstName() + " " + employee.getLastName());
|
employeeFilter.setItemLabelGenerator(this::getEmployeeLabel);
|
||||||
employeeFilter.addValueChangeListener(event -> updateDocumentGrid());
|
employeeFilter.setValue(employees.getFirst());
|
||||||
|
employeeFilter.addValueChangeListener(event -> {
|
||||||
|
updateDocumentGrid(documentTypeFilter.getValue(), event.getValue());
|
||||||
|
});
|
||||||
return employeeFilter;
|
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) {
|
private void navigateToEditDocumentView(final Document document) {
|
||||||
navigateToDocumentView(document, "edit");
|
navigateToDocumentView(document, "edit");
|
||||||
}
|
}
|
||||||
@ -101,10 +127,6 @@ public class DocumentsListView extends Main {
|
|||||||
getUI().ifPresent(ui -> ui.navigate(DocumentView.class, document.getId().toString() + "/" + action));
|
getUI().ifPresent(ui -> ui.navigate(DocumentView.class, document.getId().toString() + "/" + action));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void navigateToDownloadDocumentView(final Document document) {
|
|
||||||
// No operation
|
|
||||||
}
|
|
||||||
|
|
||||||
private void navigateToAddDocumentView() {
|
private void navigateToAddDocumentView() {
|
||||||
getUI().ifPresent(ui -> ui.navigate(DocumentView.class, "new"));
|
getUI().ifPresent(ui -> ui.navigate(DocumentView.class, "new"));
|
||||||
}
|
}
|
||||||
@ -114,8 +136,30 @@ public class DocumentsListView extends Main {
|
|||||||
documentGrid.setPageSize(5);
|
documentGrid.setPageSize(5);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateDocumentGrid() {
|
private void updateDocumentGrid(final DocumentType documentType, final Employee employee) {
|
||||||
documentGrid.setPagingDataProvider((page, pageSize) -> fetchDocuments((int) page, pageSize));
|
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) {
|
private List<Document> fetchDocuments(final int page, final int size) {
|
||||||
@ -136,6 +180,24 @@ public class DocumentsListView extends Main {
|
|||||||
sortOrder.getDirection() == SortDirection.ASCENDING);
|
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
|
@FunctionalInterface
|
||||||
private interface DocumentActionHandler {
|
private interface DocumentActionHandler {
|
||||||
void handle(Document document);
|
void handle(Document document);
|
||||||
|
Loading…
Reference in New Issue
Block a user