From 2636e121a43d89caa0cdded7e52c779c2a725192 Mon Sep 17 00:00:00 2001 From: Henning Gerhardt Date: Thu, 5 Oct 2023 16:44:17 +0200 Subject: [PATCH] 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 + + + + + + + + + + + + +