Wednesday, December 23, 2009

Create Account Hook in Liferay... Dump the Ext Env!

Although I love the Extension Environment in Liferay, many of my colleagues do not so now that things have slowed down a bit I've been implementing more with hooks in v5.2.2. Ray Auge wrote some interesting information so that combined with the WOL portlet, I set out to hook some changes for creating an account. This was truly fast and easy (not to mention a god send) so here's a simple working example...

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!

4 comments:

  1. Excellent this is what I've been looking for!

    Thank you,

    Best regards,
    Doug

    http://dougmolineux.com

    ReplyDelete
  2. Thanks for the example, I have tough one question, for what do you need the HooksPortlet.java file?

    Thank's in advance,
    Alex

    ReplyDelete
  3. Good example Easy to understand keep it up :)

    ReplyDelete