From ced42923327641cb59070376d3e0b3e3cf8cdc0e Mon Sep 17 00:00:00 2001 From: Vincenzo Mecca Date: Mon, 20 Jan 2025 13:26:39 +0100 Subject: [PATCH] [CST-14901][DSC-1357][#8662] Adds IT for isLatestVersion --- .../indexobject/ItemIndexFactoryImpl.java | 43 +------------------ .../dspace/content/service/ItemServiceIT.java | 35 +++++++++++++++ 2 files changed, 37 insertions(+), 41 deletions(-) diff --git a/dspace-api/src/main/java/org/dspace/discovery/indexobject/ItemIndexFactoryImpl.java b/dspace-api/src/main/java/org/dspace/discovery/indexobject/ItemIndexFactoryImpl.java index 7cdb8b93d80e..682f87fbcec9 100644 --- a/dspace-api/src/main/java/org/dspace/discovery/indexobject/ItemIndexFactoryImpl.java +++ b/dspace-api/src/main/java/org/dspace/discovery/indexobject/ItemIndexFactoryImpl.java @@ -67,8 +67,6 @@ import org.dspace.services.factory.DSpaceServicesFactory; import org.dspace.util.MultiFormatDateParser; import org.dspace.util.SolrUtils; -import org.dspace.versioning.Version; -import org.dspace.versioning.VersionHistory; import org.dspace.versioning.service.VersionHistoryService; import org.dspace.xmlworkflow.storedcomponents.XmlWorkflowItem; import org.dspace.xmlworkflow.storedcomponents.service.XmlWorkflowItemService; @@ -151,7 +149,7 @@ public SolrInputDocument buildDocument(Context context, IndexableItem indexableI doc.addField("withdrawn", item.isWithdrawn()); doc.addField("discoverable", item.isDiscoverable()); doc.addField("lastModified", SolrUtils.getDateFormatter().format(item.getLastModified())); - doc.addField("latestVersion", isLatestVersion(context, item)); + doc.addField("latestVersion", itemService.isLatestVersion(context, item)); EPerson submitter = item.getSubmitter(); if (submitter != null) { @@ -175,43 +173,6 @@ public SolrInputDocument buildDocument(Context context, IndexableItem indexableI return doc; } - /** - * Check whether the given item is the latest version. - * If the latest item cannot be determined, because either the version history or the latest version is not present, - * assume the item is latest. - * @param context the DSpace context. - * @param item the item that should be checked. - * @return true if the item is the latest version, false otherwise. - */ - protected boolean isLatestVersion(Context context, Item item) throws SQLException { - VersionHistory history = versionHistoryService.findByItem(context, item); - if (history == null) { - // not all items have a version history - // if an item does not have a version history, it is by definition the latest version - return true; - } - - // start with the very latest version of the given item (may still be in workspace) - Version latestVersion = versionHistoryService.getLatestVersion(context, history); - - // find the latest version of the given item that is archived - while (latestVersion != null && !latestVersion.getItem().isArchived()) { - latestVersion = versionHistoryService.getPrevious(context, history, latestVersion); - } - - // could not find an archived version of the given item - if (latestVersion == null) { - // this scenario should never happen, but let's err on the side of showing too many items vs. to little - // (see discovery.xml, a lot of discovery configs filter out all items that are not the latest version) - return true; - } - - // sanity check - assert latestVersion.getItem().isArchived(); - - return item.equals(latestVersion.getItem()); - } - @Override public SolrInputDocument buildNewDocument(Context context, IndexableItem indexableItem) throws SQLException, IOException { @@ -704,7 +665,7 @@ public List getIndexableObjects(Context context, Item item) throws SQLException return List.copyOf(workflowItemIndexFactory.getIndexableObjects(context, xmlWorkflowItem)); } - if (!isLatestVersion(context, item)) { + if (!itemService.isLatestVersion(context, item)) { // the given item is an older version of another item return List.of(new IndexableItem(item)); } diff --git a/dspace-api/src/test/java/org/dspace/content/service/ItemServiceIT.java b/dspace-api/src/test/java/org/dspace/content/service/ItemServiceIT.java index 0704c2d98d1c..24cbce37f5a6 100644 --- a/dspace-api/src/test/java/org/dspace/content/service/ItemServiceIT.java +++ b/dspace-api/src/test/java/org/dspace/content/service/ItemServiceIT.java @@ -11,6 +11,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.hasSize; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; @@ -990,4 +991,38 @@ public void testFindByMetadataQuery() throws Exception { context.restoreAuthSystemState(); } + @Test + public void testIsLatestVersion() throws Exception { + assertTrue("Original should be the latest version", this.itemService.isLatestVersion(context, item)); + + context.turnOffAuthorisationSystem(); + + Version firstVersion = versioningService.createNewVersion(context, item); + Item firstPublication = firstVersion.getItem(); + WorkspaceItem firstPublicationWSI = workspaceItemService.findByItem(context, firstPublication); + installItemService.installItem(context, firstPublicationWSI); + + context.commit(); + context.restoreAuthSystemState(); + + assertTrue("First version should be valid", this.itemService.isLatestVersion(context, firstPublication)); + assertFalse("Original version should not be valid", this.itemService.isLatestVersion(context, item)); + + context.turnOffAuthorisationSystem(); + + Version secondVersion = versioningService.createNewVersion(context, item); + Item secondPublication = secondVersion.getItem(); + WorkspaceItem secondPublicationWSI = workspaceItemService.findByItem(context, secondPublication); + installItemService.installItem(context, secondPublicationWSI); + + context.commit(); + context.restoreAuthSystemState(); + + assertTrue("Second version should be valid", this.itemService.isLatestVersion(context, secondPublication)); + assertFalse("First version should not be valid", this.itemService.isLatestVersion(context, firstPublication)); + assertFalse("Original version should not be valid", this.itemService.isLatestVersion(context, item)); + + context.turnOffAuthorisationSystem(); + } + }