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.model.User;
31  import com.liferay.portal.util.PortletKeys;
32  import com.liferay.portlet.PortalPreferences;
33  import com.liferay.portlet.PortletPreferencesFactoryUtil;
34  import com.liferay.portlet.enterpriseadmin.util.EnterpriseAdminUtil;
35  
36  import java.util.ArrayList;
37  import java.util.HashMap;
38  import java.util.List;
39  import java.util.Map;
40  
41  import javax.portlet.PortletConfig;
42  import javax.portlet.PortletURL;
43  import javax.portlet.RenderRequest;
44  
45  import org.apache.commons.logging.Log;
46  import org.apache.commons.logging.LogFactory;
47  
48  /**
49   * <a href="UserSearch.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Brian Wing Shun Chan
52   *
53   */
54  public class UserSearch extends SearchContainer<User> {
55  
56      static List<String> headerNames = new ArrayList<String>();
57      static Map<String, String> orderableHeaders = new HashMap<String, String>();
58  
59      static {
60          headerNames.add("first-name");
61          headerNames.add("last-name");
62          headerNames.add("screen-name");
63          //headerNames.add("email-address");
64          headerNames.add("job-title");
65          headerNames.add("organizations");
66  
67          orderableHeaders.put("first-name", "first-name");
68          orderableHeaders.put("last-name", "last-name");
69          orderableHeaders.put("screen-name", "screen-name");
70          //orderableHeaders.put("email-address", "email-address");
71          orderableHeaders.put("job-title", "job-title");
72      }
73  
74      public static final String EMPTY_RESULTS_MESSAGE = "no-users-were-found";
75  
76      public UserSearch(RenderRequest renderRequest, PortletURL iteratorURL) {
77          super(
78              renderRequest, new UserDisplayTerms(renderRequest),
79              new UserSearchTerms(renderRequest), DEFAULT_CUR_PARAM,
80              DEFAULT_DELTA, iteratorURL, headerNames, EMPTY_RESULTS_MESSAGE);
81  
82          PortletConfig portletConfig = (PortletConfig)renderRequest.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.ORGANIZATION_ADMIN)) {
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 prefs =
120                 PortletPreferencesFactoryUtil.getPortalPreferences(
121                     renderRequest);
122 
123             String orderByCol = ParamUtil.getString(
124                 renderRequest, "orderByCol");
125             String orderByType = ParamUtil.getString(
126                 renderRequest, "orderByType");
127 
128             if (Validator.isNotNull(orderByCol) &&
129                 Validator.isNotNull(orderByType)) {
130 
131                 prefs.setValue(
132                     PortletKeys.ENTERPRISE_ADMIN, "users-order-by-col",
133                     orderByCol);
134                 prefs.setValue(
135                     PortletKeys.ENTERPRISE_ADMIN, "users-order-by-type",
136                     orderByType);
137             }
138             else {
139                 orderByCol = prefs.getValue(
140                     PortletKeys.ENTERPRISE_ADMIN, "users-order-by-col",
141                     "last-name");
142                 orderByType = prefs.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 = LogFactory.getLog(UserSearch.class);
161 
162 }