Skip to content

Commit

Permalink
DSC-1561 extend RelatedEntityItemEnhancer to manage a list of metadat…
Browse files Browse the repository at this point in the history
…a to use as source and a list of related item metadata from which extract the enhanced metadata
  • Loading branch information
abollini committed Mar 2, 2024
1 parent 96a5cbb commit f6b4127
Show file tree
Hide file tree
Showing 8 changed files with 290 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ public class RelatedEntityItemEnhancer extends AbstractItemEnhancer {
/**
* the metadata used to navigate the relation, i.e. dc.contributor.author
*/
private String sourceItemMetadataField;
private List<String> sourceItemMetadataFields;

/**
* the metadata that is copied from the linked entity, i.e. person.identifier.orcid
*/
private String relatedItemMetadataField;
private List<String> relatedItemMetadataFields;

@Override
public boolean canEnhance(Context context, Item item) {
Expand Down Expand Up @@ -166,16 +166,10 @@ private Map<String, List<MetadataValueDTO>> getToBeVirtualMetadata(Context conte
mvRelated.setValue(PLACEHOLDER_PARENT_METADATA_VALUE);
tobeVirtualMetadata.add(mvRelated);
} else {
List<MetadataValue> relatedItemMetadataValues = getMetadataValues(relatedItem,
relatedItemMetadataField);
if (relatedItemMetadataValues.isEmpty()) {
MetadataValueDTO mvRelated = new MetadataValueDTO();
mvRelated.setSchema(VIRTUAL_METADATA_SCHEMA);
mvRelated.setElement(VIRTUAL_METADATA_ELEMENT);
mvRelated.setQualifier(getVirtualQualifier());
mvRelated.setValue(PLACEHOLDER_PARENT_METADATA_VALUE);
tobeVirtualMetadata.add(mvRelated);
} else {
boolean foundAtLeastOneValue = false;
for (String relatedItemMetadataField : relatedItemMetadataFields) {
List<MetadataValue> relatedItemMetadataValues = getMetadataValues(relatedItem,
relatedItemMetadataField);
for (MetadataValue relatedItemMetadataValue : relatedItemMetadataValues) {
MetadataValueDTO mvRelated = new MetadataValueDTO();
mvRelated.setSchema(VIRTUAL_METADATA_SCHEMA);
Expand All @@ -188,8 +182,17 @@ private Map<String, List<MetadataValueDTO>> getToBeVirtualMetadata(Context conte
mvRelated.setConfidence(Choices.CF_ACCEPTED);
}
tobeVirtualMetadata.add(mvRelated);
foundAtLeastOneValue = true;
}
}
if (!foundAtLeastOneValue) {
MetadataValueDTO mvRelated = new MetadataValueDTO();
mvRelated.setSchema(VIRTUAL_METADATA_SCHEMA);
mvRelated.setElement(VIRTUAL_METADATA_ELEMENT);
mvRelated.setQualifier(getVirtualQualifier());
mvRelated.setValue(PLACEHOLDER_PARENT_METADATA_VALUE);
tobeVirtualMetadata.add(mvRelated);
}
}
tobeVirtualMetadataMap.put(authority, tobeVirtualMetadata);
}
Expand Down Expand Up @@ -225,8 +228,10 @@ private List<MetadataValue> getObsoleteVirtualFields(Item item) {
}

private Set<String> getVirtualSources(Item item) {
return itemService.getMetadataByMetadataString(item, sourceItemMetadataField).stream()
.filter(mv -> UUIDUtils.fromString(mv.getAuthority()) != null).map(mv -> mv.getAuthority())
return sourceItemMetadataFields.stream()
.flatMap(field -> itemService.getMetadataByMetadataString(item, field).stream())
.filter(mv -> UUIDUtils.fromString(mv.getAuthority()) != null)
.map(mv -> mv.getAuthority())
.collect(Collectors.toSet());
}

Expand Down Expand Up @@ -276,28 +281,32 @@ private boolean performEnhancement(Context context, Item item) throws SQLExcepti
Map<String, List<MetadataValue>> currentVirtualsMap = getCurrentVirtualsMap(item);
Set<String> virtualSources = getVirtualSources(item);
for (String authority : virtualSources) {
boolean foundAtLeastOne = false;
if (!currentVirtualsMap.containsKey(authority)) {
result = true;
Item relatedItem = findRelatedEntityItem(context, authority);
if (relatedItem == null) {
addVirtualField(context, item, PLACEHOLDER_PARENT_METADATA_VALUE, null, null, Choices.CF_UNSET);
addVirtualSourceField(context, item, authority);
continue;
}

List<MetadataValue> relatedItemMetadataValues = getMetadataValues(relatedItem,
relatedItemMetadataField);
if (relatedItemMetadataValues.isEmpty()) {
for (String relatedItemMetadataField : relatedItemMetadataFields) {
List<MetadataValue> relatedItemMetadataValues = getMetadataValues(relatedItem,
relatedItemMetadataField);
for (MetadataValue relatedItemMetadataValue : relatedItemMetadataValues) {
foundAtLeastOne = true;
addVirtualField(context, item, relatedItemMetadataValue.getValue(),
relatedItemMetadataValue.getAuthority(), relatedItemMetadataValue.getLanguage(),
relatedItemMetadataValue.getConfidence());
addVirtualSourceField(context, item, authority);
}
}
if (!foundAtLeastOne) {
addVirtualField(context, item, PLACEHOLDER_PARENT_METADATA_VALUE, null, null, Choices.CF_UNSET);
addVirtualSourceField(context, item, authority);
continue;
}
for (MetadataValue relatedItemMetadataValue : relatedItemMetadataValues) {
addVirtualField(context, item, relatedItemMetadataValue.getValue(),
relatedItemMetadataValue.getAuthority(), relatedItemMetadataValue.getLanguage(),
relatedItemMetadataValue.getConfidence());
addVirtualSourceField(context, item, authority);
}
result = true;
}
}
return result;
Expand Down Expand Up @@ -340,12 +349,28 @@ public void setSourceEntityType(String sourceEntityType) {
this.sourceEntityType = sourceEntityType;
}

@Deprecated
public void setSourceItemMetadataField(String sourceItemMetadataField) {
this.sourceItemMetadataField = sourceItemMetadataField;
LOGGER.warn(
"RelatedEntityItemEnhancer configured using the old single source item metadata field, "
+ "please update the configuration to use the list");
this.sourceItemMetadataFields = List.of(sourceItemMetadataField);
}

@Deprecated
public void setRelatedItemMetadataField(String relatedItemMetadataField) {
this.relatedItemMetadataField = relatedItemMetadataField;
LOGGER.warn(
"RelatedEntityItemEnhancer configured using the old single related item metadata field, "
+ "please update the configuration to use the list");
this.relatedItemMetadataFields = List.of(relatedItemMetadataField);
}

public void setRelatedItemMetadataFields(List<String> relatedItemMetadataFields) {
this.relatedItemMetadataFields = relatedItemMetadataFields;
}

public void setSourceItemMetadataFields(List<String> sourceItemMetadataFields) {
this.sourceItemMetadataFields = sourceItemMetadataFields;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
The contents of this file are subject to the license and copyright
detailed in the LICENSE and NOTICE files at the root of the source
tree and available online at
http://www.dspace.org/license/
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"
>

<context:annotation-config />

<bean class="org.dspace.content.enhancer.impl.RelatedEntityItemEnhancer" >
<property name="sourceEntityType" value="TestEntity" />
<property name="sourceItemMetadataFields">
<list>
<value>dc.contributor.author</value>
<value>dc.contributor.editor</value>
</list>
</property>
<property name="relatedItemMetadataFields">
<list>
<value>person.affiliation.name</value>
<value>person.identifier.orcid</value>
</list>
</property>
<property name="virtualQualifier" value="testmultival" />
</bean>

</beans>
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public void testWorkbookBuildingFromItemDtos() throws Exception {
with("dc.contributor.author", "White, Walter", authorId, 600),
with("oairecerif.author.affiliation", PLACEHOLDER_PARENT_METADATA_VALUE),
with("cris.virtual.department", PLACEHOLDER_PARENT_METADATA_VALUE),
with("cris.virtual.author-orcid", PLACEHOLDER_PARENT_METADATA_VALUE)));
with("cris.virtual.orcid", PLACEHOLDER_PARENT_METADATA_VALUE)));

assertThat(getItemBitstreamsByBundle(firstItem, "ORIGINAL"), contains(
bitstreamWith("Bitstream 1", "First bitstream content"),
Expand All @@ -242,8 +242,8 @@ public void testWorkbookBuildingFromItemDtos() throws Exception {
with("oairecerif.author.affiliation", "Company", 1),
with("cris.virtual.department", PLACEHOLDER_PARENT_METADATA_VALUE),
with("cris.virtual.department", PLACEHOLDER_PARENT_METADATA_VALUE),
with("cris.virtual.author-orcid", PLACEHOLDER_PARENT_METADATA_VALUE),
with("cris.virtual.author-orcid", PLACEHOLDER_PARENT_METADATA_VALUE)
with("cris.virtual.orcid", PLACEHOLDER_PARENT_METADATA_VALUE),
with("cris.virtual.orcid", PLACEHOLDER_PARENT_METADATA_VALUE)
));

assertThat(getItemBitstreamsByBundle(secondItem, "ORIGINAL"), contains(
Expand Down
Loading

0 comments on commit f6b4127

Please sign in to comment.