001    /**
002     * Copyright (c) 2000-present 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            public static final String EMPTY_RESULTS_MESSAGE = "no-sites-were-found";
043    
044            public static List<String> headerNames = new ArrayList<String>();
045            public static Map<String, String> orderableHeaders =
046                    new HashMap<String, String>();
047    
048            static {
049                    headerNames.add("name");
050                    headerNames.add("type");
051    
052                    orderableHeaders.put("name", "name");
053                    orderableHeaders.put("type", "type");
054            }
055    
056            public GroupSearch(PortletRequest portletRequest, PortletURL iteratorURL) {
057                    super(
058                            portletRequest, new GroupDisplayTerms(portletRequest),
059                            new GroupSearchTerms(portletRequest), DEFAULT_CUR_PARAM,
060                            DEFAULT_DELTA, iteratorURL, headerNames, EMPTY_RESULTS_MESSAGE);
061    
062                    GroupDisplayTerms displayTerms = (GroupDisplayTerms)getDisplayTerms();
063    
064                    iteratorURL.setParameter(
065                            GroupDisplayTerms.DESCRIPTION, displayTerms.getDescription());
066                    iteratorURL.setParameter(
067                            GroupDisplayTerms.NAME, displayTerms.getName());
068    
069                    try {
070                            PortalPreferences preferences =
071                                    PortletPreferencesFactoryUtil.getPortalPreferences(
072                                            portletRequest);
073    
074                            String orderByCol = ParamUtil.getString(
075                                    portletRequest, "orderByCol");
076                            String orderByType = ParamUtil.getString(
077                                    portletRequest, "orderByType");
078    
079                            if (Validator.isNotNull(orderByCol) &&
080                                    Validator.isNotNull(orderByType)) {
081    
082                                    preferences.setValue(
083                                            PortletKeys.USERS_ADMIN, "groups-order-by-col", orderByCol);
084                                    preferences.setValue(
085                                            PortletKeys.USERS_ADMIN, "groups-order-by-type",
086                                            orderByType);
087                            }
088                            else {
089                                    orderByCol = preferences.getValue(
090                                            PortletKeys.USERS_ADMIN, "groups-order-by-col", "name");
091                                    orderByType = preferences.getValue(
092                                            PortletKeys.USERS_ADMIN, "groups-order-by-type", "asc");
093                            }
094    
095                            OrderByComparator<Group> orderByComparator =
096                                    UsersAdminUtil.getGroupOrderByComparator(
097                                            orderByCol, orderByType);
098    
099                            setOrderableHeaders(orderableHeaders);
100                            setOrderByCol(orderByCol);
101                            setOrderByType(orderByType);
102                            setOrderByComparator(orderByComparator);
103                    }
104                    catch (Exception e) {
105                            _log.error(e);
106                    }
107            }
108    
109            private static final Log _log = LogFactoryUtil.getLog(GroupSearch.class);
110    
111    }