Tuesday, April 14, 2015

Liferay Inter Portlet Communication with Public Render Parameters

Inter Portlet Communication shortly we can call IPC is the way of making communication among different Portlets which are in same page or reside in other pages.

In the IPC mechanism Portlet will share data among the Portlets and Portlet will work based on the other Portlet.

JSR 168/Portlet 1.0 there is no better Inter Portlet Communication  later in JSR 286/ Portlet 2.0 have added capability of better Inter Portlet Communication with different ways.

Public Render Parameters are the one of the way to make Inter Portlet Communication among the Portlets and these Portlets may be in same page or different pages in the portal.

Public Render Parameters we will define specific parameter in the Portlet with help of that data will be carried from one Portlet to other Portlet and these Public Render Parameters accessible by specific Portlet where we already defined these parameters.

IPC we have two categories of Portlets Sender Portlet and Receiver Portlets. Sender Portlet will send some data or push some data then receiver Portlet will receive the data and will process 
subsequent steps.

Implement Public Render Parameters IPC

Assume we have sender Portlet and receiver Portlets from sender Portlet we will send user email address and send it to receiver Portlet once receiver Portlet get email address then it will show full details of user in the receiver Portlet.

The following are implementation steps

Step: 1

To make Inter Portlet Communication among all Portlet which are reside in same page or across the all the pages we need to update following portal property in portal-ext.properties file.


portlet.public.render.parameter.distribution=layout-set


Default value for portlet.public.render.parameter.distribution is layout and if we not updated this property then IPC will work among Portlets and which are in the same page

Previous versions of Liferay you may use following property



portlet.public.render.parameter.distribution=ALL_PORTLETS


Note:

As we know that portal-ext.properties file was in Liferay Home directory or we can Application Server Parent Directory.

Step: 2

We need to define public render parameter configuration in the sender Portlet of portlet.xml file
Assume we have two Portlets Portlet A and Portlet B and assume Portlet A is sender Portlet then we need to define public render parameter configuration in Portlet A of Portlet.xml file.

We need to define public render parameter so that whole communication will be taken care by defined public render parameter.

The following is configuration should be in portlet.xml file


<portlet-app>
<portlet>
.......
<supported-public-render-parameter>userEmailAddress</supported-public-render-parameter>
........
</portlet>
<public-render-parameter>
<identifier>userEmailAddress</identifier>
<qname xmlns:x="http://liferaysavvy.com/userEmailAddress">x:userEmailAddress</qname>
</public-render-parameter>
</portlet-app>


Note:

Please place the xml tags in right position

Step: 3

Now we need to set value for Public Render Parameter in the sender Portlet processAction(--) method or our Custom Action Method and this value will be received by the receiver Portlet.

The following is sample code snippet in the “Custom Action Method”


public void getData(ActionRequest actionRequest,
ActionResponse actionResponse) throws IOException, PortletException, SystemException {

String userEmailAddress = ParamUtil.getString(actionRequest,"userEmailAddress");
actionResponse.setRenderParameter("userEmailAddress", userEmailAddress);

}


Step: 4

We also need to define public render parameter configuration in Receiver Portlet. The parameter identifier and configuration we chosen in the Sender Portlet same configurations need to be replicate in the Receiver Portlet of portlet.xml file.

The following is configuration should be in portlet.xml file


<portlet-app>
<portlet>
.......
<supported-public-render-parameter>userEmailAddress</supported-public-render-parameter>
........
</portlet>
<public-render-parameter>
<identifier>userEmailAddress</identifier>
<qname xmlns:x="http://liferaysavvy.com/userEmailAddress">x:userEmailAddress</qname>
</public-render-parameter>
</portlet-app>


Note:

 Please place the xml tags in right position

Step: 5

Finally we need to get public render parameter value in the Receiver Portlet that already set in the Sender Portlet. We already know parameter was identified by unique name so that same name we have to use to get the value.

The following is sample code in Receiver Portlet “ view.jsp”  page


String userEmailAddress=ParamUtil.getString(renderRequest,"userEmailAddress",null);


We can remove public render parameter from Portlet with following code.


renderResponse.removePublicRenderParameter("userEmailAddress");


Download Portlet Public Render Parameters IPC Portlets


Sender Portlet Screen



Receiver Portlet Screen


Inter Portlet Communication between Portlets in the page


Complete Code Example

Sender Portlet

Portlet.xml file (/PublicRenderParametersIPCSender-portlet/docroot/WEB-INF/portlet.xml)


<?xml version="1.0"?>
<portlet-app xmlns=
"http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=
"http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" version="2.0">
<portlet>
<portlet-name>
Public Render Parameters IPC Sender Portlet
</portlet-name>
<display-name>
Public Render Parameters IPC Sender Portlet
</display-name>
<portlet-class>
com.meera.liferay.ipc.PublicRenderParametersIPCSenderAction
</portlet-class>
<init-param>
<name>view-template</name>
<value>/html/jsps/view.jsp</value>
</init-param>
<expiration-cache>0</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
<portlet-mode>view</portlet-mode>
</supports>
<portlet-info>
<title>Public Render Parameters IPC Sender Portlet</title>
<short-title>
Public Render Parameters IPC Sender Portlet
</short-title>
<keywords></keywords>
</portlet-info>
<security-role-ref>
<role-name>administrator</role-name>
</security-role-ref>
<security-role-ref>
<role-name>guest</role-name>
</security-role-ref>
<security-role-ref>
<role-name>power-user</role-name>
</security-role-ref>
<security-role-ref>
<role-name>user</role-name>
</security-role-ref>
<supported-public-render-parameter>
userEmailAddress</supported-public-render-parameter>
</portlet>
<public-render-parameter>
<identifier>userEmailAddress</identifier>
<qname xmlns:x="http://liferaysavvy.com/userEmailAddress">
x:userEmailAddress</qname>
</public-render-parameter>
</portlet-app>


