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