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