diff --git a/source/java/org/alfresco/repo/version/Version2ServiceImpl.java b/source/java/org/alfresco/repo/version/Version2ServiceImpl.java index ac97fa8913..369ca8ed9b 100644 --- a/source/java/org/alfresco/repo/version/Version2ServiceImpl.java +++ b/source/java/org/alfresco/repo/version/Version2ServiceImpl.java @@ -862,33 +862,39 @@ public class Version2ServiceImpl extends VersionServiceImpl implements VersionSe } /** - * Gets current version of the passed node ref + * Gets current version of the passed node reference. The node reference is expected to be the 'live' node. * * This uses the version label as a mechanism for looking up the version node. */ private Pair getCurrentVersionImpl(NodeRef versionHistoryRef, NodeRef nodeRef) { + // The noderef should not be a version type node. + if (Version2Model.STORE_ID.equals(nodeRef.getStoreRef().getIdentifier())) + { + throw new IllegalArgumentException("The node reference " + nodeRef + " is pointing to a version node, when a reference to a live node is expected."); + } + Pair result = null; String versionLabel = (String)this.nodeService.getProperty(nodeRef, ContentModel.PROP_VERSION_LABEL); - // note: resultant list is ordered by (a) explicit index and (b) association creation time + // Note: resultant list is ordered by (a) explicit index and (b) association creation time List versionAssocs = getVersionAssocs(versionHistoryRef, false); // Current version should be head version (since no branching) - int cnt = versionAssocs.size(); - for (int i = cnt; i > 0; i--) + int versionCount = versionAssocs.size(); + for (int i = versionCount; i > 0; i--) { ChildAssociationRef versionAssoc = versionAssocs.get(i-1); String tempLabel = (String)this.dbNodeService.getProperty(versionAssoc.getChildRef(), Version2Model.PROP_QNAME_VERSION_LABEL); if (tempLabel != null && tempLabel.equals(versionLabel) == true) { - boolean headVersion = (i == cnt); + boolean headVersion = (i == versionCount); - if (! headVersion) + if (!headVersion) { - throw new ConcurrencyFailureException("Unexpected: current version does not appear to be 1st version in the list ["+versionHistoryRef+", "+nodeRef+"]"); + throw new ConcurrencyFailureException("Unexpected: current version does not appear to be 1st version in the list ["+versionHistoryRef+", "+nodeRef+"]"); } result = new Pair(headVersion, getVersion(versionAssoc.getChildRef())); @@ -902,7 +908,7 @@ public class Version2ServiceImpl extends VersionServiceImpl implements VersionSe /** * Check if versions are marked with invalid version label, if true > apply default serial version label (e.g. "1.0", "1.1") * - * @param versionHistory a version histore node reference + * @param versionHistory a version history node reference * @param nodeRef a node reference */ private void checkForCorruptedVersions(NodeRef versionHistory, NodeRef nodeRef) diff --git a/source/java/org/alfresco/service/cmr/version/VersionService.java b/source/java/org/alfresco/service/cmr/version/VersionService.java index ba224c9b02..0819662826 100644 --- a/source/java/org/alfresco/service/cmr/version/VersionService.java +++ b/source/java/org/alfresco/service/cmr/version/VersionService.java @@ -169,11 +169,10 @@ public interface VersionService throws AspectMissingException; /** - * Gets the version object for the current version of the node reference - * passed. + * Gets the version object for the current version of the node reference passed. *

* Returns null if the node is not versionable or has not been versioned. - * @param nodeRef the node reference + * @param nodeRef the node reference of the 'live' node * @return the version object for the current version */ @Auditable(parameters = {"nodeRef"}) diff --git a/source/test-java/org/alfresco/repo/version/VersionServiceImplTest.java b/source/test-java/org/alfresco/repo/version/VersionServiceImplTest.java index 9ba104a938..d28d1b9546 100644 --- a/source/test-java/org/alfresco/repo/version/VersionServiceImplTest.java +++ b/source/test-java/org/alfresco/repo/version/VersionServiceImplTest.java @@ -308,7 +308,7 @@ public class VersionServiceImplTest extends BaseVersionStoreTest assertEquals(3, vh.getAllVersions().size()); // TODO check list of versions ... ! - } + } /** * Tests the creation of multiple versions of a versionable node with null version properties @@ -336,10 +336,41 @@ public class VersionServiceImplTest extends BaseVersionStoreTest createVersion(node); } - // TODO test versioning numberious times with branchs implies by different workspaces + /** + * Test retrieving the current version for a node with multiple versions + */ + public void testGetCurrentVersion() + { + NodeRef versionableNode = createNewVersionableNode(); + createVersion(versionableNode); + createVersion(versionableNode); + createVersion(versionableNode); + + VersionHistory vh = this.versionService.getVersionHistory(versionableNode); + Version version = vh.getRootVersion(); + + // Get current version from live node + NodeRef node = version.getVersionedNodeRef(); + Version currentVersion = versionService.getCurrentVersion(node); + assertNotNull("Failed to retrieve the current version from the head", currentVersion); + + try + { + // Get current version from the version node (frozen state version node) - not allowed (MNT-15447) + node = version.getFrozenStateNodeRef(); + currentVersion = versionService.getCurrentVersion(node); + fail("Getting the current version is only allowed on live nodes, not on version nodes."); + } + catch (IllegalArgumentException ex) + { + // expected + } + } + + // TODO test versioning numberious times with branches implies by different workspaces /** - * Test versioning the children of a verionable node + * Test versioning the children of a versionable node */ public void testVersioningChildren() {