Skip to content

Commit

Permalink
[CST-14901][DSC-1357][DSpace#8662] Adds IT for isLatestVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
vins01-4science committed Jan 20, 2025
1 parent 74d0494 commit ced4292
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

}

0 comments on commit ced4292

Please sign in to comment.