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