Monday, February 7, 2011

InterPortlet Communication Liferay 6.0

InterPortlet Communication:
  1. create ipc-baseball "IPC Baseball"
  2. in the portlet.xml file.
First, change the portlet name, display name, and portlet class:
<portlet-name>pitcher-portlet</portlet-name>
<display-name>Pitcher Portlet</display-name>
<portlet-class>com.plugin.portlet.PitcherPortlet</portlet-class>
  1. Next add the init parameter in portlet.xml
<init-param>
<name>view-jsp</name>
<value>/pitcher/view.jsp</value> A
</init-param>
  1. Modify the portlet-info tag so it reads:
<portlet-info>
<title>Pitcher Portlet</title>
<short-title>Pitcher Portlet</short-title>
<keywords>IPC Baseball Pitcher</keywords>
</portlet-info>
  1. Next add Catcher declaration for the portlet in portlet.xml creating a new
<portlet>
<portlet-name>catcher-portlet</portlet-name>
<display-name>Catcher Portlet</display-name>
<portlet-class>
com.liferayinaction.portlets.CatcherPortlet
</portlet-class>
<init-param>
<name>view-jsp</name>
<value>/catcher/view.jsp</value>
</init-param>
<expiration-cache>0</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
</supports>
<portlet-info>
<title>Catcher Portlet</title>
<short-title>Catcher Portlet</short-title>
<keywords>IPC Baseball Catcher</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>
</portlet>
  1. Below the last </portlet> add the event
<event-definition>
<qname xmlns:x="http://liferay.com/events">x:ipc.pitch</qname>
<value-type>java.lang.String</value-type>
</event-definition>
  1. Place the before Pitcher Portlet’s </portet> and below last <security-role-ref> of Pitcher Portlet to tell the event handle who is sending the event.
<supported-publishing-event>
<qname xmlns:x="http://liferay.com/events">x:ipc.pitch</qname>
</supported-publishing-event>
  1. Place the before Catcher Portlet’s </portet>to tell the event handler who is recieveing the event
<supported-processing-event>
<qname xmlns:x="http://liferay.com/events">x:ipc.pitch</qname>
</supported-processing-event>
  1. Create docroot/catcher/view.jsp
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
<%
String pitch = (String)renderRequest.getParameter("pitch");
System.out.println(pitch);
%>
<p>And the pitch is....</p>
<p>
<% if (pitch!=null) { %>
<%=pitch %>pitch
<% } else { %>
... waiting for pitch.
<% } %>

  1. Create docroot/pitcher/view.jsp
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
<p>Click the link below to pitch the ball. </p>
<a href="<portlet:actionURL name="pitchBall"></portlet:actionURL>">Pitch!</a>
  1. Under docroot/src create a class CatchPortlet.java
package com.plugin.portlet;

import java.io.IOException;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletMode;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.PortletURL;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.portlet.EventRequest;
import javax.portlet.Event;
import javax.portlet.EventResponse;
import java.math.*;
import java.lang.annotation.*;
import javax.portlet.*;
import javax.xml.namespace.QName;

public class CatcherPortlet extends GenericPortlet {
public void init() throws PortletException {
viewJSP = getInitParameter("view-jsp");
}

public void doView(RenderRequest req, RenderResponse res)
throws IOException, PortletException {
include(viewJSP, req, res);
}

protected void include(String path, RenderRequest req, RenderResponse res)
throws IOException, PortletException {
PortletRequestDispatcher prd = getPortletContext()
.getRequestDispatcher(path);
if (prd == null) {
_log.error(path + " is not a valid include");
} else {
prd.include(req, res);
}
}

@ProcessEvent(qname = "{http://liferay.com/events}ipc.pitch")
public void catchBall(EventRequest request, EventResponse response) {
Event event = request.getEvent();
String pitch = (String) event.getValue();
System.out.println(pitch+"In class");
response.setRenderParameter("pitch", pitch);
}
protected String viewJSP;
private static Log _log = LogFactory.getLog(PitcherPortlet.class);

}

  1. Under docroot/src create a class PitchPortlet.java
package com.plugin.portlet;

import java.io.IOException;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletMode;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.PortletURL;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.portlet.EventRequest;
import javax.portlet.Event;
import javax.portlet.EventResponse;
import javax.portlet.*;
import java.util.Random;
import javax.xml.namespace.QName;


