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.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.Group;
026    import com.liferay.portal.model.User;
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 Brian Wing Shun Chan
041     */
042    public class GroupSearch extends SearchContainer<Group> {
043    
044            public static final String EMPTY_RESULTS_MESSAGE = "no-sites-were-found";
045    
046            public static List<String> headerNames = new ArrayList<>();
047            public static Map<String, String> orderableHeaders = new HashMap<>();
048    
049            static {
050                    headerNames.add("name");
051                    headerNames.add("type");
052    
053                    orderableHeaders.put("name", "name");
054                    orderableHeaders.put("type", "type");
055            }
056    
057            public GroupSearch(PortletRequest portletRequest, PortletURL iteratorURL) {
058                    super(
059                            portletRequest, new GroupDisplayTerms(portletRequest),
060                            new GroupSearchTerms(portletRequest), DEFAULT_CUR_PARAM,
061                            DEFAULT_DELTA, iteratorURL, headerNames, EMPTY_RESULTS_MESSAGE);
062    
063                    GroupDisplayTerms displayTerms = (GroupDisplayTerms)getDisplayTerms();
064    
065                    iteratorURL.setParameter(
066                            GroupDisplayTerms.DESCRIPTION, displayTerms.getDescription());
067                    iteratorURL.setParameter(
068                            GroupDisplayTerms.NAME, displayTerms.getName());
069    
070                    try {
071                            PortalPreferences preferences =
072                                    PortletPreferencesFactoryUtil.getPortalPreferences(
073                                            portletRequest);
074    
075                            String portletId = PortletProviderUtil.getPortletId(
076                                    User.class.getName(), PortletProvider.Action.VIEW);
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                                            portletId, "groups-order-by-col", orderByCol);
088                                    preferences.setValue(
089                                            portletId, "groups-order-by-type", orderByType);
090                            }
091                            else {
092                                    orderByCol = preferences.getValue(
093                                            portletId, "groups-order-by-col", "name");
094                                    orderByType = preferences.getValue(
095                                            portletId, "groups-order-by-type", "asc");
096                            }
097    
098                            OrderByComparator<Group> orderByComparator =
099                                    UsersAdminUtil.getGroupOrderByComparator(
100                                            orderByCol, orderByType);
101    
102                            setOrderableHeaders(orderableHeaders);
103                            setOrderByCol(orderByCol);
104                            setOrderByType(orderByType);
105                            setOrderByComparator(orderByComparator);
106                    }
107                    catch (Exception e) {
108                            _log.error(e);
109                    }
110            }
111    
112            private static final Log _log = LogFactoryUtil.getLog(GroupSearch.class);
113    
114    }