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());