Monday, July 8, 2013

Sorting (Case insensitive) using Custom Comparator in Liferay

Search Container is widely used taglib in Liferay.
We will follow few steps to implement ordering on container columns
 
1) Write below code in your jsp .
<%
 
String orderByCol = ParamUtil.getString(renderRequest, "orderByCol");
String orderByType = ParamUtil.getString(renderRequest, "orderByType"); 
 
/* Code to set default arrow on particular column while landing on this page */
 
PortalPreferences portalPrefs = PortletPreferencesFactoryUtil.getPortalPreferences(request);
 
if (Validator.isNotNull(orderByCol) && Validator.isNotNull(orderByType)) {
portalPrefs.setValue("DemoName", "DemoName-order-by-col", orderByCol);
portalPrefs.setValue("DemoName", "DemoName-order-by-type", orderByType);
 
} else {
 
orderByCol = portalPrefs.getValue("DemoName", "DemoName-order-by-col", "keyProperty");
orderByType = portalPrefs.getValue("DemoName", "DemoName-order-by-type", "desc");
}
 
/* Code to convert asc in desc after clicking on cloumn */
 
if(orderByCol==null || orderByCol.equals(StringPool.BLANK)) {
renderRequest.setAttribute("orderByCol","keyProperty");
}
if(orderByType==null || orderByType.equals(StringPool.BLANK)) {
orderByType="desc";
renderRequest.setAttribute("orderByType",orderByType);
}
%>
 
2) Pass Custom comparator to your search container results.
 
<liferay-ui:search-container orderByCol="<%=orderByCol %>" orderByType="<%=orderByType %>"  iteratorURL="<%=iteratorURL %>"  delta='10'>
<liferay-ui:search-container-results>
<%
 
List<YourModel> lst = lst; //Fetch List for sorting purpose;
if(lst!=null){
Collections.sort(lst,DemoComparatorUtil.getDemoComparator(orderByType, orderByCol)); //Pass custom comparator for sorting
total = lst.size();
results = ListUtil.subList(lst, searchContainer.getStart(), searchContainer.getEnd());
pageContext.setAttribute("results", results);
pageContext.setAttribute("total", total);
 
}
 
%>
 
</liferay-ui:search-container-results>
.
.
.
</liferay-ui:search-container>
 
3) Now create custom comparator mentioned above "DemoComparator" :
 
public class DemoComparatorUtil {


    public static DemoComparator getDemoComparator(String orderByType,String orderByCol){
        DemoComparator demoComparator;    
        
        if (orderByType.equals("desc")) {
            demoComparator = new DemoComparator(false, false);
        } else {
            demoComparator = new DemoComparator(true, false);
        }
        
        if (orderByCol != null) {
            demoComparator.addOrderBy(orderByCol);
        }        
            return demoComparator;    
    }
}
class DemoComparator implements Comparator<YourModel>, Serializable{


        private boolean asc;
        private boolean caseSensitive;
        private List<DocumentComparatorOrderBy> columns = new ArrayList<DocumentComparatorOrderBy>();


    public DemoComparator(){
        this(true,false);
    }

    public DemoComparator(boolean asc, boolean caseSensitive) {
        this.asc = asc;
        this.caseSensitive = caseSensitive;
    }
    
    public void addOrderBy(String name) {
        addOrderBy(name, asc, caseSensitive);
    }

    public void addOrderBy(String name, boolean asc, boolean caseSensitive) {
    DocumentComparatorOrderBy orderBy = new DocumentComparatorOrderBy(name, asc, caseSensitive);
        columns.add(orderBy);
    }
    
    public int compare(YourModel arg0, YourModel arg1) {
    
        int result=0;
        for (DocumentComparatorOrderBy orderBy : columns) {
            String value1 = "";
            String value2 = "";
            
            if (orderBy.getName().equals("keyProperty")) {
                if (!orderBy.isAsc()) {
                    String temp = value1;
                    value1 = value2;
                    value2 = temp;        
                }
            
                if ((value1 != null) && (value2 != null)) {
                    if (orderBy.isCaseSensitive()) {
                        result = value1.compareTo(value2);
                    } else {
                        result = value1.compareToIgnoreCase(value2);
                    }
                }    
            
            }
        
        
            if (result != 0) {
                return result;
            }
        }
        return 0;
    }

}

You can use this single comparator for all columns in your search container by checking key property in if-else condition only.
Enjoy..!!

1 comment:

  1. I like to sort control panel admin blog entries search container based on status "draft" or "published". Please help me.

    ReplyDelete