Saturday 8 October 2016

How to Override the Language Properties file in Liferay 7 DXP

How to Override the Language Properties file in Liferay 7 DXP?
In this tutorial we are going to see how we can Override and change the Language Properties in Liferay 7. In the previous tutorials we had seen how we can write a portlet filter and how we can use model listeners in liferay.
In this tutorial we are going to Override the Sign In text to Please Sign In
To Override and change the Language Properties in Liferay 7 follow the following steps -:
The Language.Properties file resides in the poratl-impl.jar and also it can be found at the following location in the source code liferay-portal/portal-impl/src/content/Language.properties


In the first step copy the Language.properties file from liferay portal source or the portal-impl.jar file and paste it into the src/main/resources/content of any custom module.


Something like this




Once done with this we will have to create a new Component class.
               @Component(property = {
                            "language.id=en_US"
                                   }, service = ResourceBundle.class)


This class should extend java.util.ResourceBundle.  The complete implementation of the class can be as follows.





Now update the Language.properties file.
sign-in=Please Sign In


BEFORE LANGUAGE.PROPERTIES UPDATE



AFTER LANGUAGE.PROPERTIES UPDATE






For more information on the above topic you can visit my youtube channel



Friday 23 September 2016

How to Hook a liferay Action Command in Liferay 7 DXP?

How to Hook a liferay Action Command in Liferay 7 DXP?
In this tutorial we are going to see how we can hook an action command in liferay. In the previous tutorials we had seen how we can write a portlet filter and how we can use model listeners in liferay.
We are going to hook the LoginActionCommand in this tutorial.  
To hook a liferay action follow the following steps -:
First we need to find the name of the module for which we need to hook the action. In this case it is Login portlet.
To do this type lb | grep Login in the GOGO Shell command.
This will yield multiple results which contain the Login as the module name. Something like below.




Liferay login web is the one which we are interested in. We can see that the Liferay login Web is having the version 1.0.7. We will need this value going forward.
To view the detail of the above module, type the following command in the gogo shell comand.


b 220   this will give out the details of the above module. Something like below.


Pull out the compelete name of the module as in the above highlighted figure. which would be
com.liferay.login.web
We would be using this to import the Java classes.
From the details of the module extract the path that you need to hook. In our case it is /login/login


The details  are as follows
{com.liferay.portal.kernel.portlet.bridges.mvc.MVCActionCommand}={mvc.command.name=/login/login, javax.portlet.name=[com_liferay_login_web_portlet_FastLoginPortlet,com_liferay_login_web_portlet_LoginPortlet], component.name=com.liferay.login.web.internal.portlet.action.LoginMVCActionCommand, component.id=1156, service.id=610, service.bundleid=220, service.scope=bundle}


In bold are the details that we need to ovverride/hook the ActionCommand.


Once we are having the above details we can use these to create a class that will ovveride the ActionCommand. This can be included in any type of module while creating from eclipse.


We need to specify the above details in the properties of component


@Component(immediate = true, property = { "javax.portlet.name=com_liferay_login_web_portlet_FastLoginPortlet",
"javax.portlet.name=com_liferay_login_web_portlet_LoginPortlet", "mvc.command.name=/login/login",
"service.ranking:Integer=700" }, service = MVCActionCommand.class)

NOTE - : The service.ranking:Integer=700 should be higher number than that of service.id which was 610 in our case.


The complete code for the Hook is given below.



The result will be a message printed in the console.


For more information on the above topic you can visit my youtube channel

Tuesday 30 August 2016

How to create Portlet Filters in Liferay 7 DXP

How to write a Portlet Filter in Liferay 7 DXP?
In this tutorial we are going to see how we can write a portlet filter in liferay 7. Portlet Filter can generally be implemented in a class using any of the portlet. In short you can write a portlet filter for any portlet in any portlet project. It is not necessary that the portlet filter will needs to be implemented in its own portlet project.
To write a portlet filter we only need to know the name of the portlet that we need to write the filter for.
The portlet filters can be of following type -:
  • Render Filter
  • Resource Filter
  • Action Filter
So whenever any of the above type of Url is called from the controller one of the above filter is called depending upon the type of the filter. i.e if a URL of action type is called the Action Filter will be called and if Resource URL is called then the Resource filter will be called.
The Render Filter will implement the javax.portlet.filter.RenderFilter class and will implement the doFilter(RenderRequest request, RenderResponse response, FilterChain chain) method.
The ActionFilter will implement the javax.portlet.filter. ActionFilter class and will implement the doFilter(ActionRequest request, ActionResponse response, FilterChain chain) method.
The ResourceFilter will implement the javax.portlet.filter. ResourceFilter class and will implement the doFilter(ResourceRequest request, ResourceResponse response, FilterChain chain) method.


