Create a portlet. So in your WEB-INF directory you will have the usual files liferay-display.xml, liferay-portlet.xml, portlet.xml, and web.xml. In my source folder I created a HooksPortlet.java file too. In the pages directory I created a view.jsp which for now just displays a message saying the portlet exists.
Create another class file (I called CreateAccountHook.java) and dump the following in to it:
package net.blah.hook.createaccount;
import com.liferay.portal.ModelListenerException;
import com.liferay.portal.model.BaseModel;
import com.liferay.portal.model.ModelListener;
import com.liferay.portal.model.User;
/**
* This hook intercepts the account creation process AFTER the account has been created.
*/
public class CreateAccountHook implements ModelListener {
public void onAfterCreate(BaseModel arg0) throws ModelListenerException {
User uzer = (User)arg0;
System.out.println("\n\t--------------- New user is uzer: "+uzer.getEmailAddress()+" with id of: "+uzer.getUserId()+"\n\n-------------------------");
}
public void onAfterRemove(BaseModel arg0) throws ModelListenerException { }
public void onAfterUpdate(BaseModel arg0) throws ModelListenerException { }
public void onBeforeCreate(BaseModel arg0) throws ModelListenerException { }
public void onBeforeRemove(BaseModel arg0) throws ModelListenerException { }
public void onBeforeUpdate(BaseModel arg0) throws ModelListenerException { }
public void onBeforeAddAssociation(Object arg0, String arg1, Object arg2) throws ModelListenerException { }
public void onAfterRemoveAssociation(Object arg0, String arg1, Object arg2) throws ModelListenerException { }
public void onAfterAddAssociation(Object arg0, String arg1, Object arg2) throws ModelListenerException { }
public void onBeforeRemoveAssociation(Object arg0, String arg1, Object arg2) throws ModelListenerException { }
}
Next create a file called liferay-hook.xml and dump the following into it:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hook PUBLIC "-//Liferay//DTD Hook 5.2.0//EN" "http://www.liferay.com/dtd/liferay-hook_5_2_0.dtd">
<hook>
<portal-properties>my-portal.properties</portal-properties>
</hook>
Note: You can call the .properties file whatever you want.
And lastly, the my-portal.properties file has to be in the WEB-INF/classes directory when it's delivered in the war. Place the following in the properties file:
value.object.listener.com.liferay.portal.model.User = net.blah.hook.createaccount.CreateAccountHook
Build the portlet and install it into Liferay. Create an account and you will see the system.out displayed in the console.
Enjoy!
Excellent this is what I've been looking for!
ReplyDeleteThank you,
Best regards,
Doug
http://dougmolineux.com
Awesome! Glad I could help!
ReplyDeleteThanks for the example, I have tough one question, for what do you need the HooksPortlet.java file?
ReplyDeleteThank's in advance,
Alex
Good example Easy to understand keep it up :)
ReplyDelete