In this ‘How to?’ series of posts, we discussed about JSP hook last time. In this post, I am going to describe about Model Listener Hook and how to implement it in Liferay 7/DXP.
Following are the list of hooks available in Liferay 7/DXP and we have already discussed about hyperlinked item in list.
Let’s first understand model when listener hooks are useful.
Model Listener hook are designed to listen for events performed during changes in Model object. Ex. on delete of user from Liferay, you may want to perform some business logic or other desire operations.
Following are the list of events Liferay supports for model listener hook,
Unlike previous versions of Liferay, DXP does not support XML bases configuration. During implementation exercise, we will understand substitute of XML based configuration in Liferay DXP/7.
Here I assume that you have already read previous posts and you have your development environment and other prerequisite configured to start with model even listener hook.
In this post, I have taken example to demonstrates how model listener can be implemented for user delete event. Let’s understand steps to configure model listener as follow,
To create a Liferay Workspace in IDE follow the following steps:
Select File menu → click New → select Liferay Workspace Project. Upon creation of the workspace, the structure should look like below:
Following the following steps to create Liferay Module Project in workspace.
Open File Menu → Click New → Select Liferay Module Project and Provide project name as ModelListenerHook. Select run time name accordingly and click Next. Please see following snapshot for reference,
As you see in following snapshot, provide appropriate Component Class name and package. For this example I am providing class name as UserModelListener and package as com.enprowess.model.listener,
On click of Finish, you should see folder structure as following snapshot,
Component class must extend BaseModelListener<User> abstract class. Provide appropriate generic based on model listener you are planning to implement. In this example, I am adding model listener for User class so I have mentioned ‘User’ as genetic.
Now override ‘OnAfterRemove()’ method to add logger. Here in this example I am printing log on User delete event. Please see following screenshot for reference,
Now add component annotation block in UserModelListener class to mark it as model listener component as follow,
With above steps, you have successfully implemented Model listener hook. You can deploy this hook in Liferay DXP/7 and check log on user delete event. In my next blog, I will talk about another implementation in Liferay 7/DXP. Till then happy learning!