1   /**
2    * Copyright (c) 2000-2010 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   *
12   * 
13   */
14  
15  package com.liferay.portlet.directory.util;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.search.SearchException;
20  import com.liferay.portal.kernel.xml.Document;
21  import com.liferay.portal.kernel.xml.Element;
22  import com.liferay.portal.model.User;
23  import com.liferay.portal.search.BaseOpenSearchImpl;
24  import com.liferay.portal.service.UserLocalServiceUtil;
25  import com.liferay.portal.theme.ThemeDisplay;
26  import com.liferay.portal.util.PortletKeys;
27  import com.liferay.portal.util.WebKeys;
28  import com.liferay.portal.util.comparator.UserLastNameComparator;
29  
30  import java.util.Date;
31  import java.util.List;
32  
33  import javax.portlet.PortletURL;
34  
35  import javax.servlet.http.HttpServletRequest;
36  
37  /**
38   * <a href="DirectoryOpenSearchImpl.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   */
42  public class DirectoryOpenSearchImpl extends BaseOpenSearchImpl {
43  
44      public static final String SEARCH_PATH = "/c/directory/open_search";
45  
46      public String search(
47              HttpServletRequest request, String keywords, int startPage,
48              int itemsPerPage)
49          throws SearchException {
50  
51          try {
52              return _search(request, keywords, startPage, itemsPerPage);
53          }
54          catch (Exception e) {
55              throw new SearchException(e);
56          }
57      }
58  
59      private String _search(
60              HttpServletRequest request, String keywords, int startPage,
61              int itemsPerPage)
62          throws Exception {
63  
64          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
65              WebKeys.THEME_DISPLAY);
66  
67          int start = (startPage * itemsPerPage) - itemsPerPage;
68          int end = startPage * itemsPerPage;
69  
70          List<User> results = UserLocalServiceUtil.search(
71              themeDisplay.getCompanyId(), keywords, Boolean.TRUE, null, start,
72              end, new UserLastNameComparator(true));
73  
74          int total = UserLocalServiceUtil.searchCount(
75              themeDisplay.getCompanyId(), keywords, Boolean.TRUE, null);
76  
77          Object[] values = addSearchResults(
78              keywords, startPage, itemsPerPage, total, start,
79              "Liferay Directory Search: " + keywords, SEARCH_PATH, themeDisplay);
80  
81          Document doc = (Document)values[0];
82          Element root = (Element)values[1];
83  
84          for (User user : results) {
85              String portletId = PortletKeys.DIRECTORY;
86  
87              //String portletTitle = PortalUtil.getPortletTitle(
88              //  portletId, themeDisplay.getUser());
89  
90              PortletURL portletURL = getPortletURL(
91                  request, portletId, themeDisplay.getScopeGroupId());
92  
93              portletURL.setParameter("struts_action", "/directory/view_user");
94              portletURL.setParameter(
95                  "p_u_i_d", String.valueOf(user.getUserId()));
96  
97              String title = user.getFullName();
98              String url = portletURL.toString();
99              Date modifedDate = user.getModifiedDate();
100             String content =
101                 user.getFullName() + " &lt;" + user.getEmailAddress() + "&gt;";
102             double score = 1.0;
103 
104             addSearchResult(root, title, url, modifedDate, content, score);
105         }
106 
107         if (_log.isDebugEnabled()) {
108             _log.debug("Return\n" + doc.asXML());
109         }
110 
111         return doc.asXML();
112     }
113 
114     private static Log _log = LogFactoryUtil.getLog(
115         DirectoryOpenSearchImpl.class);
116 
117 }