Skip to content

Commit

Permalink
Merge pull request kitodo#6106 from matthias-ronge/extract-rename-pro…
Browse files Browse the repository at this point in the history
…cesses

Extracts the renaming of processes into the process service
  • Loading branch information
solth authored Aug 14, 2024
2 parents 0e6e0ee + 402984d commit 335afc0
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 68 deletions.
71 changes: 3 additions & 68 deletions Kitodo/src/main/java/org/kitodo/production/forms/ProcessForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

package org.kitodo.production.forms;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
Expand Down Expand Up @@ -87,6 +86,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");

Expand Down Expand Up @@ -274,73 +274,18 @@ private boolean renameAfterProcessTitleChanged() {
Helper.setErrorMessage("processTitleInvalid", new Object[] {validateRegEx });
return false;
} else {
renamePropertiesValuesForProcessTitle(this.process.getProperties());
renamePropertiesValuesForProcessTitle(this.process.getTemplates());
removePropertiesWithEmptyTitle(this.process.getWorkpieces());

try {
renameImageDirectories();
renameOcrDirectories();
renameDefinedDirectories();
processService.renameProcess(this.process, this.newProcessTitle);
} catch (IOException | RuntimeException e) {
Helper.setErrorMessage("errorRenaming", new Object[] {Helper.getTranslation("directory") }, logger, e);
}

this.process.setTitle(this.newProcessTitle);

// remove Tiffwriter file
ServiceManager.getKitodoScriptService().deleteTiffHeaderFile(List.of(process));
}
return true;
}

private void renamePropertiesValuesForProcessTitle(List<Property> properties) {
for (Property property : properties) {
if (Objects.nonNull(property.getValue()) && property.getValue().contains(this.process.getTitle())) {
property.setValue(property.getValue().replaceAll(this.process.getTitle(), this.newProcessTitle));
}
}
}

private void renameImageDirectories() throws IOException {
URI imageDirectory = fileService.getImagesDirectory(process);
renameDirectories(imageDirectory);
}

private void renameOcrDirectories() throws IOException {
URI ocrDirectory = fileService.getOcrDirectory(process);
renameDirectories(ocrDirectory);
}

private void renameDirectories(URI directory) throws IOException {
if (fileService.isDirectory(directory)) {
List<URI> subDirs = fileService.getSubUris(directory);
for (URI imageDir : subDirs) {
if (fileService.isDirectory(imageDir)) {
fileService.renameFile(imageDir, imageDir.toString().replace(process.getTitle(), newProcessTitle));
}
}
}
}

private void renameDefinedDirectories() {
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.
*/
Expand Down Expand Up @@ -742,7 +687,7 @@ public void deleteProperty() {
this.process.getProperties().remove(this.property);

List<Property> propertiesToFilterTitle = this.process.getProperties();
removePropertiesWithEmptyTitle(propertiesToFilterTitle);
processService.removePropertiesWithEmptyTitle(propertiesToFilterTitle, this.process);
loadProcessProperties();
}

Expand All @@ -756,16 +701,6 @@ public void duplicateProperty() {
loadProcessProperties();
}

// TODO: is it really a case that title is empty?
private void removePropertiesWithEmptyTitle(List<Property> properties) {
for (Property processProperty : properties) {
if (Objects.isNull(processProperty.getTitle()) || processProperty.getTitle().isEmpty()) {
processProperty.getProcesses().clear();
this.process.getProperties().remove(processProperty);
}
}
}

/**
* Get dockets for select list.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2586,6 +2586,95 @@ public static void createXML(Process process, User user) throws IOException {
xmlExport.exportXmlLog(getDocketData(process), destination);
}

/**
* Renames a process.
*
* @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<Property> 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));
}
}
}

/**
* Removes properties with empty title.
*
* <p>
* TODO: Is it really a case that title is empty?
*
* @param properties
* property list to be checked
* @param process
* process from which the properties are to be deleted
*/
public void removePropertiesWithEmptyTitle(List<Property> 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<URI> 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.
*
Expand Down

0 comments on commit 335afc0

Please sign in to comment.