Back end customizations in LR7

Liferay 7 has been developed keeping modularity as the soul and core of development. This feature of Liferay 7 has made it quite usable and customizable. This modularity has provided liferay developers, the facility that they can customize(override) any back end functionality quite easily. In Liferay 7 you do not need to create any separate hooks, any xml-entries(in almost all the cases) whether you want to use any Lifecycle events(pre-login, post-login etc), service wrappers, Model Listeners, create or override any MVC command, StrutsActions etc. All of these have become quite easy and straight forward now.

For all of these things you just need to create your custom source class and define some properties. If you are creating something new for example Model Listeners, Lifecycle events, service wrappers, MVC command etc you just need to do:

a) Create a custom source file.
b) Define some properties

Let's take a few examples

1. Post-login event
    @Component(
        immediate = true, property = {"key=login.events.post"},         //specifies post-login event
        service = LifecycleAction.class                                              //specifies Lifecycle implementation
    )
    public class PostLoginLifecycleAction implements LifecycleAction{
                      // your functionality here        
    }

Check this blade sample for more on how pre-login or post-login events are implemented.

 

2. ModelListener

@Component(
        immediate = true,
        service = ModelListener.class                                              //specifies ModelListener Implementation
    )
    public class SampleUserModelListener extends BaseModelListener<User> {
                       // your functionality here        
    }

Check this blade sample for more on how model listeners are implemented.

 

3. MVC Commands

You need to create different source files for each MVC Command and that command will be mapped as:


@Component(
        property = {
            "javax.portlet.name=your_portlet_name_id",               //your portlets name
            "mvc.command.name=/view",                                      //your mvc command defined in jsp
            },
        service = MVCRenderCommand.class)   

// If creating MVCRenderCommand. Use MVCActionCommand.class if creating functionality of Action Command

 

In jsps define mvccommands as:

MVCRenderCommand:

<portlet:renderURL var="view">
      <portlet:param name="mvcRenderCommandName" value="/view" />
</portlet:renderURL>

MVCActionCommand:

<portlet:actionURL name="mvcActionCommandName" var="editSample" >
</portlet:actionURL>

Check this Overriding MVC Commands for more on how MVCCommands are implemented.

 

Existing source customization

1.Create a new source file.
2.Go to original file, copy all @Component properties into your new source file. add new property "service.ranking:Integer=100" inside properties of @Component part with a value higher than original file as following (I am overriding com.liferay.journal.exportimport.data.handler.JournalPortletDataHandler here):

@Component(
        property = {"javax.portlet.name=" + JournalPortletKeys.JOURNAL",
                "service.ranking:Integer=10"
                },
        service = PortletDataHandler.class
    )

Check this original JournalPortletDataHandler.

Only service.ranking:Integer=10 is added to tell OSGI that my ranking is higher. Now OSGI will pick this file at run time.

Code your required functionality into this file and done.

 

Doing all customization was quite easy and we just needed a pure java source file, no xmls were needed in above cases.smileyyes

Blogs