1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class UserSearch extends SearchContainer<User> {
53  
54      static List<String> headerNames = new ArrayList<String>();
55      static Map<String, String> orderableHeaders = new HashMap<String, String>();
56  
57      static {
58          headerNames.add("first-name");
59          headerNames.add("last-name");
60          headerNames.add("screen-name");
61          //headerNames.add("email-address");
62          headerNames.add("job-title");
63          headerNames.add("organizations");
64  
65          orderableHeaders.put("first-name", "first-name");
66          orderableHeaders.put("last-name", "last-name");
67          orderableHeaders.put("screen-name", "screen-name");
68          //orderableHeaders.put("email-address", "email-address");
69          orderableHeaders.put("job-title", "job-title");
70      }
71  
72      public static final String EMPTY_RESULTS_MESSAGE = "no-users-were-found";
73  
74      public UserSearch(PortletRequest portletRequest, PortletURL iteratorURL) {
75          super(
76              portletRequest, new UserDisplayTerms(portletRequest),
77              new UserSearchTerms(portletRequest), DEFAULT_CUR_PARAM,
78              DEFAULT_DELTA, iteratorURL, headerNames, EMPTY_RESULTS_MESSAGE);
79  
80          PortletConfig portletConfig =
81              (PortletConfig)portletRequest.getAttribute(
82                  JavaConstants.JAVAX_PORTLET_CONFIG);
83  
84          UserDisplayTerms displayTerms = (UserDisplayTerms)getDisplayTerms();
85          UserSearchTerms searchTerms = (UserSearchTerms)getSearchTerms();
86  
87          String portletName = portletConfig.getPortletName();
88  
89          if (!portletName.equals(PortletKeys.ENTERPRISE_ADMIN) &&
90              !portletName.equals(PortletKeys.ORGANIZATION_ADMIN)) {
91  
92              displayTerms.setActive(true);
93              searchTerms.setActive(true);
94          }
95  
96          iteratorURL.setParameter(
97              UserDisplayTerms.FIRST_NAME, displayTerms.getFirstName());
98          iteratorURL.setParameter(
99              UserDisplayTerms.MIDDLE_NAME, displayTerms.getMiddleName());
100         iteratorURL.setParameter(
101             UserDisplayTerms.LAST_NAME, displayTerms.getLastName());
102         iteratorURL.setParameter(
103             UserDisplayTerms.SCREEN_NAME, displayTerms.getScreenName());
104         iteratorURL.setParameter(
105             UserDisplayTerms.EMAIL_ADDRESS, displayTerms.getEmailAddress());
106         iteratorURL.setParameter(
107             UserDisplayTerms.ACTIVE, String.valueOf(displayTerms.isActive()));
108         iteratorURL.setParameter(
109             UserDisplayTerms.ORGANIZATION_ID,
110             String.valueOf(displayTerms.getOrganizationId()));
111         iteratorURL.setParameter(
112             UserDisplayTerms.ROLE_ID, String.valueOf(displayTerms.getRoleId()));
113         iteratorURL.setParameter(
114             UserDisplayTerms.USER_GROUP_ID,
115             String.valueOf(displayTerms.getUserGroupId()));
116 
117         try {
118             PortalPreferences prefs =
119                 PortletPreferencesFactoryUtil.getPortalPreferences(
120                     portletRequest);
121 
122             String orderByCol = ParamUtil.getString(
123                 portletRequest, "orderByCol");
124             String orderByType = ParamUtil.getString(
125                 portletRequest, "orderByType");
126 
127             if (Validator.isNotNull(orderByCol) &&
128                 Validator.isNotNull(orderByType)) {
129 
130                 prefs.setValue(
131                     PortletKeys.ENTERPRISE_ADMIN, "users-order-by-col",
132                     orderByCol);
133                 prefs.setValue(
134                     PortletKeys.ENTERPRISE_ADMIN, "users-order-by-type",
135                     orderByType);
136             }
137             else {
138                 orderByCol = prefs.getValue(
139                     PortletKeys.ENTERPRISE_ADMIN, "users-order-by-col",
140                     "last-name");
141                 orderByType = prefs.getValue(
142                     PortletKeys.ENTERPRISE_ADMIN, "users-order-by-type", "asc");
143             }
144 
145             OrderByComparator orderByComparator =
146                 EnterpriseAdminUtil.getUserOrderByComparator(
147                     orderByCol, orderByType);
148 
149             setOrderableHeaders(orderableHeaders);
150             setOrderByCol(orderByCol);
151             setOrderByType(orderByType);
152             setOrderByComparator(orderByComparator);
153         }
154         catch (Exception e) {
155             _log.error(e);
156         }
157     }
158 
159     private static Log _log = LogFactoryUtil.getLog(UserSearch.class);
160 
161 }