Custom Behavior in Alfresco

In Previous article I have explain about Behavior and Policies in Alfresco. Now in this Article I am going to explain how to implement Custom Behavior with one example. We have a requirement where we want to keep track of number of time a document is viewed in this case we cannot use any rule we are going to use behavior . We will have one property which will keep track of number of time documents is viewed value of that property will be incremented each time anyone view the document. We can achieve this by following below steps.

Step 1:

Create custom aspect with Custom property No of Document Views. If you are not aware about custom content model you can refer my previous articles to implement this.

“custom:docviewno”

Step 2:

Identify the policy which needs to be associated with behavior to achieve our goal. In this case we need to invoke our code each time document is viewed and for this operation we can use this policy.

Interface: org.alfresco.repo.content.ContentServicePolicies

Method: onContentRead

Step 3:

Write Custom Behaviour

public class ContentViewTracker

implements ContentServicePolicies. onContentRead, {

// Dependencies

private NodeService nodeService;

private PolicyComponent policyComponent;

// Behaviours

private Behaviour onContentRead;

Generally this class has two dependencies that Spring will handle for us. One is the NodeService which will be used in the average calculation logic and the other is the PolicyComponent which is used to bind the behavior to the policies.

Step 4:

Add policy binding logic in the Behavior class’s init method

public void init() {

// Create behaviours

this. onContentRead = new JavaBehaviour(this," onContentRead ",

NotificationFrequency.EVERY_EVENT);

// Bind behaviours to node policies

this.policyComponent.bindPropertyBehaviour (

Qname.createQName(NamespaceService.ALFRESCO_URI,"onContentRead"),

Qname.createQName(“custom.model”,“docviewno”),this.onContentRead);

Step 5:

Implement business logic of incrementing the “custom:docviewno” property value by one inside onContentRead Method

Step 6:

Configure custom behavior class as a spring bean so that it will be registered with Alfresco. For that create custom-behaviour-context.xml and add following entry in to it.

<bean id="customBehavior" initmethod="init">

<property name="nodeService">

<ref bean="nodeService" />

</property>

<property name="policyComponent">

<ref bean="policyComponent" />

</property>

</bean>

Step 7:

Deploy everything into alfresco

Extension files will go under <ALF_HOME>/tomcat/share/classes/alfresco/extension

All java classes can be compiled and bundled in to jar and will go under

<ALF_HOME>/tomcat/webapps/alfresco/WEB-INF/lib

This is how you can create your custom behavior hope this article helps you in implementing your requirements.

Looking for quality Alfresco Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , | Posted under Alfresco | RSS 2.0

Author Spotlight

mitpatoliya

mitpatoliya

I love opensource technologies working with those technologies from the time I have stepped in to the Software Industry. Alfresco CMS is my area of expertise. I have worked on various complex implementations which involved integration of Alfresco with other technologies, extensively worked with JBPM workflows and Webscripts.

2 Comments on Custom Behavior in Alfresco

  1. N26 says:

    I set up the following Behaviour

    public void init()
    {
    // Create behaviours
    this.onUpdateProperties = new JavaBehaviour(this, “onUpdateProperties”, NotificationFrequency.TRANSACTION_COMMIT);
    // Bind behaviours to node policies
    this.policyComponent.bindPropertyBehaviour(NodeServicePolicies.OnUpdatePropertiesPolicy.QNAME, ContentModel.ASPECT_TITLED, this.onUpdateProperties);
    }

    @Override
    public void onUpdateProperties(NodeRef nodeRef, Map before, Map after)
    {

    }
    And the method this.onUpdateProperties is never called when any property of the ASPECT_TITLED is modified. Is there something that I am doing incorrectly?

    • mitpatoliya mitpatoliya says:

      I assume you are deploying your class properly.
      Have you checked custom aspect example of alfresco sdk? It is using same policy that will be good starting point.

Leave a Reply

Your email address will not be published. Required fields are marked *


2 + = 3

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>