Friday 3 May 2013

Show Portlet if user is Logged In




If you want to show portlet only after user is login otherwise hide it.
Then use following code in your doView() method of a particular portlet.

ThemeDisplay themeDisplay= (ThemeDisplay)renderRequest.getAttribute(WebKeys.THEME_DISPLAY);
        if(!themeDisplay.isSignedIn()){
            renderRequest.setAttribute(WebKeys.PORTLET_CONFIGURATOR_VISIBILITY, Boolean.FALSE);
        }

Thursday 28 February 2013

Get Phone number typeId using following snippet
 (this is for Organization Phone nos)

 String type = Organization.class.getName() + ListTypeConstants.PHONE;
 List phoneTypes = ListTypeServiceUtil.getListTypes(type);
 System.out.println("phoneTypes : "+phoneTypes);


Thursday 7 February 2013

Avoid resubmit on actionURL



Make following entry in liferay-portlet.xml 
<action-url-redirect>true</action-url-redirect>
 By default its value is false. 

Thursday 31 January 2013

Liferay - add user programmatically

Create Liferay Portal User Programmatically

final ThemeDisplay themeDisplay = (ThemeDisplay) renderRequest
               .getAttribute(WebKeys.THEME_DISPLAY);
User user = createPortalUser("tanaji", themeDisplay.getCompanyId(),
               themeDisplay.getUserId(), themeDisplay.getLocale());
System.out.println("Created User : "+user);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Create new Portal User function.
private User createPortalUser(String userName, final long companyId,
           final long userId, final Locale locale) {

       User user = null;
       try {
           user = UserLocalServiceUtil.addUser(userId, companyId, false,
                   "test", "test", false, userName + "screenName", userName
                           + "@liferay.com", 0L, "", locale, userName
                           + "firstName", "middleName", userName + "lastName",
                   0, 0, false, 0, 1, 1970, "Job Title", null, null, null,
                   null, false, new ServiceContext());

       } catch (Exception e) {
           System.out.println("Portal user creation failed " + e.getMessage());
       }
       return user;
   }

Tuesday 8 January 2013

Multiple Action Classes with MVCPortlet

Liferay MVCPortlet has ability to move individual actions into separate classes.

Following steps are required 

Step 1 : Add the following initialization parameter to your portlet.xml file:
             
              <init-param>
                     <name>action.package.prefix</name>
                    <value>com.tana.portlet.action</value>
             </init-param>
              
            (CustomActionCommand class is located in this package com.tana.portlet.action)
 
Step 2 : Implementing an ActionCommand interface

               Your custom Action class should follow the two approach

               1) Custom action class must be suffix with -ActionCommand
 
               2) Custom action class must be implemented with the ActionCommand
                    interface
  
Portlet Class - CustomPortlet.java
package com.tana.portlet.action;
public class CustomPortlet extends MVCPortlet {
    

    @Override
    public void doView(RenderRequest renderRequest,
            RenderResponse renderResponse) throws IOException, PortletException {
        // TODO Auto-generated method stub
        System.out.println("CustomPortlet porlet is called!");   
        PortletRequestDispatcher dispatcher = null;
        String param = (String) renderRequest.getAttribute("param");
        if(param != null && param.equals("secondPage")) {
            renderResponse.setContentType("text/html");
            System.out.println("********************");
            dispatcher = getPortletContext().getRequestDispatcher("/html/customportlet/second.jsp");
            dispatcher.include(renderRequest, renderResponse);
        } else {
            super.doView(renderRequest, renderResponse);
        }
    }
}


Custom action class - CustomActionCommand.java 
processCommand() method of a CustomActionCommand class is called to perform Action. 
public class CustomActionCommand implements ActionCommand {

   @Override
    public boolean processCommand(PortletRequest request, PortletResponse response)
            throws PortletException {
        // TODO Auto-generated method stub
        System.out.println("CustomActionCommand class is called!");
        request.setAttribute("param","secondPage");
        return true;
    }
}

Step 3 : JSP page 
             
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />

<portlet:actionURL  var="actionCommandUrl">
         <portlet:param name="<%=javax.portlet.ActionRequest.ACTION_NAME %>" value="Custom"></portlet:param>
</portlet:actionURL>

This is the <b>CustomPortlet</b> portlet in View mode.
<br>     
<br>
<form action="<%=actionCommandUrl%>" method="post" name="myform">
        <input type="submit" value="Call Custom-Action Class">
</form>


- Create another jsp page under /html/customportlet/second.jsp this directory.

Multiple action can be perform into a single request. This would be done by setting javax.portlet.ActionRequest.ACTION_NAME parameter as param name and list of action as value which is separated by comma.
e.g <portlet:actionURL  var="actionCommandUrl">
         <portlet:param name="<%=javax.portlet.ActionRequest.ACTION_NAME %>"      value="Action1,Action2">

</portlet:param>

Wednesday 2 January 2013

Create URL for public & private pages of a Organization


Create url for public and private pages of a organization in a Liferay theme.

Make this entry in init_custom.vm file

    #set($organizationPublicURL="/web/$user.getScreenName()")
    #set($organizationPrivateURL="/user/$user.getScreenName()")

Sunday 9 December 2012

JSTL with Liferay

To configure JSTL Core library with Liferay require following steps

Step 1 : Add jstl-api.jar and jstl-impl.jar into your liferay-plugin-package.properties file
            /WEB-INF/liferay-plugin-package.properties
             or
             copy both these jars into your project - /WEB-INF/lib folder


Step 2 : Include following syntax to JSTL Core library in your JSP:
             <%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>

e.g        Set attribute in your java class
                      Employee e1 =new Employee(1, "Tanaji", "Mumbai");
                      List<Employee> empList = new ArrayList<Employee>();
                      empList.add(e1);
                      request.setAttribute("employeeList", empList);               

            Now you can use JSTL tag directly in your JSP file like this....
             <c:forEach var="emp" items="${employeeList}" >
                     Employee Name :-> <c:out value="${emp.name}"></c:out>
            </c:forEach>