Saturday, 14 September 2013

Liferay : Tag cloud OOB integration with custom MVC portlet

Hi There,

Hope you are doing good.
In “Hi I am Liferay” we have seen last two things which might be small but equaly important for Liferay developer.

  1. Next we will see how to integrate tag portlet [OOB portlet] with any custom portlet. [Liferay 6.1]


Requirement description : I have my own custom MVC portlet called fruit portlet which will describe all fruit characteristics and items which made from it, which will be filter base on tag, when user will click on any tag for example an apple all entries which tagged as an apple will display along with description,and only those tags should be display on the page which are associated with custom entity.


Solution :
Step 1) As Tag [Tag cloud or Tag navigation] portlet is different entity we need to work on IPC so when user click on any tag your portlet[custom portlet] should know at time of render page.
To accomplish it you need to do certain things as below.
In portlet.xml of your portlet you need to have following entries.
<portlet-app  ........>
<portlet>
 <portlet-name>Fruit</portlet-name>
 <display-name>This is fruit decstiption</display-name>
                       |
                       |

 <supported-public-render-parameter>tag</supported-public-render-parameter>

</portlet> 

<public-render-parameter>
   <identifier>tag</identifier>
   <qname xmlns:x="http://www.liferay.com/public-render-parameters">x:tag</qname>
</public-render-parameter> 

</portlet-app  ........>

Step 2) In JSP of your portlet where you want to display tags related to your entity you need to write following code.


 <liferay-ui:asset-tags-navigation
    classNameId="<%= classNameId %>"
    displayStyle="<%= displayStyle %>"
    hidePortletWhenEmpty="<%= true %>"
    maxAssetTags="<%= maxAssetTags %>"
    showAssetCount="<%= showAssetCount %>"
    showZeroAssetCount="<%= showZeroAssetCount %>"
/>



Where
1.classNameId (long) = your entity class name id for which you need to display tags, in our case we will get it as below 

long clssNameId = com.liferay.portal.util.PortalUtil.getClassNameId(FruitEntry.class.getName());
 
2.displayStyle (String) = We will give here "cloud" as we want tag cloud look rather navigation.

3.hidePortletWhenEmpty (Boolean)= if true it will hide portlet when there is no related tags found 

4.maxAssetTags (int) =  number of tags to be displayed on the page.

5.showAssetCount (Boolean) =  true 
 
6.showZeroAssetCount (Bollean) = false

 
Step 3) In controller class in doView() method of portlet every time you need to check as follow.
 

String tagName = renderRequest.getParameter("tag");

when user click on any tag [on tag cloud portlet] you will get tag name in doView() 
as tagName as shown in code above, now you can right some code which will bring list of assets
which is associated with clicked tag.

Hope this helps.

For more information you can contact on sagar.vyas.blogspot@gmail.com

Thanks,
Sagar Vyas 

Saturday, 7 September 2013

Liferay : Solution: Problem to remember Current Tab liferay-ui:tabs while performing action/render on jsp ?



Hi,
         

                      Many of us are using <liferay-ui:tabs ..> </ liferay-ui:tabs> and there are good road map given in Liferay’s site too in shape of blogs and forums, but one question is frequently asked by people that “How to persist current tab ? or How to remember current tab even though page gets refresh ? all are these open question ”


This is basic question but no clear answer given anywhere.

I would to provide neat and clean explanation for same.

Scenario:
1.  I have view1, view2 and view3 which includes in master.jsp and one controller called MyController.java [JSR-286] portlet which extends MVCPortlet class of Liferay.
2.  I have used following code in my master.jsp

<%
 PortletURL portletURL = renderResponse.createRenderURL();
 String currTAB = ParamUtil.getString(request, "currTAB", tabs1);
 portletURL.setParameter("currTAB", currTAB);
 %>
<liferay-ui:tabs param="currTAB"  names="Tab1,Tab2,Tab3"   refresh="<%= true %>" url="<%=portletURL.toString() %>">

<liferay-ui:section >
   <%@ includefile="/jsp/view1.jsp" %>
</liferay-ui:section>

<liferay-ui:section>
<%@ include file="="/jsp/view2.jsp " %>
</liferay-ui:section>

<liferay-ui:section>
<%@ include file="="/jsp/view3.jsp " %>
</liferay-ui:section>

</liferay-ui:tabs>
 

Problem:


Suppose you are on view2.jsp and there you are submitting a form [or in other words your refreshing a current page which is view2.jsp] and you will be redirected to default page [in our case it is view1.jsp], it should not happened cause you have performed an action on view2.jsp page you suppose to rendered to view2.jsp instead of view1.jsp


Root cause: 

When you refresh the page by actionURL or renderURL your client that means your browser does not know where you were earlier other words on which tab? So it will be redirecting to default page which is view1.jsp in our case.
 

How to resolve this?

So we know root cause now question is how to tell browser current tab? An obvious answer is keep live one parameter in each URL which will always tell to your browser that where to redirect.


How to achieve this?

As you might have observed ”currTAB” parameter in <liferay-ui:tabs>  which will help you to achieve this, when you click on either of tab value of “currTAB” is automatically maintain accordingly. For ex. When user click on 3rd tab which name is “Tab3” it automatically store currTAB=”Tab3” as parameter in url.

Now you just need to maintain this variable. Like as below,

String currTAB = ParamUtil.getString(request, "currTAB", “Tab1”);
 portletURL.setParameter("currTAB", currTAB);
    
     What does above code says “It will try to get currTAB variable and if it is not found the same it will pass Tab1 as parameter value”

     You need to write above code at everywhere you require maintaining tab value.

     Make sure you keep refresh="<%= true %>" otherwise page will be not refreshed.

Hope this helps.

In case of more information require you can contact on

Thanks,
Sagar Vyas