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.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.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 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                    super(
064                            portletRequest, new OrganizationDisplayTerms(portletRequest),
065                            new OrganizationSearchTerms(portletRequest), DEFAULT_CUR_PARAM,
066                            DEFAULT_DELTA, iteratorURL, headerNames, EMPTY_RESULTS_MESSAGE);
067    
068                    OrganizationDisplayTerms displayTerms =
069                            (OrganizationDisplayTerms)getDisplayTerms();
070    
071                    iteratorURL.setParameter(
072                            OrganizationDisplayTerms.CITY, displayTerms.getCity());
073                    iteratorURL.setParameter(
074                            OrganizationDisplayTerms.COUNTRY_ID,
075                            String.valueOf(displayTerms.getCountryId()));
076                    iteratorURL.setParameter(
077                            OrganizationDisplayTerms.NAME, displayTerms.getName());
078                    iteratorURL.setParameter(
079                            OrganizationDisplayTerms.PARENT_ORGANIZATION_ID,
080                            String.valueOf(displayTerms.getParentOrganizationId()));
081                    iteratorURL.setParameter(
082                            OrganizationDisplayTerms.REGION_ID,
083                            String.valueOf(displayTerms.getRegionId()));
084                    iteratorURL.setParameter(
085                            OrganizationDisplayTerms.STREET, displayTerms.getStreet());
086                    iteratorURL.setParameter(
087                            OrganizationDisplayTerms.ZIP, displayTerms.getZip());
088    
089                    try {
090                            PortalPreferences preferences =
091                                    PortletPreferencesFactoryUtil.getPortalPreferences(
092                                            portletRequest);
093    
094                            String orderByCol = ParamUtil.getString(
095                                    portletRequest, "orderByCol");
096                            String orderByType = ParamUtil.getString(
097                                    portletRequest, "orderByType");
098    
099                            if (Validator.isNotNull(orderByCol) &&
100                                    Validator.isNotNull(orderByType)) {
101    
102                                    preferences.setValue(
103                                            PortletKeys.ENTERPRISE_ADMIN, "organizations-order-by-col",
104                                            orderByCol);
105                                    preferences.setValue(
106                                            PortletKeys.ENTERPRISE_ADMIN, "organizations-order-by-type",
107                                            orderByType);
108                            }
109                            else {
110                                    orderByCol = preferences.getValue(
111                                            PortletKeys.ENTERPRISE_ADMIN, "organizations-order-by-col",
112                                            "name");
113                                    orderByType = preferences.getValue(
114                                            PortletKeys.ENTERPRISE_ADMIN, "organizations-order-by-type",
115                                            "asc");
116                            }
117    
118                            OrderByComparator orderByComparator =
119                                    EnterpriseAdminUtil.getOrganizationOrderByComparator(
120                                            orderByCol, orderByType);
121    
122                            setOrderableHeaders(orderableHeaders);
123                            setOrderByCol(orderByCol);
124                            setOrderByType(orderByType);
125                            setOrderByComparator(orderByComparator);
126                    }
127                    catch (Exception e) {
128                            _log.error(e);
129                    }
130            }
131    
132            private static Log _log = LogFactoryUtil.getLog(OrganizationSearch.class);
133    
134    }