Developing Hook Using Maven And Step By Step Liferray Hook System Capabilities | Xplantr

Developing Hook using maven and Step by Step Liferay Hook system capabilities.

I am assuming that you have your Liferay maven based development environment is ready. If not, I would recommend to read out this simple-way-to-setup-liferay-environment-through-maven. This is pretty straight forward and lovely pitch of guideline. So now you have your environment ready and the directory structure looks like the following image. Yes, we can now start developing a sample hook to achieve some functionalities.

Figure: Liferay Maven based development directory structure.

Project Environment: Linux Liferay 6.x+ Maven 2

Create Hook

Lets goto your hook directory from command prompt (i.e. cd /home/workspace/example/liferay/src/hook )

Type command mvn archetype:generate

A list of archetype will come up.

Find liferay-hook-archetype from the list and check the number for the same archetype. You will get something like below but number could be changed based on the version.

21: remote -> com.liferay.maven.archetypes:liferay-ext-archetype (Provides an archetype to create Liferay extensions.)

22: remote -> com.liferay.maven.archetypes:liferay-hook-archetype (Provides an archetype to create Liferay hooks.)

23: remote -> com.liferay.maven.archetypes:liferay-layouttpl-archetype (Provides an archetype to create Liferay layout templates.)

24: remote -> com.liferay.maven.archetypes:liferay-portlet-archetype (Provides an archetype to create Liferay portlets.)

In next step, you need to select Liferay Version. So select version accordingly (i.e. 6.1.0)

Next, define proper groupId (i.e. com.example.liferay)

Define proper artifactId (i.e. samplehook)

Specify version (i.e. 1.0-SNAPSHOT)

Package(i.e. com.example.liferay)

Confirm all the values and hit ‘Y’ for confirmation

Yahooooooooo , Your are done !!! Your Hook is created. Now you know how to create a hook in a minute, great! (y)

Before deploying your package, just confirm your Portal deploy path into your auto generated pom.xml inside your sample hook ( /home/workspace/example/liferay/src/hook/sample-hook/pom.xml )

Open the pom.xml in a text editor and find the …………..

/home/workspace/example/liferay/bundle/deploy
6.1.0
/src/main/webapp/WEB-INF/liferay-hook.xml and open that file in an editor and the add /META-INF/custom_jsps

So it will look like the following.

/META-INF/custom_jsps

Now copy any blog portlet jsp file, for example – I want to override edit_entry.jsp, and put that into /src/main/webapp/META-INF/custom_jsps/html/portlet/blogs

Change anything your want into that file and deploy the hook. You would see that the default look and feel got changed if u change anything there. You are done with JSP overriding. Note : If you want to know what Liferay did with the original file, then goto your
/webapp/ROOT/WEB-INF/html/portlet/blogs/ and u can find that the original file renamed edit_entry.portal.jsp Portal Properties


Well, if you want to override any portal property value, you need to add
portal.properties into your liferay-hook.xml like the following.


/META-INF/custom_jsps
portal.properties


Now create a portal.properties into your src/main/resources and change the value. For example – by default terms.of.use.required=true into liferay. 
You can put that into your hook portal.properties and change it to false.

# # Set this to true if all users are required to agree to the terms of use.

# terms.of.use.required=false

Deploy the hook and check the term’s page. You might need to restart the tomcat if u dont see its not effecting. Event Liferay has a few event handler connection points throughout its lifecycle.

The available events are: Application Startup Events (application.startup.events)

Login Events (login.events.pre, login.events.post)

Service Events (servlet.service.events.pre, servlet.service.events.post)

Now I want to override any event with my custom implementation, for example – application.startup.events, where I want to change some default behavior. So I can put that property into src/main/resources/portal.properties and override the default value. Example : In liferay source, the default value of application.startup.events property is application.startup.events=com.liferay.portal.events.AppStartupAction... I can change that to custom one like the following into

portal.properties application.startup.events=com.liferay.samplehook.events.StartupAction

package com.liferay.samplehook.actions;

import com.liferay.portal.kernel.dao.orm.QueryUtil;

import com.liferay.portal.kernel.events.ActionException;

import java.io.InputStream; import java.util.*;

/** * Action called on startup, used to create initial data.

* * @author Rony * */

