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