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.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            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("parent-organization");
048                    headerNames.add("type");
049                    headerNames.add("city");
050                    headerNames.add("region");
051                    headerNames.add("country");
052    
053                    orderableHeaders.put("name", "name");
054                    orderableHeaders.put("type", "type");
055            }
056    
057            public static final String EMPTY_RESULTS_MESSAGE =
058                    "no-organizations-were-found";
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.ZIP, displayTerms.getZip());
095    
096                    try {
097                            PortalPreferences preferences =
098                                    PortletPreferencesFactoryUtil.getPortalPreferences(
099                                            portletRequest);
100    
101                            String orderByCol = ParamUtil.getString(
102                                    portletRequest, "orderByCol");
103                            String orderByType = ParamUtil.getString(
104                                    portletRequest, "orderByType");
105    
106                            if (Validator.isNotNull(orderByCol) &&
107                                    Validator.isNotNull(orderByType)) {
108    
109                                    preferences.setValue(
110                                            PortletKeys.USERS_ADMIN, "organizations-order-by-col",
111                                            orderByCol);
112                                    preferences.setValue(
113                                            PortletKeys.USERS_ADMIN, "organizations-order-by-type",
114                                            orderByType);
115                            }
116                            else {
117                                    orderByCol = preferences.getValue(
118                                            PortletKeys.USERS_ADMIN, "organizations-order-by-col",
119                                            "name");
120                                    orderByType = preferences.getValue(
121                                            PortletKeys.USERS_ADMIN, "organizations-order-by-type",
122                                            "asc");
123                            }
124    
125                            OrderByComparator orderByComparator =
126                                    UsersAdminUtil.getOrganizationOrderByComparator(
127                                            orderByCol, orderByType);
128    
129                            setOrderableHeaders(orderableHeaders);
130                            setOrderByCol(orderByCol);
131                            setOrderByType(orderByType);
132                            setOrderByComparator(orderByComparator);
133                    }
134                    catch (Exception e) {
135                            _log.error(e);
136                    }
137            }
138    
139            private static Log _log = LogFactoryUtil.getLog(OrganizationSearch.class);
140    
141    }