Perfil-Empleado-Administrativo-Documentacion #39
@ -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
|
||||||
@ -49,4 +52,20 @@ 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(DocumentType documentType, Employee employee, int start, 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,12 +5,13 @@ 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;
|
||||||
import com.vaadin.flow.component.html.Anchor;
|
|
||||||
import com.vaadin.flow.component.html.Main;
|
import com.vaadin.flow.component.html.Main;
|
||||||
import com.vaadin.flow.component.html.Span;
|
import com.vaadin.flow.component.html.Span;
|
||||||
|
import com.vaadin.flow.component.notification.Notification;
|
||||||
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;
|
||||||
@ -22,7 +23,6 @@ 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.io.ByteArrayInputStream;
|
||||||
import java.util.Base64;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
@ -36,12 +36,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() {
|
||||||
@ -55,9 +57,7 @@ public class DocumentsListView extends Main {
|
|||||||
private void configureDocumentGrid() {
|
private void configureDocumentGrid() {
|
||||||
documentGrid.setColumns("fileName", "documentType", "creator");
|
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::downloadDocument);
|
|
||||||
configurePagination();
|
configurePagination();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,6 +67,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)));
|
||||||
@ -79,21 +84,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(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");
|
||||||
}
|
}
|
||||||
@ -106,10 +128,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"));
|
||||||
}
|
}
|
||||||
@ -119,8 +137,27 @@ public class DocumentsListView extends Main {
|
|||||||
documentGrid.setPageSize(5);
|
documentGrid.setPageSize(5);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateDocumentGrid() {
|
private void updateDocumentGrid(DocumentType documentType, 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(DocumentType documentType) {
|
||||||
|
return documentType != null && !"All".equals(documentType.name());
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isValidEmployee(Employee employee) {
|
||||||
|
return employee != null && !"All".equals(employee.getFirstName());
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Document> fetchFilteredDocuments(int page, 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) {
|
||||||
@ -142,14 +179,21 @@ public class DocumentsListView extends Main {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void downloadDocument(final Document document) {
|
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(),
|
StreamResource resource = new StreamResource(document.getFileName(),
|
||||||
() -> new ByteArrayInputStream(document.getFileData()));
|
() -> new ByteArrayInputStream(document.getFileData()));
|
||||||
resource.setContentType("application/pdf");
|
resource.setContentType("application/pdf");
|
||||||
resource.setHeader("Content-Disposition", "attachment; filename=\"" + document.getFileName() + ".pdf\"");
|
resource.setHeader("Content-Disposition", "attachment; filename=\"" + document.getFileName() + ".pdf\"");
|
||||||
getUI().ifPresent(ui -> {
|
return resource;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void openDocumentStream(StreamResource resource, UI ui) {
|
||||||
StreamRegistration registration = ui.getSession().getResourceRegistry().registerResource(resource);
|
StreamRegistration registration = ui.getSession().getResourceRegistry().registerResource(resource);
|
||||||
ui.getPage().open(registration.getResourceUri().toString());
|
ui.getPage().open(registration.getResourceUri().toString());
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@FunctionalInterface
|
@FunctionalInterface
|
||||||
|
Loading…
Reference in New Issue
Block a user