Thursday, July 15, 2010

Liferay refactoring: Moving existing extension to plugin SDK portlets

Yes I know it is frustrating. You want to keep your code separated in different WAR files containing individual portlets (Plugin SDK programming) but at the same time you want to reuse lots of lines of code that Liferay team already took care of.

You realize very fast that most of the amazing amount of code ready to be used is actually buried in the portal code in a way that to reuse it most of the people will be forced to use the extension environment.

Well, the good news is that with some effort the migration from extension environment to plugin SDK is actually just a matter of some tweaking.

The steps below are just theory. For the stuff working just checkout the maven project, deploy it in Liferay and look for "Update Password" portlet from "Add application". You will get a form to change the user password.

The "My Account" portlet comes with a functionality called "Update Password". I will show how to use the same functionality from a plugin SDK portlet.

1. Create a typical plugin SDK Portlet scaffold. You can go the extra mile and get it as a Maven project. It is just a simple Maven JSP Portlet scaffold project in case you are following this instructions to migrate another extension environment portlet, for example would you help me get the Detail portlet (also known as user Profile which is part of the "My Account" portlet as well)? ;-)
2. Identify which JSP liferay is using to provide the functionality you want. For example you want the "Change Password" or better said "Update Password" functionality. The JSP involved in that GUI is update_password.jsp
3. Copy the JSP code from ~/liferay/src/portal-web/docroot/html/portal/update_password.jsp and include it as your view.
4. Resolve dependencies, inclusions (imports). An IDE is a must have for this part.
5. Test the functionality. While it might compile out of the box the functionality might be broken. For the case of the update_password.jsp for example it uses a Struts action that we will reuse. The code sets via the SessionErrors object some attributes in the Servlet session which are not available from the Portlet session. To get them exposed you will need to change your friend portal-ext.properties. In my local machine:

#vi ~/liferay/ext/ext-impl/src/portal-ext.properties
...
session.shared.attributes=org.apache.struts.action.LOCALE,COMPANY_,USER_,LIFERAY_SHARED_,com.liferay.portal.kernel.servlet.SessionErrors
...



I hope this post will promote the migration of many other portlets to the plugin SDK environment.

No comments:

Followers