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