public class StartupAction extends SimpleAction {

public void run(String[] ids) throws ActionException {

try {

doRun(GetterUtil.getLong(ids[0])); }

catch (Exception e) { throw new ActionException(e); } }

// Do your custom coding here. }

Do your customization in AppStartupAction, deploy it and restart the tomcat. Or you can also define override event using liferay-hook.xml like the following :

/META-INF/custom_jsps
portal.properties com.liferay.samplehook.ServicePreAction servlet.service.events.pre

Now you know two ways/process to override liferay events. Done! Overriding a Portal Service Liferay provide dummy wrapper classes for all of its services, for example: 

BlogsEntryLocalServiceWrapper is created as a wrapper of the BlogsEntryLocalService that is used to add, remove and retrieve blog entry. To modify the functionality of BlogsEntryLocalService from our hook, all we have to do is create a class that extends from BlogsEntryLocalServiceWrapper, override some of its methods or what ever you want, then we have to configure so that Liferay pick this extension instead of default wrapper class.

Sample code

// package com.liferay.samplehook.service;

import com.liferay.portal.kernel.exception.PortalException;

import com.liferay.portal.kernel.exception.SystemException;

public class CustomBlogsEntryLocalServiceImpl extends CustomBlogsEntryLocalServiceWrapper{

//Add custom coding }

Next, lets ask Liferay to pick this class instead of the default one. Edit your liferay-hook.xml and add the tags like the follow. Deploy the hook and restart the tomcat and check the effect. /META-INF/custom_jsps
portal.properties com.liferay.samplehook.ServicePreAction servlet.service.events.pre com.liferay.portal.service.BlogsEntryLocalService com.liferay.samplehook.service. CustomBlogsEntryLocalServiceImpl We are done and we can follow this technique to override other Liferay services.

Model Listeners Overriding default Model Listener: In Liferay, we have some model listeners and we need to override them for sometimes. 

In hook portal.properties, we can define our own class to override them.

Lets take LayoutSetListener as an example and we want to set our own theme for all community layouts.

In Liferay portal.properties, we have a Listener class like this: value.object.listener.com.liferay.portal.model.LayoutSet=com.liferay.portal.model.LayoutSetListener

Now lets define our own class into Hook portal.properties to customize the layout like this: value.object.listener.com.liferay.portal.model.LayoutSet= com.liferay.samplehook .modellisteners.LayoutSetListener Example code to add custom theme for all group layouts by default instead of default Classic theme.

public class LayoutSetListener extends BaseModelListener {

public void onAfterCreate(LayoutSet layoutSet) throws ModelListenerException {

try { Group group = GroupLocalServiceUtil.getGroup(layoutSet.getGroupId());

if (group.isCommunity()) { addCommunityLayouts(group); }

}

catch (Exception e) { throw new ModelListenerException(e); } }

protected void addCommunityLayouts(Group group) throws Exception { // Look and Feel updateLookAndFeel(group); }

private void updateLookAndFeel(Group group) throws Exception { LayoutSetLocalServiceUtil.updateLookAndFeel(group.getGroupId(), “sample_WAR_sampletheme”, “”, “”, false); } }

We are done adding our own theme overriding LayoutSet model.

Add custom Model Listener: If we want to add our own Model Listener, we can also easily achieve that using hook system. Lets say we want to add a blog post Listener, we can inform liferay to pickup our custom model by adding a tag into the hook liferay-hook.xml file. So your liferay-hook.xml will look like this: 

/META-INF/custom_jsps
portal.properties com.liferay.samplehook.ServicePreAction servlet.service.events.pre com.liferay.portal.service.BlogsEntryLocalService com.liferay.samplehook.service.
CustomBlogsEntryLocalServiceImpl com.liferay.samplehook .modellisteners.MyBlogEntryListener com.liferay.portlet.blogs.model.BlogsEntry

public class MyBlogEntryListener implements ModelListener { //Custom code here. }

You can also Look into the post for detail at http://www.liferay.com/community/wiki/-/wiki/Main/Portal+Hook+Plugins

We are done! Overriding/Adding struts actions We can also override and add new Struts-actions from Hook System. Edit your liferay-hook.xml and add the following lines.

/META-INF/custom_jsps
portal.properties com.liferay.samplehook.ServicePreAction servlet.service.events.pre com.liferay.portal.service.BlogsEntryLocalService com.liferay.samplehook.service. CustomBlogsEntryLocalServiceImpl com.liferay.samplehook .modellisteners.MyBlogEntryListener com.liferay.portlet.blogs.model.BlogsEntry /portal/sample com.liferay.samplehook.action.SampleStrutsAction This has been explained in detail at http://www.liferay.com/web/mika.koivisto/blog/-/blogs/7132115 Language Bundles We can also add new language package or override existing translation from the hook like the following way. /META-INF/custom_jsps
portal.properties content/Language_en.properties content/Language_nl.properties content/Language_fi.properties com.liferay.samplehook.ServicePreAction servlet.service.events.pre com.liferay.portal.service.BlogsEntryLocalService com.liferay.samplehook.service. CustomBlogsEntryLocalServiceImpl com.liferay.samplehook .modellisteners.MyBlogEntryListener com.liferay.portlet.blogs.model.BlogsEntry /portal/sample com.liferay.samplehook.action.SampleStrutsAction

Great,now we know how to develop a hook using Maven what we can achieve from Liferay Hook system at a glance.