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