From e38a31ea00ac70a69951f6df61b12db2404905c9 Mon Sep 17 00:00:00 2001 From: Arved Solth Date: Thu, 16 May 2024 14:31:11 +0200 Subject: [PATCH] Handle exception during Kitodo Script execution --- .../KitodoScriptExecutionException.java | 23 ++++++++ .../services/command/AddDataScript.java | 3 +- .../services/command/DeleteDataScript.java | 3 +- .../services/command/EditDataScript.java | 18 ++++-- .../services/command/KitodoScriptService.java | 55 ++++++++++--------- .../services/command/OverwriteDataScript.java | 3 +- .../resources/messages/errors_de.properties | 1 + .../resources/messages/errors_en.properties | 1 + .../WEB-INF/resources/css/pattern-library.css | 7 +++ 9 files changed, 80 insertions(+), 34 deletions(-) create mode 100644 Kitodo/src/main/java/org/kitodo/exceptions/KitodoScriptExecutionException.java diff --git a/Kitodo/src/main/java/org/kitodo/exceptions/KitodoScriptExecutionException.java b/Kitodo/src/main/java/org/kitodo/exceptions/KitodoScriptExecutionException.java new file mode 100644 index 00000000000..0c8b538fc70 --- /dev/null +++ b/Kitodo/src/main/java/org/kitodo/exceptions/KitodoScriptExecutionException.java @@ -0,0 +1,23 @@ +/* + * (c) Kitodo. Key to digital objects e. V. + * + * 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); + } +} diff --git a/Kitodo/src/main/java/org/kitodo/production/services/command/AddDataScript.java b/Kitodo/src/main/java/org/kitodo/production/services/command/AddDataScript.java index 0181e74957d..476c2287af4 100644 --- a/Kitodo/src/main/java/org/kitodo/production/services/command/AddDataScript.java +++ b/Kitodo/src/main/java/org/kitodo/production/services/command/AddDataScript.java @@ -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 { @@ -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 metadataCollection = getMetadataCollection(metadataScript, workpiece); generateValueForMetadataScript(metadataScript, metadataCollection, process, metadataFile); diff --git a/Kitodo/src/main/java/org/kitodo/production/services/command/DeleteDataScript.java b/Kitodo/src/main/java/org/kitodo/production/services/command/DeleteDataScript.java index 9b2a871bb8d..3da7dabe935 100644 --- a/Kitodo/src/main/java/org/kitodo/production/services/command/DeleteDataScript.java +++ b/Kitodo/src/main/java/org/kitodo/production/services/command/DeleteDataScript.java @@ -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 { @@ -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 metadataCollection = getMetadataCollection(metadataScript, workpiece); diff --git a/Kitodo/src/main/java/org/kitodo/production/services/command/EditDataScript.java b/Kitodo/src/main/java/org/kitodo/production/services/command/EditDataScript.java index 9207ebe4d2f..50718d10dae 100644 --- a/Kitodo/src/main/java/org/kitodo/production/services/command/EditDataScript.java +++ b/Kitodo/src/main/java/org/kitodo/production/services/command/EditDataScript.java @@ -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; @@ -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 scripts = parseScript(script); for (MetadataScript metadataScript : scripts) { executeScript(metadataFile, process, metadataScript); @@ -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. @@ -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 getMetadataCollection(MetadataScript metadataScript, Workpiece workpiece) { + public Collection getMetadataCollection(MetadataScript metadataScript, Workpiece workpiece) + throws KitodoScriptExecutionException { Collection 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(); } diff --git a/Kitodo/src/main/java/org/kitodo/production/services/command/KitodoScriptService.java b/Kitodo/src/main/java/org/kitodo/production/services/command/KitodoScriptService.java index 368a7bec02f..1d7a4e59d61 100644 --- a/Kitodo/src/main/java/org/kitodo/production/services/command/KitodoScriptService.java +++ b/Kitodo/src/main/java/org/kitodo/production/services/command/KitodoScriptService.java @@ -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; @@ -253,10 +254,10 @@ private boolean executeRemainingScript(List processes, String script) private void deleteData(List 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); @@ -264,20 +265,20 @@ private void deleteData(List processes, String 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 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 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 metadataScripts = addDataScript.parseScript(script); + try { generateScriptValues(addDataScript, metadataScripts, parentProcess); for (Process child : parentProcess.getChildren()) { LegacyMetsModsDigitalDocumentHelper childMetadataFile = ServiceManager.getProcessService() @@ -287,9 +288,9 @@ private void copyDataToChildren(List 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); } } @@ -302,10 +303,10 @@ private void generateScriptValues(AddDataScript addDataScript, List 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); @@ -313,9 +314,9 @@ private void overwriteData(List processes, String 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); } } @@ -373,10 +374,10 @@ private void deleteProcess(List processes, boolean contentOnly) { private void addData(List 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); @@ -384,9 +385,9 @@ private void addData(List processes, String 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); } } diff --git a/Kitodo/src/main/java/org/kitodo/production/services/command/OverwriteDataScript.java b/Kitodo/src/main/java/org/kitodo/production/services/command/OverwriteDataScript.java index 756c1276333..ef4e2d11665 100644 --- a/Kitodo/src/main/java/org/kitodo/production/services/command/OverwriteDataScript.java +++ b/Kitodo/src/main/java/org/kitodo/production/services/command/OverwriteDataScript.java @@ -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 metadataCollection = getMetadataCollection(metadataScript, workpiece); diff --git a/Kitodo/src/main/resources/messages/errors_de.properties b/Kitodo/src/main/resources/messages/errors_de.properties index 7f798275246..5204a55a81b 100644 --- a/Kitodo/src/main/resources/messages/errors_de.properties +++ b/Kitodo/src/main/resources/messages/errors_de.properties @@ -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 diff --git a/Kitodo/src/main/resources/messages/errors_en.properties b/Kitodo/src/main/resources/messages/errors_en.properties index 2929d7d7366..1f56eeebc3d 100644 --- a/Kitodo/src/main/resources/messages/errors_en.properties +++ b/Kitodo/src/main/resources/messages/errors_en.properties @@ -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 diff --git a/Kitodo/src/main/webapp/WEB-INF/resources/css/pattern-library.css b/Kitodo/src/main/webapp/WEB-INF/resources/css/pattern-library.css index 24a76ab9ba7..9f51d761f36 100644 --- a/Kitodo/src/main/webapp/WEB-INF/resources/css/pattern-library.css +++ b/Kitodo/src/main/webapp/WEB-INF/resources/css/pattern-library.css @@ -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 {