Skip to content

Commit

Permalink
Merge pull request kitodo#5800 from slub/adjust_metada_replacement
Browse files Browse the repository at this point in the history
Adjust metadata replacement
  • Loading branch information
solth authored Dec 4, 2023
2 parents 457cc74 + 34af646 commit b8aec9c
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,8 @@ private String determineReplacementForMetadata(Matcher variableFinder) {

switch (metadataLevel) {
case ALL:
List<LogicalDivision> 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;
}
Expand All @@ -411,6 +410,24 @@ private String determineReplacementForMetadata(Matcher variableFinder) {
}
}

private String determinateReplacementForAll(Matcher variableFinder, String dollarSignIfToKeep) {
List<LogicalDivision> 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<LogicalDivision> 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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,26 @@
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 {

int projectId = 12;

@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";
Expand All @@ -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";
Expand All @@ -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";
Expand All @@ -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";
Expand All @@ -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";
Expand All @@ -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";
Expand All @@ -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";

Expand All @@ -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";

Expand All @@ -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";

Expand All @@ -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";

Expand All @@ -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";
Expand All @@ -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";
Expand All @@ -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);
Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<mets:mets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:kitodo="http://meta.kitodo.org/v1/" xmlns:mets="http://www.loc.gov/METS/">
<mets:metsHdr CREATEDATE="2023-09-18T09:02:55">
<mets:metsDocumentID>750440</mets:metsDocumentID>
</mets:metsHdr>
<mets:dmdSec ID="DMDLOG_0000">
<mets:mdWrap MDTYPE="OTHER" OTHERMDTYPE="KITODO">
<mets:xmlData>
<kitodo:kitodo>
<kitodo:metadata name="TitleDocMain">Andreas Hammerschmids Chor-Music</kitodo:metadata>
<kitodo:metadata name="TitleDocMainShort">Andreas Hammerschmids Chor-Music</kitodo:metadata>
<kitodo:metadata name="singleDigCollection">Drucke des 17. Jahrhunderts</kitodo:metadata>
<kitodo:metadata name="singleDigCollection">LDP: Sammlung Fürsten- und Landesschule Grimma</kitodo:metadata>
<kitodo:metadata name="singleDigCollection">Musik</kitodo:metadata>
<kitodo:metadata name="slub_script">keine_OCR</kitodo:metadata>
<kitodo:metadata name="docType">Monograph</kitodo:metadata>
<kitodo:metadata name="slub_ownerDigi">SLUB Dresden</kitodo:metadata>
<kitodo:metadata name="slub_ownerOrig">SLUB Dresden</kitodo:metadata>
</kitodo:kitodo>
</mets:xmlData>
</mets:mdWrap>
</mets:dmdSec>
<mets:fileSec>
<mets:fileGrp USE="LOCAL"/>
</mets:fileSec>
<mets:structMap TYPE="LOGICAL">
<mets:div DMDID="DMDLOG_0000"
ID="LOG_0000"
LABEL="Andreas Hammerschmids Chor-Music"
ORDERLABEL="Andreas Hammerschmids Chor-Music"
TYPE="Monograph"/>
</mets:structMap>
<mets:structMap TYPE="PHYSICAL">
<mets:div ID="PHYS_0000"/>
</mets:structMap>
<mets:structLink>
<mets:smLink xmlns:xlink="http://www.w3.org/1999/xlink" xlink:to="" xlink:from=""/>
</mets:structLink>
</mets:mets>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<mets:mets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:kitodo="http://meta.kitodo.org/v1/" xmlns:mets="http://www.loc.gov/METS/">
<mets:metsHdr CREATEDATE="2023-08-29T06:02:00.000+02:00" LASTMODDATE="2023-09-20T19:36:14.446+02:00">
<mets:metsDocumentID>740460</mets:metsDocumentID>
</mets:metsHdr>
<mets:dmdSec ID="uuid-b6cf1e58-29a9-3a16-9a91-f0998a104007">
<mets:mdWrap>
<mets:xmlData>
<kitodo:kitodo>
<kitodo:metadata name="PeriodicalIssue">01-Musiktheater</kitodo:metadata>
<kitodo:metadata name="docType">PeriodicalIssue</kitodo:metadata>
<kitodo:metadata name="singleDigCollection">Saxonica</kitodo:metadata>
<kitodo:metadata name="TitleDocMain">01-Musiktheater</kitodo:metadata>
<kitodo:metadata name="singleDigCollection">Performance Ephemera</kitodo:metadata>
<kitodo:metadata name="singleDigCollection">Musik</kitodo:metadata>
<kitodo:metadata name="slub_script">Antiqua</kitodo:metadata>
<kitodo:metadata name="DocLanguage">ger</kitodo:metadata>
<kitodo:metadata name="slub_ownerOrig">SLUB Dresden</kitodo:metadata>
<kitodo:metadata name="slub_ownerDigi">SLUB Dresden</kitodo:metadata>
</kitodo:kitodo>
</mets:xmlData>
</mets:mdWrap>
</mets:dmdSec>
<mets:fileSec/>
<mets:structMap TYPE="PHYSICAL">
<mets:div ID="PHYS_0000"/>
</mets:structMap>
<mets:structMap TYPE="LOGICAL">
<mets:div ID="LOG_0009" ORDER="1" ORDERLABEL="1855-04">
<mets:div ID="LOG_0010" ORDER="1" ORDERLABEL="1855-04-30">
<mets:div ID="LOG_0011" DMDID="uuid-b6cf1e58-29a9-3a16-9a91-f0998a104007" TYPE="PeriodicalIssue" ORDER="1"/>
</mets:div>
</mets:div>
</mets:structMap>
<mets:structLink/>
</mets:mets>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<mets:mets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:kitodo="http://meta.kitodo.org/v1/" xmlns:mets="http://www.loc.gov/METS/">
<mets:metsHdr CREATEDATE="2023-10-05T09:31:19.619+02:00" LASTMODDATE="2023-10-05T09:39:45.551+02:00">
<mets:metsDocumentID>761701</mets:metsDocumentID>
</mets:metsHdr>
<mets:dmdSec ID="uuid-0c7cc09b-db59-32d1-ae04-98d848ed2e54">
<mets:mdWrap>
<mets:xmlData>
<kitodo:kitodo>
<kitodo:metadata name="DocLanguage">ger</kitodo:metadata>
<kitodo:metadata name="CurrentNo">1829</kitodo:metadata>
<kitodo:metadata name="docType">Periodical</kitodo:metadata>
<kitodo:metadata name="slub_script">Fraktur</kitodo:metadata>
<kitodo:metadata name="PublicationYearSorting">1828</kitodo:metadata>
<kitodo:metadata name="slub_ownerDigi">SLUB Dresden</kitodo:metadata>
<kitodo:metadata name="CurrentNoSorting">18290000</kitodo:metadata>
<kitodo:metadata name="slub_ownerOrig">SLUB Dresden</kitodo:metadata>
<kitodo:metadata name="TitleDocMain">Königl. Sächs. gnädigst privileg. Pirnaischer Haus- und Landwirthschafts-Calender</kitodo:metadata>
</kitodo:kitodo>
</mets:xmlData>
</mets:mdWrap>
</mets:dmdSec>
<mets:fileSec/>
<mets:structMap TYPE="PHYSICAL">
<mets:div ID="uuid-439342a2-66b7-4545-a3fa-dcf41653e51d"/>
</mets:structMap>
<mets:structMap TYPE="LOGICAL">
<mets:div ID="uuid-95c027f4-4e75-4506-bbfd-1e48550cdfde" DMDID="uuid-0c7cc09b-db59-32d1-ae04-98d848ed2e54" TYPE="PeriodicalVolume" ORDERLABEL="Königl. Sächs. gnädigst privileg. Pirnaischer Haus- und Landwirthschafts-Calender" LABEL="Königl. Sächs. gnädigst privileg. Pirnaischer Haus- und Landwirthschafts-Calender"/>
</mets:structMap>
<mets:structLink/>
</mets:mets>

0 comments on commit b8aec9c

Please sign in to comment.