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.JavaConstants;
27  import com.liferay.portal.kernel.util.OrderByComparator;
28  import com.liferay.portal.kernel.util.ParamUtil;
29  import com.liferay.portal.kernel.util.Validator;
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.PortletConfig;
41  import javax.portlet.PortletURL;
42  import javax.portlet.RenderRequest;
43  
44  import org.apache.commons.logging.Log;
45  import org.apache.commons.logging.LogFactory;
46  
47  /**
48   * <a href="UserSearch.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   *
52   */
53  public class UserSearch extends SearchContainer {
54  
55      static List<String> headerNames = new ArrayList<String>();
56      static Map<String, String> orderableHeaders = new HashMap<String, String>();
57  
58      static {
59          headerNames.add("first-name");
60          headerNames.add("last-name");
61          headerNames.add("screen-name");
62          //headerNames.add("email-address");
63          headerNames.add("job-title");
64          headerNames.add("organizations");
65  
66          orderableHeaders.put("first-name", "first-name");
67          orderableHeaders.put("last-name", "last-name");
68          orderableHeaders.put("screen-name", "screen-name");
69          //orderableHeaders.put("email-address", "email-address");
70          orderableHeaders.put("job-title", "job-title");
71      }
72  
73      public static final String EMPTY_RESULTS_MESSAGE = "no-users-were-found";
74  
75      public UserSearch(RenderRequest req, PortletURL iteratorURL) {
76          super(req, new UserDisplayTerms(req), new UserSearchTerms(req),
77                DEFAULT_CUR_PARAM, DEFAULT_DELTA, iteratorURL, headerNames,
78                EMPTY_RESULTS_MESSAGE);
79  
80          PortletConfig portletConfig = (PortletConfig)req.getAttribute(
81              JavaConstants.JAVAX_PORTLET_CONFIG);
82  
83          UserDisplayTerms displayTerms = (UserDisplayTerms)getDisplayTerms();
84          UserSearchTerms searchTerms = (UserSearchTerms)getSearchTerms();
85  
86          String portletName = portletConfig.getPortletName();
87  
88          if (!portletName.equals(PortletKeys.ENTERPRISE_ADMIN) &&
89              !portletName.equals(PortletKeys.ORGANIZATION_ADMIN)) {
90  
91              displayTerms.setActive(true);
92              searchTerms.setActive(true);
93          }
94  
95          iteratorURL.setParameter(
96              UserDisplayTerms.FIRST_NAME, displayTerms.getFirstName());
97          iteratorURL.setParameter(
98              UserDisplayTerms.MIDDLE_NAME, displayTerms.getMiddleName());
99          iteratorURL.setParameter(
100             UserDisplayTerms.LAST_NAME, displayTerms.getLastName());
101         iteratorURL.setParameter(
102             UserDisplayTerms.SCREEN_NAME, displayTerms.getScreenName());
103         iteratorURL.setParameter(
104             UserDisplayTerms.EMAIL_ADDRESS, displayTerms.getEmailAddress());
105         iteratorURL.setParameter(
106             UserDisplayTerms.ACTIVE, String.valueOf(displayTerms.isActive()));
107         iteratorURL.setParameter(
108             UserDisplayTerms.ORGANIZATION_ID,
109             String.valueOf(displayTerms.getOrganizationId()));
110         iteratorURL.setParameter(
111             UserDisplayTerms.ROLE_ID, String.valueOf(displayTerms.getRoleId()));
112         iteratorURL.setParameter(
113             UserDisplayTerms.USER_GROUP_ID,
114             String.valueOf(displayTerms.getUserGroupId()));
115 
116         try {
117             PortalPreferences prefs =
118                 PortletPreferencesFactoryUtil.getPortalPreferences(req);
119 
120             String orderByCol = ParamUtil.getString(req, "orderByCol");
121             String orderByType = ParamUtil.getString(req, "orderByType");
122 
123             if (Validator.isNotNull(orderByCol) &&
124                 Validator.isNotNull(orderByType)) {
125 
126                 prefs.setValue(
127                     PortletKeys.ENTERPRISE_ADMIN, "users-order-by-col",
128                     orderByCol);
129                 prefs.setValue(
130                     PortletKeys.ENTERPRISE_ADMIN, "users-order-by-type",
131                     orderByType);
132             }
133             else {
134                 orderByCol = prefs.getValue(
135                     PortletKeys.ENTERPRISE_ADMIN, "users-order-by-col",
136                     "last-name");
137                 orderByType = prefs.getValue(
138                     PortletKeys.ENTERPRISE_ADMIN, "users-order-by-type", "asc");
139             }
140 
141             OrderByComparator orderByComparator =
142                 EnterpriseAdminUtil.getUserOrderByComparator(
143                     orderByCol, orderByType);
144 
145             setOrderableHeaders(orderableHeaders);
146             setOrderByCol(orderByCol);
147             setOrderByType(orderByType);
148             setOrderByComparator(orderByComparator);
149         }
150         catch (Exception e) {
151             _log.error(e);
152         }
153     }
154 
155     private static Log _log = LogFactory.getLog(UserSearch.class);
156 
157 }