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.portlet.PortletProvider;
021    import com.liferay.portal.kernel.portlet.PortletProviderUtil;
022    import com.liferay.portal.kernel.util.OrderByComparator;
023    import com.liferay.portal.kernel.util.ParamUtil;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.model.User;
026    import com.liferay.portal.model.UserGroup;
027    import com.liferay.portlet.PortalPreferences;
028    import com.liferay.portlet.PortletPreferencesFactoryUtil;
029    import com.liferay.users.admin.kernel.util.UsersAdminUtil;
030    
031    import java.util.ArrayList;
032    import java.util.HashMap;
033    import java.util.List;
034    import java.util.Map;
035    
036    import javax.portlet.PortletRequest;
037    import javax.portlet.PortletURL;
038    
039    /**
040     * @author Charles May
041     */
042    public class UserGroupSearch extends SearchContainer<UserGroup> {
043    
044            public static final String EMPTY_RESULTS_MESSAGE =
045                    "no-user-groups-were-found";
046    
047            public static List<String> headerNames = new ArrayList<>();
048            public static Map<String, String> orderableHeaders = new HashMap<>();
049    
050            static {
051                    headerNames.add("name");
052                    headerNames.add("description");
053    
054                    orderableHeaders.put("name", "name");
055                    orderableHeaders.put("description", "description");
056            }
057    
058            public UserGroupSearch(
059                    PortletRequest portletRequest, PortletURL iteratorURL) {
060    
061                    super(
062                            portletRequest, new UserGroupDisplayTerms(portletRequest),
063                            new UserGroupDisplayTerms(portletRequest), DEFAULT_CUR_PARAM,
064                            DEFAULT_DELTA, iteratorURL, headerNames, EMPTY_RESULTS_MESSAGE);
065    
066                    UserGroupDisplayTerms displayTerms =
067                            (UserGroupDisplayTerms)getDisplayTerms();
068    
069                    iteratorURL.setParameter(
070                            UserGroupDisplayTerms.DESCRIPTION, displayTerms.getDescription());
071                    iteratorURL.setParameter(
072                            UserGroupDisplayTerms.NAME, displayTerms.getName());
073    
074                    try {
075                            PortalPreferences preferences =
076                                    PortletPreferencesFactoryUtil.getPortalPreferences(
077                                            portletRequest);
078    
079                            String orderByCol = ParamUtil.getString(
080                                    portletRequest, "orderByCol");
081                            String orderByType = ParamUtil.getString(
082                                    portletRequest, "orderByType");
083                            String portletId = PortletProviderUtil.getPortletId(
084                                    User.class.getName(), PortletProvider.Action.VIEW);
085    
086                            if (Validator.isNotNull(orderByCol) &&
087                                    Validator.isNotNull(orderByType)) {
088    
089                                    preferences.setValue(
090                                            portletId, "user-groups-order-by-col", orderByCol);
091                                    preferences.setValue(
092                                            portletId, "user-groups-order-by-type", orderByType);
093                            }
094                            else {
095                                    orderByCol = preferences.getValue(
096                                            portletId, "user-groups-order-by-col", "name");
097                                    orderByType = preferences.getValue(
098                                            portletId, "user-groups-order-by-type", "asc");
099                            }
100    
101                            OrderByComparator<UserGroup> 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 final Log _log = LogFactoryUtil.getLog(
116                    UserGroupSearch.class);
117    
118    }