1   /**
2    * Copyright (c) 2000-2009 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.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.Role;
32  import com.liferay.portal.util.PortletKeys;
33  import com.liferay.portal.util.PropsValues;
34  import com.liferay.portlet.PortalPreferences;
35  import com.liferay.portlet.PortletPreferencesFactoryUtil;
36  import com.liferay.portlet.enterpriseadmin.util.EnterpriseAdminUtil;
37  
38  import java.util.ArrayList;
39  import java.util.HashMap;
40  import java.util.List;
41  import java.util.Map;
42  
43  import javax.portlet.PortletRequest;
44  import javax.portlet.PortletURL;
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  
61          if ((PropsValues.ROLES_COMMUNITY_SUBTYPES.length > 0) ||
62              (PropsValues.ROLES_ORGANIZATION_SUBTYPES.length > 0) ||
63              (PropsValues.ROLES_REGULAR_SUBTYPES.length > 0)) {
64  
65              headerNames.add("subtype");
66          }
67  
68          headerNames.add("description");
69  
70          orderableHeaders.put("name", "name");
71          orderableHeaders.put("type", "type");
72          orderableHeaders.put("description", "description");
73      }
74  
75      public static final String EMPTY_RESULTS_MESSAGE = "no-roles-were-found";
76  
77      public RoleSearch(PortletRequest portletRequest, PortletURL iteratorURL) {
78          super(
79              portletRequest, new RoleDisplayTerms(portletRequest),
80              new RoleSearchTerms(portletRequest), DEFAULT_CUR_PARAM,
81              DEFAULT_DELTA, iteratorURL, headerNames, EMPTY_RESULTS_MESSAGE);
82  
83          RoleDisplayTerms displayTerms = (RoleDisplayTerms)getDisplayTerms();
84  
85          iteratorURL.setParameter(RoleDisplayTerms.NAME, displayTerms.getName());
86          iteratorURL.setParameter(
87              RoleDisplayTerms.DESCRIPTION, displayTerms.getDescription());
88          iteratorURL.setParameter(
89              RoleDisplayTerms.TYPE, String.valueOf(displayTerms.getType()));
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, "roles-order-by-col",
106                     orderByCol);
107                 preferences.setValue(
108                     PortletKeys.ENTERPRISE_ADMIN, "roles-order-by-type",
109                     orderByType);
110             }
111             else {
112                 orderByCol = preferences.getValue(
113                     PortletKeys.ENTERPRISE_ADMIN, "roles-order-by-col", "name");
114                 orderByType = preferences.getValue(
115                     PortletKeys.ENTERPRISE_ADMIN, "roles-order-by-type", "asc");
116             }
117 
118             OrderByComparator orderByComparator =
119                 EnterpriseAdminUtil.getRoleOrderByComparator(
120                     orderByCol, orderByType);
121 
122             setOrderableHeaders(orderableHeaders);
123             setOrderByCol(orderByCol);
124             setOrderByType(orderByType);
125             setOrderByComparator(orderByComparator);
126         }
127         catch (Exception e) {
128             _log.error(e);
129         }
130     }
131 
132     private static Log _log = LogFactoryUtil.getLog(RoleSearch.class);
133 
134 }