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.usersadmin.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.Organization;
024    import com.liferay.portal.util.PortletKeys;
025    import com.liferay.portlet.PortalPreferences;
026    import com.liferay.portlet.PortletPreferencesFactoryUtil;
027    import com.liferay.portlet.usersadmin.util.UsersAdminUtil;
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 OrganizationSearch extends SearchContainer<Organization> {
041    
042            public static final String EMPTY_RESULTS_MESSAGE =
043                    "no-organizations-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("name");
051                    headerNames.add("parent-organization");
052                    headerNames.add("type");
053                    headerNames.add("city");
054                    headerNames.add("region");
055                    headerNames.add("country");
056    
057                    orderableHeaders.put("name", "name");
058                    orderableHeaders.put("type", "type");
059            }
060    
061            public OrganizationSearch(
062                    PortletRequest portletRequest, PortletURL iteratorURL) {
063    
064                    this(portletRequest, DEFAULT_CUR_PARAM, iteratorURL);
065            }
066    
067            public OrganizationSearch(
068                    PortletRequest portletRequest, String curParam,
069                    PortletURL iteratorURL) {
070    
071                    super(
072                            portletRequest, new OrganizationDisplayTerms(portletRequest),
073                            new OrganizationSearchTerms(portletRequest), curParam,
074                            DEFAULT_DELTA, iteratorURL, headerNames, EMPTY_RESULTS_MESSAGE);
075    
076                    OrganizationDisplayTerms displayTerms =
077                            (OrganizationDisplayTerms)getDisplayTerms();
078    
079                    iteratorURL.setParameter(
080                            OrganizationDisplayTerms.CITY, displayTerms.getCity());
081                    iteratorURL.setParameter(
082                            OrganizationDisplayTerms.COUNTRY_ID,
083                            String.valueOf(displayTerms.getCountryId()));
084                    iteratorURL.setParameter(
085                            OrganizationDisplayTerms.NAME, displayTerms.getName());
086                    iteratorURL.setParameter(
087                            OrganizationDisplayTerms.PARENT_ORGANIZATION_ID,
088                            String.valueOf(displayTerms.getParentOrganizationId()));
089                    iteratorURL.setParameter(
090                            OrganizationDisplayTerms.REGION_ID,
091                            String.valueOf(displayTerms.getRegionId()));
092                    iteratorURL.setParameter(
093                            OrganizationDisplayTerms.STREET, displayTerms.getStreet());
094                    iteratorURL.setParameter(
095                            OrganizationDisplayTerms.TYPE, displayTerms.getType());
096                    iteratorURL.setParameter(
097                            OrganizationDisplayTerms.ZIP, displayTerms.getZip());
098    
099                    try {
100                            PortalPreferences preferences =
101                                    PortletPreferencesFactoryUtil.getPortalPreferences(
102                                            portletRequest);
103    
104                            String orderByCol = ParamUtil.getString(
105                                    portletRequest, "orderByCol");
106                            String orderByType = ParamUtil.getString(
107                                    portletRequest, "orderByType");
108    
109                            if (Validator.isNotNull(orderByCol) &&
110                                    Validator.isNotNull(orderByType)) {
111    
112                                    preferences.setValue(
113                                            PortletKeys.USERS_ADMIN, "organizations-order-by-col",
114                                            orderByCol);
115                                    preferences.setValue(
116                                            PortletKeys.USERS_ADMIN, "organizations-order-by-type",
117                                            orderByType);
118                            }
119                            else {
120                                    orderByCol = preferences.getValue(
121                                            PortletKeys.USERS_ADMIN, "organizations-order-by-col",
122                                            "name");
123                                    orderByType = preferences.getValue(
124                                            PortletKeys.USERS_ADMIN, "organizations-order-by-type",
125                                            "asc");
126                            }
127    
128                            OrderByComparator<Organization> orderByComparator =
129                                    UsersAdminUtil.getOrganizationOrderByComparator(
130                                            orderByCol, orderByType);
131    
132                            setOrderableHeaders(orderableHeaders);
133                            setOrderByCol(orderByCol);
134                            setOrderByType(orderByType);
135                            setOrderByComparator(orderByComparator);
136                    }
137                    catch (Exception e) {
138                            _log.error(e);
139                    }
140            }
141    
142            private static final Log _log = LogFactoryUtil.getLog(
143                    OrganizationSearch.class);
144    
145    }