Skip to content

Commit

Permalink
Extracts the renaming of processes into the process service
Browse files Browse the repository at this point in the history
  • Loading branch information
matthias-ronge committed Jun 18, 2024
1 parent 1092761 commit ba98129
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 67 deletions.
70 changes: 3 additions & 67 deletions Kitodo/src/main/java/org/kitodo/production/forms/ProcessForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -270,73 +271,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 @@ -738,7 +684,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 @@ -752,16 +698,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 @@ -2566,6 +2566,85 @@ 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));
}
}
}

// TODO: is it really a case that title is empty?
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 ba98129

Please sign in to comment.