Building a Portlet

From Null-pointer

Jump to: navigation, search

Contents

Prerequisite

Creating a Portlet

Liferay Portlets must be compiled with Java5. e.g. -source=1.5 -target=1.5

cd portlets
ant –Dportlet.name=myportlet –Dportlet.display.name=”My Portlet” create

Deploying the Portlet

cd myporlet-portlet
ant deploy

Service builder

ant build-service compile

The service builder is a handy way of creating your data access layer. This will create you business objects and the data access persistence layer

Create the file /docroot/WEB-INF/service.xml. It uses the dtd liferay-service-builder_5_0_0.dtd

Setting the column size

By default all String colums are set to VARCHAR(75), if this is not big enough you will need to adjust the size. This is set in /docroot/META-INF/portlet-model.hints.xml. You may need to run build-service before setting the size, and again once you have set the size.

<model name="com.linhopesolutions.canopy.model.Overview">
 <field name="overviewId" type="long" />
 <field name="text" type="String">
  <hint name="max-length">10000</hint>
 </field>
 <field name="published" type="boolean" />
 <field name="lastModifiedBy" type="long" />
 <field name="lastModifiedOn" type="Date" />
</model>
  • max-length < 4000 = VARCHAR(max-length)
  • max-length = 4000 = STRING
  • max-length > 4000 = TEXT

Customisations

Add all your custom methods to the model to the classes in the package [package].model.impl with the Class [object]Impl.java

All all you database access customisation in the package [package].service.impl with the Class [object]LocalServiceImpl.java

Using the persistence layer

//create an overview
Overview overview = new OverviewImpl();
if ( overview.getOverviewId() == 0)
{
  overview = OverviewLocalServiceUtil.addOverview( overview ) ;
}
else
{
  overview = OverviewLocalServiceUtil.updateOverview( overview );
}
//get an Overview
overview = OverviewLocalServiceUtil.getOverview( primaryKey );
//delete an overview
OverviewLocalServiceUtil.deleteOverview( primaryKey );
//or
OverviewLocalServiceUtil.deleteOverview( overview );


Portlet

You need to extend javax.portlet.GenericPortlet.

By default you should override:

  • void doView(RenderRequest request, RenderResponse response)
  • void processAction(ActionRequest request, ActionResponse response)

You can also overrider:

  • void doEdit(RenderRequest request, RenderResponse response)
  • void doHelp(RenderRequest request, RenderResponse response)

For these methods to work you need to ensure that you have configure you Portlet to allow them. This is done in the file /docroot/WEB-INF/portlet.xml

<supports>
 <mime-type>text/html</mime-type>
 <portlet-mode>EDIT</portlet-mode>
</supports>

The recommended way of getting the mode is using the key ActionRequest.ACTION_NAME

To get you custom parameters from the request, Liferay provided a helpful util class ParamUtil

int overviewId = ParamUtil.getInteger( request, "overviewId" );
String overviewText = ParamUtil.getString( request, "overviewText" );

Window state

Liferay extends the Portlet window states, and these are defined in LiferayWindowState

To get a page returned in a 'raw' format, in other words, without all the framework look and feel surrounding the response use the state LiferayWindowState.EXCLUSIVE. This is needed for ajax calls.

Useful information

  • To get the logged in User id : long userId = PortalUtil.getUserId( request );
  • To get the Portal instance being used: PortalUtil.getCompany( request )

Portlet JSP

Portlet tag library

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:namespace/>
<portlet:renderURL 
  portletMode="view"
  windowState="<%=LiferayWindowState.NORMAL.toString()%>" >
  <portlet:param name="<%=ActionRequest.ACTION_NAME %>" value="viewOverview" />
  <portlet:param name="overviewId" value="<%= Util.toString( overviewId )%>" />
</portlet:renderURL>

<portlet:actionURL 
 name="editOverview"
 windowState="="<%=LiferayWindowState.NORMAL.toString()%>"
 portletMode="edit"
/>

jQuery

WYSISYG Editor

  • initEditor() needed to set the content of the editor
  • getContent() needed to retrieve the content of the editor
<%!
public static final String EDITOR_WYSIWYG_IMPL_KEY
   = "editor.wysiwyg.docroot.myporlet.jsp";
%>

<script type="text/javascript">
</script>


<form name="manageProgrammeForm" id="<portlet:namespace />_manageProgrammeForm"
action="<portlet:actionURL  name="saveProgramme"/>" method="post"
onSubmit="<portlet:namespace />getContent();">

<liferay-ui:input-editor editorImpl="<%=EDITOR_WYSIWYG_IMPL_KEY %>" name="OverviewText" />
<input name="<portlet:namespace />content"  id="<portlet:namespace />content"
type="hidden" value="" />

</form>

Liferay uses UnicodeFormatter.toString(content) to set the content, but I recommend you use <%= content.replace("\"", "\\\"" ).replace("\n", " ") %>

Configuration

liferay-display.xml

/docroot/WEB-INF/liferay-display.xml This describes which category your Porlets should be shown in when using the 'Add application' feature in Liferay

<!DOCTYPE display PUBLIC "-//Liferay//DTD Display 5.2.0//EN" 
 "http://www.liferay.com/dtd/liferay-display_5_2_0.dtd">

<display>
 <category name="category.canopy">
  <portlet id="myprofile" />
  <portlet id="mycourses" />
  <portlet id="mycoursesadmin" />
  <portlet id="ServiceAdmin" />
  <portlet id="service" />
  <portlet id="documentStoreAdmin" />
  <portlet id="documentStoreViewer" />
 </category>
</display>

import reference

  • javax.portlet.ActionRequest
  • com.liferay.portal.kernel.portlet.LiferayWindowState
  • com.liferay.portal.kernel.util.ParamUtil
  • com.liferay.portal.util.PortalUtil
Personal tools