1   /**
2    * Copyright (c) 2000-2008 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.util.OrderByComparator;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.kernel.util.Validator;
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.PortletURL;
40  import javax.portlet.RenderRequest;
41  
42  import org.apache.commons.logging.Log;
43  import org.apache.commons.logging.LogFactory;
44  
45  /**
46   * <a href="RoleSearch.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   *
50   */
51  public class RoleSearch extends SearchContainer {
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("type");
59          headerNames.add("description");
60  
61          orderableHeaders.put("name", "name");
62          orderableHeaders.put("type", "type");
63          orderableHeaders.put("description", "description");
64      }
65  
66      public static final String EMPTY_RESULTS_MESSAGE = "no-roles-were-found";
67  
68      public RoleSearch(RenderRequest req, PortletURL iteratorURL) {
69          super(req, new RoleDisplayTerms(req), new RoleSearchTerms(req),
70                DEFAULT_CUR_PARAM, DEFAULT_DELTA, iteratorURL, headerNames,
71                EMPTY_RESULTS_MESSAGE);
72  
73          RoleDisplayTerms displayTerms = (RoleDisplayTerms)getDisplayTerms();
74  
75          iteratorURL.setParameter(RoleDisplayTerms.NAME, displayTerms.getName());
76          iteratorURL.setParameter(
77              RoleDisplayTerms.DESCRIPTION, displayTerms.getDescription());
78          iteratorURL.setParameter(
79              RoleDisplayTerms.TYPE, String.valueOf(displayTerms.getType()));
80  
81          try {
82              PortalPreferences prefs =
83                  PortletPreferencesFactoryUtil.getPortalPreferences(req);
84  
85              String orderByCol = ParamUtil.getString(req, "orderByCol");
86              String orderByType = ParamUtil.getString(req, "orderByType");
87  
88              if (Validator.isNotNull(orderByCol) &&
89                  Validator.isNotNull(orderByType)) {
90  
91                  prefs.setValue(
92                      PortletKeys.ENTERPRISE_ADMIN, "roles-order-by-col",
93                      orderByCol);
94                  prefs.setValue(
95                      PortletKeys.ENTERPRISE_ADMIN, "roles-order-by-type",
96                      orderByType);
97              }
98              else {
99                  orderByCol = prefs.getValue(
100                     PortletKeys.ENTERPRISE_ADMIN, "roles-order-by-col", "name");
101                 orderByType = prefs.getValue(
102                     PortletKeys.ENTERPRISE_ADMIN, "roles-order-by-type", "asc");
103             }
104 
105             OrderByComparator orderByComparator =
106                 EnterpriseAdminUtil.getRoleOrderByComparator(
107                     orderByCol, orderByType);
108 
109             setOrderableHeaders(orderableHeaders);
110             setOrderByCol(orderByCol);
111             setOrderByType(orderByType);
112             setOrderByComparator(orderByComparator);
113         }
114         catch (Exception e) {
115             _log.error(e);
116         }
117     }
118 
119     private static Log _log = LogFactory.getLog(RoleSearch.class);
120 
121 }