Liferay Hook provide facility to create listener on particular model. We require to implement implements ModelListener on our custom class. Lets create one Hook which allow to do action on the various activity on particular model action.
- Create one hook.(e.g. UserListener-hook)
- Add portal properties inside liferay-hook.xml file under web inf.
<?xml version="1.0"?>
<!DOCTYPE hook PUBLIC "-//Liferay//DTD Hook 6.2.0//EN" "http://www.liferay.com/dtd/liferay-hook_6_2_0.dtd">
<hook>
<portal-properties>portal.properties</portal-properties>
</hook>
- It will create portal.properties inside src folder.
- Add below property inside portlet.properties file
value.object.listener.com.liferay.portal.model.User=com.sa.CreateAccountHook
In above property we have created listener for the User model
and we will implements ModelListener on our custom class in my case it is com.sa.CreateAccountHook.
- Now create one custom class in your package which provide in portlet.properties.
My case the package is com.sa
class CreateAccountHook.
- We will implements ModelListener on that. After implementing its method it will show like below.
package com.sa;
import com.liferay.portal.ModelListenerException;
import com.liferay.portal.model.ModelListener;
import com.liferay.portal.model.User;
public class CreateAccountHook implements ModelListener {
@Override
public void onAfterAddAssociation(Object arg0, String arg1, Object arg2)
throws ModelListenerException {
}
@Override
public void onAfterCreate(Object arg0) throws ModelListenerException {
}
@Override
public void onAfterRemove(Object arg0) throws ModelListenerException {
}
@Override
public void onAfterRemoveAssociation(Object arg0, String arg1, Object arg2)
throws ModelListenerException {
}
@Override
public void onAfterUpdate(Object arg0) throws ModelListenerException {
}
@Override
public void onBeforeAddAssociation(Object arg0, String arg1, Object arg2)
throws ModelListenerException {
}
@Override
public void onBeforeCreate(Object arg0) throws ModelListenerException {
}
@Override
public void onBeforeRemove(Object arg0) throws ModelListenerException {
}
@Override
public void onBeforeRemoveAssociation(Object arg0, String arg1, Object arg2)
throws ModelListenerException {
}
@Override
public void onBeforeUpdate(Object arg0) throws ModelListenerException {
}
}
- The each method will invoke after certain event on the User model. So for test the above listener lets add some code in the methods.
package com.sa;
import com.liferay.portal.ModelListenerException;
import com.liferay.portal.model.ModelListener;
import com.liferay.portal.model.User;
public class CreateAccountHook implements ModelListener {
@Override
public void onAfterAddAssociation(Object arg0, String arg1, Object arg2)
throws ModelListenerException {
}
@Override
public void onAfterCreate(Object arg0) throws ModelListenerException {
User user = (User) arg0;
System.out.println("\n\t--------------- New user is user: "
+ user.getEmailAddress() + " with id of: " + user.getUserId()
+ "\n\n-------------------------");
}
@Override
public void onAfterRemove(Object arg0) throws ModelListenerException {
System.out.println("######USER REMOVED SUCCESSFULLY########");
System.out.println("######USER REMOVED SUCCESSFULLY########");
System.out.println("######USER REMOVED SUCCESSFULLY########");
System.out.println("######USER REMOVED SUCCESSFULLY########");
System.out.println("######USER REMOVED SUCCESSFULLY########");
System.out.println("######USER REMOVED SUCCESSFULLY########");
}
@Override
public void onAfterRemoveAssociation(Object arg0, String arg1, Object arg2)
throws ModelListenerException {
}
@Override
public void onAfterUpdate(Object arg0) throws ModelListenerException {
}
@Override
public void onBeforeAddAssociation(Object arg0, String arg1, Object arg2)
throws ModelListenerException {
}
@Override
public void onBeforeCreate(Object arg0) throws ModelListenerException {
}
@Override
public void onBeforeRemove(Object arg0) throws ModelListenerException {
}
@Override
public void onBeforeRemoveAssociation(Object arg0, String arg1, Object arg2)
throws ModelListenerException {
}
@Override
public void onBeforeUpdate(Object arg0) throws ModelListenerException {
}
}
- There are two methods we have added code inside it.
The method will invoke after Creating User.
onAfterRemove
The method will invoke after removing User.
- Now Deploy our portlet. Create User in Liferay portal
It will invoke onAfterCreate method.
It will show message on console
--------------- New user is user: test1@gmail.com with id of: 11609
-------------------------
-------------------------
- Now delete any User.
It will invoke onAfterRemove
######USER REMOVED SUCCESSFULLY########
######USER REMOVED SUCCESSFULLY########
######USER REMOVED SUCCESSFULLY########
######USER REMOVED SUCCESSFULLY########
######USER REMOVED SUCCESSFULLY########
######USER REMOVED SUCCESSFULLY########
- We have implemented inbuilt User object model listener similarly we can implement for the Role and other model.
- You can download source code from below link.
https://drive.google.com/file/d/0B9B1NsG0lyx6NC01eVJpOHllMm8/view?usp=sharing
Great!
ReplyDeletei'm using liferay 6.2 and the interface is make use of Generic framework i'm i write ???
ReplyDeleteYes ModelListener interface for Generic Frameworks. It can be use for any model of liferay. E.g JournalArticle etc
Delete