001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.usersadmin.search;
016    
017    import com.liferay.portal.kernel.dao.search.SearchContainer;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.OrderByComparator;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.model.Group;
024    import com.liferay.portal.util.PortletKeys;
025    import com.liferay.portlet.PortalPreferences;
026    import com.liferay.portlet.PortletPreferencesFactoryUtil;
027    import com.liferay.portlet.usersadmin.util.UsersAdminUtil;
028    
029    import java.util.ArrayList;
030    import java.util.HashMap;
031    import java.util.List;
032    import java.util.Map;
033    
034    import javax.portlet.PortletRequest;
035    import javax.portlet.PortletURL;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     */
040    public class GroupSearch extends SearchContainer<Group> {
041    
042            static List<String> headerNames = new ArrayList<String>();
043            static Map<String, String> orderableHeaders = new HashMap<String, String>();
044    
045            static {
046                    headerNames.add("name");
047                    headerNames.add("type");
048    
049                    orderableHeaders.put("name", "name");
050                    orderableHeaders.put("type", "type");
051            }
052    
053            public static final String EMPTY_RESULTS_MESSAGE = "no-sites-were-found";
054    
055            public GroupSearch(PortletRequest portletRequest, PortletURL iteratorURL) {
056                    super(
057                            portletRequest, new GroupDisplayTerms(portletRequest),
058                            new GroupSearchTerms(portletRequest), DEFAULT_CUR_PARAM,
059                            DEFAULT_DELTA, iteratorURL, headerNames, EMPTY_RESULTS_MESSAGE);
060    
061                    GroupDisplayTerms displayTerms = (GroupDisplayTerms)getDisplayTerms();
062    
063                    iteratorURL.setParameter(
064                            GroupDisplayTerms.DESCRIPTION, displayTerms.getDescription());
065                    iteratorURL.setParameter(
066                            GroupDisplayTerms.NAME, displayTerms.getName());
067    
068                    try {
069                            PortalPreferences preferences =
070                                    PortletPreferencesFactoryUtil.getPortalPreferences(
071                                            portletRequest);
072    
073                            String orderByCol = ParamUtil.getString(
074                                    portletRequest, "orderByCol");
075                            String orderByType = ParamUtil.getString(
076                                    portletRequest, "orderByType");
077    
078                            if (Validator.isNotNull(orderByCol) &&
079                                    Validator.isNotNull(orderByType)) {
080    
081                                    preferences.setValue(
082                                            PortletKeys.USERS_ADMIN, "groups-order-by-col", orderByCol);
083                                    preferences.setValue(
084                                            PortletKeys.USERS_ADMIN, "groups-order-by-type",
085                                            orderByType);
086                            }
087                            else {
088                                    orderByCol = preferences.getValue(
089                                            PortletKeys.USERS_ADMIN, "groups-order-by-col", "name");
090                                    orderByType = preferences.getValue(
091                                            PortletKeys.USERS_ADMIN, "groups-order-by-type", "asc");
092                            }
093    
094                            OrderByComparator orderByComparator =
095                                    UsersAdminUtil.getGroupOrderByComparator(
096                                            orderByCol, orderByType);
097    
098                            setOrderableHeaders(orderableHeaders);
099                            setOrderByCol(orderByCol);
100                            setOrderByType(orderByType);
101                            setOrderByComparator(orderByComparator);
102                    }
103                    catch (Exception e) {
104                            _log.error(e);
105                    }
106            }
107    
108            private static Log _log = LogFactoryUtil.getLog(GroupSearch.class);
109    
110    }