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.ENTERPRISE_ADMIN_USERS)) &&
89              (!portletName.equals(PortletKeys.ENTERPRISE_ADMIN_ORGANIZATIONS))) {
90  
91              displayTerms.setActive(true);
92              searchTerms.setActive(true);
93          }
94  
95          iteratorURL.setParameter(
96              UserDisplayTerms.FIRST_NAME, displayTerms.getFirstName());
97          iteratorURL.setParameter(
98              UserDisplayTerms.MIDDLE_NAME, displayTerms.getMiddleName());
99          iteratorURL.setParameter(
100             UserDisplayTerms.LAST_NAME, displayTerms.getLastName());
101         iteratorURL.setParameter(
102             UserDisplayTerms.SCREEN_NAME, displayTerms.getScreenName());
103         iteratorURL.setParameter(
104             UserDisplayTerms.EMAIL_ADDRESS, displayTerms.getEmailAddress());
105 
106         if (displayTerms.hasActive()) {
107             iteratorURL.setParameter(
108                 UserDisplayTerms.ACTIVE,
109                 String.valueOf(displayTerms.isActive()));
110         }
111 
112         iteratorURL.setParameter(
113             UserDisplayTerms.ORGANIZATION_ID,
114             String.valueOf(displayTerms.getOrganizationId()));
115         iteratorURL.setParameter(
116             UserDisplayTerms.ROLE_ID, String.valueOf(displayTerms.getRoleId()));
117         iteratorURL.setParameter(
118             UserDisplayTerms.USER_GROUP_ID,
119             String.valueOf(displayTerms.getUserGroupId()));
120 
121         try {
122             PortalPreferences preferences =
123                 PortletPreferencesFactoryUtil.getPortalPreferences(
124                     portletRequest);
125 
126             String orderByCol = ParamUtil.getString(
127                 portletRequest, "orderByCol");
128             String orderByType = ParamUtil.getString(
129                 portletRequest, "orderByType");
130 
131             if (Validator.isNotNull(orderByCol) &&
132                 Validator.isNotNull(orderByType)) {
133 
134                 preferences.setValue(
135                     PortletKeys.ENTERPRISE_ADMIN, "users-order-by-col",
136                     orderByCol);
137                 preferences.setValue(
138                     PortletKeys.ENTERPRISE_ADMIN, "users-order-by-type",
139                     orderByType);
140             }
141             else {
142                 orderByCol = preferences.getValue(
143                     PortletKeys.ENTERPRISE_ADMIN, "users-order-by-col",
144                     "last-name");
145                 orderByType = preferences.getValue(
146                     PortletKeys.ENTERPRISE_ADMIN, "users-order-by-type", "asc");
147             }
148 
149             OrderByComparator orderByComparator =
150                 EnterpriseAdminUtil.getUserOrderByComparator(
151                     orderByCol, orderByType);
152 
153             setOrderableHeaders(orderableHeaders);
154             setOrderByCol(orderByCol);
155             setOrderByType(orderByType);
156             setOrderByComparator(orderByComparator);
157         }
158         catch (Exception e) {
159             _log.error(e);
160         }
161     }
162 
163     private static Log _log = LogFactoryUtil.getLog(UserSearch.class);
164 
165 }