001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.usersadmin.search;
016    
017    import com.liferay.portal.kernel.dao.search.SearchContainer;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.JavaConstants;
021    import com.liferay.portal.kernel.util.OrderByComparator;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.kernel.workflow.WorkflowConstants;
025    import com.liferay.portal.model.User;
026    import com.liferay.portal.util.PortletKeys;
027    import com.liferay.portlet.PortalPreferences;
028    import com.liferay.portlet.PortletPreferencesFactoryUtil;
029    import com.liferay.portlet.usersadmin.util.UsersAdminUtil;
030    
031    import java.util.ArrayList;
032    import java.util.HashMap;
033    import java.util.List;
034    import java.util.Map;
035    
036    import javax.portlet.PortletConfig;
037    import javax.portlet.PortletRequest;
038    import javax.portlet.PortletURL;
039    
040    /**
041     * @author Brian Wing Shun Chan
042     */
043    public class UserSearch extends SearchContainer<User> {
044    
045            static List<String> headerNames = new ArrayList<String>();
046            static Map<String, String> orderableHeaders = new HashMap<String, String>();
047    
048            static {
049                    headerNames.add("first-name");
050                    headerNames.add("last-name");
051                    headerNames.add("screen-name");
052                    //headerNames.add("email-address");
053                    headerNames.add("job-title");
054                    headerNames.add("organizations");
055    
056                    orderableHeaders.put("first-name", "first-name");
057                    orderableHeaders.put("last-name", "last-name");
058                    orderableHeaders.put("screen-name", "screen-name");
059                    //orderableHeaders.put("email-address", "email-address");
060                    orderableHeaders.put("job-title", "job-title");
061            }
062    
063            public static final String EMPTY_RESULTS_MESSAGE = "no-users-were-found";
064    
065            public UserSearch(PortletRequest portletRequest, PortletURL iteratorURL) {
066                    this(portletRequest, DEFAULT_CUR_PARAM, iteratorURL);
067            }
068    
069            public UserSearch(
070                    PortletRequest portletRequest, String curParam,
071                    PortletURL iteratorURL) {
072    
073                    super(
074                            portletRequest, new UserDisplayTerms(portletRequest),
075                            new UserSearchTerms(portletRequest), curParam, DEFAULT_DELTA,
076                            iteratorURL, headerNames, EMPTY_RESULTS_MESSAGE);
077    
078                    PortletConfig portletConfig =
079                            (PortletConfig)portletRequest.getAttribute(
080                                    JavaConstants.JAVAX_PORTLET_CONFIG);
081    
082                    UserDisplayTerms displayTerms = (UserDisplayTerms)getDisplayTerms();
083                    UserSearchTerms searchTerms = (UserSearchTerms)getSearchTerms();
084    
085                    String portletName = portletConfig.getPortletName();
086    
087                    if (!portletName.equals(PortletKeys.USERS_ADMIN)) {
088                            displayTerms.setStatus(WorkflowConstants.STATUS_APPROVED);
089                            searchTerms.setStatus(WorkflowConstants.STATUS_APPROVED);
090                    }
091    
092                    iteratorURL.setParameter(
093                            UserDisplayTerms.STATUS, String.valueOf(displayTerms.getStatus()));
094    
095                    iteratorURL.setParameter(
096                            UserDisplayTerms.EMAIL_ADDRESS, displayTerms.getEmailAddress());
097                    iteratorURL.setParameter(
098                            UserDisplayTerms.FIRST_NAME, displayTerms.getFirstName());
099                    iteratorURL.setParameter(
100                            UserDisplayTerms.LAST_NAME, displayTerms.getLastName());
101                    iteratorURL.setParameter(
102                            UserDisplayTerms.MIDDLE_NAME, displayTerms.getMiddleName());
103                    iteratorURL.setParameter(
104                            UserDisplayTerms.ORGANIZATION_ID,
105                            String.valueOf(displayTerms.getOrganizationId()));
106                    iteratorURL.setParameter(
107                            UserDisplayTerms.ROLE_ID, String.valueOf(displayTerms.getRoleId()));
108                    iteratorURL.setParameter(
109                            UserDisplayTerms.SCREEN_NAME, displayTerms.getScreenName());
110                    iteratorURL.setParameter(
111                            UserDisplayTerms.USER_GROUP_ID,
112                            String.valueOf(displayTerms.getUserGroupId()));
113    
114                    try {
115                            PortalPreferences preferences =
116                                    PortletPreferencesFactoryUtil.getPortalPreferences(
117                                            portletRequest);
118    
119                            String orderByCol = ParamUtil.getString(
120                                    portletRequest, "orderByCol");
121                            String orderByType = ParamUtil.getString(
122                                    portletRequest, "orderByType");
123    
124                            if (Validator.isNotNull(orderByCol) &&
125                                    Validator.isNotNull(orderByType)) {
126    
127                                    preferences.setValue(
128                                            PortletKeys.USERS_ADMIN, "users-order-by-col", orderByCol);
129                                    preferences.setValue(
130                                            PortletKeys.USERS_ADMIN, "users-order-by-type",
131                                            orderByType);
132                            }
133                            else {
134                                    orderByCol = preferences.getValue(
135                                            PortletKeys.USERS_ADMIN, "users-order-by-col", "last-name");
136                                    orderByType = preferences.getValue(
137                                            PortletKeys.USERS_ADMIN, "users-order-by-type", "asc");
138                            }
139    
140                            OrderByComparator orderByComparator =
141                                    UsersAdminUtil.getUserOrderByComparator(
142                                            orderByCol, orderByType);
143    
144                            setOrderableHeaders(orderableHeaders);
145                            setOrderByCol(orderByCol);
146                            setOrderByType(orderByType);
147                            setOrderByComparator(orderByComparator);
148                    }
149                    catch (Exception e) {
150                            _log.error(e);
151                    }
152            }
153    
154            private static Log _log = LogFactoryUtil.getLog(UserSearch.class);
155    
156    }