Tuesday, November 25, 2014

Liferay Portlet Configuration Page in Plugin Portlet

Liferay configuration page provides a way to store portlet related configurations.
Generally in the development we may need to store portlet specific configurations and we need one user interface page to input some values and store into database. These portlet specific configuration will be stored in portlet preferences table.

To input these values we will use portlet configuration page there we can design user interface to submit values and page can be access through portlet configuration icon that is at top right corner of portlet it can see visible when we drag and drop the portlet in the page.

Assume Scenario:

In our custom portlet we want to display Site specific users in the portlet. We will store Site Id in portlet preferences and we will take site Id to get site ,form the site we will fetch users to display it in the table.

to set these portlet configuration we will use portlet configuration page there user will change site so that user selected site will be stored in portlet preference.

Portlet Configuration Page Implementation Steps.
  1. Create configuration JSP page
  2. Implement Custom Configuration Action Class
  3. Configure Configuration Action Class in liferay-portlet.xml
Create configuration JSP page

First we need to create portlet configuration page and there use will input data.
Create form and create requited input elements.

In our case we only need select box that should display all sites form there user will select one option.

We should create configuration action URL using <liferay-portlet:actionURL/> tag and we should use "portletConfiguration" attribute value as “true”

Example Configuration Action URL

<liferay-portlet:actionURL var="portletConfigurationActionURL" portletConfiguration="true"/>

When we submit form using above URL then it will invoke Custom Configuration Action Class

Implement Custom Configuration Action Class

We need to implement Custom Configuration Action class there will capture portlet configurations and will stored as portlet preferences.

Our Custom class should implments the ConfigurationAction interface and in the render method we need to specify the configuration JSP page path that was created in the first step.

We have processAction(--) method there will write configuration values storing logic and render(---) method we simply specify the portlet configuration jsp page path. We will use portlet preference to store the configuration values.

Example Syntax:

public class PortletConfigurationAction implements ConfigurationAction {
public void processAction(PortletConfig portletConfig,
ActionRequest actionRequest, ActionResponse actionResponse)
throws Exception {
}
public String render(PortletConfig portletConfig,
RenderRequest renderRequest, RenderResponse renderResponse)
throws Exception {
return "/html/portletconfiguration/portlet_configuration.jsp";
}
}

Configure Configuration Action Class in “liferay-portlet.xml”

Finally we need to configure the Configuration Implementation java class in liferay-portlet.xml file. We need to give Java class with complete package where it is reside.

The following is tag to configure portlet configuration class

<configuration-action-class>
com.meera.portletconfig.PortletConfigurationAction
</configuration-action-class>

The following is complete code example for portlet configuration

Create liferay Plugin portlet using Liferay IDE and add following jsp pages and java classes then you can be ready with portlet configuration page.

init.jsp (/html/portletconfiguration/init.jsp)

<%@page import="com.liferay.portal.kernel.util.Validator"%>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>
<%@ taglib uri="http://liferay.com/tld/security" prefix="liferay-security" %>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<%@ taglib uri="http://liferay.com/tld/util" prefix="liferay-util" %>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ page import="com.liferay.portal.kernel.util.Constants" %>
<%@ page import="com.liferay.portal.kernel.util.GetterUtil" %>
<%@ page import="com.liferay.portal.kernel.util.ParamUtil" %>
<%@ page import="com.liferay.portal.kernel.util.StringPool" %>
<%@ page import="com.liferay.portal.util.PortalUtil" %>
<%@ page import="com.liferay.portlet.PortletPreferencesFactoryUtil" %>
<%@ page import="javax.portlet.PortletPreferences" %>
<%@ page import="javax.portlet.WindowState" %>
<%@ page import="javax.portlet.PortletURL"%>
<%@ page import="javax.portlet.ActionRequest" %>
<%@ page import="javax.portlet.PortletPreferences" %>
<%@ page import="com.liferay.portal.kernel.language.LanguageUtil" %>
<%@ page import="com.liferay.portal.security.permission.ActionKeys"%>
<liferay-theme:defineObjects />
<portlet:defineObjects />
<%
String currentURL = PortalUtil.getCurrentURL(request);
PortletPreferences preferences = renderRequest.getPreferences();
String portletResource = ParamUtil.getString(request, "portletResource");
if (Validator.isNotNull(portletResource)) {
preferences = PortletPreferencesFactoryUtil.getPortletSetup(request, portletResource);
}
String siteId = preferences.getValue("siteId","1");
%>


portlet_configuration.jsp (/html/portletconfiguration/portlet_configuration.jsp)

