google.com, pub-5747754088801811, DIRECT, f08c47fec0942fa0 Skip to main content

Model Listener Hook

                             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.
onAfterCreate 

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
It will show message on console

######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

Comments

  1. i'm using liferay 6.2 and the interface is make use of Generic framework i'm i write ???

    ReplyDelete
    Replies
    1. Yes ModelListener interface for Generic Frameworks. It can be use for any model of liferay. E.g JournalArticle etc

      Delete

Post a Comment

Popular posts from this blog

Disable cache content of browser access on back button after logout liferay dxp

Some time we have requirement where we do not want to allow back button after logout and show cached browser content. Liferay provide properties which with we are able to restrict to see content on back button after logout. If user click back button after logout it will show the login page. We require to provide below properties in portal-ext.properties. Restart the server once applying below properties browser.cache.signed.in.disabled=true

Liferay crud porrtlet with Jasper Reports.

I have used Meera Prince Crud operation demo for the Jasper reports which allow to export report in PDF , word, XLS format. It will look lie below image. Find Source code for the complete example on below location. LIFERAY JASPER REPORT INTEGRATION 1 I have available PhoneManger Crud Portlet which have pagination and crud functionality. 2 I create report-listing-mobile.jrxml  file with help of Ireport tool. <?xml version="1.0" encoding="UTF-8"?> <jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="report-listing" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin=&q