Skip to content

Commit

Permalink
Implement Kitodo Script to explode processes
Browse files Browse the repository at this point in the history
  • Loading branch information
matthias-ronge committed Jun 18, 2024
1 parent 1092761 commit 9c70021
Show file tree
Hide file tree
Showing 3 changed files with 640 additions and 21 deletions.
46 changes: 25 additions & 21 deletions Kitodo/src/main/java/org/kitodo/production/forms/ProcessForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -270,45 +270,49 @@ 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();
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) {
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 static void renamePropertiesValuesForProcessTitle(List<Property> properties, String processTitle, String newProcessTitle) {
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));
if (Objects.nonNull(property.getValue()) && property.getValue().contains(processTitle)) {
property.setValue(property.getValue().replaceAll(processTitle, newProcessTitle));
}
}
}

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

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

private void renameDirectories(URI directory) throws IOException {
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) {
Expand All @@ -319,7 +323,7 @@ private void renameDirectories(URI directory) throws IOException {
}
}

private void renameDefinedDirectories() {
private static void renameDefinedDirectories(Process process, String newProcessTitle) {
String[] processDirs = ConfigCore.getStringArrayParameter(ParameterCore.PROCESS_DIRS);
for (String processDir : processDirs) {
// TODO: check it out
Expand Down Expand Up @@ -738,7 +742,7 @@ public void deleteProperty() {
this.process.getProperties().remove(this.property);

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

Expand All @@ -753,11 +757,11 @@ public void duplicateProperty() {
}

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

0 comments on commit 9c70021

Please sign in to comment.