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.util.OrderByComparator;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.model.Role;
024    import com.liferay.portal.util.PortletKeys;
025    import com.liferay.portal.util.PropsValues;
026    import com.liferay.portlet.PortalPreferences;
027    import com.liferay.portlet.PortletPreferencesFactoryUtil;
028    import com.liferay.portlet.usersadmin.util.UsersAdminUtil;
029    
030    import java.util.ArrayList;
031    import java.util.HashMap;
032    import java.util.List;
033    import java.util.Map;
034    
035    import javax.portlet.PortletRequest;
036    import javax.portlet.PortletURL;
037    
038    /**
039     * @author Brian Wing Shun Chan
040     */
041    public class RoleSearch extends SearchContainer<Role> {
042    
043            public static final String EMPTY_RESULTS_MESSAGE = "no-roles-were-found";
044    
045            public static List<String> headerNames = new ArrayList<String>();
046            public static Map<String, String> orderableHeaders =
047                    new HashMap<String, String>();
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 orderByCol = ParamUtil.getString(
087                                    portletRequest, "orderByCol");
088                            String orderByType = ParamUtil.getString(
089                                    portletRequest, "orderByType");
090    
091                            if (Validator.isNotNull(orderByCol) &&
092                                    Validator.isNotNull(orderByType)) {
093    
094                                    preferences.setValue(
095                                            PortletKeys.ROLES_ADMIN, "roles-order-by-col", orderByCol);
096                                    preferences.setValue(
097                                            PortletKeys.ROLES_ADMIN, "roles-order-by-type",
098                                            orderByType);
099                            }
100                            else {
101                                    orderByCol = preferences.getValue(
102                                            PortletKeys.ROLES_ADMIN, "roles-order-by-col", "title");
103                                    orderByType = preferences.getValue(
104                                            PortletKeys.ROLES_ADMIN, "roles-order-by-type", "asc");
105                            }
106    
107                            OrderByComparator<Role> orderByComparator =
108                                    UsersAdminUtil.getRoleOrderByComparator(
109                                            orderByCol, orderByType);
110    
111                            setOrderableHeaders(orderableHeaders);
112                            setOrderByCol(orderByCol);
113                            setOrderByType(orderByType);
114                            setOrderByComparator(orderByComparator);
115                    }
116                    catch (Exception e) {
117                            _log.error(e);
118                    }
119            }
120    
121            private static final Log _log = LogFactoryUtil.getLog(RoleSearch.class);
122    
123    }