Wednesday, 8 April 2015

How to use Lieray Service without creating portlet

There are 6 simple steps to check / test OOTB Liferay's LocalServiceUtil methods

Step 1: Go to the Liferay login page

Step 2: Login using portal admin

Step 3: Go to the "Control Panel" -> "Server Administration" -> "Script" tab

Step 4: Select the "Javascript" as Language

Step 5: Provide below script under "Script" tab

    // ### Javascript Sample ###

    number = Packages.com.liferay.portal.service.UserLocalServiceUtil.getUsersCount();
    
    out.println(number);

Step 6: Click on "Execute"

Cheers! You are done. Output is available under the "Output" section

FYI, 1) We have used User Local Service to get user count like wise we can use any liferay OOTB service.
        2) Apart from "Javascript", Liferay provides "Beanshell", "Groovy", "Python", "Ruby" languages.

Cheers!
Henal Saraiya
(Senior Consultant)
CIGNEX Datamatics  

Tuesday, 7 April 2015

How to create Action Filter in Liferay

How to create Action Filter in Liferay

1) Create a filter class inside portlet

    Here is the sample code for the ActionFilter
   
    package com.test;

    import java.io.IOException;    
    import javax.portlet.ActionRequest;
    import javax.portlet.ActionResponse;
    import javax.portlet.PortletException;
    import javax.portlet.filter.ActionFilter;
    import javax.portlet.filter.FilterChain;
    import javax.portlet.filter.FilterConfig;
    
    public class ActionTestFilter implements ActionFilter {
    
    public void init(FilterConfig arg0) throws PortletException {
    System.out.println("Hey I am in init method of filter");
    }
    
    public void doFilter(ActionRequest arg0, ActionResponse arg1,
    FilterChain arg2) throws IOException, PortletException {
    System.out.println("Hey I am in doFilter");
    }
    
    @Override
    public void destroy() {
    System.out.println("Hey I am in destroy method of filter");
    }
    
    }
   
So far, we have decided what action needs to be performed before "init" , "destroy" and  "processAction" method gets called of a portlet.

2) Use portlet.xml file to specify for which portlet we need to call above filter
    (It is possible that in one war we might have created more than one portlet)

3) Below entries need to be added after <portlet> tag inside "portlet.xml" file  
    <filter>
<filter-name>Action</filter-name>
<filter-class>com.test.ActionTestFilter</filter-class>
<lifecycle>ACTION_PHASE</lifecycle>
</filter>

<filter-mapping>
<filter-name>Action</filter-name>
<portlet-name>*</portlet-name>
</filter-mapping>

4) We have configured "*" inside "<portlet-name>*</portlet-name>" which means filter will be called across all the portlet available in same war file.

Thats it! Custom filter is configured.

Now, Lets test it.

1) Deploy portlet and you will see below message inside log file

    Hey I am in init method of filter

2) Call processAction method and you will see below message inside log file.

    Hey I am in doFilter
   
3) Undeploy portlet and you will see below message inside log file

    Hey I am in destroy method of filter
   
Cheers!
Henal Saraiya
(Senior Consultant)
CIGNEX Datamatics     

Monday, 30 March 2015

How to remove Liferay Ext / How to undeploy Liferay Ext


There are 10 easy steps to remove/undeploy ext environment from Liferay Bundle

Step 0 : Stop Server / Tomcat Bundle

Step 1 : Go to the location "...\liferay-portal-*\tomcat-*\webapps\ROOT\WEB-INF and remove below files
    >   ext-*-*-ext.xml
    >   liferay-portlet-ext.xml
    >   portlet-ext.xml
    >   struts-config-ext.xml
    >   tiles-defs-ext.xml

Step 2: Go to the location "...\liferay-portal-*\tomcat-*\webapps\ROOT\WEB-INF\lib and remove below jar files
    >   ext-*-ext-impl.jar
    >   ext-*-ext-util-bridges.jar
    >   ext-*-ext-util-java.jar
    >   ext-*-ext-util-taglib.jar

Step 3: Go to the location "...\liferay-portal-*\tomcat-*\lib\ext and delete the below service jar
    >   ext-*-ext-service.jar

Step 4: Go to the location "...\liferay-portal-*\tomcat-*\webapps and delete the below folder
    >   *-ext

Step 5: Go to the location "...\liferay-portal-*\tomcat-*\webapps\ROOT\WEB-INF\classes / ...\liferay-portal-*\ and modify portal-ext.properties file
    Note : 1) Need to check for any of the start up events like "application.startup.events="
           2) Skip such events on server start up

Step 6: Remove "temp" and "work" folders from the location "liferay-portal-*\tomcat-*"

Step 7: Start tomcat bundle

Step 8: Put .war file of ext inside "deploy" folder and wait for below message on catalina.out

    "Extension environment for *-ext has been applied. You must reboot the server and redeploy all other plugins."
   
Step 9: Uncomment any changes in portal-ext.properties file made in point #5

Step 10: Restart your server

All Done! Ext is removed/undeployed. Enjoy Coding.

Cheers!
Henal Saraiya
(Senior Consultant)
CIGNEX Datamatics 

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