Skip to content

Commit

Permalink
Enable replacing empty metadata values in Kitodo with existing metada…
Browse files Browse the repository at this point in the history
…ta values from the catalog
  • Loading branch information
solth committed Nov 2, 2024
1 parent 5cc1268 commit c4b5ec0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,16 +171,20 @@ else if (canReplace()) {
}
}

// "<-"
private boolean canKeep() {
return oldValues.size() >= metadataView.getMinOccurs()
&& oldValues.size() <= metadataView.getMaxOccurs();
}

// "<-->"
private boolean canAdd() {
return metadataView.getMinOccurs() <= oldValues.size() + newValues.size()
&& oldValues.size() + newValues.size() <= metadataView.getMaxOccurs();
&& oldValues.size() + newValues.size() <= metadataView.getMaxOccurs()
&& !(oldValues.isEmpty() || newValues.isEmpty());
}

// "->"
private boolean canReplace() {
return newValues.size() >= metadataView.getMinOccurs()
&& newValues.size() <= metadataView.getMaxOccurs();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,9 @@ private static List<MetadataComparison> initializeMetadataComparisons(Process pr
HashSet<Metadata> oldValues = filterEntries(metadataKey, oldMetadata);
HashSet<Metadata> newValues = filterEntries(metadataKey, newMetadata);
if (!Objects.equals(oldValues, newValues) && Objects.nonNull(metadataView)) {
if (newValues.isEmpty()) {
selectionMode = Reimport.KEEP;
}
metadataComparisons.add(new MetadataComparison(metadataKey, oldValues, newValues, metadataView,
selectionMode));
}
Expand Down Expand Up @@ -550,27 +553,20 @@ public static String metadataToString(Metadata metadata) {
* @param comparisons list of metadata comparisons used for the update
*/
public static void updateMetadataWithNewValues(Workpiece workpiece, List<MetadataComparison> comparisons) {
List<Metadata> rootMetadata = new LinkedList<>(workpiece.getLogicalStructure().getMetadata());
for (MetadataComparison comparison : comparisons) {
for (Metadata metadata : rootMetadata) {
if (metadata.getKey().equals(comparison.getMetadataKey())) {
switch (comparison.getSelection()) {
case ADD:
// extend existing values with new values
workpiece.getLogicalStructure().getMetadata().addAll(comparison.getNewValues());
break;
case REPLACE:
// replace existing values with new values
workpiece.getLogicalStructure().getMetadata().removeAll(comparison.getOldValues());
workpiece.getLogicalStructure().getMetadata().addAll(comparison.getNewValues());
break;
default:
// keep existing values and discard new values
logger.info("Keep existing values for metadata {}", metadata.getKey());
}
// skip remaining metadata entries of current key because they have all been processed at this point
switch (comparison.getSelection()) {
case ADD:
// extend existing values with new values
workpiece.getLogicalStructure().getMetadata().addAll(comparison.getNewValues());
break;
}
case REPLACE:
// replace existing values with new values
workpiece.getLogicalStructure().getMetadata().removeAll(comparison.getOldValues());
workpiece.getLogicalStructure().getMetadata().addAll(comparison.getNewValues());
break;
default:
// keep existing values and discard new values
logger.info("Keep existing values for metadata {}", comparison.getMetadataKey());
}
}
}
Expand Down

0 comments on commit c4b5ec0

Please sign in to comment.