From 8c283d8553066cd4aac4388dbd8b78a8e4ac6c46 Mon Sep 17 00:00:00 2001 From: Matthias Ronge Date: Tue, 18 Jun 2024 15:11:23 +0200 Subject: [PATCH] Extracts the renaming of processes into the process service --- .../kitodo/production/forms/ProcessForm.java | 104 ++---------------- .../services/data/ProcessService.java | 81 ++++++++++++++ 2 files changed, 93 insertions(+), 92 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 b1abe02a8e8..5c7d8e5347d 100644 --- a/Kitodo/src/main/java/org/kitodo/production/forms/ProcessForm.java +++ b/Kitodo/src/main/java/org/kitodo/production/forms/ProcessForm.java @@ -84,6 +84,7 @@ public class ProcessForm extends TemplateBaseForm { private Property property; private final FilterMenu filterMenu = new FilterMenu(this); private final transient FileService fileService = ServiceManager.getFileService(); + private final transient ProcessService processService = ServiceManager.getProcessService(); private final transient WorkflowControllerService workflowControllerService = new WorkflowControllerService(); private final String processEditPath = MessageFormat.format(REDIRECT_PATH, "processEdit"); @@ -185,7 +186,7 @@ public String save() { } try { - ServiceManager.getProcessService().save(this.process, true); + processService.save(this.process, true); return processesPage; } catch (DataException e) { Helper.setErrorMessage(ERROR_SAVING, new Object[] {ObjectType.PROCESS.getTranslationSingular() }, @@ -204,7 +205,7 @@ public String save() { */ public String createProcessAsChild(ProcessDTO processDTO) { try { - Process process = ServiceManager.getProcessService().getById(processDTO.getId()); + Process process = processService.getById(processDTO.getId()); if (Objects.nonNull(process.getTemplate()) && Objects.nonNull(process.getProject())) { return CREATE_PROCESS_PATH + "&templateId=" + process.getTemplate().getId() + "&projectId=" + process.getProject().getId() + "&parentId=" + process.getId(); @@ -271,7 +272,7 @@ private boolean renameAfterProcessTitleChanged() { return false; } else { try { - renameProcess(this.process, this.newProcessTitle); + processService.renameProcess(this.process, this.newProcessTitle); } catch (IOException | RuntimeException e) { Helper.setErrorMessage("errorRenaming", new Object[] {Helper.getTranslation("directory") }, logger, e); } @@ -282,77 +283,6 @@ private boolean renameAfterProcessTitleChanged() { return true; } - /** - * Renames a process. In particular, the variable {@code (processtitle)} - * must be adjusted. This also applies to the directory structure. - * - * @param process - * process to be renamed - * @param newProcessTitle - * new process name - * @throws IOException - * if an error occurs while accessing the file system - */ - // FIXME: Functionality should not be implemented in form classes - public void renameProcess(Process process, String newProcessTitle) throws IOException { - renamePropertiesValuesForProcessTitle(process.getProperties(), process.getTitle(), newProcessTitle); - renamePropertiesValuesForProcessTitle(process.getTemplates(), process.getTitle(), newProcessTitle); - removePropertiesWithEmptyTitle(process.getWorkpieces(), process); - - renameImageDirectories(process, newProcessTitle); - renameOcrDirectories(process, newProcessTitle); - renameDefinedDirectories(process, newProcessTitle); - - process.setTitle(this.newProcessTitle); - } - - private void renamePropertiesValuesForProcessTitle(List properties, String processTitle, String newProcessTitle) { - for (Property property : properties) { - if (Objects.nonNull(property.getValue()) && property.getValue().contains(processTitle)) { - property.setValue(property.getValue().replaceAll(processTitle, newProcessTitle)); - } - } - } - - private void renameImageDirectories(Process process, String newProcessTitle) throws IOException { - URI imageDirectory = fileService.getImagesDirectory(process); - renameDirectories(imageDirectory, process, newProcessTitle); - } - - private void renameOcrDirectories(Process process, String newProcessTitle) throws IOException { - URI ocrDirectory = fileService.getOcrDirectory(process); - renameDirectories(ocrDirectory, process, newProcessTitle); - } - - private void renameDirectories(URI directory, Process process, String newProcessTitle) throws IOException { - if (fileService.isDirectory(directory)) { - List subDirs = fileService.getSubUris(directory); - for (URI imageDir : subDirs) { - if (fileService.isDirectory(imageDir)) { - fileService.renameFile(imageDir, imageDir.toString().replace(process.getTitle(), newProcessTitle)); - } - } - } - } - - private void renameDefinedDirectories(Process process, String newProcessTitle) { - String[] processDirs = ConfigCore.getStringArrayParameter(ParameterCore.PROCESS_DIRS); - for (String processDir : processDirs) { - // TODO: check it out - URI processDirAbsolute = ServiceManager.getProcessService().getProcessDataDirectory(process) - .resolve(processDir.replace("(processtitle)", process.getTitle())); - - File dir = new File(processDirAbsolute); - boolean renamed; - if (dir.isDirectory()) { - renamed = dir.renameTo(new File(dir.getAbsolutePath().replace(process.getTitle(), newProcessTitle))); - if (!renamed) { - Helper.setErrorMessage("errorRenaming", new Object[] {dir.getName() }); - } - } - } - } - /** * Remove template properties. */ @@ -523,7 +453,7 @@ public void setTaskStatusDown() { private void refreshParent() { try { if (Objects.nonNull(process.getParent())) { - this.process.setParent(ServiceManager.getProcessService().getById(process.getParent().getId())); + this.process.setParent(processService.getById(process.getParent().getId())); } } catch (DAOException e) { Helper.setErrorMessage(ERROR_LOADING_ONE, @@ -754,7 +684,7 @@ public void deleteProperty() { this.process.getProperties().remove(this.property); List propertiesToFilterTitle = this.process.getProperties(); - removePropertiesWithEmptyTitle(propertiesToFilterTitle, this.process); + processService.removePropertiesWithEmptyTitle(propertiesToFilterTitle, this.process); loadProcessProperties(); } @@ -768,16 +698,6 @@ public void duplicateProperty() { loadProcessProperties(); } - // TODO: is it really a case that title is empty? - private void removePropertiesWithEmptyTitle(List properties, Process process) { - for (Property processProperty : properties) { - if (Objects.isNull(processProperty.getTitle()) || processProperty.getTitle().isEmpty()) { - processProperty.getProcesses().clear(); - process.getProperties().remove(processProperty); - } - } - } - /** * Get dockets for select list. * @@ -877,7 +797,7 @@ public void load(int id) { } try { if (id != 0) { - setProcess(ServiceManager.getProcessService().getById(id)); + setProcess(processService.getById(id)); } setSaveDisabled(true); } catch (DAOException e) { @@ -996,7 +916,7 @@ public void setFilter(String filter) { * @return String containing titles of current tasks of given process */ public String getCurrentTaskTitles(ProcessDTO processDTO) { - return ServiceManager.getProcessService().createProgressTooltip(processDTO); + return processService.createProgressTooltip(processDTO); } /** @@ -1006,7 +926,7 @@ public String getCurrentTaskTitles(ProcessDTO processDTO) { */ public List getAllParentProcesses(int processId) { try { - return ProcessService.getAllParentProcesses(ServiceManager.getProcessService().getById(processId)); + return ProcessService.getAllParentProcesses(processService.getById(processId)); } catch (DAOException e) { Helper.setErrorMessage(ERROR_LOADING_ONE, new Object[] {ObjectType.PROCESS.getTranslationSingular(), processId }, logger, e); return new ArrayList<>(); @@ -1022,7 +942,7 @@ public List getAllParentProcesses(int processId) { */ public int getNumberOfChildProcesses(int processId) { try { - return ServiceManager.getProcessService().getNumberOfChildren(processId); + return processService.getNumberOfChildren(processId); } catch (DAOException e) { Helper.setErrorMessage(ERROR_LOADING_ONE, new Object[] {ObjectType.PROCESS.getTranslationSingular(), processId }, logger, e); return 0; @@ -1052,7 +972,7 @@ public String convertProcessingDate(Date date) { * @return List of filtered tasks as DTO objects */ public List getCurrentTasksForUser(ProcessDTO processDTO) { - return ServiceManager.getProcessService().getCurrentTasksForUser(processDTO, ServiceManager.getUserService().getCurrentUser()); + return processService.getCurrentTasksForUser(processDTO, ServiceManager.getUserService().getCurrentUser()); } /** @@ -1061,7 +981,7 @@ public List getCurrentTasksForUser(ProcessDTO processDTO) { */ public String getAmount() { try { - return ServiceManager.getProcessService().count(ServiceManager.getProcessService() + return processService.count(processService .getQueryForFilter(isShowClosedProcesses(), isShowInactiveProjects(), filter)).toString(); } catch (DataException e) { Helper.setErrorMessage(e); 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 25a3abcf045..e2b5643213a 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 @@ -2566,6 +2566,87 @@ public static void createXML(Process process, User user) throws IOException { xmlExport.exportXmlLog(getDocketData(process), destination); } + /** + * Renames a process. In particular, the variable {@code (processtitle)} + * must be adjusted. This also applies to the directory structure. + * + * @param process + * process to be renamed + * @param newProcessTitle + * new process name + * @throws IOException + * if an error occurs while accessing the file system + */ + public void renameProcess(Process process, String newProcessTitle) throws IOException { + renamePropertiesValuesForProcessTitle(process.getProperties(), process.getTitle(), newProcessTitle); + renamePropertiesValuesForProcessTitle(process.getTemplates(), process.getTitle(), newProcessTitle); + removePropertiesWithEmptyTitle(process.getWorkpieces(), process); + + renameImageDirectories(process, newProcessTitle); + renameOcrDirectories(process, newProcessTitle); + renameDefinedDirectories(process, newProcessTitle); + + process.setTitle(newProcessTitle); + } + + private void renamePropertiesValuesForProcessTitle(List properties, String processTitle, String newProcessTitle) { + for (Property property : properties) { + if (Objects.nonNull(property.getValue()) && property.getValue().contains(processTitle)) { + property.setValue(property.getValue().replaceAll(processTitle, newProcessTitle)); + } + } + } + + + // TODO: is it really a case that title is empty? + public void removePropertiesWithEmptyTitle(List properties, Process process) { + for (Property processProperty : properties) { + if (Objects.isNull(processProperty.getTitle()) || processProperty.getTitle().isEmpty()) { + processProperty.getProcesses().clear(); + process.getProperties().remove(processProperty); + } + } + } + + private void renameImageDirectories(Process process, String newProcessTitle) throws IOException { + URI imageDirectory = fileService.getImagesDirectory(process); + renameDirectories(imageDirectory, process, newProcessTitle); + } + + private void renameOcrDirectories(Process process, String newProcessTitle) throws IOException { + URI ocrDirectory = fileService.getOcrDirectory(process); + renameDirectories(ocrDirectory, process, newProcessTitle); + } + + private void renameDirectories(URI directory, Process process, String newProcessTitle) throws IOException { + if (fileService.isDirectory(directory)) { + List subDirs = fileService.getSubUris(directory); + for (URI imageDir : subDirs) { + if (fileService.isDirectory(imageDir)) { + fileService.renameFile(imageDir, imageDir.toString().replace(process.getTitle(), newProcessTitle)); + } + } + } + } + + private void renameDefinedDirectories(Process process, String newProcessTitle) { + String[] processDirs = ConfigCore.getStringArrayParameter(ParameterCore.PROCESS_DIRS); + for (String processDir : processDirs) { + // TODO: check it out + URI processDirAbsolute = ServiceManager.getProcessService().getProcessDataDirectory(process) + .resolve(processDir.replace("(processtitle)", process.getTitle())); + + File dir = new File(processDirAbsolute); + boolean renamed; + if (dir.isDirectory()) { + renamed = dir.renameTo(new File(dir.getAbsolutePath().replace(process.getTitle(), newProcessTitle))); + if (!renamed) { + Helper.setErrorMessage("errorRenaming", new Object[] {dir.getName() }); + } + } + } + } + /** * Create and return PieChartModel for given process values. *