1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.enterpriseadmin.search;
24  
25  import com.liferay.portal.kernel.dao.search.SearchContainer;
26  import com.liferay.portal.kernel.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.util.OrderByComparator;
29  import com.liferay.portal.kernel.util.ParamUtil;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.model.Organization;
32  import com.liferay.portal.util.PortletKeys;
33  import com.liferay.portlet.PortalPreferences;
34  import com.liferay.portlet.PortletPreferencesFactoryUtil;
35  import com.liferay.portlet.enterpriseadmin.util.EnterpriseAdminUtil;
36  
37  import java.util.ArrayList;
38  import java.util.HashMap;
39  import java.util.List;
40  import java.util.Map;
41  
42  import javax.portlet.PortletRequest;
43  import javax.portlet.PortletURL;
44  
45  /**
46   * <a href="OrganizationSearch.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   *
50   */
51  public class OrganizationSearch extends SearchContainer<Organization> {
52  
53      static List<String> headerNames = new ArrayList<String>();
54      static Map<String, String> orderableHeaders = new HashMap<String, String>();
55  
56      static {
57          headerNames.add("name");
58          headerNames.add("parent-organization");
59          headerNames.add("type");
60          headerNames.add("city");
61          headerNames.add("region");
62          headerNames.add("country");
63  
64          orderableHeaders.put("name", "name");
65          orderableHeaders.put("type", "type");
66      }
67  
68      public static final String EMPTY_RESULTS_MESSAGE =
69          "no-organizations-were-found";
70  
71      public OrganizationSearch(
72          PortletRequest portletRequest, PortletURL iteratorURL) {
73  
74          super(
75              portletRequest, new OrganizationDisplayTerms(portletRequest),
76              new OrganizationSearchTerms(portletRequest), DEFAULT_CUR_PARAM,
77              DEFAULT_DELTA, iteratorURL, headerNames, EMPTY_RESULTS_MESSAGE);
78  
79          OrganizationDisplayTerms displayTerms =
80              (OrganizationDisplayTerms)getDisplayTerms();
81  
82          iteratorURL.setParameter(
83              OrganizationDisplayTerms.NAME, displayTerms.getName());
84          iteratorURL.setParameter(
85              OrganizationDisplayTerms.STREET, displayTerms.getStreet());
86          iteratorURL.setParameter(
87              OrganizationDisplayTerms.CITY, displayTerms.getCity());
88          iteratorURL.setParameter(
89              OrganizationDisplayTerms.ZIP, displayTerms.getZip());
90          iteratorURL.setParameter(
91              OrganizationDisplayTerms.COUNTRY_ID,
92              String.valueOf(displayTerms.getCountryId()));
93          iteratorURL.setParameter(
94              OrganizationDisplayTerms.REGION_ID,
95              String.valueOf(displayTerms.getRegionId()));
96          iteratorURL.setParameter(
97              OrganizationDisplayTerms.PARENT_ORGANIZATION_ID,
98              String.valueOf(displayTerms.getParentOrganizationId()));
99  
100         try {
101             PortalPreferences preferences =
102                 PortletPreferencesFactoryUtil.getPortalPreferences(
103                     portletRequest);
104 
105             String orderByCol = ParamUtil.getString(
106                 portletRequest, "orderByCol");
107             String orderByType = ParamUtil.getString(
108                 portletRequest, "orderByType");
109 
110             if (Validator.isNotNull(orderByCol) &&
111                 Validator.isNotNull(orderByType)) {
112 
113                 preferences.setValue(
114                     PortletKeys.ENTERPRISE_ADMIN, "organizations-order-by-col",
115                     orderByCol);
116                 preferences.setValue(
117                     PortletKeys.ENTERPRISE_ADMIN, "organizations-order-by-type",
118                     orderByType);
119             }
120             else {
121                 orderByCol = preferences.getValue(
122                     PortletKeys.ENTERPRISE_ADMIN, "organizations-order-by-col",
123                     "name");
124                 orderByType = preferences.getValue(
125                     PortletKeys.ENTERPRISE_ADMIN, "organizations-order-by-type",
126                     "asc");
127             }
128 
129             OrderByComparator orderByComparator =
130                 EnterpriseAdminUtil.getOrganizationOrderByComparator(
131                     orderByCol, orderByType);
132 
133             setOrderableHeaders(orderableHeaders);
134             setOrderByCol(orderByCol);
135             setOrderByType(orderByType);
136             setOrderByComparator(orderByComparator);
137         }
138         catch (Exception e) {
139             _log.error(e);
140         }
141     }
142 
143     private static Log _log = LogFactoryUtil.getLog(OrganizationSearch.class);
144 
145 }