Skip to content

Commit

Permalink
[DSC-1214] Fixed virtual field place after item update
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaGiamminonni committed Aug 31, 2023
1 parent b14d87f commit 8264b30
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private void cleanObsoleteVirtualFields(Context context, Item item) throws SQLEx
}

private void updateVirtualFieldsPlaces(Context context, Item item) {
List<MetadataValue> virtualSourceFields = getMetadataValues(item, getVirtualSourceMetadataField());
List<MetadataValue> virtualSourceFields = getVirtualSourceFields(item);
for (MetadataValue virtualSourceField : virtualSourceFields) {
metadataWithPlaceToUpdate(item, virtualSourceField)
.ifPresent(updatePlaces(item, virtualSourceField));
Expand Down Expand Up @@ -113,9 +113,9 @@ private List<MetadataValue> getObsoleteVirtualFields(Item item) {

List<MetadataValue> obsoleteVirtualFields = new ArrayList<>();

List<MetadataValue> virtualSourceFields = getMetadataValues(item, getVirtualSourceMetadataField());
List<MetadataValue> virtualSourceFields = getVirtualSourceFields(item);
for (MetadataValue virtualSourceField : virtualSourceFields) {
if (isRelatedSourceNoMorePresent(item, virtualSourceField)) {
if (!isPlaceholder(virtualSourceField) && isRelatedSourceNoMorePresent(item, virtualSourceField)) {
obsoleteVirtualFields.add(virtualSourceField);
getRelatedVirtualField(item, virtualSourceField).ifPresent(obsoleteVirtualFields::add);
}
Expand All @@ -131,7 +131,7 @@ private boolean isRelatedSourceNoMorePresent(Item item, MetadataValue virtualSou
}

private Optional<MetadataValue> getRelatedVirtualField(Item item, MetadataValue virtualSourceField) {
return getMetadataValues(item, getVirtualMetadataField()).stream()
return getVirtualFields(item).stream()
.filter(metadataValue -> metadataValue.getPlace() == virtualSourceField.getPlace())
.findFirst();
}
Expand All @@ -141,6 +141,7 @@ private void performEnhancement(Context context, Item item) throws SQLException
if (noEnhanceableMetadata(context, item)) {
return;
}

for (MetadataValue metadataValue : getEnhanceableMetadataValue(item)) {

if (wasValueAlreadyUsedForEnhancement(item, metadataValue)) {
Expand Down Expand Up @@ -191,9 +192,19 @@ private List<MetadataValue> getEnhanceableMetadataValue(Item item) {
}

private boolean wasValueAlreadyUsedForEnhancement(Item item, MetadataValue metadataValue) {
return getMetadataValues(item, getVirtualSourceMetadataField()).stream()

if (isPlaceholderAtPlace(getVirtualFields(item), metadataValue.getPlace())) {
return true;
}

return getVirtualSourceFields(item).stream()
.anyMatch(virtualSourceField -> virtualSourceField.getPlace() == metadataValue.getPlace()
&& hasAuthorityEqualsTo(metadataValue, virtualSourceField.getValue()));

}

private boolean isPlaceholderAtPlace(List<MetadataValue> metadataValues, int place) {
return place < metadataValues.size() ? isPlaceholder(metadataValues.get(place)) : false;
}

private boolean hasAuthorityEqualsTo(MetadataValue metadataValue, String authority) {
Expand All @@ -209,10 +220,22 @@ private Item findRelatedEntityItem(Context context, MetadataValue metadataValue)
}
}

private boolean isPlaceholder(MetadataValue metadataValue) {
return PLACEHOLDER_PARENT_METADATA_VALUE.equals(metadataValue.getValue());
}

private List<MetadataValue> getMetadataValues(Item item, String metadataField) {
return itemService.getMetadataByMetadataString(item, metadataField);
}

private List<MetadataValue> getVirtualSourceFields(Item item) {
return getMetadataValues(item, getVirtualSourceMetadataField());
}

private List<MetadataValue> getVirtualFields(Item item) {
return getMetadataValues(item, getVirtualMetadataField());
}

private void addVirtualField(Context context, Item item, String value) throws SQLException {
itemService.addMetadata(context, item, VIRTUAL_METADATA_SCHEMA, VIRTUAL_METADATA_ELEMENT,
getVirtualQualifier(), null, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static org.dspace.app.matcher.MetadataValueMatcher.with;
import static org.dspace.core.CrisConstants.PLACEHOLDER_PARENT_METADATA_VALUE;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItem;
Expand Down Expand Up @@ -290,6 +291,75 @@ public void testWithWorkspaceItem() throws Exception {

}

@Test
@SuppressWarnings("unchecked")
public void testEnhancementAfterItemUpdate() throws Exception {

context.turnOffAuthorisationSystem();

Item person = ItemBuilder.createItem(context, collection)
.withTitle("Walter White")
.withOrcidIdentifier("0000-0000-1111-2222")
.build();

String personId = person.getID().toString();

Item publication = ItemBuilder.createItem(context, collection)
.withTitle("Test publication")
.withEntityType("Publication")
.withAuthor("Jesse Pinkman")
.withAuthor("Saul Goodman")
.withAuthor("Walter White", person.getID().toString())
.withAuthor("Gus Fring")
.build();

context.restoreAuthSystemState();
publication = commitAndReload(publication);

assertThat(getMetadataValues(publication, "dc.contributor.author"), contains(
with("dc.contributor.author", "Jesse Pinkman"),
with("dc.contributor.author", "Saul Goodman", 1),
with("dc.contributor.author", "Walter White", personId, 2, 600),
with("dc.contributor.author", "Gus Fring", 3)));

assertThat(getMetadataValues(publication, "cris.virtual.author-orcid"), contains(
with("cris.virtual.author-orcid", PLACEHOLDER_PARENT_METADATA_VALUE),
with("cris.virtual.author-orcid", PLACEHOLDER_PARENT_METADATA_VALUE, 1),
with("cris.virtual.author-orcid", "0000-0000-1111-2222", 2),
with("cris.virtual.author-orcid", PLACEHOLDER_PARENT_METADATA_VALUE, 3)));

assertThat(getMetadataValues(publication, "cris.virtualsource.author-orcid"), contains(
with("cris.virtualsource.author-orcid", PLACEHOLDER_PARENT_METADATA_VALUE),
with("cris.virtualsource.author-orcid", PLACEHOLDER_PARENT_METADATA_VALUE, 1),
with("cris.virtualsource.author-orcid", personId, 2),
with("cris.virtualsource.author-orcid", PLACEHOLDER_PARENT_METADATA_VALUE, 3)));

context.turnOffAuthorisationSystem();
itemService.addMetadata(context, publication, "dc", "title", "alternative", null, "Other name");
itemService.update(context, publication);
context.restoreAuthSystemState();
publication = commitAndReload(publication);

assertThat(getMetadataValues(publication, "dc.contributor.author"), contains(
with("dc.contributor.author", "Jesse Pinkman"),
with("dc.contributor.author", "Saul Goodman", 1),
with("dc.contributor.author", "Walter White", personId, 2, 600),
with("dc.contributor.author", "Gus Fring", 3)));

assertThat(getMetadataValues(publication, "cris.virtual.author-orcid"), contains(
with("cris.virtual.author-orcid", PLACEHOLDER_PARENT_METADATA_VALUE),
with("cris.virtual.author-orcid", PLACEHOLDER_PARENT_METADATA_VALUE, 1),
with("cris.virtual.author-orcid", "0000-0000-1111-2222", 2),
with("cris.virtual.author-orcid", PLACEHOLDER_PARENT_METADATA_VALUE, 3)));

assertThat(getMetadataValues(publication, "cris.virtualsource.author-orcid"), contains(
with("cris.virtualsource.author-orcid", PLACEHOLDER_PARENT_METADATA_VALUE),
with("cris.virtualsource.author-orcid", PLACEHOLDER_PARENT_METADATA_VALUE, 1),
with("cris.virtualsource.author-orcid", personId, 2),
with("cris.virtualsource.author-orcid", PLACEHOLDER_PARENT_METADATA_VALUE, 3)));

}

private MetadataValue getFirstMetadataValue(Item item, String metadataField) {
return getMetadataValues(item, metadataField).get(0);
}
Expand Down

0 comments on commit 8264b30

Please sign in to comment.