public class PitcherPortlet extends GenericPortlet {

public void init() throws PortletException {
viewJSP = getInitParameter("view-jsp");
}

public void doView(RenderRequest req, RenderResponse res)
throws IOException, PortletException {
include(viewJSP, req, res);
}

protected void include(String path, RenderRequest req, RenderResponse res)
throws IOException, PortletException {
PortletRequestDispatcher prd = getPortletContext()
.getRequestDispatcher(path);
if (prd == null) {
_log.error(path + " is not a valid include");
} else {
prd.include(req, res);
}
}

@ProcessAction(name = "pitchBall")
public void pitchBall(ActionRequest request, ActionResponse response) {
String pitchType = null;
// Send an Event that the ball has been pitched.
Random random = new Random(System.currentTimeMillis());
int pitch = random.nextInt(3) + 1;
switch (pitch) {
case 1:
pitchType = "Fast Ball";
break;
case 2:
pitchType = "Curve Ball";
break;
case 3:
pitchType = "Slider";
break;
// should never print
default:
pitchType = "Screw Ball";
}
QName qName = new QName("http://liferay.com/events", "ipc.pitch");
System.out.println(pitchType+"");
response.setEvent(qName, pitchType);
}

protected String viewJSP;
private static Log _log = LogFactory.getLog(PitcherPortlet.class);

}

  1. Compile and deploy the portlet.

Create Portlet with different modes (edit, view, help). In Liferay6.0

Create Portlet with different modes (edit, view, help). In Liferay6.0
(Should work with liferay 5.2.3)
  1. Install and setup Portal code 6.0.
  2. Install Plugin code 6.0
  3. Go to command prompt >workspace>pluginsdk>portlet> create.bat hello-world "Hello World"
  4. Go to portlet.xml under WEB-INF and update
  5. <portlet-class>com.liferay.util.bridges.mvc.MVCPortlet</portlet-class>
To
<portlet-class>com.liferayinaction.portlet.HelloYouPortlet</portlet-class>
  1. Add the edit mode along with view mode.
<init-param>
<name>edit-jsp</name>
<value>/edit.jsp</value>
</init-param>
  1. Under WEB-INF create a src folder and add the class
package com.plugin.portlet;

import java.io.IOException;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletMode;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.PortletURL;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class HelloYouPortlet extends GenericPortlet {
public void init() throws PortletException {
editJSP = getInitParameter("edit-jsp");
viewJSP = getInitParameter("view-jsp");
}

public void doEdit(RenderRequest renderRequest,
RenderResponse renderResponse) throws IOException, PortletException {
renderResponse.setContentType("text/html");

PortletURL addName = renderResponse.createActionURL();
addName.setParameter("addName", "addName");
renderRequest.setAttribute("addNameUrl", addName.toString());
include(editJSP, renderRequest, renderResponse);
}

public void doView(RenderRequest renderRequest,
RenderResponse renderResponse) throws IOException, PortletException {
PortletPreferences prefs = renderRequest.getPreferences();
String username = (String) prefs.getValue("name", "no");
if (username.equalsIgnoreCase("no")) {
username = "";
}
renderRequest.setAttribute("userName", username);
include(viewJSP, renderRequest, renderResponse);
}

public void processAction(ActionRequest actionRequest,
ActionResponse actionResponse) throws IOException, PortletException {
String addName = actionRequest.getParameter("addName");
if (addName != null) {
PortletPreferences prefs = actionRequest.getPreferences();
prefs.setValue("name", actionRequest.getParameter("username"));
prefs.store();
actionResponse.setPortletMode(PortletMode.VIEW);
}
}

protected void include(String path, RenderRequest renderRequest,
RenderResponse renderResponse) throws IOException, PortletException {
PortletRequestDispatcher portletRequestDispatcher = getPortletContext()
.getRequestDispatcher(path);
if (portletRequestDispatcher == null) {
_log.error(path + " is not a valid include");
} else {
portletRequestDispatcher.include(renderRequest, renderResponse);
}
}

protected String editJSP;
protected String viewJSP;
private static Log _log = LogFactory.getLog(HelloYouPortlet.class);
}
  1. Update view.jsp as
<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
<jsp:useBean id="userName" class="java.lang.String" scope="request"/>
<portlet:defineObjects />
<p>This is the Hello You portlet.</p>
<p>Hello <%=userName %>!</p>
  1. Create edit.jsp at same location as view.jsp
<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
<jsp:useBean class="java.lang.String" id="addNameUrl" scope="request" />
<portlet:defineObjects />
<form
id = "<portlet:namespace />helloForm"
action="<%=addNameUrl %>"
method="post">
<table>
<tr>
<td>Name:</td>
<td><input type="text" name="username"></td>
</tr>
</table>
<input type="submit" id="nameButton" title="Add Name" value="Add Name">
  1. Add the following to portlet.xml
<supports>
<mime-type>text/html</mime-type>
<portlet-mode>view</portlet-mode>
<portlet-mode>edit</portlet-mode>
</supports>

  1. From ant-build.xml compile and deploy.
  2. Start Tomcat and Liferay. Now on view there will be hello displayed.
  3. Click on the button of the portlet where there is look and feel
Go to preference and type in Harshal.
  1. Click on Add which will take back to view now the portlet will display hello harshal.