Skip to content

Commit

Permalink
Refactor preservation logic and include hidden metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
BartChris committed Oct 1, 2024
1 parent c4ddc1b commit dc87dde
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,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 @@ -123,21 +123,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,6 +59,7 @@ 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.
Expand Down Expand Up @@ -612,11 +612,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 @@ -676,16 +671,29 @@ public void preserve() throws InvalidMetadataValueException, NoSuchMetadataField
division.setLabel(null);
}
metadata.clear();
Set<String> specialFields = new HashSet<>(Set.of(METADATA_KEY_LABEL, METADATA_KEY_ORDERLABEL,
METADATA_KEY_CONTENTIDS));

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 (!hiddenMetadata.isEmpty()) {
for (Metadata hiddenmetadatum : hiddenMetadata) {
if (hiddenmetadatum instanceof MetadataEntry
&& specialFields.contains(hiddenmetadatum.getKey())) {
updateDivision(hiddenmetadatum.getKey(), ((MetadataEntry) hiddenmetadatum).getValue());
}
}
}
metadata.addAll(hiddenMetadata);
}
} catch (InvalidMetadataValueException invalidValueException) {
Expand All @@ -705,6 +713,42 @@ public void preserve() throws InvalidMetadataValueException, NoSuchMetadataField
}
}

private void updateDivisionFromProcessDetail(String key, ProcessSimpleMetadata processDetail) throws InvalidMetadataValueException {
String simpleValue = extractSimpleValue(processDetail);
if (processDetail.getSettings().isValid(simpleValue, getListForLeadingMetadataFields())) {
throw new InvalidMetadataValueException(key, simpleValue);
};
if (simpleValue == null) {
return;
}
updateDivision(key, simpleValue);
}

private String extractSimpleValue(ProcessDetail value) {
if (value instanceof ProcessTextMetadata) {
return ((ProcessTextMetadata) value).getValue();
} else if (value instanceof ProcessSelectMetadata) {
return String.join(" ", ((ProcessSelectMetadata) value).getSelectedItems());
}
return null;
}

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 @@ -191,20 +191,6 @@ public List<String> getSelectedItems() {
return selectedItems;
}

@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;
}
}

@Override
public boolean isValid() {
for (String selectedItem : selectedItems) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ protected BiConsumer<Division<?>, String> getStructureFieldSetters(MetadataViewI
}
}

public SimpleMetadataViewInterface getSettings() {
return settings;
}

/**
* Returns if the field may be edited. Some fields may be disallowed to be
* edit from the rule set.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,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 Down

0 comments on commit dc87dde

Please sign in to comment.