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()")