1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.enterpriseadmin.search;
21  
22  import com.liferay.portal.kernel.dao.search.SearchContainer;
23  import com.liferay.portal.kernel.log.Log;
24  import com.liferay.portal.kernel.log.LogFactoryUtil;
25  import com.liferay.portal.kernel.util.JavaConstants;
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.User;
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.PortletRequest;
42  import javax.portlet.PortletURL;
43  
44  /**
45   * <a href="UserSearch.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   *
49   */
50  public class UserSearch extends SearchContainer<User> {
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("first-name");
57          headerNames.add("last-name");
58          headerNames.add("screen-name");
59          //headerNames.add("email-address");
60          headerNames.add("job-title");
61          headerNames.add("organizations");
62  
63          orderableHeaders.put("first-name", "first-name");
64          orderableHeaders.put("last-name", "last-name");
65          orderableHeaders.put("screen-name", "screen-name");
66          //orderableHeaders.put("email-address", "email-address");
67          orderableHeaders.put("job-title", "job-title");
68      }
69  
70      public static final String EMPTY_RESULTS_MESSAGE = "no-users-were-found";
71  
72      public UserSearch(PortletRequest portletRequest, PortletURL iteratorURL) {
73          super(
74              portletRequest, new UserDisplayTerms(portletRequest),
75              new UserSearchTerms(portletRequest), DEFAULT_CUR_PARAM,
76              DEFAULT_DELTA, iteratorURL, headerNames, EMPTY_RESULTS_MESSAGE);
77  
78          PortletConfig portletConfig =
79              (PortletConfig)portletRequest.getAttribute(
80                  JavaConstants.JAVAX_PORTLET_CONFIG);
81  
82          UserDisplayTerms displayTerms = (UserDisplayTerms)getDisplayTerms();
83          UserSearchTerms searchTerms = (UserSearchTerms)getSearchTerms();
84  
85          String portletName = portletConfig.getPortletName();
86  
87          if (!portletName.equals(PortletKeys.ENTERPRISE_ADMIN) &&
88              !portletName.equals(PortletKeys.ORGANIZATION_ADMIN)) {
89  
90              displayTerms.setActive(true);
91              searchTerms.setActive(true);
92          }
93  
94          iteratorURL.setParameter(
95              UserDisplayTerms.FIRST_NAME, displayTerms.getFirstName());
96          iteratorURL.setParameter(
97              UserDisplayTerms.MIDDLE_NAME, displayTerms.getMiddleName());
98          iteratorURL.setParameter(
99              UserDisplayTerms.LAST_NAME, displayTerms.getLastName());
100         iteratorURL.setParameter(
101             UserDisplayTerms.SCREEN_NAME, displayTerms.getScreenName());
102         iteratorURL.setParameter(
103             UserDisplayTerms.EMAIL_ADDRESS, displayTerms.getEmailAddress());
104         iteratorURL.setParameter(
105             UserDisplayTerms.ACTIVE, String.valueOf(displayTerms.isActive()));
106         iteratorURL.setParameter(
107             UserDisplayTerms.ORGANIZATION_ID,
108             String.valueOf(displayTerms.getOrganizationId()));
109         iteratorURL.setParameter(
110             UserDisplayTerms.ROLE_ID, String.valueOf(displayTerms.getRoleId()));
111         iteratorURL.setParameter(
112             UserDisplayTerms.USER_GROUP_ID,
113             String.valueOf(displayTerms.getUserGroupId()));
114 
115         try {
116             PortalPreferences prefs =
117                 PortletPreferencesFactoryUtil.getPortalPreferences(
118                     portletRequest);
119 
120             String orderByCol = ParamUtil.getString(
121                 portletRequest, "orderByCol");
122             String orderByType = ParamUtil.getString(
123                 portletRequest, "orderByType");
124 
125             if (Validator.isNotNull(orderByCol) &&
126                 Validator.isNotNull(orderByType)) {
127 
128                 prefs.setValue(
129                     PortletKeys.ENTERPRISE_ADMIN, "users-order-by-col",
130                     orderByCol);
131                 prefs.setValue(
132                     PortletKeys.ENTERPRISE_ADMIN, "users-order-by-type",
133                     orderByType);
134             }
135             else {
136                 orderByCol = prefs.getValue(
137                     PortletKeys.ENTERPRISE_ADMIN, "users-order-by-col",
138                     "last-name");
139                 orderByType = prefs.getValue(
140                     PortletKeys.ENTERPRISE_ADMIN, "users-order-by-type", "asc");
141             }
142 
143             OrderByComparator orderByComparator =
144                 EnterpriseAdminUtil.getUserOrderByComparator(
145                     orderByCol, orderByType);
146 
147             setOrderableHeaders(orderableHeaders);
148             setOrderByCol(orderByCol);
149             setOrderByType(orderByType);
150             setOrderByComparator(orderByComparator);
151         }
152         catch (Exception e) {
153             _log.error(e);
154         }
155     }
156 
157     private static Log _log = LogFactoryUtil.getLog(UserSearch.class);
158 
159 }