001    /**
002     * Copyright (c) 2000-2010 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.enterpriseadmin.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.model.User;
025    import com.liferay.portal.util.PortletKeys;
026    import com.liferay.portlet.PortalPreferences;
027    import com.liferay.portlet.PortletPreferencesFactoryUtil;
028    import com.liferay.portlet.enterpriseadmin.util.EnterpriseAdminUtil;
029    
030    import java.util.ArrayList;
031    import java.util.HashMap;
032    import java.util.List;
033    import java.util.Map;
034    
035    import javax.portlet.PortletConfig;
036    import javax.portlet.PortletRequest;
037    import javax.portlet.PortletURL;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     */
042    public class UserSearch extends SearchContainer<User> {
043    
044            static List<String> headerNames = new ArrayList<String>();
045            static Map<String, String> orderableHeaders = new HashMap<String, String>();
046    
047            static {
048                    headerNames.add("first-name");
049                    headerNames.add("last-name");
050                    headerNames.add("screen-name");
051                    //headerNames.add("email-address");
052                    headerNames.add("job-title");
053                    headerNames.add("organizations");
054    
055                    orderableHeaders.put("first-name", "first-name");
056                    orderableHeaders.put("last-name", "last-name");
057                    orderableHeaders.put("screen-name", "screen-name");
058                    //orderableHeaders.put("email-address", "email-address");
059                    orderableHeaders.put("job-title", "job-title");
060            }
061    
062            public static final String EMPTY_RESULTS_MESSAGE = "no-users-were-found";
063    
064            public UserSearch(PortletRequest portletRequest, PortletURL iteratorURL) {
065                    super(
066                            portletRequest, new UserDisplayTerms(portletRequest),
067                            new UserSearchTerms(portletRequest), DEFAULT_CUR_PARAM,
068                            DEFAULT_DELTA, iteratorURL, headerNames, EMPTY_RESULTS_MESSAGE);
069    
070                    PortletConfig portletConfig =
071                            (PortletConfig)portletRequest.getAttribute(
072                                    JavaConstants.JAVAX_PORTLET_CONFIG);
073    
074                    UserDisplayTerms displayTerms = (UserDisplayTerms)getDisplayTerms();
075                    UserSearchTerms searchTerms = (UserSearchTerms)getSearchTerms();
076    
077                    String portletName = portletConfig.getPortletName();
078    
079                    if ((!portletName.equals(PortletKeys.ENTERPRISE_ADMIN)) &&
080                            (!portletName.equals(PortletKeys.ENTERPRISE_ADMIN_USERS)) &&
081                            (!portletName.equals(PortletKeys.ENTERPRISE_ADMIN_ORGANIZATIONS))) {
082    
083                            displayTerms.setActive(true);
084                            searchTerms.setActive(true);
085                    }
086    
087                    if (displayTerms.hasActive()) {
088                            iteratorURL.setParameter(
089                                    UserDisplayTerms.ACTIVE,
090                                    String.valueOf(displayTerms.isActive()));
091                    }
092    
093                    iteratorURL.setParameter(
094                            UserDisplayTerms.EMAIL_ADDRESS, displayTerms.getEmailAddress());
095                    iteratorURL.setParameter(
096                            UserDisplayTerms.FIRST_NAME, displayTerms.getFirstName());
097                    iteratorURL.setParameter(
098                            UserDisplayTerms.LAST_NAME, displayTerms.getLastName());
099                    iteratorURL.setParameter(
100                            UserDisplayTerms.MIDDLE_NAME, displayTerms.getMiddleName());
101                    iteratorURL.setParameter(
102                            UserDisplayTerms.ORGANIZATION_ID,
103                            String.valueOf(displayTerms.getOrganizationId()));
104                    iteratorURL.setParameter(
105                            UserDisplayTerms.ROLE_ID, String.valueOf(displayTerms.getRoleId()));
106                    iteratorURL.setParameter(
107                            UserDisplayTerms.SCREEN_NAME, displayTerms.getScreenName());
108                    iteratorURL.setParameter(
109                            UserDisplayTerms.USER_GROUP_ID,
110                            String.valueOf(displayTerms.getUserGroupId()));
111    
112                    try {
113                            PortalPreferences preferences =
114                                    PortletPreferencesFactoryUtil.getPortalPreferences(
115                                            portletRequest);
116    
117                            String orderByCol = ParamUtil.getString(
118                                    portletRequest, "orderByCol");
119                            String orderByType = ParamUtil.getString(
120                                    portletRequest, "orderByType");
121    
122                            if (Validator.isNotNull(orderByCol) &&
123                                    Validator.isNotNull(orderByType)) {
124    
125                                    preferences.setValue(
126                                            PortletKeys.ENTERPRISE_ADMIN, "users-order-by-col",
127                                            orderByCol);
128                                    preferences.setValue(
129                                            PortletKeys.ENTERPRISE_ADMIN, "users-order-by-type",
130                                            orderByType);
131                            }
132                            else {
133                                    orderByCol = preferences.getValue(
134                                            PortletKeys.ENTERPRISE_ADMIN, "users-order-by-col",
135                                            "last-name");
136                                    orderByType = preferences.getValue(
137                                            PortletKeys.ENTERPRISE_ADMIN, "users-order-by-type", "asc");
138                            }
139    
140                            OrderByComparator orderByComparator =
141                                    EnterpriseAdminUtil.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    }