Fire a rule only when the version changes

cancel
Showing results for 
Search instead for 
Did you mean: 
akreienbring
Active Member

Fire a rule only when the version changes

Jump to solution

I couldn't find a way to do this :-(

There is "Items ar updated" but that triggers even if a property changes.

And there is "Items are created or enter this folder" which does NOT trigger when a new version is uploaded.

Is this correct?

1 Solution

Accepted Solutions
abhinavmishra14
Advanced

Re: Fire a rule only when the version changes

Jump to solution

You can implement a behavior.

Custom behavior can listen to onCreateVersion event or choose from below given available events on version policy and execute your custom logic from the implementation methods: 

https://docs.alfresco.com/5.2/references/dev-extension-points-behaviors.html

org.alfresco.repo.version.VersionServicePolicies
  • beforeCreateVersion
  • afterCreateVersion
  • onCreateVersion
  • calculateVersionLabel

 

It could be something like:

public class ObjectVersionBehaviour implements VersionServicePolicies.OnCreateVersionPolicy {
	
	private final NodeService nodeService;

	private final PolicyComponent policyComponent;

	public ObjectVersionBehaviour(final ServiceRegistry serviceRegistry,
			final PolicyComponent policyComponent) {	
		//TODO:: get the required service from serviceRegistry and 
		this.nodeService = serviceRegistry.getNodeService();
		this.policyComponent = policyComponent;
		
		//Binding on cm:content type
		createBindings(VersionServicePolicies.BeforeCreateVersionPolicy.QNAME, ContentModel.TYPE_CONTENT,
				Behaviour.NotificationFrequency.EVERY_EVENT);
	}

	private void createBindings(final QName policy, final QName type,
			final Behaviour.NotificationFrequency frequency) {
		policyComponent.bindClassBehaviour(policy, type, new JavaBehaviour(this, policy.getLocalName(), frequency));
	}
	
	@Override
	public void onCreateVersion(final QName classRef, final NodeRef versionableNode, 
				final Map<String, Serializable> versionProperties,
				final PolicyScope nodeDetails){
		//Your custom logic on version creation
	}
}

Refer this tutorial if want to learn more on implementing behaviors:

https://ecmarchitect.com/alfresco-developer-series-tutorials/behaviors/tutorial/tutorial.html

~Abhinav
(ACSCE, AWS SAA, Azure Admin)

View solution in original post

2 Replies
abhinavmishra14
Advanced

Re: Fire a rule only when the version changes

Jump to solution

You can implement a behavior.

Custom behavior can listen to onCreateVersion event or choose from below given available events on version policy and execute your custom logic from the implementation methods: 

https://docs.alfresco.com/5.2/references/dev-extension-points-behaviors.html

org.alfresco.repo.version.VersionServicePolicies
  • beforeCreateVersion
  • afterCreateVersion
  • onCreateVersion
  • calculateVersionLabel

 

It could be something like:

public class ObjectVersionBehaviour implements VersionServicePolicies.OnCreateVersionPolicy {
	
	private final NodeService nodeService;

	private final PolicyComponent policyComponent;

	public ObjectVersionBehaviour(final ServiceRegistry serviceRegistry,
			final PolicyComponent policyComponent) {	
		//TODO:: get the required service from serviceRegistry and 
		this.nodeService = serviceRegistry.getNodeService();
		this.policyComponent = policyComponent;
		
		//Binding on cm:content type
		createBindings(VersionServicePolicies.BeforeCreateVersionPolicy.QNAME, ContentModel.TYPE_CONTENT,
				Behaviour.NotificationFrequency.EVERY_EVENT);
	}

	private void createBindings(final QName policy, final QName type,
			final Behaviour.NotificationFrequency frequency) {
		policyComponent.bindClassBehaviour(policy, type, new JavaBehaviour(this, policy.getLocalName(), frequency));
	}
	
	@Override
	public void onCreateVersion(final QName classRef, final NodeRef versionableNode, 
				final Map<String, Serializable> versionProperties,
				final PolicyScope nodeDetails){
		//Your custom logic on version creation
	}
}

Refer this tutorial if want to learn more on implementing behaviors:

https://ecmarchitect.com/alfresco-developer-series-tutorials/behaviors/tutorial/tutorial.html

~Abhinav
(ACSCE, AWS SAA, Azure Admin)
akreienbring
Active Member

Re: Fire a rule only when the version changes

Jump to solution

Thank you!

As I already followed this helpful tutorial I was able to implement a custom behaviour that reacts on a change of the version.

Although I wonder why these events can not be populated to the rule definition site. That would make things easier for people not using an IDE. :-)

Please have a look at my "follow-up-problem" that I just posted here :