Provides an API for managing file versions (i.e. content of type, or subtype, cm:content). Note that folders are not versionable.
Alfresco has a strong versioning story, which gives you the ability to version any file content stored in the repository, no matter what the file type. Versions are full files and not diffs of the files. Alfresco gives you the ability to have both major and minor versions of content. Versions can be created/updated by checkout/checkin, by rule, through any interface or through script/APIs.
If a content file has the aspect cm:versionable applied to it, then multiple versions of the file can be managed. The VersionService provides an API to allow you to do this programmatically:
- createVersion - this creates a new version of the file, which is placed at the end of the appropriate version history. If the file has no version history then one is created and this version is considered to be the initial version.
- getVersionHistory - this gets the version history that relates to the file.
- deleteVersionHistory - this deletes the version history for a versioned file.
- getCurrentVersion - gets the current version for a file.
- revert - reverts the state of a file to that of a previous version.
- restore - restores a previously deleted file from a version in its version history.
Alfresco provides the ability to apply behavior policies (see Behaviour Policies) to content and metadata within the repository. You can think of these as event listeners, that allow you to take custom actions based on what is happening within the repository.
In the following example we are listening to the afterCreateVersion event, and when triggered we check if we have reached the maximum number of versions that we want to store. If we have, then we delete the last one (by default Alfresco has no limit of how many versions it stores):
publicclassMaxVersionPolicyimplementsVersionServicePolicies.AfterCreateVersionPolicy { privatestatic Log logger = LogFactory.getLog(MaxVersionPolicy.class); private ServiceRegistry serviceRegistry; private PolicyComponent policyComponent; private Behaviour afterCreateVersion; /** * Max number of versions we will store of a file in the repo */ privateint maxVersions; publicvoidsetServiceRegistry(ServiceRegistry serviceRegistry) { this.serviceRegistry = serviceRegistry; } publicvoidsetPolicyComponent(PolicyComponent policyComponent) { this.policyComponent = policyComponent; } publicvoidsetMaxVersions(int maxVersions) { this.maxVersions = maxVersions; } /** * Spring bean init() method */ publicvoidinit() { this.afterCreateVersion = new JavaBehaviour(this, "afterCreateVersion", Behaviour.NotificationFrequency.TRANSACTION_COMMIT); this.policyComponent.bindClassBehaviour(QName.createQName( NamespaceService.ALFRESCO_URI, "afterCreateVersion"), MaxVersionPolicy.class, this.afterCreateVersion); } @Override publicvoidafterCreateVersion(NodeRef versionableNode, Version version) { VersionHistory versionHistory = serviceRegistry.getVersionService().getVersionHistory(versionableNode); if (versionHistory != null) { logger.debug("Current number of versions: " + versionHistory.getAllVersions().size()); logger.debug("least recent/root version: " + versionHistory.getRootVersion().getVersionLabel()); // If the current number of versions in the VersionHistory is greater // than the maxVersions limit, remove the root/least recent version if (versionHistory.getAllVersions().size() > maxVersions) { logger.debug("Removing Version: " + versionHistory.getRootVersion().getVersionLabel()); serviceRegistry.getVersionService().deleteVersion(versionableNode, versionHistory.getRootVersion()); } } else { logger.debug("versionHistory does not exist"); } } }
The Spring bean for the MaxVersionPolicy class looks like this:
<beanid="org.alfresco.training.maxVersion"class="org.alfresco.training.platformsample.MaxVersionPolicy"init-method="init"> <property name="policyComponent"> <ref bean="policyComponent" /> </property> <property name="serviceRegistry"> <ref bean="ServiceRegistry" /> </property> <!-- The max number of versions per versioned file --> <property name="maxVersions"> <value>10</value> </property> </bean>