Wednesday, January 2, 2013

Tabs with Search Container in Liferay

Generally we have to write a lot of javascript and java code in order to deal with paging in Web based application.

But with liferay you don't have to deal with that any more. Your life become easier with liferay.

As I used to post regularly in liferay forums. I found that most of the people face issues of pagination with Tabbing.
Here I would represent how search container works with Tabs.

Below are some of the screenshot for the demo that I am going to discuss in this post.







As you will refer above screenshot, you will be able to see two tabs and both of them having list of data.
First Tab show the list of all the User Group and the Second Tab shows the list of all Users for the Current Company.

Below are the steps you need to follow :-

1) First Create the Controller Class which will extend the MVC Controller and put it's Entry in the portlet.xml.

com.blog.demo.controller.SearContainerController

2) In order to create the Tabbing View, you have to write below code in your jsp page. So in my case I have created the view.jsp and whose entry by default set in the portlet.xml.

	









		
			
				
						<%@include file="/html/portlet/searchcontainer-with-tabs/tabs/usergroup.jsp" %>
			
			
		
		
			
			
					<%@include file="/html/portlet/searchcontainer-with-tabs/tabs/user.jsp" %>
			
			
		
		

 

usergroup.jsp

                
                
                    
					
                    
                    
                     
                  

                

       

user.jsp

                
                 
 
                    
 
     
     
 
     
  

                

       
NOTE :- Here on the basis of  "tabs" parameter, it will return the search container.

4) In order to make the code more readable, I created the seperate class for both the UserGroup i.e., UserGroupUtil and for User i.e.,UserUtil

UserGroupUtil
public class UserGroupUtil
{

    public static void searchContainerData(RenderRequest renderRequest,RenderResponse renderResponse)
    {
        List<usergroup> userGroupList = new ArrayList<usergroup>();
        Map<string tring="tring"> paramMap= new HashMap<string tring="tring">();

        paramMap.put("tabs", "UserGroup");

        PortletURL iteratorURL= renderResponse.createRenderURL();
        Iterator<map .entry=".entry" string="string" tring="tring">&gt; entries = paramMap.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry<string string="string"> entry = entries.next();
            // System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
            iteratorURL.setParameter(entry.getKey(), entry.getValue());
        }
        PortletConfig portletConfig = (PortletConfig)renderRequest.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG);
        ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(WebKeys.THEME_DISPLAY);
        SearchContainer<usergroup> searchContainer = new SearchContainer<usergroup>(renderRequest, null,
                null, SearchContainer.DEFAULT_CUR_PARAM, ParamUtil.getInteger(renderRequest,  SearchContainer.DEFAULT_DELTA_PARAM, 5),
                iteratorURL, null, LanguageUtil.get(portletConfig, themeDisplay.getLocale(), "No UserGroups were Found"));
        int total = 0;
        try {

            userGroupList=UserGroupLocalServiceUtil.getUserGroups(themeDisplay.getCompanyId());
            total = userGroupList.size();

        } catch (SystemException e) {
            SessionErrors.add(renderRequest, SystemException.class.getName());
        }
        userGroupList = ListUtil.subList(userGroupList, searchContainer.getStart(), searchContainer.getEnd());

        searchContainer.setTotal(total);
        searchContainer.setResults(userGroupList);
        renderRequest.setAttribute("userGroupSearchContainer", searchContainer);

    }
}


UserUtil
public class UserUtil
{

    public static void searchContainerData(RenderRequest renderRequest,RenderResponse renderResponse)
    {
        List<user> userList = new ArrayList<user>();
        Map<string tring="tring"> paramMap= new HashMap<string tring="tring">();

        paramMap.put("tabs", "User");

        PortletURL iteratorURL= renderResponse.createRenderURL();
        Iterator<map .entry=".entry" string="string" tring="tring">&gt; entries = paramMap.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry<string string="string"> entry = entries.next();
            // System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
            iteratorURL.setParameter(entry.getKey(), entry.getValue());
        }

        PortletConfig portletConfig = (PortletConfig)renderRequest.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG);
        ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(WebKeys.THEME_DISPLAY);
        SearchContainer<user> searchContainer = new SearchContainer<user>(renderRequest, null,
                null, SearchContainer.DEFAULT_CUR_PARAM, ParamUtil.getInteger(renderRequest,  SearchContainer.DEFAULT_DELTA_PARAM, 10),
                iteratorURL, null, LanguageUtil.get(portletConfig, themeDisplay.getLocale(), "No Users were Found"));
        int total = 0;
        try {

            userList = UserLocalServiceUtil.getCompanyUsers(themeDisplay.getCompanyId(), -1, -1);
            total = userList.size();

        } catch (SystemException e) {
            SessionErrors.add(renderRequest, SystemException.class.getName());
        }
        userList = ListUtil.subList(userList, searchContainer.getStart(), searchContainer.getEnd());

        searchContainer.setTotal(total);
        searchContainer.setResults(userList);
        renderRequest.setAttribute("userSearchContainer", searchContainer);

    }
}

In order to work pagging in proper way for search container with different tabs. We need to create the iterator URL in proper manner. Because Pagging in Search Container works based on IteratorURL. So we need to pass proper parameter in the Iterator URL. The Below code plays an key role in the Tabbing with Search Container.
Map<String,String> paramMap= new HashMap<String,String>();



        paramMap.put("tabs", "User");



        PortletURL iteratorURL= renderResponse.createRenderURL();

        Iterator<Map.Entry<String, String>> entries = paramMap.entrySet().iterator();

        while (entries.hasNext()) {

            Map.Entry<String, String> entry = entries.next();

            // System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());

            iteratorURL.setParameter(entry.getKey(), entry.getValue());

        }

In the above code I used HashMap, because if we have more parameter to pass then we can pass easily in the Iterator URL. Hope it will be useful !!!!

No comments:

Post a Comment