Fix duplicate key exception

This commit is contained in:
Oliver Stöhr 2020-10-06 13:46:17 +02:00
parent 6c5b92a674
commit 977335bc66
6 changed files with 29 additions and 23 deletions

View File

@ -11,7 +11,12 @@
package org.kitodo.api;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
public class Metadata {
/**
@ -74,4 +79,17 @@ public class Metadata {
return domain == metadata.domain
&& Objects.equals(key, metadata.key);
}
/**
* Map Collection of Metadata objects to a Map of Metadata and String objects.
* @param metadataCollection Collection of Metadata objects
* @return Map of Metadata and String objects as java.util.Map
*/
public static Map<Metadata, String> mapToKey(Collection<Metadata> metadataCollection) {
if (Objects.isNull(metadataCollection)) {
return Collections.emptyMap();
}
return metadataCollection.parallelStream()
.collect(Collectors.toMap(Function.identity(), Metadata::getKey, (duplicateOne, duplicateTwo) -> duplicateOne));
}
}

View File

@ -170,11 +170,9 @@ public class MetadataValidation implements MetadataValidationInterface {
Collection<ValidationResult> results = new ArrayList<>();
StructuralElementViewInterface divisionView = ruleset.getStructuralElementView(type, null,
metadataLanguage);
results.add(checkForMandatoryQuantitiesOfTheMetadataRecursive(metadata.parallelStream().collect(
Collectors.toMap(Function.identity(), Metadata::getKey)),
results.add(checkForMandatoryQuantitiesOfTheMetadataRecursive(Metadata.mapToKey(metadata),
divisionView, elementString.concat(": "), translations));
results.add(checkForDetailsInTheMetadataRecursive(metadata.parallelStream().collect(
Collectors.toMap(Function.identity(), Metadata::getKey)),
results.add(checkForDetailsInTheMetadataRecursive(Metadata.mapToKey(metadata),
divisionView, elementString.concat(": "), translations));
return results;
}
@ -288,8 +286,7 @@ public class MetadataValidation implements MetadataValidationInterface {
for (Metadata metadata : metadataViewWithValues.getValue()) {
if (metadata instanceof MetadataGroup) {
ValidationResult validationResult = checkForMandatoryQuantitiesOfTheMetadataRecursive(
((MetadataGroup) metadata).getGroup().parallelStream()
.collect(Collectors.toMap(Function.identity(), Metadata::getKey)),
Metadata.mapToKey(((MetadataGroup) metadata).getGroup()),
(ComplexMetadataViewInterface) metadataView, location + metadataView.getLabel() + " - ",
translations);
if (validationResult.getState().equals(State.WARNING)) {
@ -348,8 +345,7 @@ public class MetadataValidation implements MetadataValidationInterface {
} else if (metadata instanceof MetadataGroup
&& metadataView instanceof ComplexMetadataViewInterface) {
ValidationResult validationResult = checkForDetailsInTheMetadataRecursive(
((MetadataGroup) metadata).getGroup().parallelStream()
.collect(Collectors.toMap(Function.identity(), Metadata::getKey)),
Metadata.mapToKey(((MetadataGroup) metadata).getGroup()),
(ComplexMetadataViewInterface) metadataView, location + metadataView.getLabel() + " - ",
translations);
if (validationResult.getState().equals(State.ERROR)) {

View File

@ -168,8 +168,7 @@ public class ProcessFieldedMetadata extends ProcessDetail implements Serializabl
*/
private void createMetadataTable() {
// the existing metadata is passed to the rule set, which sorts it
Map<Metadata, String> metadataWithKeys = addLabels(metadata).parallelStream()
.collect(Collectors.toMap(Function.identity(), Metadata::getKey, (duplicateOne, duplicateTwo) -> duplicateOne));
Map<Metadata, String> metadataWithKeys = Metadata.mapToKey(addLabels(metadata));
List<MetadataViewWithValuesInterface<Metadata>> tableData = metadataView
.getSortedVisibleMetadata(metadataWithKeys, additionallySelectedFields);

View File

@ -507,8 +507,7 @@ public class AddDocStrucTypeDialog {
try {
if (Objects.nonNull(selectedMetadataTreeNode)
&& Objects.nonNull(selectedMetadataTreeNode.getData())) {
existingMetadata = ((ProcessFieldedMetadata) selectedMetadataTreeNode.getData()).getMetadata().stream()
.collect(Collectors.toMap(Function.identity(), Metadata::getKey));
existingMetadata = Metadata.mapToKey(((ProcessFieldedMetadata) selectedMetadataTreeNode.getData()).getMetadata());
ComplexMetadataViewInterface metadataView = dataEditor.getRuleset().getMetadataView(
((ProcessFieldedMetadata) selectedMetadataTreeNode.getData()).getMetadataID(),
dataEditor.getAcquisitionStage(), dataEditor.getPriorityList());

View File

@ -86,8 +86,7 @@ public class LegacyLogicalDocStructHelper implements LegacyDocStructHelperInterf
@Override
@Deprecated
public void addMetadata(LegacyMetadataHelper metadata) {
Map<Metadata, String> metadataEntriesMappedToKeyNames = includedStructuralElement.getMetadata().parallelStream()
.collect(Collectors.toMap(Function.identity(), Metadata::getKey));
Map<Metadata, String> metadataEntriesMappedToKeyNames = Metadata.mapToKey(includedStructuralElement.getMetadata());
Optional<MetadataViewInterface> optionalKeyView = divisionView
.getAddableMetadata(metadataEntriesMappedToKeyNames, Collections.emptyList()).parallelStream()
.filter(keyView -> keyView.getId().equals(metadata.getMetadataType().getName())).findFirst();
@ -134,8 +133,7 @@ public class LegacyLogicalDocStructHelper implements LegacyDocStructHelperInterf
@Override
@Deprecated
public List<LegacyMetadataTypeHelper> getAddableMetadataTypes() {
Map<Metadata, String> metadataEntriesMappedToKeyNames = includedStructuralElement.getMetadata().parallelStream()
.collect(Collectors.toMap(Function.identity(), Metadata::getKey));
Map<Metadata, String> metadataEntriesMappedToKeyNames = Metadata.mapToKey(includedStructuralElement.getMetadata());
Collection<MetadataViewInterface> addableKeys = divisionView.getAddableMetadata(metadataEntriesMappedToKeyNames,
Collections.emptyList());
ArrayList<LegacyMetadataTypeHelper> addableMetadataTypes = new ArrayList<>(addableKeys.size());
@ -159,8 +157,7 @@ public class LegacyLogicalDocStructHelper implements LegacyDocStructHelperInterf
@Deprecated
public List<LegacyMetadataHelper> getAllMetadata() {
List<LegacyMetadataHelper> allMetadata = new LinkedList<>();
Map<Metadata, String> metadataEntriesMappedToKeyNames = includedStructuralElement.getMetadata().parallelStream()
.collect(Collectors.toMap(Function.identity(), Metadata::getKey));
Map<Metadata, String> metadataEntriesMappedToKeyNames = Metadata.mapToKey(includedStructuralElement.getMetadata());
List<MetadataViewWithValuesInterface<Metadata>> entryViews = divisionView
.getSortedVisibleMetadata(metadataEntriesMappedToKeyNames, Collections.emptyList());
for (MetadataViewWithValuesInterface<Metadata> entryView : entryViews) {
@ -182,8 +179,7 @@ public class LegacyLogicalDocStructHelper implements LegacyDocStructHelperInterf
@Deprecated
public List<LegacyMetadataHelper> getAllMetadataByType(LegacyMetadataTypeHelper metadataType) {
List<LegacyMetadataHelper> allMetadata = new LinkedList<>();
Map<Metadata, String> metadataEntriesMappedToKeyNames = includedStructuralElement.getMetadata().parallelStream()
.collect(Collectors.toMap(Function.identity(), Metadata::getKey));
Map<Metadata, String> metadataEntriesMappedToKeyNames = Metadata.mapToKey(includedStructuralElement.getMetadata());
List<MetadataViewWithValuesInterface<Metadata>> entryViews = divisionView
.getSortedVisibleMetadata(metadataEntriesMappedToKeyNames, Collections.emptyList());
for (MetadataViewWithValuesInterface<Metadata> entryView : entryViews) {

View File

@ -363,9 +363,7 @@ public class NewspaperProcessesGenerator extends ProcessGenerator {
final Collection<String> processTitleKeys = ruleset.getFunctionalKeys(FunctionalMetadata.PROCESS_TITLE);
newspaperProcessTitleViews = newspaperView
.getAddableMetadata(overallWorkpiece.getRootElement().getMetadata().parallelStream()
.collect(Collectors.toMap(Function.identity(), Metadata::getKey)),
Collections.emptyList())
.getAddableMetadata(Metadata.mapToKey(overallWorkpiece.getRootElement().getMetadata()), Collections.emptyList())
.parallelStream().filter(SimpleMetadataViewInterface.class::isInstance)
.map(SimpleMetadataViewInterface.class::cast)
.filter(metadataView -> processTitleKeys.contains(metadataView.getId())).collect(Collectors.toList());