Skip to content

Commit

Permalink
Merge pull request #6375 from BartChris/3_8_refactor_preserve_logic
Browse files Browse the repository at this point in the history
[3.8] Refactor preservation logic and include hidden metadata
  • Loading branch information
solth authored Jan 23, 2025
2 parents 988ff34 + fa2b943 commit 4c1457a
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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";
Expand All @@ -96,20 +97,6 @@ public Collection<Metadata> getMetadata(boolean skipEmpty) throws InvalidMetadat
return Collections.emptyList();
}

@Override
Pair<BiConsumer<Division<?>, 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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -123,21 +120,6 @@ public String getLabel() {
*/
public abstract Collection<Metadata> 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<BiConsumer<Division<?>, 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> additionallySelectedFields = new ArrayList<>();
private static final Set<String> specialFields = Set.of(METADATA_KEY_LABEL, METADATA_KEY_ORDERLABEL,
METADATA_KEY_CONTENTIDS);

private boolean copy;

Expand Down Expand Up @@ -612,11 +614,6 @@ public List<ProcessDetail> getRows() {
return new UnmodifiableList<>(rows);
}

@Override
Pair<BiConsumer<Division<?>, String>, String> getStructureFieldValue() {
return null;
}

public TreeNode getTreeNode() {
return treeNode;
}
Expand Down Expand Up @@ -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<BiConsumer<Division<?>, 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) {
Expand All @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,17 @@
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;
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;

public class ProcessSelectMetadata extends ProcessSimpleMetadata implements Serializable {
private static final Logger logger = LogManager.getLogger(ProcessSelectMetadata.class);
Expand Down Expand Up @@ -192,17 +188,8 @@ public List<String> getSelectedItems() {
}

@Override
Pair<BiConsumer<Division<?>, 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -56,20 +52,15 @@ protected ProcessSimpleMetadata(ProcessFieldedMetadata container, SimpleMetadata
*/
abstract ProcessSimpleMetadata getClone();

protected BiConsumer<Division<?>, 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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -105,20 +100,6 @@ public Collection<Metadata> getMetadataWithFilledValues() {
return getMetadata(true);
}

@Override
Pair<BiConsumer<Division<?>, 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()) {
Expand All @@ -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.
*
Expand Down

0 comments on commit 4c1457a

Please sign in to comment.