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

Friday 12 August 2016

Creating Liferay JSP hook with Liferay 7

Creating Liferay JSP hook with Liferay 7
Liferay 7 doesnt support the hook, as all of the bundles are generated using the OSGI. In OSGI you need to create a fragment and attach the same with the Orginal module. By this we can chnage the existing JSPs in the module.
Liferay IDE as of now doesnt supooort the direct creation of the hook.  So we need to create the hook project using blade and then we can go ahead and do rest of the stuff using the IDE.
Also point to keep in mind is that Liferay 7 GA2 is having a bug due to which theJSP hooks are not getting deployed in the liferay. For further information on that you can go ahead and have a look on this issue https://issues.liferay.com/browse/LPS-66814.
So in this tutorial we will be using the Liferay 7 GA 1.
Following is the command to create a blank project for JSP hook
blade create -t fragment -h <HOST_BUNDLE_SYMBOLIC_NAME> -H <HOST_BUNDLE_VERSION> <PROJECT_NAME>
HOW TO CREATE JSP HOOK IN LIFERAY 7 USING FRAGMENTS TEMPLATE
Step 1 -> Find the value for -h i.e the Host bundle Symbolic name. Open the GOGO shell  as in the below figure
And type lb
This will list down all of the modules/bundles running in that particular server. As we are working on the login jsp hook. we can find the login modules by writing the following command
lb | grep Login

Here the list of all of the Login modules. Now we are interested in the Login Web module.  Modules are listed in the following fashion
BUNDLE_ID | STATUS  |LEVEL |NAME (VERSION)
We can see that Lifeary Login Bundle with Bundle ID 371 is active and is of version (1.0.3).
So now we need to find out the  HOST_BUNDLE_SYMBOLIC_NAME and the HOST_BUNDLE_VERSION in order to create a fragment.
We can get the further details of the bundle by using the command b <BUNDLE_ID>
so we will punch in b 371 this will give us the complete details of the bundle.
We can see that first line is com.liferay.login.web_1.0.3[371], this will give the desired result that we are seeking.
com.liferay.login.web -> HOST_BUNDLE_SYMBOLIC_NAME
1.0.3 -> HOST_BUNDLE_VERSION
So now we are having all of the required details so that we can create a JSP hook for login portlet.
Goto the workspace's module folder  that you have created and punch in the following command.
F:\LR7\wokspace\FirstWorkspace\modules>blade create -t fragment -h com.liferay.login.web -H 1.0.3 my-login-jsp-hook

This will create a blank project in the module folder.
Now we need to copy down the JSPs from the login module which we want to change. To do that import the source  of liferay into eclipse and go to the
/portal-master/modules/apps/foundation/login/login-web/src/main/resources/META-INF/resources
Now copy down the required jsps and paste them into the project that we had just created.
So the result will be something like this

Now customize the changes in the login.jsp  and post that deploy the project


Once deployed you can see that the changes are reflected in the portal as shown below


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

RELATED POSTS

 

Copyright @ 2016 LiferayRevisited.