Skip to content

Commit

Permalink
DSC-1459 support virtual metadata based on other virtual
Browse files Browse the repository at this point in the history
  • Loading branch information
abollini committed Mar 10, 2024
1 parent 03b269b commit a4a510e
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1979,7 +1979,9 @@ public List<MetadataValue> getMetadata(Item item, String schema, String element,
List<MetadataValue> dbMetadataValues = item.getMetadata();

List<MetadataValue> fullMetadataValueList = new LinkedList<>();
fullMetadataValueList.addAll(relationshipMetadataService.getRelationshipMetadata(item, true));
if (configurationService.getBooleanProperty("item.enable-virtual-metadata", false)) {
fullMetadataValueList.addAll(relationshipMetadataService.getRelationshipMetadata(item, true));
}
fullMetadataValueList.addAll(dbMetadataValues);

item.setCachedMetadata(MetadataValueComparators.sort(fullMetadataValueList));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,9 @@ public int saveAffectedItemsForUpdate(Context context, UUID uuid) {
+ " ( SELECT metadata_field_id FROM metadatafieldregistry "
+ " WHERE metadata_schema_id = :schema AND element = 'virtualsource'"
+ " AND text_value = :uuid) "
+ " AND NOT EXISTS ("
+ " SELECT 1"
+ " AND dspace_object_id NOT IN ("
+ " SELECT uuid"
+ " FROM itemupdate_metadata_enhancement"
+ " WHERE uuid = :uuid"
+ " )";
NativeQuery<?> queryUpdate = session.createNativeQuery(sqlUpdate);
queryUpdate.setParameter("uuid", uuid.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.dspace.authority.service.AuthorityValueService;
import org.dspace.content.Item;
import org.dspace.content.MetadataValue;
import org.dspace.content.authority.Choices;
Expand Down Expand Up @@ -307,8 +308,14 @@ private List<MetadataValue> getVirtualFields(Item item) {

private void addVirtualField(Context context, Item item, String value, String authority, String lang,
int confidence) throws SQLException {
itemService.addMetadata(context, item, VIRTUAL_METADATA_SCHEMA, VIRTUAL_METADATA_ELEMENT, getVirtualQualifier(),
lang, value, authority, confidence);
if (StringUtils.startsWith(authority, AuthorityValueService.GENERATE)
|| StringUtils.startsWith(authority, AuthorityValueService.REFERENCE)) {
itemService.addMetadata(context, item, VIRTUAL_METADATA_SCHEMA, VIRTUAL_METADATA_ELEMENT,
getVirtualQualifier(), lang, value, null, Choices.CF_UNSET);
} else {
itemService.addMetadata(context, item, VIRTUAL_METADATA_SCHEMA, VIRTUAL_METADATA_ELEMENT,
getVirtualQualifier(), lang, value, authority, confidence);
}
}

private void addVirtualSourceField(Context context, Item item, String sourceValueAuthority) throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public void enhance(Context context, Item item, boolean deepMode) {

if (isUpdateNeeded) {
updateItem(context, item);
try {
saveAffectedItemsForUpdate(context, item.getID());
} catch (SQLException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ public static MetadataValueMatcher withNoPlace(String field, String value) {
return with(field, value, null, null, null, -1);
}

public static MetadataValueMatcher withNoPlace(String field, String value, String authority) {
return with(field, value, null, authority, null, 600);
}

public static MetadataValueMatcher withSecurity(String field, String value, Integer securityLevel) {
return with(field, value, null, null, 0, -1, securityLevel);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@

import java.sql.SQLException;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.codec.binary.StringUtils;
import org.dspace.AbstractIntegrationTestWithDatabase;
import org.dspace.app.matcher.CustomItemMatcher;
import org.dspace.authorize.AuthorizeException;
Expand All @@ -36,11 +34,11 @@
import org.dspace.content.Collection;
import org.dspace.content.Item;
import org.dspace.content.MetadataValue;
import org.dspace.content.WorkspaceItem;
import org.dspace.content.enhancer.service.ItemEnhancerService;
import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.content.service.ItemService;
import org.dspace.core.ReloadableEntity;
import org.dspace.services.ConfigurationService;
import org.dspace.utils.DSpace;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -56,8 +54,11 @@ public class RelatedItemEnhancerPollerIT extends AbstractIntegrationTestWithData

@Before
public void setup() throws InterruptedException {
final DSpace dspace = new DSpace();
ConfigurationService configurationService = dspace.getConfigurationService();
configurationService.setProperty("item.enable-virtual-metadata", false);
itemService = ContentServiceFactory.getInstance().getItemService();
itemEnhancerService = new DSpace().getSingletonService(ItemEnhancerService.class);
itemEnhancerService = dspace.getSingletonService(ItemEnhancerService.class);
spyItemEnhancerService = spy(itemEnhancerService);
poller.setItemEnhancerService(spyItemEnhancerService);
poller.setItemService(itemService);
Expand Down Expand Up @@ -184,6 +185,8 @@ public void testUpdateRelatedItemAreProcessed() throws Exception {
verify(spyItemEnhancerService).enhance(any(), argThat(new CustomItemMatcher(publication2.getID())), eq(true));
// 2 + 1 iteration as the last poll will return null
verify(spyItemEnhancerService, times(3)).pollItemToUpdate(any());
verify(spyItemEnhancerService).saveAffectedItemsForUpdate(any(), eq(publication.getID()));
verify(spyItemEnhancerService).saveAffectedItemsForUpdate(any(), eq(publication2.getID()));
verifyNoMoreInteractions(spyItemEnhancerService);
person = context.reloadEntity(person);
person2 = context.reloadEntity(person2);
Expand Down Expand Up @@ -256,6 +259,7 @@ public void testUpdateRelatedItemAreProcessed() throws Exception {
verify(spyItemEnhancerService).enhance(any(), argThat(new CustomItemMatcher(publication3.getID())), eq(true));
// 1 + 1 iteration as the last poll will return null
verify(spyItemEnhancerService, times(2)).pollItemToUpdate(any());
verify(spyItemEnhancerService).saveAffectedItemsForUpdate(any(), eq(publication3.getID()));
verifyNoMoreInteractions(spyItemEnhancerService);
person = context.reloadEntity(person);
person2 = context.reloadEntity(person2);
Expand Down Expand Up @@ -317,24 +321,10 @@ public void testUpdateRelatedItemAreProcessed() throws Exception {

}

private List<Integer> getPlacesAsVirtualSource(Item person1, Item publication, String metadata) {
return getMetadataValues(publication, metadata).stream()
.filter(mv -> StringUtils.equals(mv.getValue(), person1.getID().toString())).map(mv -> mv.getPlace())
.collect(Collectors.toList());
}

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

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

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

@SuppressWarnings("rawtypes")
private <T extends ReloadableEntity> T commitAndReload(T entity) throws SQLException, AuthorizeException {
context.commit();
Expand Down
3 changes: 2 additions & 1 deletion dspace/config/modules/authority.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,5 @@ authority.controlled.dc.type = true
choices.plugin.dc.type = ControlledVocabularyAuthority

# DSpace-CRIS stores by default the authority of controlled vocabularies
vocabulary.plugin.authority.store = true
vocabulary.plugin.authority.store = true
authority.controlled.cris.virtual.department = true

0 comments on commit a4a510e

Please sign in to comment.