Tuesday, 24 February 2015

How to make Liferay's Action Request Parameters available in Render / doView

There are four different ways to pass/copy parameters from Liferay's Action Phase to Render Phase.

1) Pass all parameters
    -   Use PortalUtil class to copy all the parameters in processAction method
    -   PortalUtil.copyRequestParameters(actionRequest, actionResponse);    
        (This can be used if all the parameters passed are required in render method)

2) Pass selective parameters
    -   Manually set variables inside renderparameter inside processAction method
    -   actionResponse.setRenderParameter("action", "Y");    
        (Value set here can be used in rendermethod using renderRequest.getParameter("action"))
 
3) Pass all parameters across all processAction method
    -   Specify below value inside portlet.xml file
                 <init-param>
<name>copy-request-parameters</name>
<value>true</value> 
</init-param>  
   (Need to redeploy your portlet to brig above change into effect.)
 
4) Pass all parameters via manually reading
    -   Some people create Enumeration to iterate over all the elements and put in RenderParameter one by one
    -   Ex:
    Enumeration<String> paramNames = actionRequest.getParameterNames();
    while(paramNames.hasMoreElements()){        
        actionResponse.setRenderParameter(paramNames.nextElement(), actionRequest.getParameter(paramNames.nextElement()));
    }  
   
Cheers!
Henal Saraiya
(Senior Consultant)
CIGNEX Datamatics

Monday, 23 February 2015

How to read Liferay's URL parameter via script

Steps to read Liferay URL parameter via script.

1) Add a tag in your .JSP to read portlet namespace
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>

2) Create a global script variable inside your JSP for portlet name space
<script type="text/javascript" charset="utf-8">
var portletNamespace ='<portlet:namespace/>';
</script>

In a js file we can not use '<portlet:namespace/>' directly, hence we need to create a global js variable which can be accessed in .js file.

3) Create a function which reads and returns the URL parameter
function getURLParameterValue(parameterName, url) {
parameterName = parameterName.replace(/[\[]/, '\\\[').replace(/[\]]/, '\\\]');
var results = new RegExp('[\\?&]'+parameterName+'=?([^&#]*)').exec(url || window.location.href);
return results == null ? null : results[1];
}

Here, function accepts two values
#1) "parameterName" whose value needs to be read (This is an actual name from the URL parameter)
#2) "url" (Its an href location / window's current URL)

4) Read the URL in the javascript function
Let's say this is your URL :     <protocol>://<hostname>:<port>/group/control_panel/manage?p_auth=asdfg&p_p_auth=xyz&p_p_id=_sometext_WAR_sometextportlet_&p_p_lifecycle=1&p_p_state=maximized&p_p_mode=view&doAsGroupId=12345&refererPlid=5555&_sometext_WAR_sometextportlet_id=123456

Ex: Read Liferay Parameter
>) getURLParameterValue(portletNamespace + "id", window.location.href)
will return "123456"
Ex: Read Any parameter without portletNamespace prefixed
>) getURLParameterValue("p_auth", window.location.href) 
will return "asdfg"

Cheers!
Henal Saraiya
(Senior Consultant)
CIGNEX Datamatics

Friday, 20 February 2015

How to use Liferay's serveResource method

Steps to use Liferay's serveResource method / Ajax call

Step 1: Create "resourceURL" in your JSP

Here is the snippet for the "View.jsp"

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:resourceURL var="dynamiceResourceURL"></portlet:resourceURL>

<a href="dynamiceResourceURL">Dynamic Content</a>

Step 2: Create a "serveResource" method inside your MVCPortlet/Controller

Here is the snippet for <Your>Portlet

        @Override
public void serveResource(ResourceRequest resourceRequest,
ResourceResponse resourceResponse) throws IOException,
PortletException {
System.out.println("In serveResource method");
resourceResponse.setContentType("text/html");
PrintWriter writer = resourceResponse.getWriter();
writer.print("This is dynamic content.");
super.serveResource(resourceRequest, resourceResponse);
}

Step 3: Drop <your portlet> on portal page and click on the link "Dynamic Content". It will show result "This is dynamic content." which is returned by "serverResource" method of <your> controller.


Cheers!
Henal Saraiya
(Senior Consultant)
CIGNEX Datamatics


Wednesday, 11 February 2015

Liferay + Alloy UI + Iterate Element via CSS Class

How to apply "selectedClass" on "li" element based on the dynamic value (Page Load).

<!-- HTML structure -->

<ul class="ul_container">
<li class="selectLiDynamically" id="sort_1" textContent="first">
<a href="#">Hello One</a> 
</li>
<li class="selectLiDynamically" id="sort_2" textContent="second">
<a href="#">Hello Two</a>
</li>
<li class="selectLiDynamically" id="sort_3" textContent="third">
<a href="#">Hello Three</a>
</li>
</ul>

//AUI script

AUI().ready('node', function (A) {
A.all('.selectLiDynamically').each(function(obj) {
if(obj.attr("textContent") == '<%=selectValue%>') {
if(A.one("#"+obj.attr("id"))) {
A.one("#"+obj.attr("id")).addClass(selectedClass);
}
} else {
if(A.one("#"+obj.attr("id"))) {
A.one("#"+obj.attr("id")).removeClass(selectedClass);
}
}
});
});

Here '<%=selectValue%>' is the value which comes dynamically from the request and based on that value, we iterate all the "li" element via class value i.e. "selectLiDynamically".

If dynamic value matches with the "id" of the "li" element, we add the "selectedClass" on that object else we remove "selectedClass" from the "li" element.

We are done with applying css class dynamically via AUI, on page load.

Cheers!
Henal Saraiya
(Senior Consultant)
CIGNEX Datamatics