From a4a510ee36620317c20cbb4618bc9fe27732cf24 Mon Sep 17 00:00:00 2001 From: Andrea Bollini Date: Sun, 10 Mar 2024 18:48:37 +0100 Subject: [PATCH] DSC-1459 support virtual metadata based on other virtual --- .../org/dspace/content/ItemServiceImpl.java | 4 ++- ...emForMetadataEnhancementUpdateDAOImpl.java | 5 ++-- .../impl/RelatedEntityItemEnhancer.java | 11 ++++++-- .../service/impl/ItemEnhancerServiceImpl.java | 5 ++++ .../app/matcher/MetadataValueMatcher.java | 4 +++ .../enhancer/RelatedItemEnhancerPollerIT.java | 26 ++++++------------- dspace/config/modules/authority.cfg | 3 ++- 7 files changed, 33 insertions(+), 25 deletions(-) diff --git a/dspace-api/src/main/java/org/dspace/content/ItemServiceImpl.java b/dspace-api/src/main/java/org/dspace/content/ItemServiceImpl.java index 6b3ef003edca..b34e8bdfbd7c 100644 --- a/dspace-api/src/main/java/org/dspace/content/ItemServiceImpl.java +++ b/dspace-api/src/main/java/org/dspace/content/ItemServiceImpl.java @@ -1979,7 +1979,9 @@ public List getMetadata(Item item, String schema, String element, List dbMetadataValues = item.getMetadata(); List 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)); diff --git a/dspace-api/src/main/java/org/dspace/content/dao/impl/ItemForMetadataEnhancementUpdateDAOImpl.java b/dspace-api/src/main/java/org/dspace/content/dao/impl/ItemForMetadataEnhancementUpdateDAOImpl.java index e3d8f1f28f3f..4d295f938633 100644 --- a/dspace-api/src/main/java/org/dspace/content/dao/impl/ItemForMetadataEnhancementUpdateDAOImpl.java +++ b/dspace-api/src/main/java/org/dspace/content/dao/impl/ItemForMetadataEnhancementUpdateDAOImpl.java @@ -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()); diff --git a/dspace-api/src/main/java/org/dspace/content/enhancer/impl/RelatedEntityItemEnhancer.java b/dspace-api/src/main/java/org/dspace/content/enhancer/impl/RelatedEntityItemEnhancer.java index 984620ae9d66..870dfbddfa8e 100644 --- a/dspace-api/src/main/java/org/dspace/content/enhancer/impl/RelatedEntityItemEnhancer.java +++ b/dspace-api/src/main/java/org/dspace/content/enhancer/impl/RelatedEntityItemEnhancer.java @@ -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; @@ -307,8 +308,14 @@ private List 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 { diff --git a/dspace-api/src/main/java/org/dspace/content/enhancer/service/impl/ItemEnhancerServiceImpl.java b/dspace-api/src/main/java/org/dspace/content/enhancer/service/impl/ItemEnhancerServiceImpl.java index 17c79accbd80..97816515ea36 100644 --- a/dspace-api/src/main/java/org/dspace/content/enhancer/service/impl/ItemEnhancerServiceImpl.java +++ b/dspace-api/src/main/java/org/dspace/content/enhancer/service/impl/ItemEnhancerServiceImpl.java @@ -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); + } } } diff --git a/dspace-api/src/test/java/org/dspace/app/matcher/MetadataValueMatcher.java b/dspace-api/src/test/java/org/dspace/app/matcher/MetadataValueMatcher.java index 55ceb779ba0b..1566f965a8d1 100644 --- a/dspace-api/src/test/java/org/dspace/app/matcher/MetadataValueMatcher.java +++ b/dspace-api/src/test/java/org/dspace/app/matcher/MetadataValueMatcher.java @@ -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); } diff --git a/dspace-server-webapp/src/test/java/org/dspace/content/enhancer/RelatedItemEnhancerPollerIT.java b/dspace-server-webapp/src/test/java/org/dspace/content/enhancer/RelatedItemEnhancerPollerIT.java index 752973026905..62aca5e22945 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/content/enhancer/RelatedItemEnhancerPollerIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/content/enhancer/RelatedItemEnhancerPollerIT.java @@ -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; @@ -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; @@ -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); @@ -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); @@ -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); @@ -317,24 +321,10 @@ public void testUpdateRelatedItemAreProcessed() throws Exception { } - private List 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 getMetadataValues(Item item, String metadataField) { return itemService.getMetadataByMetadataString(item, metadataField); } - private List getMetadataValues(WorkspaceItem item, String metadataField) { - return itemService.getMetadataByMetadataString(item.getItem(), metadataField); - } - @SuppressWarnings("rawtypes") private T commitAndReload(T entity) throws SQLException, AuthorizeException { context.commit(); diff --git a/dspace/config/modules/authority.cfg b/dspace/config/modules/authority.cfg index ee42fc52763e..b0cc1f011de2 100644 --- a/dspace/config/modules/authority.cfg +++ b/dspace/config/modules/authority.cfg @@ -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 \ No newline at end of file +vocabulary.plugin.authority.store = true +authority.controlled.cris.virtual.department = true