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