1   /**
2    * Copyright (c) 2000-2010 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   *
12   * 
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.Role;
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="RoleSearch.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   */
42  public class RoleSearch extends SearchContainer<Role> {
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("type");
50          headerNames.add("description");
51  
52          orderableHeaders.put("name", "name");
53          orderableHeaders.put("type", "type");
54          orderableHeaders.put("description", "description");
55      }
56  
57      public static final String EMPTY_RESULTS_MESSAGE = "no-roles-were-found";
58  
59      public RoleSearch(PortletRequest portletRequest, PortletURL iteratorURL) {
60          super(
61              portletRequest, new RoleDisplayTerms(portletRequest),
62              new RoleSearchTerms(portletRequest), DEFAULT_CUR_PARAM,
63              DEFAULT_DELTA, iteratorURL, headerNames, EMPTY_RESULTS_MESSAGE);
64  
65          RoleDisplayTerms displayTerms = (RoleDisplayTerms)getDisplayTerms();
66  
67          iteratorURL.setParameter(RoleDisplayTerms.NAME, displayTerms.getName());
68          iteratorURL.setParameter(
69              RoleDisplayTerms.DESCRIPTION, displayTerms.getDescription());
70          iteratorURL.setParameter(
71              RoleDisplayTerms.TYPE, String.valueOf(displayTerms.getType()));
72  
73          try {
74              PortalPreferences prefs =
75                  PortletPreferencesFactoryUtil.getPortalPreferences(
76                      portletRequest);
77  
78              String orderByCol = ParamUtil.getString(
79                  portletRequest, "orderByCol");
80              String orderByType = ParamUtil.getString(
81                  portletRequest, "orderByType");
82  
83              if (Validator.isNotNull(orderByCol) &&
84                  Validator.isNotNull(orderByType)) {
85  
86                  prefs.setValue(
87                      PortletKeys.ENTERPRISE_ADMIN, "roles-order-by-col",
88                      orderByCol);
89                  prefs.setValue(
90                      PortletKeys.ENTERPRISE_ADMIN, "roles-order-by-type",
91                      orderByType);
92              }
93              else {
94                  orderByCol = prefs.getValue(
95                      PortletKeys.ENTERPRISE_ADMIN, "roles-order-by-col", "name");
96                  orderByType = prefs.getValue(
97                      PortletKeys.ENTERPRISE_ADMIN, "roles-order-by-type", "asc");
98              }
99  
100             OrderByComparator orderByComparator =
101                 EnterpriseAdminUtil.getRoleOrderByComparator(
102                     orderByCol, orderByType);
103 
104             setOrderableHeaders(orderableHeaders);
105             setOrderByCol(orderByCol);
106             setOrderByType(orderByType);
107             setOrderByComparator(orderByComparator);
108         }
109         catch (Exception e) {
110             _log.error(e);
111         }
112     }
113 
114     private static Log _log = LogFactoryUtil.getLog(RoleSearch.class);
115 
116 }