From fa2b9438a941d5d4c5841fb0bbad05c458aeb820 Mon Sep 17 00:00:00 2001 From: BartChris Date: Wed, 22 Jan 2025 12:35:24 +0100 Subject: [PATCH] refactor_preserve_logic --- .../createprocess/ProcessBooleanMetadata.java | 23 ++------ .../forms/createprocess/ProcessDetail.java | 18 ------ .../createprocess/ProcessFieldedMetadata.java | 57 +++++++++++++++---- .../createprocess/ProcessSelectMetadata.java | 17 +----- .../createprocess/ProcessSimpleMetadata.java | 25 +++----- .../createprocess/ProcessTextMetadata.java | 24 ++------ 6 files changed, 66 insertions(+), 98 deletions(-) diff --git a/Kitodo/src/main/java/org/kitodo/production/forms/createprocess/ProcessBooleanMetadata.java b/Kitodo/src/main/java/org/kitodo/production/forms/createprocess/ProcessBooleanMetadata.java index b006b43764f..342aa095643 100644 --- a/Kitodo/src/main/java/org/kitodo/production/forms/createprocess/ProcessBooleanMetadata.java +++ b/Kitodo/src/main/java/org/kitodo/production/forms/createprocess/ProcessBooleanMetadata.java @@ -16,17 +16,13 @@ import java.util.Collections; import java.util.Objects; import java.util.Optional; -import java.util.function.BiConsumer; import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.tuple.Pair; import org.kitodo.api.Metadata; import org.kitodo.api.MetadataEntry; import org.kitodo.api.dataeditor.rulesetmanagement.Domain; import org.kitodo.api.dataeditor.rulesetmanagement.SimpleMetadataViewInterface; -import org.kitodo.api.dataformat.Division; import org.kitodo.exceptions.InvalidMetadataValueException; -import org.kitodo.exceptions.NoSuchMetadataFieldException; /** * A row on the metadata panel that contains an on/off switch. @@ -70,6 +66,11 @@ public String getMetadataID() { return settings.getId(); } + @Override + public String extractSimpleValue() { + return settings.convertBoolean(active).orElse(null); + } + @Override public String getInput() { return "toggleSwitch"; @@ -96,20 +97,6 @@ public Collection getMetadata(boolean skipEmpty) throws InvalidMetadat return Collections.emptyList(); } - @Override - Pair, String>, String> getStructureFieldValue() - throws InvalidMetadataValueException, NoSuchMetadataFieldException { - - if (settings.getDomain().orElse(Domain.DESCRIPTION).equals(Domain.METS_DIV)) { - if (!isValid()) { - throw new InvalidMetadataValueException(label, settings.convertBoolean(active).orElse("")); - } - return Pair.of(super.getStructureFieldSetters(settings), settings.convertBoolean(active).orElse(null)); - } else { - return null; - } - } - /** * Returns whether the switch is on. * diff --git a/Kitodo/src/main/java/org/kitodo/production/forms/createprocess/ProcessDetail.java b/Kitodo/src/main/java/org/kitodo/production/forms/createprocess/ProcessDetail.java index a1d64bf6859..7af5eb31972 100644 --- a/Kitodo/src/main/java/org/kitodo/production/forms/createprocess/ProcessDetail.java +++ b/Kitodo/src/main/java/org/kitodo/production/forms/createprocess/ProcessDetail.java @@ -15,13 +15,10 @@ import java.io.Serializable; import java.util.Collection; import java.util.EnumMap; -import java.util.function.BiConsumer; -import org.apache.commons.lang3.tuple.Pair; import org.kitodo.api.MdSec; import org.kitodo.api.Metadata; import org.kitodo.api.dataeditor.rulesetmanagement.Domain; -import org.kitodo.api.dataformat.Division; import org.kitodo.api.dataformat.LogicalDivision; import org.kitodo.exceptions.InvalidMetadataValueException; import org.kitodo.exceptions.NoSuchMetadataFieldException; @@ -123,21 +120,6 @@ public String getLabel() { */ public abstract Collection getMetadata(boolean skipEmpty) throws InvalidMetadataValueException; - /** - * If the metadata entry addresses a property of the structure, returns a - * pair of the setter and the value to set; else {@code null}. This method - * it to be called when saving the data. - * - * @return if data is to be written a pair of the setter of the - * {@link LogicalDivision} and the value to set, else null - * @throws InvalidMetadataValueException - * if the metadata form contains syntactically wrong input - * @throws NoSuchMetadataFieldException - * if the field configured in the rule set does not exist - */ - abstract Pair, String>, String> getStructureFieldValue() - throws InvalidMetadataValueException, NoSuchMetadataFieldException; - /** * Returns whether this metadata entry is leading for options of other * metadata entries. If true, the application must refresh the metadata diff --git a/Kitodo/src/main/java/org/kitodo/production/forms/createprocess/ProcessFieldedMetadata.java b/Kitodo/src/main/java/org/kitodo/production/forms/createprocess/ProcessFieldedMetadata.java index 4007c9b379f..497cafc5037 100644 --- a/Kitodo/src/main/java/org/kitodo/production/forms/createprocess/ProcessFieldedMetadata.java +++ b/Kitodo/src/main/java/org/kitodo/production/forms/createprocess/ProcessFieldedMetadata.java @@ -24,11 +24,10 @@ import java.util.Map.Entry; import java.util.Objects; import java.util.Optional; -import java.util.function.BiConsumer; +import java.util.Set; import java.util.stream.Collectors; import org.apache.commons.collections4.list.UnmodifiableList; -import org.apache.commons.lang3.tuple.Pair; import org.apache.logging.log4j.Level; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -60,11 +59,14 @@ public class ProcessFieldedMetadata extends ProcessDetail implements Serializabl public static final ProcessFieldedMetadata EMPTY = new ProcessFieldedMetadata(); public static final String METADATA_KEY_LABEL = "LABEL"; public static final String METADATA_KEY_ORDERLABEL = "ORDERLABEL"; + public static final String METADATA_KEY_CONTENTIDS = "CONTENTIDS"; /** * Fields the user has selected to show in addition, with no data yet. */ private final Collection additionallySelectedFields = new ArrayList<>(); + private static final Set specialFields = Set.of(METADATA_KEY_LABEL, METADATA_KEY_ORDERLABEL, + METADATA_KEY_CONTENTIDS); private boolean copy; @@ -612,11 +614,6 @@ public List getRows() { return new UnmodifiableList<>(rows); } - @Override - Pair, String>, String> getStructureFieldValue() { - return null; - } - public TreeNode getTreeNode() { return treeNode; } @@ -677,16 +674,27 @@ public void preserve() throws InvalidMetadataValueException, NoSuchMetadataField division.setLabel(null); } metadata.clear(); + for (TreeNode child : treeNode.getChildren()) { ProcessDetail row = (ProcessDetail) child.getData(); - Pair, String>, String> metsFieldValue = row.getStructureFieldValue(); - if (Objects.nonNull(metsFieldValue)) { - metsFieldValue.getKey().accept(division, metsFieldValue.getValue()); + String id = row.getMetadataID(); + if (row instanceof ProcessSimpleMetadata && specialFields.contains(id) + && ((ProcessSimpleMetadata) row).getSettings() + .getDomain().orElse(Domain.DESCRIPTION).equals(Domain.METS_DIV)) { + updateDivisionFromProcessDetail(id, (ProcessSimpleMetadata) row); } else { metadata.addAll(row.getMetadataWithFilledValues()); } } - if (Objects.nonNull(hiddenMetadata)) { + if (Objects.nonNull(hiddenMetadata) && !hiddenMetadata.isEmpty()) { + for (Metadata hidden : hiddenMetadata) { + if (hidden instanceof MetadataEntry) { + MetadataEntry entry = (MetadataEntry) hidden; + if (specialFields.contains(entry.getKey())) { + updateDivision(entry.getKey(), entry.getValue()); + } + } + } metadata.addAll(hiddenMetadata); } } catch (InvalidMetadataValueException invalidValueException) { @@ -706,6 +714,33 @@ public void preserve() throws InvalidMetadataValueException, NoSuchMetadataField } } + private void updateDivisionFromProcessDetail(String key, ProcessSimpleMetadata processDetail) throws InvalidMetadataValueException { + String simpleValue = processDetail.extractSimpleValue(); + if (!processDetail.getSettings().isValid(simpleValue, getListForLeadingMetadataFields())) { + throw new InvalidMetadataValueException(key, simpleValue); + }; + if (simpleValue == null) { + return; + } + updateDivision(key, simpleValue); + } + + private void updateDivision(String key, String value) { + switch (key) { + case METADATA_KEY_LABEL: + division.setLabel(value); + break; + case METADATA_KEY_ORDERLABEL: + division.setOrderlabel(value); + break; + case METADATA_KEY_CONTENTIDS: + division.getContentIds().add(URI.create(value)); + break; + default: + break; + } + } + /** * Removes a process detail. * diff --git a/Kitodo/src/main/java/org/kitodo/production/forms/createprocess/ProcessSelectMetadata.java b/Kitodo/src/main/java/org/kitodo/production/forms/createprocess/ProcessSelectMetadata.java index 1f29530712c..bc326244824 100644 --- a/Kitodo/src/main/java/org/kitodo/production/forms/createprocess/ProcessSelectMetadata.java +++ b/Kitodo/src/main/java/org/kitodo/production/forms/createprocess/ProcessSelectMetadata.java @@ -20,11 +20,9 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Objects; -import java.util.function.BiConsumer; import javax.faces.model.SelectItem; -import org.apache.commons.lang3.tuple.Pair; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.kitodo.api.MdSec; @@ -32,9 +30,7 @@ import org.kitodo.api.MetadataEntry; import org.kitodo.api.dataeditor.rulesetmanagement.Domain; import org.kitodo.api.dataeditor.rulesetmanagement.SimpleMetadataViewInterface; -import org.kitodo.api.dataformat.Division; import org.kitodo.exceptions.InvalidMetadataValueException; -import org.kitodo.exceptions.NoSuchMetadataFieldException; public class ProcessSelectMetadata extends ProcessSimpleMetadata implements Serializable { private static final Logger logger = LogManager.getLogger(ProcessSelectMetadata.class); @@ -192,17 +188,8 @@ public List getSelectedItems() { } @Override - Pair, String>, String> getStructureFieldValue() - throws InvalidMetadataValueException, NoSuchMetadataFieldException { - if (settings.getDomain().orElse(Domain.DESCRIPTION).equals(Domain.METS_DIV)) { - String value = String.join(" ", selectedItems); - if (!settings.isValid(value, container.getListForLeadingMetadataFields())) { - throw new InvalidMetadataValueException(label, value); - } - return Pair.of(super.getStructureFieldSetters(settings), value); - } else { - return null; - } + public String extractSimpleValue() { + return String.join(" ", getSelectedItems()); } @Override diff --git a/Kitodo/src/main/java/org/kitodo/production/forms/createprocess/ProcessSimpleMetadata.java b/Kitodo/src/main/java/org/kitodo/production/forms/createprocess/ProcessSimpleMetadata.java index 1ed5997920f..1b29dbefc90 100644 --- a/Kitodo/src/main/java/org/kitodo/production/forms/createprocess/ProcessSimpleMetadata.java +++ b/Kitodo/src/main/java/org/kitodo/production/forms/createprocess/ProcessSimpleMetadata.java @@ -12,20 +12,16 @@ package org.kitodo.production.forms.createprocess; import java.io.Serializable; -import java.net.URI; import java.util.Arrays; import java.util.List; import java.util.Objects; -import java.util.function.BiConsumer; import org.kitodo.api.dataeditor.rulesetmanagement.ComplexMetadataViewInterface; -import org.kitodo.api.dataeditor.rulesetmanagement.MetadataViewInterface; import org.kitodo.api.dataeditor.rulesetmanagement.SimpleMetadataViewInterface; import org.kitodo.api.dataeditor.rulesetmanagement.StructuralElementViewInterface; import org.kitodo.api.dataformat.Division; import org.kitodo.api.dataformat.LogicalDivision; import org.kitodo.api.dataformat.PhysicalDivision; -import org.kitodo.exceptions.NoSuchMetadataFieldException; abstract class ProcessSimpleMetadata extends ProcessDetail implements Serializable { @@ -56,20 +52,15 @@ protected ProcessSimpleMetadata(ProcessFieldedMetadata container, SimpleMetadata */ abstract ProcessSimpleMetadata getClone(); - protected BiConsumer, String> getStructureFieldSetters(MetadataViewInterface field) - throws NoSuchMetadataFieldException { - String key = field.getId(); + /** + * Returns a simpler string representation of the Metadata. + * + * @return A string representation of the Metadata + */ + abstract String extractSimpleValue(); - switch (key.toUpperCase()) { - case "LABEL": - return Division::setLabel; - case "ORDERLABEL": - return Division::setOrderlabel; - case "CONTENTIDS": - return (division, value) -> division.getContentIds().add(URI.create(value)); - default: - throw new NoSuchMetadataFieldException(key, field.getLabel()); - } + public SimpleMetadataViewInterface getSettings() { + return settings; } /** diff --git a/Kitodo/src/main/java/org/kitodo/production/forms/createprocess/ProcessTextMetadata.java b/Kitodo/src/main/java/org/kitodo/production/forms/createprocess/ProcessTextMetadata.java index cafa033051f..b0218e17845 100644 --- a/Kitodo/src/main/java/org/kitodo/production/forms/createprocess/ProcessTextMetadata.java +++ b/Kitodo/src/main/java/org/kitodo/production/forms/createprocess/ProcessTextMetadata.java @@ -15,17 +15,12 @@ import java.util.Collection; import java.util.Collections; import java.util.Objects; -import java.util.function.BiConsumer; -import org.apache.commons.lang3.tuple.Pair; import org.kitodo.api.Metadata; import org.kitodo.api.MetadataEntry; import org.kitodo.api.dataeditor.rulesetmanagement.Domain; import org.kitodo.api.dataeditor.rulesetmanagement.InputType; import org.kitodo.api.dataeditor.rulesetmanagement.SimpleMetadataViewInterface; -import org.kitodo.api.dataformat.Division; -import org.kitodo.exceptions.InvalidMetadataValueException; -import org.kitodo.exceptions.NoSuchMetadataFieldException; public class ProcessTextMetadata extends ProcessSimpleMetadata implements Serializable { @@ -105,20 +100,6 @@ public Collection getMetadataWithFilledValues() { return getMetadata(true); } - @Override - Pair, String>, String> getStructureFieldValue() - throws InvalidMetadataValueException, NoSuchMetadataFieldException { - - if (settings.getDomain().orElse(Domain.DESCRIPTION).equals(Domain.METS_DIV)) { - if (!settings.isValid(value, container.getListForLeadingMetadataFields())) { - throw new InvalidMetadataValueException(label, value); - } - return Pair.of(super.getStructureFieldSetters(settings), value); - } else { - return null; - } - } - @Override public boolean isValid() { if (Objects.isNull(value) || value.isEmpty()) { @@ -127,6 +108,11 @@ public boolean isValid() { return settings.isValid(value, container.getListForLeadingMetadataFields()); } + @Override + public String extractSimpleValue() { + return getValue(); + } + /** * Returns the contents of the text input field of this process metadata. *