Select Page

Alfresco behaviors can be very useful when you need to perform some system-level action as part of an operation on a node. For example, let’s say whenever an instance of my custom type is created, I want to generate a unique id value and record it in a custom attribute on the new node. I can register a behavior that handles the onCreateNode event, bind it to my custom type, and then I can provide a simple Java method to do the work of generating the id value and setting the property. If you’re new to behaviors, Jeff Potts provides an in-depth discussion and tutorial.

I recently encountered an issue with behaviors where a custom onCreateNode behavior bound to a custom type doesn’t fire when a node of that type is created via CMIS, but works fine when a node is created by other means (e.g. the JavaScript API). Binding to a built-in type (e.g. cm:content) also works, even from CMIS. A description of this problem can be found in Alfresco’s JIRA repository, and a workaround is generously provided in this forum post.

The approach that the workaround takes is to bind the behavior not to a custom type, but to a built-in type like cm:content. Then, the code in the event handler checks the type of the node being created to see if it is an instance of the custom type.

While the workaround is basically correct, there is one additional check needed. In certain cases, such as when a thumbnail or preview is created by Share, it seems that Alfresco creates a temporary cm:content node.  When this happens, the node will have already been marked  deleted when the onCreateNode event fires, causing an exception to occur in the call to NodeService.getType(). Therefore, it is necessary to check the status of the node prior to calling getType() as follows:

	public void onCreateNode(ChildAssociationRef childAssocRef) {

		NodeRef newNode = childAssocRef.getChildRef();

		Status status = _nodeService.getNodeStatus(newNode);

		if (status != null && !status.isDeleted()) {
			if (_nodeService.getType(newNode).isMatch(Model.TYPE_INVOICE)) {

				// The node is of the right type and has not been deleted.  Take some action

			}
		}
    }

Pin It on Pinterest

Sharing is caring

Share this post with your friends!