View.jsp (/PublicRenderParametersIPCSender-portlet/docroot/html/jsps/view.jsp)


<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet"%>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme"%>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui"%>
<portlet:defineObjects />
<liferay-theme:defineObjects />
<portlet:actionURL var="sendDataActionURL" windowState="normal"
name="getData">
</portlet:actionURL>
<h1>Public Render Parameters IPC Sender Portlet</h1>
<aui:form action="<%=sendDataActionURL%>" method="post" name="smsForm">
<aui:input name="userEmailAddress" id="userEmailAddress" label="User Email Address">
<aui:validator name="required" />
<aui:validator name="email"></aui:validator>
</aui:input>
<aui:button type="submit" value="Send SMS"></aui:button>
</aui:form>


Portlet Action Class (PublicRenderParametersIPCSenderAction.java)


package com.meera.liferay.ipc;

import java.io.IOException;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;

import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
public class PublicRenderParametersIPCSenderAction extends MVCPortlet {
public void getData(ActionRequest actionRequest,
ActionResponse actionResponse) throws IOException, PortletException, SystemException {
String userEmailAddress = ParamUtil.getString(actionRequest,"userEmailAddress");
actionResponse.setRenderParameter("userEmailAddress", userEmailAddress);
}

}


Receiver Portlet

Portlet.xml (/PublicRenderParametersIPCReceiver-portlet/docroot/WEB-INF/portlet.xml)


<?xml version="1.0"?>
<portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" version="2.0">

<portlet>
<portlet-name>
Public Render Parameters IPC Receiver Portlet
</portlet-name>
<display-name>
Public Render Parameters IPC Receiver Portlet
</display-name>
<portlet-class>
com.meera.liferay.ipc.PublicRenderParametersIPCReceiverAction
</portlet-class>
<init-param>
<name>view-template</name>
<value>/html/jsps/view.jsp</value>
</init-param>
<expiration-cache>0</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
<portlet-mode>view</portlet-mode>
</supports>
<portlet-info>
<title>Public Render Parameters IPC Receiver Portlet</title>
<short-title>
Public Render Parameters IPC Receiver Portlet
</short-title>
<keywords></keywords>
</portlet-info>
<security-role-ref>
<role-name>administrator</role-name>
</security-role-ref>
<security-role-ref>
<role-name>guest</role-name>
</security-role-ref>
<security-role-ref>
<role-name>power-user</role-name>
</security-role-ref>
<security-role-ref>
<role-name>user</role-name>
</security-role-ref>
<supported-public-render-parameter>userEmailAddress</supported-public-render-parameter>
</portlet>
<public-render-parameter>
<identifier>userEmailAddress</identifier>
<qname xmlns:x="http://liferaysavvy.com/userEmailAddress">
x:userEmailAddress</qname>
</public-render-parameter>
</portlet-app>


View.jsp (/PublicRenderParametersIPCReceiver-portlet/docroot/html/jsps/view.jsp)


<%@page import="com.liferay.portal.NoSuchUserException"%>
<%@page import="com.liferay.portal.service.UserLocalServiceUtil"%>
<%@page import="com.liferay.portal.model.User"%>
<%@page import="com.liferay.portal.kernel.util.ParamUtil"%>
<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet"%>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme"%>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui"%>
<portlet:defineObjects />
<liferay-theme:defineObjects />
<h1>Receive User Data from Sender Portlet</h1>
<%
String message=null;
String userEmailAddress=ParamUtil.getString
(renderRequest,"userEmailAddress",null);
User senderPortletUserOject=null;
try{
if(userEmailAddress!=null){
senderPortletUserOject=UserLocalServiceUtil.getUserByEmailAddress
(themeDisplay.getCompanyId(), userEmailAddress);
}
}catch(NoSuchUserException e){
message="No User exists with the given Email Address.";
}catch(Exception e){
message="There is problem in view the user details.";
}

if(senderPortletUserOject!=null){
%>
<table border="1">
<tr>
<td>User Id</td>
<td><%=senderPortletUserOject.getUserId()%></td>
</tr>
<tr>
<td>First Name</td>
<td><%=senderPortletUserOject.getFirstName()%></td>
</tr>
<tr>
<td>Last Name</td>
<td><%=senderPortletUserOject.getLastName()%></td>
</tr>
<tr>
<td>Email Address</td>
<td><%=senderPortletUserOject.getEmailAddress()%></td>
</tr>
<tr>
<td>Screen Name</td>
<td><%=senderPortletUserOject.getScreenName()%></td>
</tr>
</table>

<%}else{%>
<%=message%>
<%}%>


Portlet Action Class (PublicRenderParametersIPCReceiverAction.java)


package com.meera.liferay.ipc;
import com.liferay.util.bridges.mvc.MVCPortlet;
public class PublicRenderParametersIPCReceiverAction extends MVCPortlet {


}



0 comments :

Post a Comment

Recent Posts

Recent Posts Widget

Popular Posts