Skip to content

Commit

Permalink
integrate variable replacer
Browse files Browse the repository at this point in the history
  • Loading branch information
Kathrin-Huber committed Dec 1, 2020
1 parent f681b63 commit 403fef5
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void executeScript(LegacyMetsModsDigitalDocumentHelper metadataFile, Proc
}

if (Objects.isNull(metadataScript.getValue())) {
generateValueForMetadataScript(metadataScript, metadataCollection);
generateValueForMetadataScript(metadataScript, metadataCollection, process, metadataFile);
}

MetadataEntry metadataEntry = new MetadataEntry();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void executeScript(LegacyMetsModsDigitalDocumentHelper metadataFile, Proc
Collection<Metadata> metadataCollection = child.getMetadata();

if (Objects.isNull(metadataScript.getValue())) {
generateValueForMetadataScript(metadataScript, metadataCollection);
generateValueForMetadataScript(metadataScript, metadataCollection, process, metadataFile);
}

List<Metadata> metadataCollectionCopy = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
import org.kitodo.data.database.beans.Process;
import org.kitodo.data.elasticsearch.exceptions.CustomResponseException;
import org.kitodo.data.exceptions.DataException;
import org.kitodo.production.helper.VariableReplacer;
import org.kitodo.production.helper.metadata.legacytypeimplementations.LegacyMetsModsDigitalDocumentHelper;
import org.kitodo.production.helper.metadata.legacytypeimplementations.LegacyPrefsHelper;
import org.kitodo.production.services.ServiceManager;

public abstract class EditDataScript {
Expand Down Expand Up @@ -67,12 +69,23 @@ private List<MetadataScript> parseScript(String script) {
* Generates the script value when a metadata root is given.
* @param metadataScript the script to set the value
* @param metadataCollection the metadata collection to extract the value from
* @param process the process to replace variables
* @param metadataFile the metadatafile to read metadata
*/
public void generateValueForMetadataScript(MetadataScript metadataScript, Collection<Metadata> metadataCollection) {
for (Metadata metadata : metadataCollection) {
if (metadata.getKey().equals(metadataScript.getRootName())) {
metadataScript.setValue(((MetadataEntry) metadata).getValue());
public void generateValueForMetadataScript(MetadataScript metadataScript, Collection<Metadata> metadataCollection, Process process, LegacyMetsModsDigitalDocumentHelper metadataFile) {
if (metadataScript.getRoot().startsWith("@")) {
for (Metadata metadata : metadataCollection) {
if (metadata.getKey().equals(metadataScript.getRootName())) {
metadataScript.setValue(((MetadataEntry) metadata).getValue());
}
}
} else if (metadataScript.getRoot().startsWith("$")) {
LegacyPrefsHelper legacyPrefsHelper = ServiceManager.getRulesetService().getPreferences(process.getRuleset());
VariableReplacer replacer = new VariableReplacer(metadataFile, legacyPrefsHelper,
process, null);

String replaced = replacer.replace(metadataScript.getRootName());
metadataScript.setValue(replaced);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public MetadataScript(String command) {
String[] commandParts = command.split("=");
goal = commandParts[0];
String rootOrValue = commandParts[1];
if (rootOrValue.startsWith("@")) {
if (rootOrValue.startsWith("@") || rootOrValue.startsWith("$")) {
root = rootOrValue;
} else {
value = rootOrValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void executeScript(LegacyMetsModsDigitalDocumentHelper metadataFile, Proc
IncludedStructuralElement child = allIncludedStructuralElements.get(0);
Collection<Metadata> metadata = child.getMetadata();
if (Objects.isNull(metadataScript.getValue())) {
generateValueForMetadataScript(metadataScript, metadata);
generateValueForMetadataScript(metadataScript, metadata, process, metadataFile);
}
for (Metadata metadatum : metadata) {
if (metadatum instanceof MetadataEntry) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,27 @@ public void shouldCopyDataWithRoot() throws Exception {
Assert.assertEquals("does not contain metadata", 1, processByMetadata.size() );
}

@Test
public void shouldAddDataWithVariable() throws Exception {
Process process = ServiceManager.getProcessService().getById(2);
String metadataKey = "LegalNoteAndTermsOfUse";
HashMap<String, String> metadataSearchMap = new HashMap<>();
metadataSearchMap.put(metadataKey, "2");

List<ProcessDTO> processByMetadata = ServiceManager.getProcessService().findByMetadata(metadataSearchMap);
Assert.assertEquals("does not contain metadata", 0, processByMetadata.size() );

String script = "action:addData " + metadataKey + "=$(processid)";
List<Process> processes = new ArrayList<>();
processes.add(process);
KitodoScriptService kitodoScript = new KitodoScriptService();
kitodoScript.execute(processes, script);

Thread.sleep(2000);
processByMetadata = ServiceManager.getProcessService().findByMetadata(metadataSearchMap);
Assert.assertEquals("does not contain metadata", 1, processByMetadata.size() );
}

@Test
public void shouldDeleteData() throws Exception {
Process process = ServiceManager.getProcessService().getById(2);
Expand Down Expand Up @@ -427,4 +448,35 @@ public void shouldOverwriteDataWithRoot() throws Exception {
Assert.assertEquals("should contain new metadata value", 1, processByMetadata.size());

}

@Test
public void shouldOverwriteDataWithVariable() throws Exception {
Process process = ServiceManager.getProcessService().getById(2);
String metadataKey = "TitleDocMainShort";
HashMap<String, String> oldMetadataSearchMap = new HashMap<>();
oldMetadataSearchMap.put(metadataKey, "SecondMetaShort");

HashMap<String, String> newMetadataSearchMap = new HashMap<>();
newMetadataSearchMap.put(metadataKey, "2");

List<ProcessDTO> processByMetadata = ServiceManager.getProcessService().findByMetadata(oldMetadataSearchMap);
Assert.assertEquals("should contain metadata", 1, processByMetadata.size() );

processByMetadata = ServiceManager.getProcessService().findByMetadata(newMetadataSearchMap);
Assert.assertEquals("should contain new metadata value", 0, processByMetadata.size());

String script = "action:overwriteData " + metadataKey + "=$(processid)";
List<Process> processes = new ArrayList<>();
processes.add(process);
KitodoScriptService kitodoScript = new KitodoScriptService();
kitodoScript.execute(processes, script);

Thread.sleep(2000);
processByMetadata = ServiceManager.getProcessService().findByMetadata(oldMetadataSearchMap);
Assert.assertEquals("should not contain metadata anymore", 0, processByMetadata.size());

processByMetadata = ServiceManager.getProcessService().findByMetadata(newMetadataSearchMap);
Assert.assertEquals("should contain new metadata value", 1, processByMetadata.size());

}
}

0 comments on commit 403fef5

Please sign in to comment.