From 7cb20996250b24ead3bb8021249fd81cb7014894 Mon Sep 17 00:00:00 2001 From: BartChris Date: Mon, 16 Sep 2024 15:27:23 +0200 Subject: [PATCH] Optimize closing of processes --- .../services/data/ProcessService.java | 25 +++++++++++++++++++ .../workflow/WorkflowControllerService.java | 7 ++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/Kitodo/src/main/java/org/kitodo/production/services/data/ProcessService.java b/Kitodo/src/main/java/org/kitodo/production/services/data/ProcessService.java index 5ecc4467811..162a1cece58 100644 --- a/Kitodo/src/main/java/org/kitodo/production/services/data/ProcessService.java +++ b/Kitodo/src/main/java/org/kitodo/production/services/data/ProcessService.java @@ -805,6 +805,31 @@ public List findLinkableChildProcesses(String searchInput, int rules return linkableProcesses; } + /** + * Checks whether all child processes of the given parent process are in a completed state. + * + * @param parentProcess The parent process whose child processes are being checked. + * @return true if all child processes are in a completed state, false otherwise. + */ + public boolean areAllChildProcessesInCompletedState(Process parentProcess) { + String hql = "SELECT count(p) FROM Process p WHERE p.parent = :parent " + + "AND p.sortHelperStatus NOT IN (:completedStates)"; + + Map parameters = new HashMap<>(); + parameters.put("parent", parentProcess); + parameters.put("completedStates", List.of( + ProcessState.COMPLETED20.getValue(), + ProcessState.COMPLETED.getValue() + )); + try { + Long count = countDatabaseRows(hql, parameters); + return count == 0; // If count is 0, all child processes are completed + } catch (DAOException e) { + logger.error(e.getMessage(), e); + return false; + } + } + /** * Searches for linkable processes based on user input. A process can be * linked if it has the same rule set, belongs to the same client, and the diff --git a/Kitodo/src/main/java/org/kitodo/production/services/workflow/WorkflowControllerService.java b/Kitodo/src/main/java/org/kitodo/production/services/workflow/WorkflowControllerService.java index 5575caa251e..9e1a180d32c 100644 --- a/Kitodo/src/main/java/org/kitodo/production/services/workflow/WorkflowControllerService.java +++ b/Kitodo/src/main/java/org/kitodo/production/services/workflow/WorkflowControllerService.java @@ -474,11 +474,14 @@ private void activateTasksForClosedTask(Task closedTask) throws DataException, I TaskManager.addTask(thread); } - closeParent(process); + if (closedTask.isLast()) { + closeParent(process); + } } private void closeParent(Process process) throws DataException { - if (Objects.nonNull(process.getParent()) && allChildrenClosed(process.getParent())) { + if (Objects.nonNull(process.getParent()) && ServiceManager.getProcessService() + .areAllChildProcessesInCompletedState(process.getParent())) { process.getParent().setSortHelperStatus(ProcessState.COMPLETED.getValue()); ServiceManager.getProcessService().save(process.getParent()); closeParent(process.getParent());