There are various instances when we had a requirement in Alfresco in which we need to do certain operation based on particular events like node creation, node deletion etc… First Obvious solution which comes to our mind is Rules. Create rules with business logic embedded in it and invoke those rules when event occur. But it has certain limitations like it cannot be bind with certain service APIs or certain Events like download and view content. For instance we want to keep track of number of times document is viewed we cannot use any rule to meet this requirement. Do not worry we have a feature called behavior which comes to rescue.
Behaviors are used throughout Alfresco. Auditing and versioning are examples where behaviors are involved. In one case we needed to default some metadata values using logic that couldn’t be expressed using the Alfresco content model so we wrote a custom behavior that set the metadata appropriately. In another, we needed a way to synchronize metadata between folders and the items in those folders so we wrote a custom behavior to handle the sync. Like this Alfresco behaviors have come in handy several times in our various implementations.
Next obvious question which comes to our mind is how to bind this behavior with those events and APIs? We have concept called Policies which can serve the purpose. Policies are nothing but a set of Interfaces which provide way to bind behavior to APIs or events. Each of those interfaces contains inner interfaces that represent the policies you can hook into
This is brief description about Behavior and Policies in Alfresco in upcoming articles We will see how to create one in Alfresco till then you can try it yourself.