Tuesday, March 29, 2016

Liferay : Add custom structs action in the built-in-portlet

         Hooks are well known plugin provided by liferay.
It helps to customize / override liferay portal core features.
Note : Ext plugin do the same but Hooks are hot deployable.


Try out how to create your own hook 

Hook offers us to override the portal in below listed ways,
  • JSP pages
  • Property files
  • Services
  • Struts Action
  • Action Event
  • Language properties

We can not limit our self only to over-ride ,In some situation we may need to add new Struts action in the existing portlet. so, that can be called from the over-ridden jsp page.

Use case : Download the selected blog content from the blogs portlet.

Explanation : The built in blogs portlet have functionality to display the list of available blogs entry from the database. This use case ask to have some downloadable blogs content in the html format.

Solution : 
  • Create new hook project that keep blogs entry jsp over-ridden
Over ride the jsp html/portlet/blogs/view_entry_content.jsp

  • Crete new portlet:resourceURL url in the jsp page and make hyper link to it (<a/>)

<portlet:resourceURL var="pdfGenURL"> <portlet:param name="struts_action" value="/blogs/view_pdf" /> <portlet:param name="entryId" value="
<%= String.valueOf(entry.getEntryId()) %>" /> </portlet:resourceURL>



               Now we have to answer for a question that, which serveResoruce method will be called when calling pdfGenURL link?
              Yes , we need to add some new Struts action class that have serveResource method and it get the blogs content using Blogs Service class and serve it as html page.

  • Create new class CustomStrutsAction.java and extends with BaseStrutsPortletAction
Note : All the Custom action class should extends one of the Structs Base or other Action impl
  • Create new serverResource method 
               


@Override
public void serveResource(StrutsPortletAction originalStrutsPortletAction,
PortletConfig portletConfig, ResourceRequest resourceRequest,
ResourceResponse resourceResponse) { //Your implementation appears here //Code to get blogs content and render it as html mime type }

  • Register this class in the docroot/WEB-INF/liferay-hook.xml
<hook> <struts-action>
<struts-action-path>/blogs/view_pdf</struts-action-path>
<struts-action-impl><your-package>.CustomStrutsAction</struts-action-impl>
</struts-action>
</hook>



         Note : The only difference to add and over-ride action class is where we are going to register it.
If you register it in the struts-config.xml then it would be considered as the over-ridden action class. Looking for all built-in action list find here. But for our case we have registered it in the liferay-hook.xml

Now try out this from my git location. 
First Fork it and try from your git repo. If you make interesting on it give pull request to me.




Merging two sorted arrays - Big O (n+m) time complexity

 Problem :  Merge the two sorted arrays. Edge case :  Array can empty Arrays can be in different size let getMaxLength = ( input1 , input...