001    /**
002     * Copyright (c) 2000-2010 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.enterpriseadmin.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.Group;
024    import com.liferay.portal.util.PortletKeys;
025    import com.liferay.portlet.PortalPreferences;
026    import com.liferay.portlet.PortletPreferencesFactoryUtil;
027    import com.liferay.portlet.enterpriseadmin.util.EnterpriseAdminUtil;
028    
029    import java.util.ArrayList;
030    import java.util.HashMap;
031    import java.util.List;
032    import java.util.Map;
033    
034    import javax.portlet.PortletRequest;
035    import javax.portlet.PortletURL;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     */
040    public class GroupSearch extends SearchContainer<Group> {
041    
042            static List<String> headerNames = new ArrayList<String>();
043            static Map<String, String> orderableHeaders = new HashMap<String, String>();
044    
045            static {
046                    headerNames.add("name");
047                    headerNames.add("type");
048    
049                    orderableHeaders.put("name", "name");
050                    orderableHeaders.put("type", "type");
051            }
052    
053            public static final String EMPTY_RESULTS_MESSAGE =
054                    "no-communities-were-found";
055    
056            public GroupSearch(PortletRequest portletRequest, PortletURL iteratorURL) {
057                    super(
058                            portletRequest, new GroupDisplayTerms(portletRequest),
059                            new GroupSearchTerms(portletRequest), DEFAULT_CUR_PARAM,
060                            DEFAULT_DELTA, iteratorURL, headerNames, EMPTY_RESULTS_MESSAGE);
061    
062                    GroupDisplayTerms displayTerms = (GroupDisplayTerms)getDisplayTerms();
063    
064                    iteratorURL.setParameter(
065                            GroupDisplayTerms.DESCRIPTION, displayTerms.getDescription());
066                    iteratorURL.setParameter(
067                            GroupDisplayTerms.NAME, displayTerms.getName());
068    
069                    try {
070                            PortalPreferences preferences =
071                                    PortletPreferencesFactoryUtil.getPortalPreferences(
072                                            portletRequest);
073    
074                            String orderByCol = ParamUtil.getString(
075                                    portletRequest, "orderByCol");
076                            String orderByType = ParamUtil.getString(
077                                    portletRequest, "orderByType");
078    
079                            if (Validator.isNotNull(orderByCol) &&
080                                    Validator.isNotNull(orderByType)) {
081    
082                                    preferences.setValue(
083                                            PortletKeys.ENTERPRISE_ADMIN, "groups-order-by-col",
084                                            orderByCol);
085                                    preferences.setValue(
086                                            PortletKeys.ENTERPRISE_ADMIN, "groups-order-by-type",
087                                            orderByType);
088                            }
089                            else {
090                                    orderByCol = preferences.getValue(
091                                            PortletKeys.ENTERPRISE_ADMIN, "groups-order-by-col",
092                                            "name");
093                                    orderByType = preferences.getValue(
094                                            PortletKeys.ENTERPRISE_ADMIN, "groups-order-by-type",
095                                            "asc");
096                            }
097    
098                            OrderByComparator orderByComparator =
099                                    EnterpriseAdminUtil.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 Log _log = LogFactoryUtil.getLog(GroupSearch.class);
113    
114    }