1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class OrganizationSearch extends SearchContainer<Organization> {
51  
52      static List<String> headerNames = new ArrayList<String>();
53      static Map<String, String> orderableHeaders = new HashMap<String, String>();
54  
55      static {
56          headerNames.add("name");
57          headerNames.add("parent-organization");
58          headerNames.add("type");
59          headerNames.add("city");
60          headerNames.add("region");
61          headerNames.add("country");
62  
63          orderableHeaders.put("name", "name");
64          orderableHeaders.put("type", "type");
65      }
66  
67      public static final String EMPTY_RESULTS_MESSAGE =
68          "no-organizations-were-found";
69  
70      public OrganizationSearch(
71          PortletRequest portletRequest, PortletURL iteratorURL) {
72  
73          super(
74              portletRequest, new OrganizationDisplayTerms(portletRequest),
75              new OrganizationSearchTerms(portletRequest), DEFAULT_CUR_PARAM,
76              DEFAULT_DELTA, iteratorURL, headerNames, EMPTY_RESULTS_MESSAGE);
77  
78          OrganizationDisplayTerms displayTerms =
79              (OrganizationDisplayTerms)getDisplayTerms();
80  
81          iteratorURL.setParameter(
82              OrganizationDisplayTerms.NAME, displayTerms.getName());
83          iteratorURL.setParameter(
84              OrganizationDisplayTerms.STREET, displayTerms.getStreet());
85          iteratorURL.setParameter(
86              OrganizationDisplayTerms.CITY, displayTerms.getCity());
87          iteratorURL.setParameter(
88              OrganizationDisplayTerms.ZIP, displayTerms.getZip());
89          iteratorURL.setParameter(
90              OrganizationDisplayTerms.COUNTRY_ID,
91              String.valueOf(displayTerms.getCountryId()));
92          iteratorURL.setParameter(
93              OrganizationDisplayTerms.REGION_ID,
94              String.valueOf(displayTerms.getRegionId()));
95          iteratorURL.setParameter(
96              OrganizationDisplayTerms.PARENT_ORGANIZATION_ID,
97              String.valueOf(displayTerms.getParentOrganizationId()));
98  
99          try {
100             PortalPreferences prefs =
101                 PortletPreferencesFactoryUtil.getPortalPreferences(
102                     portletRequest);
103 
104             String orderByCol = ParamUtil.getString(
105                 portletRequest, "orderByCol");
106             String orderByType = ParamUtil.getString(
107                 portletRequest, "orderByType");
108 
109             if (Validator.isNotNull(orderByCol) &&
110                 Validator.isNotNull(orderByType)) {
111 
112                 prefs.setValue(
113                     PortletKeys.ENTERPRISE_ADMIN, "organizations-order-by-col",
114                     orderByCol);
115                 prefs.setValue(
116                     PortletKeys.ENTERPRISE_ADMIN, "organizations-order-by-type",
117                     orderByType);
118             }
119             else {
120                 orderByCol = prefs.getValue(
121                     PortletKeys.ENTERPRISE_ADMIN, "organizations-order-by-col",
122                     "name");
123                 orderByType = prefs.getValue(
124                     PortletKeys.ENTERPRISE_ADMIN, "organizations-order-by-type",
125                     "asc");
126             }
127 
128             OrderByComparator orderByComparator =
129                 EnterpriseAdminUtil.getOrganizationOrderByComparator(
130                     orderByCol, orderByType);
131 
132             setOrderableHeaders(orderableHeaders);
133             setOrderByCol(orderByCol);
134             setOrderByType(orderByType);
135             setOrderByComparator(orderByComparator);
136         }
137         catch (Exception e) {
138             _log.error(e);
139         }
140     }
141 
142     private static Log _log = LogFactoryUtil.getLog(OrganizationSearch.class);
143 
144 }