From 34af64666cd12839588e02e3a87045b0b8a292f6 Mon Sep 17 00:00:00 2001 From: Henning Gerhardt Date: Mon, 23 Oct 2023 16:12:49 +0200 Subject: [PATCH] Refactor: move big case block into method --- .../production/helper/VariableReplacer.java | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) 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 1360b51274f..5a41273c9dd 100644 --- a/Kitodo/src/main/java/org/kitodo/production/helper/VariableReplacer.java +++ b/Kitodo/src/main/java/org/kitodo/production/helper/VariableReplacer.java @@ -392,20 +392,7 @@ private String determineReplacementForMetadata(Matcher variableFinder) { switch (metadataLevel) { case ALL: - List 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 (Objects.isNull(allFirstchildValue) || StringUtils.isEmpty(allFirstchildValue)) { - List firstChildChildren = allChildren.get(0).getChildren(); - if (!firstChildChildren.isEmpty()) { - allFirstchildValue = MetadataEditor.getMetadataValue(firstChildChildren.get(0), variableFinder.group(5)); - } - } - } + String allFirstchildValue = determinateReplacementForAll(variableFinder, dollarSignIfToKeep); if (Objects.nonNull(allFirstchildValue)) { return allFirstchildValue; @@ -423,6 +410,24 @@ private String determineReplacementForMetadata(Matcher variableFinder) { } } + private String determinateReplacementForAll(Matcher variableFinder, String dollarSignIfToKeep) { + List 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 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)) {