Files
alfresco-community-repo/source/java/org/alfresco/repo/version/common/AbstractVersionServiceImpl.java
Alan Davis 91eb2644ad Merged 5.2.N (5.2.1) to HEAD (5.2)
125781 rmunteanu: Merged 5.1.N (5.1.2) to 5.2.N (5.2.1)
      125603 rmunteanu: Merged 5.1.1 (5.1.1) to 5.1.N (5.1.2)
         125484 slanglois: MNT-16155 Update source headers - remove old Copyrights from Java and JSP dource files


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@127808 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
2016-06-03 16:40:56 +00:00

320 lines
11 KiB
Java

package org.alfresco.repo.version.common;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import org.alfresco.repo.policy.ClassPolicyDelegate;
import org.alfresco.repo.policy.PolicyComponent;
import org.alfresco.repo.policy.PolicyScope;
import org.alfresco.repo.version.VersionRevertCallback;
import org.alfresco.repo.version.VersionRevertCallback.RevertAspectAction;
import org.alfresco.repo.version.VersionRevertCallback.RevertAssocAction;
import org.alfresco.repo.version.VersionRevertDetails;
import org.alfresco.repo.version.VersionServicePolicies;
import org.alfresco.repo.version.VersionServicePolicies.AfterCreateVersionPolicy;
import org.alfresco.repo.version.VersionServicePolicies.AfterVersionRevertPolicy;
import org.alfresco.repo.version.VersionServicePolicies.BeforeCreateVersionPolicy;
import org.alfresco.repo.version.VersionServicePolicies.CalculateVersionLabelPolicy;
import org.alfresco.repo.version.VersionServicePolicies.OnCreateVersionPolicy;
import org.alfresco.repo.version.VersionServicePolicies.OnRevertVersionPolicy;
import org.alfresco.repo.version.common.versionlabel.SerialVersionLabelPolicy;
import org.alfresco.service.cmr.dictionary.DictionaryService;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.version.Version;
import org.alfresco.service.cmr.version.VersionServiceException;
import org.alfresco.service.namespace.QName;
/**
* Abstract version service implementation.
*
* @author Roy Wetherall
*/
public abstract class AbstractVersionServiceImpl
{
/**
* The common node service
*/
protected NodeService nodeService ;
/**
* Policy component
*/
protected PolicyComponent policyComponent;
/**
* The dictionary service
*/
protected DictionaryService dictionaryService;
/**
* Policy delegates
*/
private ClassPolicyDelegate<BeforeCreateVersionPolicy> beforeCreateVersionDelegate;
private ClassPolicyDelegate<AfterCreateVersionPolicy> afterCreateVersionDelegate;
private ClassPolicyDelegate<OnCreateVersionPolicy> onCreateVersionDelegate;
private ClassPolicyDelegate<CalculateVersionLabelPolicy> calculateVersionLabelDelegate;
private ClassPolicyDelegate<OnRevertVersionPolicy> onRevertVersionDelegate;
private ClassPolicyDelegate<AfterVersionRevertPolicy> afterVersionRevertDelegate;
/**
* Sets the general node service
*
* @param nodeService the node service
*/
public void setNodeService(NodeService nodeService)
{
this.nodeService = nodeService;
}
/**
* Sets the policy component
*
* @param policyComponent the policy component
*/
public void setPolicyComponent(PolicyComponent policyComponent)
{
this.policyComponent = policyComponent;
}
/**
* Sets the dictionary service
*
* @param dictionaryService the dictionary service
*/
public void setDictionaryService(DictionaryService dictionaryService)
{
this.dictionaryService = dictionaryService;
}
/**
* Initialise method
*/
public void initialise()
{
// Register the policies
this.beforeCreateVersionDelegate = this.policyComponent.registerClassPolicy(VersionServicePolicies.BeforeCreateVersionPolicy.class);
this.afterCreateVersionDelegate = this.policyComponent.registerClassPolicy(VersionServicePolicies.AfterCreateVersionPolicy.class);
this.onCreateVersionDelegate = this.policyComponent.registerClassPolicy(VersionServicePolicies.OnCreateVersionPolicy.class);
this.calculateVersionLabelDelegate = this.policyComponent.registerClassPolicy(VersionServicePolicies.CalculateVersionLabelPolicy.class);
this.onRevertVersionDelegate = this.policyComponent.registerClassPolicy(VersionServicePolicies.OnRevertVersionPolicy.class);
this.afterVersionRevertDelegate = this.policyComponent.registerClassPolicy(VersionServicePolicies.AfterVersionRevertPolicy.class);
}
/**
* Invokes after version has been reverted
*
* @param nodeRef the node that has been reverted
* @param version the reverted version
*/
protected void invokeAfterVersionRevert(NodeRef nodeRef,Version version)
{
// invoke for node type
QName nodeTypeQName = nodeService.getType(nodeRef);
this.afterVersionRevertDelegate.get(nodeTypeQName).afterVersionRevert(nodeRef, version);
// invoke for node aspects
Set<QName> nodeAspectQNames = nodeService.getAspects(nodeRef);
this.afterVersionRevertDelegate.get(nodeAspectQNames).afterVersionRevert(nodeRef, version);
}
/**
* Invokes the before create version policy behaviour
*
* @param nodeRef the node being versioned
*/
protected void invokeBeforeCreateVersion(NodeRef nodeRef)
{
// invoke for node type
QName nodeTypeQName = nodeService.getType(nodeRef);
this.beforeCreateVersionDelegate.get(nodeTypeQName).beforeCreateVersion(nodeRef);
// invoke for node aspects
Set<QName> nodeAspectQNames = nodeService.getAspects(nodeRef);
this.beforeCreateVersionDelegate.get(nodeAspectQNames).beforeCreateVersion(nodeRef);
}
/**
* Invoke the after create version policy bahaviour
*
* @param nodeRef the nodeRef versioned
* @param version the created version
*/
protected void invokeAfterCreateVersion(NodeRef nodeRef, Version version)
{
// invoke for node type
QName nodeTypeQName = nodeService.getType(nodeRef);
this.afterCreateVersionDelegate.get(nodeTypeQName).afterCreateVersion(nodeRef, version);
// invoke for node aspects
Set<QName> nodeAspectQNames = nodeService.getAspects(nodeRef);
this.afterCreateVersionDelegate.get(nodeAspectQNames).afterCreateVersion(nodeRef, version);
}
/**
* Invoke the on create version policy behaviour
*
*/
protected void invokeOnCreateVersion(
NodeRef nodeRef,
Map<String, Serializable> versionProperties,
PolicyScope nodeDetails)
{
// Sort out the policies for the node type
QName classRef = this.nodeService.getType(nodeRef);
invokeOnCreateVersion(classRef, nodeRef, versionProperties, nodeDetails);
// Sort out the policies for the aspects
Collection<QName> aspects = this.nodeService.getAspects(nodeRef);
for (QName aspect : aspects)
{
invokeOnCreateVersion(aspect, nodeRef, versionProperties, nodeDetails);
}
}
/**
* How should revert deal with this aspect?
* @param aspectName QName
* @param revertDetails VersionRevertDetails
* @return the action to be taken
*/
protected RevertAspectAction getRevertAspectAction(QName aspectName, VersionRevertDetails revertDetails)
{
// first check for a policy for the node type
Collection<OnRevertVersionPolicy> policies = onRevertVersionDelegate.getList(aspectName);
for(OnRevertVersionPolicy policy : policies)
{
VersionRevertCallback cb = policy.getRevertVersionCallback(aspectName, revertDetails);
if(cb != null)
{
RevertAspectAction action = cb.getRevertAspectAction(aspectName, revertDetails);
if(action != null && action == RevertAspectAction.IGNORE)
{
// ignore always wins
return action;
}
}
}
return RevertAspectAction.REVERT;
}
/**
* How should revert deal with this association
* @param className QName
* @param assocName QName
* @param revertDetails VersionRevertDetails
* @return the action to be taken
*/
protected RevertAssocAction getRevertAssocAction(QName className, QName assocName, VersionRevertDetails revertDetails)
{
Collection<OnRevertVersionPolicy> policies = onRevertVersionDelegate.getList(className);
for(OnRevertVersionPolicy policy : policies)
{
VersionRevertCallback cb = policy.getRevertVersionCallback(className, revertDetails);
if(cb != null)
{
RevertAssocAction action = cb.getRevertAssocAction(assocName, revertDetails);
if(action != null && action == RevertAssocAction.IGNORE)
{
// ignore always wins
return action;
}
}
}
return RevertAssocAction.REVERT;
}
/**
* Invokes the on create version policy behaviour for a given type
*
* @param classRef QName
* @param nodeDetails NodeRef
* @param nodeRef Map<String, Serializable>
* @param versionProperties PolicyScope
*/
private void invokeOnCreateVersion(
QName classRef,
NodeRef nodeRef,
Map<String, Serializable> versionProperties,
PolicyScope nodeDetails)
{
// Call the default implementation
defaultOnCreateVersion(
classRef,
nodeRef,
versionProperties,
nodeDetails);
// Call the policy definitions
Collection<OnCreateVersionPolicy> policies = this.onCreateVersionDelegate.getList(classRef);
for (VersionServicePolicies.OnCreateVersionPolicy policy : policies)
{
policy.onCreateVersion(
classRef,
nodeRef,
versionProperties,
nodeDetails);
}
}
/**
* Default implementation of the on create version policy.
* Override if you wish to supply your own policy.
*
* @param classRef QName
* @param nodeRef NodeRef
* @param versionProperties Map<String, Serializable>
* @param nodeDetails PolicyScope
*/
abstract protected void defaultOnCreateVersion(
QName classRef,
NodeRef nodeRef,
Map<String, Serializable> versionProperties,
PolicyScope nodeDetails);
/**
* Invoke the calculate version label policy behaviour
*
* @param classRef QName
* @param preceedingVersion Version
* @param versionNumber int
* @param versionProperties Map<String, Serializable>
* @return String
*/
protected String invokeCalculateVersionLabel(
QName classRef,
Version preceedingVersion,
int versionNumber,
Map<String, Serializable>versionProperties)
{
String versionLabel = null;
Collection<CalculateVersionLabelPolicy> behaviours = this.calculateVersionLabelDelegate.getList(classRef);
if (behaviours.size() == 0)
{
// Default the version label to the SerialVersionLabelPolicy
SerialVersionLabelPolicy defaultVersionLabelPolicy = new SerialVersionLabelPolicy();
versionLabel = defaultVersionLabelPolicy.calculateVersionLabel(classRef, preceedingVersion, versionNumber, versionProperties);
}
else if (behaviours.size() == 1)
{
// Call the policy behaviour
CalculateVersionLabelPolicy[] arr = behaviours.toArray(new CalculateVersionLabelPolicy[]{});
versionLabel = arr[0].calculateVersionLabel(classRef, preceedingVersion, versionNumber, versionProperties);
}
else
{
// Error since we can only deal with a single caculate version label policy
throw new VersionServiceException("More than one CalculateVersionLabelPolicy behaviour has been registered for the type " + classRef.toString());
}
return versionLabel;
}
abstract public StoreRef getVersionStoreReference();
}