diff --git a/Kitodo/src/main/java/org/kitodo/production/forms/createprocess/TitleRecordLinkTab.java b/Kitodo/src/main/java/org/kitodo/production/forms/createprocess/TitleRecordLinkTab.java index 831ad34c9b2..0224b4f742d 100644 --- a/Kitodo/src/main/java/org/kitodo/production/forms/createprocess/TitleRecordLinkTab.java +++ b/Kitodo/src/main/java/org/kitodo/production/forms/createprocess/TitleRecordLinkTab.java @@ -16,6 +16,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.Comparator; import java.util.LinkedList; import java.util.List; import java.util.Locale; @@ -311,26 +312,27 @@ public void searchForParentProcesses() { } try { List processes = ServiceManager.getProcessService().findLinkableParentProcesses(searchQuery, - createProcessForm.getProject().getId(), createProcessForm.getTemplate().getRuleset().getId()); + createProcessForm.getTemplate().getRuleset().getId()); if (processes.isEmpty()) { Helper.setMessage("createProcessForm.titleRecordLinkTab.searchButtonClick.noHits"); } indicationOfMoreHitsVisible = processes.size() > MAXIMUM_NUMBER_OF_HITS; possibleParentProcesses = ServiceManager.getImportService() .getPotentialParentProcesses(processes, MAXIMUM_NUMBER_OF_HITS); - } catch (DataException | DAOException e) { + } catch (DataException | DAOException | IOException e) { Helper.setErrorMessage("createProcessForm.titleRecordLinkTab.searchButtonClick.error", e.getMessage(), logger, e); indicationOfMoreHitsVisible = false; possibleParentProcesses = Collections.emptyList(); } + possibleParentProcesses.sort(Comparator.comparing(SelectItem::getLabel)); for (SelectItem selectItem : possibleParentProcesses) { if (!selectItem.isDisabled()) { - int processId = Integer.parseInt(selectItem.getValue().toString()); try { + int processId = Integer.parseInt(selectItem.getValue().toString()); setParentAsTitleRecord(ServiceManager.getProcessService().getById(processId)); break; - } catch (DAOException e) { + } catch (DAOException | NumberFormatException e) { logger.error(e); } } @@ -447,16 +449,12 @@ public void setTitleRecordProcess(Process titleRecordProcess) { public void setParentAsTitleRecord(Process parentProcess) { createProcessForm.setEditActiveTabIndex(CreateProcessForm.TITLE_RECORD_LINK_TAB_INDEX); try { - ProcessDTO parentProcessDto = ServiceManager.getProcessService().findById(parentProcess.getId()); - possibleParentProcesses = ServiceManager.getImportService() - .getPotentialParentProcesses(Collections.singletonList(parentProcessDto), MAXIMUM_NUMBER_OF_HITS); - if (ImportService.userMayLinkToParent(parentProcess.getId())) { setChosenParentProcess(String.valueOf(parentProcess.getId())); } else { setChosenParentProcess(null); } - } catch (DAOException | DataException e) { + } catch (DAOException e) { Helper.setErrorMessage(e); } chooseParentProcess(); diff --git a/Kitodo/src/main/java/org/kitodo/production/services/data/ImportService.java b/Kitodo/src/main/java/org/kitodo/production/services/data/ImportService.java index 4e51f574577..9f304372706 100644 --- a/Kitodo/src/main/java/org/kitodo/production/services/data/ImportService.java +++ b/Kitodo/src/main/java/org/kitodo/production/services/data/ImportService.java @@ -1428,19 +1428,23 @@ public Collection getDetailsOfRecordIdentifierMis * @param maxNumber limit * @return list of "SelectItem" objects corresponding to given "ProcessDTO" objects. * @throws DAOException when checking whether user can link to given "ProcessDTO"s fails + * @throws IOException when opening ruleset file fails */ public ArrayList getPotentialParentProcesses(List parentCandidates, int maxNumber) - throws DAOException { + throws DAOException, IOException { ArrayList possibleParentProcesses = new ArrayList<>(); for (ProcessDTO process : parentCandidates.subList(0, Math.min(parentCandidates.size(), maxNumber))) { - SelectItem selectItem = new SelectItem(process.getId().toString(), process.getTitle()); - selectItem.setDisabled(!userMayLinkToParent(process.getId())); - if (!processInAssignedProject(process.getId())) { - String problem = Helper.getTranslation("projectNotAssignedToCurrentUser", process.getProject().getTitle()); - selectItem.setDescription(problem); - selectItem.setLabel(selectItem.getLabel() + " (" + problem + ")"); + if (ProcessService.canCreateChildProcess(process) || ProcessService.canCreateProcessWithCalendar(process)) { + SelectItem selectItem = new SelectItem(process.getId().toString(), process.getTitle()); + selectItem.setDisabled(!userMayLinkToParent(process.getId())); + if (!processInAssignedProject(process.getId())) { + String problem = Helper.getTranslation("projectNotAssignedToCurrentUser", process.getProject() + .getTitle()); + selectItem.setDescription(problem); + selectItem.setLabel(selectItem.getLabel() + " (" + problem + ")"); + } + possibleParentProcesses.add(selectItem); } - possibleParentProcesses.add(selectItem); } return possibleParentProcesses; } @@ -1452,7 +1456,7 @@ public ArrayList getPotentialParentProcesses(List parent * @return whether the process with the provided ID belongs to a project assigned to the current user or not * @throws DAOException when retrieving the process with the ID "processId" from the database fails */ - public static Boolean processInAssignedProject(int processId) throws DAOException { + public static boolean processInAssignedProject(int processId) throws DAOException { Process process = ServiceManager.getProcessService().getById(processId); if (Objects.nonNull(process)) { return ServiceManager.getUserService().getCurrentUser().getProjects().contains(process.getProject()); 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 4e933aab53f..798c876e555 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 @@ -815,15 +815,13 @@ public List findLinkableChildProcesses(String searchInput, int rules * * @param searchInput * user input - * @param projectId - * the id of the allowed project * @param rulesetId * the id of the allowed ruleset * @return found processes * @throws DataException * if the search engine fails */ - public List findLinkableParentProcesses(String searchInput, int projectId, int rulesetId) + public List findLinkableParentProcesses(String searchInput, int rulesetId) throws DataException { BoolQueryBuilder processQuery = new BoolQueryBuilder() diff --git a/Kitodo/src/test/java/org/kitodo/production/services/data/ProcessServiceIT.java b/Kitodo/src/test/java/org/kitodo/production/services/data/ProcessServiceIT.java index 51fb6c62c55..effadfd5462 100644 --- a/Kitodo/src/test/java/org/kitodo/production/services/data/ProcessServiceIT.java +++ b/Kitodo/src/test/java/org/kitodo/production/services/data/ProcessServiceIT.java @@ -319,7 +319,7 @@ public void shouldNotFindByTokenizedPropertyTitleAndWrongValue() throws DataExce @Test public void shouldFindLinkableParentProcesses() throws DataException { assertEquals("Processes were not found in index!", 1, - processService.findLinkableParentProcesses("HierarchyParent", 1, 1).size()); + processService.findLinkableParentProcesses("HierarchyParent", 1).size()); } @Ignore("for second process is attached task which is processed by blocked user")