1   /**
2    * Copyright (c) 2000-2008 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.util.OrderByComparator;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.model.Group;
30  import com.liferay.portal.util.PortletKeys;
31  import com.liferay.portlet.PortalPreferences;
32  import com.liferay.portlet.PortletPreferencesFactoryUtil;
33  import com.liferay.portlet.enterpriseadmin.util.EnterpriseAdminUtil;
34  
35  import java.util.ArrayList;
36  import java.util.HashMap;
37  import java.util.List;
38  import java.util.Map;
39  
40  import javax.portlet.PortletURL;
41  import javax.portlet.RenderRequest;
42  
43  import org.apache.commons.logging.Log;
44  import org.apache.commons.logging.LogFactory;
45  
46  /**
47   * <a href="GroupSearch.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Brian Wing Shun Chan
50   *
51   */
52  public class GroupSearch extends SearchContainer<Group> {
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          orderableHeaders.put("name", "name");
62          orderableHeaders.put("type", "type");
63      }
64  
65      public static final String EMPTY_RESULTS_MESSAGE =
66          "no-communities-were-found";
67  
68      public GroupSearch(RenderRequest renderRequest, PortletURL iteratorURL) {
69          super(
70              renderRequest, new GroupDisplayTerms(renderRequest),
71              new GroupSearchTerms(renderRequest), DEFAULT_CUR_PARAM,
72              DEFAULT_DELTA, iteratorURL, headerNames, EMPTY_RESULTS_MESSAGE);
73  
74          GroupDisplayTerms displayTerms = (GroupDisplayTerms)getDisplayTerms();
75  
76          iteratorURL.setParameter(
77              GroupDisplayTerms.NAME, displayTerms.getName());
78          iteratorURL.setParameter(
79              GroupDisplayTerms.DESCRIPTION, displayTerms.getDescription());
80  
81          try {
82              PortalPreferences prefs =
83                  PortletPreferencesFactoryUtil.getPortalPreferences(
84                      renderRequest);
85  
86              String orderByCol = ParamUtil.getString(
87                  renderRequest, "orderByCol");
88              String orderByType = ParamUtil.getString(
89                  renderRequest, "orderByType");
90  
91              if (Validator.isNotNull(orderByCol) &&
92                  Validator.isNotNull(orderByType)) {
93  
94                  prefs.setValue(
95                      PortletKeys.ENTERPRISE_ADMIN, "groups-order-by-col",
96                      orderByCol);
97                  prefs.setValue(
98                      PortletKeys.ENTERPRISE_ADMIN, "groups-order-by-type",
99                      orderByType);
100             }
101             else {
102                 orderByCol = prefs.getValue(
103                     PortletKeys.ENTERPRISE_ADMIN, "groups-order-by-col",
104                     "name");
105                 orderByType = prefs.getValue(
106                     PortletKeys.ENTERPRISE_ADMIN, "groups-order-by-type",
107                     "asc");
108             }
109 
110             OrderByComparator orderByComparator =
111                 EnterpriseAdminUtil.getGroupOrderByComparator(
112                     orderByCol, orderByType);
113 
114             setOrderableHeaders(orderableHeaders);
115             setOrderByCol(orderByCol);
116             setOrderByType(orderByType);
117             setOrderByComparator(orderByComparator);
118         }
119         catch (Exception e) {
120             _log.error(e);
121         }
122     }
123 
124     private static Log _log = LogFactory.getLog(GroupSearch.class);
125 
126 }