From dbda5dcc0f26718819f1a2783fe31056d222058a Mon Sep 17 00:00:00 2001 From: Henning Gerhardt Date: Thu, 5 Oct 2023 15:41:03 +0200 Subject: [PATCH 1/4] Made to be used process id and directory configurable --- .../helper/VariableReplacerTest.java | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/Kitodo/src/test/java/org/kitodo/production/helper/VariableReplacerTest.java b/Kitodo/src/test/java/org/kitodo/production/helper/VariableReplacerTest.java index 403fbaf4b24..4caafc90e88 100644 --- a/Kitodo/src/test/java/org/kitodo/production/helper/VariableReplacerTest.java +++ b/Kitodo/src/test/java/org/kitodo/production/helper/VariableReplacerTest.java @@ -31,7 +31,7 @@ public class VariableReplacerTest { @Test public void shouldReplaceTitle() { - VariableReplacer variableReplacer = new VariableReplacer(null, prepareProcess(), null); + VariableReplacer variableReplacer = new VariableReplacer(null, prepareProcess(2, "2"), null); String replaced = variableReplacer.replace("-title (processtitle) -hardcoded test"); String expected = "-title Replacement -hardcoded test"; @@ -41,7 +41,7 @@ public void shouldReplaceTitle() { @Test public void shouldReplacePrefs() { - VariableReplacer variableReplacer = new VariableReplacer(null, prepareProcess(), null); + VariableReplacer variableReplacer = new VariableReplacer(null, prepareProcess(2, "2"), null); String replaced = variableReplacer.replace("-prefs (prefs) -hardcoded test"); String expected = "-prefs src/test/resources/rulesets/ruleset_test.xml -hardcoded test"; @@ -51,7 +51,7 @@ public void shouldReplacePrefs() { @Test public void shouldReplaceProcessPath() { - VariableReplacer variableReplacer = new VariableReplacer(null, prepareProcess(), null); + VariableReplacer variableReplacer = new VariableReplacer(null, prepareProcess(2, "2"), null); String replaced = variableReplacer.replace("-processpath (processpath) -hardcoded test"); String expected = "-processpath 2 -hardcoded test"; @@ -61,7 +61,7 @@ public void shouldReplaceProcessPath() { @Test public void shouldReplaceProjectId() { - VariableReplacer variableReplacer = new VariableReplacer(null, prepareProcess(), null); + VariableReplacer variableReplacer = new VariableReplacer(null, prepareProcess(2, "2"), null); String replaced = variableReplacer.replace("-processpath (projectid) -hardcoded test"); String expected = "-processpath " + projectId + " -hardcoded test"; @@ -71,7 +71,7 @@ public void shouldReplaceProjectId() { @Test public void shouldReplaceTitleAndFilename() { - VariableReplacer variableReplacer = new VariableReplacer(null, prepareProcess(), null); + VariableReplacer variableReplacer = new VariableReplacer(null, prepareProcess(2, "2"), null); String testFilenameWithPath = "src/testFile.txt"; String testFilename = "testFile.txt"; @@ -84,7 +84,7 @@ public void shouldReplaceTitleAndFilename() { @Test public void shouldReplaceFilename() { - VariableReplacer variableReplacer = new VariableReplacer(null, prepareProcess(), null); + VariableReplacer variableReplacer = new VariableReplacer(null, prepareProcess(2, "2"), null); String testFilenameWithPath = "src/testFile.txt"; String testFilename = "testFile.txt"; @@ -98,7 +98,7 @@ public void shouldReplaceFilename() { @Test public void shouldReplaceBasename() { - VariableReplacer variableReplacer = new VariableReplacer(null, prepareProcess(), null); + VariableReplacer variableReplacer = new VariableReplacer(null, prepareProcess(2, "2"), null); String testFilename = "src/testFilename.txt"; @@ -110,7 +110,7 @@ public void shouldReplaceBasename() { @Test public void shouldReplaceRelativePath() { - VariableReplacer variableReplacer = new VariableReplacer(null, prepareProcess(), null); + VariableReplacer variableReplacer = new VariableReplacer(null, prepareProcess(2, "2"), null); String testFilenameWithPath = "src/testFile.txt"; @@ -123,7 +123,7 @@ public void shouldReplaceRelativePath() { @Test public void shouldContainFile() { - VariableReplacer variableReplacer = new VariableReplacer(null, prepareProcess(), null); + VariableReplacer variableReplacer = new VariableReplacer(null, prepareProcess(2, "2"), null); String toBeMatched = "src/(basename)/test.txt"; @@ -132,7 +132,7 @@ public void shouldContainFile() { @Test public void shouldNotContainFile() { - VariableReplacer variableReplacer = new VariableReplacer(null, prepareProcess(), null); + VariableReplacer variableReplacer = new VariableReplacer(null, prepareProcess(2, "2"), null); String toBeMatched = "src/(projectid)/test.txt"; @@ -142,7 +142,7 @@ public void shouldNotContainFile() { @Test public void shouldReplaceGeneratorSource() { - VariableReplacer variableReplacer = new VariableReplacer(null, prepareProcess(), null); + VariableReplacer variableReplacer = new VariableReplacer(null, prepareProcess(2, "2"), null); String replaced = variableReplacer.replace("-filename (generatorsource) -hardcoded test"); String expected = "-filename " + "images/Replacementscans" + " -hardcoded test"; @@ -152,7 +152,7 @@ public void shouldReplaceGeneratorSource() { @Test public void shouldReplaceGeneratorSourcePath() { - VariableReplacer variableReplacer = new VariableReplacer(null, prepareProcess(), null); + VariableReplacer variableReplacer = new VariableReplacer(null, prepareProcess(2, "2"), null); String replaced = variableReplacer.replace("-filename (generatorsourcepath) -hardcoded test"); String expected = "-filename " + KitodoConfig.getKitodoDataDirectory() + "2/" + "images/Replacementscans" + " -hardcoded test"; @@ -162,7 +162,7 @@ public void shouldReplaceGeneratorSourcePath() { @Test public void shouldReplaceOcrdWorkflowId() { - Process process = prepareProcess(); + Process process = prepareProcess(2, "2"); Template template = new Template(); template.setOcrdWorkflowId("/template-ocrd-workflow.sh"); process.setTemplate(template); @@ -180,15 +180,15 @@ public void shouldReplaceOcrdWorkflowId() { } - private Process prepareProcess() { + private Process prepareProcess(int processId, String processFolder) { Process process = new Process(); - process.setId(2); + process.setId(processId); process.setTitle("Replacement"); Ruleset ruleset = new Ruleset(); ruleset.setId(1); ruleset.setFile("ruleset_test.xml"); process.setRuleset(ruleset); - process.setProcessBaseUri(URI.create("2")); + process.setProcessBaseUri(URI.create(processFolder)); Folder scansFolder = new Folder(); scansFolder.setFileGroup("SOURCE"); scansFolder.setPath("images/(processtitle)scans"); From 2636e121a43d89caa0cdded7e52c779c2a725192 Mon Sep 17 00:00:00 2001 From: Henning Gerhardt Date: Thu, 5 Oct 2023 16:44:17 +0200 Subject: [PATCH 2/4] Fix access to meta data on more deep structure elements --- .../production/helper/VariableReplacer.java | 13 ++++++- .../helper/VariableReplacerTest.java | 36 +++++++++++++++++ .../variableReplacer/monograph/meta.xml | 39 +++++++++++++++++++ .../variableReplacer/newspaperIssue/meta.xml | 36 +++++++++++++++++ .../periodicalVolume/meta.xml | 31 +++++++++++++++ 5 files changed, 153 insertions(+), 2 deletions(-) create mode 100644 Kitodo/src/test/resources/metadata/variableReplacer/monograph/meta.xml create mode 100644 Kitodo/src/test/resources/metadata/variableReplacer/newspaperIssue/meta.xml create mode 100644 Kitodo/src/test/resources/metadata/variableReplacer/periodicalVolume/meta.xml diff --git a/Kitodo/src/main/java/org/kitodo/production/helper/VariableReplacer.java b/Kitodo/src/main/java/org/kitodo/production/helper/VariableReplacer.java index 2ff1e071157..5703d86fe0a 100644 --- a/Kitodo/src/main/java/org/kitodo/production/helper/VariableReplacer.java +++ b/Kitodo/src/main/java/org/kitodo/production/helper/VariableReplacer.java @@ -393,8 +393,17 @@ private String determineReplacementForMetadata(Matcher variableFinder) { switch (metadataLevel) { case ALL: List allChildren = workpiece.getLogicalStructure().getChildren(); - String allFirstchildValue = allChildren.isEmpty() ? null - : MetadataEditor.getMetadataValue(allChildren.get(0), variableFinder.group(5)); + String allFirstchildValue = null; + if (!allChildren.isEmpty()) { + allFirstchildValue = MetadataEditor.getMetadataValue(allChildren.get(0), variableFinder.group(5)); + if (Objects.isNull(allFirstchildValue)) { + List firstChildChildren = allChildren.get(0).getChildren(); + if (!firstChildChildren.isEmpty()) { + allFirstchildValue = MetadataEditor.getMetadataValue(firstChildChildren.get(0), variableFinder.group(5)); + } + } + } + if (Objects.nonNull(allFirstchildValue)) { return allFirstchildValue; } diff --git a/Kitodo/src/test/java/org/kitodo/production/helper/VariableReplacerTest.java b/Kitodo/src/test/java/org/kitodo/production/helper/VariableReplacerTest.java index 4caafc90e88..8a44734da5f 100644 --- a/Kitodo/src/test/java/org/kitodo/production/helper/VariableReplacerTest.java +++ b/Kitodo/src/test/java/org/kitodo/production/helper/VariableReplacerTest.java @@ -15,15 +15,18 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import java.io.IOException; import java.net.URI; import org.junit.Test; +import org.kitodo.api.dataformat.Workpiece; import org.kitodo.config.KitodoConfig; import org.kitodo.data.database.beans.Folder; import org.kitodo.data.database.beans.Process; import org.kitodo.data.database.beans.Project; import org.kitodo.data.database.beans.Ruleset; import org.kitodo.data.database.beans.Template; +import org.kitodo.production.services.ServiceManager; public class VariableReplacerTest { @@ -179,6 +182,39 @@ public void shouldReplaceOcrdWorkflowId() { assertEquals("String was replaced incorrectly!", expected, replaced); } + @Test + public void shouldReturnMetadataOfNewspaperIssue() throws IOException { + Process process = prepareProcess(2, "variableReplacer/newspaperIssue"); + Workpiece workpiece = ServiceManager.getProcessService().readMetadataFile(process).getWorkpiece(); + VariableReplacer variableReplacer = new VariableReplacer(workpiece, process, null); + + String replaced = variableReplacer.replace("-language $(meta.DocLanguage) -scriptType $(meta.slub_script)"); + String expected = "-language ger -scriptType Antiqua"; + assertEquals("String should contain expected metadata!", expected, replaced); + } + + @Test + public void shouldReturnMetadataOfPeriodialVolume() throws IOException { + Process process = prepareProcess(2, "variableReplacer/periodicalVolume"); + Workpiece workpiece = ServiceManager.getProcessService().readMetadataFile(process).getWorkpiece(); + VariableReplacer variableReplacer = new VariableReplacer(workpiece, process, null); + + String replaced = variableReplacer.replace("-language $(meta.DocLanguage) -scriptType $(meta.slub_script)"); + String expected = "-language ger -scriptType Fraktur"; + assertEquals("String should contain expected metadata!", expected, replaced); + } + + @Test + public void shouldReturnMetadataOfMonograph() throws IOException { + Process process = prepareProcess(2, "variableReplacer/monograph"); + Workpiece workpiece = ServiceManager.getProcessService().readMetadataFile(process).getWorkpiece(); + VariableReplacer variableReplacer = new VariableReplacer(workpiece, process, null); + + String replaced = variableReplacer.replace("-language $(meta.DocLanguage) -scriptType $(meta.slub_script)"); + // missing meta data element will be replaced by emtpy string and a warning message appear in the log + String expected = "-language -scriptType keine_OCR"; + assertEquals("String should contain expected metadata!", expected, replaced); + } private Process prepareProcess(int processId, String processFolder) { Process process = new Process(); diff --git a/Kitodo/src/test/resources/metadata/variableReplacer/monograph/meta.xml b/Kitodo/src/test/resources/metadata/variableReplacer/monograph/meta.xml new file mode 100644 index 00000000000..7ee5100c9dc --- /dev/null +++ b/Kitodo/src/test/resources/metadata/variableReplacer/monograph/meta.xml @@ -0,0 +1,39 @@ + + + + 750440 + + + + + + Andreas Hammerschmids Chor-Music + Andreas Hammerschmids Chor-Music + Drucke des 17. Jahrhunderts + LDP: Sammlung Fürsten- und Landesschule Grimma + Musik + keine_OCR + Monograph + SLUB Dresden + SLUB Dresden + + + + + + + + + + + + + + + + + diff --git a/Kitodo/src/test/resources/metadata/variableReplacer/newspaperIssue/meta.xml b/Kitodo/src/test/resources/metadata/variableReplacer/newspaperIssue/meta.xml new file mode 100644 index 00000000000..114e49274c9 --- /dev/null +++ b/Kitodo/src/test/resources/metadata/variableReplacer/newspaperIssue/meta.xml @@ -0,0 +1,36 @@ + + + + 740460 + + + + + + 01-Musiktheater + PeriodicalIssue + Saxonica + 01-Musiktheater + Performance Ephemera + Musik + Antiqua + ger + SLUB Dresden + SLUB Dresden + + + + + + + + + + + + + + + + + diff --git a/Kitodo/src/test/resources/metadata/variableReplacer/periodicalVolume/meta.xml b/Kitodo/src/test/resources/metadata/variableReplacer/periodicalVolume/meta.xml new file mode 100644 index 00000000000..5440d51096e --- /dev/null +++ b/Kitodo/src/test/resources/metadata/variableReplacer/periodicalVolume/meta.xml @@ -0,0 +1,31 @@ + + + + 761701 + + + + + + ger + 1829 + Periodical + Fraktur + 1828 + SLUB Dresden + 18290000 + SLUB Dresden + Königl. Sächs. gnädigst privileg. Pirnaischer Haus- und Landwirthschafts-Calender + + + + + + + + + + + + + From d24ce9b8d4fa5f7a44407cd15246e841eb40df8b Mon Sep 17 00:00:00 2001 From: Henning Gerhardt Date: Mon, 23 Oct 2023 15:58:11 +0200 Subject: [PATCH 3/4] Add suggestion to check top element before first grandchild element. --- .../java/org/kitodo/production/helper/VariableReplacer.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Kitodo/src/main/java/org/kitodo/production/helper/VariableReplacer.java b/Kitodo/src/main/java/org/kitodo/production/helper/VariableReplacer.java index 5703d86fe0a..1360b51274f 100644 --- a/Kitodo/src/main/java/org/kitodo/production/helper/VariableReplacer.java +++ b/Kitodo/src/main/java/org/kitodo/production/helper/VariableReplacer.java @@ -397,6 +397,9 @@ private String determineReplacementForMetadata(Matcher variableFinder) { if (!allChildren.isEmpty()) { allFirstchildValue = MetadataEditor.getMetadataValue(allChildren.get(0), variableFinder.group(5)); if (Objects.isNull(allFirstchildValue)) { + allFirstchildValue = determineReplacementForTopstruct(variableFinder, dollarSignIfToKeep); + } + if (Objects.isNull(allFirstchildValue) || StringUtils.isEmpty(allFirstchildValue)) { List firstChildChildren = allChildren.get(0).getChildren(); if (!firstChildChildren.isEmpty()) { allFirstchildValue = MetadataEditor.getMetadataValue(firstChildChildren.get(0), variableFinder.group(5)); From 34af64666cd12839588e02e3a87045b0b8a292f6 Mon Sep 17 00:00:00 2001 From: Henning Gerhardt Date: Mon, 23 Oct 2023 16:12:49 +0200 Subject: [PATCH 4/4] Refactor: move big case block into method --- .../production/helper/VariableReplacer.java | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/Kitodo/src/main/java/org/kitodo/production/helper/VariableReplacer.java b/Kitodo/src/main/java/org/kitodo/production/helper/VariableReplacer.java index 1360b51274f..5a41273c9dd 100644 --- a/Kitodo/src/main/java/org/kitodo/production/helper/VariableReplacer.java +++ b/Kitodo/src/main/java/org/kitodo/production/helper/VariableReplacer.java @@ -392,20 +392,7 @@ private String determineReplacementForMetadata(Matcher variableFinder) { switch (metadataLevel) { case ALL: - List allChildren = workpiece.getLogicalStructure().getChildren(); - String allFirstchildValue = null; - if (!allChildren.isEmpty()) { - allFirstchildValue = MetadataEditor.getMetadataValue(allChildren.get(0), variableFinder.group(5)); - if (Objects.isNull(allFirstchildValue)) { - allFirstchildValue = determineReplacementForTopstruct(variableFinder, dollarSignIfToKeep); - } - if (Objects.isNull(allFirstchildValue) || StringUtils.isEmpty(allFirstchildValue)) { - List firstChildChildren = allChildren.get(0).getChildren(); - if (!firstChildChildren.isEmpty()) { - allFirstchildValue = MetadataEditor.getMetadataValue(firstChildChildren.get(0), variableFinder.group(5)); - } - } - } + String allFirstchildValue = determinateReplacementForAll(variableFinder, dollarSignIfToKeep); if (Objects.nonNull(allFirstchildValue)) { return allFirstchildValue; @@ -423,6 +410,24 @@ private String determineReplacementForMetadata(Matcher variableFinder) { } } + private String determinateReplacementForAll(Matcher variableFinder, String dollarSignIfToKeep) { + List allChildren = workpiece.getLogicalStructure().getChildren(); + String allFirstchildValue = null; + if (!allChildren.isEmpty()) { + allFirstchildValue = MetadataEditor.getMetadataValue(allChildren.get(0), variableFinder.group(5)); + if (Objects.isNull(allFirstchildValue)) { + allFirstchildValue = determineReplacementForTopstruct(variableFinder, dollarSignIfToKeep); + } + if (StringUtils.isEmpty(allFirstchildValue)) { + List firstChildChildren = allChildren.get(0).getChildren(); + if (!firstChildChildren.isEmpty()) { + allFirstchildValue = MetadataEditor.getMetadataValue(firstChildChildren.get(0), variableFinder.group(5)); + } + } + } + return allFirstchildValue; + } + private String determineReplacementForTopstruct(Matcher variableFinder, String failureResult) { String value = MetadataEditor.getMetadataValue(workpiece.getLogicalStructure(), variableFinder.group(5)); if (Objects.isNull(value)) {