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.model.Role;
021    import com.liferay.portal.kernel.model.RoleConstants;
022    import com.liferay.portal.kernel.portlet.PortalPreferences;
023    import com.liferay.portal.kernel.portlet.PortletPreferencesFactoryUtil;
024    import com.liferay.portal.kernel.portlet.PortletProvider;
025    import com.liferay.portal.kernel.portlet.PortletProviderUtil;
026    import com.liferay.portal.kernel.util.OrderByComparator;
027    import com.liferay.portal.kernel.util.ParamUtil;
028    import com.liferay.portal.kernel.util.Validator;
029    import com.liferay.portal.util.PropsValues;
030    import com.liferay.users.admin.kernel.util.UsersAdminUtil;
031    
032    import java.util.ArrayList;
033    import java.util.HashMap;
034    import java.util.List;
035    import java.util.Map;
036    
037    import javax.portlet.PortletRequest;
038    import javax.portlet.PortletURL;
039    
040    /**
041     * @author Brian Wing Shun Chan
042     */
043    public class RoleSearch extends SearchContainer<Role> {
044    
045            public static final String EMPTY_RESULTS_MESSAGE = "no-roles-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("title");
052                    headerNames.add("type");
053    
054                    if ((PropsValues.ROLES_ORGANIZATION_SUBTYPES.length > 0) ||
055                            (PropsValues.ROLES_REGULAR_SUBTYPES.length > 0) ||
056                            (PropsValues.ROLES_SITE_SUBTYPES.length > 0)) {
057    
058                            headerNames.add("subtype");
059                    }
060    
061                    headerNames.add("description");
062    
063                    orderableHeaders.put("title", "title");
064                    orderableHeaders.put("type", "type");
065                    orderableHeaders.put("description", "description");
066            }
067    
068            public RoleSearch(PortletRequest portletRequest, PortletURL iteratorURL) {
069                    super(
070                            portletRequest, new RoleDisplayTerms(portletRequest),
071                            new RoleSearchTerms(portletRequest), DEFAULT_CUR_PARAM,
072                            DEFAULT_DELTA, iteratorURL, headerNames, EMPTY_RESULTS_MESSAGE);
073    
074                    RoleDisplayTerms displayTerms = (RoleDisplayTerms)getDisplayTerms();
075                    RoleSearchTerms searchTerms = (RoleSearchTerms)getSearchTerms();
076    
077                    if (ParamUtil.getInteger(portletRequest, "type") == 0) {
078                            displayTerms.setType(RoleConstants.TYPE_REGULAR);
079                            searchTerms.setType(RoleConstants.TYPE_REGULAR);
080                    }
081    
082                    iteratorURL.setParameter(
083                            RoleDisplayTerms.DESCRIPTION, displayTerms.getDescription());
084                    iteratorURL.setParameter(RoleDisplayTerms.NAME, displayTerms.getName());
085                    iteratorURL.setParameter(
086                            RoleDisplayTerms.TYPE, String.valueOf(displayTerms.getType()));
087    
088                    try {
089                            PortalPreferences preferences =
090                                    PortletPreferencesFactoryUtil.getPortalPreferences(
091                                            portletRequest);
092    
093                            String portletId = PortletProviderUtil.getPortletId(
094                                    Role.class.getName(), PortletProvider.Action.BROWSE);
095    
096                            String orderByCol = ParamUtil.getString(
097                                    portletRequest, "orderByCol");
098                            String orderByType = ParamUtil.getString(
099                                    portletRequest, "orderByType");
100    
101                            if (Validator.isNotNull(orderByCol) &&
102                                    Validator.isNotNull(orderByType)) {
103    
104                                    preferences.setValue(
105                                            portletId, "roles-order-by-col", orderByCol);
106                                    preferences.setValue(
107                                            portletId, "roles-order-by-type", orderByType);
108                            }
109                            else {
110                                    orderByCol = preferences.getValue(
111                                            portletId, "roles-order-by-col", "title");
112                                    orderByType = preferences.getValue(
113                                            portletId, "roles-order-by-type", "asc");
114                            }
115    
116                            OrderByComparator<Role> orderByComparator =
117                                    UsersAdminUtil.getRoleOrderByComparator(
118                                            orderByCol, orderByType);
119    
120                            setOrderableHeaders(orderableHeaders);
121                            setOrderByCol(orderByCol);
122                            setOrderByType(orderByType);
123                            setOrderByComparator(orderByComparator);
124                    }
125                    catch (Exception e) {
126                            _log.error(e);
127                    }
128            }
129    
130            private static final Log _log = LogFactoryUtil.getLog(RoleSearch.class);
131    
132    }