1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.enterpriseadmin.search;
16  
17  import com.liferay.portal.kernel.dao.search.SearchContainer;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.util.OrderByComparator;
21  import com.liferay.portal.kernel.util.ParamUtil;
22  import com.liferay.portal.kernel.util.Validator;
23  import com.liferay.portal.model.Organization;
24  import com.liferay.portal.util.PortletKeys;
25  import com.liferay.portlet.PortalPreferences;
26  import com.liferay.portlet.PortletPreferencesFactoryUtil;
27  import com.liferay.portlet.enterpriseadmin.util.EnterpriseAdminUtil;
28  
29  import java.util.ArrayList;
30  import java.util.HashMap;
31  import java.util.List;
32  import java.util.Map;
33  
34  import javax.portlet.PortletRequest;
35  import javax.portlet.PortletURL;
36  
37  /**
38   * <a href="OrganizationSearch.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   */
42  public class OrganizationSearch extends SearchContainer<Organization> {
43  
44      static List<String> headerNames = new ArrayList<String>();
45      static Map<String, String> orderableHeaders = new HashMap<String, String>();
46  
47      static {
48          headerNames.add("name");
49          headerNames.add("parent-organization");
50          headerNames.add("type");
51          headerNames.add("city");
52          headerNames.add("region");
53          headerNames.add("country");
54  
55          orderableHeaders.put("name", "name");
56          orderableHeaders.put("type", "type");
57      }
58  
59      public static final String EMPTY_RESULTS_MESSAGE =
60          "no-organizations-were-found";
61  
62      public OrganizationSearch(
63          PortletRequest portletRequest, PortletURL iteratorURL) {
64  
65          super(
66              portletRequest, new OrganizationDisplayTerms(portletRequest),
67              new OrganizationSearchTerms(portletRequest), DEFAULT_CUR_PARAM,
68              DEFAULT_DELTA, iteratorURL, headerNames, EMPTY_RESULTS_MESSAGE);
69  
70          OrganizationDisplayTerms displayTerms =
71              (OrganizationDisplayTerms)getDisplayTerms();
72  
73          iteratorURL.setParameter(
74              OrganizationDisplayTerms.NAME, displayTerms.getName());
75          iteratorURL.setParameter(
76              OrganizationDisplayTerms.STREET, displayTerms.getStreet());
77          iteratorURL.setParameter(
78              OrganizationDisplayTerms.CITY, displayTerms.getCity());
79          iteratorURL.setParameter(
80              OrganizationDisplayTerms.ZIP, displayTerms.getZip());
81          iteratorURL.setParameter(
82              OrganizationDisplayTerms.COUNTRY_ID,
83              String.valueOf(displayTerms.getCountryId()));
84          iteratorURL.setParameter(
85              OrganizationDisplayTerms.REGION_ID,
86              String.valueOf(displayTerms.getRegionId()));
87          iteratorURL.setParameter(
88              OrganizationDisplayTerms.PARENT_ORGANIZATION_ID,
89              String.valueOf(displayTerms.getParentOrganizationId()));
90  
91          try {
92              PortalPreferences preferences =
93                  PortletPreferencesFactoryUtil.getPortalPreferences(
94                      portletRequest);
95  
96              String orderByCol = ParamUtil.getString(
97                  portletRequest, "orderByCol");
98              String orderByType = ParamUtil.getString(
99                  portletRequest, "orderByType");
100 
101             if (Validator.isNotNull(orderByCol) &&
102                 Validator.isNotNull(orderByType)) {
103 
104                 preferences.setValue(
105                     PortletKeys.ENTERPRISE_ADMIN, "organizations-order-by-col",
106                     orderByCol);
107                 preferences.setValue(
108                     PortletKeys.ENTERPRISE_ADMIN, "organizations-order-by-type",
109                     orderByType);
110             }
111             else {
112                 orderByCol = preferences.getValue(
113                     PortletKeys.ENTERPRISE_ADMIN, "organizations-order-by-col",
114                     "name");
115                 orderByType = preferences.getValue(
116                     PortletKeys.ENTERPRISE_ADMIN, "organizations-order-by-type",
117                     "asc");
118             }
119 
120             OrderByComparator orderByComparator =
121                 EnterpriseAdminUtil.getOrganizationOrderByComparator(
122                     orderByCol, orderByType);
123 
124             setOrderableHeaders(orderableHeaders);
125             setOrderByCol(orderByCol);
126             setOrderByType(orderByType);
127             setOrderByComparator(orderByComparator);
128         }
129         catch (Exception e) {
130             _log.error(e);
131         }
132     }
133 
134     private static Log _log = LogFactoryUtil.getLog(OrganizationSearch.class);
135 
136 }