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..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,9 +392,8 @@ 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 = determinateReplacementForAll(variableFinder, dollarSignIfToKeep); + if (Objects.nonNull(allFirstchildValue)) { return allFirstchildValue; } @@ -411,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)) { 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..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 { @@ -31,7 +34,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 +44,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 +54,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 +64,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 +74,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 +87,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 +101,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 +113,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 +126,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 +135,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 +145,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 +155,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 +165,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); @@ -179,16 +182,49 @@ 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() { + 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"); 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 + + + + + + + + + + + + +