So the first step will be to define the class as a component whose service class will be PortetFilter.class, Next we need to provide the name of the portlet that can be done in the property. Define the name and component in the following manner.


@Component(
immediate = true,
property = {
"javax.portlet.name=<NAME_OF_THE_PORTLET>",
},
service = PortletFilter.class
)


So the complete implementation for the Render Method could be as follows.

And once you drag the portlet on to a page the following can be seen in the console log.





The code for action portlet filter will be


Monday 29 August 2016

How to use Model Listeners in Liferay 7 DXP

How to use Model Listeners in Liferay 7 DXP ?
As we know that in liferay 7 or DXP everything is a declared as a component/module so to add a model listener in Liferay 7 we need to define the class as a component.
We will be using the @Component class and to declare the component/module. The service class need to be defined as the ModelListener class. The service class defines the types under which to register this Component as a service.
This will allow us to define the model listener which we want to listen to.
@Component(immediate = true, service = ModelListener.class)
We will be extending the BaseModelListener class and overriding the required methods.
Below is the code that can be used to implement the model listener for User model.



If we create a new user using the create account page or from the control panel we would see that the desired model is getting called.



We can see that while a user is getting created all the model listener methods that we had overridden are getting called.

Friday 26 August 2016

How to hook Post Login and Pre Login actions or events in Liferay 7

How to Hook Login Events in Liferay 7 ?

In this tutorial we are going to learn how we can hook the Login POST and PRE actions in liferay 7. To hook the post login or pre login event we need to use the liferay's com.liferay.portal.kernel.events.LifecycleAction  class to process the events.

As we know that everything in liferay 7 is a component and we need to define this too as a component using the @Component annotation. In the component we  need to define the LifecycleAction.class as a service.  So the final @Component setup will look like this.

For Post Login Event we need to setup the @Component like this -:

@Component(
immediate = true, property = {"key=login.events.post"},
service = LifecycleAction.class
)

For Pre Login Event we need to setup the @Component like this -:

@Component(
immediate = true, property = {"key=login.events.pre"},
service = LifecycleAction.class

)

In both of the cases we need to implement the method processLifecycleEvent(LifecycleEvent lifecycleEvent) .

So below is the code snippet for both of the events.

Code for LoginPostAction


Code for LoginPreAction




Tuesday 23 August 2016

How to create a schedular in lifery 7

How to create a scheduler in liferay7

Prior to liferay7 we use to create a scheduler using the liferay-portlet.xml entry. But in Liferay 7 we are not having liferay-portlet.xml and hence we need to look into some other option. In Liferay 7 we can create a schedular using the BaseSchedulerEntryMessageListener class.
The baseSchedularEntryMessageListener class will be used to register a scheduler listener, which will in turn utilize the SchedularEngineHelper to register the scheduler.
To create a scheduler in liferay 7 use the following steps.
First create a class in any of the project module previously created.
Register this class as a component using the @Component annotation.
Override the doReceive(Message message) method. This method will actually run the business logic.
To register the scheduler we need to register the scheduler using the @activate annotation. This annotation's method will be called while activating the module and hence this can be used to register the scheduler in SchedularEngineHelper.
Also to deactivate this scheduler we will use the @Deactivate annotation. This will be called when we want to uninstall this module and hence this can be use to unregister this scheduler.
To define a trigger we will be using the class TriggerFactoryUtil. We can create two type of triggers - SIMPLE and CRON. Here we are going to create a simple trigger.



TriggerFactoryUtil.createTrigger(getEventListenerClass(), getEventListenerClass(),5, TimeUnit.SECOND);
The above scheduler will run every 5 seconds.

The complete code for the scheduler is given below. 



For more details on the above topic please go through the below video on my youtube channel.


RELATED POSTS

Sunday 21 August 2016

Overriding liferay services in Liferay 7

How to customize liferay services in Liferay 7/Overriding liferay services in liferay 7

How to override the liferay services?
As we used to work in the LR 6.2, we can override the liferay services using the servicewrapper template of blade CLI.

Following are the steps to do so.
First we will be creating a new Liferay Module as shown below.
Fill in the Name of the project and select the template as servicewrapper now click next.
In the next step fill in the package and the your custom class. Click on the button next to service and select the service wrapper that you need to override. In our case we are going to override the UserLocalServiceWrapper.

Override the required methods in the MyUserLocalServiceOverride and deploy the project using gradel deploy.

Now try to login and you can see that the methods are being called and system trace is getting printed out.



For more details on the above topic please go through the below video on my youtube channel.

RELATED POSTS

 

Copyright @ 2016 LiferayRevisited.