Skip to content

Commit

Permalink
Merge pull request #6083 from effective-webwork/kitodo-script-npe
Browse files Browse the repository at this point in the history
Handle exception during Kitodo Script execution
  • Loading branch information
solth authored May 23, 2024
2 parents 5e8d67b + e38a31e commit c3d5519
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* (c) Kitodo. Key to digital objects e. V. <[email protected]>
*
* This file is part of the Kitodo project.
*
* It is licensed under GNU General Public License version 3 or later.
*
* For the full copyright and license information, please read the
* GPL3-License.txt file that was distributed with this source code.
*/

package org.kitodo.exceptions;

public class KitodoScriptExecutionException extends Exception {

/**
* Constructor with given exception message.
* @param message as String
*/
public KitodoScriptExecutionException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.kitodo.api.MetadataEntry;
import org.kitodo.api.dataformat.Workpiece;
import org.kitodo.data.database.beans.Process;
import org.kitodo.exceptions.KitodoScriptExecutionException;
import org.kitodo.production.helper.metadata.legacytypeimplementations.LegacyMetsModsDigitalDocumentHelper;

public class AddDataScript extends EditDataScript {
Expand All @@ -29,7 +30,7 @@ public class AddDataScript extends EditDataScript {
* @param metadataScript the script to execute
*/
public void executeScript(LegacyMetsModsDigitalDocumentHelper metadataFile, Process process,
MetadataScript metadataScript) {
MetadataScript metadataScript) throws KitodoScriptExecutionException {
Workpiece workpiece = metadataFile.getWorkpiece();
Collection<Metadata> metadataCollection = getMetadataCollection(metadataScript, workpiece);
generateValueForMetadataScript(metadataScript, metadataCollection, process, metadataFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.kitodo.api.MetadataEntry;
import org.kitodo.api.dataformat.Workpiece;
import org.kitodo.data.database.beans.Process;
import org.kitodo.exceptions.KitodoScriptExecutionException;
import org.kitodo.production.helper.metadata.legacytypeimplementations.LegacyMetsModsDigitalDocumentHelper;

public class DeleteDataScript extends EditDataScript {
Expand All @@ -30,7 +31,7 @@ public class DeleteDataScript extends EditDataScript {
* @param metadataScript the script to execute
*/
public void executeScript(LegacyMetsModsDigitalDocumentHelper metadataFile, Process process,
MetadataScript metadataScript) {
MetadataScript metadataScript) throws KitodoScriptExecutionException {
Workpiece workpiece = metadataFile.getWorkpiece();

Collection<Metadata> metadataCollection = getMetadataCollection(metadataScript, workpiece);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import org.kitodo.data.database.beans.Process;
import org.kitodo.data.elasticsearch.exceptions.CustomResponseException;
import org.kitodo.data.exceptions.DataException;
import org.kitodo.exceptions.KitodoScriptExecutionException;
import org.kitodo.production.helper.Helper;
import org.kitodo.production.helper.VariableReplacer;
import org.kitodo.production.helper.metadata.legacytypeimplementations.LegacyMetsModsDigitalDocumentHelper;
import org.kitodo.production.services.ServiceManager;
Expand All @@ -42,7 +44,8 @@ public abstract class EditDataScript {
* @param process - the process to run the script on
* @param script - the script to run
*/
public void process(LegacyMetsModsDigitalDocumentHelper metadataFile, Process process, String script) {
public void process(LegacyMetsModsDigitalDocumentHelper metadataFile, Process process, String script)
throws KitodoScriptExecutionException {
List<MetadataScript> scripts = parseScript(script);
for (MetadataScript metadataScript : scripts) {
executeScript(metadataFile, process, metadataScript);
Expand All @@ -56,7 +59,7 @@ public void process(LegacyMetsModsDigitalDocumentHelper metadataFile, Process pr
* @param metadataScript the script to execute
*/
public abstract void executeScript(LegacyMetsModsDigitalDocumentHelper metadataFile, Process process,
MetadataScript metadataScript);
MetadataScript metadataScript) throws KitodoScriptExecutionException;

/**
* Parses the given input to MetadataScripts.
Expand Down Expand Up @@ -134,13 +137,20 @@ public void generateValueFromParent(MetadataScript metadataScript, Process paren
* @param workpiece the workpiece to get the collection from.
* @return the metadataCollection.
*/
public Collection<Metadata> getMetadataCollection(MetadataScript metadataScript, Workpiece workpiece) {
public Collection<Metadata> getMetadataCollection(MetadataScript metadataScript, Workpiece workpiece)
throws KitodoScriptExecutionException {
Collection<Metadata> metadataCollection;

if (Objects.nonNull(metadataScript.getTypeTarget())) {
LogicalDivision structuralElement = getLogicalDivisionWithType(metadataScript.getTypeTarget(),
workpiece.getLogicalStructure());
metadataCollection = Objects.isNull(structuralElement) ? null : structuralElement.getMetadata();
if (Objects.nonNull(structuralElement)) {
metadataCollection = structuralElement.getMetadata();
} else {
throw new KitodoScriptExecutionException(
Helper.getTranslation("kitodoScript.noStructureOfTypeFound",
metadataScript.getTypeTarget()));
}
} else {
metadataCollection = workpiece.getLogicalStructure().getMetadata();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.kitodo.data.exceptions.DataException;
import org.kitodo.exceptions.CommandException;
import org.kitodo.exceptions.InvalidImagesException;
import org.kitodo.exceptions.KitodoScriptExecutionException;
import org.kitodo.exceptions.MediaNotFoundException;
import org.kitodo.export.ExportDms;
import org.kitodo.production.enums.GenerationMode;
Expand Down Expand Up @@ -253,31 +254,31 @@ private boolean executeRemainingScript(List<Process> processes, String script)

private void deleteData(List<Process> processes, String script) {
String currentProcessTitle = null;
try {
script = script.replaceFirst("\\s*action:deleteData\\s+(.*?)[\r\n\\s]*", "$1");
DeleteDataScript deleteDataScript = new DeleteDataScript();
for (Process process : processes) {
script = script.replaceFirst("\\s*action:deleteData\\s+(.*?)[\r\n\\s]*", "$1");
DeleteDataScript deleteDataScript = new DeleteDataScript();
for (Process process : processes) {
try {
currentProcessTitle = process.getTitle();
LegacyMetsModsDigitalDocumentHelper metadataFile = ServiceManager.getProcessService()
.readMetadataFile(process);
deleteDataScript.process(metadataFile, process, script);
ServiceManager.getMetsService().saveWorkpiece(metadataFile.getWorkpiece(),
ServiceManager.getProcessService().getMetadataFileUri(process));
Helper.setMessage("deleteDataOk", currentProcessTitle);
} catch (IOException | KitodoScriptExecutionException e) {
Helper.setErrorMessage("deleteDataError", currentProcessTitle + ": " + e.getMessage(), logger, e);
}
} catch (IOException e) {
Helper.setErrorMessage("deleteDataError", currentProcessTitle + ":" + e.getMessage(), logger, e);
}
}

private void copyDataToChildren(List<Process> processes, String script) {
String currentProcessTitle = null;
try {
script = script.replaceFirst("\\s*action:copyDataToChildren\\s+(.*?)[\r\n\\s]*", "$1");
AddDataScript addDataScript = new AddDataScript();
for (Process parentProcess : processes) {
currentProcessTitle = parentProcess.getTitle();
List<MetadataScript> metadataScripts = addDataScript.parseScript(script);
String currentProcessTitle;
script = script.replaceFirst("\\s*action:copyDataToChildren\\s+(.*?)[\r\n\\s]*", "$1");
AddDataScript addDataScript = new AddDataScript();
for (Process parentProcess : processes) {
currentProcessTitle = parentProcess.getTitle();
List<MetadataScript> metadataScripts = addDataScript.parseScript(script);
try {
generateScriptValues(addDataScript, metadataScripts, parentProcess);
for (Process child : parentProcess.getChildren()) {
LegacyMetsModsDigitalDocumentHelper childMetadataFile = ServiceManager.getProcessService()
Expand All @@ -287,9 +288,9 @@ private void copyDataToChildren(List<Process> processes, String script) {
}
}
Helper.setMessage("addDataOk", currentProcessTitle);
} catch (IOException | KitodoScriptExecutionException e) {
Helper.setErrorMessage("addDataError", currentProcessTitle + ": " + e.getMessage(), logger, e);
}
} catch (IOException e) {
Helper.setErrorMessage("addDataOk", currentProcessTitle + ":" + e.getMessage(), logger, e);
}
}

Expand All @@ -302,20 +303,20 @@ private void generateScriptValues(AddDataScript addDataScript, List<MetadataScri

private void overwriteData(List<Process> processes, String script) {
String currentProcessTitle = null;
try {
script = script.replaceFirst("\\s*action:overwriteData\\s+(.*?)[\r\n\\s]*", "$1");
OverwriteDataScript overwriteDataScript = new OverwriteDataScript();
for (Process process : processes) {
script = script.replaceFirst("\\s*action:overwriteData\\s+(.*?)[\r\n\\s]*", "$1");
OverwriteDataScript overwriteDataScript = new OverwriteDataScript();
for (Process process : processes) {
try {
currentProcessTitle = process.getTitle();
LegacyMetsModsDigitalDocumentHelper metadataFile = ServiceManager.getProcessService()
.readMetadataFile(process);
overwriteDataScript.process(metadataFile, process, script);
ServiceManager.getMetsService().saveWorkpiece(metadataFile.getWorkpiece(),
ServiceManager.getProcessService().getMetadataFileUri(process));
Helper.setMessage("overwriteDataOk", currentProcessTitle);
} catch (IOException | KitodoScriptExecutionException e) {
Helper.setErrorMessage("overwriteDataError", currentProcessTitle + ": " + e.getMessage(), logger, e);
}
} catch (IOException e) {
Helper.setErrorMessage("overwriteDataError", currentProcessTitle + ":" + e.getMessage(), logger, e);
}
}

Expand Down Expand Up @@ -373,20 +374,20 @@ private void deleteProcess(List<Process> processes, boolean contentOnly) {

private void addData(List<Process> processes, String script) {
String currentProcessTitle = null;
try {
script = script.replaceFirst("\\s*action:addData\\s+(.*?)[\r\n\\s]*", "$1");
AddDataScript addDataScript = new AddDataScript();
for (Process process : processes) {
script = script.replaceFirst("\\s*action:addData\\s+(.*?)[\r\n\\s]*", "$1");
AddDataScript addDataScript = new AddDataScript();
for (Process process : processes) {
try {
currentProcessTitle = process.getTitle();
LegacyMetsModsDigitalDocumentHelper metadataFile = ServiceManager.getProcessService()
.readMetadataFile(process);
addDataScript.process(metadataFile, process, script);
ServiceManager.getMetsService().saveWorkpiece(metadataFile.getWorkpiece(),
ServiceManager.getProcessService().getMetadataFileUri(process));
Helper.setMessage("addDataOk", currentProcessTitle);
} catch (IOException | KitodoScriptExecutionException e) {
Helper.setErrorMessage("addDataError", currentProcessTitle + ": " + e.getMessage(), logger, e);
}
} catch (IOException e) {
Helper.setErrorMessage("addDataError", currentProcessTitle + ":" + e.getMessage(), logger, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
import org.kitodo.api.MetadataGroup;
import org.kitodo.api.dataformat.Workpiece;
import org.kitodo.data.database.beans.Process;
import org.kitodo.exceptions.KitodoScriptExecutionException;
import org.kitodo.production.helper.metadata.legacytypeimplementations.LegacyMetsModsDigitalDocumentHelper;

public class OverwriteDataScript extends EditDataScript {

@Override
public void executeScript(LegacyMetsModsDigitalDocumentHelper metadataFile, Process process,
MetadataScript metadataScript) {
MetadataScript metadataScript) throws KitodoScriptExecutionException {
Workpiece workpiece = metadataFile.getWorkpiece();

Collection<Metadata> metadataCollection = getMetadataCollection(metadataScript, workpiece);
Expand Down
1 change: 1 addition & 0 deletions Kitodo/src/main/resources/messages/errors_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ kitodoScript.importProcesses.project.noProjectWithID=Ein solches Projekt existie
kitodoScript.importProcesses.template.isNull=Parameter ''template'' fehlt!
kitodoScript.importProcesses.template.isNoTemplateID=''template'' ist keine g\u00F6ltige Vorlagen-ID
kitodoScript.importProcesses.template.noTemplateWithID=Eine solche Produktionsvorlage existiert nicht!
kitodoScript.noStructureOfTypeFound=Kein Strukturelement vom Typ "{0}" gefunden

# L
errorLoadingDocTypes=Regelsatz Konfigurationsfehler: Kein DocType ('division') gefunden
Expand Down
1 change: 1 addition & 0 deletions Kitodo/src/main/resources/messages/errors_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ kitodoScript.importProcesses.project.noProjectWithID=There is no project with th
kitodoScript.importProcesses.template.isNull=Missing value for ''template''!
kitodoScript.importProcesses.template.isNoTemplateID=''template'' is not int!
kitodoScript.importProcesses.template.noTemplateWithID=There is no production template with that ID
kitodoScript.noStructureOfTypeFound=No structure element with type "{0}" found

# L
errorLoadingDocTypes=Ruleset configuration error: No DocType ('division') found
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1734,6 +1734,13 @@ Popup dialogs
color: var(--pure-white);
}

.ui-messages-error.ui-corner-all ul,
.ui-messages-warn.ui-corner-all ul,
.ui-messages-info.ui-corner-all ul {
max-height: 80px;
overflow-y: auto;
}

.ui-messages-error.ui-corner-all span.ui-icon-close,
.ui-messages-warn.ui-corner-all span.ui-icon-close,
.ui-messages-info.ui-corner-all span.ui-icon-close {
Expand Down

0 comments on commit c3d5519

Please sign in to comment.