001    /**
002     * Copyright (c) 2000-2013 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            static List<String> headerNames = new ArrayList<String>();
044            static Map<String, String> orderableHeaders = new HashMap<String, String>();
045    
046            static {
047                    headerNames.add("title");
048                    headerNames.add("type");
049    
050                    if ((PropsValues.ROLES_ORGANIZATION_SUBTYPES.length > 0) ||
051                            (PropsValues.ROLES_REGULAR_SUBTYPES.length > 0) ||
052                            (PropsValues.ROLES_SITE_SUBTYPES.length > 0)) {
053    
054                            headerNames.add("subtype");
055                    }
056    
057                    headerNames.add("description");
058    
059                    orderableHeaders.put("title", "title");
060                    orderableHeaders.put("type", "type");
061                    orderableHeaders.put("description", "description");
062            }
063    
064            public static final String EMPTY_RESULTS_MESSAGE = "no-roles-were-found";
065    
066            public RoleSearch(PortletRequest portletRequest, PortletURL iteratorURL) {
067                    super(
068                            portletRequest, new RoleDisplayTerms(portletRequest),
069                            new RoleSearchTerms(portletRequest), DEFAULT_CUR_PARAM,
070                            DEFAULT_DELTA, iteratorURL, headerNames, EMPTY_RESULTS_MESSAGE);
071    
072                    RoleDisplayTerms displayTerms = (RoleDisplayTerms)getDisplayTerms();
073    
074                    iteratorURL.setParameter(
075                            RoleDisplayTerms.DESCRIPTION, displayTerms.getDescription());
076                    iteratorURL.setParameter(RoleDisplayTerms.NAME, displayTerms.getName());
077                    iteratorURL.setParameter(
078                            RoleDisplayTerms.TYPE, String.valueOf(displayTerms.getType()));
079    
080                    try {
081                            PortalPreferences preferences =
082                                    PortletPreferencesFactoryUtil.getPortalPreferences(
083                                            portletRequest);
084    
085                            String orderByCol = ParamUtil.getString(
086                                    portletRequest, "orderByCol");
087                            String orderByType = ParamUtil.getString(
088                                    portletRequest, "orderByType");
089    
090                            if (Validator.isNotNull(orderByCol) &&
091                                    Validator.isNotNull(orderByType)) {
092    
093                                    preferences.setValue(
094                                            PortletKeys.ROLES_ADMIN, "roles-order-by-col", orderByCol);
095                                    preferences.setValue(
096                                            PortletKeys.ROLES_ADMIN, "roles-order-by-type",
097                                            orderByType);
098                            }
099                            else {
100                                    orderByCol = preferences.getValue(
101                                            PortletKeys.ROLES_ADMIN, "roles-order-by-col", "title");
102                                    orderByType = preferences.getValue(
103                                            PortletKeys.ROLES_ADMIN, "roles-order-by-type", "asc");
104                            }
105    
106                            OrderByComparator orderByComparator =
107                                    UsersAdminUtil.getRoleOrderByComparator(
108                                            orderByCol, orderByType);
109    
110                            setOrderableHeaders(orderableHeaders);
111                            setOrderByCol(orderByCol);
112                            setOrderByType(orderByType);
113                            setOrderByComparator(orderByComparator);
114                    }
115                    catch (Exception e) {
116                            _log.error(e);
117                    }
118            }
119    
120            private static Log _log = LogFactoryUtil.getLog(RoleSearch.class);
121    
122    }