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.portlet.PortletProvider;
021    import com.liferay.portal.kernel.portlet.PortletProviderUtil;
022    import com.liferay.portal.kernel.util.OrderByComparator;
023    import com.liferay.portal.kernel.util.ParamUtil;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.model.Organization;
026    import com.liferay.portal.model.User;
027    import com.liferay.portlet.PortalPreferences;
028    import com.liferay.portlet.PortletPreferencesFactoryUtil;
029    import com.liferay.users.admin.kernel.util.UsersAdminUtil;
030    
031    import java.util.ArrayList;
032    import java.util.HashMap;
033    import java.util.List;
034    import java.util.Map;
035    
036    import javax.portlet.PortletRequest;
037    import javax.portlet.PortletURL;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     */
042    public class OrganizationSearch extends SearchContainer<Organization> {
043    
044            public static final String EMPTY_RESULTS_MESSAGE =
045                    "no-organizations-were-found";
046    
047            public static List<String> headerNames = new ArrayList<>();
048            public static Map<String, String> orderableHeaders = new HashMap<>();
049    
050            static {
051                    headerNames.add("name");
052                    headerNames.add("parent-organization");
053                    headerNames.add("type");
054                    headerNames.add("city");
055                    headerNames.add("region");
056                    headerNames.add("country");
057    
058                    orderableHeaders.put("name", "name");
059                    orderableHeaders.put("type", "type");
060            }
061    
062            public OrganizationSearch(
063                    PortletRequest portletRequest, PortletURL iteratorURL) {
064    
065                    this(portletRequest, DEFAULT_CUR_PARAM, iteratorURL);
066            }
067    
068            public OrganizationSearch(
069                    PortletRequest portletRequest, String curParam,
070                    PortletURL iteratorURL) {
071    
072                    super(
073                            portletRequest, new OrganizationDisplayTerms(portletRequest),
074                            new OrganizationSearchTerms(portletRequest), curParam,
075                            DEFAULT_DELTA, iteratorURL, headerNames, EMPTY_RESULTS_MESSAGE);
076    
077                    OrganizationDisplayTerms displayTerms =
078                            (OrganizationDisplayTerms)getDisplayTerms();
079    
080                    iteratorURL.setParameter(
081                            OrganizationDisplayTerms.CITY, displayTerms.getCity());
082                    iteratorURL.setParameter(
083                            OrganizationDisplayTerms.COUNTRY_ID,
084                            String.valueOf(displayTerms.getCountryId()));
085                    iteratorURL.setParameter(
086                            OrganizationDisplayTerms.NAME, displayTerms.getName());
087                    iteratorURL.setParameter(
088                            OrganizationDisplayTerms.PARENT_ORGANIZATION_ID,
089                            String.valueOf(displayTerms.getParentOrganizationId()));
090                    iteratorURL.setParameter(
091                            OrganizationDisplayTerms.REGION_ID,
092                            String.valueOf(displayTerms.getRegionId()));
093                    iteratorURL.setParameter(
094                            OrganizationDisplayTerms.STREET, displayTerms.getStreet());
095                    iteratorURL.setParameter(
096                            OrganizationDisplayTerms.TYPE, displayTerms.getType());
097                    iteratorURL.setParameter(
098                            OrganizationDisplayTerms.ZIP, displayTerms.getZip());
099    
100                    try {
101                            PortalPreferences preferences =
102                                    PortletPreferencesFactoryUtil.getPortalPreferences(
103                                            portletRequest);
104    
105                            String portletId = PortletProviderUtil.getPortletId(
106                                    User.class.getName(), PortletProvider.Action.VIEW);
107    
108                            String orderByCol = ParamUtil.getString(
109                                    portletRequest, "orderByCol");
110                            String orderByType = ParamUtil.getString(
111                                    portletRequest, "orderByType");
112    
113                            if (Validator.isNotNull(orderByCol) &&
114                                    Validator.isNotNull(orderByType)) {
115    
116                                    preferences.setValue(
117                                            portletId, "organizations-order-by-col", orderByCol);
118                                    preferences.setValue(
119                                            portletId, "organizations-order-by-type", orderByType);
120                            }
121                            else {
122                                    orderByCol = preferences.getValue(
123                                            portletId, "organizations-order-by-col", "name");
124                                    orderByType = preferences.getValue(
125                                            portletId, "organizations-order-by-type", "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    }