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.rolesadmin.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.Role;
026    import com.liferay.portal.util.PropsValues;
027    import com.liferay.portlet.PortalPreferences;
028    import com.liferay.portlet.PortletPreferencesFactoryUtil;
029    import com.liferay.portlet.usersadmin.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 RoleSearch extends SearchContainer<Role> {
043    
044            public static final String EMPTY_RESULTS_MESSAGE = "no-roles-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("title");
051                    headerNames.add("type");
052    
053                    if ((PropsValues.ROLES_ORGANIZATION_SUBTYPES.length > 0) ||
054                            (PropsValues.ROLES_REGULAR_SUBTYPES.length > 0) ||
055                            (PropsValues.ROLES_SITE_SUBTYPES.length > 0)) {
056    
057                            headerNames.add("subtype");
058                    }
059    
060                    headerNames.add("description");
061    
062                    orderableHeaders.put("title", "title");
063                    orderableHeaders.put("type", "type");
064                    orderableHeaders.put("description", "description");
065            }
066    
067            public RoleSearch(PortletRequest portletRequest, PortletURL iteratorURL) {
068                    super(
069                            portletRequest, new RoleDisplayTerms(portletRequest),
070                            new RoleSearchTerms(portletRequest), DEFAULT_CUR_PARAM,
071                            DEFAULT_DELTA, iteratorURL, headerNames, EMPTY_RESULTS_MESSAGE);
072    
073                    RoleDisplayTerms displayTerms = (RoleDisplayTerms)getDisplayTerms();
074    
075                    iteratorURL.setParameter(
076                            RoleDisplayTerms.DESCRIPTION, displayTerms.getDescription());
077                    iteratorURL.setParameter(RoleDisplayTerms.NAME, displayTerms.getName());
078                    iteratorURL.setParameter(
079                            RoleDisplayTerms.TYPE, String.valueOf(displayTerms.getType()));
080    
081                    try {
082                            PortalPreferences preferences =
083                                    PortletPreferencesFactoryUtil.getPortalPreferences(
084                                            portletRequest);
085    
086                            String portletId = PortletProviderUtil.getPortletId(
087                                    Role.class.getName(), PortletProvider.Action.BROWSE);
088    
089                            String orderByCol = ParamUtil.getString(
090                                    portletRequest, "orderByCol");
091                            String orderByType = ParamUtil.getString(
092                                    portletRequest, "orderByType");
093    
094                            if (Validator.isNotNull(orderByCol) &&
095                                    Validator.isNotNull(orderByType)) {
096    
097                                    preferences.setValue(
098                                            portletId, "roles-order-by-col", orderByCol);
099                                    preferences.setValue(
100                                            portletId, "roles-order-by-type", orderByType);
101                            }
102                            else {
103                                    orderByCol = preferences.getValue(
104                                            portletId, "roles-order-by-col", "title");
105                                    orderByType = preferences.getValue(
106                                            portletId, "roles-order-by-type", "asc");
107                            }
108    
109                            OrderByComparator<Role> orderByComparator =
110                                    UsersAdminUtil.getRoleOrderByComparator(
111                                            orderByCol, orderByType);
112    
113                            setOrderableHeaders(orderableHeaders);
114                            setOrderByCol(orderByCol);
115                            setOrderByType(orderByType);
116                            setOrderByComparator(orderByComparator);
117                    }
118                    catch (Exception e) {
119                            _log.error(e);
120                    }
121            }
122    
123            private static final Log _log = LogFactoryUtil.getLog(RoleSearch.class);
124    
125    }