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