[DURACOM-125][#8736] Preserved order of modified MetadataValues

feat:
  - Introduced new Class with static methods usable to order
`MetadataValue` lists;
  - Introduced ITs also for `PatchMetadata` replace operations;
  - Introduced new method `DspaceObject#getMetadataFieldId`.
ref:
  - Replaced old sort method inside `ItemServiceImpl` with the new one
This commit is contained in:
Vincenzo Mecca
2023-03-24 17:34:02 +01:00
parent 882373faa1
commit b1c1edc7f0
6 changed files with 203 additions and 93 deletions

View File

@@ -126,6 +126,11 @@ public abstract class DSpaceObjectServiceImpl<T extends DSpaceObject> implements
}
}
// Sort the metadataValues if they have been modified,
// is used to preserve the default order.
if (dso.isMetadataModified()) {
values.sort(MetadataValueComparators.defaultComparator);
}
// Create an array of matching values
return values;
}
@@ -542,7 +547,7 @@ public abstract class DSpaceObjectServiceImpl<T extends DSpaceObject> implements
int add = 4 - tokens.length;
if (add > 0) {
tokens = (String[]) ArrayUtils.addAll(tokens, new String[add]);
tokens = ArrayUtils.addAll(tokens, new String[add]);
}
return tokens;
@@ -603,21 +608,18 @@ public abstract class DSpaceObjectServiceImpl<T extends DSpaceObject> implements
//If two places are the same then the MetadataValue instance will be placed before the
//RelationshipMetadataValue instance.
//This is done to ensure that the order is correct.
metadataValues.sort(new Comparator<MetadataValue>() {
@Override
public int compare(MetadataValue o1, MetadataValue o2) {
int compare = o1.getPlace() - o2.getPlace();
if (compare == 0) {
if (o1 instanceof RelationshipMetadataValue && o2 instanceof RelationshipMetadataValue) {
return compare;
} else if (o1 instanceof RelationshipMetadataValue) {
return 1;
} else if (o2 instanceof RelationshipMetadataValue) {
return -1;
}
metadataValues.sort((o1, o2) -> {
int compare = o1.getPlace() - o2.getPlace();
if (compare == 0) {
if (o1 instanceof RelationshipMetadataValue && o2 instanceof RelationshipMetadataValue) {
return compare;
} else if (o1 instanceof RelationshipMetadataValue) {
return 1;
} else if (o2 instanceof RelationshipMetadataValue) {
return -1;
}
return compare;
}
return compare;
});
for (MetadataValue metadataValue : metadataValues) {
//Retrieve & store the place for each metadata value
@@ -634,7 +636,7 @@ public abstract class DSpaceObjectServiceImpl<T extends DSpaceObject> implements
String authority = metadataValue.getAuthority();
String relationshipId = StringUtils.split(authority, "::")[1];
Relationship relationship = relationshipService.find(context, Integer.parseInt(relationshipId));
if (relationship.getLeftItem().equals((Item) dso)) {
if (relationship.getLeftItem().equals(dso)) {
relationship.setLeftPlace(mvPlace);
} else {
relationship.setRightPlace(mvPlace);