<%@page import="com.liferay.portal.model.Group"%>
<%@page import="java.util.List"%>
<%@page import="com.liferay.portal.service.GroupLocalServiceUtil"%>
<%@page import="com.liferay.portal.service.GroupLocalService"%>
<%@page import="com.liferay.portal.kernel.util.Constants"%>
<%@include file="init.jsp" %>
<%
List<Group> list=GroupLocalServiceUtil.getGroups(0,GroupLocalServiceUtil.getGroupsCount());
%>
<liferay-ui:success key="potlet-config-saved" message="Portlet Configuration have been successfully saved" />
<liferay-portlet:actionURL var="configurationActionURL" portletConfiguration="true"/>
<aui:form action="<%=configurationActionURL%>" method="post" name="configurationForm">
Select Site:<br/>
<aui:select name="siteId" id="siteId">
<%for(Group group:list) {%>
<aui:option value="<%=group.getGroupId() %>" label="<%=group.getName() %>"></aui:option>
<%} %>
</aui:select>
<br/>
<aui:button type="submit" name="Save Configuration" value="Save Configuration"></aui:button>
</aui:form>

view.jsp (/html/portletconfiguration/view.jsp)

<%@page import="java.util.ArrayList"%>
<%@page import="com.liferay.portal.model.User"%>
<%@page import="java.util.List"%>
<%@page import="com.liferay.portal.service.GroupLocalServiceUtil"%>
<%@page import="com.liferay.portal.service.UserLocalServiceUtil"%>
<%@page import="com.liferay.portal.service.UserLocalService"%>
<%@include file="init.jsp"%>
<%
List<User> users=new ArrayList<User>();
long longSiteId = GetterUtil.getLong(siteId);
String SiteName="";
try{
users = UserLocalServiceUtil.getGroupUsers(longSiteId);
SiteName=GroupLocalServiceUtil.getGroup(longSiteId).getName();
}catch(Exception e){

}

%>
<h3>Selected Site "<%=SiteName%>" Users</h3>
<table border="1">
<tr>
<th>User First Name</th>
<th>User Last Name</th>
<th>User Screen Name</th>
<th>User Email Address</th>
</tr>
<%
for (User userobj : users) {
%>
<tr>
<td><%=userobj.getFirstName()%></td>
<td><%=userobj.getLastName()%></td>
<td><%=userobj.getScreenName()%></td>
<td><%=userobj.getEmailAddress()%></td>

</tr>
<%
}
%>
</table>

PortletConfigurationAction.java (com.meera.portletconfig.PortletConfigurationAction.java)

package com.meera.portletconfig;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.PortletPreferences;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import com.liferay.portal.kernel.portlet.ConfigurationAction;
import com.liferay.portal.kernel.servlet.SessionMessages;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portlet.PortletPreferencesFactoryUtil;
public class PortletConfigurationAction implements ConfigurationAction{
public void processAction(PortletConfig portletConfig,
ActionRequest actionRequest, ActionResponse actionResponse)
throws Exception {
String siteId = ParamUtil.getString(actionRequest, "siteId");
String portletResource = ParamUtil.getString(actionRequest,"portletResource");
System.out.println("siteId"+siteId);
PortletPreferences preferences = PortletPreferencesFactoryUtil.getPortletSetup(actionRequest, portletResource);
preferences.setValue("siteId", siteId);
preferences.store();
SessionMessages.add(actionRequest,"potlet-config-saved");
}
public String render(PortletConfig portletConfig,
RenderRequest renderRequest, RenderResponse renderResponse)
throws Exception {
return "/html/portletconfiguration/portlet_configuration.jsp";
}
}

liferay-portlet.xml

<liferay-portlet-app>
<portlet>
<portlet-name>portlet-configuration-action</portlet-name>
<icon>/icon.png</icon>
<configuration-action-class>
com.meera.portletconfig.PortletConfigurationAction
</configuration-action-class>
<header-portlet-css>/css/main.css</header-portlet-css>
<footer-portlet-javascript>
/js/main.js
</footer-portlet-javascript>
<css-class-wrapper>
portlet-configuration-action-portlet
</css-class-wrapper>
</portlet>

Download portlet from following location


In download you can see source code and war file you can use war file to deploy directly in your portal server and this portlet was developed using Liferay 6.2 Plugins SDK.

Usage of portlet

Download portlet and deploy the portlet in your portal server

Login as Administrator then add portlet to page which is available in Sample Category with name Portlet Configuration

Go to Portlet configuration page click on portlet configuration icon then you can see portlet configuration page.


Select site name then save the configurations


 Finally selected site users will be displayed in the portlet view page and we can change site name when ever we want like simple go to configuration page select the site name.
 
Author

2 comments :

  1. If an above issue get incapable to fix it's anything but, a call to our ability at Google Chrome Technical Support Phone Number. too many redirects error message

    ReplyDelete
  2. Developer will become happy to read your article. It is beneficial for them. Sometime, you have need to shift the luggage. You Can get it from Local Moving Services in Ridgefield CT. Make easy for yourself.

    ReplyDelete

Recent Posts

Recent Posts Widget

Popular Posts