Andrey Braslavskiy
Andrey Braslavskiy

Reputation: 221

Alfresco VersionHistory returns different wrong version after redeploy

Sometimes, after Alfresco restart, versionService.getVersionHistory doesn't return the previous node version.

For example I have four node versions.

VersionHistory versionHistory = versionService.getVersionHistory(actionedUponNodeRef);
List<Version> versions = (List<Version>) versionHistory.getAllVersions();
if (versions.size() > 1) {
    Version prevVersion = versions.get(1);
}

And usually it works correct. prevVersion has 3rd node version, but sometimes, after Alfresco restart, it returns the 2nd node version, until I redeploy Alfresco again.

How can I always get the previous node version?

Upvotes: 0

Views: 137

Answers (1)

Tahir Malik
Tahir Malik

Reputation: 6643

I guess with all the Casting the order gets messed up. I've checked the code and the original Versions is of Type ArrayList, which has a super type of List. Then it gets send back as a Collection and you convert it back to a List.

Normally this shouldn't be a problem.

In your case I'd copy the code from the ScriptNode.GetVersionHistory which is used in Alfresco & Share to display the versions. Here is the snippet:

 public Scriptable getVersionHistory()
    {
        if (this.versions == null && getIsVersioned())
        {
            VersionHistory history = this.services.getVersionService().getVersionHistory(this.nodeRef);
            if (history != null)
            {
                Collection<Version> allVersions = history.getAllVersions();
                Object[] versions = new Object[allVersions.size()];
                int i = 0;
                for (Version version : allVersions)
                {
                    versions[i++] = new ScriptVersion(version, this.services, this.scope);
                }
                this.versions = Context.getCurrentContext().newArray(this.scope, versions);
            }
        }
        return this.versions;
    }

Upvotes: 1

Related Questions