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