12/29/15

Overriding class or bean in Alfresco

Bean Overriding in Alfresco

Hi Readers

Many a times you require to override Alfresco's default functionality which is written in form of a class or bean.
In Alfresco most of the configuration changes can be done by alfresco-global.properties file only. But many a times for more advanced customization requirements such as plugging in custom code components, Alfresco provides a mechanism for supplementing and overriding the actual Spring bean definitions.

Lets take an example to override Alfresco's Email functionality which is written as a class and loaded into Alfresco as bean.

Default Email functionality is written in WorkflowNotificationUtils class in alfresco. To modify that we will create a new class as CustomWorkflowNotificationUtils. And to override existing functionality CustomWorkflowNotificationUtils needs to extend WorkflowNotificationUtils.

You can create this custom class in package com.xseed.alfresco.util;

Your CustomWorkflowNotificationUtils class will be


public class CustomWorkflowNotificationUtils extends WorkflowNotificationUtils {


 public void sendWorkflowAssignedNotificationEMail(String taskId, String taskTitle, String description, Date dueDate, Integer priority, NodeRef workflowPackage, String[] assignedAuthorites, boolean pooled) {

 System.out.println("//*********** This Function is overridden in class CustomWorkflowNotificationUtils ***********//");

 }

}


Now you need to create a bean of CustomWorkflowNotificationUtils class. By default Alfresco will load WorkflowNotificationUtils bean with id="workflowNotification". So to override this bean with bean of your new class, you need to define a new bean with same id.

As per Alfresco docs you can define your custom beans Globally in dev-context.xml. So create a dev-context.xml file at $TOMCAT_HOME/shared/classes/alfresco/extension/ if not already exists.

Define your bean in dev-context.xml


<?xml version='1.0' encoding='UTF-8'?>
DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>

<beans>

 <bean id="workflowNotification" class="com.xseed.alfresco.util.CustomWorkflowNotificationUtils">
  <property name="workflowService" ref="workflowServiceImpl"/>
  <property name="nodeService" ref="NodeService"/>
  <property name="notificationService" ref="NotificationService"/>
 </bean>

</beans>


Restart the Alfresco server to load your new bean and class.

Now every time alfresco uses send email functionality. It will use your custom overridden class.
You can verify this on console by seeing a message

//*********** This Function is overridden in class CustomWorkflowNotificationUtils ***********//