From ef9c9290eb903b71c4a1997f3681299384df24d2 Mon Sep 17 00:00:00 2001 From: Ikram Maalej Date: Tue, 17 Jan 2023 15:24:30 +0100 Subject: [PATCH 1/3] Fix select all Processes --- .../kitodo/production/forms/ProcessForm.java | 43 ++++++++++++ .../production/forms/ProcessListBaseView.java | 70 ++++++++++++++++++- .../kitodo/production/model/LazyDTOModel.java | 2 +- .../services/data/base/SearchService.java | 8 ++- .../main/webapp/WEB-INF/resources/js/lists.js | 24 ++++--- .../includes/processes/processList.xhtml | 17 ++++- 6 files changed, 148 insertions(+), 16 deletions(-) diff --git a/Kitodo/src/main/java/org/kitodo/production/forms/ProcessForm.java b/Kitodo/src/main/java/org/kitodo/production/forms/ProcessForm.java index 70093686c61..2a7603d303c 100644 --- a/Kitodo/src/main/java/org/kitodo/production/forms/ProcessForm.java +++ b/Kitodo/src/main/java/org/kitodo/production/forms/ProcessForm.java @@ -60,6 +60,9 @@ import org.kitodo.production.services.file.FileService; import org.kitodo.production.services.workflow.WorkflowControllerService; import org.primefaces.PrimeFaces; +import org.primefaces.event.SelectEvent; +import org.primefaces.event.ToggleSelectEvent; +import org.primefaces.event.UnselectEvent; import org.primefaces.model.SortOrder; @Named("ProcessForm") @@ -1125,4 +1128,44 @@ public String navigateToProcessesList(boolean resetTableViewState) { return "/pages/processes?tabIndex=0&faces-redirect=true"; } + /** + * Callback function triggered when a process is unselected in the data table. + * + * @param unselectEvent as UnUnselectEvent + */ + public void onRowUnselect(UnselectEvent unselectEvent) { + if (allSelected) { + excludedProcessIds.add(getProcessId(unselectEvent.getObject())); + } + } + + /** + * Callback function triggered when a process is selected in the data table. + * + * @param selectEvent as SelectEvent + */ + public void onRowSelect(SelectEvent selectEvent) { + if (allSelected) { + excludedProcessIds.remove(getProcessId(selectEvent.getObject())); + PrimeFaces.current().executeScript("PF('processesTable').selection=new Array('@all')"); + PrimeFaces.current().executeScript("$(PF('processesTable').selectionHolder).val('@all')"); + } + } + + /** + * Callback function triggered when all processes are selected or unselected in the data table. + * + * @param toggleSelectEvent as ToggleSelectEvent + */ + public void selectAll(ToggleSelectEvent toggleSelectEvent) { + setAllSelected(false); + } + + private int getProcessId(Object process) { + if (process instanceof Process) { + return ((Process) process).getId(); + } else { + return ((ProcessDTO) process).getId(); + } + } } diff --git a/Kitodo/src/main/java/org/kitodo/production/forms/ProcessListBaseView.java b/Kitodo/src/main/java/org/kitodo/production/forms/ProcessListBaseView.java index 56a904687ad..247b0a3e83a 100644 --- a/Kitodo/src/main/java/org/kitodo/production/forms/ProcessListBaseView.java +++ b/Kitodo/src/main/java/org/kitodo/production/forms/ProcessListBaseView.java @@ -17,6 +17,7 @@ import java.net.URI; import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Objects; @@ -42,6 +43,8 @@ import org.kitodo.production.services.data.ProcessService; import org.kitodo.production.services.dataformat.MetsService; import org.primefaces.PrimeFaces; +import org.primefaces.component.datatable.DataTable; +import org.primefaces.event.data.PageEvent; import org.primefaces.model.charts.hbar.HorizontalBarChartModel; import org.primefaces.model.charts.pie.PieChartModel; @@ -62,6 +65,9 @@ public class ProcessListBaseView extends BaseForm { private final HashMap exportable = new HashMap<>(); + boolean allSelected = false; + HashSet excludedProcessIds = new HashSet<>(); + /** * Constructor. */ @@ -70,6 +76,43 @@ public ProcessListBaseView() { super.setLazyDTOModel(new LazyProcessDTOModel(ServiceManager.getProcessService())); } + /** + * Gets excludedProcessIds. + * + * @return value of excludedProcessIds + */ + public HashSet getExcludedProcessIds() { + return excludedProcessIds; + } + + /** + * Sets excludedProcessIds. + * + * @param excludedProcessIds value of excludedProcessIds + */ + public void setExcludedProcessIds(HashSet excludedProcessIds) { + this.excludedProcessIds = excludedProcessIds; + } + + /** + * Gets allSelected. + * + * @return value of allSelected + */ + public boolean isAllSelected() { + return allSelected; + } + + /** + * Sets allSelected. + * + * @param allSelected value of allSelected + */ + public void setAllSelected(boolean allSelected) { + this.allSelected = allSelected; + excludedProcessIds.clear(); + } + /** * Returns the list of the processes currently selected in the user interface. * Converts ProcessDTO instances to Process instances in case of displaying search results. @@ -80,6 +123,16 @@ public ProcessListBaseView() { public List getSelectedProcesses() { List selectedProcesses = new ArrayList<>(); if (selectedProcessesOrProcessDTOs.size() > 0) { + ProcessService processService = ServiceManager.getProcessService(); + if (allSelected) { + try { + this.selectedProcessesOrProcessDTOs = processService.findByQuery(processService.getQueryForFilter( + this.isShowClosedProcesses(), isShowInactiveProjects(), getFilter()) + .mustNot(processService.createSetQueryForIds(new ArrayList<>(excludedProcessIds))), false); + } catch (DataException e) { + logger.error(e.getMessage()); + } + } if (selectedProcessesOrProcessDTOs.get(0) instanceof ProcessDTO) { // list contains ProcessDTO instances try { @@ -87,11 +140,11 @@ public List getSelectedProcesses() { .convertDtosToBeans((List) selectedProcessesOrProcessDTOs); } catch (DAOException e) { Helper.setErrorMessage(ERROR_LOADING_MANY, - new Object[] {ObjectType.PROCESS.getTranslationPlural() }, logger, e); + new Object[]{ObjectType.PROCESS.getTranslationPlural()}, logger, e); } } else if (selectedProcessesOrProcessDTOs.get(0) instanceof Process) { // list contains Process instances - selectedProcesses = (List) selectedProcessesOrProcessDTOs; + selectedProcesses = (List) selectedProcessesOrProcessDTOs; } } return selectedProcesses; @@ -556,4 +609,17 @@ public void setSelectedProcessesOrProcessDTOs(List selectedPro this.selectedProcessesOrProcessDTOs = selectedProcessesOrProcessDTOs; } + /** + * Update selection and first row to show in datatable on PageEvent. + * @param pageEvent PageEvent triggered by data tables paginator + */ + public void onPageChange(PageEvent pageEvent) { + this.setFirstRow(((DataTable) pageEvent.getSource()).getFirst()); + if (allSelected) { + PrimeFaces.current() + .executeScript("PF('processesTable').selectAllRows();"); + excludedProcessIds.forEach(processId -> PrimeFaces.current() + .executeScript("PF('processesTable').unselectRow($('tr[data-rk=\"" + processId + "\"]'), true);")); + } + } } diff --git a/Kitodo/src/main/java/org/kitodo/production/model/LazyDTOModel.java b/Kitodo/src/main/java/org/kitodo/production/model/LazyDTOModel.java index 7676b353e79..791a8268cc9 100644 --- a/Kitodo/src/main/java/org/kitodo/production/model/LazyDTOModel.java +++ b/Kitodo/src/main/java/org/kitodo/production/model/LazyDTOModel.java @@ -74,7 +74,7 @@ public LazyDTOModel(SearchDatabaseService searchService) { public Object getRowData(String rowKey) { try { return searchService.getById(Integer.parseInt(rowKey)); - } catch (DAOException e) { + } catch (DAOException | NumberFormatException e) { logger.error(e.getMessage()); return null; } diff --git a/Kitodo/src/main/java/org/kitodo/production/services/data/base/SearchService.java b/Kitodo/src/main/java/org/kitodo/production/services/data/base/SearchService.java index 6dca457de7f..0758af802dc 100644 --- a/Kitodo/src/main/java/org/kitodo/production/services/data/base/SearchService.java +++ b/Kitodo/src/main/java/org/kitodo/production/services/data/base/SearchService.java @@ -567,7 +567,13 @@ protected List convertRelatedJSONObjectToDTO(Map ids) { + /** + * Builds a ElasticSearch query for list of Ids. + * + * @param ids as a List of Integer + * @return query as QueryBuilder + */ + public QueryBuilder createSetQueryForIds(List ids) { return termsQuery("_id", ids); } diff --git a/Kitodo/src/main/webapp/WEB-INF/resources/js/lists.js b/Kitodo/src/main/webapp/WEB-INF/resources/js/lists.js index 28ed1868e60..3d2687426f8 100644 --- a/Kitodo/src/main/webapp/WEB-INF/resources/js/lists.js +++ b/Kitodo/src/main/webapp/WEB-INF/resources/js/lists.js @@ -35,11 +35,7 @@ function registerRowToggleEvents(event) { PrimeFaces.widget.DataTable.prototype.updateData = (function () { let cachedFunction = PrimeFaces.widget.DataTable.prototype.updateData; return function () { - let reselectAll = (typeof this.selection !== "undefined" && this.selection[0] === '@all'); let result = cachedFunction.apply(this, arguments); - if (reselectAll) { - this.selectAllRows(); - } return result; }; })(); @@ -53,10 +49,18 @@ $(document).on("click", ".allSelectable .ui-chkbox-all .ui-chkbox-box", function table.unselectAllRows(); }); -$(document).on("click", ".allSelectable .ui-chkbox .ui-chkbox-box", function () { - let tableId = $(this).closest(".allSelectable").attr('id').split(":").at(-1); - let table = new PF(tableId); - if (typeof table.selection !== "undefined" && table.selection[0] === '@all') { - table.unselectAllRows(); - } +$(window).on("load", function () { + $.ready.then(function () { + if (typeof PF('processesTable').selection !== "undefined" && (PF('processesTable').selection[0] === '@all' || PF('processesTable').selection.length === PF('processesTable').cfg.paginator.rowCount )) { + PF('processesTable').selectAllRows(); + PF('processesTable').selection=new Array("@all"); + $(PF('processesTable').selectionHolder).val('@all'); + let excludedIds = $('#excludedProcessIds').children(); + for(let i = 0; i < excludedIds.length; i++) { + let processId = excludedIds.get(i).textContent; + PF('processesTable').unselectRow($('tr[data-rk="' + processId + '"]'), true); + } + + } + }); }); diff --git a/Kitodo/src/main/webapp/WEB-INF/templates/includes/processes/processList.xhtml b/Kitodo/src/main/webapp/WEB-INF/templates/includes/processes/processList.xhtml index bb336c17efa..a667ce10078 100644 --- a/Kitodo/src/main/webapp/WEB-INF/templates/includes/processes/processList.xhtml +++ b/Kitodo/src/main/webapp/WEB-INF/templates/includes/processes/processList.xhtml @@ -29,6 +29,7 @@ lazy="true" paginator="true" resizableColumns="true" + rowSelectMode="checkbox" liveResize="true" sortBy="#{process.id}" sortOrder="descending" @@ -42,7 +43,13 @@ + oncomplete="registerRowToggleEvents();"/> + + + @@ -249,9 +256,15 @@
  • -
  • +
+ +