1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class RoleSearch extends SearchContainer<Role> {
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  
60          if ((PropsValues.ROLES_COMMUNITY_SUBTYPES.length > 0) ||
61              (PropsValues.ROLES_ORGANIZATION_SUBTYPES.length > 0) ||
62              (PropsValues.ROLES_REGULAR_SUBTYPES.length > 0)) {
63  
64              headerNames.add("subtype");
65          }
66  
67          headerNames.add("description");
68  
69          orderableHeaders.put("name", "name");
70          orderableHeaders.put("type", "type");
71          orderableHeaders.put("description", "description");
72      }
73  
74      public static final String EMPTY_RESULTS_MESSAGE = "no-roles-were-found";
75  
76      public RoleSearch(PortletRequest portletRequest, PortletURL iteratorURL) {
77          super(
78              portletRequest, new RoleDisplayTerms(portletRequest),
79              new RoleSearchTerms(portletRequest), DEFAULT_CUR_PARAM,
80              DEFAULT_DELTA, iteratorURL, headerNames, EMPTY_RESULTS_MESSAGE);
81  
82          RoleDisplayTerms displayTerms = (RoleDisplayTerms)getDisplayTerms();
83  
84          iteratorURL.setParameter(RoleDisplayTerms.NAME, displayTerms.getName());
85          iteratorURL.setParameter(
86              RoleDisplayTerms.DESCRIPTION, displayTerms.getDescription());
87          iteratorURL.setParameter(
88              RoleDisplayTerms.TYPE, String.valueOf(displayTerms.getType()));
89  
90          try {
91              PortalPreferences preferences =
92                  PortletPreferencesFactoryUtil.getPortalPreferences(
93                      portletRequest);
94  
95              String orderByCol = ParamUtil.getString(
96                  portletRequest, "orderByCol");
97              String orderByType = ParamUtil.getString(
98                  portletRequest, "orderByType");
99  
100             if (Validator.isNotNull(orderByCol) &&
101                 Validator.isNotNull(orderByType)) {
102 
103                 preferences.setValue(
104                     PortletKeys.ENTERPRISE_ADMIN, "roles-order-by-col",
105                     orderByCol);
106                 preferences.setValue(
107                     PortletKeys.ENTERPRISE_ADMIN, "roles-order-by-type",
108                     orderByType);
109             }
110             else {
111                 orderByCol = preferences.getValue(
112                     PortletKeys.ENTERPRISE_ADMIN, "roles-order-by-col", "name");
113                 orderByType = preferences.getValue(
114                     PortletKeys.ENTERPRISE_ADMIN, "roles-order-by-type", "asc");
115             }
116 
117             OrderByComparator orderByComparator =
118                 EnterpriseAdminUtil.getRoleOrderByComparator(
119                     orderByCol, orderByType);
120 
121             setOrderableHeaders(orderableHeaders);
122             setOrderByCol(orderByCol);
123             setOrderByType(orderByType);
124             setOrderByComparator(orderByComparator);
125         }
126         catch (Exception e) {
127             _log.error(e);
128         }
129     }
130 
131     private static Log _log = LogFactoryUtil.getLog(RoleSearch.class);
132 
133 }