1   /**
2    * Copyright (c) 2000-2009 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.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.util.JavaConstants;
29  import com.liferay.portal.kernel.util.OrderByComparator;
30  import com.liferay.portal.kernel.util.ParamUtil;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.model.User;
33  import com.liferay.portal.util.PortletKeys;
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.PortletConfig;
44  import javax.portlet.PortletRequest;
45  import javax.portlet.PortletURL;
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<User> {
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(PortletRequest portletRequest, PortletURL iteratorURL) {
76          super(
77              portletRequest, new UserDisplayTerms(portletRequest),
78              new UserSearchTerms(portletRequest), DEFAULT_CUR_PARAM,
79              DEFAULT_DELTA, iteratorURL, headerNames, EMPTY_RESULTS_MESSAGE);
80  
81          PortletConfig portletConfig =
82              (PortletConfig)portletRequest.getAttribute(
83                  JavaConstants.JAVAX_PORTLET_CONFIG);
84  
85          UserDisplayTerms displayTerms = (UserDisplayTerms)getDisplayTerms();
86          UserSearchTerms searchTerms = (UserSearchTerms)getSearchTerms();
87  
88          String portletName = portletConfig.getPortletName();
89  
90          if ((!portletName.equals(PortletKeys.ENTERPRISE_ADMIN)) &&
91              (!portletName.equals(PortletKeys.ENTERPRISE_ADMIN_USERS))) {
92  
93              displayTerms.setActive(true);
94              searchTerms.setActive(true);
95          }
96  
97          iteratorURL.setParameter(
98              UserDisplayTerms.FIRST_NAME, displayTerms.getFirstName());
99          iteratorURL.setParameter(
100             UserDisplayTerms.MIDDLE_NAME, displayTerms.getMiddleName());
101         iteratorURL.setParameter(
102             UserDisplayTerms.LAST_NAME, displayTerms.getLastName());
103         iteratorURL.setParameter(
104             UserDisplayTerms.SCREEN_NAME, displayTerms.getScreenName());
105         iteratorURL.setParameter(
106             UserDisplayTerms.EMAIL_ADDRESS, displayTerms.getEmailAddress());
107         iteratorURL.setParameter(
108             UserDisplayTerms.ACTIVE, String.valueOf(displayTerms.isActive()));
109         iteratorURL.setParameter(
110             UserDisplayTerms.ORGANIZATION_ID,
111             String.valueOf(displayTerms.getOrganizationId()));
112         iteratorURL.setParameter(
113             UserDisplayTerms.ROLE_ID, String.valueOf(displayTerms.getRoleId()));
114         iteratorURL.setParameter(
115             UserDisplayTerms.USER_GROUP_ID,
116             String.valueOf(displayTerms.getUserGroupId()));
117 
118         try {
119             PortalPreferences preferences =
120                 PortletPreferencesFactoryUtil.getPortalPreferences(
121                     portletRequest);
122 
123             String orderByCol = ParamUtil.getString(
124                 portletRequest, "orderByCol");
125             String orderByType = ParamUtil.getString(
126                 portletRequest, "orderByType");
127 
128             if (Validator.isNotNull(orderByCol) &&
129                 Validator.isNotNull(orderByType)) {
130 
131                 preferences.setValue(
132                     PortletKeys.ENTERPRISE_ADMIN, "users-order-by-col",
133                     orderByCol);
134                 preferences.setValue(
135                     PortletKeys.ENTERPRISE_ADMIN, "users-order-by-type",
136                     orderByType);
137             }
138             else {
139                 orderByCol = preferences.getValue(
140                     PortletKeys.ENTERPRISE_ADMIN, "users-order-by-col",
141                     "last-name");
142                 orderByType = preferences.getValue(
143                     PortletKeys.ENTERPRISE_ADMIN, "users-order-by-type", "asc");
144             }
145 
146             OrderByComparator orderByComparator =
147                 EnterpriseAdminUtil.getUserOrderByComparator(
148                     orderByCol, orderByType);
149 
150             setOrderableHeaders(orderableHeaders);
151             setOrderByCol(orderByCol);
152             setOrderByType(orderByType);
153             setOrderByComparator(orderByComparator);
154         }
155         catch (Exception e) {
156             _log.error(e);
157         }
158     }
159 
160     private static Log _log = LogFactoryUtil.getLog(UserSearch.class);
161 
162 }